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
|
#define LAZY_THREAD_CREATE 1
#define AT_FORK_INIT_VALUE 0
#define DEACTIVATE_ATFORK_MUTEX 0
/*
* tclUnixNotify.c --
*
* This file contains the implementation of the select()-based
* Unix-specific notifier, which is the lowest-level part of the Tcl
* event loop. This file works together with generic/tclNotify.c.
*
* Copyright (c) 1995-1997 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include "tclInt.h"
#ifndef HAVE_COREFOUNDATION /* Darwin/Mac OS X CoreFoundation notifier is
* in tclMacOSXNotify.c */
#include <signal.h>
/*
* This structure is used to keep track of the notifier info for a registered
* file.
*/
typedef struct FileHandler {
|
<
|
>
|
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
|
#define AT_FORK_INIT_VALUE 0
#define RESET_ATFORK_MUTEX 1
/*
* tclUnixNotify.c --
*
* This file contains the implementation of the select()-based
* Unix-specific notifier, which is the lowest-level part of the Tcl
* event loop. This file works together with generic/tclNotify.c.
*
* Copyright (c) 1995-1997 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include "tclInt.h"
#ifndef HAVE_COREFOUNDATION /* Darwin/Mac OS X CoreFoundation notifier is
* in tclMacOSXNotify.c */
#include <signal.h>
#include <assert.h>
/*
* This structure is used to keep track of the notifier info for a registered
* file.
*/
typedef struct FileHandler {
|
| ︙ | | | ︙ | |
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
|
* fields. */
#ifdef __CYGWIN__
void *event; /* Any other thread alerts a notifier
* that an event is ready to be processed
* by sending this event. */
void *hwnd; /* Messaging window. */
#else /* !__CYGWIN__ */
Tcl_Condition waitCV; /* Any other thread alerts a notifier that an
* event is ready to be processed by signaling
* this condition variable. */
#endif /* __CYGWIN__ */
int eventReady; /* True if an event is ready to be processed.
* Used as condition flag together with waitCV
* above. */
#endif /* TCL_THREADS */
} ThreadSpecificData;
static Tcl_ThreadDataKey dataKey;
#ifdef TCL_THREADS
/*
* The following static indicates the number of threads that have initialized
* notifiers.
*
* You must hold the notifierMutex lock before accessing this variable.
*/
static int notifierCount = 0;
/*
* The following static stores the process ID of the initialized notifier
* thread. If it changes, we have passed a fork and we should start a new
* notifier thread.
*
* You must hold the notifierMutex lock before accessing this variable.
*/
static pid_t processIDInitialized = 0;
/*
* The following variable points to the head of a doubly-linked list of
* ThreadSpecificData structures for all threads that are currently waiting on
* an event.
*
* You must hold the notifierMutex lock before accessing this list.
*/
|
|
>
<
<
<
<
<
<
<
<
<
<
|
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
|
* fields. */
#ifdef __CYGWIN__
void *event; /* Any other thread alerts a notifier
* that an event is ready to be processed
* by sending this event. */
void *hwnd; /* Messaging window. */
#else /* !__CYGWIN__ */
pthread_cond_t waitCV; /* Any other thread alerts a notifier that an
* event is ready to be processed by signaling
* this condition variable. */
int waitCVinitialized; /* Variable to flag initialization of the structure */
#endif /* __CYGWIN__ */
int eventReady; /* True if an event is ready to be processed.
* Used as condition flag together with waitCV
* above. */
#endif /* TCL_THREADS */
} ThreadSpecificData;
static Tcl_ThreadDataKey dataKey;
#ifdef TCL_THREADS
/*
* The following static indicates the number of threads that have initialized
* notifiers.
*
* You must hold the notifierMutex lock before accessing this variable.
*/
static int notifierCount = 0;
/*
* The following variable points to the head of a doubly-linked list of
* ThreadSpecificData structures for all threads that are currently waiting on
* an event.
*
* You must hold the notifierMutex lock before accessing this list.
*/
|
| ︙ | | | ︙ | |
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
|
static int triggerPipe = -1;
/*
* The notifierMutex locks access to all of the global notifier state.
*/
TCL_DECLARE_MUTEX(notifierMutex)
#if LAZY_THREAD_CREATE == 1
TCL_DECLARE_MUTEX(notifierInitMutex)
/*
* The following static indicates if the notifier thread is running.
*
* You must hold the notifierInitLock before accessing this variable.
*/
static int notifierThreadRunning = 0;
#endif
/*
* The notifier thread signals the notifierCV when it has finished
* initializing the triggerPipe and right before the notifier thread
* terminates.
*/
static Tcl_Condition notifierCV;
/*
* The pollState bits
* POLL_WANT is set by each thread before it waits on its condition
* variable. It is checked by the notifier before it does select.
* POLL_DONE is set by the notifier if it goes into select after seeing
* POLL_WANT. The idea is to ensure it tries a select with the
|
<
<
<
|
|
|
<
|
<
|
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
|
static int triggerPipe = -1;
/*
* The notifierMutex locks access to all of the global notifier state.
*/
pthread_mutex_t notifierInitMutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t notifierMutex = PTHREAD_MUTEX_INITIALIZER;
/*
* The following static indicates if the notifier thread is running.
*
* You must hold the notifierInitMutex before accessing this variable.
*/
static int notifierThreadRunning = 0;
/*
* The notifier thread signals the notifierCV when it has finished
* initializing the triggerPipe and right before the notifier thread
* terminates.
*/
static pthread_cond_t notifierCV = PTHREAD_COND_INITIALIZER;
/*
* The pollState bits
* POLL_WANT is set by each thread before it waits on its condition
* variable. It is checked by the notifier before it does select.
* POLL_DONE is set by the notifier if it goes into select after seeing
* POLL_WANT. The idea is to ensure it tries a select with the
|
| ︙ | | | ︙ | |
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
* Threaded-cygwin specific functions in this file:
*/
static DWORD __stdcall NotifierProc(void *hwnd, unsigned int message,
void *wParam, void *lParam);
#endif /* TCL_THREADS && __CYGWIN__ */
#if LAZY_THREAD_CREATE == 1
static void
StartNotifierThread(void)
{
fprintf(stderr, "=== StartNotifierThread()\n");
Tcl_MutexLock(¬ifierInitMutex);
TclpMasterLock();
TclpMutexLock();
fprintf(stderr, "=== StartNotifierThread() locked notifierThreadRunning %d notifierCount %d\n", notifierThreadRunning, notifierCount);
if (!notifierCount) {
Tcl_Panic("StartNotifierThread: notifier not initialized");
}
if (!notifierThreadRunning) {
fprintf(stderr, "=== StartNotifierThread() really start\n");
if (TclpThreadCreate(¬ifierThread, NotifierThreadProc, NULL,
TCL_THREAD_STACK_DEFAULT, TCL_THREAD_JOINABLE) != TCL_OK) {
Tcl_Panic("Tcl_InitNotifier: unable to start notifier thread");
}
processIDInitialized = getpid();
notifierThreadRunning = 1;
}
TclpMutexUnlock();
TclpMasterUnlock();
Tcl_MutexUnlock(¬ifierInitMutex);
}
#endif
/*
*----------------------------------------------------------------------
*
* Tcl_InitNotifier --
*
* Initializes the platform specific notifier state.
*
* Results:
* Returns a handle to the notifier state for this thread.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
ClientData
Tcl_InitNotifier(void)
{
//fprintf(stderr, "==== Tcl_InitNotifier atForkInit %d notifierCount %d\n", atForkInit, notifierCount);
if (tclNotifierHooks.initNotifierProc) {
return tclNotifierHooks.initNotifierProc();
} else {
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
#ifdef TCL_THREADS
tsdPtr->eventReady = 0;
/*
* Start the Notifier thread if necessary.
*/
Tcl_MutexLock(¬ifierMutex);
#if defined(HAVE_PTHREAD_ATFORK) && !defined(__APPLE__) && !defined(__hpux)
/*
* Install pthread_atfork handlers to reinitialize the notifier in the
* child of a fork.
*/
if (!atForkInit) {
int result = pthread_atfork(AtForkPrepare, AtForkParent, AtForkChild);
fprintf(stderr, "==== calling pthread_atfork()\n");
if (result) {
Tcl_Panic("Tcl_InitNotifier: pthread_atfork failed");
}
atForkInit = 1;
}
#endif /* HAVE_PTHREAD_ATFORK */
/*
* Check if my process id changed, e.g. I was forked
* In this case, restart the notifier thread and close the
* pipe to the original notifier thread
*/
#if LAZY_THREAD_CREATE == 0
if (notifierCount > 0 && processIDInitialized != getpid()) {
// fprintf(stderr, "==== reset notifierCount for %d\n", getpid());
Tcl_ConditionFinalize(¬ifierCV);
notifierCount = 0;
processIDInitialized = 0;
close(triggerPipe);
triggerPipe = -1;
}
#endif
if (notifierCount == 0) {
#if LAZY_THREAD_CREATE == 0
if (TclpThreadCreate(¬ifierThread, NotifierThreadProc, NULL,
TCL_THREAD_STACK_DEFAULT, TCL_THREAD_JOINABLE) != TCL_OK) {
Tcl_Panic("Tcl_InitNotifier: unable to start notifier thread");
}
processIDInitialized = getpid();
#endif
}
notifierCount++;
#if LAZY_THREAD_CREATE == 0
/*
* Wait for the notifier pipe to be created.
*/
while (triggerPipe < 0) {
Tcl_ConditionWait(¬ifierCV, ¬ifierMutex, NULL);
}
#endif
Tcl_MutexUnlock(¬ifierMutex);
#endif /* TCL_THREADS */
return tsdPtr;
}
}
/*
*----------------------------------------------------------------------
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
<
<
<
<
<
<
<
|
>
|
|
|
|
|
|
>
>
>
>
>
>
>
>
>
>
|
<
<
|
|
<
|
>
|
>
>
>
|
>
>
|
>
|
|
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
<
<
|
<
<
<
|
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
* Threaded-cygwin specific functions in this file:
*/
static DWORD __stdcall NotifierProc(void *hwnd, unsigned int message,
void *wParam, void *lParam);
#endif /* TCL_THREADS && __CYGWIN__ */
#if TCL_THREADS
/*
*----------------------------------------------------------------------
*
* StartNotifierThread --
*
* Start a notfier thread and wait for the notifier pipe to be created.
*
* Results:
* None.
*
* Side effects:
* Running Thread.
*
*----------------------------------------------------------------------
*/
static void
StartNotifierThread(void)
{
pthread_mutex_lock(¬ifierInitMutex);
if (!notifierThreadRunning) {
fprintf(stderr, "=== StartNotifierThread()\n");
if (TclpThreadCreate(¬ifierThread, NotifierThreadProc, NULL,
TCL_THREAD_STACK_DEFAULT, TCL_THREAD_JOINABLE) != TCL_OK) {
Tcl_Panic("Tcl_InitNotifier: unable to start notifier thread");
}
pthread_mutex_lock(¬ifierMutex);
/*
* Wait for the notifier pipe to be created.
*/
while (triggerPipe < 0) {
pthread_cond_wait(¬ifierCV, ¬ifierMutex);
}
pthread_mutex_unlock(¬ifierMutex);
notifierThreadRunning = 1;
}
pthread_mutex_unlock(¬ifierInitMutex);
}
#endif /* TCL_THREADS */
/*
*----------------------------------------------------------------------
*
* Tcl_InitNotifier --
*
* Initializes the platform specific notifier state.
*
* Results:
* Returns a handle to the notifier state for this thread.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
ClientData
Tcl_InitNotifier(void)
{
if (tclNotifierHooks.initNotifierProc) {
return tclNotifierHooks.initNotifierProc();
} else {
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
#ifdef TCL_THREADS
tsdPtr->eventReady = 0;
#ifndef __CYGWIN__
/*
* Initialize thread specific condition variable for this thread.
*/
if (tsdPtr->waitCVinitialized == 0) {
pthread_cond_init(&tsdPtr->waitCV, NULL);
tsdPtr->waitCVinitialized = 1;
}
#endif
pthread_mutex_lock(¬ifierInitMutex);
# if defined(HAVE_PTHREAD_ATFORK) && !defined(__APPLE__) && !defined(__hpux)
/*
* Register pthread_atfork handlers once per process
*/
if (!atForkInit) {
int result = pthread_atfork(AtForkPrepare, AtForkParent, AtForkChild);
fprintf(stderr, "==== calling pthread_atfork()\n");
if (result) {
Tcl_Panic("Tcl_InitNotifier: pthread_atfork failed");
}
atForkInit = 1;
}
# endif /* HAVE_PTHREAD_ATFORK */
notifierCount++;
pthread_mutex_unlock(¬ifierInitMutex);
#endif /* TCL_THREADS */
return tsdPtr;
}
}
/*
*----------------------------------------------------------------------
|
| ︙ | | | ︙ | |
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
|
if (tclNotifierHooks.finalizeNotifierProc) {
tclNotifierHooks.finalizeNotifierProc(clientData);
return;
} else {
#ifdef TCL_THREADS
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
Tcl_MutexLock(¬ifierMutex);
notifierCount--;
//fprintf(stderr, "==== Tcl_Tcl_FinalizeNotifier (after decr) atForkInit %d notifierCount %d\n", atForkInit, notifierCount);
/*
* If this is the last thread to use the notifier, close the notifier
* pipe and wait for the background thread to terminate.
*/
if (notifierCount == 0) {
#if LAZY_THREAD_CREATE == 1
if (triggerPipe != -1) {
if (write(triggerPipe, "q", 1) != 1) {
Tcl_Panic("Tcl_FinalizeNotifier: %s",
"unable to write q to triggerPipe");
}
close(triggerPipe);
triggerPipe = -1;
if (notifierThreadRunning) {
int result = Tcl_JoinThread(notifierThread, NULL);
if (result) {
Tcl_Panic("Tcl_FinalizeNotifier: unable to join notifier "
"thread");
}
notifierThreadRunning = 0;
}
}
#else
int result;
# if AT_FORK_INIT_VALUE == 1
if (triggerPipe != -1) {
if (write(triggerPipe, "q", 1) != 1) {
Tcl_Panic("Tcl_FinalizeNotifier: %s",
"unable to write q to triggerPipe");
}
close(triggerPipe);
triggerPipe = -1;
}
# else
if (triggerPipe < 0) {
Tcl_Panic("Tcl_FinalizeNotifier: %s",
"notifier pipe not initialized");
}
/*
* Send "q" message to the notifier thread so that it will
* terminate. The notifier will return from its call to select()
* and notice that a "q" message has arrived, it will then close
* its side of the pipe and terminate its thread. Note the we can
* not just close the pipe and check for EOF in the notifier thread
* because if a background child process was created with exec,
* select() would not register the EOF on the pipe until the child
* processes had terminated. [Bug: 4139] [Bug: 1222872]
*/
if (write(triggerPipe, "q", 1) != 1) {
Tcl_Panic("Tcl_FinalizeNotifier: %s",
"unable to write q to triggerPipe");
}
close(triggerPipe);
while(triggerPipe >= 0) {
Tcl_ConditionWait(¬ifierCV, ¬ifierMutex, NULL);
}
result = Tcl_JoinThread(notifierThread, NULL);
if (result) {
Tcl_Panic("Tcl_FinalizeNotifier: %s",
"unable to join notifier thread");
}
# endif
#endif
}
/*
* Clean up any synchronization objects in the thread local storage.
*/
#ifdef __CYGWIN__
CloseHandle(tsdPtr->event);
#else /* __CYGWIN__ */
Tcl_ConditionFinalize(&(tsdPtr->waitCV));
#endif /* __CYGWIN__ */
Tcl_MutexUnlock(¬ifierMutex);
#endif /* TCL_THREADS */
}
}
/*
*----------------------------------------------------------------------
*
|
>
>
>
>
>
>
>
>
|
<
|
|
>
|
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
|
>
|
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
|
if (tclNotifierHooks.finalizeNotifierProc) {
tclNotifierHooks.finalizeNotifierProc(clientData);
return;
} else {
#ifdef TCL_THREADS
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
/*
* Check if FinializeNotifier was called without a prior InitNotifier
* in this thread.
*/
#ifndef __CYGWIN__
assert(tsdPtr->waitCVinitialized == 1);
#endif
pthread_mutex_lock(¬ifierInitMutex);
notifierCount--;
/*
* If this is the last thread to use the notifier, close the notifier
* pipe and wait for the background thread to terminate.
*/
if (notifierCount == 0) {
if (triggerPipe != -1) {
if (write(triggerPipe, "q", 1) != 1) {
Tcl_Panic("Tcl_FinalizeNotifier: %s",
"unable to write q to triggerPipe");
}
close(triggerPipe);
while(triggerPipe != -1) {
pthread_cond_wait(¬ifierCV, ¬ifierMutex);
}
if (notifierThreadRunning) {
int result = pthread_join((pthread_t) notifierThread, NULL);
if (result) {
Tcl_Panic("Tcl_FinalizeNotifier: unable to join notifier "
"thread");
}
notifierThreadRunning = 0;
}
}
}
/*
* Clean up any synchronization objects in the thread local storage.
*/
#ifdef __CYGWIN__
CloseHandle(tsdPtr->event);
#else /* __CYGWIN__ */
pthread_cond_destroy(&tsdPtr->waitCV);
#endif /* __CYGWIN__ */
pthread_mutex_unlock(¬ifierInitMutex);
#endif /* TCL_THREADS */
}
}
/*
*----------------------------------------------------------------------
*
|
| ︙ | | | ︙ | |
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
|
if (tclNotifierHooks.alertNotifierProc) {
tclNotifierHooks.alertNotifierProc(clientData);
return;
} else {
#ifdef TCL_THREADS
ThreadSpecificData *tsdPtr = clientData;
Tcl_MutexLock(¬ifierMutex);
tsdPtr->eventReady = 1;
# ifdef __CYGWIN__
PostMessageW(tsdPtr->hwnd, 1024, 0, 0);
# else
Tcl_ConditionNotify(&tsdPtr->waitCV);
# endif /* __CYGWIN__ */
Tcl_MutexUnlock(¬ifierMutex);
#endif /* TCL_THREADS */
}
}
/*
*----------------------------------------------------------------------
*
|
|
>
|
|
|
|
>
|
|
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
|
if (tclNotifierHooks.alertNotifierProc) {
tclNotifierHooks.alertNotifierProc(clientData);
return;
} else {
#ifdef TCL_THREADS
ThreadSpecificData *tsdPtr = clientData;
pthread_mutex_lock(¬ifierMutex);
tsdPtr->eventReady = 1;
# ifdef __CYGWIN__
PostMessageW(tsdPtr->hwnd, 1024, 0, 0);
# else
pthread_cond_broadcast(&tsdPtr->waitCV);
# endif /* __CYGWIN__ */
pthread_mutex_unlock(¬ifierMutex);
#endif /* TCL_THREADS */
}
}
/*
*----------------------------------------------------------------------
*
|
| ︙ | | | ︙ | |
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
|
int mode) /* Either TCL_SERVICE_ALL, or
* TCL_SERVICE_NONE. */
{
fprintf(stderr, "==== Tcl_ServiceModeHook mode %d\n", mode);
if (tclNotifierHooks.serviceModeHookProc) {
tclNotifierHooks.serviceModeHookProc(mode);
return;
} else {
#if LAZY_THREAD_CREATE == 1
if (mode == TCL_SERVICE_ALL) {
StartNotifierThread();
}
#else
/* Does nothing in this implementation. */
#endif
}
}
/*
*----------------------------------------------------------------------
*
|
<
<
|
>
|
<
<
<
|
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
|
int mode) /* Either TCL_SERVICE_ALL, or
* TCL_SERVICE_NONE. */
{
fprintf(stderr, "==== Tcl_ServiceModeHook mode %d\n", mode);
if (tclNotifierHooks.serviceModeHookProc) {
tclNotifierHooks.serviceModeHookProc(mode);
return;
} else if (mode == TCL_SERVICE_ALL) {
#if TCL_THREADS
StartNotifierThread();
#endif
}
}
/*
*----------------------------------------------------------------------
*
|
| ︙ | | | ︙ | |
652
653
654
655
656
657
658
659
660
661
662
663
664
665
|
if (tclNotifierHooks.createFileHandlerProc) {
tclNotifierHooks.createFileHandlerProc(fd, mask, proc, clientData);
return;
} else {
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
FileHandler *filePtr;
for (filePtr = tsdPtr->firstFileHandlerPtr; filePtr != NULL;
filePtr = filePtr->nextPtr) {
if (filePtr->fd == fd) {
break;
}
}
if (filePtr == NULL) {
|
>
>
>
>
>
>
>
|
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
|
if (tclNotifierHooks.createFileHandlerProc) {
tclNotifierHooks.createFileHandlerProc(fd, mask, proc, clientData);
return;
} else {
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
FileHandler *filePtr;
/*
* Check if InitNotifier was called before in this thread
*/
#ifndef __CYGWIN__
assert(tsdPtr->waitCVinitialized == 1);
#endif
for (filePtr = tsdPtr->firstFileHandlerPtr; filePtr != NULL;
filePtr = filePtr->nextPtr) {
if (filePtr->fd == fd) {
break;
}
}
if (filePtr == NULL) {
|
| ︙ | | | ︙ | |
723
724
725
726
727
728
729
730
731
732
733
734
735
736
|
tclNotifierHooks.deleteFileHandlerProc(fd);
return;
} else {
FileHandler *filePtr, *prevPtr;
int i;
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
/*
* Find the entry for the given file (and return if there isn't one).
*/
for (prevPtr = NULL, filePtr = tsdPtr->firstFileHandlerPtr; ;
prevPtr = filePtr, filePtr = filePtr->nextPtr) {
if (filePtr == NULL) {
|
>
>
>
>
>
>
>
|
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
|
tclNotifierHooks.deleteFileHandlerProc(fd);
return;
} else {
FileHandler *filePtr, *prevPtr;
int i;
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
/*
* Check if InitNotifier was called before in this thread
*/
#ifndef __CYGWIN__
assert(tsdPtr->waitCVinitialized == 1);
#endif
/*
* Find the entry for the given file (and return if there isn't one).
*/
for (prevPtr = NULL, filePtr = tsdPtr->firstFileHandlerPtr; ;
prevPtr = filePtr, filePtr = filePtr->nextPtr) {
if (filePtr == NULL) {
|
| ︙ | | | ︙ | |
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
|
* Search through the file handlers to find the one whose handle matches
* the event. We do this rather than keeping a pointer to the file handler
* directly in the event, so that the handler can be deleted while the
* event is queued without leaving a dangling pointer.
*/
tsdPtr = TCL_TSD_INIT(&dataKey);
for (filePtr = tsdPtr->firstFileHandlerPtr; filePtr != NULL;
filePtr = filePtr->nextPtr) {
if (filePtr->fd != fileEvPtr->fd) {
continue;
}
/*
* The code is tricky for two reasons:
* 1. The file handler's desired events could have changed since the
|
>
>
>
>
>
>
>
>
|
<
|
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
|
* Search through the file handlers to find the one whose handle matches
* the event. We do this rather than keeping a pointer to the file handler
* directly in the event, so that the handler can be deleted while the
* event is queued without leaving a dangling pointer.
*/
tsdPtr = TCL_TSD_INIT(&dataKey);
/*
* Check if InitNotifier was called before in this thread
*/
#ifndef __CYGWIN__
assert(tsdPtr->waitCVinitialized == 1);
#endif
for (filePtr = tsdPtr->firstFileHandlerPtr; filePtr != NULL; filePtr = filePtr->nextPtr) {
if (filePtr->fd != fileEvPtr->fd) {
continue;
}
/*
* The code is tricky for two reasons:
* 1. The file handler's desired events could have changed since the
|
| ︙ | | | ︙ | |
925
926
927
928
929
930
931
932
933
934
935
936
937
938
|
*/
struct timeval timeout, *timeoutPtr;
int numFound;
#endif /* TCL_THREADS */
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
/*
* Set up the timeout structure. Note that if there are no events to
* check for, we return with a negative result rather than blocking
* forever.
*/
if (timePtr != NULL) {
|
>
>
>
>
>
>
>
|
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
|
*/
struct timeval timeout, *timeoutPtr;
int numFound;
#endif /* TCL_THREADS */
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
/*
* Check if InitNotifier was called before in this thread
*/
#ifndef __CYGWIN__
assert(tsdPtr->waitCVinitialized == 1);
#endif
/*
* Set up the timeout structure. Note that if there are no events to
* check for, we return with a negative result rather than blocking
* forever.
*/
if (timePtr != NULL) {
|
| ︙ | | | ︙ | |
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
|
} else {
timeoutPtr = NULL;
#endif /* !TCL_THREADS */
}
#ifdef TCL_THREADS
/*
* Place this thread on the list of interested threads, signal the
* notifier thread, and wait for a response or a timeout.
*/
#if LAZY_THREAD_CREATE == 1
StartNotifierThread();
#endif
#ifdef __CYGWIN__
if (!tsdPtr->hwnd) {
WNDCLASS class;
class.style = 0;
class.cbClsExtra = 0;
|
|
>
|
<
<
|
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
|
} else {
timeoutPtr = NULL;
#endif /* !TCL_THREADS */
}
#ifdef TCL_THREADS
/*
* Start notifier thread and place this thread on the list of
* interested threads, signal the notifier thread, and wait for a
* response or a timeout.
*/
StartNotifierThread();
#ifdef __CYGWIN__
if (!tsdPtr->hwnd) {
WNDCLASS class;
class.style = 0;
class.cbClsExtra = 0;
|
| ︙ | | | ︙ | |
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
|
class.lpszClassName, 0, 0, 0, 0, 0, NULL, NULL,
TclWinGetTclInstance(), NULL);
tsdPtr->event = CreateEventW(NULL, 1 /* manual */,
0 /* !signaled */, NULL);
}
#endif /* __CYGWIN */
Tcl_MutexLock(¬ifierMutex);
if (timePtr != NULL && timePtr->sec == 0 && (timePtr->usec == 0
#if defined(__APPLE__) && defined(__LP64__)
/*
* On 64-bit Darwin, pthread_cond_timedwait() appears to have
* a bug that causes it to wait forever when passed an
* absolute time which has already been exceeded by the system
|
|
|
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
|
class.lpszClassName, 0, 0, 0, 0, 0, NULL, NULL,
TclWinGetTclInstance(), NULL);
tsdPtr->event = CreateEventW(NULL, 1 /* manual */,
0 /* !signaled */, NULL);
}
#endif /* __CYGWIN */
pthread_mutex_lock(¬ifierInitMutex);
if (timePtr != NULL && timePtr->sec == 0 && (timePtr->usec == 0
#if defined(__APPLE__) && defined(__LP64__)
/*
* On 64-bit Darwin, pthread_cond_timedwait() appears to have
* a bug that causes it to wait forever when passed an
* absolute time which has already been exceeded by the system
|
| ︙ | | | ︙ | |
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
|
DWORD timeout;
if (timePtr) {
timeout = timePtr->sec * 1000 + timePtr->usec / 1000;
} else {
timeout = 0xFFFFFFFF;
}
Tcl_MutexUnlock(¬ifierMutex);
MsgWaitForMultipleObjects(1, &tsdPtr->event, 0, timeout, 1279);
Tcl_MutexLock(¬ifierMutex);
}
#else
Tcl_ConditionWait(&tsdPtr->waitCV, ¬ifierMutex, timePtr);
#endif /* __CYGWIN__ */
}
tsdPtr->eventReady = 0;
#ifdef __CYGWIN__
while (PeekMessageW(&msg, NULL, 0, 0, 0)) {
/*
|
|
|
|
|
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
|
DWORD timeout;
if (timePtr) {
timeout = timePtr->sec * 1000 + timePtr->usec / 1000;
} else {
timeout = 0xFFFFFFFF;
}
pthread_mutex_unlock(¬ifierMutex);
MsgWaitForMultipleObjects(1, &tsdPtr->event, 0, timeout, 1279);
pthread_mutex_lock(¬ifierMutex);
}
#else
pthread_cond_wait(&tsdPtr->waitCV, ¬ifierMutex);
#endif /* __CYGWIN__ */
}
tsdPtr->eventReady = 0;
#ifdef __CYGWIN__
while (PeekMessageW(&msg, NULL, 0, 0, 0)) {
/*
|
| ︙ | | | ︙ | |
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
|
fileEvPtr->header.proc = FileHandlerEventProc;
fileEvPtr->fd = filePtr->fd;
Tcl_QueueEvent((Tcl_Event *) fileEvPtr, TCL_QUEUE_TAIL);
}
filePtr->readyMask = mask;
}
#ifdef TCL_THREADS
Tcl_MutexUnlock(¬ifierMutex);
#endif /* TCL_THREADS */
return 0;
}
}
#ifdef TCL_THREADS
/*
|
|
|
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
|
fileEvPtr->header.proc = FileHandlerEventProc;
fileEvPtr->fd = filePtr->fd;
Tcl_QueueEvent((Tcl_Event *) fileEvPtr, TCL_QUEUE_TAIL);
}
filePtr->readyMask = mask;
}
#ifdef TCL_THREADS
pthread_mutex_unlock(¬ifierInitMutex);
#endif /* TCL_THREADS */
return 0;
}
}
#ifdef TCL_THREADS
/*
|
| ︙ | | | ︙ | |
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
|
"could not make receive pipe close-on-exec");
}
if (fcntl(fds[1], F_SETFD, FD_CLOEXEC) < 0) {
Tcl_Panic("NotifierThreadProc: %s",
"could not make trigger pipe close-on-exec");
}
// fprintf(stderr, "=== Starting Notifier Thread\n");
/*
* Install the write end of the pipe into the global variable.
*/
Tcl_MutexLock(¬ifierMutex);
triggerPipe = fds[1];
/*
* Signal any threads that are waiting.
*/
Tcl_ConditionNotify(¬ifierCV);
Tcl_MutexUnlock(¬ifierMutex);
/*
* Look for file events and report them to interested threads.
*/
while (1) {
FD_ZERO(&readableMask);
FD_ZERO(&writableMask);
FD_ZERO(&exceptionMask);
/*
* Compute the logical OR of the select masks from all the waiting
* notifiers.
*/
Tcl_MutexLock(¬ifierMutex);
timePtr = NULL;
for (tsdPtr = waitingListPtr; tsdPtr; tsdPtr = tsdPtr->nextPtr) {
for (i = tsdPtr->numFdBits-1; i >= 0; --i) {
if (FD_ISSET(i, &tsdPtr->checkMasks.readable)) {
FD_SET(i, &readableMask);
}
if (FD_ISSET(i, &tsdPtr->checkMasks.writable)) {
|
<
<
<
<
>
>
|
|
|
>
|
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
|
"could not make receive pipe close-on-exec");
}
if (fcntl(fds[1], F_SETFD, FD_CLOEXEC) < 0) {
Tcl_Panic("NotifierThreadProc: %s",
"could not make trigger pipe close-on-exec");
}
/*
* Install the write end of the pipe into the global variable.
*/
/*
* Signal any threads that are waiting.
*/
pthread_mutex_lock(¬ifierMutex);
triggerPipe = fds[1];
pthread_cond_broadcast(¬ifierCV);
pthread_mutex_unlock(¬ifierMutex);
/*
* Look for file events and report them to interested threads.
*/
while (1) {
FD_ZERO(&readableMask);
FD_ZERO(&writableMask);
FD_ZERO(&exceptionMask);
/*
* Compute the logical OR of the select masks from all the waiting
* notifiers.
*/
pthread_mutex_lock(¬ifierMutex);
timePtr = NULL;
for (tsdPtr = waitingListPtr; tsdPtr; tsdPtr = tsdPtr->nextPtr) {
for (i = tsdPtr->numFdBits-1; i >= 0; --i) {
if (FD_ISSET(i, &tsdPtr->checkMasks.readable)) {
FD_SET(i, &readableMask);
}
if (FD_ISSET(i, &tsdPtr->checkMasks.writable)) {
|
| ︙ | | | ︙ | |
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
|
* bits that were present when the thread tried to poll.
*/
tsdPtr->pollState |= POLL_DONE;
timePtr = &poll;
}
}
Tcl_MutexUnlock(¬ifierMutex);
/*
* Set up the select mask to include the receive pipe.
*/
if (receivePipe >= numFdBits) {
numFdBits = receivePipe + 1;
}
FD_SET(receivePipe, &readableMask);
if (select(numFdBits, &readableMask, &writableMask, &exceptionMask,
timePtr) == -1) {
/*
* Try again immediately on an error.
*/
continue;
}
/*
* Alert any threads that are waiting on a ready file descriptor.
*/
Tcl_MutexLock(¬ifierMutex);
for (tsdPtr = waitingListPtr; tsdPtr; tsdPtr = tsdPtr->nextPtr) {
found = 0;
for (i = tsdPtr->numFdBits-1; i >= 0; --i) {
if (FD_ISSET(i, &tsdPtr->checkMasks.readable)
&& FD_ISSET(i, &readableMask)) {
FD_SET(i, &tsdPtr->readyMasks.readable);
|
|
<
|
>
|
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
|
* bits that were present when the thread tried to poll.
*/
tsdPtr->pollState |= POLL_DONE;
timePtr = &poll;
}
}
pthread_mutex_unlock(¬ifierMutex);
/*
* Set up the select mask to include the receive pipe.
*/
if (receivePipe >= numFdBits) {
numFdBits = receivePipe + 1;
}
FD_SET(receivePipe, &readableMask);
if (select(numFdBits, &readableMask, &writableMask, &exceptionMask,
timePtr) == -1) {
/*
* Try again immediately on an error.
*/
continue;
}
/*
* Alert any threads that are waiting on a ready file descriptor.
*/
pthread_mutex_lock(¬ifierMutex);
for (tsdPtr = waitingListPtr; tsdPtr; tsdPtr = tsdPtr->nextPtr) {
found = 0;
for (i = tsdPtr->numFdBits-1; i >= 0; --i) {
if (FD_ISSET(i, &tsdPtr->checkMasks.readable)
&& FD_ISSET(i, &readableMask)) {
FD_SET(i, &tsdPtr->readyMasks.readable);
|
| ︙ | | | ︙ | |
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
|
tsdPtr->nextPtr = tsdPtr->prevPtr = NULL;
tsdPtr->onList = 0;
tsdPtr->pollState = 0;
}
#ifdef __CYGWIN__
PostMessageW(tsdPtr->hwnd, 1024, 0, 0);
#else
Tcl_ConditionNotify(&tsdPtr->waitCV);
#endif /* __CYGWIN__ */
}
}
Tcl_MutexUnlock(¬ifierMutex);
/*
* Consume the next byte from the notifier pipe if the pipe was
* readable. Note that there may be multiple bytes pending, but to
* avoid a race condition we only read one at a time.
*/
if (FD_ISSET(receivePipe, &readableMask)) {
i = read(receivePipe, buf, 1);
if ((i == 0) || ((i == 1) && (buf[0] == 'q'))) {
/*
* Someone closed the write end of the pipe or sent us a Quit
* message [Bug: 4139] and then closed the write end of the
|
|
|
<
|
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
|
tsdPtr->nextPtr = tsdPtr->prevPtr = NULL;
tsdPtr->onList = 0;
tsdPtr->pollState = 0;
}
#ifdef __CYGWIN__
PostMessageW(tsdPtr->hwnd, 1024, 0, 0);
#else
pthread_cond_broadcast(&tsdPtr->waitCV);
#endif /* __CYGWIN__ */
}
}
pthread_mutex_unlock(¬ifierMutex);
/*
* Consume the next byte from the notifier pipe if the pipe was
* readable. Note that there may be multiple bytes pending, but to
* avoid a race condition we only read one at a time.
*/
if (FD_ISSET(receivePipe, &readableMask)) {
i = read(receivePipe, buf, 1);
if ((i == 0) || ((i == 1) && (buf[0] == 'q'))) {
/*
* Someone closed the write end of the pipe or sent us a Quit
* message [Bug: 4139] and then closed the write end of the
|
| ︙ | | | ︙ | |
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
|
/*
* Clean up the read end of the pipe and signal any threads waiting on
* termination of the notifier thread.
*/
fprintf(stderr, "=== Stopping Notifier Thread\n");
close(receivePipe);
Tcl_MutexLock(¬ifierMutex);
triggerPipe = -1;
Tcl_ConditionNotify(¬ifierCV);
Tcl_MutexUnlock(¬ifierMutex);
TclpThreadExit(0);
}
#if defined(HAVE_PTHREAD_ATFORK) && !defined(__APPLE__) && !defined(__hpux)
/*
*----------------------------------------------------------------------
|
>
|
|
|
|
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
|
/*
* Clean up the read end of the pipe and signal any threads waiting on
* termination of the notifier thread.
*/
fprintf(stderr, "=== Stopping Notifier Thread\n");
close(receivePipe);
pthread_mutex_lock(¬ifierMutex);
triggerPipe = -1;
pthread_cond_broadcast(¬ifierCV);
pthread_mutex_unlock(¬ifierMutex);
TclpThreadExit(0);
}
#if defined(HAVE_PTHREAD_ATFORK) && !defined(__APPLE__) && !defined(__hpux)
/*
*----------------------------------------------------------------------
|
| ︙ | | | ︙ | |
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
|
*
*----------------------------------------------------------------------
*/
static void
AtForkPrepare(void)
{
#if DEACTIVATE_ATFORK_MUTEX == 0
Tcl_MutexLock(¬ifierMutex);
TclpMasterLock();
TclpMutexLock();
#endif
}
/*
*----------------------------------------------------------------------
*
* AtForkParent --
|
|
|
<
<
|
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
|
*
*----------------------------------------------------------------------
*/
static void
AtForkPrepare(void)
{
#if RESET_ATFORK_MUTEX == 0
pthread_mutex_lock(¬ifierInitMutex);
#endif
}
/*
*----------------------------------------------------------------------
*
* AtForkParent --
|
| ︙ | | | ︙ | |
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
|
*
*----------------------------------------------------------------------
*/
static void
AtForkParent(void)
{
#if DEACTIVATE_ATFORK_MUTEX == 0
TclpMutexUnlock();
TclpMasterUnlock();
Tcl_MutexUnlock(¬ifierMutex);
#endif
//fprintf(stderr, "==== atParent %d notifierCount %d atForkInit %d\n", atForkInit, notifierCount, atForkInit);
}
/*
*----------------------------------------------------------------------
*
* AtForkChild --
*
* Unlock and reinstall the notifier in the child after a fork.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static void
AtForkChild(void)
{
#if DEACTIVATE_ATFORK_MUTEX == 0
TclpMutexUnlock();
TclpMasterUnlock();
TclMutexUnlockAndFinalize(¬ifierMutex);
#endif
#if LAZY_THREAD_CREATE == 1
//Tcl_MutexLock(¬ifierInitMutex);
notifierThreadRunning = 0;
if (notifierCount > 0) {
Tcl_ConditionFinalize(¬ifierCV);
notifierCount = 0;
processIDInitialized = 0;
close(triggerPipe);
triggerPipe = -1;
}
//Tcl_MutexUnlock(¬ifierInitMutex);
#endif
// fprintf(stderr, "==== AtForkChild() notifierCount %d notifierThreadRunning %d atForkInit %d\n",notifierCount,notifierThreadRunning, atForkInit);
Tcl_InitNotifier();
}
#endif /* HAVE_PTHREAD_ATFORK */
#endif /* TCL_THREADS */
#endif /* !HAVE_COREFOUNDATION */
|
|
<
<
|
<
>
>
>
>
|
|
<
>
>
|
>
<
<
>
|
>
>
>
>
|
>
|
|
>
>
>
>
>
|
|
|
>
>
>
>
>
>
|
<
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
|
*
*----------------------------------------------------------------------
*/
static void
AtForkParent(void)
{
#if RESET_ATFORK_MUTEX == 0
pthread_mutex_unlock(¬ifierInitMutex);
#endif
}
/*
*----------------------------------------------------------------------
*
* AtForkChild --
*
* Unlock and reinstall the notifier in the child after a fork.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static void
AtForkChild(void)
{
if (notifierThreadRunning == 1) {
pthread_cond_destroy(¬ifierCV);
}
#if RESET_ATFORK_MUTEX == 0
pthread_mutex_unlock(¬ifierInitMutex);
#else
pthread_mutex_init(¬ifierInitMutex, NULL);
pthread_mutex_init(¬ifierMutex, NULL);
#endif
pthread_cond_init(¬ifierCV, NULL);
/*
* notifierThreadRunning == 1: thread is running, (there might be data in notifier lists)
* atForkInit == 0: InitNotifier was never called
* notifierCount != 0: unbalanced InitNotifier() / FinalizeNotifier calls
* waitingListPtr != 0: there are threads currently waiting for events.
*/
if (atForkInit == 1) {
notifierCount = 0;
if (notifierThreadRunning == 1) {
#ifndef __CYGWIN__
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
#endif
notifierThreadRunning = 0;
close(triggerPipe);
triggerPipe = -1;
/*
* The waitingListPtr might contain event info from multiple
* threads, which are invalid here, so setting it to NULL is not
* unreasonable.
*/
waitingListPtr = NULL;
/*
* The tsdPtr from before the fork is copied as well. But since
* we are paranoic, we don't trust its condvar and reset it.
*/
#ifndef __CYGWIN__
assert(tsdPtr->waitCVinitialized == 1);
pthread_cond_destroy(&tsdPtr->waitCV);
pthread_cond_init(&tsdPtr->waitCV, NULL);
#endif
/*
* The list of registered event handlers at fork time is in
* tsdPtr->firstFileHandlerPtr;
*/
}
}
assert(notifierCount == 0);
assert(triggerPipe == -1);
assert(waitingListPtr == NULL);
Tcl_InitNotifier();
}
#endif /* HAVE_PTHREAD_ATFORK */
#endif /* TCL_THREADS */
#endif /* !HAVE_COREFOUNDATION */
|
| ︙ | | | ︙ | |