-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent.cpp
More file actions
81 lines (71 loc) · 1.98 KB
/
event.cpp
File metadata and controls
81 lines (71 loc) · 1.98 KB
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
#include "event.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
char *id_str(Id id, char *s)
{
int w;
if (is_src(id))
w = snprintf(s, IDSTRLEN, "Src ");
else if (is_dst(id))
w = snprintf(s, IDSTRLEN, "Dst ");
else
w = snprintf(s, IDSTRLEN, "Rtr ");
snprintf(s + w, IDSTRLEN - w, "%d", id.value);
return s;
}
static int cmp_pri(pqueue_pri_t next, pqueue_pri_t curr) { return next > curr; }
static pqueue_pri_t get_pri(void *a) { return ((TimedEvent *)a)->time; }
static void set_pri(void *a, pqueue_pri_t pri) { ((TimedEvent *)a)->time = pri; }
static size_t get_pos(void *a) { return ((TimedEvent *)a)->pos; }
static void set_pos(void *a, size_t pos) { ((TimedEvent *)a)->pos = pos; }
void eventq_init(EventQueue *eq)
{
eq->time_ = -1;
eq->pq = pqueue_init(1000, cmp_pri, get_pri, set_pri, get_pos, set_pos);
assert(eq->pq);
}
void eventq_destroy(EventQueue *eq)
{
while (pqueue_size(eq->pq) > 0) {
TimedEvent *te = (TimedEvent *)pqueue_pop(eq->pq);
free(te);
}
pqueue_free(eq->pq);
}
void schedule(EventQueue *eq, long time, Event e)
{
TimedEvent *te = (TimedEvent *)malloc(sizeof(TimedEvent));
te->time = time;
te->event = e;
pqueue_insert(eq->pq, te);
}
void reschedule(EventQueue *eq, long reltime, Event e)
{
schedule(eq, eq->time_ + reltime, e);
}
long curr_time(const EventQueue *eq)
{
return eq->time_;
}
// This is mainly used for the debugger, where it should be able to process
// all events at a specific time and stop right before the time changes.
long next_time(const EventQueue *eq)
{
return ((TimedEvent *)pqueue_peek(eq->pq))->time;
}
Event eventq_pop(EventQueue *eq)
{
TimedEvent *te = (TimedEvent *)pqueue_pop(eq->pq);
long time = te->time;
Event e = te->event;
assert(time >= eq->time_ && "time goes backward!");
// Update simulation time.
eq->time_ = time;
free(te);
return e;
}
int eventq_empty(const EventQueue *eq)
{
return pqueue_size(eq->pq) == 0;
}