-
Notifications
You must be signed in to change notification settings - Fork 1
/
eventq.c
312 lines (284 loc) · 8.78 KB
/
eventq.c
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
/* eventq.c - event queue manager routines */
/* SimpleScalar(TM) Tool Suite
* Copyright (C) 1994-2001 by Todd M. Austin, Ph.D. and SimpleScalar, LLC.
* All Rights Reserved.
*
* THIS IS A LEGAL DOCUMENT, BY USING SIMPLESCALAR,
* YOU ARE AGREEING TO THESE TERMS AND CONDITIONS.
*
* No portion of this work may be used by any commercial entity, or for any
* commercial purpose, without the prior, written permission of SimpleScalar,
* LLC (info@simplescalar.com). Nonprofit and noncommercial use is permitted
* as described below.
*
* 1. SimpleScalar is provided AS IS, with no warranty of any kind, express
* or implied. The user of the program accepts full responsibility for the
* application of the program and the use of any results.
*
* 2. Nonprofit and noncommercial use is encouraged. SimpleScalar may be
* downloaded, compiled, executed, copied, and modified solely for nonprofit,
* educational, noncommercial research, and noncommercial scholarship
* purposes provided that this notice in its entirety accompanies all copies.
* Copies of the modified software can be delivered to persons who use it
* solely for nonprofit, educational, noncommercial research, and
* noncommercial scholarship purposes provided that this notice in its
* entirety accompanies all copies.
*
* 3. ALL COMMERCIAL USE, AND ALL USE BY FOR PROFIT ENTITIES, IS EXPRESSLY
* PROHIBITED WITHOUT A LICENSE FROM SIMPLESCALAR, LLC (info@simplescalar.com).
*
* 4. No nonprofit user may place any restrictions on the use of this software,
* including as modified by the user, by any other authorized user.
*
* 5. Noncommercial and nonprofit users may distribute copies of SimpleScalar
* in compiled or executable form as set forth in Section 2, provided that
* either: (A) it is accompanied by the corresponding machine-readable source
* code, or (B) it is accompanied by a written offer, with no time limit, to
* give anyone a machine-readable copy of the corresponding source code in
* return for reimbursement of the cost of distribution. This written offer
* must permit verbatim duplication by anyone, or (C) it is distributed by
* someone who received only the executable form, and is accompanied by a
* copy of the written offer of source code.
*
* 6. SimpleScalar was developed by Todd M. Austin, Ph.D. The tool suite is
* currently maintained by SimpleScalar LLC (info@simplescalar.com). US Mail:
* 2395 Timbercrest Court, Ann Arbor, MI 48105.
*
* Copyright (C) 2000-2001 by The Regents of The University of Michigan.
* Copyright (C) 1994-2001 by Todd M. Austin, Ph.D. and SimpleScalar, LLC.
*/
#include <stdio.h>
#include <stdlib.h>
#include "host.h"
#include "misc.h"
#include "machine.h"
#include "eventq.h"
int eventq_max_events;
int eventq_event_count;
struct eventq_desc *eventq_pending;
struct eventq_desc *eventq_free;
static EVENTQ_ID_TYPE next_ID = 1;
void
eventq_init(int max_events)
{
eventq_max_events = max_events;
eventq_event_count = 0;
eventq_pending = NULL;
eventq_free = NULL;
}
#define __QUEUE_EVENT(WHEN, ID, ACTION) \
struct eventq_desc *prev, *ev, *new; \
/* get a free event descriptor */ \
if (!eventq_free) \
{ \
if (eventq_max_events && eventq_event_count >= eventq_max_events) \
panic("too many events"); \
eventq_free = calloc(1, sizeof(struct eventq_desc)); \
} \
new = eventq_free; \
eventq_free = eventq_free->next; \
/* plug in event data */ \
new->when = (WHEN); (ID) = new->id = next_ID++; ACTION; \
/* locate insertion point */ \
for (prev=NULL,ev=eventq_pending; \
ev && ev->when < when; \
prev=ev, ev=ev->next); \
/* insert new record */ \
if (prev) \
{ \
/* insert middle or end */ \
new->next = prev->next; \
prev->next = new; \
} \
else \
{ \
/* insert beginning */ \
new->next = eventq_pending; \
eventq_pending = new; \
}
EVENTQ_ID_TYPE
eventq_queue_setbit(SS_TIME_TYPE when,
BITMAP_ENT_TYPE *bmap, int sz, int bitnum)
{
EVENTQ_ID_TYPE id;
__QUEUE_EVENT(when, id, \
new->action = EventSetBit; new->data.bit.bmap = bmap; \
new->data.bit.sz = sz; new->data.bit.bitnum = bitnum);
return id;
}
EVENTQ_ID_TYPE
eventq_queue_clearbit(SS_TIME_TYPE when,
BITMAP_ENT_TYPE *bmap, int sz, int bitnum)
{
EVENTQ_ID_TYPE id;
__QUEUE_EVENT(when, id, \
new->action = EventClearBit; new->data.bit.bmap = bmap; \
new->data.bit.sz = sz; new->data.bit.bitnum = bitnum);
return id;
}
EVENTQ_ID_TYPE
eventq_queue_setflag(SS_TIME_TYPE when, int *pflag, int value)
{
EVENTQ_ID_TYPE id;
__QUEUE_EVENT(when, id, \
new->action = EventSetFlag; \
new->data.flag.pflag = pflag; new->data.flag.value = value);
return id;
}
EVENTQ_ID_TYPE
eventq_queue_addop(SS_TIME_TYPE when, int *summand, int addend)
{
EVENTQ_ID_TYPE id;
__QUEUE_EVENT(when, id, \
new->action = EventAddOp; \
new->data.addop.summand = summand; \
new->data.addop.addend = addend);
return id;
}
EVENTQ_ID_TYPE
eventq_queue_callback(SS_TIME_TYPE when,
void (*fn)(SS_TIME_TYPE time, int arg), int arg)
{
EVENTQ_ID_TYPE id;
__QUEUE_EVENT(when, id, \
new->action = EventCallback; new->data.callback.fn = fn;\
new->data.callback.arg = arg);
return id;
}
#define EXECUTE_ACTION(ev, now) \
/* execute action */ \
switch (ev->action) { \
case EventSetBit: \
BITMAP_SET(ev->data.bit.bmap, ev->data.bit.sz, ev->data.bit.bitnum);\
break; \
case EventClearBit: \
BITMAP_CLEAR(ev->data.bit.bmap, ev->data.bit.sz, ev->data.bit.bitnum);\
break; \
case EventSetFlag: \
*ev->data.flag.pflag = ev->data.flag.value; \
break; \
case EventAddOp: \
*ev->data.addop.summand += ev->data.addop.addend; \
break; \
case EventCallback: \
(*ev->data.callback.fn)(now, ev->data.callback.arg); \
break; \
default: \
panic("bogus event action"); \
}
/* execute an event immediately, returns non-zero if the event was
located an deleted */
int
eventq_execute(EVENTQ_ID_TYPE id)
{
struct eventq_desc *prev, *ev;
for (prev=NULL,ev=eventq_pending; ev; prev=ev,ev=ev->next)
{
if (ev->id == id)
{
if (prev)
{
/* middle of end of list */
prev->next = ev->next;
}
else /* !prev */
{
/* beginning of list */
eventq_pending = ev->next;
}
/* handle action, now is munged */
EXECUTE_ACTION(ev, 0);
/* put event on free list */
ev->next = eventq_free;
eventq_free = ev;
/* return success */
return TRUE;
}
}
/* not found */
return FALSE;
}
/* remove an event from the eventq, action is never performed, returns
non-zero if the event was located an deleted */
int
eventq_remove(EVENTQ_ID_TYPE id)
{
struct eventq_desc *prev, *ev;
for (prev=NULL,ev=eventq_pending; ev; prev=ev,ev=ev->next)
{
if (ev->id == id)
{
if (prev)
{
/* middle of end of list */
prev->next = ev->next;
}
else /* !prev */
{
/* beginning of list */
eventq_pending = ev->next;
}
/* put event on free list */
ev->next = eventq_free;
eventq_free = ev;
/* return success */
return TRUE;
}
}
/* not found */
return FALSE;
}
void
eventq_service_events(SS_TIME_TYPE now)
{
while (eventq_pending && eventq_pending->when <= now)
{
struct eventq_desc *ev = eventq_pending;
/* handle action */
EXECUTE_ACTION(ev, now);
/* return the event record to the free list */
eventq_pending = ev->next;
ev->next = eventq_free;
eventq_free = ev;
}
}
void
eventq_dump(FILE *stream)
{
struct eventq_desc *ev;
if (!stream)
stream = stderr;
fprintf(stream, "Pending Events: ");
for (ev=eventq_pending; ev; ev=ev->next)
{
fprintf(stream, "@ %.0f:%s:",
(double)ev->when,
ev->action == EventSetBit ? "set bit"
: ev->action == EventClearBit ? "clear bit"
: ev->action == EventSetFlag ? "set flag"
: ev->action == EventAddOp ? "add operation"
: ev->action == EventCallback ? "call back"
: (abort(), ""));
switch (ev->action) {
case EventSetBit:
case EventClearBit:
fprintf(stream, "0x%p, %d, %d",
ev->data.bit.bmap, ev->data.bit.sz, ev->data.bit.bitnum);
break;
case EventSetFlag:
fprintf(stream, "0x%p, %d", ev->data.flag.pflag, ev->data.flag.value);
break;
case EventAddOp:
fprintf(stream, "0x%p, %d",
ev->data.addop.summand, ev->data.addop.addend);
break;
case EventCallback:
fprintf(stream, "0x%p, %d",
ev->data.callback.fn, ev->data.callback.arg);
break;
default:
panic("bogus event action");
}
fprintf(stream, " ");
}
}