OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
event_manager.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #include <string>
6 
7 #include "io/event_manager.h"
8 #include "base/logging.h"
9 #include "io/io_log.h"
10 
11 using boost::asio::io_context;
12 
14 
15 EventManager::EventManager() : shutdown_(false), running_(false) {
16 }
17 
19  shutdown_ = true;
20  io_service_.stop();
21 }
22 
24  Lock();
25  io_context::work work(io_service_);
26  do {
27  if (shutdown_) break;
28  boost::system::error_code ec;
29  try {
30  io_service_.run(ec);
31  if (ec) {
32  EVENT_MANAGER_LOG_ERROR("io_service run failed: " <<
33  ec.message());
34  break;
35  }
36  } catch (std::exception &except) {
37  static std::string what = except.what();
38  EVENT_MANAGER_LOG_ERROR("Exception caught in io_service run : " <<
39  what);
40  assert(false);
41  } catch (...) {
42  EVENT_MANAGER_LOG_ERROR("Exception caught in io_service run : "
43  "bailing out");
44  assert(false);
45  }
46  } while (true);
47  Unlock();
48 }
49 
51  Lock();
52  if (shutdown_) {
53  Unlock();
54  return 0;
55  }
56  boost::system::error_code err;
57  size_t res = io_service_.run_one(err);
58  if (res == 0)
59  io_service_.reset();
60  Unlock();
61  return res;
62 }
63 
65  Lock();
66  if (shutdown_) {
67  Unlock();
68  return 0;
69  }
70  boost::system::error_code err;
71  size_t res = io_service_.poll(err);
72  if (res == 0)
73  io_service_.reset();
74  Unlock();
75  return res;
76 }
77 
79  return running_;
80 }
81 
83  tbb::spin_mutex::scoped_lock lock(guard_running_);
84  assert(io_mutex_.try_lock());
85  running_ = true;
86 }
87 
89  tbb::spin_mutex::scoped_lock lock(guard_running_);
90  io_mutex_.unlock();
91  running_ = false;
92 }
SandeshTraceBufferPtr IOTraceBuf
bool IsRunning() const
size_t RunOnce()
boost::shared_ptr< TraceBuffer< SandeshTrace > > SandeshTraceBufferPtr
Definition: sandesh_trace.h:18
boost::asio::io_context io_service_
Definition: event_manager.h:51
#define EVENT_MANAGER_LOG_ERROR(arg)
Definition: io_log.h:136
size_t Poll()
tbb::spin_mutex io_mutex_
Definition: event_manager.h:53
#define IO_TRACE_BUF
Definition: io_log.h:17
tbb::spin_mutex guard_running_
Definition: event_manager.h:55
SandeshTraceBufferPtr SandeshTraceBufferCreate(const std::string &buf_name, size_t buf_size, bool trace_enable=true)
Definition: sandesh_trace.h:46