Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : * Copyright Contributors to the OpenSDN project 4 : */ 5 : 6 : #ifndef SRC_IO_EVENT_MANAGER_H_ 7 : #define SRC_IO_EVENT_MANAGER_H_ 8 : 9 : #pragma once 10 : 11 : #include <tbb/spin_mutex.h> 12 : #include <boost/asio/io_service.hpp> 13 : 14 : #include "base/util.h" 15 : 16 : // 17 : // Wrapper around boost::io_service. 18 : // 19 : // The mutex and the related assertions in Run, RunOnce and Poll are used 20 : // to detect the case where multiple threads are running the EventManager. 21 : // This typically happens in unit tests that inadvertently call RunOnce or 22 : // Poll directly or indirectly after having started a ServerThread (which 23 : // calls Run). 24 : // 25 : class EventManager { 26 : public: 27 : EventManager(); 28 : 29 : // Run until shutdown. 30 : void Run(); 31 : 32 : // Run at most once. 33 : size_t RunOnce(); 34 : 35 : // Run all ready handlers, without blocking. 36 : size_t Poll(); 37 : 38 : void Shutdown(); 39 : 40 : // Information whether mutex is already acquired 41 : bool IsRunning() const; 42 : 43 435341 : boost::asio::io_context *io_service() { return &io_service_; } 44 : 45 : private: 46 : // Atomic mutex lock operation and changing the running_ flag 47 : void Lock(); 48 : 49 : // Atomic mutex unlock operation and changing the running_ flag 50 : void Unlock(); 51 : 52 : boost::asio::io_context io_service_; 53 : bool shutdown_; 54 : tbb::spin_mutex io_mutex_; 55 : bool running_; 56 : tbb::spin_mutex guard_running_; 57 : 58 : DISALLOW_COPY_AND_ASSIGN(EventManager); 59 : }; 60 : 61 : #endif // SRC_IO_EVENT_MANAGER_H_