/* ============================================================================= * thread_startup * -- Create pool of secondary threads * -- numThread is total number of threads (primary + secondaries) * ============================================================================= */ void thread_startup (long numThread) { long i; global_numThread = numThread; global_doShutdown = FALSE; /* Set up barrier */ assert(global_barrierPtr == NULL); global_barrierPtr = THREAD_BARRIER_ALLOC(numThread); assert(global_barrierPtr); THREAD_BARRIER_INIT(global_barrierPtr, numThread); #ifdef MAP_USE_TFAVLTREE #ifdef SEPERATE_MAINTENANCE thread_done = (bool_t *)malloc((numThread -4) * sizeof(bool_t)); for(i = 0; i < numThread -4; i++) { thread_done[i] = FALSE; } /* assert(global_barrierPtr2 == NULL); */ /* global_barrierPtr2 = THREAD_BARRIER_ALLOC(numThread-4); */ /* assert(global_barrierPtr2); */ /* THREAD_BARRIER_INIT(global_barrierPtr2, numThread-4); */ /* assert(global_barrierPtr3 == NULL); */ /* global_barrierPtr3 = THREAD_BARRIER_ALLOC(4); */ /* assert(global_barrierPtr3); */ /* THREAD_BARRIER_INIT(global_barrierPtr3, 4); */ #endif #endif /* Set up ids */ THREAD_LOCAL_INIT(global_threadId); assert(global_threadIds == NULL); global_threadIds = (long*)malloc(numThread * sizeof(long)); assert(global_threadIds); for (i = 0; i < numThread; i++) { global_threadIds[i] = i; } /* Set up thread list */ assert(global_threads == NULL); global_threads = (THREAD_T*)malloc(numThread * sizeof(THREAD_T)); assert(global_threads); /* Set up pool */ THREAD_ATTR_INIT(global_threadAttr); for (i = 1; i < numThread; i++) { THREAD_CREATE(global_threads[i], global_threadAttr, &threadWait, &global_threadIds[i]); } /* * Wait for primary thread to call thread_start */ }
/* ============================================================================= * thread_startup * -- Create pool of secondary threads * -- numThread is total number of threads (primary + secondaries) * ============================================================================= */ void thread_startup (long numThread) { long i; totalThreads = numThread; global_numThread = numThread; global_doShutdown = FALSE; /* Set up barrier */ assert(global_barrierPtr == NULL); global_barrierPtr = THREAD_BARRIER_ALLOC(numThread); assert(global_barrierPtr); THREAD_BARRIER_INIT(global_barrierPtr, numThread); /* Set up ids */ THREAD_LOCAL_INIT(global_threadId); assert(global_threadIds == NULL); global_threadIds = (long*)malloc(numThread * sizeof(long)); assert(global_threadIds); for (i = 0; i < numThread; i++) { global_threadIds[i] = i; } /* Set up thread list */ assert(global_threads == NULL); global_threads = (THREAD_T*)malloc(numThread * sizeof(THREAD_T)); assert(global_threads); threadArgsArr = (thread_args_t*) malloc(numThread * sizeof(thread_args_t)); threadArgsArr[0].aborts = 0; threadArgsArr[0].commits = 0; threadArgsArr[0].threadId = global_threadIds[0]; /* Set up pool */ THREAD_ATTR_INIT(global_threadAttr); for (i = 1; i < numThread; i++) { threadArgsArr[i].aborts = 0; threadArgsArr[i].commits = 0; threadArgsArr[i].threadId = global_threadIds[i]; THREAD_CREATE(global_threads[i], global_threadAttr, &threadWait, &(threadArgsArr[i])); } /* * Wait for primary thread to call thread_start */ }
/* ============================================================================= * thread_startup * -- Create pool of secondary threads * -- numThread is total number of threads (primary + secondaries) * ============================================================================= */ void thread_startup (long numThread) { long i; global_numThread = numThread; global_doShutdown = FALSE; /* Set up barrier */ assert(global_barrierPtr == NULL); global_barrierPtr = THREAD_BARRIER_ALLOC(numThread); assert(global_barrierPtr); THREAD_BARRIER_INIT(global_barrierPtr, numThread); /* Set up ids */ THREAD_LOCAL_INIT(global_threadId); assert(global_threadIds == NULL); global_threadIds = (long*)malloc(numThread * sizeof(long)); assert(global_threadIds); for (i = 0; i < numThread; i++) { global_threadIds[i] = i; } /* Set up thread list */ assert(global_threads == NULL); global_threads = (THREAD_T*)malloc(numThread * sizeof(THREAD_T)); assert(global_threads); /* Set up pool */ for (i = 1; i < numThread; i++) { THREAD_ATTR_T attr; THREAD_ATTR_INIT(attr); pthread_attr_init(&attr); pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); THREAD_CREATE(global_threads[i], attr, &threadWait, &global_threadIds[i]); } /* * Wait for primary thread to call thread_start */ }
/* ============================================================================= * thread_startup * -- Create pool of secondary threads * -- numThread is total number of threads (primary + secondaries) * ============================================================================= */ void thread_startup (long numThread) { int i; global_numThread = numThread; global_doShutdown = FALSE; /* Set up barrier */ assert(global_barrierPtr == NULL); global_barrierPtr = THREAD_BARRIER_ALLOC(numThread); assert(global_barrierPtr); THREAD_BARRIER_INIT(global_barrierPtr, numThread); /* Set up ids */ THREAD_LOCAL_INIT(global_threadId); assert(global_threadIds == NULL); global_threadIds = (long*)malloc(numThread * sizeof(long)); assert(global_threadIds); for (i = 0; i < numThread; i++) { global_threadIds[i] = i; } /* Set up thread list */ assert(global_threads == NULL); global_threads = (THREAD_T*)malloc(numThread * sizeof(THREAD_T)); assert(global_threads); //writers_lock.value = 0; /* Set up pool */ THREAD_ATTR_INIT(global_threadAttr); for (i = 1; i < numThread; i++) { THREAD_CREATE(global_threads[i], global_threadAttr, &threadWait, &global_threadIds[i]); } }
TSS_RESULT tcsd_thread_create(int socket, char *hostname) { UINT32 thread_num = -1; int rc = TCS_SUCCESS; #ifndef TCSD_SINGLE_THREAD_DEBUG THREAD_ATTR_DECLARE(tcsd_thread_attr); /* init the thread attribute */ if ((rc = THREAD_ATTR_INIT(tcsd_thread_attr))) { LogError("Initializing thread attribute failed: error=%d: %s", rc, strerror(rc)); rc = TCSERR(TSS_E_INTERNAL_ERROR); goto out; } /* make all threads joinable */ if ((rc = THREAD_ATTR_SETJOINABLE(tcsd_thread_attr))) { LogError("Making thread attribute joinable failed: error=%d: %s", rc, strerror(rc)); rc = TCSERR(TSS_E_INTERNAL_ERROR); goto out; } MUTEX_LOCK(tm->lock); #endif if (tm->num_active_threads == tm->max_threads) { if (hostname != NULL) { LogError("max number of connections reached (%d), new connection" " from %s refused.", tm->max_threads, hostname); } else { LogError("max number of connections reached (%d), new connection" " refused.", tm->max_threads); } rc = TCSERR(TSS_E_CONNECTION_FAILED); #ifndef TCSD_SINGLE_THREAD_DEBUG goto out_unlock; #else goto out; #endif } /* search for an open slot to store the thread data in */ for (thread_num = 0; thread_num < tm->max_threads; thread_num++) { if (tm->thread_data[thread_num].thread_id == THREAD_NULL) break; } DBG_ASSERT(thread_num != tm->max_threads); tm->thread_data[thread_num].sock = socket; tm->thread_data[thread_num].context = NULL_TCS_HANDLE; if (hostname != NULL) tm->thread_data[thread_num].hostname = hostname; #ifdef TCSD_SINGLE_THREAD_DEBUG (void)tcsd_thread_run((void *)(&(tm->thread_data[thread_num]))); #else tm->thread_data[thread_num].thread_id = calloc(1, sizeof(THREAD_TYPE)); if (tm->thread_data[thread_num].thread_id == NULL) { rc = TCSERR(TSS_E_OUTOFMEMORY); LogError("malloc of %zd bytes failed.", sizeof(THREAD_TYPE)); goto out_unlock; } if ((rc = THREAD_CREATE(tm->thread_data[thread_num].thread_id, &tcsd_thread_attr, tcsd_thread_run, (void *)(&(tm->thread_data[thread_num]))))) { LogError("Thread create failed: %d", rc); rc = TCSERR(TSS_E_INTERNAL_ERROR); goto out_unlock; } tm->num_active_threads++; out_unlock: MUTEX_UNLOCK(tm->lock); #endif out: /* cleanup in case of error */ if (rc != TCS_SUCCESS) { if (hostname != NULL) { tm->thread_data[thread_num].hostname = NULL; free(hostname); } close(socket); } return rc; }