int tracker_relationship_init() { int result; pthread_t tid; pthread_attr_t thread_attr; if ((result=init_pthread_attr(&thread_attr, g_thread_stack_size)) != 0) { logError("file: "__FILE__", line: %d, " \ "init_pthread_attr fail, program exit!", __LINE__); return result; } if ((result=pthread_create(&tid, &thread_attr, \ relationship_thread_entrance, NULL)) != 0) { logError("file: "__FILE__", line: %d, " \ "create thread failed, errno: %d, error info: %s", \ __LINE__, result, STRERROR(result)); return result; } pthread_attr_destroy(&thread_attr); return 0; }
static void log_gzip(LogContext *pContext) { if (COMPRESS_IN_NEW_THREAD(pContext->compress_log_flags)) { int result; pthread_t tid; pthread_attr_t thread_attr; if ((result=init_pthread_attr(&thread_attr, 0) != 0)) { return; } if ((result=pthread_create(&tid, &thread_attr, log_gzip_func, pContext)) != 0) { fprintf(stderr, "file: "__FILE__", line: %d, " \ "create thread failed, " \ "errno: %d, error info: %s", \ __LINE__, result, STRERROR(result)); } pthread_attr_destroy(&thread_attr); } else { log_gzip_func(pContext); } }
int create_work_threads(int *count, void *(*start_func)(void *), \ void *arg, pthread_t *tids, const int stack_size) { int result; pthread_attr_t thread_attr; pthread_t *ptid; pthread_t *ptid_end; if ((result=init_pthread_attr(&thread_attr, stack_size)) != 0) { return result; } result = 0; ptid_end = tids + (*count); for (ptid=tids; ptid<ptid_end; ptid++) { if ((result=pthread_create(ptid, &thread_attr, \ start_func, arg)) != 0) { *count = ptid - tids; logError("file: "__FILE__", line: %d, " \ "create thread failed, startup threads: %d, " \ "errno: %d, error info: %s", \ __LINE__, *count, \ result, STRERROR(result)); break; } } pthread_attr_destroy(&thread_attr); return result; }
CThread::CThread() : m_pthread_func(this), m_tid(-1) { int res; if((res = init_pthread_attr(0, true)) != 0){ KL_SYS_ERRORLOG("file: "__FILE__", line: %d, " \ "init thread attribute failed, err: %s", \ __LINE__, strerror(res)); throw res; } }
CThread::CThread(CThreadFunc *pThreadFunc, const int stack_size, \ bool bdetach) : m_pthread_func(pThreadFunc), m_tid(-1) { int res; if((res = init_pthread_attr(stack_size, bdetach)) != 0){ KL_SYS_ERRORLOG("file: "__FILE__", line: %d, " \ "init thread attribute failed, err: %s", \ __LINE__, strerror(res)); throw res; } }
int sched_start(ScheduleArray *pScheduleArray, pthread_t *ptid, \ const int stack_size, bool * volatile pcontinue_flag) { int result; pthread_attr_t thread_attr; ScheduleContext *pContext; pContext = (ScheduleContext *)malloc(sizeof(ScheduleContext)); if (pContext == NULL) { result = errno != 0 ? errno : ENOMEM; logError("file: "__FILE__", line: %d, " \ "malloc %d bytes failed, " \ "errno: %d, error info: %s", \ __LINE__, (int)sizeof(ScheduleContext), \ result, STRERROR(result)); return result; } if ((result=init_pthread_attr(&thread_attr, stack_size)) != 0) { free(pContext); return result; } if ((result=sched_dup_array(pScheduleArray, \ &(pContext->scheduleArray))) != 0) { free(pContext); return result; } pContext->pcontinue_flag = pcontinue_flag; if ((result=pthread_create(ptid, &thread_attr, \ sched_thread_entrance, pContext)) != 0) { free(pContext); logError("file: "__FILE__", line: %d, " \ "create thread failed, " \ "errno: %d, error info: %s", \ __LINE__, result, STRERROR(result)); } pthread_attr_destroy(&thread_attr); return result; }
int sched_start_ex(ScheduleArray *pScheduleArray, pthread_t *ptid, const int stack_size, bool * volatile pcontinue_flag, ScheduleContext **ppContext) { int result; pthread_attr_t thread_attr; ScheduleContext *pContext; pContext = (ScheduleContext *)malloc(sizeof(ScheduleContext)); if (pContext == NULL) { result = errno != 0 ? errno : ENOMEM; logError("file: "__FILE__", line: %d, " \ "malloc %d bytes failed, " \ "errno: %d, error info: %s", \ __LINE__, (int)sizeof(ScheduleContext), \ result, STRERROR(result)); return result; } memset(pContext, 0, sizeof(ScheduleContext)); if ((result=init_pthread_attr(&thread_attr, stack_size)) != 0) { free(pContext); return result; } if ((result=sched_dup_array(pScheduleArray, \ &(pContext->scheduleArray))) != 0) { free(pContext); return result; } if (timer_slot_count > 0) { if ((result=fast_mblock_init(&pContext->mblock, sizeof(FastDelayTask), mblock_alloc_once)) != 0) { free(pContext); return result; } g_current_time = time(NULL); if ((result=fast_timer_init(&pContext->timer, timer_slot_count, g_current_time)) != 0) { free(pContext); return result; } if ((result=init_pthread_lock(&pContext->delay_queue.lock)) != 0) { free(pContext); return result; } pContext->timer_init = true; } pContext->pcontinue_flag = pcontinue_flag; if ((result=pthread_create(ptid, &thread_attr, \ sched_thread_entrance, pContext)) != 0) { free(pContext); logError("file: "__FILE__", line: %d, " \ "create thread failed, " \ "errno: %d, error info: %s", \ __LINE__, result, STRERROR(result)); } *ppContext = pContext; pthread_attr_destroy(&thread_attr); return result; }
int trunk_sync_thread_start(const FDFSStorageBrief *pStorage) { int result; pthread_attr_t pattr; pthread_t tid; if (pStorage->status == FDFS_STORAGE_STATUS_DELETED || \ pStorage->status == FDFS_STORAGE_STATUS_IP_CHANGED || \ pStorage->status == FDFS_STORAGE_STATUS_NONE) { return 0; } if (is_local_host_ip(pStorage->ip_addr)) //can't self sync to self { return 0; } if ((result=init_pthread_attr(&pattr, g_thread_stack_size)) != 0) { return result; } /* //printf("start storage ip_addr: %s, g_trunk_sync_thread_count=%d\n", pStorage->ip_addr, g_trunk_sync_thread_count); */ if ((result=pthread_create(&tid, &pattr, trunk_sync_thread_entrance, \ (void *)pStorage)) != 0) { logError("file: "__FILE__", line: %d, " \ "create thread failed, errno: %d, " \ "error info: %s", \ __LINE__, result, STRERROR(result)); pthread_attr_destroy(&pattr); return result; } if ((result=pthread_mutex_lock(&trunk_sync_thread_lock)) != 0) { logError("file: "__FILE__", line: %d, " \ "call pthread_mutex_lock fail, " \ "errno: %d, error info: %s", \ __LINE__, result, STRERROR(result)); } g_trunk_sync_thread_count++; trunk_sync_tids = (pthread_t *)realloc(trunk_sync_tids, sizeof(pthread_t) * \ g_trunk_sync_thread_count); if (trunk_sync_tids == NULL) { logError("file: "__FILE__", line: %d, " \ "malloc %d bytes fail, " \ "errno: %d, error info: %s", \ __LINE__, (int)sizeof(pthread_t) * \ g_trunk_sync_thread_count, \ errno, STRERROR(errno)); } else { trunk_sync_tids[g_trunk_sync_thread_count - 1] = tid; } if ((result=pthread_mutex_unlock(&trunk_sync_thread_lock)) != 0) { logError("file: "__FILE__", line: %d, " \ "call pthread_mutex_unlock fail, " \ "errno: %d, error info: %s", \ __LINE__, result, STRERROR(result)); } pthread_attr_destroy(&pattr); return 0; }
int storage_dio_init() { int result; int bytes; int threads_count_per_path; int context_count; struct storage_dio_thread_data *pThreadData; struct storage_dio_thread_data *pDataEnd; struct storage_dio_context *pContext; struct storage_dio_context *pContextEnd; pthread_t tid; pthread_attr_t thread_attr; if ((result=init_pthread_lock(&g_dio_thread_lock)) != 0) { return result; } if ((result=init_pthread_attr(&thread_attr, g_thread_stack_size)) != 0) { logError("file: "__FILE__", line: %d, " \ "init_pthread_attr fail, program exit!", __LINE__); return result; } bytes = sizeof(struct storage_dio_thread_data) * g_fdfs_store_paths.count; g_dio_thread_data = (struct storage_dio_thread_data *)malloc(bytes); if (g_dio_thread_data == NULL) { logError("file: "__FILE__", line: %d, " \ "malloc %d bytes fail, errno: %d, error info: %s", \ __LINE__, bytes, errno, STRERROR(errno)); return errno != 0 ? errno : ENOMEM; } memset(g_dio_thread_data, 0, bytes); threads_count_per_path = g_disk_reader_threads + g_disk_writer_threads; context_count = threads_count_per_path * g_fdfs_store_paths.count; bytes = sizeof(struct storage_dio_context) * context_count; g_dio_contexts = (struct storage_dio_context *)malloc(bytes); if (g_dio_contexts == NULL) { logError("file: "__FILE__", line: %d, " \ "malloc %d bytes fail, " \ "errno: %d, error info: %s", __LINE__, \ bytes, errno, STRERROR(errno)); return errno != 0 ? errno : ENOMEM; } memset(g_dio_contexts, 0, bytes); g_dio_thread_count = 0; pDataEnd = g_dio_thread_data + g_fdfs_store_paths.count; for (pThreadData=g_dio_thread_data; pThreadData<pDataEnd; pThreadData++) { pThreadData->count = threads_count_per_path; pThreadData->contexts = g_dio_contexts + (pThreadData - \ g_dio_thread_data) * threads_count_per_path; pThreadData->reader = pThreadData->contexts; pThreadData->writer = pThreadData->contexts+g_disk_reader_threads; pContextEnd = pThreadData->contexts + pThreadData->count; for (pContext=pThreadData->contexts; pContext<pContextEnd; \ pContext++) { if ((result=blocked_queue_init(&(pContext->queue))) != 0) { return result; } if ((result=pthread_create(&tid, &thread_attr, \ dio_thread_entrance, pContext)) != 0) { logError("file: "__FILE__", line: %d, " \ "create thread failed, " \ "startup threads: %d, " \ "errno: %d, error info: %s", \ __LINE__, g_dio_thread_count, \ result, STRERROR(result)); return result; } else { pthread_mutex_lock(&g_dio_thread_lock); g_dio_thread_count++; pthread_mutex_unlock(&g_dio_thread_lock); } } } pthread_attr_destroy(&thread_attr); return result; }