From 5f83d31eceac5d0a48f75293f02725a59168beaf Mon Sep 17 00:00:00 2001 From: "jan.nijtmans" Date: Tue, 17 Aug 2021 16:16:29 +0000 Subject: tip#511 proposed implementation --- doc/Async.3 | 35 +++++--- generic/tcl.decls | 5 ++ generic/tclAsync.c | 208 ++++++++++++++++++++++++++++++++++------------- generic/tclDecls.h | 6 ++ generic/tclInt.h | 4 + generic/tclStubInit.c | 1 + macosx/tclMacOSXNotify.c | 149 ++++++++++++++++++++++++++++++++- unix/configure | 35 ++++++++ unix/configure.ac | 7 ++ unix/tclConfig.h.in | 3 + unix/tclEpollNotfy.c | 59 ++++++++++++++ unix/tclKqueueNotfy.c | 71 ++++++++++++++-- unix/tclSelectNotfy.c | 137 +++++++++++++++++++++++++++++-- unix/tclUnixNotfy.c | 76 +++++++++++++---- win/tclWinNotify.c | 49 +++++++++++ 15 files changed, 746 insertions(+), 99 deletions(-) diff --git a/doc/Async.3 b/doc/Async.3 index 347ba3d..e6ec5f8 100644 --- a/doc/Async.3 +++ b/doc/Async.3 @@ -9,7 +9,7 @@ .so man.macros .BS .SH NAME -Tcl_AsyncCreate, Tcl_AsyncMark, Tcl_AsyncInvoke, Tcl_AsyncDelete, Tcl_AsyncReady \- handle asynchronous events +Tcl_AsyncCreate, Tcl_AsyncMark, Tcl_AsyncMarkFromSignal, Tcl_AsyncInvoke, Tcl_AsyncDelete, Tcl_AsyncReady \- handle asynchronous events .SH SYNOPSIS .nf \fB#include \fR @@ -17,11 +17,16 @@ Tcl_AsyncCreate, Tcl_AsyncMark, Tcl_AsyncInvoke, Tcl_AsyncDelete, Tcl_AsyncReady Tcl_AsyncHandler \fBTcl_AsyncCreate\fR(\fIproc, clientData\fR) .sp +void \fBTcl_AsyncMark\fR(\fIasync\fR) .sp int +\fBTcl_AsyncMarkFromSignal\fR(\fIasync\fR, \fIsigNumber\fR) +.sp +int \fBTcl_AsyncInvoke\fR(\fIinterp, code\fR) .sp +void \fBTcl_AsyncDelete\fR(\fIasync\fR) .sp int @@ -34,6 +39,8 @@ Procedure to invoke to handle an asynchronous event. One-word value to pass to \fIproc\fR. .AP Tcl_AsyncHandler async in Token for asynchronous event handler. +.AP int sigNumber in +POSIX signal number, when used in a signal context. .AP Tcl_Interp *interp in Tcl interpreter in which command was being evaluated when handler was invoked, or NULL if handler was invoked when there was no interpreter @@ -60,10 +67,11 @@ to a clean state, such as after the current Tcl command completes. .PP \fBTcl_AsyncCreate\fR, \fBTcl_AsyncDelete\fR, and \fBTcl_AsyncReady\fR are thread sensitive. They access and/or set a thread-specific data -structure in the event of a core built with \fI\-\-enable\-threads\fR. The token -created by \fBTcl_AsyncCreate\fR contains the needed thread information it -was called from so that calling \fBTcl_AsyncMark\fR(\fItoken\fR) will only yield -the origin thread into the asynchronous handler. +structure in the event of a core built with \fI\-\-enable\-threads\fR. +The token created by \fBTcl_AsyncCreate\fR contains the needed thread +information it was called from so that calling \fBTcl_AsyncMarkFromSignal\fR +or \fBTcl_AsyncMark\fR with this token will only yield the origin +thread into the asynchronous handler. .PP \fBTcl_AsyncCreate\fR creates an asynchronous handler and returns a token for it. @@ -72,13 +80,16 @@ any occurrences of the asynchronous event that it is intended to handle (it is not safe to create a handler at the time of an event). When an asynchronous event occurs the code that detects the event -(such as a signal handler) should call \fBTcl_AsyncMark\fR with the -token for the handler. -\fBTcl_AsyncMark\fR will mark the handler as ready to execute, but it -will not invoke the handler immediately. -Tcl will call the \fIproc\fR associated with the handler later, when -the world is in a safe state, and \fIproc\fR can then carry out -the actions associated with the asynchronous event. +(such as a POSIX signal handler) should call \fBTcl_AsyncMarkFromSignal\fR +with the token for the handler and the POSIX signal number. The +return value of this function is true, when the handler will be +marked, false otherwise. +For non-signal contexts, \fBTcl_AsyncMark\fR serves the same purpose. +\fBTcl_AsyncMarkFromSignal\fR and \fBTcl_AsyncMark\fR will mark +the handler as ready to execute, but will not invoke the handler +immediately. Tcl will call the \fIproc\fR associated with the +handler later, when the world is in a safe state, and \fIproc\fR +can then carry out the actions associated with the asynchronous event. \fIProc\fR should have arguments and result that match the type \fBTcl_AsyncProc\fR: .PP diff --git a/generic/tcl.decls b/generic/tcl.decls index 3dec972..cec3354 100644 --- a/generic/tcl.decls +++ b/generic/tcl.decls @@ -2427,6 +2427,11 @@ declare 657 { int Tcl_UniCharIsUnicode(int ch) } +# TIP #511 +declare 658 { + int Tcl_AsyncMarkFromSignal(Tcl_AsyncHandler async, int sigNumber) +} + # ----- BASELINE -- FOR -- 8.7.0 ----- # ############################################################################## diff --git a/generic/tclAsync.c b/generic/tclAsync.c index 3a09304..bce984d 100644 --- a/generic/tclAsync.c +++ b/generic/tclAsync.c @@ -25,9 +25,9 @@ typedef struct AsyncHandler { int ready; /* Non-zero means this handler should be * invoked in the next call to * Tcl_AsyncInvoke. */ - struct AsyncHandler *nextPtr; - /* Next in list of all handlers for the - * process. */ + struct AsyncHandler *nextPtr, *prevPtr; + /* Next, previous in list of all handlers + * for the process. */ Tcl_AsyncProc *proc; /* Procedure to call when handler is * invoked. */ ClientData clientData; /* Value to pass to handler when it is @@ -38,16 +38,10 @@ typedef struct AsyncHandler { * associated to. */ Tcl_ThreadId originThrdId; /* Origin thread where this token was created * and where it will be yielded. */ + ClientData notifierData; /* Platform notifier data or NULL. */ } AsyncHandler; typedef struct ThreadSpecificData { - /* - * The variables below maintain a list of all existing handlers specific - * to the calling thread. - */ - AsyncHandler *firstHandler; /* First handler defined for process, or NULL - * if none. */ - AsyncHandler *lastHandler; /* Last handler or NULL. */ int asyncReady; /* This is set to 1 whenever a handler becomes * ready and it is cleared to zero whenever * Tcl_AsyncInvoke is called. It can be @@ -58,24 +52,29 @@ typedef struct ThreadSpecificData { * currently working. If so then we won't set * asyncReady again until Tcl_AsyncInvoke * returns. */ - Tcl_Mutex asyncMutex; /* Thread-specific AsyncHandler linked-list - * lock */ } ThreadSpecificData; static Tcl_ThreadDataKey dataKey; + +/* Mutex to protect linked-list of AsyncHandlers in the process. */ +TCL_DECLARE_MUTEX(asyncMutex) + +/* List of all existing handlers of the process. */ +static AsyncHandler *firstHandler = NULL; +static AsyncHandler *lastHandler = NULL; /* *---------------------------------------------------------------------- * * TclFinalizeAsync -- * - * Finalizes the mutex in the thread local data structure for the async + * Finalizes the thread local data structure for the async * subsystem. * * Results: * None. * * Side effects: - * Forgets knowledge of the mutex should it have been created. + * Cleans up left-over async handlers for the calling thread. * *---------------------------------------------------------------------- */ @@ -83,10 +82,40 @@ static Tcl_ThreadDataKey dataKey; void TclFinalizeAsync(void) { - ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); + AsyncHandler *token, *toDelete = NULL; + Tcl_ThreadId self = Tcl_GetCurrentThread(); + + Tcl_MutexLock(&asyncMutex); + for (token = firstHandler; token != NULL;) { + AsyncHandler *nextToken = token->nextPtr; - if (tsdPtr->asyncMutex != NULL) { - Tcl_MutexFinalize(&tsdPtr->asyncMutex); + if (token->originThrdId == self) { + if (token->prevPtr == NULL) { + firstHandler = token->nextPtr; + if (firstHandler == NULL) { + lastHandler = NULL; + break; + } + } else { + token->prevPtr->nextPtr = token->nextPtr; + if (token == lastHandler) { + lastHandler = token->prevPtr; + } + } + if (token->nextPtr != NULL) { + token->nextPtr->prevPtr = token->prevPtr; + } + token->nextPtr = toDelete; + token->prevPtr = NULL; + toDelete = token; + } + token = nextToken; + } + Tcl_MutexUnlock(&asyncMutex); + while (toDelete != NULL) { + token = toDelete; + toDelete = toDelete->nextPtr; + ckfree(token); } } @@ -121,19 +150,22 @@ Tcl_AsyncCreate( asyncPtr = (AsyncHandler*)ckalloc(sizeof(AsyncHandler)); asyncPtr->ready = 0; asyncPtr->nextPtr = NULL; + asyncPtr->prevPtr = NULL; asyncPtr->proc = proc; asyncPtr->clientData = clientData; asyncPtr->originTsd = tsdPtr; asyncPtr->originThrdId = Tcl_GetCurrentThread(); + asyncPtr->notifierData = TclpNotifierData(); - Tcl_MutexLock(&tsdPtr->asyncMutex); - if (tsdPtr->firstHandler == NULL) { - tsdPtr->firstHandler = asyncPtr; + Tcl_MutexLock(&asyncMutex); + if (firstHandler == NULL) { + firstHandler = asyncPtr; } else { - tsdPtr->lastHandler->nextPtr = asyncPtr; + asyncPtr->prevPtr = lastHandler; + lastHandler->nextPtr = asyncPtr; } - tsdPtr->lastHandler = asyncPtr; - Tcl_MutexUnlock(&tsdPtr->asyncMutex); + lastHandler = asyncPtr; + Tcl_MutexUnlock(&asyncMutex); return (Tcl_AsyncHandler) asyncPtr; } @@ -162,13 +194,84 @@ Tcl_AsyncMark( { AsyncHandler *token = (AsyncHandler *) async; - Tcl_MutexLock(&token->originTsd->asyncMutex); + Tcl_MutexLock(&asyncMutex); token->ready = 1; if (!token->originTsd->asyncActive) { token->originTsd->asyncReady = 1; Tcl_ThreadAlert(token->originThrdId); } - Tcl_MutexUnlock(&token->originTsd->asyncMutex); + Tcl_MutexUnlock(&asyncMutex); + +} + +/* + *---------------------------------------------------------------------- + * + * Tcl_AsyncMarkFromSignal -- + * + * This procedure is similar to Tcl_AsyncMark but must be used + * in POSIX signal contexts. In addition to Tcl_AsyncMark the + * signal number is passed. + * + * Results: + * True, when the handler will be marked, false otherwise. + * + * Side effects: + * The handler gets marked for invocation later. + * + *---------------------------------------------------------------------- + */ + +int +Tcl_AsyncMarkFromSignal( + Tcl_AsyncHandler async, /* Token for handler. */ + int sigNumber) /* Signal number. */ +{ +#ifdef TCL_THREADS + AsyncHandler *token = (AsyncHandler *) async; + + return TclAsyncNotifier(sigNumber, token->originThrdId, + token->notifierData, &token->ready, -1); +#else + Tcl_AsyncMark(async); + return 1; +#endif +} + +/* + *---------------------------------------------------------------------- + * + * TclAsyncMarkFromNotifier -- + * + * This procedure is called from the notifier thread and + * invokes Tcl_AsyncMark for specifically marked handlers. + * + * Results: + * None. + * + * Side effects: + * Handlers get marked for invocation later. + * + *---------------------------------------------------------------------- + */ + +void +TclAsyncMarkFromNotifier(void) +{ + AsyncHandler *token; + + Tcl_MutexLock(&asyncMutex); + for (token = firstHandler; token != NULL; + token = token->nextPtr) { + if (token->ready == -1) { + token->ready = 1; + if (!token->originTsd->asyncActive) { + token->originTsd->asyncReady = 1; + Tcl_ThreadAlert(token->originThrdId); + } + } + } + Tcl_MutexUnlock(&asyncMutex); } /* @@ -200,11 +303,12 @@ Tcl_AsyncInvoke( { AsyncHandler *asyncPtr; ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); + Tcl_ThreadId self = Tcl_GetCurrentThread(); - Tcl_MutexLock(&tsdPtr->asyncMutex); + Tcl_MutexLock(&asyncMutex); if (tsdPtr->asyncReady == 0) { - Tcl_MutexUnlock(&tsdPtr->asyncMutex); + Tcl_MutexUnlock(&asyncMutex); return code; } tsdPtr->asyncReady = 0; @@ -224,8 +328,11 @@ Tcl_AsyncInvoke( */ while (1) { - for (asyncPtr = tsdPtr->firstHandler; asyncPtr != NULL; + for (asyncPtr = firstHandler; asyncPtr != NULL; asyncPtr = asyncPtr->nextPtr) { + if (asyncPtr->originThrdId != self) { + continue; + } if (asyncPtr->ready) { break; } @@ -234,12 +341,12 @@ Tcl_AsyncInvoke( break; } asyncPtr->ready = 0; - Tcl_MutexUnlock(&tsdPtr->asyncMutex); + Tcl_MutexUnlock(&asyncMutex); code = asyncPtr->proc(asyncPtr->clientData, interp, code); - Tcl_MutexLock(&tsdPtr->asyncMutex); + Tcl_MutexLock(&asyncMutex); } tsdPtr->asyncActive = 0; - Tcl_MutexUnlock(&tsdPtr->asyncMutex); + Tcl_MutexUnlock(&asyncMutex); return code; } @@ -271,9 +378,7 @@ void Tcl_AsyncDelete( Tcl_AsyncHandler async) /* Token for handler to delete. */ { - ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); AsyncHandler *asyncPtr = (AsyncHandler *) async; - AsyncHandler *prevPtr, *thisPtr; /* * Assure early handling of the constraint @@ -283,33 +388,22 @@ Tcl_AsyncDelete( Tcl_Panic("Tcl_AsyncDelete: async handler deleted by the wrong thread"); } - /* - * If we come to this point when TSD's for the current - * thread have already been garbage-collected, we are - * in the _serious_ trouble. OTOH, we tolerate calling - * with already cleaned-up handler list (should we?). - */ - - Tcl_MutexLock(&tsdPtr->asyncMutex); - if (tsdPtr->firstHandler != NULL) { - prevPtr = thisPtr = tsdPtr->firstHandler; - while (thisPtr != NULL && thisPtr != asyncPtr) { - prevPtr = thisPtr; - thisPtr = thisPtr->nextPtr; - } - if (thisPtr == NULL) { - Tcl_Panic("Tcl_AsyncDelete: cannot find async handler"); + Tcl_MutexLock(&asyncMutex); + if (asyncPtr->prevPtr == NULL) { + firstHandler = asyncPtr->nextPtr; + if (firstHandler == NULL) { + lastHandler = NULL; } - if (asyncPtr == tsdPtr->firstHandler) { - tsdPtr->firstHandler = asyncPtr->nextPtr; - } else { - prevPtr->nextPtr = asyncPtr->nextPtr; - } - if (asyncPtr == tsdPtr->lastHandler) { - tsdPtr->lastHandler = prevPtr; + } else { + asyncPtr->prevPtr->nextPtr = asyncPtr->nextPtr; + if (asyncPtr == lastHandler) { + lastHandler = asyncPtr->prevPtr; } } - Tcl_MutexUnlock(&tsdPtr->asyncMutex); + if (asyncPtr->nextPtr != NULL) { + asyncPtr->nextPtr->prevPtr = asyncPtr->prevPtr; + } + Tcl_MutexUnlock(&asyncMutex); ckfree(asyncPtr); } diff --git a/generic/tclDecls.h b/generic/tclDecls.h index 713f3e9..637ed06 100644 --- a/generic/tclDecls.h +++ b/generic/tclDecls.h @@ -1939,6 +1939,9 @@ EXTERN const char * Tcl_UtfNext(const char *src); EXTERN const char * Tcl_UtfPrev(const char *src, const char *start); /* 657 */ EXTERN int Tcl_UniCharIsUnicode(int ch); +/* 658 */ +EXTERN int Tcl_AsyncMarkFromSignal(Tcl_AsyncHandler async, + int sigNumber); typedef struct { const struct TclPlatStubs *tclPlatStubs; @@ -2632,6 +2635,7 @@ typedef struct TclStubs { const char * (*tcl_UtfNext) (const char *src); /* 655 */ const char * (*tcl_UtfPrev) (const char *src, const char *start); /* 656 */ int (*tcl_UniCharIsUnicode) (int ch); /* 657 */ + int (*tcl_AsyncMarkFromSignal) (Tcl_AsyncHandler async, int sigNumber); /* 658 */ } TclStubs; extern const TclStubs *tclStubsPtr; @@ -3976,6 +3980,8 @@ extern const TclStubs *tclStubsPtr; (tclStubsPtr->tcl_UtfPrev) /* 656 */ #define Tcl_UniCharIsUnicode \ (tclStubsPtr->tcl_UniCharIsUnicode) /* 657 */ +#define Tcl_AsyncMarkFromSignal \ + (tclStubsPtr->tcl_AsyncMarkFromSignal) /* 658 */ #endif /* defined(USE_TCL_STUBS) */ diff --git a/generic/tclInt.h b/generic/tclInt.h index 07a5fb6..b561cbd 100644 --- a/generic/tclInt.h +++ b/generic/tclInt.h @@ -2926,6 +2926,9 @@ MODULE_SCOPE void TclArgumentBCRelease(Tcl_Interp *interp, CmdFrame *cfPtr); MODULE_SCOPE void TclArgumentGet(Tcl_Interp *interp, Tcl_Obj *obj, CmdFrame **cfPtrPtr, int *wordPtr); +MODULE_SCOPE int TclAsyncNotifier(int sigNumber, Tcl_ThreadId threadId, + ClientData clientData, int *flagPtr, int value); +MODULE_SCOPE void TclAsyncMarkFromNotifier(void); MODULE_SCOPE double TclBignumToDouble(const void *bignum); MODULE_SCOPE int TclByteArrayMatch(const unsigned char *string, int strLen, const unsigned char *pattern, @@ -3141,6 +3144,7 @@ MODULE_SCOPE Tcl_Obj * TclpTempFileNameForLibrary(Tcl_Interp *interp, MODULE_SCOPE Tcl_Obj * TclNewFSPathObj(Tcl_Obj *dirPtr, const char *addStrRep, int len); MODULE_SCOPE void TclpAlertNotifier(ClientData clientData); +MODULE_SCOPE ClientData TclpNotifierData(void); MODULE_SCOPE void TclpServiceModeHook(int mode); MODULE_SCOPE void TclpSetTimer(const Tcl_Time *timePtr); MODULE_SCOPE int TclpWaitForEvent(const Tcl_Time *timePtr); diff --git a/generic/tclStubInit.c b/generic/tclStubInit.c index 5028916..64e63ea 100644 --- a/generic/tclStubInit.c +++ b/generic/tclStubInit.c @@ -1942,6 +1942,7 @@ const TclStubs tclStubs = { Tcl_UtfNext, /* 655 */ Tcl_UtfPrev, /* 656 */ Tcl_UniCharIsUnicode, /* 657 */ + Tcl_AsyncMarkFromSignal, /* 658 */ }; /* !END!: Do not edit above this line. */ diff --git a/macosx/tclMacOSXNotify.c b/macosx/tclMacOSXNotify.c index c870a36..e80d6f9 100644 --- a/macosx/tclMacOSXNotify.c +++ b/macosx/tclMacOSXNotify.c @@ -458,6 +458,20 @@ static int receivePipe = -1; /* Output end of triggerPipe */ static int notifierThreadRunning; /* + * The following static flag indicates that async handlers are pending. + */ + +#ifdef TCL_THREADS +static int asyncPending = 0; +#endif + +/* + * Signal mask information for notifier thread. + */ +static sigset_t notifierSigMask; +static sigset_t allSigMask; + +/* * This is the thread ID of the notifier thread that does select. Only valid * when notifierThreadRunning is non-zero. * @@ -803,6 +817,15 @@ StartNotifierThread(void) int result; pthread_attr_t attr; + /* + * Arrange for the notifier thread to start with all + * signals blocked. In its mainloop it unblocks the + * signals at safe points. + */ + + sigfillset(&allSigMask); + pthread_sigmask(SIG_BLOCK, &allSigMask, ¬ifierSigMask); + pthread_attr_init(&attr); pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); @@ -814,6 +837,12 @@ StartNotifierThread(void) Tcl_Panic("StartNotifierThread: unable to start notifier thread"); } notifierThreadRunning = 1; + + /* + * Restore original signal mask. + */ + + pthread_sigmask(SIG_SETMASK, ¬ifierSigMask, NULL); } UNLOCK_NOTIFIER_INIT; } @@ -876,6 +905,14 @@ TclpFinalizeNotifier( "thread"); } notifierThreadRunning = 0; + + /* + * If async marks are outstanding, perform actions now. + */ + if (asyncPending) { + asyncPending = 0; + TclAsyncMarkFromNotifier(); + } } close(receivePipe); @@ -1283,6 +1320,29 @@ FileHandlerEventProc( /* *---------------------------------------------------------------------- * + * TclpNotifierData -- + * + * This function returns a ClientData pointer to be associated + * with a Tcl_AsyncHandler. + * + * Results: + * On MacOSX, returns always NULL. + * + * Side effects: + * None. + * + *---------------------------------------------------------------------- + */ + +ClientData +TclpNotifierData(void) +{ + return NULL; +} + +/* + *---------------------------------------------------------------------- + * * TclpWaitForEvent -- * * This function is called by Tcl_DoOneEvent to wait for new events on @@ -1829,6 +1889,61 @@ TclUnixWaitForFile( /* *---------------------------------------------------------------------- * + * TclAsyncNotifier -- + * + * This procedure sets the async mark of an async handler to a + * given value, if it is called from the notifier thread. + * + * Result: + * True, when the handler will be marked, false otherwise. + * + * Side effetcs: + * The trigger pipe is written when called from the notifier + * thread. + * + *---------------------------------------------------------------------- + */ + +int +TclAsyncNotifier( + int sigNumber, /* Signal number. */ + Tcl_ThreadId threadId, /* Target thread. */ + TCL_UNUSED(ClientData), /* Notifier data. */ + int *flagPtr, /* Flag to mark. */ + int value) /* Value of mark. */ +{ +#ifdef TCL_THREADS + /* + * WARNING: + * This code most likely runs in a signal handler. Thus, + * only few async-signal-safe system calls are allowed, + * e.g. pthread_self(), sem_post(), write(). + */ + + if (pthread_equal(pthread_self(), (pthread_t) notifierThread)) { + if (notifierThreadRunning) { + *flagPtr = value; + if (!asyncPending) { + asyncPending = 1; + write(triggerPipe, "S", 1); + } + return 1; + } + return 0; + } + + /* + * Re-send the signal to the notifier thread. + */ + + pthread_kill((pthread_t) notifierThread, sigNumber); +#endif + return 0; +} + +/* + *---------------------------------------------------------------------- + * * NotifierThreadProc -- * * This routine is the initial (and only) function executed by the @@ -1856,7 +1971,7 @@ NotifierThreadProc( { ThreadSpecificData *tsdPtr; fd_set readableMask, writableMask, exceptionalMask; - int i, numFdBits = 0, polling; + int i, ret, numFdBits = 0, polling; struct timeval poll = {0., 0.}, *timePtr; char buf[2]; @@ -1909,8 +2024,25 @@ NotifierThreadProc( } FD_SET(receivePipe, &readableMask); - if (select(numFdBits, &readableMask, &writableMask, &exceptionalMask, - timePtr) == -1) { + /* + * Signals are unblocked only during select(). + */ + + pthread_sigmask(SIG_SETMASK, ¬ifierSigMask, NULL); + ret = select(numFdBits, &readableMask, &writableMask, &exceptionalMask, + timePtr); + pthread_sigmask(SIG_BLOCK, &allSigMask, NULL); + + if (ret == -1) { + /* + * In case a signal was caught during select(), + * perform work on async handlers now. + */ + if (errno == EINTR && asyncPending) { + asyncPending = 0; + TclAsyncMarkFromNotifier(); + } + /* * Try again immediately on an error. */ @@ -1998,6 +2130,11 @@ NotifierThreadProc( break; } + + if (asyncPending) { + asyncPending = 0; + TclAsyncMarkFromNotifier(); + } } } pthread_exit(0); @@ -2120,6 +2257,12 @@ AtForkChild(void) Tcl_InitNotifier(); } } + + /* + * Restart the notifier thread for signal handling. + */ + + StartNotifierThread(); } #endif /* HAVE_PTHREAD_ATFORK */ diff --git a/unix/configure b/unix/configure index 98d3a50..701377b 100755 --- a/unix/configure +++ b/unix/configure @@ -9205,6 +9205,41 @@ printf "%s\n" "#define NO_FD_SET 1" >>confdefs.h fi +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for pselect" >&5 +printf %s "checking for pselect... " >&6; } +if test ${tcl_cv_func_pselect+y} +then : + printf %s "(cached) " >&6 +else $as_nop + + cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ +#include +int +main (void) +{ +void *func = pselect; + ; + return 0; +} +_ACEOF +if ac_fn_c_try_compile "$LINENO" +then : + tcl_cv_func_pselect=yes +else $as_nop + tcl_cv_func_pselect=no +fi +rm -f core conftest.err conftest.$ac_objext conftest.beam conftest.$ac_ext +fi +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $tcl_cv_func_pselect" >&5 +printf "%s\n" "$tcl_cv_func_pselect" >&6; } +tcl_ok=$tcl_cv_func_pselect +if test $tcl_ok = yes; then + +printf "%s\n" "#define HAVE_PSELECT 1" >>confdefs.h + +fi + #------------------------------------------------------------------------ # Options for the notifier. Checks for epoll(7) on Linux, and # kqueue(2) on {DragonFly,Free,Net,Open}BSD diff --git a/unix/configure.ac b/unix/configure.ac index 485f13d..b824ede 100644 --- a/unix/configure.ac +++ b/unix/configure.ac @@ -318,6 +318,13 @@ if test $tcl_ok = no; then AC_DEFINE(NO_FD_SET, 1, [Do we have fd_set?]) fi +AC_CACHE_CHECK([for pselect], tcl_cv_func_pselect, [ + AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include ]], [[void *func = pselect;]])],[tcl_cv_func_pselect=yes],[tcl_cv_func_pselect=no])]) +tcl_ok=$tcl_cv_func_pselect +if test $tcl_ok = yes; then + AC_DEFINE(HAVE_PSELECT, 1, [Should we use pselect()?]) +fi + #------------------------------------------------------------------------ # Options for the notifier. Checks for epoll(7) on Linux, and # kqueue(2) on {DragonFly,Free,Net,Open}BSD diff --git a/unix/tclConfig.h.in b/unix/tclConfig.h.in index 0bf3c3d..d7f51bf 100644 --- a/unix/tclConfig.h.in +++ b/unix/tclConfig.h.in @@ -190,6 +190,9 @@ /* Define to 1 if you have the `pthread_attr_setstacksize' function. */ #undef HAVE_PTHREAD_ATTR_SETSTACKSIZE +/* Define to 1 if you have the `pselect' function */ +#undef HAVE_PSELECT + /* Does putenv() copy strings or incorporate them by reference? */ #undef HAVE_PUTENV_THAT_COPIES diff --git a/unix/tclEpollNotfy.c b/unix/tclEpollNotfy.c index 4be1439..ce7d6b1 100644 --- a/unix/tclEpollNotfy.c +++ b/unix/tclEpollNotfy.c @@ -111,6 +111,7 @@ typedef struct ThreadSpecificData { /* Pointer to at most maxReadyEvents events * returned by epoll_wait(2). */ size_t maxReadyEvents; /* Count of epoll_events in readyEvents. */ + int asyncPending; /* True when signal triggered thread. */ } ThreadSpecificData; static Tcl_ThreadDataKey dataKey; @@ -478,6 +479,10 @@ PlatformEventsWait( timePtr->tv_usec = 0; } } + if (tsdPtr->asyncPending) { + tsdPtr->asyncPending = 0; + TclAsyncMarkFromNotifier(); + } return numFound; } @@ -765,6 +770,60 @@ TclpWaitForEvent( return 0; } +/* + *---------------------------------------------------------------------- + * + * TclAsyncNotifier -- + * + * This procedure sets the async mark of an async handler to a + * given value, if it is called from the target thread. + * + * Result: + * True, when the handler will be marked, false otherwise. + * + * Side effects: + * The signal may be resent to the target thread. + * + *---------------------------------------------------------------------- + */ + +int +TclAsyncNotifier( + int sigNumber, /* Signal number. */ + Tcl_ThreadId threadId, /* Target thread. */ + ClientData clientData, /* Notifier data. */ + int *flagPtr, /* Flag to mark. */ + int value) /* Value of mark. */ +{ +#ifdef TCL_THREADS + /* + * WARNING: + * This code most likely runs in a signal handler. Thus, + * only few async-signal-safe system calls are allowed, + * e.g. pthread_self(), sem_post(), write(). + */ + + if (pthread_equal(pthread_self(), (pthread_t) threadId)) { + ThreadSpecificData *tsdPtr = (ThreadSpecificData *) clientData; + + *flagPtr = value; + if (tsdPtr != NULL && !tsdPtr->asyncPending) { + tsdPtr->asyncPending = 1; + TclpAlertNotifier(tsdPtr); + return 1; + } + return 0; + } + + /* + * Re-send the signal to the proper target thread. + */ + + pthread_kill((pthread_t) threadId, sigNumber); +#endif + return 0; +} + #endif /* NOTIFIER_EPOLL && TCL_THREADS */ #else TCL_MAC_EMPTY_FILE(unix_tclEpollNotfy_c) diff --git a/unix/tclKqueueNotfy.c b/unix/tclKqueueNotfy.c index c0b9f6f..872f71c 100644 --- a/unix/tclKqueueNotfy.c +++ b/unix/tclKqueueNotfy.c @@ -102,6 +102,7 @@ typedef struct ThreadSpecificData { struct kevent *readyEvents; /* Pointer to at most maxReadyEvents events * returned by kevent(2). */ size_t maxReadyEvents; /* Count of kevents in readyEvents. */ + int asyncPending; /* True when signal triggered thread. */ } ThreadSpecificData; static Tcl_ThreadDataKey dataKey; @@ -165,11 +166,11 @@ PlatformEventsControl( Tcl_StatBuf fdStat; if (isNew) { - newPedPtr = (struct PlatformEventData *) + newPedPtr = (struct PlatformEventData *) ckalloc(sizeof(struct PlatformEventData)); - newPedPtr->filePtr = filePtr; - newPedPtr->tsdPtr = tsdPtr; - filePtr->pedPtr = newPedPtr; + newPedPtr->filePtr = filePtr; + newPedPtr->tsdPtr = tsdPtr; + filePtr->pedPtr = newPedPtr; } /* @@ -213,7 +214,7 @@ PlatformEventsControl( EVFILT_WRITE, op, 0, 0, filePtr->pedPtr); numChanges++; } - if (numChanges) { + if (numChanges) { if (kevent(tsdPtr->eventsFd, changeList, numChanges, NULL, 0, NULL) == -1) { Tcl_Panic("kevent: %s", strerror(errno)); @@ -363,7 +364,7 @@ TclpInitNotifier(void) filePtr->mask = TCL_READABLE; PlatformEventsControl(filePtr, tsdPtr, EV_ADD, 1); if (!tsdPtr->readyEvents) { - tsdPtr->maxReadyEvents = 512; + tsdPtr->maxReadyEvents = 512; tsdPtr->readyEvents = (struct kevent *) ckalloc( tsdPtr->maxReadyEvents * sizeof(tsdPtr->readyEvents[0])); } @@ -483,6 +484,10 @@ PlatformEventsWait( timePtr->tv_usec = 0; } } + if (tsdPtr->asyncPending) { + tsdPtr->asyncPending = 0; + TclAsyncMarkFromNotifier(); + } return numFound; } @@ -761,6 +766,60 @@ TclpWaitForEvent( return 0; } +/* + *---------------------------------------------------------------------- + * + * TclAsyncNotifier -- + * + * This procedure sets the async mark of an async handler to a + * given value, if it is called from the target thread. + * + * Result: + * True, when the handler will be marked, false otherwise. + * + * Side effects: + * The signal may be resent to the target thread. + * + *---------------------------------------------------------------------- + */ + +int +TclAsyncNotifier( + int sigNumber, /* Signal number. */ + Tcl_ThreadId threadId, /* Target thread. */ + ClientData clientData, /* Notifier data. */ + int *flagPtr, /* Flag to mark. */ + int value) /* Value of mark. */ +{ +#ifdef TCL_THREADS + /* + * WARNING: + * This code most likely runs in a signal handler. Thus, + * only few async-signal-safe system calls are allowed, + * e.g. pthread_self(), sem_post(), write(). + */ + + if (pthread_equal(pthread_self(), (pthread_t) threadId)) { + ThreadSpecificData *tsdPtr = (ThreadSpecificData *) clientData; + + *flagPtr = value; + if (tsdPtr != NULL && !tsdPtr->asyncPending) { + tsdPtr->asyncPending = 1; + TclpAlertNotifier(tsdPtr); + return 1; + } + return 0; + } + + /* + * Re-send the signal to the proper target thread. + */ + + pthread_kill((pthread_t) threadId, sigNumber); +#endif + return 0; +} + #endif /* NOTIFIER_KQUEUE && TCL_THREADS */ #else TCL_MAC_EMPTY_FILE(unix_tclKqueueNotfy_c) diff --git a/unix/tclSelectNotfy.c b/unix/tclSelectNotfy.c index 82f2ef7..e69ffd8 100644 --- a/unix/tclSelectNotfy.c +++ b/unix/tclSelectNotfy.c @@ -148,6 +148,7 @@ static ThreadSpecificData *waitingListPtr = NULL; */ static int triggerPipe = -1; +static int otherPipe = -1; /* * The notifierMutex locks access to all of the global notifier state. @@ -164,9 +165,15 @@ static pthread_mutex_t notifierMutex = PTHREAD_MUTEX_INITIALIZER; static int notifierThreadRunning = 0; /* + * The following static flag indicates that async handlers are pending. + */ + +static int asyncPending = 0; + +/* * The notifier thread signals the notifierCV when it has finished * initializing the triggerPipe and right before the notifier thread - * terminates. + * terminates. This condition is used to deal with the signal mask, too. */ static pthread_cond_t notifierCV = PTHREAD_COND_INITIALIZER; @@ -190,6 +197,14 @@ static pthread_cond_t notifierCV = PTHREAD_COND_INITIALIZER; */ static Tcl_ThreadId notifierThread; + +/* + * Signal mask information for notifier thread. + */ + +static sigset_t notifierSigMask; +static sigset_t allSigMask; + #endif /* TCL_THREADS */ /* @@ -409,6 +424,14 @@ TclpFinalizeNotifier( "unable to join notifier thread"); } notifierThreadRunning = 0; + + /* + * If async marks are outstanding, perform actions now. + */ + if (asyncPending) { + asyncPending = 0; + TclAsyncMarkFromNotifier(); + } } } @@ -875,6 +898,61 @@ TclpWaitForEvent( /* *---------------------------------------------------------------------- * + * TclAsyncNotifier -- + * + * This procedure sets the async mark of an async handler to a + * given value, if it is called from the notifier thread. + * + * Result: + * True, when the handler will be marked, false otherwise. + * + * Side effetcs: + * The trigger pipe is written when called from the notifier + * thread. + * + *---------------------------------------------------------------------- + */ + +int +TclAsyncNotifier( + int sigNumber, /* Signal number. */ + Tcl_ThreadId threadId, /* Target thread. */ + TCL_UNUSED(ClientData), /* Notifier data. */ + int *flagPtr, /* Flag to mark. */ + int value) /* Value of mark. */ +{ +#ifdef TCL_THREADS + /* + * WARNING: + * This code most likely runs in a signal handler. Thus, + * only few async-signal-safe system calls are allowed, + * e.g. pthread_self(), sem_post(), write(). + */ + + if (pthread_equal(pthread_self(), (pthread_t) notifierThread)) { + if (notifierThreadRunning) { + *flagPtr = value; + if (!asyncPending) { + asyncPending = 1; + write(triggerPipe, "S", 1); + } + return 1; + } + return 0; + } + + /* + * Re-send the signal to the notifier thread. + */ + + pthread_kill((pthread_t) notifierThread, sigNumber); +#endif + return 0; +} + +/* + *---------------------------------------------------------------------- + * * NotifierThreadProc -- * * This routine is the initial (and only) function executed by the @@ -906,8 +984,7 @@ NotifierThreadProc( fd_set readableMask; fd_set writableMask; fd_set exceptionMask; - int i; - int fds[2], receivePipe; + int i, fds[2], receivePipe, ret; long found; struct timeval poll = {0, 0}, *timePtr; char buf[2]; @@ -917,6 +994,14 @@ NotifierThreadProc( Tcl_Panic("NotifierThreadProc: %s", "could not create trigger pipe"); } + /* + * Ticket [c6897e6e6a]. + */ + + if (fds[0] >= FD_SETSIZE || fds[1] >= FD_SETSIZE) { + Tcl_Panic("NotifierThreadProc: %s", "too many open files"); + } + receivePipe = fds[0]; if (TclUnixSetBlockingMode(receivePipe, TCL_MODE_NONBLOCKING) < 0) { @@ -942,6 +1027,7 @@ NotifierThreadProc( pthread_mutex_lock(¬ifierMutex); triggerPipe = fds[1]; + otherPipe = fds[0]; /* * Signal any threads that are waiting. @@ -1002,12 +1088,44 @@ NotifierThreadProc( } FD_SET(receivePipe, &readableMask); - if (select(numFdBits, &readableMask, &writableMask, &exceptionMask, - timePtr) == -1) { + /* + * Signals are unblocked only during select(). + */ + +#ifdef HAVE_PSELECT + { + struct timespec tspec, *tspecPtr; + + if (timePtr == NULL) { + tspecPtr = NULL; + } else { + tspecPtr = &tspec; + tspecPtr->tv_sec = timePtr->tv_sec; + tspecPtr->tv_nsec = timePtr->tv_usec * 1000; + } + ret = pselect(numFdBits, &readableMask, &writableMask, + &exceptionMask, tspecPtr, ¬ifierSigMask); + } +#else + pthread_sigmask(SIG_SETMASK, ¬ifierSigMask, NULL); + ret = select(numFdBits, &readableMask, &writableMask, &exceptionMask, + timePtr); + pthread_sigmask(SIG_BLOCK, &allSigMask, NULL); +#endif + + if (ret == -1) { /* - * Try again immediately on an error. + * In case a signal was caught during select(), + * perform work on async handlers now. */ + if (errno == EINTR && asyncPending) { + asyncPending = 0; + TclAsyncMarkFromNotifier(); + } + /* + * Try again immediately on select() error. + */ continue; } @@ -1063,6 +1181,12 @@ NotifierThreadProc( break; } } while (1); + + if (asyncPending) { + asyncPending = 0; + TclAsyncMarkFromNotifier(); + } + if ((i == 0) || (buf[0] == 'q')) { break; } @@ -1076,6 +1200,7 @@ NotifierThreadProc( close(receivePipe); pthread_mutex_lock(¬ifierMutex); triggerPipe = -1; + otherPipe = -1; pthread_cond_broadcast(¬ifierCV); pthread_mutex_unlock(¬ifierMutex); diff --git a/unix/tclUnixNotfy.c b/unix/tclUnixNotfy.c index c1f00d5..e7e70ef 100644 --- a/unix/tclUnixNotfy.c +++ b/unix/tclUnixNotfy.c @@ -350,24 +350,24 @@ AlertSingleThread( { tsdPtr->eventReady = 1; if (tsdPtr->onList) { - /* - * Remove the ThreadSpecificData structure of this thread from the - * waiting list. This prevents us from continuously spinning on - * epoll_wait until the other threads runs and services the file - * event. - */ - - if (tsdPtr->prevPtr) { + /* + * Remove the ThreadSpecificData structure of this thread from the + * waiting list. This prevents us from continuously spinning on + * epoll_wait until the other threads runs and services the file + * event. + */ + + if (tsdPtr->prevPtr) { tsdPtr->prevPtr->nextPtr = tsdPtr->nextPtr; - } else { + } else { waitingListPtr = tsdPtr->nextPtr; - } - if (tsdPtr->nextPtr) { + } + if (tsdPtr->nextPtr) { tsdPtr->nextPtr->prevPtr = tsdPtr->prevPtr; - } - tsdPtr->nextPtr = tsdPtr->prevPtr = NULL; - tsdPtr->onList = 0; - tsdPtr->pollState = 0; + } + tsdPtr->nextPtr = tsdPtr->prevPtr = NULL; + tsdPtr->onList = 0; + tsdPtr->pollState = 0; } #ifdef __CYGWIN__ PostMessageW(tsdPtr->hwnd, 1024, 0, 0); @@ -403,6 +403,10 @@ AtForkChild(void) pthread_mutex_init(¬ifierMutex, NULL); pthread_cond_init(¬ifierCV, NULL); +#ifdef NOTIFIER_SELECT + asyncPending = 0; +#endif + /* * notifierThreadRunning == 1: thread is running, (there might be data in * notifier lists) @@ -420,6 +424,10 @@ AtForkChild(void) close(triggerPipe); triggerPipe = -1; +#ifdef NOTIFIER_SELECT + close(otherPipe); + otherPipe = -1; +#endif /* * The waitingListPtr might contain event info from multiple * threads, which are invalid here, so setting it to NULL is not @@ -456,6 +464,14 @@ AtForkChild(void) } Tcl_InitNotifier(); + +#ifdef NOTIFIER_SELECT + /* + * Restart the notifier thread for signal handling. + */ + + StartNotifierThread("AtForkChild"); +#endif } #endif /* HAVE_PTHREAD_ATFORK */ #endif /* TCL_THREADS */ @@ -464,6 +480,36 @@ AtForkChild(void) /* *---------------------------------------------------------------------- * + * TclpNotifierData -- + * + * This function returns a ClientData pointer to be associated + * with a Tcl_AsyncHandler. + * + * Results: + * For the epoll and kqueue notifiers, this function returns the + * thread specific data. Otherwise NULL. + * + * Side effects: + * None. + * + *---------------------------------------------------------------------- + */ + +ClientData +TclpNotifierData(void) +{ +#if defined(NOTIFIER_EPOLL) || defined(NOTIFIER_KQUEUE) + ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); + + return (ClientData) tsdPtr; +#else + return NULL; +#endif +} + +/* + *---------------------------------------------------------------------- + * * TclUnixWaitForFile -- * * This function waits synchronously for a file to become readable or diff --git a/win/tclWinNotify.c b/win/tclWinNotify.c index 068675c..f951acf 100644 --- a/win/tclWinNotify.c +++ b/win/tclWinNotify.c @@ -353,6 +353,32 @@ TclpServiceModeHook( /* *---------------------------------------------------------------------- * + * TclAsyncNotifier -- + * + * This procedure is a no-op on Windows. + * + * Result: + * Always true. + * + * Side effetcs: + * None. + *---------------------------------------------------------------------- + */ + +int +TclAsyncNotifier( + int sigNumber, /* Signal number. */ + Tcl_ThreadId threadId, /* Target thread. */ + ClientData clientData, /* Notifier data. */ + int *flagPtr, /* Flag to mark. */ + int value) /* Value of mark. */ +{ + return 0; +} + +/* + *---------------------------------------------------------------------- + * * NotifierProc -- * * This procedure is invoked by Windows to process events on the notifier @@ -396,6 +422,29 @@ NotifierProc( /* *---------------------------------------------------------------------- * + * TclpNotifierData -- + * + * This function returns a ClientData pointer to be associated + * with a Tcl_AsyncHandler. + * + * Results: + * On Windows, returns always NULL. + * + * Side effects: + * None. + * + *---------------------------------------------------------------------- + */ + +ClientData +TclpNotifierData(void) +{ + return NULL; +} + +/* + *---------------------------------------------------------------------- + * * TclpWaitForEvent -- * * This function is called by Tcl_DoOneEvent to wait for new events on -- cgit v0.12