void NsInitQueue(void) { Ns_TlsAlloc(&ctdtls, NULL); Ns_MutexSetName(&connlock, "ns:connlock"); Ns_MutexSetName(&joinlock, "ns:joinlock"); }
void NsInitThreads(void) { char *env; int err; err = pthread_key_create(&key, FreeThread); if (err != 0) { NsThreadFatal("NsPthreadsInit", "pthread_key_create", err); } stackdown = StackDown(&env); pagesize = getpagesize(); env = getenv("NS_THREAD_GUARDSIZE"); if (env == NULL || Tcl_GetInt(NULL, env, &guardsize) != TCL_OK || guardsize < 2) { guardsize = 2 * pagesize; } guardsize = PageRound(guardsize); markpages = getenv("NS_THREAD_MARKPAGES") ? 1 : 0; dumpdir = getenv("NS_THREAD_DUMPDIR"); env = getenv("NS_THREAD_LOGFILE"); if (env != NULL) { if (strcmp(env, "-") == 0) { logfp = stderr; } else { logfp = fopen(env, "a"); } } Ns_MutexSetName(&uidlock, "ns:uidlock"); }
void NsThreads_LibInit(void) { static int once = 0; if (!once) { once = 1; NsInitThreads(); NsInitMaster(); NsInitReentrant(); Ns_MutexSetName(&threadlock, "ns:threads"); Ns_MutexSetName(&sizelock, "ns:stacksize"); Ns_TlsAlloc(&key, CleanupThread); stackdef = 64 * 1024; stackmin = 16 * 1024; } }
void NsInitConf(void) { Ns_DString addr; static char cwd[PATH_MAX]; extern char *nsBuildDate; /* NB: Declared in stamp.c */ Ns_ThreadSetName("-main-"); /* * Set various core environment variables. */ nsconf.build = nsBuildDate; nsconf.name = NSD_NAME; nsconf.version = NSD_VERSION; nsconf.tcl.version = TCL_VERSION; nsconf.http.major = HTTP_MAJOR; nsconf.http.minor = HTTP_MINOR; time(&nsconf.boot_t); nsconf.pid = getpid(); nsconf.home = getcwd(cwd, sizeof(cwd)); if (gethostname(nsconf.hostname, sizeof(nsconf.hostname)) != 0) { strcpy(nsconf.hostname, "localhost"); } Ns_DStringInit(&addr); if (Ns_GetAddrByHost(&addr, nsconf.hostname)) { strcpy(nsconf.address, addr.string); } else { strcpy(nsconf.address, "0.0.0.0"); } Ns_DStringFree(&addr); /* * Set various default values. */ nsconf.shutdowntimeout = SHUTDOWNTIMEOUT; nsconf.sched.maxelapsed = SCHED_MAXELAPSED; nsconf.backlog = LISTEN_BACKLOG; nsconf.http.major = HTTP_MAJOR; nsconf.http.minor = HTTP_MINOR; nsconf.tcl.lockoninit = TCL_INITLCK; /* * At library load time the server is considered started. * Normally it's marked stopped immediately by Ns_Main unless * libnsd is being used for some other, non-server program. */ Ns_MutexSetName(&nsconf.state.lock, "nsd:state"); nsconf.state.started = 1; }
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); }
int main(int argc, char *argv[]) { int i, code; Ns_Thread threads[10]; Ns_Thread self, dumper; void *arg; char *p; #if PTHREAD_TEST pthread_t tids[10]; #endif NsThreads_LibInit(); Ns_ThreadSetName("-main-"); /* * Jump directly to memory test if requested. */ for (i = 1; i < argc; ++i) { p = argv[i]; switch (*p) { case 'n': break; case 'm': nthreads = atoi(p + 1); goto mem; break; } } Ns_ThreadCreate(DetachedThread, NULL, 0, NULL); Ns_ThreadCreate(DumperThread, NULL, 0, &dumper); Ns_MutexSetName(&lock, "startlock"); Ns_MutexSetName(&dlock, "dumplock"); Ns_MutexSetName(&mlock, "msglock"); Ns_MutexSetName(&block, "busylock"); Ns_ThreadStackSize(81920); Ns_SemaInit(&sema, 3); Msg("sema initialized to 3"); atexit(AtExit); Msg("pid = %d", getpid()); Ns_TlsAlloc(&key, TlsLogArg); for (i = 0; i < 10; ++i) { Msg("starting work thread %d", i); Ns_ThreadCreate(WorkThread, (void *) i, 0, &threads[i]); } sleep(1); /* Ns_CondSignal(&cond); */ Ns_SemaPost(&sema, 10); Msg("sema post 10"); Ns_RWLockWrLock(&rwlock); Msg("rwlock write locked (main thread)"); sleep(1); Ns_RWLockUnlock(&rwlock); Msg("rwlock write unlocked (main thread)"); for (i = 0; i < 10; ++i) { Msg("waiting for thread %d to exit", i); Ns_ThreadJoin(&threads[i], (void **) &code); Msg("thread %d exited - code: %d", i, code); } #if PTHREAD_TEST for (i = 0; i < 10; ++i) { pthread_create(&tids[i], NULL, Pthread, (void *) i); printf("pthread: create %d = %d\n", i, (int) tids[i]); Ns_ThreadYield(); } Ns_MutexLock(&plock); pgo = 1; Ns_MutexUnlock(&plock); Ns_CondBroadcast(&pcond); for (i = 0; i < 10; ++i) { pthread_join(tids[i], &arg); printf("pthread: join %d = %d\n", i, (int) arg); } #endif Ns_ThreadSelf(&self); Ns_MutexLock(&dlock); dstop = 1; Ns_CondSignal(&dcond); Ns_MutexUnlock(&dlock); Ns_ThreadJoin(&dumper, NULL); Msg("threads joined"); for (i = 0; i < 10; ++i) { Ns_ThreadCreate(CheckStackThread, NULL, 8192*(i+1), &threads[i]); } for (i = 0; i < 10; ++i) { Ns_ThreadJoin(&threads[i], &arg); printf("check stack %d = %d\n", i, (int) arg); } /*Ns_ThreadEnum(DumpThreads, NULL);*/ /*Ns_MutexEnum(DumpLocks, NULL);*/ mem: MemTime(0); MemTime(1); return 0; }