-
Notifications
You must be signed in to change notification settings - Fork 0
/
sched.c
139 lines (125 loc) · 2.69 KB
/
sched.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
/*----------------------------------------------------------------------
*
* PACMAN for BBN BitGraphs
*
* File: sched.c
* Contents: passive real-time-clock-based scheduler
* Author: Bob Brown (rlb)
* Description: An event list manager for the scheduling
* of C procedures after given time delays
*
* Copyright (c) 1987 Robert L. Brown. All Rights Reserved. This code
* is public domain.
*
*----------------------------------------------------------------------
*/
#include "style.h"
/*
* Beacuse the scheduler calls routines that re-enqueue themselves,
* The following constant is needed to prevent elpoll() from starving
* the keyboard polling routine when the monsters are running flat-out.
*/
#define MAXLOOPS 5
/*
* elinit() - initialize the event list to empty.
* eladd() - add an event to the event list.
* elpoll() - called at in polling loop to run scheduled events.
*/
/*
* MAXEVENTS ...
*
* 1 for pacman
* 4 for monsters
* 4 for power pill effects
* 1 for bonus fruits
* 3 for tone generators (probably need many more)
*
*/
#define MAXEVENTS 24
#define NULLEVENT (event *)(0)
typedef struct ev {
struct ev *next;
long time;
int (*routine)(); /* procedure to call */
char *arg0; /* optional first argument */
int arg1; /* optional second argument */
} event;
event *Eventfree;
event *Eventhead;
event Event[MAXEVENTS];
extern long clock;
elinit()
{
register event *ep;
Eventfree = NULLEVENT;
for ( ep=Event ; ep<&Event[MAXEVENTS] ; ep++ ) {
ep->next = Eventfree;
Eventfree = ep;
}
Eventhead = NULLEVENT;
clock = 0;
}
/*
* eladd - add an event to the event list
*/
/* VARARGS 2 */
eladd(delta,routine,arg0,arg1)
int delta;
int (*routine)();
char *arg0;
int arg1;
{
long time;
register event *ep, *pp, *cp;
#if V1_25
delta = (delta+5)/10; /* 100HZ clock */
#endif
if ( Eventfree == NULLEVENT ) {
error("Event list overflow");
return;
}
cp = Eventfree;
Eventfree = Eventfree->next;
time = delta+clock;
cp->time = time;
cp->routine = routine;
cp->arg0 = arg0;
cp->arg1 = arg1;
ep = Eventhead;
pp = NULLEVENT;
while ( ep!=NULLEVENT && time>ep->time ) {
pp = ep;
ep = ep->next;
}
cp->next = ep;
if ( pp == NULLEVENT )
Eventhead = cp;
else
pp->next = cp;
}
/*
* elpoll - check if anything to schedule
*/
elpoll()
{
register int maxloops;
register event *ep;
maxloops = MAXLOOPS;
while ( (ep=Eventhead)!=NULLEVENT && ep->time < clock && --maxloops > 0) {
Eventhead = ep->next;
(*ep->routine)(ep->arg0,ep->arg1);
ep->next = Eventfree;
Eventfree = ep;
}
}
/*
* eladjust - adjust the scheduler based on processor delay
*/
eladjust(ms)
register int ms;
{
#if V1_25
ms = (ms+5)/10;
#endif
clock -= ms;
}