-
Notifications
You must be signed in to change notification settings - Fork 39
/
SchedulerBase.cpp
executable file
·712 lines (570 loc) · 16.6 KB
/
SchedulerBase.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
/**************************************************************
* Copyright (c) 2010-2013, Dynamic Network Services, Inc.
* Jake Montgomery ([email protected]) & Tom Daly ([email protected])
* Distributed under the FreeBSD License - see LICENSE
***************************************************************/
#include "common.h"
#include "SchedulerBase.h"
#include "utils.h"
#include "SmartPointer.h"
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
using namespace std;
/**
* A timer class for use with SchedulerBase
*
*/
class TimerImpl : public Timer
{
private:
SchedulerBase *m_scheduler;
SchedulerBase::timer_set *m_activeTimers; // Use only from main thread. The active timer list that this is part of when active.
Timer::Callback m_callback;
void *m_userdata;
TimeSpec m_expireTime;
TimeSpec m_startTime; // only valid when not stopped.
bool m_stopped;
char *m_name; // for logging
Timer::Priority::Value m_priority;
public:
TimerImpl(SchedulerBase &scheduler, SchedulerBase::timer_set *timerSet, const char *name) : Timer(),
m_scheduler(&scheduler),
m_activeTimers(timerSet),
m_callback(NULL),
m_userdata(NULL),
m_stopped(true),
m_name(NULL),
m_priority(Timer::Priority::Hi)
{
if (name)
m_name = strdup(name);
else
{
char *tempName = (char *)malloc(32);
snprintf(tempName, 32, "%p", this);
m_name = tempName;
}
m_expireTime.tv_sec = 0;
m_expireTime.tv_nsec = 0;
};
~TimerImpl()
{
// This will remove it from the active list.
Stop();
if (m_name)
free(m_name);
};
void SetCallback(Timer::Callback callback, void *userdata)
{
LogAssert(m_scheduler->IsMainThread());
m_callback = callback;
m_userdata = userdata;
}
void Stop()
{
LogAssert(m_scheduler->IsMainThread());
if (m_stopped)
LogOptional(Log::TimerDetail, "Stopping ignored on stopped timer %s", m_name);
else
{
// Remove us from active timers list. (Must remove before changing timer.)
SchedulerBase::timer_set_it found = SchedulerBase::TimeSetFindExact(*m_activeTimers, this);
if (found != m_activeTimers->end())
m_activeTimers->erase(found);
m_stopped = true;
LogOptional(Log::TimerDetail, "Stopping timer %s. (%zu timers)", m_name, m_activeTimers->size());
}
}
bool SetMsTimer(uint32_t ms)
{
// no check for overflow. But that would be a lot of years ;-)
return SetMicroTimer(uint64_t(ms) * 1000);
}
bool SetMicroTimer(uint64_t micro)
{
LogAssert(m_scheduler->IsMainThread());
TimeSpec startTime(TimeSpec::MonoNow());
if (startTime.empty())
return false;
return setExpireTime(startTime, micro);
}
bool UpdateMicroTimer(uint64_t micro)
{
LogAssert(m_scheduler->IsMainThread());
if (IsStopped())
return SetMicroTimer(micro);
return setExpireTime(m_startTime, micro);
}
bool IsStopped() const
{
LogAssert(m_scheduler->IsMainThread());
return m_stopped;
}
void SetPriority(Timer::Priority::Value pri)
{
LogAssert(m_scheduler->IsMainThread());
m_priority = pri;
}
Timer::Priority::Value GetPriority()
{
LogAssert(m_scheduler->IsMainThread());
return m_priority;
}
/**
* Get the time that this will expire.
*
* @note Call only on main thread. See IsMainThread().
*
* @return const struct TimeSpec&
*/
const TimeSpec& GetExpireTime() const
{
return m_expireTime;
}
/**
* Called by the Scheduler to mark the timer as stopped and run its action. Will
* Remove the timer from the active list.
*
*/
void ExpireTimer()
{
LogAssert(!m_stopped);
Stop();
LogOptional(Log::TimerDetail, "Expired timer %s calling callback", m_name);
m_callback(this, m_userdata);
}
/**
* Gets the name
*
* @return const char*
*/
const char* Name()
{
return m_name;
}
private:
/**
* Changes the start and expire time for the timer.
*
* @param startTime - The time of the last timer start. May be m_startTime.
* @param micro - Time to expire from startTime in microseconds.
*
* @return bool - false on failure.
*/
bool setExpireTime(const struct timespec &startTime, uint64_t micro)
{
bool expireChange, startChange;
TimeSpec expireTime(startTime);
expireTime += TimeSpec(TimeSpec::Microsec, micro);
startChange = (m_startTime != startTime);
expireChange = (m_stopped || m_expireTime != expireTime);
//LogOptional(Log::Temp, "Timer %s before change %zu items",m_name, m_activeTimers->size());
if (!expireChange && !startChange)
{
LogOptional(Log::TimerDetail, "Timer %s no change. %" PRIu64 " microseconds. Expires:%jd:%09ld", m_name, micro, (intmax_t)expireTime.tv_sec, expireTime.tv_nsec);
return true;
}
LogOptional(Log::TimerDetail, "%s timer %s for %" PRIu64 " microseconds from %jd:%09ld. Expires:%jd:%09ld",
m_stopped ? "Starting" : startChange ? "Resetting" : "Advancing",
m_name,
micro,
(intmax_t)startTime.tv_sec,
startTime.tv_nsec,
(intmax_t)expireTime.tv_sec,
expireTime.tv_nsec
);
// Start time does not effect sorting in active timer list, so we can set it now.
// Stopped also should not matter.
if (startChange)
m_startTime = startTime;
m_stopped = false;
if (expireChange)
{
SchedulerBase::timer_set_it found = SchedulerBase::TimeSetFindExact(*m_activeTimers, this);
if (found != m_activeTimers->end())
{
if (!willTimerBeSorted(found, expireTime))
{
m_activeTimers->erase(found);
found = m_activeTimers->end(); // cause it to be added back
}
}
// Actually set the time
m_expireTime = expireTime;
if (found == m_activeTimers->end())
{
try
{
m_activeTimers->insert(this);
}
catch (std::exception &e)
{
m_stopped = true;
gLog.Message(Log::Error, "Failed to add timer: %s.", e.what());
return false;
}
}
}
//LogOptional(Log::Temp, "Timer %s after change %zu items",m_name, m_activeTimers->size());
return true;
}
/**
* Checks if the timer item is still in proper order.
*
* @param item [in] - an iterator to the item to check in m_activeTimers-> Must
* be a valid item (not end())
* @param expireTime [in] - The proposed new expire time.
*
* @return bool - true if the item is would still be in sorted order.
*/
bool willTimerBeSorted(const SchedulerBase::timer_set_it item, const struct timespec &expireTime)
{
SchedulerBase::timer_set_it temp;
if (item != m_activeTimers->begin())
{
// We want the timer to be later than, or equal to, the previous item
if (0 > timespecCompare(expireTime, (*(--(temp = item)))->GetExpireTime()))
return false;
}
(temp = item)++;
if (temp != m_activeTimers->end())
{
// we want the timer to be earlier or equal to the next item.
if (0 < timespecCompare(expireTime, (*temp)->GetExpireTime()))
return false;
}
return true;
}
};
SchedulerBase::SchedulerBase() : Scheduler(),
m_isStarted(false),
m_wantsShutdown(false),
m_activeTimers(compareTimers),
m_timerCount(0)
{
m_mainThread = pthread_self();
}
SchedulerBase::~SchedulerBase()
{
for (SignalItemHashMap::iterator sig = m_signals.begin(); sig != m_signals.end(); sig++)
{
::close(sig->second.fdRead);
::close(sig->second.fdWrite);
}
}
bool SchedulerBase::Run()
{
uint32_t iter = 0;
TimeSpec timeout;
TimeSpec immediate;
bool gotEvents;
if (!LogVerify(IsMainThread()))
return false;
m_isStarted = true;
// Start with a quick event check.
timeout = immediate;
while (true)
{
iter++;
if (m_wantsShutdown)
break;
//
// Get event, or timeout.
//
gLog.Optional(Log::TimerDetail, "checking events (%u)", iter);
gotEvents = waitForEvents(timeout);
// By default the next event check is immediately.
timeout = immediate;
//
// High priority timers, if any, get handled now
//
while (!m_wantsShutdown && expireTimer(Timer::Priority::Hi))
{ //nothing
}
if (m_wantsShutdown)
break;
//
// Handle any events.
//
if (gotEvents)
{
int socketId;
gLog.Optional(Log::TimerDetail, "Handling events (%u)", iter);
while (-1 != (socketId = getNextSocketEvent()))
{
// we have a socket event .. is it a socket or a signal?
SocketItemHashMap::iterator foundSocket;
SignalItemHashMap::iterator foundSignal;
if (m_sockets.end() != (foundSocket = m_sockets.find(socketId)))
{
if (LogVerify(foundSocket->second.callback != NULL))
foundSocket->second.callback(socketId, foundSocket->second.userdata);
}
else if (m_signals.end() != (foundSignal = m_signals.find(socketId)))
{
if (LogVerify(foundSignal->second.callback != NULL))
{
// 'Drain' the pipe.
char drain[128];
int result;
size_t reads = 0;
while (0 < (result = ::read(socketId, drain, sizeof(drain))))
reads++;
if (reads == 0 && result < 0)
gLog.LogError("Failed to read from pipe %d: %s", socketId, ErrnoToString());
else if (result == 0)
gLog.LogError("Signaling pipe write end for %d closed", socketId);
foundSignal->second.callback(foundSignal->second.fdWrite, foundSignal->second.userdata);
}
}
else
{
gLog.Optional(Log::TimerDetail, "Socket (%d) signaled with no handler (%u).", socketId, iter);
}
if (m_wantsShutdown)
break;
}
if (m_wantsShutdown)
break;
}
//
// Handle a low priority timer if there are no events.
// TODO: starvation is a potential problem for low priority timers.
//
if (!gotEvents && !expireTimer(Timer::Priority::Low))
{
// No events and no more timers, so we are ready to sleep again.
timeout = getNextTimerTimeout();
}
if (m_wantsShutdown)
break;
} // while true
return true;
}
/**
* Helper for Run().
*
* Gets the timeout period between now and the next timer.
*
* @return - The timeout value
*/
TimeSpec SchedulerBase::getNextTimerTimeout()
{
//
// Calculate next scheduled timer time.
//
if (m_activeTimers.empty())
{
// Just for laughs ... and because we do not run on low power machines, wake up
// every few seconds.
return TimeSpec(3, 0);
}
TimeSpec now(TimeSpec::MonoNow());
if (now.empty())
return TimeSpec(TimeSpec::Millisec, 200); // 200 ms?
TimeSpec result = (*m_activeTimers.begin())->GetExpireTime() - now;
if (result.IsNegative())
return TimeSpec();
return result;
}
/**
*
* Helper for Run().
* Expires the next timer with priority of minPri or higher.
*
* @return - false if there are no more timers.
*/
bool SchedulerBase::expireTimer(Timer::Priority::Value minPri)
{
TimeSpec now(TimeSpec::MonoNow());
if (now.empty())
return false;
for (timer_set_it nextTimer = m_activeTimers.begin(); nextTimer != m_activeTimers.end(); nextTimer++)
{
TimerImpl *timer = *nextTimer;
if (0 < timespecCompare(timer->GetExpireTime(), now))
return false; // non-expired timer ... we are done!
if (timer->GetPriority() >= minPri)
{
#ifdef BFD_TEST_TIMERS
TimeSpec dif = now - timer->GetExpireTime();
gLog.Optional(Log::Temp, "Timer %s is off by %.4f ms",
timer->Name(),
timespecToSeconds(dif) * 1000.0);
#endif
// Expire the timer, which will run the action.
// Note that the action could also modify the m_activeTimers list.
timer->ExpireTimer();
return true;
}
}
return false;
}
bool SchedulerBase::IsMainThread()
{
return(bool)pthread_equal(m_mainThread, pthread_self());
}
bool SchedulerBase::SetSocketCallback(int socket, Scheduler::SocketCallback callback, void *userdata)
{
LogAssert(IsMainThread());
if (!LogVerify(callback) || !LogVerify(socket != -1))
return false;
if (!watchSocket(socket))
return false;
schedulerSocketItem item;
item.callback = callback;
item.userdata = userdata;
item.socket = socket;
m_sockets[socket] = item;
return true;
}
void SchedulerBase::RemoveSocketCallback(int socket)
{
LogAssert(IsMainThread());
SocketItemHashMap::iterator foundSocket;
foundSocket = m_sockets.find(socket);
if (foundSocket == m_sockets.end())
{
gLog.LogError("RemoveSocketCallback called with unknown socket %d", socket);
return;
}
m_sockets.erase(foundSocket);
unWatchSocket(socket);
}
bool SchedulerBase::CreateSignalChannel(int *outSigId, SignalCallback callback, void *userdata)
{
LogAssert(IsMainThread());
if (!LogVerify(outSigId) || !LogVerify(callback))
return false;
*outSigId = -1;
// Create a set of pipes
int fdPipe[2];
int flags;
int ret = pipe(fdPipe);
if (ret != 0)
{
gLog.ErrnoError(errno, "Unable to create pipe for signaling");
return false;
}
FileDescriptor pipeRead(fdPipe[0]);
FileDescriptor pipeWrite(fdPipe[1]);
flags = fcntl(pipeRead, F_GETFL);
flags = flags | O_NONBLOCK;
if (-1 == fcntl(pipeRead, F_SETFL, flags))
{
gLog.LogError("Failed to set read pipe to non-blocking.");
return false;
}
flags = fcntl(pipeWrite, F_GETFL);
flags = flags | O_NONBLOCK;
if (-1 == fcntl(pipeWrite, F_SETFL, flags))
{
gLog.LogError("Failed to set write pipe to non-blocking.");
return false;
}
if (!watchSocket(pipeRead))
return false;
schedulerSignalItem item;
item.callback = callback;
item.userdata = userdata;
item.fdWrite = pipeWrite;
item.fdRead = pipeRead;
m_signals[item.fdRead] = item;
pipeWrite.Detach();
pipeRead.Detach();
*outSigId = item.fdWrite;
gLog.Optional(Log::TimerDetail, "Created signal channel from %d to %d .", item.fdWrite, item.fdRead);
return true;
}
bool SchedulerBase::Signal(int sigId)
{
char sig = 'x';
if (1 != ::write(sigId, &sig, 1))
{
gLog.LogError("Failed to signal on pipe %d: %s", sigId, ErrnoToString());
return false;
}
return true;
}
void SchedulerBase::RemoveSignalChannel(int sigId)
{
LogAssert(IsMainThread());
SignalItemHashMap::iterator foundSignal;
for (SignalItemHashMap::iterator sig = m_signals.begin(); sig != m_signals.end(); sig++)
{
if (sig->second.fdWrite == sigId)
{
int readPipe = sig->second.fdRead;
int writePipe = sig->second.fdWrite;
m_signals.erase(sig);
unWatchSocket(readPipe);
::close(readPipe);
::close(writePipe);
return;
}
}
gLog.LogError("RemoveSignalChannel called with unknown signal %d", sigId);
}
void SchedulerBase::RequestShutdown()
{
LogAssert(IsMainThread());
m_wantsShutdown = true;
}
Timer* SchedulerBase::MakeTimer(const char *name)
{
m_timerCount++;
return new TimerImpl(*this, &m_activeTimers, name);
}
/**
* Call when completely done with a timer.
*
* @param timer
*/
void SchedulerBase::FreeTimer(Timer *timer)
{
if (timer)
{
TimerImpl *theTimer = static_cast<TimerImpl *>(timer);
m_timerCount--;
delete theTimer;
}
}
// Return true if lhs should be before rhs.
bool SchedulerBase::compareTimers(const TimerImpl *lhs, const TimerImpl *rhs)
{
// Stopped timers should not be in the list.
LogAssert(!lhs->IsStopped() && !rhs->IsStopped());
// We want the "earliest" timers first. So we return true if lhs is earlier than
// rhs. -1 if left is earlier.
return (0 > timespecCompare(lhs->GetExpireTime(), rhs->GetExpireTime()));
}
/**
* Finds a TimerImpl in the timer_set.
*
* Note that timer_set::find() would return any timer that matches, which is
* defined as matching expire time. This function will **only** return an
* iterator that points to the specific TimerImpl provided.
*
*
* @param timerSet [in] - The timer set to search.
* @param target [in] - The object to search for.
*
* @return SchedulerBase::timer_set_it - An iterator to the timer, or
* timerSet.end() if no match was found.
*
*/
SchedulerBase::timer_set_it SchedulerBase::TimeSetFindExact(SchedulerBase::timer_set &timerSet,
TimerImpl *target)
{
std::pair<timer_set_it, timer_set_it> range;
range = timerSet.equal_range(target);
for (timer_set_it it = range.first; it != range.second; ++it)
{
// Compare as pointers
if (target == *it)
return it;
}
return timerSet.end();
}