delete timeout;
}
- while (!noWaitLoopCallbacks_.empty()) {
- delete &noWaitLoopCallbacks_.front();
+ while (!runBeforeLoopCallbacks_.empty()) {
+ delete &runBeforeLoopCallbacks_.front();
}
(void) runLoopCallbacks(false);
while (!stop_) {
++nextLoopCnt_;
+ // Run the before loop callbacks
+ LoopCallbackList callbacks;
+ callbacks.swap(runBeforeLoopCallbacks_);
+
+ while(!callbacks.empty()) {
+ auto* item = &callbacks.front();
+ callbacks.pop_front();
+ item->runLoopCallback();
+ }
+
// nobody can add loop callbacks from within this thread if
// we don't have to handle anything to start with...
if (blocking && loopCallbacks_.empty()) {
- LoopCallbackList callbacks;
- callbacks.swap(noWaitLoopCallbacks_);
-
- while(!callbacks.empty()) {
- auto* item = &callbacks.front();
- callbacks.pop_front();
- item->runLoopCallback();
- }
-
res = event_base_loop(evb_, EVLOOP_ONCE);
} else {
res = event_base_loop(evb_, EVLOOP_ONCE | EVLOOP_NONBLOCK);
void EventBase::runBeforeLoop(LoopCallback* callback) {
DCHECK(isInEventBaseThread());
callback->cancelLoopCallback();
- noWaitLoopCallbacks_.push_back(*callback);
+ runBeforeLoopCallbacks_.push_back(*callback);
}
bool EventBase::runInEventBaseThread(void (*fn)(void*), void* arg) {
*/
void runOnDestruction(LoopCallback* callback);
+ /**
+ * Adds a callback that will run immediately *before* the event loop.
+ * This is very similar to runInLoop(), but will not cause the loop to break:
+ * For example, this callback could be used to get loop times.
+ */
void runBeforeLoop(LoopCallback* callback);
/**
CobTimeout::List pendingCobTimeouts_;
LoopCallbackList loopCallbacks_;
- LoopCallbackList noWaitLoopCallbacks_;
+ LoopCallbackList runBeforeLoopCallbacks_;
LoopCallbackList onDestructionCallbacks_;
// This will be null most of the time, but point to currentCallbacks
ASSERT_EQ(0, strcmp("foo", name));
#endif
}
+
+TEST(TEventBaseTest, RunBeforeLoop) {
+ TEventBase base;
+ CountedLoopCallback cb(&base, 1, [&](){
+ base.terminateLoopSoon();
+ });
+ base.runBeforeLoop(&cb);
+ base.loopForever();
+ ASSERT_EQUAL(cb.getCount(), 0);
+}
+
+TEST(TEventBaseTest, RunBeforeLoopWait) {
+ TEventBase base;
+ CountedLoopCallback cb(&base, 1);
+ base.runAfterDelay([&](){
+ base.terminateLoopSoon();
+ }, 500);
+ base.runBeforeLoop(&cb);
+ base.loopForever();
+
+ // Check that we only ran once, and did not loop multiple times.
+ ASSERT_EQUAL(cb.getCount(), 0);
+}