summaryrefslogtreecommitdiffstats
path: root/src/timekeeper.cpp
blob: 658e5b663066f5c46a6677290d4bcdb881d5d6f4 (plain)
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
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
/*
    Copyright (C) 2005-2009  Michel de Boer <michel@twinklephone.com>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

#include <assert.h>
#include <sys/time.h>
#include <iostream>
#include <signal.h>
#include "events.h"
#include "line.h"
#include "log.h"
#include "phone.h"
#include "subscription.h"
#include "timekeeper.h"
#include "transaction_mgr.h"
#include "threads/thread.h"
#include "audits/memman.h"

extern t_phone		*phone;
extern t_event_queue	*evq_trans_layer;
extern t_event_queue	*evq_trans_mgr;
extern t_event_queue	*evq_timekeeper;
extern t_timekeeper	*timekeeper;
extern bool		threading_is_LinuxThreads;

string timer_type2str(t_timer_type t) {
	switch(t) {
	case TMR_TRANSACTION:	return "TMR_TRANSACTION";
	case TMR_PHONE:		return "TMR_PHONE";
	case TMR_LINE:		return "TMR_LINE";
	case TMR_SUBSCRIBE:	return "TMR_SUBSCRIBE";
	case TMR_PUBLISH:	return "TMR_PUBLISH";
	case TMR_STUN_TRANSACTION: return "TMR_STUN_TRANSACTION";
	}

	return "UNKNOWN";
}

///////////////////////////////////////////////////////////
// class t_timer
///////////////////////////////////////////////////////////

t_timer::t_timer(long dur) : t_id_object() {
	long d = dur;
	
	// HACK: if a timer is set to zero seconds, set it to 1 ms, otherwise
	//       the timer will not expire.
	if (dur == 0) d++;

	duration = d;
	relative_duration = d;
}

long t_timer::get_duration(void) const {
	return duration;
}

long t_timer::get_relative_duration(void) const {
	return relative_duration;
}

void t_timer::set_relative_duration(long d) {
	relative_duration = d;
}

///////////////////////////////////////////////////////////
// class t_tmr_transaction
///////////////////////////////////////////////////////////

t_tmr_transaction::t_tmr_transaction(long dur, t_sip_timer tmr,
	unsigned short tid) : t_timer(dur)
{
	sip_timer = tmr;
	transaction_id = tid;
}

void t_tmr_transaction::expired(void) {
	// Create a timeout event for the transaction manager
	evq_trans_mgr->push_timeout(this);
}

t_timer *t_tmr_transaction::copy(void) const {
	t_tmr_transaction *t = new t_tmr_transaction(*this);
	MEMMAN_NEW(t);
	return t;
}

t_timer_type t_tmr_transaction::get_type(void) const {
	return TMR_TRANSACTION;
}

unsigned short t_tmr_transaction::get_tid(void) const {
	return transaction_id;
}

t_sip_timer t_tmr_transaction::get_sip_timer(void) const {
	return sip_timer;
}

string t_tmr_transaction::get_name(void) const {
	switch(sip_timer) {
	case TIMER_T1:	return "TIMER_T1";
	case TIMER_T2:	return "TIMER_T2";
	case TIMER_T4:	return "TIMER_T4";
	case TIMER_A:	return "TIMER_A";
	case TIMER_B:	return "TIMER_B";
	case TIMER_C:	return "TIMER_C";
	case TIMER_D:	return "TIMER_D";
	case TIMER_E:	return "TIMER_E";
	case TIMER_F:	return "TIMER_F";
	case TIMER_G:	return "TIMER_G";
	case TIMER_H:	return "TIMER_H";
	case TIMER_I:	return "TIMER_I";
	case TIMER_J:	return "TIMER_J";
	case TIMER_K:	return "TIMER_K";
	}

	return "UNKNOWN";
}

///////////////////////////////////////////////////////////
// class t_tmr_phone
///////////////////////////////////////////////////////////
t_tmr_phone::t_tmr_phone(long dur, t_phone_timer ptmr, t_phone *p) : t_timer(dur)
{
	phone_timer = ptmr;
	the_phone = p;
}

void t_tmr_phone::expired(void) {
	evq_trans_layer->push_timeout(this);
}

t_timer *t_tmr_phone::copy(void) const {
	t_tmr_phone *t = new t_tmr_phone(*this);
	MEMMAN_NEW(t);
	return t;
}

t_timer_type t_tmr_phone::get_type(void) const {
	return TMR_PHONE;
}

t_phone_timer t_tmr_phone::get_phone_timer(void) const {
	return phone_timer;
}

t_phone *t_tmr_phone::get_phone(void) const {
	return the_phone;
}

string t_tmr_phone::get_name(void) const {
	switch(phone_timer) {
	case PTMR_REGISTRATION:		return "PTMR_REGISTRATION";
	case PTMR_NAT_KEEPALIVE: 	return "PTMR_NAT_KEEPALIVE";
	case PTMR_TCP_PING:		return "PTMR_TCP_PING";
	}

	return "UNKNOWN";
}

///////////////////////////////////////////////////////////
// class t_tmr_line
///////////////////////////////////////////////////////////
t_tmr_line::t_tmr_line(long dur, t_line_timer ltmr, t_object_id lid,
				t_object_id d) : t_timer(dur)
{
	line_timer = ltmr;
	line_id = lid;
	dialog_id = d;
}

void t_tmr_line::expired(void) {
	evq_trans_layer->push_timeout(this);
}

t_timer *t_tmr_line::copy(void) const {
	t_tmr_line *t = new t_tmr_line(*this);
	MEMMAN_NEW(t);
	return t;
}

t_timer_type t_tmr_line::get_type(void) const {
	return TMR_LINE;
}

t_line_timer t_tmr_line::get_line_timer(void) const {
	return line_timer;
}

t_object_id t_tmr_line::get_line_id(void) const {
	return line_id;
}

t_object_id t_tmr_line::get_dialog_id(void) const {
	return dialog_id;
}

string t_tmr_line::get_name(void) const {
	switch(line_timer) {
	case LTMR_ACK_TIMEOUT:		return "LTMR_ACK_TIMEOUT";
	case LTMR_ACK_GUARD:		return "LTMR_ACK_GUARD";
	case LTMR_INVITE_COMP:		return "LTMR_INVITE_COMP";
	case LTMR_NO_ANSWER:		return "LTMR_NO_ANSWER";
	case LTMR_RE_INVITE_GUARD:	return "LTMR_RE_INVITE_GUARD";
	case LTMR_100REL_TIMEOUT:	return "LTMR_100REL_TIMEOUT";
	case LTMR_100REL_GUARD:		return "LTMR_100REL_GUARD";
	case LTMR_CANCEL_GUARD:		return "LTMR_CANCEL_GUARD";
	case LTMR_GLARE_RETRY:		return "LTMR_GLARE_RETRY";
	}

	return "UNKNOWN";
}

///////////////////////////////////////////////////////////
// class t_tmr_subscribe
///////////////////////////////////////////////////////////
t_tmr_subscribe::t_tmr_subscribe(long dur, t_subscribe_timer stmr,
		t_object_id lid, t_object_id d, const string &event_type,
		const string &event_id) : t_timer(dur)
{
	subscribe_timer = stmr;
	line_id = lid;
	dialog_id = d;
	sub_event_type = event_type;
	sub_event_id = event_id;
}

void t_tmr_subscribe::expired(void) {
	evq_trans_layer->push_timeout(this);
}

t_timer *t_tmr_subscribe::copy(void) const {
	t_tmr_subscribe *t = new t_tmr_subscribe(*this);
	MEMMAN_NEW(t);
	return t;
}

t_timer_type t_tmr_subscribe::get_type(void) const {
	return TMR_SUBSCRIBE;
}

t_subscribe_timer t_tmr_subscribe::get_subscribe_timer(void) const {
	return subscribe_timer;
}

t_object_id t_tmr_subscribe::get_line_id(void) const {
	return line_id;
}

t_object_id t_tmr_subscribe::get_dialog_id(void) const {
	return dialog_id;
}

string t_tmr_subscribe::get_sub_event_type(void) const {
	return sub_event_type;
}

string t_tmr_subscribe::get_sub_event_id(void) const {
	return sub_event_id;
}

string t_tmr_subscribe::get_name(void) const {
	switch(subscribe_timer) {
	case STMR_SUBSCRIPTION:	return "STMR_SUBSCRIPTION";
	}

	return "UNKNOWN";
}

///////////////////////////////////////////////////////////
// class t_tmr_publish
///////////////////////////////////////////////////////////
t_tmr_publish::t_tmr_publish(long dur, t_publish_timer ptmr, const string &_event_type) :
	t_timer(dur),
	publish_timer(ptmr),
	event_type(_event_type)
{}

void t_tmr_publish::expired(void) {
	evq_trans_layer->push_timeout(this);
}

t_timer *t_tmr_publish::copy(void) const {
	t_tmr_publish *t = new t_tmr_publish(*this);
	MEMMAN_NEW(t);
	return t;
}

t_timer_type t_tmr_publish::get_type(void) const {
	return TMR_PUBLISH;
}

t_publish_timer t_tmr_publish::get_publish_timer(void) const {
	return publish_timer;
}

string t_tmr_publish::get_name(void) const {
	switch (publish_timer) {
	case PUBLISH_TMR_PUBLICATION: return "PUBLISH_TMR_PUBLICATION";
	}
	
	return "UNKNOWN";
}

///////////////////////////////////////////////////////////
// class t_tmr_stun_trans
///////////////////////////////////////////////////////////

t_tmr_stun_trans::t_tmr_stun_trans(long dur, t_stun_timer tmr,
	unsigned short tid) : t_timer(dur)
{
	stun_timer = tmr;
	transaction_id = tid;
}

void t_tmr_stun_trans::expired(void) {
	// Create a timeout event for the transaction manager
	evq_trans_mgr->push_timeout(this);
}

t_timer *t_tmr_stun_trans::copy(void) const {
	t_tmr_stun_trans *t = new t_tmr_stun_trans(*this);
	MEMMAN_NEW(t);
	return t;
}

t_timer_type t_tmr_stun_trans::get_type(void) const {
	return TMR_STUN_TRANSACTION;
}

unsigned short t_tmr_stun_trans::get_tid(void) const {
	return transaction_id;
}

t_stun_timer t_tmr_stun_trans::get_stun_timer(void) const {
	return stun_timer;
}

string t_tmr_stun_trans::get_name(void) const {
	switch(stun_timer) {
	case STUN_TMR_REQ_TIMEOUT:	return "STUN_TMR_REQ_TIMEOUT";
	}

	return "UNKNOWN";
}

///////////////////////////////////////////////////////////
// class t_timekeeper
///////////////////////////////////////////////////////////

t_timekeeper::t_timekeeper() : mutex() {
	stopped = false;
	timer_expired = false;
}

void t_timekeeper::start(void (*timeout_handler)(int)) {
	signal(SIGALRM, timeout_handler);
}

t_timekeeper::~t_timekeeper() {
	struct itimerval	itimer;

	mutex.lock();

	log_file->write_header("t_timekeeper::~t_timekeeper",
		LOG_NORMAL, LOG_INFO);
	log_file->write_raw("Clean up timekeeper.\n");

	// Stop timers
	itimer.it_interval.tv_sec = 0;
	itimer.it_interval.tv_usec = 0;
	itimer.it_value.tv_sec = 0;
	itimer.it_value.tv_usec = 0;
	setitimer(ITIMER_REAL, &itimer, NULL);

	for (list<t_timer *>::iterator i = timer_list.begin();
	     i != timer_list.end(); i++)
	{
		log_file->write_raw("\nDeleting timer:\n");
		log_file->write_raw("Id: ");
		log_file->write_raw((*i)->get_object_id());
		log_file->write_raw(", Type: ");
		log_file->write_raw(timer_type2str((*i)->get_type()));
		log_file->write_raw(", Timer: ");
		log_file->write_raw((*i)->get_name());
		log_file->write_raw("\nDuration: ");
		log_file->write_raw((*i)->get_duration());
		log_file->write_raw(", Relative duration: ");
		log_file->write_raw((*i)->get_relative_duration());
		log_file->write_endl();
		if ((*i)->get_type() == TMR_TRANSACTION) {
			log_file->write_raw("Transaction id: ");
			log_file->write_raw(
				((t_tmr_transaction *)(*i))->get_tid());
			log_file->write_endl();
		}
		MEMMAN_DELETE(*i);
		delete *i;
	}

	if (threading_is_LinuxThreads) {
		signal(SIGALRM, SIG_DFL);
	}

	log_file->write_footer();

	mutex.unlock();
}

void t_timekeeper::lock(void) {
	mutex.lock();
}

void t_timekeeper::unlock(void) {
	mutex.unlock();

	if (timer_expired) {
		timer_expired = false;
		report_expiry();
	}
}

void t_timekeeper::start_timer(t_timer *t) {
	struct itimerval	itimer;
	long			remain_msec;

	lock();

	// The next interval option is not used
	itimer.it_interval.tv_sec = 0;
	itimer.it_interval.tv_usec = 0;

	// Get duration of the timer to start
	long d = t->get_relative_duration();

	// If no timer is currently running then simply start the timer
	if (timer_list.empty()) {
		timer_list.push_back(t);
		itimer.it_value.tv_sec = d / 1000;
		itimer.it_value.tv_usec = (d % 1000) * 1000;
		setitimer(ITIMER_REAL, &itimer, NULL);

		unlock();
		return;
	}

	// Get remaining duration of current running timer
	getitimer(ITIMER_REAL, &itimer);
	remain_msec = itimer.it_value.tv_sec * 1000 +
		      itimer.it_value.tv_usec / 1000;

	// If the new timer is shorter than the current timer.
	// then the new timer should be run first.
	if (d < remain_msec) {
		// Change running timer to new timer
		itimer.it_value.tv_sec = d / 1000;
		itimer.it_value.tv_usec = (d % 1000) * 1000;
		setitimer(ITIMER_REAL, &itimer, NULL);

		// Calculate the relative duration the timer
		// that was running.
		t_timer *old_timer = timer_list.front();
		old_timer->set_relative_duration(remain_msec - d);

		// Add new timer at the front of the list
		timer_list.push_front(t);

		unlock();
		return;
	}

	// Calculate the relative duration for the new timer
	long new_duration = d - remain_msec;

	// Insert the new timer at the right position in the list.
	list<t_timer *>::iterator i;
	for (i = timer_list.begin(); i != timer_list.end(); i++)
	{
		// skip the first timer
		if (i == timer_list.begin()) continue;

		long dur = (*i)->get_relative_duration();
		if (new_duration < dur) {
			// Adjust relative duration existing timer
			(*i)->set_relative_duration(dur - new_duration);

			// Insert new timer before existing timer
			t->set_relative_duration(new_duration);
			timer_list.insert(i, t);

			unlock();
			return;
		}

		new_duration -= dur;
	}

	// Add the new timer to the end of the list
	t->set_relative_duration(new_duration);
	timer_list.push_back(t);

	unlock();
}

void t_timekeeper::stop_timer(t_object_id id) {
	struct itimerval	itimer;
	long			remain_msec;

	lock();

	// The next interval option is not used
	itimer.it_interval.tv_sec = 0;
	itimer.it_interval.tv_usec = 0;


	if (timer_list.empty()) {
		// Timer already expired or stopped
		unlock();
		return;
	}

	// Find timer
	list<t_timer *>::iterator i = timer_list.begin();
	while (i != timer_list.end()) {
		if ((*i)->get_object_id() == id) break;
		i++;
	}

	if (i == timer_list.end()) {
		// Timer already expired or stopped.
		unlock();
		return;
	}

	// If it is the current running timer, then it must be stopped
	if (i == timer_list.begin()) {
		getitimer(ITIMER_REAL, &itimer);

		// If remaining time is less then 100 msec then let it
		// expire to prevent race condition when timer expires
		// while stopping it now.
		remain_msec = itimer.it_value.tv_sec * 1000 +
			      itimer.it_value.tv_usec / 1000;
		if (remain_msec < 100) {
			stopped = true;
			unlock();
			return;
		}

		// Stop timer
		itimer.it_value.tv_sec = 0;
		itimer.it_value.tv_usec = 0;
		setitimer(ITIMER_REAL, &itimer, NULL);

		// Remove the timer
		MEMMAN_DELETE(timer_list.front());
		delete timer_list.front();
		timer_list.pop_front();

		// If a next timer exists then adjust its relative
		// duration and start it.
		if (!timer_list.empty()) {
			t_timer *next_timer = timer_list.front();
			long dur = next_timer->get_relative_duration();
			dur += remain_msec;
			next_timer->set_relative_duration(dur);
			itimer.it_value.tv_sec = dur / 1000;
			itimer.it_value.tv_usec = (dur % 1000) * 1000;
			setitimer(ITIMER_REAL, &itimer, NULL);
		}

		unlock();
		return;
	}

	// Timer is not the current running timer, so delete it
	// and adjust relative duration of the next timer.
	list<t_timer *>::iterator next = i;
	next++;

	if (next == timer_list.end()) {
		// There is no next timer
		MEMMAN_DELETE(timer_list.back());
		delete timer_list.back();
		timer_list.pop_back();
		unlock();
		return;
	}

	long dur = (*i)->get_relative_duration();
	long dur_next = (*next)->get_relative_duration();
	(*next)->set_relative_duration(dur + dur_next);
	MEMMAN_DELETE(*i);
	delete *i;
	timer_list.erase(i);

	unlock();
}

void t_timekeeper::report_expiry(void) {
	lock();
	
	if (timer_list.empty()) {
		unlock();
		return;
	}
	
	t_timer *t = timer_list.front();

	// Trigger action if timer was not stopped
	if (!stopped) {
		t->expired();
	}
	stopped = false;

	// Remove the timer
	MEMMAN_DELETE(timer_list.front());
	delete timer_list.front();
	timer_list.pop_front();

	if (timer_list.empty()) {
		unlock();
		return;
	}

	// If the relative duration of the next timer is 0, then
	// it also expired. Action should be triggerd. If not, then
	// it should be started.
	t_timer *next = timer_list.front();
	long dur = next->get_relative_duration();
	while (dur == 0) {
		next->expired();
		MEMMAN_DELETE(next);
		delete next;
		timer_list.pop_front();
		if (timer_list.empty()) break;
		next = timer_list.front();
		dur = next->get_relative_duration();
	}

	if (!timer_list.empty()) {
		struct itimerval	itimer;

		itimer.it_interval.tv_sec = 0;
		itimer.it_interval.tv_usec = 0;
		itimer.it_value.tv_sec = dur / 1000;
		itimer.it_value.tv_usec = (dur % 1000) * 1000;
		setitimer(ITIMER_REAL, &itimer, NULL);
	}

	unlock();
}

unsigned long t_timekeeper::get_remaining_time(t_object_id timer_id) {
	struct itimerval	itimer;
	unsigned long		remain_msec = 0;
	unsigned long		duration = 0;

	lock();

	// The next interval option is not used
	itimer.it_interval.tv_sec = 0;
	itimer.it_interval.tv_usec = 0;

	// Get remaining duration of current running timer
	getitimer(ITIMER_REAL, &itimer);
	remain_msec = itimer.it_value.tv_sec * 1000 +
		      itimer.it_value.tv_usec / 1000;

	// Find the timer
	list<t_timer *>::iterator i = timer_list.begin();
	while (i != timer_list.end()) {
		if (i != timer_list.begin()) {
			remain_msec += (*i)->get_relative_duration();
		}

		if ((*i)->get_object_id() == timer_id) break;

		i++;
	}

	// Return duration to originator of get event
	if (i == timer_list.end()) {
		duration = 0;
	} else {
		duration = remain_msec;
	}

	unlock();
	return duration;
}

// SIGALRM handler
void timeout_handler(int signum) {
	signal(SIGALRM, timeout_handler);
	// timekeeper.report_expiry();

	// This will signal an interrupt to the call to pop in the
	// main look t_timekeeper::run
	evq_timekeeper->interrupt();
}

void t_timekeeper::run(void) {
	t_event			*event;
	t_event_start_timer	*ev_start;
	t_event_stop_timer	*ev_stop;
	bool			timeout;
	
	// The timekeeper should not try to take the phone lock as
	// it may lead to a deadlock. Make sure an assert is raised
	// if this situation ever happens.
	phone->add_prohibited_thread();

	if (threading_is_LinuxThreads) {
		// In LinuxThreads SIGALRM caused by the expiration of a timer
		// started with setitimer is always delivered to the thread calling
		// setitimer. So the sigwait() call from another thread does not
		// work. Use a signal handler instead.
		start(timeout_handler);
	}

	bool quit = false;
	while (!quit) {
		event = evq_timekeeper->pop(timeout);

		if (timeout) {
			report_expiry();
			continue;
		}

		switch(event->get_type()) {
		case EV_START_TIMER:
			ev_start = (t_event_start_timer *)event;
			start_timer(ev_start->get_timer());
			break;
		case EV_STOP_TIMER:
			ev_stop = (t_event_stop_timer *)event;
			stop_timer(ev_stop->get_timer_id());
			break;
		case EV_QUIT:
			quit = true;
			break;
		default:
			assert(false);
		}

		MEMMAN_DELETE(event);
		delete event;
	}
}

void *timekeeper_main(void *arg) {
	timekeeper->run();
	return NULL;
}

void *timekeeper_sigwait(void *arg) {
	sigset_t	sigset;
	int		sig;

	sigemptyset(&sigset);
	sigaddset(&sigset, SIGALRM);

	while (true) {
		// When SIGCONT is received after SIGSTOP, sigwait returns
		// with EINTR ??
		if (sigwait(&sigset, &sig) == EINTR) continue;
		evq_timekeeper->interrupt();
	}
	return NULL;
}