-
Notifications
You must be signed in to change notification settings - Fork 65
/
sqTicker.c
228 lines (211 loc) · 7.67 KB
/
sqTicker.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
/* sqTicker.c
* Core cross-platform tickers. tick.er (n): one who ticks a tickee
*
* Authors: Eliot Miranda & Josh Gargus
*
* Copyright (C) 2010 by Teleplace, Inc.
*
* All rights reserved.
*
* This file is part of Squeak.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
/* High-priority and synchronous tickee function support.
*
* Tickers provide the ability to register a tickee function that will be
* called at a regular interval. There are two sets of tickers. The high-
* priority ticker is asynchronous and if possible, runs in a high-priority
* thread or if not, from an interrupt. Running from an interrupt is undesir-
* able because it is much easier to create deadlock, for example by interrupt-
* ing the VM when it has acquired some lock, and a tickee tries to acquire
* the same lock. But the use of an interrupt is forced on linux because as
* of 2006 and the 2.6.x kernel there is no way for a non-root process to
* create threads of different priorities.
*
* The synchronous ticker runs in the VM thread at the earliest opportunity once
* the tick has occurred. The VM calls the ticker from its event check.
*
* This file implements the cross-platform components, registering tickers and
* calling tickers if deadlines have been reached. The platform-specific heart-
* beats implement the threads or interrupts and the clocks.
*
* For the moment the number of asynchronous and synchronous tickees is
* hard-wired, but they could easily be defined by a command-line argument etc.
*/
#include "sq.h"
#include "sqAssert.h"
#include "sqAtomicOps.h"
#include "sqMemoryFence.h"
#define NUM_ASYNCHRONOUS_TICKEES 4
#define NUM_SYNCHRONOUS_TICKEES 4
#define MicrosecondsPerMillisecond 1000
typedef struct {
void (*tickee)(void);
long inProgress; /* used only in high-priority ticker */
usqLong tickeeDeadlineUsecs;
usqLong tickeePeriodUsecs;
} Tickee;
static int numSyncTickees = 0;
static Tickee synch[NUM_SYNCHRONOUS_TICKEES];
#if ITIMER_HEARTBEAT
/* See platforms/unix/vm/sqUnixHeartbeat.c */
static volatile int shouldYieldToHighPriorityTickerThread;
#endif
/* Add or remove a synchronous tickee. If periodms is non zero add the tickee
* calling it every periodms, aligned to roundms, if non-zero. If periodms is
* zero, remove tickee.
*/
void
addSynchronousTickee(void (*tickee)(void), unsigned periodms, unsigned roundms)
{
int i;
if (!periodms) {
for (i = 0; i < numSyncTickees; i++)
if (synch[i].tickee == tickee) {
--numSyncTickees;
if (i < numSyncTickees)
memmove(synch + i,
synch + i + 1,
sizeof(synch[i]) * (numSyncTickees - i));
return;
}
return;
}
for (i = 0; i < NUM_SYNCHRONOUS_TICKEES; i++)
if (i >= numSyncTickees
|| !synch[i].tickee
|| synch[i].tickee == tickee) {
synch[i].tickee = tickee;
synch[i].tickeePeriodUsecs = periodms * MicrosecondsPerMillisecond;
synch[i].tickeeDeadlineUsecs = synch[i].tickeePeriodUsecs
+ ioUTCMicroseconds();
if (roundms) {
synch[i].tickeeDeadlineUsecs -= synch[i].tickeeDeadlineUsecs
% (roundms * MicrosecondsPerMillisecond);
if (synch[i].tickeeDeadlineUsecs < ioUTCMicroseconds())
synch[i].tickeeDeadlineUsecs += synch[i].tickeePeriodUsecs;
}
if (i >= numSyncTickees)
++numSyncTickees;
return;
}
error("ran out of synchronous tickee slots");
}
void
ioSynchronousCheckForEvents()
{
int i;
#if ITIMER_HEARTBEAT
extern void yieldToHighPriorityTickerThread(void);
sqLowLevelMFence();
if (shouldYieldToHighPriorityTickerThread)
yieldToHighPriorityTickerThread();
#endif
for (i = 0; i < numSyncTickees; i++)
if (synch[i].tickee
&& ioUTCMicroseconds() >= synch[i].tickeeDeadlineUsecs) {
synch[i].tickeeDeadlineUsecs += synch[i].tickeePeriodUsecs;
synch[i].tickee();
}
}
static int numAsyncTickees = 0;
static Tickee async[NUM_ASYNCHRONOUS_TICKEES];
/* Add or remove an asynchronous tickee. If periodms is non zero add the
* tickee, calling it every periodms.
*
* N.B. addHighPriorityTickee is called from the VM thread, whereas
* checkHighPriorityTickees is called from the high-priority heartbeat thread
* (or an interrupt). The above 64-bit variables must therefore be read and
* written atomically to avoid either thread reading or writing a modified
* half of the variable while the other half has yet to be updated.
*/
void
addHighPriorityTickee(void (*tickee)(void), unsigned periodms)
{
int i;
if (!periodms) {
for (i = 0; i < numAsyncTickees; i++)
/* We cannot safely copy the data to keep used tickees contiguous
* because checkHighPriorityTickees could be called during the move.
* This implies first checking for an existing tickee below before
* using an empty slot because an empty slot can be created before
* a used (and subsequently modified) tickee.
*/
if (async[i].tickee == tickee) {
async[i].tickee = 0;
sqLowLevelMFence();
return;
}
return;
}
for (i = 0; i < numAsyncTickees; i++)
if (async[i].tickee == tickee)
break;
if (i >= numAsyncTickees)
for (i = 0; i < NUM_ASYNCHRONOUS_TICKEES; i++)
if (i >= numAsyncTickees
|| !async[i].tickee)
break;
if (i >= NUM_ASYNCHRONOUS_TICKEES)
error("ran out of asyncronous tickee slots");
/* first disable the tickee while updating the entry. */
async[i].tickee = 0;
sqLowLevelMFence();
async[i].tickeePeriodUsecs = periodms * MicrosecondsPerMillisecond;
async[i].tickeeDeadlineUsecs = async[i].tickeePeriodUsecs
+ ioUTCMicroseconds();
async[i].inProgress = 0;
async[i].tickee = tickee;
if (i >= numAsyncTickees)
++numAsyncTickees;
sqLowLevelMFence();
}
void
checkHighPriorityTickees(usqLong utcMicrosecondClock)
{
int i;
#if ITIMER_HEARTBEAT
extern void unblockVMThreadAfterYieldToHighPriorityTickerThread(void);
shouldYieldToHighPriorityTickerThread = 1;
#endif
/* Since this runs either in a high-priority thread or in an interrupt only
* one fence is needed. Since the VM thread will not disturb any non-zero
* entry (except for changing the period) we can read the entry without
* locking. But we need to lock the attempt to run the tickee in case for
* any reason checkHighPriorityTickees is miscalled reentrantly.
*/
sqLowLevelMFence();
for (i = 0; i < numAsyncTickees; i++)
if (async[i].tickee
&& !async[i].inProgress
&& utcMicrosecondClock >= async[i].tickeeDeadlineUsecs) {
if (sqCompareAndSwap(async[i].inProgress,0,1)) {
assert(async[i].inProgress);
async[i].tickeeDeadlineUsecs += async[i].tickeePeriodUsecs;
async[i].tickee();
async[i].inProgress = 0;
}
}
#if ITIMER_HEARTBEAT
shouldYieldToHighPriorityTickerThread = 0;
sqLowLevelMFence();
unblockVMThreadAfterYieldToHighPriorityTickerThread();
#endif
}