| 1 |
#define AT_FORK_INIT_VALUE 0 |
| 2 |
#define RESET_ATFORK_MUTEX 1 |
| 3 |
/* |
| 4 |
* tclUnixNotify.c -- |
| 5 |
* |
| 6 |
* This file contains the implementation of the select()-based |
| 7 |
* Unix-specific notifier, which is the lowest-level part of the Tcl |
| 8 |
* event loop. This file works together with generic/tclNotify.c. |
| 9 |
* |
| 10 |
* Copyright (c) 1995-1997 Sun Microsystems, Inc. |
| 11 |
* |
| 12 |
* See the file "license.terms" for information on usage and redistribution |
| 13 |
* of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 14 |
*/ |
| 15 |
|
| 16 |
#include "tclInt.h" |
| 17 |
#ifndef HAVE_COREFOUNDATION /* Darwin/Mac OS X CoreFoundation notifier is |
| 18 |
* in tclMacOSXNotify.c */ |
| 19 |
#include <signal.h> |
| 20 |
|
| 21 |
/* |
| 22 |
* This structure is used to keep track of the notifier info for a registered |
| 23 |
* file. |
| 24 |
*/ |
| 25 |
|
| 26 |
typedef struct FileHandler { |
| 27 |
int fd; |
| 28 |
int mask; /* Mask of desired events: TCL_READABLE, |
| 29 |
* etc. */ |
| 30 |
int readyMask; /* Mask of events that have been seen since |
| 31 |
* the last time file handlers were invoked |
| 32 |
* for this file. */ |
| 33 |
Tcl_FileProc *proc; /* Function to call, in the style of |
| 34 |
* Tcl_CreateFileHandler. */ |
| 35 |
void *clientData; /* Argument to pass to proc. */ |
| 36 |
struct FileHandler *nextPtr;/* Next in list of all files we care about. */ |
| 37 |
} FileHandler; |
| 38 |
|
| 39 |
/* |
| 40 |
* The following structure is what is added to the Tcl event queue when file |
| 41 |
* handlers are ready to fire. |
| 42 |
*/ |
| 43 |
|
| 44 |
typedef struct FileHandlerEvent { |
| 45 |
Tcl_Event header; /* Information that is standard for all |
| 46 |
* events. */ |
| 47 |
int fd; /* File descriptor that is ready. Used to find |
| 48 |
* the FileHandler structure for the file |
| 49 |
* (can't point directly to the FileHandler |
| 50 |
* structure because it could go away while |
| 51 |
* the event is queued). */ |
| 52 |
} FileHandlerEvent; |
| 53 |
|
| 54 |
/* |
| 55 |
* The following structure contains a set of select() masks to track readable, |
| 56 |
* writable, and exception conditions. |
| 57 |
*/ |
| 58 |
|
| 59 |
typedef struct SelectMasks { |
| 60 |
fd_set readable; |
| 61 |
fd_set writable; |
| 62 |
fd_set exception; |
| 63 |
} SelectMasks; |
| 64 |
|
| 65 |
/* |
| 66 |
* The following static structure contains the state information for the |
| 67 |
* select based implementation of the Tcl notifier. One of these structures is |
| 68 |
* created for each thread that is using the notifier. |
| 69 |
*/ |
| 70 |
|
| 71 |
typedef struct ThreadSpecificData { |
| 72 |
FileHandler *firstFileHandlerPtr; |
| 73 |
/* Pointer to head of file handler list. */ |
| 74 |
SelectMasks checkMasks; /* This structure is used to build up the |
| 75 |
* masks to be used in the next call to |
| 76 |
* select. Bits are set in response to calls |
| 77 |
* to Tcl_CreateFileHandler. */ |
| 78 |
SelectMasks readyMasks; /* This array reflects the readable/writable |
| 79 |
* conditions that were found to exist by the |
| 80 |
* last call to select. */ |
| 81 |
int numFdBits; /* Number of valid bits in checkMasks (one |
| 82 |
* more than highest fd for which |
| 83 |
* Tcl_WatchFile has been called). */ |
| 84 |
#ifdef TCL_THREADS |
| 85 |
int onList; /* True if it is in this list */ |
| 86 |
unsigned int pollState; /* pollState is used to implement a polling |
| 87 |
* handshake between each thread and the |
| 88 |
* notifier thread. Bits defined below. */ |
| 89 |
struct ThreadSpecificData *nextPtr, *prevPtr; |
| 90 |
/* All threads that are currently waiting on |
| 91 |
* an event have their ThreadSpecificData |
| 92 |
* structure on a doubly-linked listed formed |
| 93 |
* from these pointers. You must hold the |
| 94 |
* notifierMutex lock before accessing these |
| 95 |
* fields. */ |
| 96 |
#ifdef __CYGWIN__ |
| 97 |
void *event; /* Any other thread alerts a notifier that an |
| 98 |
* event is ready to be processed by sending |
| 99 |
* this event. */ |
| 100 |
void *hwnd; /* Messaging window. */ |
| 101 |
#else /* !__CYGWIN__ */ |
| 102 |
pthread_cond_t waitCV; /* Any other thread alerts a notifier that an |
| 103 |
* event is ready to be processed by signaling |
| 104 |
* this condition variable. */ |
| 105 |
#endif /* __CYGWIN__ */ |
| 106 |
int waitCVinitialized; /* Variable to flag initialization of the |
| 107 |
* structure. */ |
| 108 |
int eventReady; /* True if an event is ready to be processed. |
| 109 |
* Used as condition flag together with waitCV |
| 110 |
* above. */ |
| 111 |
#endif /* TCL_THREADS */ |
| 112 |
} ThreadSpecificData; |
| 113 |
|
| 114 |
static Tcl_ThreadDataKey dataKey; |
| 115 |
|
| 116 |
#ifdef TCL_THREADS |
| 117 |
/* |
| 118 |
* The following static indicates the number of threads that have initialized |
| 119 |
* notifiers. |
| 120 |
* |
| 121 |
* You must hold the notifierMutex lock before accessing this variable. |
| 122 |
*/ |
| 123 |
|
| 124 |
static int notifierCount = 0; |
| 125 |
|
| 126 |
/* |
| 127 |
* The following variable points to the head of a doubly-linked list of |
| 128 |
* ThreadSpecificData structures for all threads that are currently waiting on |
| 129 |
* an event. |
| 130 |
* |
| 131 |
* You must hold the notifierMutex lock before accessing this list. |
| 132 |
*/ |
| 133 |
|
| 134 |
static ThreadSpecificData *waitingListPtr = NULL; |
| 135 |
|
| 136 |
/* |
| 137 |
* The notifier thread spends all its time in select() waiting for a file |
| 138 |
* descriptor associated with one of the threads on the waitingListPtr list to |
| 139 |
* do something interesting. But if the contents of the waitingListPtr list |
| 140 |
* ever changes, we need to wake up and restart the select() system call. You |
| 141 |
* can wake up the notifier thread by writing a single byte to the file |
| 142 |
* descriptor defined below. This file descriptor is the input-end of a pipe |
| 143 |
* and the notifier thread is listening for data on the output-end of the same |
| 144 |
* pipe. Hence writing to this file descriptor will cause the select() system |
| 145 |
* call to return and wake up the notifier thread. |
| 146 |
* |
| 147 |
* You must hold the notifierMutex lock before writing to the pipe. |
| 148 |
*/ |
| 149 |
|
| 150 |
static int triggerPipe = -1; |
| 151 |
|
| 152 |
/* |
| 153 |
* The notifierMutex locks access to all of the global notifier state. |
| 154 |
*/ |
| 155 |
|
| 156 |
static pthread_mutex_t notifierInitMutex = PTHREAD_MUTEX_INITIALIZER; |
| 157 |
static pthread_mutex_t notifierMutex = PTHREAD_MUTEX_INITIALIZER; |
| 158 |
/* |
| 159 |
* The following static indicates if the notifier thread is running. |
| 160 |
* |
| 161 |
* You must hold the notifierInitMutex before accessing this variable. |
| 162 |
*/ |
| 163 |
|
| 164 |
static int notifierThreadRunning = 0; |
| 165 |
|
| 166 |
/* |
| 167 |
* The notifier thread signals the notifierCV when it has finished |
| 168 |
* initializing the triggerPipe and right before the notifier thread |
| 169 |
* terminates. |
| 170 |
*/ |
| 171 |
|
| 172 |
static pthread_cond_t notifierCV = PTHREAD_COND_INITIALIZER; |
| 173 |
|
| 174 |
/* |
| 175 |
* The pollState bits: |
| 176 |
* |
| 177 |
* POLL_WANT is set by each thread before it waits on its condition variable. |
| 178 |
* It is checked by the notifier before it does select. |
| 179 |
* |
| 180 |
* POLL_DONE is set by the notifier if it goes into select after seeing |
| 181 |
* POLL_WANT. The idea is to ensure it tries a select with the same bits |
| 182 |
* the initial thread had set. |
| 183 |
*/ |
| 184 |
|
| 185 |
#define POLL_WANT 0x1 |
| 186 |
#define POLL_DONE 0x2 |
| 187 |
|
| 188 |
/* |
| 189 |
* This is the thread ID of the notifier thread that does select. |
| 190 |
*/ |
| 191 |
|
| 192 |
static Tcl_ThreadId notifierThread; |
| 193 |
#endif /* TCL_THREADS */ |
| 194 |
|
| 195 |
/* |
| 196 |
* Static routines defined in this file. |
| 197 |
*/ |
| 198 |
|
| 199 |
#ifdef TCL_THREADS |
| 200 |
static void NotifierThreadProc(void *clientData); |
| 201 |
#if defined(HAVE_PTHREAD_ATFORK) |
| 202 |
static int atForkInit = AT_FORK_INIT_VALUE; |
| 203 |
static void AtForkPrepare(void); |
| 204 |
static void AtForkParent(void); |
| 205 |
static void AtForkChild(void); |
| 206 |
#endif /* HAVE_PTHREAD_ATFORK */ |
| 207 |
#endif /* TCL_THREADS */ |
| 208 |
static int FileHandlerEventProc(Tcl_Event *evPtr, int flags); |
| 209 |
|
| 210 |
/* |
| 211 |
* Import of Windows API when building threaded with Cygwin. |
| 212 |
*/ |
| 213 |
|
| 214 |
#if defined(TCL_THREADS) && defined(__CYGWIN__) |
| 215 |
typedef struct { |
| 216 |
void *hwnd; /* Messaging window. */ |
| 217 |
unsigned int *message; /* Message payload. */ |
| 218 |
size_t wParam; /* Event-specific "word" parameter. */ |
| 219 |
size_t lParam; /* Event-specific "long" parameter. */ |
| 220 |
int time; /* Event timestamp. */ |
| 221 |
int x; /* Event location (where meaningful). */ |
| 222 |
int y; |
| 223 |
int lPrivate; |
| 224 |
} MSG; |
| 225 |
|
| 226 |
typedef struct { |
| 227 |
unsigned int style; |
| 228 |
void *lpfnWndProc; |
| 229 |
int cbClsExtra; |
| 230 |
int cbWndExtra; |
| 231 |
void *hInstance; |
| 232 |
void *hIcon; |
| 233 |
void *hCursor; |
| 234 |
void *hbrBackground; |
| 235 |
const void *lpszMenuName; |
| 236 |
const void *lpszClassName; |
| 237 |
} WNDCLASSW; |
| 238 |
|
| 239 |
extern void CloseHandle(void *); |
| 240 |
extern void * CreateEventW(void *, unsigned char, unsigned char, |
| 241 |
void *); |
| 242 |
extern void * CreateWindowExW(void *, const void *, const void *, |
| 243 |
unsigned int, int, int, int, int, void *, void *, void *, void *); |
| 244 |
extern unsigned int DefWindowProcW(void *, int, void *, void *); |
| 245 |
extern unsigned char DestroyWindow(void *); |
| 246 |
extern int DispatchMessageW(const MSG *); |
| 247 |
extern unsigned char GetMessageW(MSG *, void *, int, int); |
| 248 |
extern void MsgWaitForMultipleObjects(unsigned int, void *, |
| 249 |
unsigned char, unsigned int, unsigned int); |
| 250 |
extern unsigned char PeekMessageW(MSG *, void *, int, int, int); |
| 251 |
extern unsigned char PostMessageW(void *, unsigned int, void *, |
| 252 |
void *); |
| 253 |
extern void PostQuitMessage(int); |
| 254 |
extern void * RegisterClassW(const WNDCLASSW *); |
| 255 |
extern unsigned char ResetEvent(void *); |
| 256 |
extern unsigned char TranslateMessage(const MSG *); |
| 257 |
|
| 258 |
/* |
| 259 |
* Threaded-cygwin specific constants and functions in this file: |
| 260 |
*/ |
| 261 |
|
| 262 |
static const wchar_t *NotfyClassName = L"TclNotifier"; |
| 263 |
static unsigned int NotifierProc(void *hwnd, unsigned int message, |
| 264 |
void *wParam, void *lParam); |
| 265 |
#endif /* TCL_THREADS && __CYGWIN__ */ |
| 266 |
|
| 267 |
#if TCL_THREADS |
| 268 |
/* |
| 269 |
*---------------------------------------------------------------------- |
| 270 |
* |
| 271 |
* StartNotifierThread -- |
| 272 |
* |
| 273 |
* Start a notfier thread and wait for the notifier pipe to be created. |
| 274 |
* |
| 275 |
* Results: |
| 276 |
* None. |
| 277 |
* |
| 278 |
* Side effects: |
| 279 |
* Running Thread. |
| 280 |
* |
| 281 |
*---------------------------------------------------------------------- |
| 282 |
*/ |
| 283 |
static void |
| 284 |
StartNotifierThread(const char *proc) |
| 285 |
{ |
| 286 |
if (!notifierThreadRunning) { |
| 287 |
pthread_mutex_lock(¬ifierInitMutex); |
| 288 |
if (!notifierThreadRunning) { |
| 289 |
if (TclpThreadCreate(¬ifierThread, NotifierThreadProc, NULL, |
| 290 |
TCL_THREAD_STACK_DEFAULT, TCL_THREAD_JOINABLE) != TCL_OK) { |
| 291 |
Tcl_Panic("%s: unable to start notifier thread", proc); |
| 292 |
} |
| 293 |
|
| 294 |
pthread_mutex_lock(¬ifierMutex); |
| 295 |
/* |
| 296 |
* Wait for the notifier pipe to be created. |
| 297 |
*/ |
| 298 |
|
| 299 |
while (triggerPipe < 0) { |
| 300 |
pthread_cond_wait(¬ifierCV, ¬ifierMutex); |
| 301 |
} |
| 302 |
pthread_mutex_unlock(¬ifierMutex); |
| 303 |
|
| 304 |
notifierThreadRunning = 1; |
| 305 |
} |
| 306 |
pthread_mutex_unlock(¬ifierInitMutex); |
| 307 |
} |
| 308 |
} |
| 309 |
#endif /* TCL_THREADS */ |
| 310 |
|
| 311 |
/* |
| 312 |
*---------------------------------------------------------------------- |
| 313 |
* |
| 314 |
* Tcl_InitNotifier -- |
| 315 |
* |
| 316 |
* Initializes the platform specific notifier state. |
| 317 |
* |
| 318 |
* Results: |
| 319 |
* Returns a handle to the notifier state for this thread. |
| 320 |
* |
| 321 |
* Side effects: |
| 322 |
* None. |
| 323 |
* |
| 324 |
*---------------------------------------------------------------------- |
| 325 |
*/ |
| 326 |
|
| 327 |
void * |
| 328 |
Tcl_InitNotifier(void) |
| 329 |
{ |
| 330 |
if (tclNotifierHooks.initNotifierProc) { |
| 331 |
return tclNotifierHooks.initNotifierProc(); |
| 332 |
} else { |
| 333 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 334 |
|
| 335 |
#ifdef TCL_THREADS |
| 336 |
tsdPtr->eventReady = 0; |
| 337 |
|
| 338 |
/* |
| 339 |
* Initialize thread specific condition variable for this thread. |
| 340 |
*/ |
| 341 |
if (tsdPtr->waitCVinitialized == 0) { |
| 342 |
#ifdef __CYGWIN__ |
| 343 |
WNDCLASSW clazz; |
| 344 |
|
| 345 |
clazz.style = 0; |
| 346 |
clazz.cbClsExtra = 0; |
| 347 |
clazz.cbWndExtra = 0; |
| 348 |
clazz.hInstance = TclWinGetTclInstance(); |
| 349 |
clazz.hbrBackground = NULL; |
| 350 |
clazz.lpszMenuName = NULL; |
| 351 |
clazz.lpszClassName = NotfyClassName; |
| 352 |
clazz.lpfnWndProc = (void *)NotifierProc; |
| 353 |
clazz.hIcon = NULL; |
| 354 |
clazz.hCursor = NULL; |
| 355 |
|
| 356 |
RegisterClassW(&clazz); |
| 357 |
tsdPtr->hwnd = CreateWindowExW(NULL, clazz.lpszClassName, |
| 358 |
clazz.lpszClassName, 0, 0, 0, 0, 0, NULL, NULL, |
| 359 |
clazz.hInstance, NULL); |
| 360 |
tsdPtr->event = CreateEventW(NULL, 1 /* manual */, |
| 361 |
0 /* !signaled */, NULL); |
| 362 |
#else |
| 363 |
pthread_cond_init(&tsdPtr->waitCV, NULL); |
| 364 |
#endif /* __CYGWIN__ */ |
| 365 |
tsdPtr->waitCVinitialized = 1; |
| 366 |
} |
| 367 |
|
| 368 |
pthread_mutex_lock(¬ifierInitMutex); |
| 369 |
#if defined(HAVE_PTHREAD_ATFORK) |
| 370 |
/* |
| 371 |
* Install pthread_atfork handlers to clean up the notifier in the |
| 372 |
* child of a fork. |
| 373 |
*/ |
| 374 |
|
| 375 |
if (!atForkInit) { |
| 376 |
int result = pthread_atfork(AtForkPrepare, AtForkParent, AtForkChild); |
| 377 |
|
| 378 |
if (result) { |
| 379 |
Tcl_Panic("Tcl_InitNotifier: pthread_atfork failed"); |
| 380 |
} |
| 381 |
atForkInit = 1; |
| 382 |
} |
| 383 |
#endif /* HAVE_PTHREAD_ATFORK */ |
| 384 |
|
| 385 |
notifierCount++; |
| 386 |
pthread_mutex_unlock(¬ifierInitMutex); |
| 387 |
|
| 388 |
#endif /* TCL_THREADS */ |
| 389 |
return tsdPtr; |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
/* |
| 394 |
*---------------------------------------------------------------------- |
| 395 |
* |
| 396 |
* Tcl_FinalizeNotifier -- |
| 397 |
* |
| 398 |
* This function is called to cleanup the notifier state before a thread |
| 399 |
* is terminated. |
| 400 |
* |
| 401 |
* Results: |
| 402 |
* None. |
| 403 |
* |
| 404 |
* Side effects: |
| 405 |
* May terminate the background notifier thread if this is the last |
| 406 |
* notifier instance. |
| 407 |
* |
| 408 |
*---------------------------------------------------------------------- |
| 409 |
*/ |
| 410 |
|
| 411 |
void |
| 412 |
Tcl_FinalizeNotifier( |
| 413 |
void *clientData) |
| 414 |
{ |
| 415 |
if (tclNotifierHooks.finalizeNotifierProc) { |
| 416 |
tclNotifierHooks.finalizeNotifierProc(clientData); |
| 417 |
return; |
| 418 |
} else { |
| 419 |
#ifdef TCL_THREADS |
| 420 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 421 |
|
| 422 |
pthread_mutex_lock(¬ifierInitMutex); |
| 423 |
notifierCount--; |
| 424 |
|
| 425 |
/* |
| 426 |
* If this is the last thread to use the notifier, close the notifier |
| 427 |
* pipe and wait for the background thread to terminate. |
| 428 |
*/ |
| 429 |
|
| 430 |
if (notifierCount == 0 && triggerPipe != -1) { |
| 431 |
if (write(triggerPipe, "q", 1) != 1) { |
| 432 |
Tcl_Panic("Tcl_FinalizeNotifier: %s", |
| 433 |
"unable to write 'q' to triggerPipe"); |
| 434 |
} |
| 435 |
close(triggerPipe); |
| 436 |
pthread_mutex_lock(¬ifierMutex); |
| 437 |
while(triggerPipe != -1) { |
| 438 |
pthread_cond_wait(¬ifierCV, ¬ifierMutex); |
| 439 |
} |
| 440 |
pthread_mutex_unlock(¬ifierMutex); |
| 441 |
if (notifierThreadRunning) { |
| 442 |
int result = pthread_join((pthread_t) notifierThread, NULL); |
| 443 |
|
| 444 |
if (result) { |
| 445 |
Tcl_Panic("Tcl_FinalizeNotifier: %s", |
| 446 |
"unable to join notifier thread"); |
| 447 |
} |
| 448 |
notifierThreadRunning = 0; |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
/* |
| 453 |
* Clean up any synchronization objects in the thread local storage. |
| 454 |
*/ |
| 455 |
|
| 456 |
#ifdef __CYGWIN__ |
| 457 |
DestroyWindow(tsdPtr->hwnd); |
| 458 |
CloseHandle(tsdPtr->event); |
| 459 |
#else /* __CYGWIN__ */ |
| 460 |
pthread_cond_destroy(&tsdPtr->waitCV); |
| 461 |
#endif /* __CYGWIN__ */ |
| 462 |
tsdPtr->waitCVinitialized = 0; |
| 463 |
|
| 464 |
pthread_mutex_unlock(¬ifierInitMutex); |
| 465 |
#endif /* TCL_THREADS */ |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
/* |
| 470 |
*---------------------------------------------------------------------- |
| 471 |
* |
| 472 |
* Tcl_AlertNotifier -- |
| 473 |
* |
| 474 |
* Wake up the specified notifier from any thread. This routine is called |
| 475 |
* by the platform independent notifier code whenever the Tcl_ThreadAlert |
| 476 |
* routine is called. This routine is guaranteed not to be called on a |
| 477 |
* given notifier after Tcl_FinalizeNotifier is called for that notifier. |
| 478 |
* |
| 479 |
* Results: |
| 480 |
* None. |
| 481 |
* |
| 482 |
* Side effects: |
| 483 |
* Signals the notifier condition variable for the specified notifier. |
| 484 |
* |
| 485 |
*---------------------------------------------------------------------- |
| 486 |
*/ |
| 487 |
|
| 488 |
void |
| 489 |
Tcl_AlertNotifier( |
| 490 |
void *clientData) |
| 491 |
{ |
| 492 |
if (tclNotifierHooks.alertNotifierProc) { |
| 493 |
tclNotifierHooks.alertNotifierProc(clientData); |
| 494 |
return; |
| 495 |
} else { |
| 496 |
#ifdef TCL_THREADS |
| 497 |
ThreadSpecificData *tsdPtr = (ThreadSpecificData *)clientData; |
| 498 |
|
| 499 |
pthread_mutex_lock(¬ifierMutex); |
| 500 |
tsdPtr->eventReady = 1; |
| 501 |
|
| 502 |
# ifdef __CYGWIN__ |
| 503 |
PostMessageW(tsdPtr->hwnd, 1024, 0, 0); |
| 504 |
# else |
| 505 |
pthread_cond_broadcast(&tsdPtr->waitCV); |
| 506 |
# endif /* __CYGWIN__ */ |
| 507 |
pthread_mutex_unlock(¬ifierMutex); |
| 508 |
#endif /* TCL_THREADS */ |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
/* |
| 513 |
*---------------------------------------------------------------------- |
| 514 |
* |
| 515 |
* Tcl_SetTimer -- |
| 516 |
* |
| 517 |
* This function sets the current notifier timer value. This interface is |
| 518 |
* not implemented in this notifier because we are always running inside |
| 519 |
* of Tcl_DoOneEvent. |
| 520 |
* |
| 521 |
* Results: |
| 522 |
* None. |
| 523 |
* |
| 524 |
* Side effects: |
| 525 |
* None. |
| 526 |
* |
| 527 |
*---------------------------------------------------------------------- |
| 528 |
*/ |
| 529 |
|
| 530 |
void |
| 531 |
Tcl_SetTimer( |
| 532 |
const Tcl_Time *timePtr) /* Timeout value, may be NULL. */ |
| 533 |
{ |
| 534 |
if (tclNotifierHooks.setTimerProc) { |
| 535 |
tclNotifierHooks.setTimerProc(timePtr); |
| 536 |
return; |
| 537 |
} else { |
| 538 |
/* |
| 539 |
* The interval timer doesn't do anything in this implementation, |
| 540 |
* because the only event loop is via Tcl_DoOneEvent, which passes |
| 541 |
* timeout values to Tcl_WaitForEvent. |
| 542 |
*/ |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
/* |
| 547 |
*---------------------------------------------------------------------- |
| 548 |
* |
| 549 |
* Tcl_ServiceModeHook -- |
| 550 |
* |
| 551 |
* This function is invoked whenever the service mode changes. |
| 552 |
* |
| 553 |
* Results: |
| 554 |
* None. |
| 555 |
* |
| 556 |
* Side effects: |
| 557 |
* None. |
| 558 |
* |
| 559 |
*---------------------------------------------------------------------- |
| 560 |
*/ |
| 561 |
|
| 562 |
void |
| 563 |
Tcl_ServiceModeHook( |
| 564 |
int mode) /* Either TCL_SERVICE_ALL, or |
| 565 |
* TCL_SERVICE_NONE. */ |
| 566 |
{ |
| 567 |
if (tclNotifierHooks.serviceModeHookProc) { |
| 568 |
tclNotifierHooks.serviceModeHookProc(mode); |
| 569 |
return; |
| 570 |
} else if (mode == TCL_SERVICE_ALL) { |
| 571 |
#if TCL_THREADS |
| 572 |
StartNotifierThread("Tcl_ServiceModeHook"); |
| 573 |
#endif |
| 574 |
} |
| 575 |
} |
| 576 |
|
| 577 |
/* |
| 578 |
*---------------------------------------------------------------------- |
| 579 |
* |
| 580 |
* Tcl_CreateFileHandler -- |
| 581 |
* |
| 582 |
* This function registers a file handler with the select notifier. |
| 583 |
* |
| 584 |
* Results: |
| 585 |
* None. |
| 586 |
* |
| 587 |
* Side effects: |
| 588 |
* Creates a new file handler structure. |
| 589 |
* |
| 590 |
*---------------------------------------------------------------------- |
| 591 |
*/ |
| 592 |
|
| 593 |
void |
| 594 |
Tcl_CreateFileHandler( |
| 595 |
int fd, /* Handle of stream to watch. */ |
| 596 |
int mask, /* OR'ed combination of TCL_READABLE, |
| 597 |
* TCL_WRITABLE, and TCL_EXCEPTION: indicates |
| 598 |
* conditions under which proc should be |
| 599 |
* called. */ |
| 600 |
Tcl_FileProc *proc, /* Function to call for each selected |
| 601 |
* event. */ |
| 602 |
void *clientData) /* Arbitrary data to pass to proc. */ |
| 603 |
{ |
| 604 |
if (tclNotifierHooks.createFileHandlerProc) { |
| 605 |
tclNotifierHooks.createFileHandlerProc(fd, mask, proc, clientData); |
| 606 |
return; |
| 607 |
} else { |
| 608 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 609 |
FileHandler *filePtr; |
| 610 |
|
| 611 |
for (filePtr = tsdPtr->firstFileHandlerPtr; filePtr != NULL; |
| 612 |
filePtr = filePtr->nextPtr) { |
| 613 |
if (filePtr->fd == fd) { |
| 614 |
break; |
| 615 |
} |
| 616 |
} |
| 617 |
if (filePtr == NULL) { |
| 618 |
filePtr = (FileHandler *)ckalloc(sizeof(FileHandler)); |
| 619 |
filePtr->fd = fd; |
| 620 |
filePtr->readyMask = 0; |
| 621 |
filePtr->nextPtr = tsdPtr->firstFileHandlerPtr; |
| 622 |
tsdPtr->firstFileHandlerPtr = filePtr; |
| 623 |
} |
| 624 |
filePtr->proc = proc; |
| 625 |
filePtr->clientData = clientData; |
| 626 |
filePtr->mask = mask; |
| 627 |
|
| 628 |
/* |
| 629 |
* Update the check masks for this file. |
| 630 |
*/ |
| 631 |
|
| 632 |
if (mask & TCL_READABLE) { |
| 633 |
FD_SET(fd, &tsdPtr->checkMasks.readable); |
| 634 |
} else { |
| 635 |
FD_CLR(fd, &tsdPtr->checkMasks.readable); |
| 636 |
} |
| 637 |
if (mask & TCL_WRITABLE) { |
| 638 |
FD_SET(fd, &tsdPtr->checkMasks.writable); |
| 639 |
} else { |
| 640 |
FD_CLR(fd, &tsdPtr->checkMasks.writable); |
| 641 |
} |
| 642 |
if (mask & TCL_EXCEPTION) { |
| 643 |
FD_SET(fd, &tsdPtr->checkMasks.exception); |
| 644 |
} else { |
| 645 |
FD_CLR(fd, &tsdPtr->checkMasks.exception); |
| 646 |
} |
| 647 |
if (tsdPtr->numFdBits <= fd) { |
| 648 |
tsdPtr->numFdBits = fd+1; |
| 649 |
} |
| 650 |
} |
| 651 |
} |
| 652 |
|
| 653 |
/* |
| 654 |
*---------------------------------------------------------------------- |
| 655 |
* |
| 656 |
* Tcl_DeleteFileHandler -- |
| 657 |
* |
| 658 |
* Cancel a previously-arranged callback arrangement for a file. |
| 659 |
* |
| 660 |
* Results: |
| 661 |
* None. |
| 662 |
* |
| 663 |
* Side effects: |
| 664 |
* If a callback was previously registered on file, remove it. |
| 665 |
* |
| 666 |
*---------------------------------------------------------------------- |
| 667 |
*/ |
| 668 |
|
| 669 |
void |
| 670 |
Tcl_DeleteFileHandler( |
| 671 |
int fd) /* Stream id for which to remove callback |
| 672 |
* function. */ |
| 673 |
{ |
| 674 |
if (tclNotifierHooks.deleteFileHandlerProc) { |
| 675 |
tclNotifierHooks.deleteFileHandlerProc(fd); |
| 676 |
return; |
| 677 |
} else { |
| 678 |
FileHandler *filePtr, *prevPtr; |
| 679 |
int i; |
| 680 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 681 |
|
| 682 |
/* |
| 683 |
* Find the entry for the given file (and return if there isn't one). |
| 684 |
*/ |
| 685 |
|
| 686 |
for (prevPtr = NULL, filePtr = tsdPtr->firstFileHandlerPtr; ; |
| 687 |
prevPtr = filePtr, filePtr = filePtr->nextPtr) { |
| 688 |
if (filePtr == NULL) { |
| 689 |
return; |
| 690 |
} |
| 691 |
if (filePtr->fd == fd) { |
| 692 |
break; |
| 693 |
} |
| 694 |
} |
| 695 |
|
| 696 |
/* |
| 697 |
* Update the check masks for this file. |
| 698 |
*/ |
| 699 |
|
| 700 |
if (filePtr->mask & TCL_READABLE) { |
| 701 |
FD_CLR(fd, &tsdPtr->checkMasks.readable); |
| 702 |
} |
| 703 |
if (filePtr->mask & TCL_WRITABLE) { |
| 704 |
FD_CLR(fd, &tsdPtr->checkMasks.writable); |
| 705 |
} |
| 706 |
if (filePtr->mask & TCL_EXCEPTION) { |
| 707 |
FD_CLR(fd, &tsdPtr->checkMasks.exception); |
| 708 |
} |
| 709 |
|
| 710 |
/* |
| 711 |
* Find current max fd. |
| 712 |
*/ |
| 713 |
|
| 714 |
if (fd+1 == tsdPtr->numFdBits) { |
| 715 |
int numFdBits = 0; |
| 716 |
|
| 717 |
for (i = fd-1; i >= 0; i--) { |
| 718 |
if (FD_ISSET(i, &tsdPtr->checkMasks.readable) |
| 719 |
|| FD_ISSET(i, &tsdPtr->checkMasks.writable) |
| 720 |
|| FD_ISSET(i, &tsdPtr->checkMasks.exception)) { |
| 721 |
numFdBits = i+1; |
| 722 |
break; |
| 723 |
} |
| 724 |
} |
| 725 |
tsdPtr->numFdBits = numFdBits; |
| 726 |
} |
| 727 |
|
| 728 |
/* |
| 729 |
* Clean up information in the callback record. |
| 730 |
*/ |
| 731 |
|
| 732 |
if (prevPtr == NULL) { |
| 733 |
tsdPtr->firstFileHandlerPtr = filePtr->nextPtr; |
| 734 |
} else { |
| 735 |
prevPtr->nextPtr = filePtr->nextPtr; |
| 736 |
} |
| 737 |
ckfree(filePtr); |
| 738 |
} |
| 739 |
} |
| 740 |
|
| 741 |
/* |
| 742 |
*---------------------------------------------------------------------- |
| 743 |
* |
| 744 |
* FileHandlerEventProc -- |
| 745 |
* |
| 746 |
* This function is called by Tcl_ServiceEvent when a file event reaches |
| 747 |
* the front of the event queue. This function is responsible for |
| 748 |
* actually handling the event by invoking the callback for the file |
| 749 |
* handler. |
| 750 |
* |
| 751 |
* Results: |
| 752 |
* Returns 1 if the event was handled, meaning it should be removed from |
| 753 |
* the queue. Returns 0 if the event was not handled, meaning it should |
| 754 |
* stay on the queue. The only time the event isn't handled is if the |
| 755 |
* TCL_FILE_EVENTS flag bit isn't set. |
| 756 |
* |
| 757 |
* Side effects: |
| 758 |
* Whatever the file handler's callback function does. |
| 759 |
* |
| 760 |
*---------------------------------------------------------------------- |
| 761 |
*/ |
| 762 |
|
| 763 |
static int |
| 764 |
FileHandlerEventProc( |
| 765 |
Tcl_Event *evPtr, /* Event to service. */ |
| 766 |
int flags) /* Flags that indicate what events to handle, |
| 767 |
* such as TCL_FILE_EVENTS. */ |
| 768 |
{ |
| 769 |
int mask; |
| 770 |
FileHandler *filePtr; |
| 771 |
FileHandlerEvent *fileEvPtr = (FileHandlerEvent *) evPtr; |
| 772 |
ThreadSpecificData *tsdPtr; |
| 773 |
|
| 774 |
if (!(flags & TCL_FILE_EVENTS)) { |
| 775 |
return 0; |
| 776 |
} |
| 777 |
|
| 778 |
/* |
| 779 |
* Search through the file handlers to find the one whose handle matches |
| 780 |
* the event. We do this rather than keeping a pointer to the file handler |
| 781 |
* directly in the event, so that the handler can be deleted while the |
| 782 |
* event is queued without leaving a dangling pointer. |
| 783 |
*/ |
| 784 |
|
| 785 |
tsdPtr = TCL_TSD_INIT(&dataKey); |
| 786 |
|
| 787 |
for (filePtr = tsdPtr->firstFileHandlerPtr; filePtr != NULL; |
| 788 |
filePtr = filePtr->nextPtr) { |
| 789 |
if (filePtr->fd != fileEvPtr->fd) { |
| 790 |
continue; |
| 791 |
} |
| 792 |
|
| 793 |
/* |
| 794 |
* The code is tricky for two reasons: |
| 795 |
* 1. The file handler's desired events could have changed since the |
| 796 |
* time when the event was queued, so AND the ready mask with the |
| 797 |
* desired mask. |
| 798 |
* 2. The file could have been closed and re-opened since the time |
| 799 |
* when the event was queued. This is why the ready mask is stored |
| 800 |
* in the file handler rather than the queued event: it will be |
| 801 |
* zeroed when a new file handler is created for the newly opened |
| 802 |
* file. |
| 803 |
*/ |
| 804 |
|
| 805 |
mask = filePtr->readyMask & filePtr->mask; |
| 806 |
filePtr->readyMask = 0; |
| 807 |
if (mask != 0) { |
| 808 |
filePtr->proc(filePtr->clientData, mask); |
| 809 |
} |
| 810 |
break; |
| 811 |
} |
| 812 |
return 1; |
| 813 |
} |
| 814 |
|
| 815 |
#if defined(TCL_THREADS) && defined(__CYGWIN__) |
| 816 |
|
| 817 |
static unsigned int |
| 818 |
NotifierProc( |
| 819 |
void *hwnd, |
| 820 |
unsigned int message, |
| 821 |
void *wParam, |
| 822 |
void *lParam) |
| 823 |
{ |
| 824 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 825 |
|
| 826 |
if (message != 1024) { |
| 827 |
return DefWindowProcW(hwnd, message, wParam, lParam); |
| 828 |
} |
| 829 |
|
| 830 |
/* |
| 831 |
* Process all of the runnable events. |
| 832 |
*/ |
| 833 |
|
| 834 |
tsdPtr->eventReady = 1; |
| 835 |
Tcl_ServiceAll(); |
| 836 |
return 0; |
| 837 |
} |
| 838 |
#endif /* TCL_THREADS && __CYGWIN__ */ |
| 839 |
|
| 840 |
/* |
| 841 |
*---------------------------------------------------------------------- |
| 842 |
* |
| 843 |
* Tcl_WaitForEvent -- |
| 844 |
* |
| 845 |
* This function is called by Tcl_DoOneEvent to wait for new events on |
| 846 |
* the message queue. If the block time is 0, then Tcl_WaitForEvent just |
| 847 |
* polls without blocking. |
| 848 |
* |
| 849 |
* Results: |
| 850 |
* Returns -1 if the select would block forever, otherwise returns 0. |
| 851 |
* |
| 852 |
* Side effects: |
| 853 |
* Queues file events that are detected by the select. |
| 854 |
* |
| 855 |
*---------------------------------------------------------------------- |
| 856 |
*/ |
| 857 |
|
| 858 |
int |
| 859 |
Tcl_WaitForEvent( |
| 860 |
const Tcl_Time *timePtr) /* Maximum block time, or NULL. */ |
| 861 |
{ |
| 862 |
if (tclNotifierHooks.waitForEventProc) { |
| 863 |
return tclNotifierHooks.waitForEventProc(timePtr); |
| 864 |
} else { |
| 865 |
FileHandler *filePtr; |
| 866 |
int mask; |
| 867 |
Tcl_Time vTime; |
| 868 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 869 |
#ifdef TCL_THREADS |
| 870 |
int waitForFiles; |
| 871 |
# ifdef __CYGWIN__ |
| 872 |
MSG msg; |
| 873 |
# endif /* __CYGWIN__ */ |
| 874 |
#else /* !TCL_THREADS */ |
| 875 |
/* |
| 876 |
* Impl. notes: timeout & timeoutPtr are used if, and only if threads |
| 877 |
* are not enabled. They are the arguments for the regular select() |
| 878 |
* used when the core is not thread-enabled. |
| 879 |
*/ |
| 880 |
|
| 881 |
struct timeval timeout, *timeoutPtr; |
| 882 |
int numFound; |
| 883 |
#endif /* TCL_THREADS */ |
| 884 |
|
| 885 |
/* |
| 886 |
* Set up the timeout structure. Note that if there are no events to |
| 887 |
* check for, we return with a negative result rather than blocking |
| 888 |
* forever. |
| 889 |
*/ |
| 890 |
|
| 891 |
if (timePtr != NULL) { |
| 892 |
/* |
| 893 |
* TIP #233 (Virtualized Time). Is virtual time in effect? And do |
| 894 |
* we actually have something to scale? If yes to both then we |
| 895 |
* call the handler to do this scaling. |
| 896 |
*/ |
| 897 |
|
| 898 |
if (timePtr->sec != 0 || timePtr->usec != 0) { |
| 899 |
vTime = *timePtr; |
| 900 |
tclScaleTimeProcPtr(&vTime, tclTimeClientData); |
| 901 |
timePtr = &vTime; |
| 902 |
} |
| 903 |
#ifndef TCL_THREADS |
| 904 |
timeout.tv_sec = timePtr->sec; |
| 905 |
timeout.tv_usec = timePtr->usec; |
| 906 |
timeoutPtr = &timeout; |
| 907 |
} else if (tsdPtr->numFdBits == 0) { |
| 908 |
/* |
| 909 |
* If there are no threads, no timeout, and no fds registered, |
| 910 |
* then there are no events possible and we must avoid deadlock. |
| 911 |
* Note that this is not entirely correct because there might be a |
| 912 |
* signal that could interrupt the select call, but we don't |
| 913 |
* handle that case if we aren't using threads. |
| 914 |
*/ |
| 915 |
|
| 916 |
return -1; |
| 917 |
} else { |
| 918 |
timeoutPtr = NULL; |
| 919 |
#endif /* !TCL_THREADS */ |
| 920 |
} |
| 921 |
|
| 922 |
#ifdef TCL_THREADS |
| 923 |
/* |
| 924 |
* Start notifier thread and place this thread on the list of |
| 925 |
* interested threads, signal the notifier thread, and wait for a |
| 926 |
* response or a timeout. |
| 927 |
*/ |
| 928 |
StartNotifierThread("Tcl_WaitForEvent"); |
| 929 |
|
| 930 |
pthread_mutex_lock(¬ifierMutex); |
| 931 |
|
| 932 |
if (timePtr != NULL && timePtr->sec == 0 && (timePtr->usec == 0 |
| 933 |
#if defined(__APPLE__) && defined(__LP64__) |
| 934 |
/* |
| 935 |
* On 64-bit Darwin, pthread_cond_timedwait() appears to have |
| 936 |
* a bug that causes it to wait forever when passed an |
| 937 |
* absolute time which has already been exceeded by the system |
| 938 |
* time; as a workaround, when given a very brief timeout, |
| 939 |
* just do a poll. [Bug 1457797] |
| 940 |
*/ |
| 941 |
|| timePtr->usec < 10 |
| 942 |
#endif /* __APPLE__ && __LP64__ */ |
| 943 |
)) { |
| 944 |
/* |
| 945 |
* Cannot emulate a polling select with a polling condition |
| 946 |
* variable. Instead, pretend to wait for files and tell the |
| 947 |
* notifier thread what we are doing. The notifier thread makes |
| 948 |
* sure it goes through select with its select mask in the same |
| 949 |
* state as ours currently is. We block until that happens. |
| 950 |
*/ |
| 951 |
|
| 952 |
waitForFiles = 1; |
| 953 |
tsdPtr->pollState = POLL_WANT; |
| 954 |
timePtr = NULL; |
| 955 |
} else { |
| 956 |
waitForFiles = (tsdPtr->numFdBits > 0); |
| 957 |
tsdPtr->pollState = 0; |
| 958 |
} |
| 959 |
|
| 960 |
if (waitForFiles) { |
| 961 |
/* |
| 962 |
* Add the ThreadSpecificData structure of this thread to the list |
| 963 |
* of ThreadSpecificData structures of all threads that are |
| 964 |
* waiting on file events. |
| 965 |
*/ |
| 966 |
|
| 967 |
tsdPtr->nextPtr = waitingListPtr; |
| 968 |
if (waitingListPtr) { |
| 969 |
waitingListPtr->prevPtr = tsdPtr; |
| 970 |
} |
| 971 |
tsdPtr->prevPtr = 0; |
| 972 |
waitingListPtr = tsdPtr; |
| 973 |
tsdPtr->onList = 1; |
| 974 |
|
| 975 |
if ((write(triggerPipe, "", 1) == -1) && (errno != EAGAIN)) { |
| 976 |
Tcl_Panic("Tcl_WaitForEvent: %s", |
| 977 |
"unable to write to triggerPipe"); |
| 978 |
} |
| 979 |
} |
| 980 |
|
| 981 |
FD_ZERO(&tsdPtr->readyMasks.readable); |
| 982 |
FD_ZERO(&tsdPtr->readyMasks.writable); |
| 983 |
FD_ZERO(&tsdPtr->readyMasks.exception); |
| 984 |
|
| 985 |
if (!tsdPtr->eventReady) { |
| 986 |
#ifdef __CYGWIN__ |
| 987 |
if (!PeekMessageW(&msg, NULL, 0, 0, 0)) { |
| 988 |
unsigned int timeout; |
| 989 |
|
| 990 |
if (timePtr) { |
| 991 |
timeout = timePtr->sec * 1000 + timePtr->usec / 1000; |
| 992 |
} else { |
| 993 |
timeout = 0xFFFFFFFF; |
| 994 |
} |
| 995 |
pthread_mutex_unlock(¬ifierMutex); |
| 996 |
MsgWaitForMultipleObjects(1, &tsdPtr->event, 0, timeout, 1279); |
| 997 |
pthread_mutex_lock(¬ifierMutex); |
| 998 |
} |
| 999 |
#else /* !__CYGWIN__ */ |
| 1000 |
if (timePtr != NULL) { |
| 1001 |
Tcl_Time now; |
| 1002 |
struct timespec ptime; |
| 1003 |
|
| 1004 |
Tcl_GetTime(&now); |
| 1005 |
ptime.tv_sec = timePtr->sec + now.sec + |
| 1006 |
(timePtr->usec + now.usec) / 1000000; |
| 1007 |
ptime.tv_nsec = 1000 * ((timePtr->usec + now.usec) % 1000000); |
| 1008 |
|
| 1009 |
pthread_cond_timedwait(&tsdPtr->waitCV, ¬ifierMutex, &ptime); |
| 1010 |
} else { |
| 1011 |
pthread_cond_wait(&tsdPtr->waitCV, ¬ifierMutex); |
| 1012 |
} |
| 1013 |
#endif /* __CYGWIN__ */ |
| 1014 |
} |
| 1015 |
tsdPtr->eventReady = 0; |
| 1016 |
|
| 1017 |
#ifdef __CYGWIN__ |
| 1018 |
while (PeekMessageW(&msg, NULL, 0, 0, 0)) { |
| 1019 |
/* |
| 1020 |
* Retrieve and dispatch the message. |
| 1021 |
*/ |
| 1022 |
|
| 1023 |
unsigned int result = GetMessageW(&msg, NULL, 0, 0); |
| 1024 |
|
| 1025 |
if (result == 0) { |
| 1026 |
PostQuitMessage(msg.wParam); |
| 1027 |
/* What to do here? */ |
| 1028 |
} else if (result != (unsigned int) -1) { |
| 1029 |
TranslateMessage(&msg); |
| 1030 |
DispatchMessageW(&msg); |
| 1031 |
} |
| 1032 |
} |
| 1033 |
ResetEvent(tsdPtr->event); |
| 1034 |
#endif /* __CYGWIN__ */ |
| 1035 |
|
| 1036 |
if (waitForFiles && tsdPtr->onList) { |
| 1037 |
/* |
| 1038 |
* Remove the ThreadSpecificData structure of this thread from the |
| 1039 |
* waiting list. Alert the notifier thread to recompute its select |
| 1040 |
* masks - skipping this caused a hang when trying to close a pipe |
| 1041 |
* which the notifier thread was still doing a select on. |
| 1042 |
*/ |
| 1043 |
|
| 1044 |
if (tsdPtr->prevPtr) { |
| 1045 |
tsdPtr->prevPtr->nextPtr = tsdPtr->nextPtr; |
| 1046 |
} else { |
| 1047 |
waitingListPtr = tsdPtr->nextPtr; |
| 1048 |
} |
| 1049 |
if (tsdPtr->nextPtr) { |
| 1050 |
tsdPtr->nextPtr->prevPtr = tsdPtr->prevPtr; |
| 1051 |
} |
| 1052 |
tsdPtr->nextPtr = tsdPtr->prevPtr = NULL; |
| 1053 |
tsdPtr->onList = 0; |
| 1054 |
if ((write(triggerPipe, "", 1) == -1) && (errno != EAGAIN)) { |
| 1055 |
Tcl_Panic("Tcl_WaitForEvent: %s", |
| 1056 |
"unable to write to triggerPipe"); |
| 1057 |
} |
| 1058 |
} |
| 1059 |
#else /* !TCL_THREADS */ |
| 1060 |
tsdPtr->readyMasks = tsdPtr->checkMasks; |
| 1061 |
numFound = select(tsdPtr->numFdBits, &tsdPtr->readyMasks.readable, |
| 1062 |
&tsdPtr->readyMasks.writable, &tsdPtr->readyMasks.exception, |
| 1063 |
timeoutPtr); |
| 1064 |
|
| 1065 |
/* |
| 1066 |
* Some systems don't clear the masks after an error, so we have to do |
| 1067 |
* it here. |
| 1068 |
*/ |
| 1069 |
|
| 1070 |
if (numFound == -1) { |
| 1071 |
FD_ZERO(&tsdPtr->readyMasks.readable); |
| 1072 |
FD_ZERO(&tsdPtr->readyMasks.writable); |
| 1073 |
FD_ZERO(&tsdPtr->readyMasks.exception); |
| 1074 |
} |
| 1075 |
#endif /* TCL_THREADS */ |
| 1076 |
|
| 1077 |
/* |
| 1078 |
* Queue all detected file events before returning. |
| 1079 |
*/ |
| 1080 |
|
| 1081 |
for (filePtr = tsdPtr->firstFileHandlerPtr; (filePtr != NULL); |
| 1082 |
filePtr = filePtr->nextPtr) { |
| 1083 |
mask = 0; |
| 1084 |
if (FD_ISSET(filePtr->fd, &tsdPtr->readyMasks.readable)) { |
| 1085 |
mask |= TCL_READABLE; |
| 1086 |
} |
| 1087 |
if (FD_ISSET(filePtr->fd, &tsdPtr->readyMasks.writable)) { |
| 1088 |
mask |= TCL_WRITABLE; |
| 1089 |
} |
| 1090 |
if (FD_ISSET(filePtr->fd, &tsdPtr->readyMasks.exception)) { |
| 1091 |
mask |= TCL_EXCEPTION; |
| 1092 |
} |
| 1093 |
|
| 1094 |
if (!mask) { |
| 1095 |
continue; |
| 1096 |
} |
| 1097 |
|
| 1098 |
/* |
| 1099 |
* Don't bother to queue an event if the mask was previously |
| 1100 |
* non-zero since an event must still be on the queue. |
| 1101 |
*/ |
| 1102 |
|
| 1103 |
if (filePtr->readyMask == 0) { |
| 1104 |
FileHandlerEvent *fileEvPtr = |
| 1105 |
(FileHandlerEvent *)ckalloc(sizeof(FileHandlerEvent)); |
| 1106 |
|
| 1107 |
fileEvPtr->header.proc = FileHandlerEventProc; |
| 1108 |
fileEvPtr->fd = filePtr->fd; |
| 1109 |
Tcl_QueueEvent((Tcl_Event *) fileEvPtr, TCL_QUEUE_TAIL); |
| 1110 |
} |
| 1111 |
filePtr->readyMask = mask; |
| 1112 |
} |
| 1113 |
#ifdef TCL_THREADS |
| 1114 |
pthread_mutex_unlock(¬ifierMutex); |
| 1115 |
#endif /* TCL_THREADS */ |
| 1116 |
return 0; |
| 1117 |
} |
| 1118 |
} |
| 1119 |
|
| 1120 |
#ifdef TCL_THREADS |
| 1121 |
/* |
| 1122 |
*---------------------------------------------------------------------- |
| 1123 |
* |
| 1124 |
* NotifierThreadProc -- |
| 1125 |
* |
| 1126 |
* This routine is the initial (and only) function executed by the |
| 1127 |
* special notifier thread. Its job is to wait for file descriptors to |
| 1128 |
* become readable or writable or to have an exception condition and then |
| 1129 |
* to notify other threads who are interested in this information by |
| 1130 |
* signalling a condition variable. Other threads can signal this |
| 1131 |
* notifier thread of a change in their interests by writing a single |
| 1132 |
* byte to a special pipe that the notifier thread is monitoring. |
| 1133 |
* |
| 1134 |
* Result: |
| 1135 |
* None. Once started, this routine never exits. It dies with the overall |
| 1136 |
* process. |
| 1137 |
* |
| 1138 |
* Side effects: |
| 1139 |
* The trigger pipe used to signal the notifier thread is created when |
| 1140 |
* the notifier thread first starts. |
| 1141 |
* |
| 1142 |
*---------------------------------------------------------------------- |
| 1143 |
*/ |
| 1144 |
|
| 1145 |
static void |
| 1146 |
NotifierThreadProc( |
| 1147 |
void *dummy) /* Not used. */ |
| 1148 |
{ |
| 1149 |
ThreadSpecificData *tsdPtr; |
| 1150 |
fd_set readableMask; |
| 1151 |
fd_set writableMask; |
| 1152 |
fd_set exceptionMask; |
| 1153 |
int i; |
| 1154 |
int fds[2], receivePipe; |
| 1155 |
long found; |
| 1156 |
struct timeval poll = {0, 0}, *timePtr; |
| 1157 |
char buf[2]; |
| 1158 |
int numFdBits = 0; |
| 1159 |
(void)dummy; |
| 1160 |
|
| 1161 |
if (pipe(fds) != 0) { |
| 1162 |
Tcl_Panic("NotifierThreadProc: %s", "could not create trigger pipe"); |
| 1163 |
} |
| 1164 |
|
| 1165 |
receivePipe = fds[0]; |
| 1166 |
|
| 1167 |
if (TclUnixSetBlockingMode(receivePipe, TCL_MODE_NONBLOCKING) < 0) { |
| 1168 |
Tcl_Panic("NotifierThreadProc: %s", |
| 1169 |
"could not make receive pipe non blocking"); |
| 1170 |
} |
| 1171 |
if (TclUnixSetBlockingMode(fds[1], TCL_MODE_NONBLOCKING) < 0) { |
| 1172 |
Tcl_Panic("NotifierThreadProc: %s", |
| 1173 |
"could not make trigger pipe non blocking"); |
| 1174 |
} |
| 1175 |
if (fcntl(receivePipe, F_SETFD, FD_CLOEXEC) < 0) { |
| 1176 |
Tcl_Panic("NotifierThreadProc: %s", |
| 1177 |
"could not make receive pipe close-on-exec"); |
| 1178 |
} |
| 1179 |
if (fcntl(fds[1], F_SETFD, FD_CLOEXEC) < 0) { |
| 1180 |
Tcl_Panic("NotifierThreadProc: %s", |
| 1181 |
"could not make trigger pipe close-on-exec"); |
| 1182 |
} |
| 1183 |
|
| 1184 |
/* |
| 1185 |
* Install the write end of the pipe into the global variable. |
| 1186 |
*/ |
| 1187 |
|
| 1188 |
pthread_mutex_lock(¬ifierMutex); |
| 1189 |
triggerPipe = fds[1]; |
| 1190 |
|
| 1191 |
/* |
| 1192 |
* Signal any threads that are waiting. |
| 1193 |
*/ |
| 1194 |
|
| 1195 |
pthread_cond_broadcast(¬ifierCV); |
| 1196 |
pthread_mutex_unlock(¬ifierMutex); |
| 1197 |
|
| 1198 |
/* |
| 1199 |
* Look for file events and report them to interested threads. |
| 1200 |
*/ |
| 1201 |
|
| 1202 |
while (1) { |
| 1203 |
FD_ZERO(&readableMask); |
| 1204 |
FD_ZERO(&writableMask); |
| 1205 |
FD_ZERO(&exceptionMask); |
| 1206 |
|
| 1207 |
/* |
| 1208 |
* Compute the logical OR of the masks from all the waiting |
| 1209 |
* notifiers. |
| 1210 |
*/ |
| 1211 |
|
| 1212 |
pthread_mutex_lock(¬ifierMutex); |
| 1213 |
timePtr = NULL; |
| 1214 |
for (tsdPtr = waitingListPtr; tsdPtr; tsdPtr = tsdPtr->nextPtr) { |
| 1215 |
for (i = tsdPtr->numFdBits-1; i >= 0; --i) { |
| 1216 |
if (FD_ISSET(i, &tsdPtr->checkMasks.readable)) { |
| 1217 |
FD_SET(i, &readableMask); |
| 1218 |
} |
| 1219 |
if (FD_ISSET(i, &tsdPtr->checkMasks.writable)) { |
| 1220 |
FD_SET(i, &writableMask); |
| 1221 |
} |
| 1222 |
if (FD_ISSET(i, &tsdPtr->checkMasks.exception)) { |
| 1223 |
FD_SET(i, &exceptionMask); |
| 1224 |
} |
| 1225 |
} |
| 1226 |
if (tsdPtr->numFdBits > numFdBits) { |
| 1227 |
numFdBits = tsdPtr->numFdBits; |
| 1228 |
} |
| 1229 |
if (tsdPtr->pollState & POLL_WANT) { |
| 1230 |
/* |
| 1231 |
* Here we make sure we go through select() with the same mask |
| 1232 |
* bits that were present when the thread tried to poll. |
| 1233 |
*/ |
| 1234 |
|
| 1235 |
tsdPtr->pollState |= POLL_DONE; |
| 1236 |
timePtr = &poll; |
| 1237 |
} |
| 1238 |
} |
| 1239 |
pthread_mutex_unlock(¬ifierMutex); |
| 1240 |
|
| 1241 |
/* |
| 1242 |
* Set up the select mask to include the receive pipe. |
| 1243 |
*/ |
| 1244 |
|
| 1245 |
if (receivePipe >= numFdBits) { |
| 1246 |
numFdBits = receivePipe + 1; |
| 1247 |
} |
| 1248 |
FD_SET(receivePipe, &readableMask); |
| 1249 |
|
| 1250 |
if (select(numFdBits, &readableMask, &writableMask, &exceptionMask, |
| 1251 |
timePtr) == -1) { |
| 1252 |
/* |
| 1253 |
* Try again immediately on an error. |
| 1254 |
*/ |
| 1255 |
|
| 1256 |
continue; |
| 1257 |
} |
| 1258 |
|
| 1259 |
/* |
| 1260 |
* Alert any threads that are waiting on a ready file descriptor. |
| 1261 |
*/ |
| 1262 |
|
| 1263 |
pthread_mutex_lock(¬ifierMutex); |
| 1264 |
for (tsdPtr = waitingListPtr; tsdPtr; tsdPtr = tsdPtr->nextPtr) { |
| 1265 |
found = 0; |
| 1266 |
|
| 1267 |
for (i = tsdPtr->numFdBits-1; i >= 0; --i) { |
| 1268 |
if (FD_ISSET(i, &tsdPtr->checkMasks.readable) |
| 1269 |
&& FD_ISSET(i, &readableMask)) { |
| 1270 |
FD_SET(i, &tsdPtr->readyMasks.readable); |
| 1271 |
found = 1; |
| 1272 |
} |
| 1273 |
if (FD_ISSET(i, &tsdPtr->checkMasks.writable) |
| 1274 |
&& FD_ISSET(i, &writableMask)) { |
| 1275 |
FD_SET(i, &tsdPtr->readyMasks.writable); |
| 1276 |
found = 1; |
| 1277 |
} |
| 1278 |
if (FD_ISSET(i, &tsdPtr->checkMasks.exception) |
| 1279 |
&& FD_ISSET(i, &exceptionMask)) { |
| 1280 |
FD_SET(i, &tsdPtr->readyMasks.exception); |
| 1281 |
found = 1; |
| 1282 |
} |
| 1283 |
} |
| 1284 |
|
| 1285 |
if (found || (tsdPtr->pollState & POLL_DONE)) { |
| 1286 |
tsdPtr->eventReady = 1; |
| 1287 |
if (tsdPtr->onList) { |
| 1288 |
/* |
| 1289 |
* Remove the ThreadSpecificData structure of this thread |
| 1290 |
* from the waiting list. This prevents us from |
| 1291 |
* continuously spining on select until the other threads |
| 1292 |
* runs and services the file event. |
| 1293 |
*/ |
| 1294 |
|
| 1295 |
if (tsdPtr->prevPtr) { |
| 1296 |
tsdPtr->prevPtr->nextPtr = tsdPtr->nextPtr; |
| 1297 |
} else { |
| 1298 |
waitingListPtr = tsdPtr->nextPtr; |
| 1299 |
} |
| 1300 |
if (tsdPtr->nextPtr) { |
| 1301 |
tsdPtr->nextPtr->prevPtr = tsdPtr->prevPtr; |
| 1302 |
} |
| 1303 |
tsdPtr->nextPtr = tsdPtr->prevPtr = NULL; |
| 1304 |
tsdPtr->onList = 0; |
| 1305 |
tsdPtr->pollState = 0; |
| 1306 |
} |
| 1307 |
#ifdef __CYGWIN__ |
| 1308 |
PostMessageW(tsdPtr->hwnd, 1024, 0, 0); |
| 1309 |
#else /* __CYGWIN__ */ |
| 1310 |
pthread_cond_broadcast(&tsdPtr->waitCV); |
| 1311 |
#endif /* __CYGWIN__ */ |
| 1312 |
} |
| 1313 |
} |
| 1314 |
pthread_mutex_unlock(¬ifierMutex); |
| 1315 |
|
| 1316 |
/* |
| 1317 |
* Consume the next byte from the notifier pipe if the pipe was |
| 1318 |
* readable. Note that there may be multiple bytes pending, but to |
| 1319 |
* avoid a race condition we only read one at a time. |
| 1320 |
*/ |
| 1321 |
|
| 1322 |
if (FD_ISSET(receivePipe, &readableMask)) { |
| 1323 |
i = read(receivePipe, buf, 1); |
| 1324 |
|
| 1325 |
if ((i == 0) || ((i == 1) && (buf[0] == 'q'))) { |
| 1326 |
/* |
| 1327 |
* Someone closed the write end of the pipe or sent us a Quit |
| 1328 |
* message [Bug: 4139] and then closed the write end of the |
| 1329 |
* pipe so we need to shut down the notifier thread. |
| 1330 |
*/ |
| 1331 |
|
| 1332 |
break; |
| 1333 |
} |
| 1334 |
} |
| 1335 |
} |
| 1336 |
|
| 1337 |
/* |
| 1338 |
* Clean up the read end of the pipe and signal any threads waiting on |
| 1339 |
* termination of the notifier thread. |
| 1340 |
*/ |
| 1341 |
|
| 1342 |
close(receivePipe); |
| 1343 |
pthread_mutex_lock(¬ifierMutex); |
| 1344 |
triggerPipe = -1; |
| 1345 |
pthread_cond_broadcast(¬ifierCV); |
| 1346 |
pthread_mutex_unlock(¬ifierMutex); |
| 1347 |
|
| 1348 |
TclpThreadExit(0); |
| 1349 |
} |
| 1350 |
|
| 1351 |
#if defined(HAVE_PTHREAD_ATFORK) |
| 1352 |
/* |
| 1353 |
*---------------------------------------------------------------------- |
| 1354 |
* |
| 1355 |
* AtForkPrepare -- |
| 1356 |
* |
| 1357 |
* Lock the notifier in preparation for a fork. |
| 1358 |
* |
| 1359 |
* Results: |
| 1360 |
* None. |
| 1361 |
* |
| 1362 |
* Side effects: |
| 1363 |
* None. |
| 1364 |
* |
| 1365 |
*---------------------------------------------------------------------- |
| 1366 |
*/ |
| 1367 |
|
| 1368 |
static void |
| 1369 |
AtForkPrepare(void) |
| 1370 |
{ |
| 1371 |
#if RESET_ATFORK_MUTEX == 0 |
| 1372 |
pthread_mutex_lock(¬ifierInitMutex); |
| 1373 |
#endif |
| 1374 |
} |
| 1375 |
|
| 1376 |
/* |
| 1377 |
*---------------------------------------------------------------------- |
| 1378 |
* |
| 1379 |
* AtForkParent -- |
| 1380 |
* |
| 1381 |
* Unlock the notifier in the parent after a fork. |
| 1382 |
* |
| 1383 |
* Results: |
| 1384 |
* None. |
| 1385 |
* |
| 1386 |
* Side effects: |
| 1387 |
* None. |
| 1388 |
* |
| 1389 |
*---------------------------------------------------------------------- |
| 1390 |
*/ |
| 1391 |
|
| 1392 |
static void |
| 1393 |
AtForkParent(void) |
| 1394 |
{ |
| 1395 |
#if RESET_ATFORK_MUTEX == 0 |
| 1396 |
pthread_mutex_unlock(¬ifierInitMutex); |
| 1397 |
#endif |
| 1398 |
} |
| 1399 |
|
| 1400 |
/* |
| 1401 |
*---------------------------------------------------------------------- |
| 1402 |
* |
| 1403 |
* AtForkChild -- |
| 1404 |
* |
| 1405 |
* Unlock and reinstall the notifier in the child after a fork. |
| 1406 |
* |
| 1407 |
* Results: |
| 1408 |
* None. |
| 1409 |
* |
| 1410 |
* Side effects: |
| 1411 |
* None. |
| 1412 |
* |
| 1413 |
*---------------------------------------------------------------------- |
| 1414 |
*/ |
| 1415 |
|
| 1416 |
static void |
| 1417 |
AtForkChild(void) |
| 1418 |
{ |
| 1419 |
if (notifierThreadRunning == 1) { |
| 1420 |
pthread_cond_destroy(¬ifierCV); |
| 1421 |
} |
| 1422 |
#if RESET_ATFORK_MUTEX == 0 |
| 1423 |
pthread_mutex_unlock(¬ifierInitMutex); |
| 1424 |
#else |
| 1425 |
pthread_mutex_init(¬ifierInitMutex, NULL); |
| 1426 |
pthread_mutex_init(¬ifierMutex, NULL); |
| 1427 |
#endif |
| 1428 |
pthread_cond_init(¬ifierCV, NULL); |
| 1429 |
|
| 1430 |
/* |
| 1431 |
* notifierThreadRunning == 1: thread is running, (there might be data in notifier lists) |
| 1432 |
* atForkInit == 0: InitNotifier was never called |
| 1433 |
* notifierCount != 0: unbalanced InitNotifier() / FinalizeNotifier calls |
| 1434 |
* waitingListPtr != 0: there are threads currently waiting for events. |
| 1435 |
*/ |
| 1436 |
|
| 1437 |
if (atForkInit == 1) { |
| 1438 |
|
| 1439 |
notifierCount = 0; |
| 1440 |
if (notifierThreadRunning == 1) { |
| 1441 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 1442 |
notifierThreadRunning = 0; |
| 1443 |
|
| 1444 |
close(triggerPipe); |
| 1445 |
triggerPipe = -1; |
| 1446 |
/* |
| 1447 |
* The waitingListPtr might contain event info from multiple |
| 1448 |
* threads, which are invalid here, so setting it to NULL is not |
| 1449 |
* unreasonable. |
| 1450 |
*/ |
| 1451 |
waitingListPtr = NULL; |
| 1452 |
|
| 1453 |
/* |
| 1454 |
* The tsdPtr from before the fork is copied as well. But since |
| 1455 |
* we are paranoiac, we don't trust its condvar and reset it. |
| 1456 |
*/ |
| 1457 |
#ifdef __CYGWIN__ |
| 1458 |
DestroyWindow(tsdPtr->hwnd); |
| 1459 |
tsdPtr->hwnd = CreateWindowExW(NULL, NotfyClassName, |
| 1460 |
NotfyClassName, 0, 0, 0, 0, 0, NULL, NULL, |
| 1461 |
TclWinGetTclInstance(), NULL); |
| 1462 |
ResetEvent(tsdPtr->event); |
| 1463 |
#else |
| 1464 |
pthread_cond_destroy(&tsdPtr->waitCV); |
| 1465 |
pthread_cond_init(&tsdPtr->waitCV, NULL); |
| 1466 |
#endif |
| 1467 |
/* |
| 1468 |
* In case, we had multiple threads running before the fork, |
| 1469 |
* make sure, we don't try to reach out to their thread local data. |
| 1470 |
*/ |
| 1471 |
tsdPtr->nextPtr = tsdPtr->prevPtr = NULL; |
| 1472 |
|
| 1473 |
/* |
| 1474 |
* The list of registered event handlers at fork time is in |
| 1475 |
* tsdPtr->firstFileHandlerPtr; |
| 1476 |
*/ |
| 1477 |
} |
| 1478 |
} |
| 1479 |
|
| 1480 |
Tcl_InitNotifier(); |
| 1481 |
} |
| 1482 |
#endif /* HAVE_PTHREAD_ATFORK */ |
| 1483 |
|
| 1484 |
#endif /* TCL_THREADS */ |
| 1485 |
|
| 1486 |
#else |
| 1487 |
TCL_MAC_EMPTY_FILE(unix_tclUnixNotfy_c) |
| 1488 |
#endif /* !HAVE_COREFOUNDATION */ |
| 1489 |
|
| 1490 |
/* |
| 1491 |
* Local Variables: |
| 1492 |
* mode: c |
| 1493 |
* c-basic-offset: 4 |
| 1494 |
* fill-column: 78 |
| 1495 |
* End: |
| 1496 |
*/ |