-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventqueue.cc
48 lines (40 loc) · 932 Bytes
/
eventqueue.cc
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
// -*- C++ -*-
//
// EventQueue class implementation.
//
// Copyright 1992-2021 Deven T. Corzine <[email protected]>
//
// SPDX-License-Identifier: MIT
//
#include "phoenix.h"
static int EventCmp(Event *event1, Event *event2)
{
if (event1->Time() > event2->Time()) return 1;
if (event1->Time() < event2->Time()) return -1;
return 0;
}
int EventQueue::Enqueue(Event *event)
{
return queue.PriorityEnqueue(event, EventCmp);
}
void EventQueue::Dequeue(Event *event)
{
queue.Remove(event);
}
struct timeval *EventQueue::Execute()
{
static struct timeval tv;
Pointer<Event> event;
while (event = (Event *) queue.First()) {
Timestamp now;
if (event->time <= now) {
event = (Event *) queue.Dequeue();
if (event->Execute()) Enqueue(event);
} else {
tv.tv_sec = event->time - now;
tv.tv_usec = 0;
return &tv;
}
}
return NULL;
}