| 1 |
/* |
| 2 |
* tclMacOSXNotify.c -- |
| 3 |
* |
| 4 |
* This file contains the implementation of a merged CFRunLoop/select() |
| 5 |
* based notifier, which is the lowest-level part of the Tcl event loop. |
| 6 |
* This file works together with generic/tclNotify.c. |
| 7 |
* |
| 8 |
* Copyright (c) 1995-1997 Sun Microsystems, Inc. |
| 9 |
* Copyright 2001-2009, Apple Inc. |
| 10 |
* Copyright (c) 2005-2009 Daniel A. Steffen <[email protected]> |
| 11 |
* |
| 12 |
* See the file "license.terms" for information on usage and redistribution of |
| 13 |
* this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 14 |
*/ |
| 15 |
|
| 16 |
#include "tclInt.h" |
| 17 |
|
| 18 |
/* |
| 19 |
* In macOS 10.12 the os_unfair_lock was introduced as a replacement for the |
| 20 |
* OSSpinLock, and the OSSpinLock was deprecated. |
| 21 |
*/ |
| 22 |
|
| 23 |
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 101200 |
| 24 |
#define USE_OS_UNFAIR_LOCK |
| 25 |
#include <os/lock.h> |
| 26 |
#undef TCL_MAC_DEBUG_NOTIFIER |
| 27 |
#endif |
| 28 |
|
| 29 |
#ifdef HAVE_COREFOUNDATION /* Traditional unix select-based notifier is |
| 30 |
* in tclUnixNotfy.c */ |
| 31 |
#include <CoreFoundation/CoreFoundation.h> |
| 32 |
#include <pthread.h> |
| 33 |
|
| 34 |
/* #define TCL_MAC_DEBUG_NOTIFIER 1 */ |
| 35 |
|
| 36 |
#if !defined(USE_OS_UNFAIR_LOCK) |
| 37 |
|
| 38 |
/* |
| 39 |
* We use the Darwin-native spinlock API rather than pthread mutexes for |
| 40 |
* notifier locking: this radically simplifies the implementation and lowers |
| 41 |
* overhead. Note that these are not pure spinlocks, they employ various |
| 42 |
* strategies to back off and relinquish the processor, making them immune to |
| 43 |
* most priority-inversion livelocks (c.f. 'man 3 OSSpinLockLock' and Darwin |
| 44 |
* sources: xnu/osfmk/{ppc,i386}/commpage/spinlocks.s). |
| 45 |
*/ |
| 46 |
|
| 47 |
#if defined(HAVE_LIBKERN_OSATOMIC_H) && defined(HAVE_OSSPINLOCKLOCK) |
| 48 |
#pragma GCC diagnostic push |
| 49 |
#pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 50 |
#pragma GCC diagnostic ignored "-Wunused-function" |
| 51 |
/* |
| 52 |
* Use OSSpinLock API where available (Tiger or later). |
| 53 |
*/ |
| 54 |
|
| 55 |
#include <libkern/OSAtomic.h> |
| 56 |
|
| 57 |
#if defined(HAVE_WEAK_IMPORT) && MAC_OS_X_VERSION_MIN_REQUIRED < 1040 |
| 58 |
/* |
| 59 |
* Support for weakly importing spinlock API. |
| 60 |
*/ |
| 61 |
#define WEAK_IMPORT_SPINLOCKLOCK |
| 62 |
|
| 63 |
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1050 |
| 64 |
#define VOLATILE volatile |
| 65 |
#else |
| 66 |
#define VOLATILE |
| 67 |
#endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= 1050 */ |
| 68 |
|
| 69 |
#ifndef bool |
| 70 |
#define bool int |
| 71 |
#endif |
| 72 |
|
| 73 |
extern void OSSpinLockLock(VOLATILE OSSpinLock *lock) |
| 74 |
WEAK_IMPORT_ATTRIBUTE; |
| 75 |
extern void OSSpinLockUnlock(VOLATILE OSSpinLock *lock) |
| 76 |
WEAK_IMPORT_ATTRIBUTE; |
| 77 |
extern bool OSSpinLockTry(VOLATILE OSSpinLock *lock) |
| 78 |
WEAK_IMPORT_ATTRIBUTE; |
| 79 |
extern void _spin_lock(VOLATILE OSSpinLock *lock) |
| 80 |
WEAK_IMPORT_ATTRIBUTE; |
| 81 |
extern void _spin_unlock(VOLATILE OSSpinLock *lock) |
| 82 |
WEAK_IMPORT_ATTRIBUTE; |
| 83 |
extern bool _spin_lock_try(VOLATILE OSSpinLock *lock) |
| 84 |
WEAK_IMPORT_ATTRIBUTE; |
| 85 |
static void (* lockLock)(VOLATILE OSSpinLock *lock) = NULL; |
| 86 |
static void (* lockUnlock)(VOLATILE OSSpinLock *lock) = NULL; |
| 87 |
static bool (* lockTry)(VOLATILE OSSpinLock *lock) = NULL; |
| 88 |
#undef VOLATILE |
| 89 |
static pthread_once_t spinLockLockInitControl = PTHREAD_ONCE_INIT; |
| 90 |
static void |
| 91 |
SpinLockLockInit(void) |
| 92 |
{ |
| 93 |
lockLock = OSSpinLockLock != NULL ? OSSpinLockLock : _spin_lock; |
| 94 |
lockUnlock = OSSpinLockUnlock != NULL ? OSSpinLockUnlock : _spin_unlock; |
| 95 |
lockTry = OSSpinLockTry != NULL ? OSSpinLockTry : _spin_lock_try; |
| 96 |
if (lockLock == NULL || lockUnlock == NULL) { |
| 97 |
Tcl_Panic("SpinLockLockInit: no spinlock API available"); |
| 98 |
} |
| 99 |
} |
| 100 |
#define SpinLockLock(p) lockLock(p) |
| 101 |
#define SpinLockUnlock(p) lockUnlock(p) |
| 102 |
#define SpinLockTry(p) lockTry(p) |
| 103 |
#else |
| 104 |
#define SpinLockLock(p) OSSpinLockLock(p) |
| 105 |
#define SpinLockUnlock(p) OSSpinLockUnlock(p) |
| 106 |
#define SpinLockTry(p) OSSpinLockTry(p) |
| 107 |
#endif /* HAVE_WEAK_IMPORT */ |
| 108 |
#define SPINLOCK_INIT OS_SPINLOCK_INIT |
| 109 |
|
| 110 |
#else |
| 111 |
/* |
| 112 |
* Otherwise, use commpage spinlock SPI directly. |
| 113 |
*/ |
| 114 |
|
| 115 |
typedef uint32_t OSSpinLock; |
| 116 |
extern void _spin_lock(OSSpinLock *lock); |
| 117 |
extern void _spin_unlock(OSSpinLock *lock); |
| 118 |
extern int _spin_lock_try(OSSpinLock *lock); |
| 119 |
#define SpinLockLock(p) _spin_lock(p) |
| 120 |
#define SpinLockUnlock(p) _spin_unlock(p) |
| 121 |
#define SpinLockTry(p) _spin_lock_try(p) |
| 122 |
#define SPINLOCK_INIT 0 |
| 123 |
|
| 124 |
#pragma GCC diagnostic pop |
| 125 |
#endif /* HAVE_LIBKERN_OSATOMIC_H && HAVE_OSSPINLOCKLOCK */ |
| 126 |
#endif /* not using os_unfair_lock */ |
| 127 |
|
| 128 |
/* |
| 129 |
* These locks control access to the global notifier state. |
| 130 |
*/ |
| 131 |
|
| 132 |
#if defined(USE_OS_UNFAIR_LOCK) |
| 133 |
static os_unfair_lock notifierInitLock = OS_UNFAIR_LOCK_INIT; |
| 134 |
static os_unfair_lock notifierLock = OS_UNFAIR_LOCK_INIT; |
| 135 |
#else |
| 136 |
static OSSpinLock notifierInitLock = SPINLOCK_INIT; |
| 137 |
static OSSpinLock notifierLock = SPINLOCK_INIT; |
| 138 |
#endif |
| 139 |
|
| 140 |
/* |
| 141 |
* Macros that abstract notifier locking/unlocking |
| 142 |
*/ |
| 143 |
|
| 144 |
#if defined(USE_OS_UNFAIR_LOCK) |
| 145 |
#define LOCK_NOTIFIER_INIT os_unfair_lock_lock(¬ifierInitLock) |
| 146 |
#define UNLOCK_NOTIFIER_INIT os_unfair_lock_unlock(¬ifierInitLock) |
| 147 |
#define LOCK_NOTIFIER os_unfair_lock_lock(¬ifierLock) |
| 148 |
#define UNLOCK_NOTIFIER os_unfair_lock_unlock(¬ifierLock) |
| 149 |
#define LOCK_NOTIFIER_TSD os_unfair_lock_lock(&tsdPtr->tsdLock) |
| 150 |
#define UNLOCK_NOTIFIER_TSD os_unfair_lock_unlock(&tsdPtr->tsdLock) |
| 151 |
#else |
| 152 |
#define LOCK_NOTIFIER_INIT SpinLockLock(¬ifierInitLock) |
| 153 |
#define UNLOCK_NOTIFIER_INIT SpinLockUnlock(¬ifierInitLock) |
| 154 |
#define LOCK_NOTIFIER SpinLockLock(¬ifierLock) |
| 155 |
#define UNLOCK_NOTIFIER SpinLockUnlock(¬ifierLock) |
| 156 |
#define LOCK_NOTIFIER_TSD SpinLockLock(&tsdPtr->tsdLock) |
| 157 |
#define UNLOCK_NOTIFIER_TSD SpinLockUnlock(&tsdPtr->tsdLock) |
| 158 |
#endif |
| 159 |
|
| 160 |
/* |
| 161 |
* The debug version of the Notifier only works if using OSSpinLock. |
| 162 |
*/ |
| 163 |
|
| 164 |
#if defined(TCL_MAC_DEBUG_NOTIFIER) && !defined(USE_OS_UNFAIR_LOCK) |
| 165 |
#define TclMacOSXNotifierDbgMsg(m, ...) \ |
| 166 |
do { \ |
| 167 |
fprintf(notifierLog?notifierLog:stderr, "tclMacOSXNotify.c:%d: " \ |
| 168 |
"%s() pid %5d thread %10p: " m "\n", __LINE__, __func__, \ |
| 169 |
getpid(), pthread_self(), ##__VA_ARGS__); \ |
| 170 |
fflush(notifierLog?notifierLog:stderr); \ |
| 171 |
} while (0) |
| 172 |
|
| 173 |
/* |
| 174 |
* Debug version of SpinLockLock that logs the time spent waiting for the lock |
| 175 |
*/ |
| 176 |
|
| 177 |
#define SpinLockLockDbg(p) \ |
| 178 |
if (!SpinLockTry(p)) { \ |
| 179 |
Tcl_WideInt s = TclpGetWideClicks(), e; \ |
| 180 |
\ |
| 181 |
SpinLockLock(p); \ |
| 182 |
e = TclpGetWideClicks(); \ |
| 183 |
TclMacOSXNotifierDbgMsg("waited on %s for %8.0f ns", \ |
| 184 |
#p, TclpWideClicksToNanoseconds(e-s)); \ |
| 185 |
} |
| 186 |
#undef LOCK_NOTIFIER_INIT |
| 187 |
#define LOCK_NOTIFIER_INIT SpinLockLockDbg(¬ifierInitLock) |
| 188 |
#undef LOCK_NOTIFIER |
| 189 |
#define LOCK_NOTIFIER SpinLockLockDbg(¬ifierLock) |
| 190 |
#undef LOCK_NOTIFIER_TSD |
| 191 |
#define LOCK_NOTIFIER_TSD SpinLockLockDbg(tsdPtr->tsdLock) |
| 192 |
#include <asl.h> |
| 193 |
static FILE *notifierLog = NULL; |
| 194 |
#ifndef NOTIFIER_LOG |
| 195 |
#define NOTIFIER_LOG "/tmp/tclMacOSXNotify.log" |
| 196 |
#endif |
| 197 |
#define OPEN_NOTIFIER_LOG \ |
| 198 |
if (!notifierLog) { \ |
| 199 |
notifierLog = fopen(NOTIFIER_LOG, "a"); \ |
| 200 |
/*TclMacOSXNotifierDbgMsg("open log"); \ |
| 201 |
*asl_set_filter(NULL, \ |
| 202 |
* ASL_FILTER_MASK_UPTO(ASL_LEVEL_DEBUG)); \ |
| 203 |
*asl_add_log_file(NULL, fileno(notifierLog));*/ \ |
| 204 |
} |
| 205 |
#define CLOSE_NOTIFIER_LOG \ |
| 206 |
if (notifierLog) { \ |
| 207 |
/*asl_remove_log_file(NULL, fileno(notifierLog)); \ |
| 208 |
*TclMacOSXNotifierDbgMsg("close log");*/ \ |
| 209 |
fclose(notifierLog); \ |
| 210 |
notifierLog = NULL; \ |
| 211 |
} |
| 212 |
#define ENABLE_ASL \ |
| 213 |
if (notifierLog) { \ |
| 214 |
/*tsdPtr->asl = asl_open(NULL, "com.apple.console", \ |
| 215 |
* ASL_OPT_NO_REMOTE); \ |
| 216 |
*asl_set_filter(tsdPtr->asl, \ |
| 217 |
* ASL_FILTER_MASK_UPTO(ASL_LEVEL_DEBUG)); \ |
| 218 |
*asl_add_log_file(tsdPtr->asl, fileno(notifierLog));*/ \ |
| 219 |
} |
| 220 |
#define DISABLE_ASL \ |
| 221 |
/*if (tsdPtr->asl) { \ |
| 222 |
* if (notifierLog) { \ |
| 223 |
* asl_remove_log_file(tsdPtr->asl, fileno(notifierLog)); \ |
| 224 |
* } \ |
| 225 |
* asl_close(tsdPtr->asl); \ |
| 226 |
*}*/ |
| 227 |
#define ASLCLIENT_DECL /*aslclient asl*/ |
| 228 |
#else |
| 229 |
#define TclMacOSXNotifierDbgMsg(m, ...) |
| 230 |
#define OPEN_NOTIFIER_LOG |
| 231 |
#define CLOSE_NOTIFIER_LOG |
| 232 |
#define ENABLE_ASL |
| 233 |
#define DISABLE_ASL |
| 234 |
#define ASLCLIENT_DECL |
| 235 |
#endif /* TCL_MAC_DEBUG_NOTIFIER */ |
| 236 |
|
| 237 |
/* |
| 238 |
* This structure is used to keep track of the notifier info for a registered |
| 239 |
* file. |
| 240 |
*/ |
| 241 |
|
| 242 |
typedef struct FileHandler { |
| 243 |
int fd; |
| 244 |
int mask; /* Mask of desired events: TCL_READABLE, |
| 245 |
* etc. */ |
| 246 |
int readyMask; /* Mask of events that have been seen since |
| 247 |
* the last time file handlers were invoked |
| 248 |
* for this file. */ |
| 249 |
Tcl_FileProc *proc; /* Function to call, in the style of |
| 250 |
* Tcl_CreateFileHandler. */ |
| 251 |
ClientData clientData; /* Argument to pass to proc. */ |
| 252 |
struct FileHandler *nextPtr;/* Next in list of all files we care about. */ |
| 253 |
} FileHandler; |
| 254 |
|
| 255 |
/* |
| 256 |
* The following structure is what is added to the Tcl event queue when file |
| 257 |
* handlers are ready to fire. |
| 258 |
*/ |
| 259 |
|
| 260 |
typedef struct { |
| 261 |
Tcl_Event header; /* Information that is standard for all |
| 262 |
* events. */ |
| 263 |
int fd; /* File descriptor that is ready. Used to find |
| 264 |
* the FileHandler structure for the file |
| 265 |
* (can't point directly to the FileHandler |
| 266 |
* structure because it could go away while |
| 267 |
* the event is queued). */ |
| 268 |
} FileHandlerEvent; |
| 269 |
|
| 270 |
/* |
| 271 |
* The following structure contains a set of select() masks to track readable, |
| 272 |
* writable, and exceptional conditions. |
| 273 |
*/ |
| 274 |
|
| 275 |
typedef struct { |
| 276 |
fd_set readable; |
| 277 |
fd_set writable; |
| 278 |
fd_set exceptional; |
| 279 |
} SelectMasks; |
| 280 |
|
| 281 |
/* |
| 282 |
* The following static structure contains the state information for the |
| 283 |
* select based implementation of the Tcl notifier. One of these structures is |
| 284 |
* created for each thread that is using the notifier. |
| 285 |
*/ |
| 286 |
|
| 287 |
typedef struct ThreadSpecificData { |
| 288 |
FileHandler *firstFileHandlerPtr; |
| 289 |
/* Pointer to head of file handler list. */ |
| 290 |
int polled; /* True if the notifier thread has polled for |
| 291 |
* this thread. */ |
| 292 |
int sleeping; /* True if runloop is inside Tcl_Sleep. */ |
| 293 |
int runLoopSourcePerformed; /* True after the runLoopSource callack was |
| 294 |
* performed. */ |
| 295 |
int runLoopRunning; /* True if this thread's Tcl runLoop is |
| 296 |
* running. */ |
| 297 |
int runLoopNestingLevel; /* Level of nested runLoop invocations. */ |
| 298 |
|
| 299 |
/* Must hold the notifierLock before accessing the following fields: */ |
| 300 |
/* Start notifierLock section */ |
| 301 |
int onList; /* True if this thread is on the |
| 302 |
* waitingList */ |
| 303 |
struct ThreadSpecificData *nextPtr, *prevPtr; |
| 304 |
/* All threads that are currently waiting on |
| 305 |
* an event have their ThreadSpecificData |
| 306 |
* structure on a doubly-linked listed formed |
| 307 |
* from these pointers. */ |
| 308 |
/* End notifierLock section */ |
| 309 |
|
| 310 |
#if defined(USE_OS_UNFAIR_LOCK) |
| 311 |
os_unfair_lock tsdLock; |
| 312 |
#else |
| 313 |
OSSpinLock tsdLock; /* Must hold this lock before acessing the |
| 314 |
* following fields from more than one |
| 315 |
* thread. */ |
| 316 |
#endif |
| 317 |
|
| 318 |
/* Start tsdLock section */ |
| 319 |
SelectMasks checkMasks; /* This structure is used to build up the |
| 320 |
* masks to be used in the next call to |
| 321 |
* select. Bits are set in response to calls |
| 322 |
* to Tcl_CreateFileHandler. */ |
| 323 |
SelectMasks readyMasks; /* This array reflects the readable/writable |
| 324 |
* conditions that were found to exist by the |
| 325 |
* last call to select. */ |
| 326 |
int numFdBits; /* Number of valid bits in checkMasks (one |
| 327 |
* more than highest fd for which |
| 328 |
* Tcl_WatchFile has been called). */ |
| 329 |
int polling; /* True if this thread is polling for |
| 330 |
* events. */ |
| 331 |
CFRunLoopRef runLoop; /* This thread's CFRunLoop, needs to be woken |
| 332 |
* up whenever the runLoopSource is |
| 333 |
* signaled. */ |
| 334 |
CFRunLoopSourceRef runLoopSource; |
| 335 |
/* Any other thread alerts a notifier that an |
| 336 |
* event is ready to be processed by signaling |
| 337 |
* this CFRunLoopSource. */ |
| 338 |
CFRunLoopObserverRef runLoopObserver, runLoopObserverTcl; |
| 339 |
/* Adds/removes this thread from waitingList |
| 340 |
* when the CFRunLoop starts/stops. */ |
| 341 |
CFRunLoopTimerRef runLoopTimer; |
| 342 |
/* Wakes up CFRunLoop after given timeout when |
| 343 |
* running embedded. */ |
| 344 |
/* End tsdLock section */ |
| 345 |
|
| 346 |
CFTimeInterval waitTime; /* runLoopTimer wait time when running |
| 347 |
* embedded. */ |
| 348 |
ASLCLIENT_DECL; |
| 349 |
} ThreadSpecificData; |
| 350 |
|
| 351 |
static Tcl_ThreadDataKey dataKey; |
| 352 |
|
| 353 |
/* |
| 354 |
* The following static indicates the number of threads that have initialized |
| 355 |
* notifiers. |
| 356 |
* |
| 357 |
* You must hold the notifierInitLock before accessing this variable. |
| 358 |
*/ |
| 359 |
|
| 360 |
static int notifierCount = 0; |
| 361 |
|
| 362 |
/* |
| 363 |
* The following variable points to the head of a doubly-linked list of |
| 364 |
* ThreadSpecificData structures for all threads that are currently waiting on |
| 365 |
* an event. |
| 366 |
* |
| 367 |
* You must hold the notifierLock before accessing this list. |
| 368 |
*/ |
| 369 |
|
| 370 |
static ThreadSpecificData *waitingListPtr = NULL; |
| 371 |
|
| 372 |
/* |
| 373 |
* The notifier thread spends all its time in select() waiting for a file |
| 374 |
* descriptor associated with one of the threads on the waitingListPtr list to |
| 375 |
* do something interesting. But if the contents of the waitingListPtr list |
| 376 |
* ever changes, we need to wake up and restart the select() system call. You |
| 377 |
* can wake up the notifier thread by writing a single byte to the file |
| 378 |
* descriptor defined below. This file descriptor is the input-end of a pipe |
| 379 |
* and the notifier thread is listening for data on the output-end of the same |
| 380 |
* pipe. Hence writing to this file descriptor will cause the select() system |
| 381 |
* call to return and wake up the notifier thread. |
| 382 |
* |
| 383 |
* You must hold the notifierLock lock before writing to the pipe. |
| 384 |
*/ |
| 385 |
|
| 386 |
static int triggerPipe = -1; |
| 387 |
static int receivePipe = -1; /* Output end of triggerPipe */ |
| 388 |
|
| 389 |
/* |
| 390 |
* The following static indicates if the notifier thread is running. |
| 391 |
* |
| 392 |
* You must hold the notifierInitLock before accessing this variable. |
| 393 |
*/ |
| 394 |
|
| 395 |
static int notifierThreadRunning; |
| 396 |
|
| 397 |
/* |
| 398 |
* This is the thread ID of the notifier thread that does select. Only valid |
| 399 |
* when notifierThreadRunning is non-zero. |
| 400 |
* |
| 401 |
* You must hold the notifierInitLock before accessing this variable. |
| 402 |
*/ |
| 403 |
|
| 404 |
static pthread_t notifierThread; |
| 405 |
|
| 406 |
/* |
| 407 |
* Custom runloop mode for running with only the runloop source for the |
| 408 |
* notifier thread. |
| 409 |
*/ |
| 410 |
|
| 411 |
#ifndef TCL_EVENTS_ONLY_RUN_LOOP_MODE |
| 412 |
#define TCL_EVENTS_ONLY_RUN_LOOP_MODE "com.tcltk.tclEventsOnlyRunLoopMode" |
| 413 |
#endif |
| 414 |
#ifdef __CONSTANT_CFSTRINGS__ |
| 415 |
#define tclEventsOnlyRunLoopMode CFSTR(TCL_EVENTS_ONLY_RUN_LOOP_MODE) |
| 416 |
#else |
| 417 |
static CFStringRef tclEventsOnlyRunLoopMode = NULL; |
| 418 |
#endif |
| 419 |
|
| 420 |
/* |
| 421 |
* CFTimeInterval to wait forever. |
| 422 |
*/ |
| 423 |
|
| 424 |
#define CF_TIMEINTERVAL_FOREVER 5.05e8 |
| 425 |
|
| 426 |
/* |
| 427 |
* Static routines defined in this file. |
| 428 |
*/ |
| 429 |
|
| 430 |
static void StartNotifierThread(void); |
| 431 |
static void NotifierThreadProc(ClientData clientData) |
| 432 |
__attribute__ ((__noreturn__)); |
| 433 |
static int FileHandlerEventProc(Tcl_Event *evPtr, int flags); |
| 434 |
static void TimerWakeUp(CFRunLoopTimerRef timer, void *info); |
| 435 |
static void QueueFileEvents(void *info); |
| 436 |
static void UpdateWaitingListAndServiceEvents( |
| 437 |
CFRunLoopObserverRef observer, |
| 438 |
CFRunLoopActivity activity, void *info); |
| 439 |
static int OnOffWaitingList(ThreadSpecificData *tsdPtr, |
| 440 |
int onList, int signalNotifier); |
| 441 |
|
| 442 |
#ifdef HAVE_PTHREAD_ATFORK |
| 443 |
static int atForkInit = 0; |
| 444 |
static void AtForkPrepare(void); |
| 445 |
static void AtForkParent(void); |
| 446 |
static void AtForkChild(void); |
| 447 |
#if defined(HAVE_WEAK_IMPORT) && MAC_OS_X_VERSION_MIN_REQUIRED < 1040 |
| 448 |
/* Support for weakly importing pthread_atfork. */ |
| 449 |
#define WEAK_IMPORT_PTHREAD_ATFORK |
| 450 |
extern int pthread_atfork(void (*prepare)(void), |
| 451 |
void (*parent)(void), void (*child)(void)) |
| 452 |
WEAK_IMPORT_ATTRIBUTE; |
| 453 |
#define MayUsePthreadAtfork() (pthread_atfork != NULL) |
| 454 |
#else |
| 455 |
#define MayUsePthreadAtfork() (1) |
| 456 |
#endif /* HAVE_WEAK_IMPORT */ |
| 457 |
|
| 458 |
/* |
| 459 |
* On Darwin 9 and later, it is not possible to call CoreFoundation after |
| 460 |
* a fork. |
| 461 |
*/ |
| 462 |
|
| 463 |
#if !defined(MAC_OS_X_VERSION_MIN_REQUIRED) || \ |
| 464 |
MAC_OS_X_VERSION_MIN_REQUIRED < 1050 |
| 465 |
MODULE_SCOPE long tclMacOSXDarwinRelease; |
| 466 |
#define noCFafterFork (tclMacOSXDarwinRelease >= 9) |
| 467 |
#else /* MAC_OS_X_VERSION_MIN_REQUIRED */ |
| 468 |
#define noCFafterFork 1 |
| 469 |
#endif /* MAC_OS_X_VERSION_MIN_REQUIRED */ |
| 470 |
#endif /* HAVE_PTHREAD_ATFORK */ |
| 471 |
|
| 472 |
/* |
| 473 |
*---------------------------------------------------------------------- |
| 474 |
* |
| 475 |
* Tcl_InitNotifier -- |
| 476 |
* |
| 477 |
* Initializes the platform specific notifier state. |
| 478 |
* |
| 479 |
* Results: |
| 480 |
* Returns a handle to the notifier state for this thread. |
| 481 |
* |
| 482 |
* Side effects: |
| 483 |
* None. |
| 484 |
* |
| 485 |
*---------------------------------------------------------------------- |
| 486 |
*/ |
| 487 |
|
| 488 |
ClientData |
| 489 |
Tcl_InitNotifier(void) |
| 490 |
{ |
| 491 |
ThreadSpecificData *tsdPtr; |
| 492 |
|
| 493 |
if (tclNotifierHooks.initNotifierProc) { |
| 494 |
return tclNotifierHooks.initNotifierProc(); |
| 495 |
} |
| 496 |
|
| 497 |
tsdPtr = TCL_TSD_INIT(&dataKey); |
| 498 |
|
| 499 |
#ifdef WEAK_IMPORT_SPINLOCKLOCK |
| 500 |
/* |
| 501 |
* Initialize support for weakly imported spinlock API. |
| 502 |
*/ |
| 503 |
if (pthread_once(&spinLockLockInitControl, SpinLockLockInit)) { |
| 504 |
Tcl_Panic("Tcl_InitNotifier: pthread_once failed"); |
| 505 |
} |
| 506 |
#endif |
| 507 |
|
| 508 |
#ifndef __CONSTANT_CFSTRINGS__ |
| 509 |
if (!tclEventsOnlyRunLoopMode) { |
| 510 |
tclEventsOnlyRunLoopMode = CFSTR(TCL_EVENTS_ONLY_RUN_LOOP_MODE); |
| 511 |
} |
| 512 |
#endif |
| 513 |
|
| 514 |
/* |
| 515 |
* Initialize CFRunLoopSource and add it to CFRunLoop of this thread. |
| 516 |
*/ |
| 517 |
|
| 518 |
if (!tsdPtr->runLoop) { |
| 519 |
CFRunLoopRef runLoop = CFRunLoopGetCurrent(); |
| 520 |
CFRunLoopSourceRef runLoopSource; |
| 521 |
CFRunLoopSourceContext runLoopSourceContext; |
| 522 |
CFRunLoopObserverContext runLoopObserverContext; |
| 523 |
CFRunLoopObserverRef runLoopObserver, runLoopObserverTcl; |
| 524 |
|
| 525 |
bzero(&runLoopSourceContext, sizeof(CFRunLoopSourceContext)); |
| 526 |
runLoopSourceContext.info = tsdPtr; |
| 527 |
runLoopSourceContext.perform = QueueFileEvents; |
| 528 |
runLoopSource = CFRunLoopSourceCreate(NULL, LONG_MIN, |
| 529 |
&runLoopSourceContext); |
| 530 |
if (!runLoopSource) { |
| 531 |
Tcl_Panic("Tcl_InitNotifier: could not create CFRunLoopSource"); |
| 532 |
} |
| 533 |
CFRunLoopAddSource(runLoop, runLoopSource, kCFRunLoopCommonModes); |
| 534 |
CFRunLoopAddSource(runLoop, runLoopSource, tclEventsOnlyRunLoopMode); |
| 535 |
|
| 536 |
bzero(&runLoopObserverContext, sizeof(CFRunLoopObserverContext)); |
| 537 |
runLoopObserverContext.info = tsdPtr; |
| 538 |
runLoopObserver = CFRunLoopObserverCreate(NULL, |
| 539 |
kCFRunLoopEntry|kCFRunLoopExit, TRUE, |
| 540 |
LONG_MIN, UpdateWaitingListAndServiceEvents, |
| 541 |
&runLoopObserverContext); |
| 542 |
if (!runLoopObserver) { |
| 543 |
Tcl_Panic("Tcl_InitNotifier: could not create " |
| 544 |
"CFRunLoopObserver"); |
| 545 |
} |
| 546 |
CFRunLoopAddObserver(runLoop, runLoopObserver, kCFRunLoopCommonModes); |
| 547 |
|
| 548 |
/* |
| 549 |
* Create a second CFRunLoopObserver with the same callback as above |
| 550 |
* for the tclEventsOnlyRunLoopMode to ensure that the callback can be |
| 551 |
* re-entered via Tcl_ServiceAll() in the kCFRunLoopBeforeWaiting case |
| 552 |
* (CFRunLoop prevents observer callback re-entry of a given observer |
| 553 |
* instance). |
| 554 |
*/ |
| 555 |
|
| 556 |
runLoopObserverTcl = CFRunLoopObserverCreate(NULL, |
| 557 |
kCFRunLoopEntry|kCFRunLoopExit, TRUE, |
| 558 |
LONG_MIN, UpdateWaitingListAndServiceEvents, |
| 559 |
&runLoopObserverContext); |
| 560 |
if (!runLoopObserverTcl) { |
| 561 |
Tcl_Panic("Tcl_InitNotifier: could not create " |
| 562 |
"CFRunLoopObserver"); |
| 563 |
} |
| 564 |
CFRunLoopAddObserver(runLoop, runLoopObserverTcl, |
| 565 |
tclEventsOnlyRunLoopMode); |
| 566 |
|
| 567 |
tsdPtr->runLoop = runLoop; |
| 568 |
tsdPtr->runLoopSource = runLoopSource; |
| 569 |
tsdPtr->runLoopObserver = runLoopObserver; |
| 570 |
tsdPtr->runLoopObserverTcl = runLoopObserverTcl; |
| 571 |
tsdPtr->runLoopTimer = NULL; |
| 572 |
tsdPtr->waitTime = CF_TIMEINTERVAL_FOREVER; |
| 573 |
#if defined(USE_OS_UNFAIR_LOCK) |
| 574 |
tsdPtr->tsdLock = OS_UNFAIR_LOCK_INIT; |
| 575 |
#else |
| 576 |
tsdPtr->tsdLock = SPINLOCK_INIT; |
| 577 |
#endif |
| 578 |
} |
| 579 |
|
| 580 |
LOCK_NOTIFIER_INIT; |
| 581 |
#ifdef HAVE_PTHREAD_ATFORK |
| 582 |
/* |
| 583 |
* Install pthread_atfork handlers to reinitialize the notifier in the |
| 584 |
* child of a fork. |
| 585 |
*/ |
| 586 |
|
| 587 |
if (MayUsePthreadAtfork() && !atForkInit) { |
| 588 |
int result = pthread_atfork(AtForkPrepare, AtForkParent, AtForkChild); |
| 589 |
|
| 590 |
if (result) { |
| 591 |
Tcl_Panic("Tcl_InitNotifier: pthread_atfork failed"); |
| 592 |
} |
| 593 |
atForkInit = 1; |
| 594 |
} |
| 595 |
#endif /* HAVE_PTHREAD_ATFORK */ |
| 596 |
if (notifierCount == 0) { |
| 597 |
int fds[2], status; |
| 598 |
|
| 599 |
/* |
| 600 |
* Initialize trigger pipe. |
| 601 |
*/ |
| 602 |
|
| 603 |
if (pipe(fds) != 0) { |
| 604 |
Tcl_Panic("Tcl_InitNotifier: could not create trigger pipe"); |
| 605 |
} |
| 606 |
|
| 607 |
status = fcntl(fds[0], F_GETFL); |
| 608 |
status |= O_NONBLOCK; |
| 609 |
if (fcntl(fds[0], F_SETFL, status) < 0) { |
| 610 |
Tcl_Panic("Tcl_InitNotifier: could not make receive pipe non " |
| 611 |
"blocking"); |
| 612 |
} |
| 613 |
status = fcntl(fds[1], F_GETFL); |
| 614 |
status |= O_NONBLOCK; |
| 615 |
if (fcntl(fds[1], F_SETFL, status) < 0) { |
| 616 |
Tcl_Panic("Tcl_InitNotifier: could not make trigger pipe non " |
| 617 |
"blocking"); |
| 618 |
} |
| 619 |
|
| 620 |
receivePipe = fds[0]; |
| 621 |
triggerPipe = fds[1]; |
| 622 |
|
| 623 |
/* |
| 624 |
* Create notifier thread lazily in Tcl_WaitForEvent() to avoid |
| 625 |
* interfering with fork() followed immediately by execve() (we cannot |
| 626 |
* execve() when more than one thread is present). |
| 627 |
*/ |
| 628 |
|
| 629 |
notifierThreadRunning = 0; |
| 630 |
OPEN_NOTIFIER_LOG; |
| 631 |
} |
| 632 |
ENABLE_ASL; |
| 633 |
notifierCount++; |
| 634 |
UNLOCK_NOTIFIER_INIT; |
| 635 |
return tsdPtr; |
| 636 |
} |
| 637 |
|
| 638 |
/* |
| 639 |
*---------------------------------------------------------------------- |
| 640 |
* |
| 641 |
* TclMacOSXNotifierAddRunLoopMode -- |
| 642 |
* |
| 643 |
* Add the tcl notifier RunLoop source, observer and timer (if any) |
| 644 |
* to the given RunLoop mode. |
| 645 |
* |
| 646 |
* Results: |
| 647 |
* None. |
| 648 |
* |
| 649 |
* Side effects: |
| 650 |
* None. |
| 651 |
* |
| 652 |
*---------------------------------------------------------------------- |
| 653 |
*/ |
| 654 |
|
| 655 |
void |
| 656 |
TclMacOSXNotifierAddRunLoopMode( |
| 657 |
const void *runLoopMode) |
| 658 |
{ |
| 659 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 660 |
CFStringRef mode = (CFStringRef) runLoopMode; |
| 661 |
|
| 662 |
if (tsdPtr->runLoop) { |
| 663 |
CFRunLoopAddSource(tsdPtr->runLoop, tsdPtr->runLoopSource, mode); |
| 664 |
CFRunLoopAddObserver(tsdPtr->runLoop, tsdPtr->runLoopObserver, mode); |
| 665 |
if (tsdPtr->runLoopTimer) { |
| 666 |
CFRunLoopAddTimer(tsdPtr->runLoop, tsdPtr->runLoopTimer, mode); |
| 667 |
} |
| 668 |
} |
| 669 |
} |
| 670 |
|
| 671 |
/* |
| 672 |
*---------------------------------------------------------------------- |
| 673 |
* |
| 674 |
* StartNotifierThread -- |
| 675 |
* |
| 676 |
* Start notifier thread if necessary. |
| 677 |
* |
| 678 |
* Results: |
| 679 |
* None. |
| 680 |
* |
| 681 |
* Side effects: |
| 682 |
* None. |
| 683 |
* |
| 684 |
*---------------------------------------------------------------------- |
| 685 |
*/ |
| 686 |
|
| 687 |
static void |
| 688 |
StartNotifierThread(void) |
| 689 |
{ |
| 690 |
LOCK_NOTIFIER_INIT; |
| 691 |
if (!notifierCount) { |
| 692 |
Tcl_Panic("StartNotifierThread: notifier not initialized"); |
| 693 |
} |
| 694 |
if (!notifierThreadRunning) { |
| 695 |
int result; |
| 696 |
pthread_attr_t attr; |
| 697 |
|
| 698 |
pthread_attr_init(&attr); |
| 699 |
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); |
| 700 |
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); |
| 701 |
pthread_attr_setstacksize(&attr, 60 * 1024); |
| 702 |
result = pthread_create(¬ifierThread, &attr, |
| 703 |
(void * (*)(void *))(void *)NotifierThreadProc, NULL); |
| 704 |
pthread_attr_destroy(&attr); |
| 705 |
if (result) { |
| 706 |
Tcl_Panic("StartNotifierThread: unable to start notifier thread"); |
| 707 |
} |
| 708 |
notifierThreadRunning = 1; |
| 709 |
} |
| 710 |
UNLOCK_NOTIFIER_INIT; |
| 711 |
} |
| 712 |
|
| 713 |
|
| 714 |
/* |
| 715 |
*---------------------------------------------------------------------- |
| 716 |
* |
| 717 |
* Tcl_FinalizeNotifier -- |
| 718 |
* |
| 719 |
* This function is called to cleanup the notifier state before a thread |
| 720 |
* is terminated. |
| 721 |
* |
| 722 |
* Results: |
| 723 |
* None. |
| 724 |
* |
| 725 |
* Side effects: |
| 726 |
* May terminate the background notifier thread if this is the last |
| 727 |
* notifier instance. |
| 728 |
* |
| 729 |
*---------------------------------------------------------------------- |
| 730 |
*/ |
| 731 |
|
| 732 |
void |
| 733 |
Tcl_FinalizeNotifier( |
| 734 |
ClientData clientData) |
| 735 |
{ |
| 736 |
ThreadSpecificData *tsdPtr; |
| 737 |
|
| 738 |
if (tclNotifierHooks.finalizeNotifierProc) { |
| 739 |
tclNotifierHooks.finalizeNotifierProc(clientData); |
| 740 |
return; |
| 741 |
} |
| 742 |
|
| 743 |
tsdPtr = TCL_TSD_INIT(&dataKey); |
| 744 |
|
| 745 |
LOCK_NOTIFIER_INIT; |
| 746 |
notifierCount--; |
| 747 |
DISABLE_ASL; |
| 748 |
|
| 749 |
/* |
| 750 |
* If this is the last thread to use the notifier, close the notifier pipe |
| 751 |
* and wait for the background thread to terminate. |
| 752 |
*/ |
| 753 |
|
| 754 |
if (notifierCount == 0) { |
| 755 |
if (triggerPipe != -1) { |
| 756 |
/* |
| 757 |
* Send "q" message to the notifier thread so that it will |
| 758 |
* terminate. The notifier will return from its call to select() |
| 759 |
* and notice that a "q" message has arrived, it will then close |
| 760 |
* its side of the pipe and terminate its thread. Note the we can |
| 761 |
* not just close the pipe and check for EOF in the notifier |
| 762 |
* thread because if a background child process was created with |
| 763 |
* exec, select() would not register the EOF on the pipe until the |
| 764 |
* child processes had terminated. [Bug: 4139] [Bug 1222872] |
| 765 |
*/ |
| 766 |
|
| 767 |
write(triggerPipe, "q", 1); |
| 768 |
close(triggerPipe); |
| 769 |
|
| 770 |
if (notifierThreadRunning) { |
| 771 |
int result = pthread_join(notifierThread, NULL); |
| 772 |
|
| 773 |
if (result) { |
| 774 |
Tcl_Panic("Tcl_FinalizeNotifier: unable to join notifier " |
| 775 |
"thread"); |
| 776 |
} |
| 777 |
notifierThreadRunning = 0; |
| 778 |
} |
| 779 |
|
| 780 |
close(receivePipe); |
| 781 |
triggerPipe = -1; |
| 782 |
} |
| 783 |
CLOSE_NOTIFIER_LOG; |
| 784 |
} |
| 785 |
UNLOCK_NOTIFIER_INIT; |
| 786 |
|
| 787 |
LOCK_NOTIFIER_TSD; /* For concurrency with Tcl_AlertNotifier */ |
| 788 |
if (tsdPtr->runLoop) { |
| 789 |
tsdPtr->runLoop = NULL; |
| 790 |
|
| 791 |
/* |
| 792 |
* Remove runLoopSource, runLoopObserver and runLoopTimer from all |
| 793 |
* CFRunLoops. |
| 794 |
*/ |
| 795 |
|
| 796 |
CFRunLoopSourceInvalidate(tsdPtr->runLoopSource); |
| 797 |
CFRelease(tsdPtr->runLoopSource); |
| 798 |
tsdPtr->runLoopSource = NULL; |
| 799 |
CFRunLoopObserverInvalidate(tsdPtr->runLoopObserver); |
| 800 |
CFRelease(tsdPtr->runLoopObserver); |
| 801 |
tsdPtr->runLoopObserver = NULL; |
| 802 |
CFRunLoopObserverInvalidate(tsdPtr->runLoopObserverTcl); |
| 803 |
CFRelease(tsdPtr->runLoopObserverTcl); |
| 804 |
tsdPtr->runLoopObserverTcl = NULL; |
| 805 |
if (tsdPtr->runLoopTimer) { |
| 806 |
CFRunLoopTimerInvalidate(tsdPtr->runLoopTimer); |
| 807 |
CFRelease(tsdPtr->runLoopTimer); |
| 808 |
tsdPtr->runLoopTimer = NULL; |
| 809 |
} |
| 810 |
} |
| 811 |
UNLOCK_NOTIFIER_TSD; |
| 812 |
} |
| 813 |
|
| 814 |
/* |
| 815 |
*---------------------------------------------------------------------- |
| 816 |
* |
| 817 |
* Tcl_AlertNotifier -- |
| 818 |
* |
| 819 |
* Wake up the specified notifier from any thread. This routine is called |
| 820 |
* by the platform independent notifier code whenever the Tcl_ThreadAlert |
| 821 |
* routine is called. This routine is guaranteed not to be called on a |
| 822 |
* given notifier after Tcl_FinalizeNotifier is called for that notifier. |
| 823 |
* |
| 824 |
* Results: |
| 825 |
* None. |
| 826 |
* |
| 827 |
* Side effects: |
| 828 |
* Signals the notifier condition variable for the specified notifier. |
| 829 |
* |
| 830 |
*---------------------------------------------------------------------- |
| 831 |
*/ |
| 832 |
|
| 833 |
void |
| 834 |
Tcl_AlertNotifier( |
| 835 |
ClientData clientData) |
| 836 |
{ |
| 837 |
ThreadSpecificData *tsdPtr = (ThreadSpecificData *)clientData; |
| 838 |
|
| 839 |
if (tclNotifierHooks.alertNotifierProc) { |
| 840 |
tclNotifierHooks.alertNotifierProc(clientData); |
| 841 |
return; |
| 842 |
} |
| 843 |
|
| 844 |
LOCK_NOTIFIER_TSD; |
| 845 |
if (tsdPtr->runLoop) { |
| 846 |
CFRunLoopSourceSignal(tsdPtr->runLoopSource); |
| 847 |
CFRunLoopWakeUp(tsdPtr->runLoop); |
| 848 |
} |
| 849 |
UNLOCK_NOTIFIER_TSD; |
| 850 |
} |
| 851 |
|
| 852 |
/* |
| 853 |
*---------------------------------------------------------------------- |
| 854 |
* |
| 855 |
* Tcl_SetTimer -- |
| 856 |
* |
| 857 |
* This function sets the current notifier timer value. |
| 858 |
* |
| 859 |
* Results: |
| 860 |
* None. |
| 861 |
* |
| 862 |
* Side effects: |
| 863 |
* Replaces any previous timer. |
| 864 |
* |
| 865 |
*---------------------------------------------------------------------- |
| 866 |
*/ |
| 867 |
|
| 868 |
void |
| 869 |
Tcl_SetTimer( |
| 870 |
const Tcl_Time *timePtr) /* Timeout value, may be NULL. */ |
| 871 |
{ |
| 872 |
ThreadSpecificData *tsdPtr; |
| 873 |
CFRunLoopTimerRef runLoopTimer; |
| 874 |
CFTimeInterval waitTime; |
| 875 |
|
| 876 |
if (tclNotifierHooks.setTimerProc) { |
| 877 |
tclNotifierHooks.setTimerProc(timePtr); |
| 878 |
return; |
| 879 |
} |
| 880 |
|
| 881 |
tsdPtr = TCL_TSD_INIT(&dataKey); |
| 882 |
runLoopTimer = tsdPtr->runLoopTimer; |
| 883 |
if (!runLoopTimer) { |
| 884 |
return; |
| 885 |
} |
| 886 |
if (timePtr) { |
| 887 |
Tcl_Time vTime = *timePtr; |
| 888 |
|
| 889 |
if (vTime.sec != 0 || vTime.usec != 0) { |
| 890 |
tclScaleTimeProcPtr(&vTime, tclTimeClientData); |
| 891 |
waitTime = vTime.sec + 1.0e-6 * vTime.usec; |
| 892 |
} else { |
| 893 |
waitTime = 0; |
| 894 |
} |
| 895 |
} else { |
| 896 |
waitTime = CF_TIMEINTERVAL_FOREVER; |
| 897 |
} |
| 898 |
tsdPtr->waitTime = waitTime; |
| 899 |
CFRunLoopTimerSetNextFireDate(runLoopTimer, |
| 900 |
CFAbsoluteTimeGetCurrent() + waitTime); |
| 901 |
} |
| 902 |
|
| 903 |
/* |
| 904 |
*---------------------------------------------------------------------- |
| 905 |
* |
| 906 |
* TimerWakeUp -- |
| 907 |
* |
| 908 |
* CFRunLoopTimer callback. |
| 909 |
* |
| 910 |
* Results: |
| 911 |
* None. |
| 912 |
* |
| 913 |
* Side effects: |
| 914 |
* None. |
| 915 |
* |
| 916 |
*---------------------------------------------------------------------- |
| 917 |
*/ |
| 918 |
|
| 919 |
static void |
| 920 |
TimerWakeUp( |
| 921 |
CFRunLoopTimerRef timer, |
| 922 |
void *info) |
| 923 |
{ |
| 924 |
} |
| 925 |
|
| 926 |
/* |
| 927 |
*---------------------------------------------------------------------- |
| 928 |
* |
| 929 |
* Tcl_ServiceModeHook -- |
| 930 |
* |
| 931 |
* This function is invoked whenever the service mode changes. |
| 932 |
* |
| 933 |
* Results: |
| 934 |
* None. |
| 935 |
* |
| 936 |
* Side effects: |
| 937 |
* None. |
| 938 |
* |
| 939 |
*---------------------------------------------------------------------- |
| 940 |
*/ |
| 941 |
|
| 942 |
void |
| 943 |
Tcl_ServiceModeHook( |
| 944 |
int mode) /* Either TCL_SERVICE_ALL, or |
| 945 |
* TCL_SERVICE_NONE. */ |
| 946 |
{ |
| 947 |
ThreadSpecificData *tsdPtr; |
| 948 |
|
| 949 |
if (tclNotifierHooks.serviceModeHookProc) { |
| 950 |
tclNotifierHooks.serviceModeHookProc(mode); |
| 951 |
return; |
| 952 |
} |
| 953 |
|
| 954 |
tsdPtr = TCL_TSD_INIT(&dataKey); |
| 955 |
|
| 956 |
if (mode == TCL_SERVICE_ALL && !tsdPtr->runLoopTimer) { |
| 957 |
if (!tsdPtr->runLoop) { |
| 958 |
Tcl_Panic("Tcl_ServiceModeHook: Notifier not initialized"); |
| 959 |
} |
| 960 |
tsdPtr->runLoopTimer = CFRunLoopTimerCreate(NULL, |
| 961 |
CFAbsoluteTimeGetCurrent() + CF_TIMEINTERVAL_FOREVER, |
| 962 |
CF_TIMEINTERVAL_FOREVER, 0, 0, TimerWakeUp, NULL); |
| 963 |
if (tsdPtr->runLoopTimer) { |
| 964 |
CFRunLoopAddTimer(tsdPtr->runLoop, tsdPtr->runLoopTimer, |
| 965 |
kCFRunLoopCommonModes); |
| 966 |
StartNotifierThread(); |
| 967 |
} |
| 968 |
} |
| 969 |
} |
| 970 |
|
| 971 |
/* |
| 972 |
*---------------------------------------------------------------------- |
| 973 |
* |
| 974 |
* Tcl_CreateFileHandler -- |
| 975 |
* |
| 976 |
* This function registers a file handler with the select notifier. |
| 977 |
* |
| 978 |
* Results: |
| 979 |
* None. |
| 980 |
* |
| 981 |
* Side effects: |
| 982 |
* Creates a new file handler structure. |
| 983 |
* |
| 984 |
*---------------------------------------------------------------------- |
| 985 |
*/ |
| 986 |
|
| 987 |
void |
| 988 |
Tcl_CreateFileHandler( |
| 989 |
int fd, /* Handle of stream to watch. */ |
| 990 |
int mask, /* OR'ed combination of TCL_READABLE, |
| 991 |
* TCL_WRITABLE, and TCL_EXCEPTION: indicates |
| 992 |
* conditions under which proc should be |
| 993 |
* called. */ |
| 994 |
Tcl_FileProc *proc, /* Function to call for each selected |
| 995 |
* event. */ |
| 996 |
ClientData clientData) /* Arbitrary data to pass to proc. */ |
| 997 |
{ |
| 998 |
ThreadSpecificData *tsdPtr; |
| 999 |
FileHandler *filePtr; |
| 1000 |
|
| 1001 |
if (tclNotifierHooks.createFileHandlerProc) { |
| 1002 |
tclNotifierHooks.createFileHandlerProc(fd, mask, proc, clientData); |
| 1003 |
return; |
| 1004 |
} |
| 1005 |
|
| 1006 |
tsdPtr = TCL_TSD_INIT(&dataKey); |
| 1007 |
|
| 1008 |
for (filePtr = tsdPtr->firstFileHandlerPtr; filePtr != NULL; |
| 1009 |
filePtr = filePtr->nextPtr) { |
| 1010 |
if (filePtr->fd == fd) { |
| 1011 |
break; |
| 1012 |
} |
| 1013 |
} |
| 1014 |
if (filePtr == NULL) { |
| 1015 |
filePtr = (FileHandler *)ckalloc(sizeof(FileHandler)); |
| 1016 |
filePtr->fd = fd; |
| 1017 |
filePtr->readyMask = 0; |
| 1018 |
filePtr->nextPtr = tsdPtr->firstFileHandlerPtr; |
| 1019 |
tsdPtr->firstFileHandlerPtr = filePtr; |
| 1020 |
} |
| 1021 |
filePtr->proc = proc; |
| 1022 |
filePtr->clientData = clientData; |
| 1023 |
filePtr->mask = mask; |
| 1024 |
|
| 1025 |
/* |
| 1026 |
* Update the check masks for this file. |
| 1027 |
*/ |
| 1028 |
|
| 1029 |
LOCK_NOTIFIER_TSD; |
| 1030 |
if (mask & TCL_READABLE) { |
| 1031 |
FD_SET(fd, &tsdPtr->checkMasks.readable); |
| 1032 |
} else { |
| 1033 |
FD_CLR(fd, &tsdPtr->checkMasks.readable); |
| 1034 |
} |
| 1035 |
if (mask & TCL_WRITABLE) { |
| 1036 |
FD_SET(fd, &tsdPtr->checkMasks.writable); |
| 1037 |
} else { |
| 1038 |
FD_CLR(fd, &tsdPtr->checkMasks.writable); |
| 1039 |
} |
| 1040 |
if (mask & TCL_EXCEPTION) { |
| 1041 |
FD_SET(fd, &tsdPtr->checkMasks.exceptional); |
| 1042 |
} else { |
| 1043 |
FD_CLR(fd, &tsdPtr->checkMasks.exceptional); |
| 1044 |
} |
| 1045 |
if (tsdPtr->numFdBits <= fd) { |
| 1046 |
tsdPtr->numFdBits = fd+1; |
| 1047 |
} |
| 1048 |
UNLOCK_NOTIFIER_TSD; |
| 1049 |
} |
| 1050 |
|
| 1051 |
/* |
| 1052 |
*---------------------------------------------------------------------- |
| 1053 |
* |
| 1054 |
* Tcl_DeleteFileHandler -- |
| 1055 |
* |
| 1056 |
* Cancel a previously-arranged callback arrangement for a file. |
| 1057 |
* |
| 1058 |
* Results: |
| 1059 |
* None. |
| 1060 |
* |
| 1061 |
* Side effects: |
| 1062 |
* If a callback was previously registered on file, remove it. |
| 1063 |
* |
| 1064 |
*---------------------------------------------------------------------- |
| 1065 |
*/ |
| 1066 |
|
| 1067 |
void |
| 1068 |
Tcl_DeleteFileHandler( |
| 1069 |
int fd) /* Stream id for which to remove callback |
| 1070 |
* function. */ |
| 1071 |
{ |
| 1072 |
FileHandler *filePtr, *prevPtr; |
| 1073 |
int i, numFdBits; |
| 1074 |
ThreadSpecificData *tsdPtr; |
| 1075 |
|
| 1076 |
if (tclNotifierHooks.deleteFileHandlerProc) { |
| 1077 |
tclNotifierHooks.deleteFileHandlerProc(fd); |
| 1078 |
return; |
| 1079 |
} |
| 1080 |
|
| 1081 |
tsdPtr = TCL_TSD_INIT(&dataKey); |
| 1082 |
numFdBits = -1; |
| 1083 |
|
| 1084 |
/* |
| 1085 |
* Find the entry for the given file (and return if there isn't one). |
| 1086 |
*/ |
| 1087 |
|
| 1088 |
for (prevPtr = NULL, filePtr = tsdPtr->firstFileHandlerPtr; ; |
| 1089 |
prevPtr = filePtr, filePtr = filePtr->nextPtr) { |
| 1090 |
if (filePtr == NULL) { |
| 1091 |
return; |
| 1092 |
} |
| 1093 |
if (filePtr->fd == fd) { |
| 1094 |
break; |
| 1095 |
} |
| 1096 |
} |
| 1097 |
|
| 1098 |
/* |
| 1099 |
* Find current max fd. |
| 1100 |
*/ |
| 1101 |
|
| 1102 |
if (fd+1 == tsdPtr->numFdBits) { |
| 1103 |
numFdBits = 0; |
| 1104 |
for (i = fd-1; i >= 0; i--) { |
| 1105 |
if (FD_ISSET(i, &tsdPtr->checkMasks.readable) |
| 1106 |
|| FD_ISSET(i, &tsdPtr->checkMasks.writable) |
| 1107 |
|| FD_ISSET(i, &tsdPtr->checkMasks.exceptional)) { |
| 1108 |
numFdBits = i+1; |
| 1109 |
break; |
| 1110 |
} |
| 1111 |
} |
| 1112 |
} |
| 1113 |
|
| 1114 |
LOCK_NOTIFIER_TSD; |
| 1115 |
if (numFdBits != -1) { |
| 1116 |
tsdPtr->numFdBits = numFdBits; |
| 1117 |
} |
| 1118 |
|
| 1119 |
/* |
| 1120 |
* Update the check masks for this file. |
| 1121 |
*/ |
| 1122 |
|
| 1123 |
if (filePtr->mask & TCL_READABLE) { |
| 1124 |
FD_CLR(fd, &tsdPtr->checkMasks.readable); |
| 1125 |
} |
| 1126 |
if (filePtr->mask & TCL_WRITABLE) { |
| 1127 |
FD_CLR(fd, &tsdPtr->checkMasks.writable); |
| 1128 |
} |
| 1129 |
if (filePtr->mask & TCL_EXCEPTION) { |
| 1130 |
FD_CLR(fd, &tsdPtr->checkMasks.exceptional); |
| 1131 |
} |
| 1132 |
UNLOCK_NOTIFIER_TSD; |
| 1133 |
|
| 1134 |
/* |
| 1135 |
* Clean up information in the callback record. |
| 1136 |
*/ |
| 1137 |
|
| 1138 |
if (prevPtr == NULL) { |
| 1139 |
tsdPtr->firstFileHandlerPtr = filePtr->nextPtr; |
| 1140 |
} else { |
| 1141 |
prevPtr->nextPtr = filePtr->nextPtr; |
| 1142 |
} |
| 1143 |
ckfree(filePtr); |
| 1144 |
} |
| 1145 |
|
| 1146 |
/* |
| 1147 |
*---------------------------------------------------------------------- |
| 1148 |
* |
| 1149 |
* FileHandlerEventProc -- |
| 1150 |
* |
| 1151 |
* This function is called by Tcl_ServiceEvent when a file event reaches |
| 1152 |
* the front of the event queue. This function is responsible for |
| 1153 |
* actually handling the event by invoking the callback for the file |
| 1154 |
* handler. |
| 1155 |
* |
| 1156 |
* Results: |
| 1157 |
* Returns 1 if the event was handled, meaning it should be removed from |
| 1158 |
* the queue. Returns 0 if the event was not handled, meaning it should |
| 1159 |
* stay on the queue. The only time the event isn't handled is if the |
| 1160 |
* TCL_FILE_EVENTS flag bit isn't set. |
| 1161 |
* |
| 1162 |
* Side effects: |
| 1163 |
* Whatever the file handler's callback function does. |
| 1164 |
* |
| 1165 |
*---------------------------------------------------------------------- |
| 1166 |
*/ |
| 1167 |
|
| 1168 |
static int |
| 1169 |
FileHandlerEventProc( |
| 1170 |
Tcl_Event *evPtr, /* Event to service. */ |
| 1171 |
int flags) /* Flags that indicate what events to handle, |
| 1172 |
* such as TCL_FILE_EVENTS. */ |
| 1173 |
{ |
| 1174 |
int mask; |
| 1175 |
FileHandler *filePtr; |
| 1176 |
FileHandlerEvent *fileEvPtr = (FileHandlerEvent *) evPtr; |
| 1177 |
ThreadSpecificData *tsdPtr; |
| 1178 |
|
| 1179 |
if (!(flags & TCL_FILE_EVENTS)) { |
| 1180 |
return 0; |
| 1181 |
} |
| 1182 |
|
| 1183 |
/* |
| 1184 |
* Search through the file handlers to find the one whose handle matches |
| 1185 |
* the event. We do this rather than keeping a pointer to the file handler |
| 1186 |
* directly in the event, so that the handler can be deleted while the |
| 1187 |
* event is queued without leaving a dangling pointer. |
| 1188 |
*/ |
| 1189 |
|
| 1190 |
tsdPtr = TCL_TSD_INIT(&dataKey); |
| 1191 |
for (filePtr = tsdPtr->firstFileHandlerPtr; filePtr != NULL; |
| 1192 |
filePtr = filePtr->nextPtr) { |
| 1193 |
if (filePtr->fd != fileEvPtr->fd) { |
| 1194 |
continue; |
| 1195 |
} |
| 1196 |
|
| 1197 |
/* |
| 1198 |
* The code is tricky for two reasons: |
| 1199 |
* 1. The file handler's desired events could have changed since the |
| 1200 |
* time when the event was queued, so AND the ready mask with the |
| 1201 |
* desired mask. |
| 1202 |
* 2. The file could have been closed and re-opened since the time |
| 1203 |
* when the event was queued. This is why the ready mask is stored |
| 1204 |
* in the file handler rather than the queued event: it will be |
| 1205 |
* zeroed when a new file handler is created for the newly opened |
| 1206 |
* file. |
| 1207 |
*/ |
| 1208 |
|
| 1209 |
mask = filePtr->readyMask & filePtr->mask; |
| 1210 |
filePtr->readyMask = 0; |
| 1211 |
if (mask != 0) { |
| 1212 |
LOCK_NOTIFIER_TSD; |
| 1213 |
if (mask & TCL_READABLE) { |
| 1214 |
FD_CLR(filePtr->fd, &tsdPtr->readyMasks.readable); |
| 1215 |
} |
| 1216 |
if (mask & TCL_WRITABLE) { |
| 1217 |
FD_CLR(filePtr->fd, &tsdPtr->readyMasks.writable); |
| 1218 |
} |
| 1219 |
if (mask & TCL_EXCEPTION) { |
| 1220 |
FD_CLR(filePtr->fd, &tsdPtr->readyMasks.exceptional); |
| 1221 |
} |
| 1222 |
UNLOCK_NOTIFIER_TSD; |
| 1223 |
filePtr->proc(filePtr->clientData, mask); |
| 1224 |
} |
| 1225 |
break; |
| 1226 |
} |
| 1227 |
return 1; |
| 1228 |
} |
| 1229 |
|
| 1230 |
/* |
| 1231 |
*---------------------------------------------------------------------- |
| 1232 |
* |
| 1233 |
* Tcl_WaitForEvent -- |
| 1234 |
* |
| 1235 |
* This function is called by Tcl_DoOneEvent to wait for new events on |
| 1236 |
* the message queue. If the block time is 0, then Tcl_WaitForEvent just |
| 1237 |
* polls without blocking. |
| 1238 |
* |
| 1239 |
* Results: |
| 1240 |
* Returns 0 if a tcl event or timeout occurred and 1 if a non-tcl |
| 1241 |
* CFRunLoop source was processed. |
| 1242 |
* |
| 1243 |
* Side effects: |
| 1244 |
* None. |
| 1245 |
* |
| 1246 |
*---------------------------------------------------------------------- |
| 1247 |
*/ |
| 1248 |
|
| 1249 |
int |
| 1250 |
Tcl_WaitForEvent( |
| 1251 |
const Tcl_Time *timePtr) /* Maximum block time, or NULL. */ |
| 1252 |
{ |
| 1253 |
int result, polling, runLoopRunning; |
| 1254 |
CFTimeInterval waitTime; |
| 1255 |
SInt32 runLoopStatus; |
| 1256 |
ThreadSpecificData *tsdPtr; |
| 1257 |
|
| 1258 |
if (tclNotifierHooks.waitForEventProc) { |
| 1259 |
return tclNotifierHooks.waitForEventProc(timePtr); |
| 1260 |
} |
| 1261 |
result = -1; |
| 1262 |
polling = 0; |
| 1263 |
waitTime = CF_TIMEINTERVAL_FOREVER; |
| 1264 |
tsdPtr = TCL_TSD_INIT(&dataKey); |
| 1265 |
|
| 1266 |
if (!tsdPtr->runLoop) { |
| 1267 |
Tcl_Panic("Tcl_WaitForEvent: Notifier not initialized"); |
| 1268 |
} |
| 1269 |
|
| 1270 |
/* |
| 1271 |
* A NULL timePtr means wait forever. |
| 1272 |
*/ |
| 1273 |
|
| 1274 |
if (timePtr) { |
| 1275 |
Tcl_Time vTime = *timePtr; |
| 1276 |
|
| 1277 |
/* |
| 1278 |
* TIP #233 (Virtualized Time). Is virtual time in effect? And do we |
| 1279 |
* actually have something to scale? If yes to both then we call the |
| 1280 |
* handler to do this scaling. |
| 1281 |
*/ |
| 1282 |
|
| 1283 |
if (vTime.sec != 0 || vTime.usec != 0) { |
| 1284 |
tclScaleTimeProcPtr(&vTime, tclTimeClientData); |
| 1285 |
waitTime = vTime.sec + 1.0e-6 * vTime.usec; |
| 1286 |
} else { |
| 1287 |
|
| 1288 |
/* |
| 1289 |
* The max block time was set to 0. |
| 1290 |
* |
| 1291 |
* If we set the waitTime to 0, then the call to CFRunLoopInMode |
| 1292 |
* may return without processing all of its sources. The Apple |
| 1293 |
* documentation says that if the waitTime is 0 "only one pass is |
| 1294 |
* made through the run loop before returning; if multiple sources |
| 1295 |
* or timers are ready to fire immediately, only one (possibly two |
| 1296 |
* if one is a version 0 source) will be fired, regardless of the |
| 1297 |
* value of returnAfterSourceHandled." This can cause some chanio |
| 1298 |
* tests to fail. So we use a small positive waitTime unless there |
| 1299 |
* is another RunLoop running. |
| 1300 |
*/ |
| 1301 |
|
| 1302 |
polling = 1; |
| 1303 |
waitTime = tsdPtr->runLoopRunning ? 0 : 0.0001; |
| 1304 |
} |
| 1305 |
} |
| 1306 |
|
| 1307 |
StartNotifierThread(); |
| 1308 |
|
| 1309 |
LOCK_NOTIFIER_TSD; |
| 1310 |
tsdPtr->polling = polling; |
| 1311 |
UNLOCK_NOTIFIER_TSD; |
| 1312 |
tsdPtr->runLoopSourcePerformed = 0; |
| 1313 |
|
| 1314 |
/* |
| 1315 |
* If the Tcl runloop is already running (e.g. if Tcl_WaitForEvent was |
| 1316 |
* called recursively) start a new runloop in a custom runloop mode |
| 1317 |
* containing only the source for the notifier thread. Otherwise wakeups |
| 1318 |
* from other sources added to the common runloop mode might get lost or |
| 1319 |
* 3rd party event handlers might get called when they do not expect to |
| 1320 |
* be. |
| 1321 |
*/ |
| 1322 |
|
| 1323 |
runLoopRunning = tsdPtr->runLoopRunning; |
| 1324 |
tsdPtr->runLoopRunning = 1; |
| 1325 |
runLoopStatus = CFRunLoopRunInMode( |
| 1326 |
runLoopRunning ? tclEventsOnlyRunLoopMode : kCFRunLoopDefaultMode, |
| 1327 |
waitTime, TRUE); |
| 1328 |
tsdPtr->runLoopRunning = runLoopRunning; |
| 1329 |
|
| 1330 |
LOCK_NOTIFIER_TSD; |
| 1331 |
tsdPtr->polling = 0; |
| 1332 |
UNLOCK_NOTIFIER_TSD; |
| 1333 |
switch (runLoopStatus) { |
| 1334 |
case kCFRunLoopRunFinished: |
| 1335 |
Tcl_Panic("Tcl_WaitForEvent: CFRunLoop finished"); |
| 1336 |
break; |
| 1337 |
case kCFRunLoopRunTimedOut: |
| 1338 |
QueueFileEvents(tsdPtr); |
| 1339 |
result = 0; |
| 1340 |
break; |
| 1341 |
case kCFRunLoopRunStopped: |
| 1342 |
case kCFRunLoopRunHandledSource: |
| 1343 |
result = tsdPtr->runLoopSourcePerformed ? 0 : 1; |
| 1344 |
break; |
| 1345 |
} |
| 1346 |
|
| 1347 |
return result; |
| 1348 |
} |
| 1349 |
|
| 1350 |
/* |
| 1351 |
*---------------------------------------------------------------------- |
| 1352 |
* |
| 1353 |
* QueueFileEvents -- |
| 1354 |
* |
| 1355 |
* CFRunLoopSource callback for queueing file events. |
| 1356 |
* |
| 1357 |
* Results: |
| 1358 |
* None. |
| 1359 |
* |
| 1360 |
* Side effects: |
| 1361 |
* Queues file events that are detected by the select. |
| 1362 |
* |
| 1363 |
*---------------------------------------------------------------------- |
| 1364 |
*/ |
| 1365 |
|
| 1366 |
static void |
| 1367 |
QueueFileEvents( |
| 1368 |
void *info) |
| 1369 |
{ |
| 1370 |
SelectMasks readyMasks; |
| 1371 |
FileHandler *filePtr; |
| 1372 |
ThreadSpecificData *tsdPtr = (ThreadSpecificData *)info; |
| 1373 |
|
| 1374 |
/* |
| 1375 |
* Queue all detected file events. |
| 1376 |
*/ |
| 1377 |
|
| 1378 |
LOCK_NOTIFIER_TSD; |
| 1379 |
FD_COPY(&tsdPtr->readyMasks.readable, &readyMasks.readable); |
| 1380 |
FD_COPY(&tsdPtr->readyMasks.writable, &readyMasks.writable); |
| 1381 |
FD_COPY(&tsdPtr->readyMasks.exceptional, &readyMasks.exceptional); |
| 1382 |
FD_ZERO(&tsdPtr->readyMasks.readable); |
| 1383 |
FD_ZERO(&tsdPtr->readyMasks.writable); |
| 1384 |
FD_ZERO(&tsdPtr->readyMasks.exceptional); |
| 1385 |
UNLOCK_NOTIFIER_TSD; |
| 1386 |
tsdPtr->runLoopSourcePerformed = 1; |
| 1387 |
|
| 1388 |
for (filePtr = tsdPtr->firstFileHandlerPtr; (filePtr != NULL); |
| 1389 |
filePtr = filePtr->nextPtr) { |
| 1390 |
int mask = 0; |
| 1391 |
|
| 1392 |
if (FD_ISSET(filePtr->fd, &readyMasks.readable)) { |
| 1393 |
mask |= TCL_READABLE; |
| 1394 |
} |
| 1395 |
if (FD_ISSET(filePtr->fd, &readyMasks.writable)) { |
| 1396 |
mask |= TCL_WRITABLE; |
| 1397 |
} |
| 1398 |
if (FD_ISSET(filePtr->fd, &readyMasks.exceptional)) { |
| 1399 |
mask |= TCL_EXCEPTION; |
| 1400 |
} |
| 1401 |
if (!mask) { |
| 1402 |
continue; |
| 1403 |
} |
| 1404 |
|
| 1405 |
/* |
| 1406 |
* Don't bother to queue an event if the mask was previously non-zero |
| 1407 |
* since an event must still be on the queue. |
| 1408 |
*/ |
| 1409 |
|
| 1410 |
if (filePtr->readyMask == 0) { |
| 1411 |
FileHandlerEvent *fileEvPtr = (FileHandlerEvent *)ckalloc(sizeof(FileHandlerEvent)); |
| 1412 |
|
| 1413 |
fileEvPtr->header.proc = FileHandlerEventProc; |
| 1414 |
fileEvPtr->fd = filePtr->fd; |
| 1415 |
Tcl_QueueEvent((Tcl_Event *) fileEvPtr, TCL_QUEUE_TAIL); |
| 1416 |
} |
| 1417 |
filePtr->readyMask = mask; |
| 1418 |
} |
| 1419 |
} |
| 1420 |
|
| 1421 |
/* |
| 1422 |
*---------------------------------------------------------------------- |
| 1423 |
* |
| 1424 |
* UpdateWaitingListAndServiceEvents -- |
| 1425 |
* |
| 1426 |
* CFRunLoopObserver callback for updating waitingList and |
| 1427 |
* servicing Tcl events. |
| 1428 |
* |
| 1429 |
* Results: |
| 1430 |
* None. |
| 1431 |
* |
| 1432 |
* Side effects: |
| 1433 |
* None. |
| 1434 |
* |
| 1435 |
*---------------------------------------------------------------------- |
| 1436 |
*/ |
| 1437 |
|
| 1438 |
static void |
| 1439 |
UpdateWaitingListAndServiceEvents( |
| 1440 |
CFRunLoopObserverRef observer, |
| 1441 |
CFRunLoopActivity activity, |
| 1442 |
void *info) |
| 1443 |
{ |
| 1444 |
ThreadSpecificData *tsdPtr = (ThreadSpecificData *)info; |
| 1445 |
if (tsdPtr->sleeping) { |
| 1446 |
return; |
| 1447 |
} |
| 1448 |
switch (activity) { |
| 1449 |
case kCFRunLoopEntry: |
| 1450 |
tsdPtr->runLoopNestingLevel++; |
| 1451 |
if (tsdPtr->numFdBits > 0 || tsdPtr->polling) { |
| 1452 |
LOCK_NOTIFIER; |
| 1453 |
if (!OnOffWaitingList(tsdPtr, 1, 1) && tsdPtr->polling) { |
| 1454 |
write(triggerPipe, "", 1); |
| 1455 |
} |
| 1456 |
UNLOCK_NOTIFIER; |
| 1457 |
} |
| 1458 |
break; |
| 1459 |
case kCFRunLoopExit: |
| 1460 |
if (tsdPtr->runLoopNestingLevel == 1) { |
| 1461 |
LOCK_NOTIFIER; |
| 1462 |
OnOffWaitingList(tsdPtr, 0, 1); |
| 1463 |
UNLOCK_NOTIFIER; |
| 1464 |
} |
| 1465 |
tsdPtr->runLoopNestingLevel--; |
| 1466 |
break; |
| 1467 |
default: |
| 1468 |
break; |
| 1469 |
} |
| 1470 |
} |
| 1471 |
|
| 1472 |
/* |
| 1473 |
*---------------------------------------------------------------------- |
| 1474 |
* |
| 1475 |
* OnOffWaitingList -- |
| 1476 |
* |
| 1477 |
* Add/remove the specified thread to/from the global waitingList and |
| 1478 |
* optionally signal the notifier. |
| 1479 |
* |
| 1480 |
* !!! Requires notifierLock to be held !!! |
| 1481 |
* |
| 1482 |
* Results: |
| 1483 |
* Boolean indicating whether the waitingList was changed. |
| 1484 |
* |
| 1485 |
* Side effects: |
| 1486 |
* None. |
| 1487 |
* |
| 1488 |
*---------------------------------------------------------------------- |
| 1489 |
*/ |
| 1490 |
|
| 1491 |
static int |
| 1492 |
OnOffWaitingList( |
| 1493 |
ThreadSpecificData *tsdPtr, |
| 1494 |
int onList, |
| 1495 |
int signalNotifier) |
| 1496 |
{ |
| 1497 |
int changeWaitingList; |
| 1498 |
|
| 1499 |
#if defined(TCL_MAC_DEBUG_NOTIFIER) && !defined(USE_OS_UNFAIR_LOCK) |
| 1500 |
if (SpinLockTry(¬ifierLock)) { |
| 1501 |
Tcl_Panic("OnOffWaitingList: notifierLock unlocked"); |
| 1502 |
} |
| 1503 |
#endif |
| 1504 |
changeWaitingList = (!onList ^ !tsdPtr->onList); |
| 1505 |
if (changeWaitingList) { |
| 1506 |
if (onList) { |
| 1507 |
tsdPtr->nextPtr = waitingListPtr; |
| 1508 |
if (waitingListPtr) { |
| 1509 |
waitingListPtr->prevPtr = tsdPtr; |
| 1510 |
} |
| 1511 |
tsdPtr->prevPtr = NULL; |
| 1512 |
waitingListPtr = tsdPtr; |
| 1513 |
tsdPtr->onList = 1; |
| 1514 |
} else { |
| 1515 |
if (tsdPtr->prevPtr) { |
| 1516 |
tsdPtr->prevPtr->nextPtr = tsdPtr->nextPtr; |
| 1517 |
} else { |
| 1518 |
waitingListPtr = tsdPtr->nextPtr; |
| 1519 |
} |
| 1520 |
if (tsdPtr->nextPtr) { |
| 1521 |
tsdPtr->nextPtr->prevPtr = tsdPtr->prevPtr; |
| 1522 |
} |
| 1523 |
tsdPtr->nextPtr = tsdPtr->prevPtr = NULL; |
| 1524 |
tsdPtr->onList = 0; |
| 1525 |
} |
| 1526 |
if (signalNotifier) { |
| 1527 |
write(triggerPipe, "", 1); |
| 1528 |
} |
| 1529 |
} |
| 1530 |
|
| 1531 |
return changeWaitingList; |
| 1532 |
} |
| 1533 |
|
| 1534 |
/* |
| 1535 |
*---------------------------------------------------------------------- |
| 1536 |
* |
| 1537 |
* Tcl_Sleep -- |
| 1538 |
* |
| 1539 |
* Delay execution for the specified number of milliseconds. |
| 1540 |
* |
| 1541 |
* Results: |
| 1542 |
* None. |
| 1543 |
* |
| 1544 |
* Side effects: |
| 1545 |
* Time passes. |
| 1546 |
* |
| 1547 |
*---------------------------------------------------------------------- |
| 1548 |
*/ |
| 1549 |
|
| 1550 |
void |
| 1551 |
Tcl_Sleep( |
| 1552 |
int ms) /* Number of milliseconds to sleep. */ |
| 1553 |
{ |
| 1554 |
Tcl_Time vdelay; |
| 1555 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 1556 |
|
| 1557 |
if (ms <= 0) { |
| 1558 |
return; |
| 1559 |
} |
| 1560 |
|
| 1561 |
/* |
| 1562 |
* TIP #233: Scale from virtual time to real-time. |
| 1563 |
*/ |
| 1564 |
|
| 1565 |
vdelay.sec = ms / 1000; |
| 1566 |
vdelay.usec = (ms % 1000) * 1000; |
| 1567 |
tclScaleTimeProcPtr(&vdelay, tclTimeClientData); |
| 1568 |
|
| 1569 |
|
| 1570 |
if (tsdPtr->runLoop) { |
| 1571 |
CFTimeInterval waitTime; |
| 1572 |
CFRunLoopTimerRef runLoopTimer = tsdPtr->runLoopTimer; |
| 1573 |
CFAbsoluteTime nextTimerFire = 0, waitEnd, now; |
| 1574 |
SInt32 runLoopStatus; |
| 1575 |
|
| 1576 |
waitTime = vdelay.sec + 1.0e-6 * vdelay.usec; |
| 1577 |
now = CFAbsoluteTimeGetCurrent(); |
| 1578 |
waitEnd = now + waitTime; |
| 1579 |
|
| 1580 |
if (runLoopTimer) { |
| 1581 |
nextTimerFire = CFRunLoopTimerGetNextFireDate(runLoopTimer); |
| 1582 |
if (nextTimerFire < waitEnd) { |
| 1583 |
CFRunLoopTimerSetNextFireDate(runLoopTimer, now + |
| 1584 |
CF_TIMEINTERVAL_FOREVER); |
| 1585 |
} else { |
| 1586 |
runLoopTimer = NULL; |
| 1587 |
} |
| 1588 |
} |
| 1589 |
tsdPtr->sleeping = 1; |
| 1590 |
do { |
| 1591 |
runLoopStatus = CFRunLoopRunInMode(kCFRunLoopDefaultMode, |
| 1592 |
waitTime, FALSE); |
| 1593 |
switch (runLoopStatus) { |
| 1594 |
case kCFRunLoopRunFinished: |
| 1595 |
Tcl_Panic("Tcl_Sleep: CFRunLoop finished"); |
| 1596 |
break; |
| 1597 |
case kCFRunLoopRunStopped: |
| 1598 |
TclMacOSXNotifierDbgMsg("CFRunLoop stopped"); |
| 1599 |
waitTime = waitEnd - CFAbsoluteTimeGetCurrent(); |
| 1600 |
break; |
| 1601 |
case kCFRunLoopRunTimedOut: |
| 1602 |
waitTime = 0; |
| 1603 |
break; |
| 1604 |
} |
| 1605 |
} while (waitTime > 0); |
| 1606 |
tsdPtr->sleeping = 0; |
| 1607 |
if (runLoopTimer) { |
| 1608 |
CFRunLoopTimerSetNextFireDate(runLoopTimer, nextTimerFire); |
| 1609 |
} |
| 1610 |
} else { |
| 1611 |
struct timespec waitTime; |
| 1612 |
|
| 1613 |
waitTime.tv_sec = vdelay.sec; |
| 1614 |
waitTime.tv_nsec = vdelay.usec * 1000; |
| 1615 |
while (nanosleep(&waitTime, &waitTime)); |
| 1616 |
} |
| 1617 |
} |
| 1618 |
|
| 1619 |
/* |
| 1620 |
*---------------------------------------------------------------------- |
| 1621 |
* |
| 1622 |
* TclUnixWaitForFile -- |
| 1623 |
* |
| 1624 |
* This function waits synchronously for a file to become readable or |
| 1625 |
* writable, with an optional timeout. |
| 1626 |
* |
| 1627 |
* Results: |
| 1628 |
* The return value is an OR'ed combination of TCL_READABLE, |
| 1629 |
* TCL_WRITABLE, and TCL_EXCEPTION, indicating the conditions that are |
| 1630 |
* present on file at the time of the return. This function will not |
| 1631 |
* return until either "timeout" milliseconds have elapsed or at least |
| 1632 |
* one of the conditions given by mask has occurred for file (a return |
| 1633 |
* value of 0 means that a timeout occurred). No normal events will be |
| 1634 |
* serviced during the execution of this function. |
| 1635 |
* |
| 1636 |
* Side effects: |
| 1637 |
* Time passes. |
| 1638 |
* |
| 1639 |
*---------------------------------------------------------------------- |
| 1640 |
*/ |
| 1641 |
|
| 1642 |
int |
| 1643 |
TclUnixWaitForFile( |
| 1644 |
int fd, /* Handle for file on which to wait. */ |
| 1645 |
int mask, /* What to wait for: OR'ed combination of |
| 1646 |
* TCL_READABLE, TCL_WRITABLE, and |
| 1647 |
* TCL_EXCEPTION. */ |
| 1648 |
int timeout) /* Maximum amount of time to wait for one of |
| 1649 |
* the conditions in mask to occur, in |
| 1650 |
* milliseconds. A value of 0 means don't wait |
| 1651 |
* at all, and a value of -1 means wait |
| 1652 |
* forever. */ |
| 1653 |
{ |
| 1654 |
Tcl_Time abortTime = {0, 0}, now; /* silence gcc 4 warning */ |
| 1655 |
struct timeval blockTime, *timeoutPtr; |
| 1656 |
int numFound, result = 0; |
| 1657 |
fd_set readableMask; |
| 1658 |
fd_set writableMask; |
| 1659 |
fd_set exceptionalMask; |
| 1660 |
|
| 1661 |
#define SET_BITS(var, bits) ((var) |= (bits)) |
| 1662 |
#define CLEAR_BITS(var, bits) ((var) &= ~(bits)) |
| 1663 |
|
| 1664 |
#ifndef _DARWIN_C_SOURCE |
| 1665 |
/* |
| 1666 |
* Sanity check fd. |
| 1667 |
*/ |
| 1668 |
|
| 1669 |
if (fd >= FD_SETSIZE) { |
| 1670 |
Tcl_Panic("TclUnixWaitForFile can't handle file id %d", fd); |
| 1671 |
/* must never get here, or select masks overrun will occur below */ |
| 1672 |
} |
| 1673 |
#endif |
| 1674 |
|
| 1675 |
/* |
| 1676 |
* If there is a non-zero finite timeout, compute the time when we give |
| 1677 |
* up. |
| 1678 |
*/ |
| 1679 |
|
| 1680 |
if (timeout > 0) { |
| 1681 |
Tcl_GetTime(&now); |
| 1682 |
abortTime.sec = now.sec + timeout/1000; |
| 1683 |
abortTime.usec = now.usec + (timeout%1000)*1000; |
| 1684 |
if (abortTime.usec >= 1000000) { |
| 1685 |
abortTime.usec -= 1000000; |
| 1686 |
abortTime.sec += 1; |
| 1687 |
} |
| 1688 |
timeoutPtr = &blockTime; |
| 1689 |
} else if (timeout == 0) { |
| 1690 |
timeoutPtr = &blockTime; |
| 1691 |
blockTime.tv_sec = 0; |
| 1692 |
blockTime.tv_usec = 0; |
| 1693 |
} else { |
| 1694 |
timeoutPtr = NULL; |
| 1695 |
} |
| 1696 |
|
| 1697 |
/* |
| 1698 |
* Initialize the select masks. |
| 1699 |
*/ |
| 1700 |
|
| 1701 |
FD_ZERO(&readableMask); |
| 1702 |
FD_ZERO(&writableMask); |
| 1703 |
FD_ZERO(&exceptionalMask); |
| 1704 |
|
| 1705 |
/* |
| 1706 |
* Loop in a mini-event loop of our own, waiting for either the file to |
| 1707 |
* become ready or a timeout to occur. |
| 1708 |
*/ |
| 1709 |
|
| 1710 |
while (1) { |
| 1711 |
if (timeout > 0) { |
| 1712 |
blockTime.tv_sec = abortTime.sec - now.sec; |
| 1713 |
blockTime.tv_usec = abortTime.usec - now.usec; |
| 1714 |
if (blockTime.tv_usec < 0) { |
| 1715 |
blockTime.tv_sec -= 1; |
| 1716 |
blockTime.tv_usec += 1000000; |
| 1717 |
} |
| 1718 |
if (blockTime.tv_sec < 0) { |
| 1719 |
blockTime.tv_sec = 0; |
| 1720 |
blockTime.tv_usec = 0; |
| 1721 |
} |
| 1722 |
} |
| 1723 |
|
| 1724 |
/* |
| 1725 |
* Setup the select masks for the fd. |
| 1726 |
*/ |
| 1727 |
|
| 1728 |
if (mask & TCL_READABLE) { |
| 1729 |
FD_SET(fd, &readableMask); |
| 1730 |
} |
| 1731 |
if (mask & TCL_WRITABLE) { |
| 1732 |
FD_SET(fd, &writableMask); |
| 1733 |
} |
| 1734 |
if (mask & TCL_EXCEPTION) { |
| 1735 |
FD_SET(fd, &exceptionalMask); |
| 1736 |
} |
| 1737 |
|
| 1738 |
/* |
| 1739 |
* Wait for the event or a timeout. |
| 1740 |
*/ |
| 1741 |
|
| 1742 |
numFound = select(fd + 1, &readableMask, &writableMask, |
| 1743 |
&exceptionalMask, timeoutPtr); |
| 1744 |
if (numFound == 1) { |
| 1745 |
if (FD_ISSET(fd, &readableMask)) { |
| 1746 |
SET_BITS(result, TCL_READABLE); |
| 1747 |
} |
| 1748 |
if (FD_ISSET(fd, &writableMask)) { |
| 1749 |
SET_BITS(result, TCL_WRITABLE); |
| 1750 |
} |
| 1751 |
if (FD_ISSET(fd, &exceptionalMask)) { |
| 1752 |
SET_BITS(result, TCL_EXCEPTION); |
| 1753 |
} |
| 1754 |
result &= mask; |
| 1755 |
if (result) { |
| 1756 |
break; |
| 1757 |
} |
| 1758 |
} |
| 1759 |
if (timeout == 0) { |
| 1760 |
break; |
| 1761 |
} |
| 1762 |
if (timeout < 0) { |
| 1763 |
continue; |
| 1764 |
} |
| 1765 |
|
| 1766 |
/* |
| 1767 |
* The select returned early, so we need to recompute the timeout. |
| 1768 |
*/ |
| 1769 |
|
| 1770 |
Tcl_GetTime(&now); |
| 1771 |
if ((abortTime.sec < now.sec) |
| 1772 |
|| (abortTime.sec==now.sec && abortTime.usec<=now.usec)) { |
| 1773 |
break; |
| 1774 |
} |
| 1775 |
} |
| 1776 |
return result; |
| 1777 |
} |
| 1778 |
|
| 1779 |
/* |
| 1780 |
*---------------------------------------------------------------------- |
| 1781 |
* |
| 1782 |
* NotifierThreadProc -- |
| 1783 |
* |
| 1784 |
* This routine is the initial (and only) function executed by the |
| 1785 |
* special notifier thread. Its job is to wait for file descriptors to |
| 1786 |
* become readable or writable or to have an exception condition and then |
| 1787 |
* to notify other threads who are interested in this information by |
| 1788 |
* signalling a condition variable. Other threads can signal this |
| 1789 |
* notifier thread of a change in their interests by writing a single |
| 1790 |
* byte to a special pipe that the notifier thread is monitoring. |
| 1791 |
* |
| 1792 |
* Result: |
| 1793 |
* None. Once started, this routine never exits. It dies with the overall |
| 1794 |
* process. |
| 1795 |
* |
| 1796 |
* Side effects: |
| 1797 |
* The trigger pipe used to signal the notifier thread is created when |
| 1798 |
* the notifier thread first starts. |
| 1799 |
* |
| 1800 |
*---------------------------------------------------------------------- |
| 1801 |
*/ |
| 1802 |
|
| 1803 |
static void |
| 1804 |
NotifierThreadProc( |
| 1805 |
ClientData clientData) /* Not used. */ |
| 1806 |
{ |
| 1807 |
ThreadSpecificData *tsdPtr; |
| 1808 |
fd_set readableMask, writableMask, exceptionalMask; |
| 1809 |
int i, numFdBits = 0, polling; |
| 1810 |
struct timeval poll = {0., 0.}, *timePtr; |
| 1811 |
char buf[2]; |
| 1812 |
|
| 1813 |
/* |
| 1814 |
* Look for file events and report them to interested threads. |
| 1815 |
*/ |
| 1816 |
|
| 1817 |
while (1) { |
| 1818 |
FD_ZERO(&readableMask); |
| 1819 |
FD_ZERO(&writableMask); |
| 1820 |
FD_ZERO(&exceptionalMask); |
| 1821 |
|
| 1822 |
/* |
| 1823 |
* Compute the logical OR of the select masks from all the waiting |
| 1824 |
* notifiers. |
| 1825 |
*/ |
| 1826 |
|
| 1827 |
timePtr = NULL; |
| 1828 |
LOCK_NOTIFIER; |
| 1829 |
for (tsdPtr = waitingListPtr; tsdPtr; tsdPtr = tsdPtr->nextPtr) { |
| 1830 |
LOCK_NOTIFIER_TSD; |
| 1831 |
for (i = tsdPtr->numFdBits-1; i >= 0; --i) { |
| 1832 |
if (FD_ISSET(i, &tsdPtr->checkMasks.readable)) { |
| 1833 |
FD_SET(i, &readableMask); |
| 1834 |
} |
| 1835 |
if (FD_ISSET(i, &tsdPtr->checkMasks.writable)) { |
| 1836 |
FD_SET(i, &writableMask); |
| 1837 |
} |
| 1838 |
if (FD_ISSET(i, &tsdPtr->checkMasks.exceptional)) { |
| 1839 |
FD_SET(i, &exceptionalMask); |
| 1840 |
} |
| 1841 |
} |
| 1842 |
if (tsdPtr->numFdBits > numFdBits) { |
| 1843 |
numFdBits = tsdPtr->numFdBits; |
| 1844 |
} |
| 1845 |
polling = tsdPtr->polling; |
| 1846 |
UNLOCK_NOTIFIER_TSD; |
| 1847 |
if ((tsdPtr->polled = polling)) { |
| 1848 |
timePtr = &poll; |
| 1849 |
} |
| 1850 |
} |
| 1851 |
UNLOCK_NOTIFIER; |
| 1852 |
|
| 1853 |
/* |
| 1854 |
* Set up the select mask to include the receive pipe. |
| 1855 |
*/ |
| 1856 |
|
| 1857 |
if (receivePipe >= numFdBits) { |
| 1858 |
numFdBits = receivePipe + 1; |
| 1859 |
} |
| 1860 |
FD_SET(receivePipe, &readableMask); |
| 1861 |
|
| 1862 |
if (select(numFdBits, &readableMask, &writableMask, &exceptionalMask, |
| 1863 |
timePtr) == -1) { |
| 1864 |
/* |
| 1865 |
* Try again immediately on an error. |
| 1866 |
*/ |
| 1867 |
|
| 1868 |
continue; |
| 1869 |
} |
| 1870 |
|
| 1871 |
/* |
| 1872 |
* Alert any threads that are waiting on a ready file descriptor. |
| 1873 |
*/ |
| 1874 |
|
| 1875 |
LOCK_NOTIFIER; |
| 1876 |
for (tsdPtr = waitingListPtr; tsdPtr; tsdPtr = tsdPtr->nextPtr) { |
| 1877 |
int found = 0; |
| 1878 |
SelectMasks readyMasks, checkMasks; |
| 1879 |
|
| 1880 |
LOCK_NOTIFIER_TSD; |
| 1881 |
FD_COPY(&tsdPtr->checkMasks.readable, &checkMasks.readable); |
| 1882 |
FD_COPY(&tsdPtr->checkMasks.writable, &checkMasks.writable); |
| 1883 |
FD_COPY(&tsdPtr->checkMasks.exceptional, &checkMasks.exceptional); |
| 1884 |
UNLOCK_NOTIFIER_TSD; |
| 1885 |
found = tsdPtr->polled; |
| 1886 |
FD_ZERO(&readyMasks.readable); |
| 1887 |
FD_ZERO(&readyMasks.writable); |
| 1888 |
FD_ZERO(&readyMasks.exceptional); |
| 1889 |
|
| 1890 |
for (i = tsdPtr->numFdBits-1; i >= 0; --i) { |
| 1891 |
if (FD_ISSET(i, &checkMasks.readable) |
| 1892 |
&& FD_ISSET(i, &readableMask)) { |
| 1893 |
FD_SET(i, &readyMasks.readable); |
| 1894 |
found = 1; |
| 1895 |
} |
| 1896 |
if (FD_ISSET(i, &checkMasks.writable) |
| 1897 |
&& FD_ISSET(i, &writableMask)) { |
| 1898 |
FD_SET(i, &readyMasks.writable); |
| 1899 |
found = 1; |
| 1900 |
} |
| 1901 |
if (FD_ISSET(i, &checkMasks.exceptional) |
| 1902 |
&& FD_ISSET(i, &exceptionalMask)) { |
| 1903 |
FD_SET(i, &readyMasks.exceptional); |
| 1904 |
found = 1; |
| 1905 |
} |
| 1906 |
} |
| 1907 |
|
| 1908 |
if (found) { |
| 1909 |
/* |
| 1910 |
* Remove the ThreadSpecificData structure of this thread from |
| 1911 |
* the waiting list. This prevents us from spinning |
| 1912 |
* continuously on select until the other threads runs and |
| 1913 |
* services the file event. |
| 1914 |
*/ |
| 1915 |
|
| 1916 |
OnOffWaitingList(tsdPtr, 0, 0); |
| 1917 |
|
| 1918 |
LOCK_NOTIFIER_TSD; |
| 1919 |
FD_COPY(&readyMasks.readable, &tsdPtr->readyMasks.readable); |
| 1920 |
FD_COPY(&readyMasks.writable, &tsdPtr->readyMasks.writable); |
| 1921 |
FD_COPY(&readyMasks.exceptional, |
| 1922 |
&tsdPtr->readyMasks.exceptional); |
| 1923 |
UNLOCK_NOTIFIER_TSD; |
| 1924 |
tsdPtr->polled = 0; |
| 1925 |
if (tsdPtr->runLoop) { |
| 1926 |
CFRunLoopSourceSignal(tsdPtr->runLoopSource); |
| 1927 |
CFRunLoopWakeUp(tsdPtr->runLoop); |
| 1928 |
} |
| 1929 |
} |
| 1930 |
} |
| 1931 |
UNLOCK_NOTIFIER; |
| 1932 |
|
| 1933 |
/* |
| 1934 |
* Consume the next byte from the notifier pipe if the pipe was |
| 1935 |
* readable. Note that there may be multiple bytes pending, but to |
| 1936 |
* avoid a race condition we only read one at a time. |
| 1937 |
*/ |
| 1938 |
|
| 1939 |
if (FD_ISSET(receivePipe, &readableMask)) { |
| 1940 |
i = read(receivePipe, buf, 1); |
| 1941 |
|
| 1942 |
if ((i == 0) || ((i == 1) && (buf[0] == 'q'))) { |
| 1943 |
/* |
| 1944 |
* Someone closed the write end of the pipe or sent us a Quit |
| 1945 |
* message [Bug: 4139] and then closed the write end of the |
| 1946 |
* pipe so we need to shut down the notifier thread. |
| 1947 |
*/ |
| 1948 |
|
| 1949 |
break; |
| 1950 |
} |
| 1951 |
} |
| 1952 |
} |
| 1953 |
pthread_exit(0); |
| 1954 |
} |
| 1955 |
|
| 1956 |
#ifdef HAVE_PTHREAD_ATFORK |
| 1957 |
/* |
| 1958 |
*---------------------------------------------------------------------- |
| 1959 |
* |
| 1960 |
* AtForkPrepare -- |
| 1961 |
* |
| 1962 |
* Lock the notifier in preparation for a fork. |
| 1963 |
* |
| 1964 |
* Results: |
| 1965 |
* None. |
| 1966 |
* |
| 1967 |
* Side effects: |
| 1968 |
* None. |
| 1969 |
* |
| 1970 |
*---------------------------------------------------------------------- |
| 1971 |
*/ |
| 1972 |
|
| 1973 |
static void |
| 1974 |
AtForkPrepare(void) |
| 1975 |
{ |
| 1976 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 1977 |
|
| 1978 |
LOCK_NOTIFIER_INIT; |
| 1979 |
LOCK_NOTIFIER; |
| 1980 |
LOCK_NOTIFIER_TSD; |
| 1981 |
} |
| 1982 |
|
| 1983 |
/* |
| 1984 |
*---------------------------------------------------------------------- |
| 1985 |
* |
| 1986 |
* AtForkParent -- |
| 1987 |
* |
| 1988 |
* Unlock the notifier in the parent after a fork. |
| 1989 |
* |
| 1990 |
* Results: |
| 1991 |
* None. |
| 1992 |
* |
| 1993 |
* Side effects: |
| 1994 |
* None. |
| 1995 |
* |
| 1996 |
*---------------------------------------------------------------------- |
| 1997 |
*/ |
| 1998 |
|
| 1999 |
static void |
| 2000 |
AtForkParent(void) |
| 2001 |
{ |
| 2002 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 2003 |
|
| 2004 |
UNLOCK_NOTIFIER_TSD; |
| 2005 |
UNLOCK_NOTIFIER; |
| 2006 |
UNLOCK_NOTIFIER_INIT; |
| 2007 |
} |
| 2008 |
|
| 2009 |
/* |
| 2010 |
*---------------------------------------------------------------------- |
| 2011 |
* |
| 2012 |
* AtForkChild -- |
| 2013 |
* |
| 2014 |
* Unlock and reinstall the notifier in the child after a fork. |
| 2015 |
* |
| 2016 |
* Results: |
| 2017 |
* None. |
| 2018 |
* |
| 2019 |
* Side effects: |
| 2020 |
* None. |
| 2021 |
* |
| 2022 |
*---------------------------------------------------------------------- |
| 2023 |
*/ |
| 2024 |
|
| 2025 |
static void |
| 2026 |
AtForkChild(void) |
| 2027 |
{ |
| 2028 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 2029 |
|
| 2030 |
/* |
| 2031 |
* If a child process unlocks an os_unfair_lock that was created in its parent |
| 2032 |
* the child will exit with an illegal instruction error. So we reinitialize |
| 2033 |
* the lock in the child rather than attempt to unlock it. |
| 2034 |
*/ |
| 2035 |
|
| 2036 |
#if defined(USE_OS_UNFAIR_LOCK) |
| 2037 |
tsdPtr->tsdLock = OS_UNFAIR_LOCK_INIT; |
| 2038 |
#else |
| 2039 |
UNLOCK_NOTIFIER_TSD; |
| 2040 |
UNLOCK_NOTIFIER; |
| 2041 |
UNLOCK_NOTIFIER_INIT; |
| 2042 |
#endif |
| 2043 |
if (tsdPtr->runLoop) { |
| 2044 |
tsdPtr->runLoop = NULL; |
| 2045 |
if (!noCFafterFork) { |
| 2046 |
CFRunLoopSourceInvalidate(tsdPtr->runLoopSource); |
| 2047 |
CFRelease(tsdPtr->runLoopSource); |
| 2048 |
if (tsdPtr->runLoopTimer) { |
| 2049 |
CFRunLoopTimerInvalidate(tsdPtr->runLoopTimer); |
| 2050 |
CFRelease(tsdPtr->runLoopTimer); |
| 2051 |
} |
| 2052 |
} |
| 2053 |
tsdPtr->runLoopSource = NULL; |
| 2054 |
tsdPtr->runLoopTimer = NULL; |
| 2055 |
} |
| 2056 |
if (notifierCount > 0) { |
| 2057 |
notifierCount = 1; |
| 2058 |
notifierThreadRunning = 0; |
| 2059 |
|
| 2060 |
/* |
| 2061 |
* Assume that the return value of Tcl_InitNotifier in the child will |
| 2062 |
* be identical to the one stored as clientData in tclNotify.c's |
| 2063 |
* ThreadSpecificData by the parent's TclInitNotifier, so discard the |
| 2064 |
* return value here. This assumption may require the fork() to be |
| 2065 |
* executed in the main thread of the parent, otherwise |
| 2066 |
* Tcl_AlertNotifier may break in the child. |
| 2067 |
*/ |
| 2068 |
|
| 2069 |
if (!noCFafterFork) { |
| 2070 |
Tcl_InitNotifier(); |
| 2071 |
} |
| 2072 |
} |
| 2073 |
} |
| 2074 |
#endif /* HAVE_PTHREAD_ATFORK */ |
| 2075 |
|
| 2076 |
#else /* HAVE_COREFOUNDATION */ |
| 2077 |
|
| 2078 |
void |
| 2079 |
TclMacOSXNotifierAddRunLoopMode( |
| 2080 |
const void *runLoopMode) |
| 2081 |
{ |
| 2082 |
Tcl_Panic("TclMacOSXNotifierAddRunLoopMode: " |
| 2083 |
"Tcl not built with CoreFoundation support"); |
| 2084 |
} |
| 2085 |
|
| 2086 |
#endif /* HAVE_COREFOUNDATION */ |
| 2087 |
|
| 2088 |
/* |
| 2089 |
* Local Variables: |
| 2090 |
* mode: c |
| 2091 |
* c-basic-offset: 4 |
| 2092 |
* fill-column: 78 |
| 2093 |
* End: |
| 2094 |
*/ |