void Ns_MutexSetName2(Ns_Mutex *mutex, char *prefix, char *name) { Mutex *mutexPtr = GETMUTEX(mutex); int plen, nlen; char *p; plen = strlen(prefix); if (plen > NS_THREAD_NAMESIZE) { plen = NS_THREAD_NAMESIZE; nlen = 0; } else { nlen = name ? strlen(name) : 0; if ((nlen + plen + 1) > NS_THREAD_NAMESIZE) { nlen = NS_THREAD_NAMESIZE - plen - 1; } } Ns_MasterLock(); p = strncpy(mutexPtr->name, prefix, (size_t)plen) + plen; if (nlen > 0) { *p++ = ':'; p = strncpy(p, name, (size_t)nlen) + nlen; } *p = '\0'; Ns_MasterUnlock(); }
void * Pthread(void *arg) { static Ns_Tls tls; /* * Allocate TLS first time (this is recommended TLS * self-initialization style. */ Ns_ThreadSetName("pthread"); sleep(5); if (tls == NULL) { Ns_MasterLock(); if (tls == NULL) { Ns_TlsAlloc(&tls, PthreadTlsCleanup); } Ns_MasterUnlock(); } Ns_TlsSet(&tls, arg); /* * Wait for exit signal from main(). */ Ns_MutexLock(&plock); while (!pgo) { Ns_CondWait(&pcond, &plock); } Ns_MutexUnlock(&plock); return arg; }
static Mutex * GetMutex(Ns_Mutex *mutex) { Ns_MasterLock(); if (*mutex == NULL) { Ns_MutexInit(mutex); } Ns_MasterUnlock(); return (Mutex *) *mutex; }
static pthread_cond_t * GetCond(Ns_Cond *cond) { if (*cond == NULL) { Ns_MasterLock(); if (*cond == NULL) { Ns_CondInit(cond); } Ns_MasterUnlock(); } return (pthread_cond_t *) *cond; }
void NsMutexInitNext(Ns_Mutex *mutex, char *prefix, unsigned int *nextPtr) { unsigned int id; char buf[20]; Ns_MasterLock(); id = *nextPtr; *nextPtr = id + 1; Ns_MasterUnlock(); sprintf(buf, "ns:%s:%u", prefix, id); Ns_MutexInit(mutex); Ns_MutexSetName(mutex, buf); }
void Ns_MutexInit(Ns_Mutex *mutex) { Mutex *mutexPtr; static unsigned int nextid; mutexPtr = ns_calloc(1, sizeof(Mutex)); mutexPtr->lock = NsLockAlloc(); Ns_MasterLock(); mutexPtr->nextPtr = firstMutexPtr; firstMutexPtr = mutexPtr; mutexPtr->id = nextid++; sprintf(mutexPtr->name, "mu%d", mutexPtr->id); Ns_MasterUnlock(); *mutex = (Ns_Mutex) mutexPtr; }
void Ns_MutexList(Tcl_DString *dsPtr) { Mutex *mutexPtr; char buf[100]; Ns_MasterLock(); mutexPtr = firstMutexPtr; while (mutexPtr != NULL) { Tcl_DStringStartSublist(dsPtr); Tcl_DStringAppendElement(dsPtr, mutexPtr->name); Tcl_DStringAppendElement(dsPtr, ""); sprintf(buf, " %d %lu %lu", mutexPtr->id, mutexPtr->nlock, mutexPtr->nbusy); Tcl_DStringAppend(dsPtr, buf, -1); Tcl_DStringEndSublist(dsPtr); mutexPtr = mutexPtr->nextPtr; } Ns_MasterUnlock(); }
void Ns_MutexDestroy(Ns_Mutex *mutex) { Mutex **mutexPtrPtr; Mutex *mutexPtr = (Mutex *) *mutex; if (mutexPtr != NULL) { NsLockFree(mutexPtr->lock); Ns_MasterLock(); mutexPtrPtr = &firstMutexPtr; while ((*mutexPtrPtr) != mutexPtr) { mutexPtrPtr = &(*mutexPtrPtr)->nextPtr; } *mutexPtrPtr = mutexPtr->nextPtr; Ns_MasterUnlock(); ns_free(mutexPtr); *mutex = NULL; } }
gdbm_error * gdbm_perrno(void) { static Ns_Tls tls; gdbm_error *errPtr; if (tls == NULL) { Ns_MasterLock(); if (tls == NULL) { Ns_TlsAlloc(&tls, gdbm_free); } Ns_MasterUnlock(); } errPtr = Ns_TlsGet(&tls); if (errPtr == NULL) { errPtr = gdbm_malloc(sizeof(gdbm_error)); *errPtr = GDBM_NO_ERROR; Ns_TlsSet(&tls, errPtr); } return errPtr; }