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