Debian Codegraph @

generic/tclAsync.c

raw
1 /*
2 * tclAsync.c --
3 *
4 * This file provides low-level support needed to invoke signal handlers
5 * in a safe way. The code here doesn't actually handle signals, though.
6 * This code is based on proposals made by Mark Diekhans and Don Libes.
7 *
8 * Copyright (c) 1993 The Regents of the University of California.
9 * Copyright (c) 1994 Sun Microsystems, Inc.
10 *
11 * See the file "license.terms" for information on usage and redistribution of
12 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 */
14
15 #include "tclInt.h"
16
17 /* Forward declaration */
18 struct ThreadSpecificData;
19
20 /*
21 * One of the following structures exists for each asynchronous handler:
22 */
23
24 typedef struct AsyncHandler {
25 int ready; /* Non-zero means this handler should be
26 * invoked in the next call to
27 * Tcl_AsyncInvoke. */
28 struct AsyncHandler *nextPtr;
29 /* Next in list of all handlers for the
30 * process. */
31 Tcl_AsyncProc *proc; /* Procedure to call when handler is
32 * invoked. */
33 ClientData clientData; /* Value to pass to handler when it is
34 * invoked. */
35 struct ThreadSpecificData *originTsd;
36 /* Used in Tcl_AsyncMark to modify thread-
37 * specific data from outside the thread it is
38 * associated to. */
39 Tcl_ThreadId originThrdId; /* Origin thread where this token was created
40 * and where it will be yielded. */
41 } AsyncHandler;
42
43 typedef struct ThreadSpecificData {
44 /*
45 * The variables below maintain a list of all existing handlers specific
46 * to the calling thread.
47 */
48 AsyncHandler *firstHandler; /* First handler defined for process, or NULL
49 * if none. */
50 AsyncHandler *lastHandler; /* Last handler or NULL. */
51 int asyncReady; /* This is set to 1 whenever a handler becomes
52 * ready and it is cleared to zero whenever
53 * Tcl_AsyncInvoke is called. It can be
54 * checked elsewhere in the application by
55 * calling Tcl_AsyncReady to see if
56 * Tcl_AsyncInvoke should be invoked. */
57 int asyncActive; /* Indicates whether Tcl_AsyncInvoke is
58 * currently working. If so then we won't set
59 * asyncReady again until Tcl_AsyncInvoke
60 * returns. */
61 Tcl_Mutex asyncMutex; /* Thread-specific AsyncHandler linked-list
62 * lock */
63 } ThreadSpecificData;
64 static Tcl_ThreadDataKey dataKey;
65
66 /*
67 *----------------------------------------------------------------------
68 *
69 * TclFinalizeAsync --
70 *
71 * Finalizes the mutex in the thread local data structure for the async
72 * subsystem.
73 *
74 * Results:
75 * None.
76 *
77 * Side effects:
78 * Forgets knowledge of the mutex should it have been created.
79 *
80 *----------------------------------------------------------------------
81 */
82
83 void
84 TclFinalizeAsync(void)
85 {
86 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
87
88 if (tsdPtr->asyncMutex != NULL) {
89 Tcl_MutexFinalize(&tsdPtr->asyncMutex);
90 }
91 }
92
93 /*
94 *----------------------------------------------------------------------
95 *
96 * Tcl_AsyncCreate --
97 *
98 * This procedure creates the data structures for an asynchronous
99 * handler, so that no memory has to be allocated when the handler is
100 * activated.
101 *
102 * Results:
103 * The return value is a token for the handler, which can be used to
104 * activate it later on.
105 *
106 * Side effects:
107 * Information about the handler is recorded.
108 *
109 *----------------------------------------------------------------------
110 */
111
112 Tcl_AsyncHandler
113 Tcl_AsyncCreate(
114 Tcl_AsyncProc *proc, /* Procedure to call when handler is
115 * invoked. */
116 ClientData clientData) /* Argument to pass to handler. */
117 {
118 AsyncHandler *asyncPtr;
119 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
120
121 asyncPtr = (AsyncHandler *)ckalloc(sizeof(AsyncHandler));
122 asyncPtr->ready = 0;
123 asyncPtr->nextPtr = NULL;
124 asyncPtr->proc = proc;
125 asyncPtr->clientData = clientData;
126 asyncPtr->originTsd = tsdPtr;
127 asyncPtr->originThrdId = Tcl_GetCurrentThread();
128
129 Tcl_MutexLock(&tsdPtr->asyncMutex);
130 if (tsdPtr->firstHandler == NULL) {
131 tsdPtr->firstHandler = asyncPtr;
132 } else {
133 tsdPtr->lastHandler->nextPtr = asyncPtr;
134 }
135 tsdPtr->lastHandler = asyncPtr;
136 Tcl_MutexUnlock(&tsdPtr->asyncMutex);
137 return (Tcl_AsyncHandler) asyncPtr;
138 }
139
140 /*
141 *----------------------------------------------------------------------
142 *
143 * Tcl_AsyncMark --
144 *
145 * This procedure is called to request that an asynchronous handler be
146 * invoked as soon as possible. It's typically called from an interrupt
147 * handler, where it isn't safe to do anything that depends on or
148 * modifies application state.
149 *
150 * Results:
151 * None.
152 *
153 * Side effects:
154 * The handler gets marked for invocation later.
155 *
156 *----------------------------------------------------------------------
157 */
158
159 void
160 Tcl_AsyncMark(
161 Tcl_AsyncHandler async) /* Token for handler. */
162 {
163 AsyncHandler *token = (AsyncHandler *) async;
164
165 Tcl_MutexLock(&token->originTsd->asyncMutex);
166 token->ready = 1;
167 if (!token->originTsd->asyncActive) {
168 token->originTsd->asyncReady = 1;
169 Tcl_ThreadAlert(token->originThrdId);
170 }
171 Tcl_MutexUnlock(&token->originTsd->asyncMutex);
172 }
173
174 /*
175 *----------------------------------------------------------------------
176 *
177 * Tcl_AsyncInvoke --
178 *
179 * This procedure is called at a "safe" time at background level to
180 * invoke any active asynchronous handlers.
181 *
182 * Results:
183 * The return value is a normal Tcl result, which is intended to replace
184 * the code argument as the current completion code for interp.
185 *
186 * Side effects:
187 * Depends on the handlers that are active.
188 *
189 *----------------------------------------------------------------------
190 */
191
192 int
193 Tcl_AsyncInvoke(
194 Tcl_Interp *interp, /* If invoked from Tcl_Eval just after
195 * completing a command, points to
196 * interpreter. Otherwise it is NULL. */
197 int code) /* If interp is non-NULL, this gives
198 * completion code from command that just
199 * completed. */
200 {
201 AsyncHandler *asyncPtr;
202 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
203
204 Tcl_MutexLock(&tsdPtr->asyncMutex);
205
206 if (tsdPtr->asyncReady == 0) {
207 Tcl_MutexUnlock(&tsdPtr->asyncMutex);
208 return code;
209 }
210 tsdPtr->asyncReady = 0;
211 tsdPtr->asyncActive = 1;
212 if (interp == NULL) {
213 code = 0;
214 }
215
216 /*
217 * Make one or more passes over the list of handlers, invoking at most one
218 * handler in each pass. After invoking a handler, go back to the start of
219 * the list again so that (a) if a new higher-priority handler gets marked
220 * while executing a lower priority handler, we execute the higher-
221 * priority handler next, and (b) if a handler gets deleted during the
222 * execution of a handler, then the list structure may change so it isn't
223 * safe to continue down the list anyway.
224 */
225
226 while (1) {
227 for (asyncPtr = tsdPtr->firstHandler; asyncPtr != NULL;
228 asyncPtr = asyncPtr->nextPtr) {
229 if (asyncPtr->ready) {
230 break;
231 }
232 }
233 if (asyncPtr == NULL) {
234 break;
235 }
236 asyncPtr->ready = 0;
237 Tcl_MutexUnlock(&tsdPtr->asyncMutex);
238 code = asyncPtr->proc(asyncPtr->clientData, interp, code);
239 Tcl_MutexLock(&tsdPtr->asyncMutex);
240 }
241 tsdPtr->asyncActive = 0;
242 Tcl_MutexUnlock(&tsdPtr->asyncMutex);
243 return code;
244 }
245
246 /*
247 *----------------------------------------------------------------------
248 *
249 * Tcl_AsyncDelete --
250 *
251 * Frees up all the state for an asynchronous handler. The handler should
252 * never be used again.
253 *
254 * Results:
255 * None.
256 *
257 * Side effects:
258 * The state associated with the handler is deleted.
259 *
260 * Failure to locate the handler in current thread private list
261 * of async handlers will result in panic; exception: the list
262 * is already empty (potential trouble?).
263 * Consequently, threads should create and delete handlers
264 * themselves. I.e. a handler created by one should not be
265 * deleted by some other thread.
266 *
267 *----------------------------------------------------------------------
268 */
269
270 void
271 Tcl_AsyncDelete(
272 Tcl_AsyncHandler async) /* Token for handler to delete. */
273 {
274 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
275 AsyncHandler *asyncPtr = (AsyncHandler *) async;
276 AsyncHandler *prevPtr, *thisPtr;
277
278 /*
279 * Assure early handling of the constraint
280 */
281
282 if (asyncPtr->originThrdId != Tcl_GetCurrentThread()) {
283 Tcl_Panic("Tcl_AsyncDelete: async handler deleted by the wrong thread");
284 }
285
286 /*
287 * If we come to this point when TSD's for the current
288 * thread have already been garbage-collected, we are
289 * in the _serious_ trouble. OTOH, we tolerate calling
290 * with already cleaned-up handler list (should we?).
291 */
292
293 Tcl_MutexLock(&tsdPtr->asyncMutex);
294 if (tsdPtr->firstHandler != NULL) {
295 prevPtr = thisPtr = tsdPtr->firstHandler;
296 while (thisPtr != NULL && thisPtr != asyncPtr) {
297 prevPtr = thisPtr;
298 thisPtr = thisPtr->nextPtr;
299 }
300 if (thisPtr == NULL) {
301 Tcl_Panic("Tcl_AsyncDelete: cannot find async handler");
302 }
303 if (asyncPtr == tsdPtr->firstHandler) {
304 tsdPtr->firstHandler = asyncPtr->nextPtr;
305 } else {
306 prevPtr->nextPtr = asyncPtr->nextPtr;
307 }
308 if (asyncPtr == tsdPtr->lastHandler) {
309 tsdPtr->lastHandler = prevPtr;
310 }
311 }
312 Tcl_MutexUnlock(&tsdPtr->asyncMutex);
313 ckfree(asyncPtr);
314 }
315
316 /*
317 *----------------------------------------------------------------------
318 *
319 * Tcl_AsyncReady --
320 *
321 * This procedure can be used to tell whether Tcl_AsyncInvoke needs to be
322 * called. This procedure is the external interface for checking the
323 * thread-specific asyncReady variable.
324 *
325 * Results:
326 * The return value is 1 whenever a handler is ready and is 0 when no
327 * handlers are ready.
328 *
329 * Side effects:
330 * None.
331 *
332 *----------------------------------------------------------------------
333 */
334
335 int
336 Tcl_AsyncReady(void)
337 {
338 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
339 return tsdPtr->asyncReady;
340 }
341
342 int *
343 TclGetAsyncReadyPtr(void)
344 {
345 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
346 return &(tsdPtr->asyncReady);
347 }
348
349 /*
350 * Local Variables:
351 * mode: c
352 * c-basic-offset: 4
353 * fill-column: 78
354 * End:
355 */