![]() |
RTBKit
0.9
Open-source framework to create real-time ad bidding systems.
|
00001 /* async_event_source.h -*- C++ -*- 00002 Jeremy Barnes, 9 November 2012 00003 Copyright (c) 2012 Datacratic Inc. All rights reserved. 00004 00005 Source of asynchronous events; a bit like a reactor. 00006 */ 00007 00008 #ifndef __service__async_event_source_h__ 00009 #define __service__async_event_source_h__ 00010 00011 00012 #include <boost/thread/thread.hpp> 00013 #include <functional> 00014 #include "jml/arch/exception.h" 00015 #include <thread> 00016 00017 namespace Datacratic { 00018 00019 struct MessageLoop; 00020 00021 /*****************************************************************************/ 00022 /* ASYNC EVENT SOURCE */ 00023 /*****************************************************************************/ 00024 00025 struct AsyncEventSource { 00026 00027 AsyncEventSource() 00028 : needsPoll(false), debug_(false), parent_(0) 00029 { 00030 } 00031 00032 virtual ~AsyncEventSource() 00033 { 00034 disconnect(); 00035 } 00036 00045 virtual int selectFd() const 00046 { 00047 return -1; 00048 } 00049 00053 virtual bool poll() const 00054 { 00055 return false; 00056 } 00057 00064 virtual bool processOne() = 0; 00065 00069 virtual bool singleThreaded() const 00070 { 00071 return true; 00072 } 00073 00075 virtual void debug(bool debugOn) 00076 { 00077 debug_ = debugOn; 00078 } 00079 00081 virtual void connected(MessageLoop * parent) 00082 { 00083 if (parent_) 00084 throw ML::Exception("attempt to connect AsyncEventSource " 00085 "to two parents"); 00086 parent_ = parent; 00087 } 00088 00090 void disconnect(); 00091 00095 bool needsPoll; 00096 00098 bool debug_; 00099 00101 MessageLoop * parent_; 00102 }; 00103 00104 00105 /*****************************************************************************/ 00106 /* MULTIPLE EVENT SOURCE */ 00107 /*****************************************************************************/ 00108 00111 // ... 00112 00113 /*****************************************************************************/ 00114 /* PERIODIC EVENT SOURCE */ 00115 /*****************************************************************************/ 00116 00117 struct PeriodicEventSource : public AsyncEventSource { 00118 PeriodicEventSource(); 00119 00120 PeriodicEventSource(double timePeriodSeconds, 00121 std::function<void (uint64_t)> onTimeout, 00122 bool singleThreaded = true); 00123 00124 ~PeriodicEventSource(); 00125 00126 void init(double timePeriodSeconds, 00127 std::function<void (uint64_t)> onTimeout, 00128 bool singleThreaded = true); 00129 00130 virtual int selectFd() const; 00131 00132 virtual bool processOne(); 00133 00134 virtual bool singleThreaded() const 00135 { 00136 return singleThreaded_; 00137 } 00138 00139 private: 00140 int timerFd; 00141 double timePeriodSeconds; 00142 std::function<void (uint64_t)> onTimeout; 00143 bool singleThreaded_; 00144 }; 00145 00146 00147 #if 0 00148 /*****************************************************************************/ 00149 /* WAKEUP EVENT SOURCE */ 00150 /*****************************************************************************/ 00151 00152 struct WakeupEventSource : public AsyncEventSource { 00153 WakeupEventSource(); 00154 00155 virtual int selectFd() const; 00156 00162 virtual bool processOne(); 00163 00164 private: 00165 ML::Wakeup_Fd wakeup; 00166 }; 00167 #endif 00168 00169 } // namespace Datacratic 00170 00171 00172 #endif /* __service__async_event_source_h__ */