void NsInitQueue(void) { Ns_TlsAlloc(&ctdtls, NULL); Ns_MutexSetName(&connlock, "ns:connlock"); Ns_MutexSetName(&joinlock, "ns:joinlock"); }
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; }
int Ns_AllocThreadLocalStorage(Ns_ThreadLocalStorage *tls, Ns_TlsCleanup *cleanup) { Ns_TlsAlloc((Ns_Tls *) tls, cleanup); return NS_OK; }
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; } }
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; }
static int JsInit(Tcl_Interp *interp, void *arg) { jsEnv *jsEnvPtr; Ns_TlsAlloc(&tls, JsCleanup); jsEnvPtr = ns_calloc(1, sizeof(jsEnv)); jsEnvPtr->runtime = JS_NewRuntime(8L * 1024L * 1024L); jsEnvPtr->context = JS_NewContext(jsEnvPtr->runtime, 8192); jsEnvPtr->object = JS_NewObject(jsEnvPtr->context, NULL, NULL, NULL); JS_InitStandardClasses(jsEnvPtr->context, jsEnvPtr->object); JS_SetErrorReporter(jsEnvPtr->context, JsLogError); Ns_TlsSet(&tls, jsEnvPtr); Ns_Log(Debug, "JsInit: %p", jsEnvPtr); Tcl_CreateObjCommand(interp, "js.eval", JsEvalObjCmd, NULL, NULL); return TCL_OK; }
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; }
/** Standard AOLserver callback. * Initialize jk2, unless already done in another server. Register URI mappping for Tomcat * If multiple virtual servers use this module, calls to Ns_ModuleInit will be made serially * in order of appearance of those servers in nsd.tcl */ int Ns_ModuleInit(char *server, char *module) { jk_env_t *env; /* configuration-related */ char* serverName=NULL; char* confPath; static char cwdBuf[PATH_MAX]; static char* serverRoot = NULL; /* APR-related */ apr_status_t aprrc; char errbuf[512]; apr_pool_t *jk_globalPool = NULL; /* URI registration */ char *hosts[2] = {"*", NULL}; jk_map_t *vhosts; int i, j, k, l, cnt1, cnt2; jk_map_t *uriMap, *webapps, *uriMaps[3]; jk_uriEnv_t *uriEnv, *hostEnv, *appEnv; if (jkInitCount++ == 0) { /* Get Tomcat installation root - this value is same for all virtual servers*/ if (serverRoot == NULL) { confPath = Ns_ConfigGetPath (NULL, module, NULL); serverRoot = (confPath? Ns_ConfigGetValue (confPath, "serverRoot") : NULL); } /* not configured in nsd.tcl? try env. variable */ if (serverRoot == NULL) { serverRoot = getenv ("TOMCAT_HOME"); } /* not in env. variables? get it from CWD */ if (serverRoot == NULL) { serverRoot = getcwd (cwdBuf, sizeof(cwdBuf)); } /* Initialize APR */ if ((aprrc=apr_initialize()) != APR_SUCCESS) { LOG_ERROR2 ("Cannot initialize APR", apr_strerror (aprrc, errbuf, sizeof(errbuf))); return NS_ERROR; } if ((aprrc=apr_pool_create(&jk_globalPool, NULL)) != APR_SUCCESS) { LOG_ERROR2 ("Cannot create global APR pool", apr_strerror (aprrc, errbuf, sizeof(errbuf))); return NS_ERROR; } /* Initialize JNI */ if (workerEnv==NULL && jk2_create_workerEnv(jk_globalPool, serverRoot)==JK_ERR) { return NS_ERROR; } env=workerEnv->globalEnv; env->setAprPool(env, jk_globalPool); /* Initialize JK2 */ if (workerEnv->init(env, workerEnv ) != JK_OK) { LOG_ERROR("Cannot initialize worker environment"); return NS_ERROR; } workerEnv->server_name = apr_pstrcat (jk_globalPool, Ns_InfoServerName(), " ", Ns_InfoServerVersion (), NULL); apr_pool_cleanup_register(jk_globalPool, NULL, jk2_shutdown, apr_pool_cleanup_null); workerEnv->was_initialized = JK_TRUE; if ((aprrc=apr_pool_userdata_set( "INITOK", "Ns_ModuleInit", NULL, jk_globalPool )) != APR_SUCCESS) { LOG_ERROR2 ("Cannot set APR pool user data", apr_strerror (aprrc, errbuf, sizeof(errbuf))); return NS_ERROR; } if (workerEnv->parentInit( env, workerEnv) != JK_OK) { LOG_ERROR ("Cannot initialize global environment"); return NS_ERROR; } /* Obtain TLS slot - it's destructor will detach threads from JVM */ jvmGlobal = workerEnv->vm->jvm; Ns_TlsAlloc (&jkTls, jkTlsDtor); Ns_Log (Notice, "nsjk2: Initialized JK2 environment"); } else { env = workerEnv->globalEnv; } Ns_RegisterShutdown (jk2_shutdown_system, NULL); /* Register URI patterns from workers2.properties with AOLserver * * Worker environment has a list of vhosts, including "*" vhost. * Each vhost has a list of web applications (contexts) associated with it. * Each webapp has a list of exact, prefix, suffix and regexp URI patterns. * * Will register URIs that are either in vhost "*", or one with name matching * this AOLserver virtual server. Will ignore regexp patterns. Will register * exact webapp URIs (context root) as JK2 somehow doesn't include them in URI * maps, even if specified in workers2.properties. * */ /* virtual server name override if specified */ confPath = Ns_ConfigGetPath (server, module, NULL); if (confPath != NULL) serverName = Ns_ConfigGetValue (confPath, "serverName"); if (serverName == NULL) serverName = server; vhosts=workerEnv->uriMap->vhosts; hosts[1]= serverName; for (i=0; i<sizeof(hosts)/sizeof(*hosts); i++) { hostEnv=vhosts->get (env, vhosts, hosts[i]); if (hostEnv==NULL || hostEnv->webapps==NULL) continue; webapps=hostEnv->webapps; cnt1=webapps->size(env, webapps); for (j=0; j<cnt1; j++) { appEnv = webapps->valueAt (env, webapps, j); if (appEnv == NULL) continue; /* register webapp root - registerURI checks if it is "/" */ registerURI (env, appEnv, server, serverName); uriMaps[0] = appEnv->exactMatch; uriMaps[1] = appEnv->prefixMatch; uriMaps[2] = appEnv->suffixMatch; for (k=0; k<sizeof(uriMaps)/sizeof(*uriMaps); k++) { if (uriMaps[k] == NULL) continue; cnt2 = uriMaps[k]->size (env, uriMaps[k]); for (l=0; l<cnt2; l++) { registerURI (env, uriMaps[k]->valueAt (env, uriMaps[k], l), server, serverName); } } } } Ns_Log (Notice, "nsjk2: Initialized on %s", server); return NS_OK; }