Tcl: Extension and Threads Interaction
Can multiple instances of Tcl_PackageInitProc () run concurrently (on different threads) if TCL has been configured for threads?
For backward compatibility reasons, I believe calls to initialization and unloading procedures should be serialized.
The manual does not talk about behavior: are the calls to these routines serialized or are the required extension developers dealing with synchronization, in a specific mutual exclusion, in these routines?
+3
source to share
1 answer
Tcl does not guarantee that these functions are called in a serialized manner; if your code cares, it should use a suitable mutex. Tcl provides portable primitives in its C library that you use like this:
#include <tcl.h>
// MUCH easier to have this as its own function
static void OneTimeSetup(void) {
static int doneSetup;
TCL_DECLARE_MUTEX(myMutex);
Tcl_MutexLock(&myMutex);
if (!doneSetup) {
// Do critical once-only setup here
doneSetup = 1;
}
Tcl_MutexUnlock(&myMutex);
}
int My_Init(Tcl_Interp *interp) {
// Declare the API version we're using, e.g., for 8.5...
if (Tcl_InitStubs(interp, "8.5", 0) == NULL)
return TCL_ERROR;
// Call out to our setup code
OneTimeSetup();
// Install Tcl commands/variables/... here
// Ready for action!
return TCL_OK;
}
+1
source to share