int main(int argc, const char *argv[]) { pool_t pool; thread_pool_init(&pool, 4); thread_pool_start(&pool); srand(100); while(1) { sleep(1); task_t tsk; tsk.thread_callback = task_func; tsk.arg = (void *)(rand() % 100); thread_pool_add_task_to_queue(&pool, tsk); } thread_pool_stop(&pool); thread_pool_destroy(&pool); return 0; }
int main(int argc,char **argv) { if(argc < 4) { fprintf(stderr,"No enough parameter.\n"); exit(EXIT_FAILURE); } thread_pool_t *pool = thread_pool_create(atoi(argv[1])); thread_pool_start(pool); int i; int end = atoi(argv[2]); for(i = 0; i < end; ++i) { thread_pool_execute(pool,callback,&value); } printf("\n"); sleep(5); for(i = 0; i < end; ++i) { thread_pool_execute(pool,callback_function,(void*)i); } sleep(atoi(argv[3])); thread_pool_destroy(pool); printf("\n"); return 0; }
thread_pool_t *thread_pool_create(int size, thread_func_t func, void *data) { int i = 0; thread_pool_t *thread_pool = NULL; if(size <= 0){ return NULL; } thread_pool = (thread_pool_t *)MALLOC_WRAPPER(sizeof(thread_pool_t)); if(NULL == thread_pool){ printf("MALLOC_WRAPPER for thread pool failed.\n"); return NULL; } thread_pool->threads = (thread_context_t *)MALLOC_WRAPPER(sizeof(thread_context_t) * size); if(NULL == thread_pool->threads){ printf("MALLOC_WRAPPER for thread_pool->threads failed.\n"); thread_pool_destroy(&thread_pool); return NULL; } bzero(thread_pool->threads, sizeof(thread_context_t) * size); thread_pool->thread_num = size; thread_pool->queue = queue_create(1024); if(NULL == thread_pool->queue){ printf("queue_create for thread_pool->queue failed.\n"); thread_pool_destroy(&thread_pool); return NULL; } thread_pool->data = data; for(i = 0; i < size; i++){ thread_pool->threads[i].id = i; thread_pool->threads[i].thread_pool = thread_pool; thread_pool->threads[i].work_continue = 1; if(0 != pthread_create(&thread_pool->threads[i].thread, NULL, func, &thread_pool->threads[i])){ printf("create thread[%d] for thread pool failed.\n", i); thread_pool_destroy(&thread_pool); return NULL; } } return thread_pool; }
static int init_server(arguments *args) { printf("Starting server...\n"); if (evthread_use_pthreads() == -1) { printf("Error occured while turning on pthreads using\n"); return -1; } signal(SIGINT, int_handler); struct evconnlistener* listener; struct sockaddr_in sin; base = event_base_new(); if (!base) { printf("Error while creating event base\n"); free(args); return -1; } thread_pool* thpool = thread_pool_init(args->ncpu, args->doc_root); memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = htonl(INADDR_ANY); sin.sin_port = htons((unsigned short)args->port); listener = evconnlistener_new_bind(base, accept_connection_cb, (void*)thpool, (LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE), -1, (struct sockaddr*) &sin, sizeof(sin)); free(args); if (!listener) { printf("Error while creating listener\n"); thread_pool_destroy(thpool); event_base_free(base); return -1; } evconnlistener_set_error_cb(listener, accept_error_cb); printf("Server is running\nUse Ctrl+C to stop server\n"); event_base_dispatch(base); evconnlistener_free(listener); thread_pool_destroy(thpool); event_base_free(base); return 0; }
void zdb_finalize() { if(!zdb_init_done) { return; } zdb_init_done = FALSE; #if ZDB_DNSSEC_SUPPORT != 0 dnssec_keystore_destroy(); dnssec_keystore_resetpath(); #endif #if ZDB_OPENSSL_SUPPORT!=0 ERR_remove_state(0); /* Init openssl */ CRYPTO_set_locking_callback(NULL); CRYPTO_set_id_callback(NULL); int i; for(i = 0; i < ssl_mutex_count; i++) { pthread_mutex_destroy(&ssl_mutex[i]); } free(ssl_mutex); ENGINE_cleanup(); #endif logger_stop(); #if ZDB_USE_THREADPOOL != 0 /* * The default value for the database. * This initialization will do nothing if it has already been done. * * The server will have to do it before calling zdb_init(); * */ if(thread_pool_initialized_by_zdb) { thread_pool_destroy(); } #endif }
void cut_teardown (void) { if (doc) document_free(doc); if (pool) thread_pool_destroy(pool); if (docset) document_set_free(docset); if (findex) fixed_index_free(findex); }
int main(int argc, char **argv) { int s; int nthread = -1; int ssize = -1; char *ip, *port; char c; ip = port = NULL; while ((c = getopt(argc, argv, "p:i:n:s:h")) != -1) { switch(c) { case 'p': port = optarg; break; case 'i': ip = optarg; break; case 'n': nthread = atoi(optarg); break; case 's': ssize = atoi(optarg); if (!STACKSIZE(ssize)) ssize = 10; break; case 'h': usage(); exit(0); default: usage(); exit(1); } } if (port == NULL) { usage(); return 1; } s = tcp_listen(ip, port); if (s == -1) return 1; struct thread_pool *pool; int r = thread_pool_create(&pool, nthread, process_msg, s, ssize); printf("thread pool create = %d\n", r); thread_pool_destroy(pool); close(s); return 0; }
void thread_pool_test(void) { int i, r; thread_pool_t pool; pool = thread_pool_create(4, 2, 8); for(i=0; i<20; i++) { r = thread_pool_push(pool, worker, &i); assert(0 == r); } thread_pool_destroy(pool); }
int stop() { http_stop(); thread_pool_destroy(); rest_destroy(); jparse_destroy(); #ifdef _DEBUG billing_destroy(); #endif api_log_printf("[HTTP] Stopped\r\n"); return 0; }
int main(void) { thread_pool_t *tpool; thread_t *tp; task_t *task; tpool = thread_pool_init(5); if (tpool == NULL) { printf("init thread pool fail"); return -1; } tp = thread_pool_get(tpool); if (tp == NULL) { printf("fail to get thread."); free(tpool); return -1; } pthread_mutex_lock(&tp->thd.lock); task = (task_t *)malloc(sizeof(task_t)); if (task == NULL) { printf("init task fail."); } task->next = NULL; task->ctx = NULL; task->handler = func; tp->thd.taskqueue = task; tp->thd.busy = 1; pthread_mutex_unlock(&tp->thd.lock); pthread_cond_signal(&tp->thd.cond); printf("in main :pid[%lu],tid[%lu],process ....\n",(unsigned long)getpid(),(unsigned long)pthread_self()); sleep(5); thread_pool_destroy(tpool); return 0; }
void test_empty_thread_pool() { dna_log(INFO, "<-------------------- test_empty_thread_pool ---------------------"); int i = 0; for (i = 0; i < 10 ; i++ ) { dna_log(INFO, ">> --- cycle %i --- <<", i+1); // global here, for all threads to share fifo = fifo_create("values", 0); dna_log(DEBUG, "starting thread pool"); thread_pool_t *pool = thread_pool_create("main pool", 1); dna_log(DEBUG, "destroying thread pool"); thread_pool_exit_all(pool); thread_pool_destroy(pool); fifo_check(); fifo_destroy(fifo); } }
int main() { int i; int tmp[TASK_NUM]; thread_pool * tp = (thread_pool*)malloc(sizeof(thread_pool)); thread_pool_init(tp,THREAD_NUM); for(i = 0; i < TASK_NUM; i++) { tmp[i] = i; #ifdef DBG_MSG printf("task #%d added to thread pool!\n",i); #endif thread_pool_add_tsk(tp,threadrun,(void*)&tmp[i]); } sleep(2); thread_pool_destroy(&tp); return 0; }
void test_busy_thread_pool() { dna_log(INFO, "<-------------------- test_busy_thread_pool ---------------------"); fifo = fifo_create("<(busy_thread_pool) value fifo>", 0); thread_pool_t *pool = thread_pool_create("<busy thread pool>", 8); // should auto-determine thread count maybe? dna_log(DEBUG, "adding %i tasks to the queue...", ELEMS); int i = 0; for( i = 0; i < ELEMS; i++ ) { thread_pool_enqueue(pool, &fifo_fill, NULL); } dna_log(DEBUG, "waiting for threads to complete on their own..."); thread_pool_exit_all(pool); thread_pool_join_all( pool ); dna_log(DEBUG, "destroying thread pool..."); thread_pool_destroy( pool ); fifo_check(); dna_log(DEBUG, "destroying global value fifo."); fifo_destroy( fifo ); }
int main() { pool_t pool; thread_pool_init(&pool, 5); thread_pool_start(&pool); srand(10086); while(1) { task_t task; task.thread_callback = task_func; task.arg = (void *)(rand() % 100); thread_pool_add_task(&pool, task); printf("ThreadPool size: %d, TaskQueue size: %d\n", (int)pool.size_, (int)pool.queue_.size_); } thread_pool_stop(&pool); thread_pool_destroy(&pool); return 0; }
int main(int argc, char* argv[]) { char c; int cpu = system_getcpucount(); socket_t server; aio_socket_t aioserver; memset(s_buffer, 'A', sizeof(s_buffer)-1); aio_socket_init(cpu); s_thpool = thread_pool_create(cpu, cpu, cpu*2); while(cpu > 0) { thread_pool_push(s_thpool, AIOWorker, NULL); --cpu; } server = Listen(50000); aioserver = aio_socket_create(server, 1); aio_socket_accept(aioserver, OnAccept, aioserver); printf("server listen at: %d\n", 50000); for(c = getchar(); 'q' != c; c = getchar()) { switch(c) { case 'c': // close socket aio_socket_destroy(aioserver); break; default: printf("unknown command.\nc : close socket\n"); } } aio_socket_destroy(aioserver); thread_pool_destroy(s_thpool); aio_socket_clean(); return 0; }
int main(int argc, char* argv[]) { tcpserver_t tcpserver; tcpserver_handler_t tcphandler; tcphandler.onerror = OnTcpError; tcphandler.onconnected = OnTcpConnected; socket_init(); g_thdpool = thread_pool_create(2, 1, 64); tcpserver = tcpserver_start(NULL, 10000, &tcphandler, NULL); http_proxy_find(OnFindProxy, NULL); while('q' != getchar()) { } tcpserver_stop(tcpserver); thread_pool_destroy(g_thdpool); socket_cleanup(); return 0; }
int main(int argc, char* argv[]) { #if defined(OS_LINUX) /* ignore pipe signal */ struct sigaction sa; sa.sa_handler = SIG_IGN; sigaction(SIGCHLD, &sa, 0); sigaction(SIGPIPE, &sa, 0); #endif int port = 554; for(int i=1; i<argc; i++) { if(streq(argv[i], "--port") && i+1<argc) { port = atoi(argv[++i]); } } size_t cpu = system_getcpucount(); g_thpool = thread_pool_create(cpu, cpu, cpu*4); aio_socket_init(cpu * 2); for(size_t i=0; i<cpu * 2; i++) thread_pool_push(g_thpool, AioWorker, NULL); // start worker // start server StartTcpServer(NULL, port); StartUdpServer(NULL, port); for(int c = getchar(); 'q' != c ; c = getchar()) { } aio_socket_destroy(s_tcp); aio_socket_clean(); thread_pool_destroy(g_thpool); return 0; }
int main(int argc, int argv) { char *args[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30"}; thread_pool_t *thread_pool = thread_pool_init(1, 2); int i = 0, tag, value; for (i = 0; i < 30; i++) { do { tag = thread_pool_add_job(thread_pool, work, args[i]); if (tag == 1) { value = thread_pool_resize(thread_pool, thread_pool->thread_num * 2, thread_pool->queue_max_num * 2); if (value == -1) { printf("参数错误!\n"); exit(-1); } else if (value == -2) { printf("申请内存错误!\n"); exit(-1); } else if (value == -3) { printf("线程创建错误!\n"); exit(-1); } } }while (tag != 0); } sleep(2); thread_pool_destroy(thread_pool); return 0; }
int _aio_destroy() { struct _aio_control_block *cb; struct _aio_context *ctp; struct _aio_prio_list *plist; thread_pool_t *tp; cb = _aio_cb; if ((tp = (thread_pool_t *)_smp_xchg((unsigned *)&_aio_cb->tp, 0)) == 0) return 0; /* kill every thread */ if (thread_pool_destroy(tp) != 0) return -1; while ((plist = cb->cb_plist)) { cb->cb_plist = plist->next; free(plist); } while ((plist = cb->cb_plist_free)) { cb->cb_plist_free = plist->next; free(plist); } while ((ctp = cb->ct_free)) { cb->ct_free = ctp->next; free(ctp); } pthread_mutex_destroy(&cb->cb_mutex); pthread_cond_destroy(&cb->cb_cond); free(cb); pthread_mutex_destroy(&_aio_init_mutex); return 0; }
void dnscore_finalize() { /* * No need to "finalize" format, dnsformat and rfc */ if(dnscore_finalizing) { /* OOPS : ALREADY BUSY SHUTTING DOWN */ /* DO NOT USE LOGGER HERE ! */ return; } dnscore_finalizing = TRUE; dnscore_shutdown(); #ifndef NDEBUG log_debug("exit: destroying the thread pool"); #endif logger_flush(); thread_pool_destroy(); #ifdef DEBUG log_debug("exit: bye (pid=%hd)", getpid()); logger_flush(); #endif scheduler_finalize(); logger_flush(); logger_finalize(); /** @note does a logger_stop */ logger_handle_finalize(); #ifndef NDEBUG /* * It may not be required right now, but in case the stdstream are filtered/buffered * this will flush them. */ #if HAS_TSIG_SUPPORT tsig_finalize(); #endif stdstream_flush_both_terms(); error_unregister_all(); rfc_finalize(); format_class_finalize(); #endif #ifndef NDEBUG #if ZDB_DEBUG_MALLOC != 0 debug_stat(TRUE); #endif #endif stdstream_flush_both_terms(); output_stream_close(&__termerr__); output_stream_close(&__termout__); }
/* * 侦听端口及处理请求 */ int SelectAndHandle( int listensockfd, int listenunixfd ) { unsigned long addrsize; fd_set rset,allset; int connectfd,maxfd=listensockfd>listenunixfd?listensockfd:listenunixfd; int i,n; uid_t uid; int nready; struct sockaddr cliaddr; char recvbuf[SIZERCV+1]; char *p; CLIENT client[FDSIZE]; addrsize = sizeof( struct sockaddr ); FD_ZERO( &allset ); FD_SET( listensockfd, &allset ); FD_SET( listenunixfd, &allset ); for( i=0; i<FDSIZE; i++ ) { client[i].fd = -1; client[i].i = i; } printf("%sSelectAndHandle\n", AT); /* 初始化线程池 */ thread_pool_t *pool = NULL; if( thread_pool_init(&pool, POOLSIZE) < 0 ) { err_sys( "thread pool init error" ); } n=1; while (1) { rset = allset; /* 接受新连接 */ if (( nready = select( maxfd + 1, &rset, NULL, NULL, NULL ) ) < 0 ) { fprintf(stderr,"%sselect err %d %s",AT, nready, strerror(errno) ); continue; } if (FD_ISSET( listensockfd, &rset )) { if (( connectfd = accept( listensockfd, &cliaddr, ( socklen_t *)&addrsize ) ) == -1 ) { fprintf(stderr, "%saccept err", AT ); sleep(1); continue; } SOCKAGAIN: for( i = 0; i<FDSIZE; i++ ) { if( client[i].fd < 0 ) { client[i].fd = connectfd; client[i].type = SOCK; client[i].addr = cliaddr; client[i].n = n; thread_pool_add_worker(pool, ( FUNC )HandleInSide, &(client[i]) ); n++; break; } } if (i==FDSIZE) { /* 线程池已满 */ fprintf(stderr,"%s,pool is full\n", AT); sleep(1); goto SOCKAGAIN; } if ( --nready <= 0 ) continue; } if( FD_ISSET( listenunixfd, &rset ) ) { #if defined(Darwin) if (( connectfd = serv_accept( listenunixfd, NULL ) ) < 0 ) #else if (( connectfd = serv_accept( listenunixfd, &uid ) ) < 0 ) #endif { fprintf(stderr, "%sserv_accept err %d %s\n", AT, connectfd, strerror(errno) ); sleep(1); continue; } UNIXAGAIN: for( i = 0; i<FDSIZE; i++ ) { if( client[i].fd < 0 ) { client[i].fd = connectfd; #if !defined(Darwin) client[i].uid = uid; #endif client[i].type = SOCK; thread_pool_add_worker(pool, ( FUNC )HandleOutSide, &(client[i]) ); n++; break; } } if (i==FDSIZE) { fprintf(stderr,"%s,pool is full\n", AT); sleep(1); goto UNIXAGAIN; } if ( --nready <= 0 ) continue; } } thread_pool_destroy(pool); pool = NULL; return EXIT_SUCCESS; }
void scan_exit() { thread_pool_destroy(scan_thread_pool); }
int _aio_init(thread_pool_attr_t *pool_attr) { static thread_pool_attr_t default_pool_attr = { NULL, _aio_block, _aio_unblock, _aio_handler, _aio_context_alloc, _aio_context_free, NULL, 3, 1, 8, 10 }; struct _aio_control_block *cb; struct _aio_context *ctp; struct _aio_prio_list *plist; sigset_t set, oset; int i; _mutex_lock(&_aio_init_mutex); if (_aio_cb) { _mutex_unlock(&_aio_init_mutex); return 0; } if ((cb = malloc(sizeof(*_aio_cb))) == NULL) { _mutex_unlock(&_aio_init_mutex); return -1; } memset(cb, 0, sizeof(*cb)); pthread_mutex_init(&cb->cb_mutex, 0); (void)pthread_cond_init(&cb->cb_cond, 0); if (pool_attr == NULL) { pool_attr = &default_pool_attr; } else { pool_attr->block_func = _aio_block; pool_attr->context_alloc = _aio_context_alloc; pool_attr->unblock_func = _aio_unblock; pool_attr->handler_func = _aio_handler; pool_attr->context_free = _aio_context_free; } pool_attr->handle = (void *)cb; /* prepare some priority list entries */ for (i = 0; i < _AIO_PRIO_LIST_LOW; i++) { plist = (struct _aio_prio_list *)malloc(sizeof(*plist)); if (!plist) { goto err_ret; } plist->next = cb->cb_plist_free; cb->cb_plist_free = plist; } cb->cb_nfree = _AIO_PRIO_LIST_LOW; /* prepare the context */ cb->ct_free = NULL; for (i = 0; i < pool_attr->maximum; i++) { if ((ctp = malloc(sizeof(*ctp))) == NULL) { goto err_ret; } ctp->next = cb->ct_free; cb->ct_free = ctp; } cb->tp = thread_pool_create(pool_attr, 0); if (cb->tp == NULL) { goto err_ret; } /* we can hook _aio_cb now */ _aio_cb = cb; /* start the pool with all sigal blocked */ if (sigfillset(&set) != 0 || (errno = pthread_sigmask(SIG_BLOCK, &set, &oset)) != EOK) { goto err_ret; } if (thread_pool_start(cb->tp) != EOK) { pthread_sigmask(SIG_SETMASK, &oset, NULL); goto err_ret; } pthread_sigmask(SIG_SETMASK, &oset, NULL); _mutex_unlock(&_aio_init_mutex); return 0; err_ret: _mutex_lock(&cb->cb_mutex); if (cb->tp) { (void)thread_pool_destroy(cb->tp); } while ((plist = cb->cb_plist_free)) { cb->cb_plist_free = plist->next; free(plist); } while ((ctp = cb->ct_free)) { cb->ct_free = ctp->next; free(ctp); } pthread_cond_destroy(&cb->cb_cond); pthread_mutex_destroy(&cb->cb_mutex); free(cb); _mutex_unlock(&_aio_init_mutex); return -1; }
int main(int argc, char **argv) { int result, nr_of_plugins, x; t_tissue_stack *t; char serv_command[20], load_command[150]; prctl(PR_SET_NAME, "TS_CORE"); // initialisation of some variable t = malloc(sizeof(*t)); init_prog(t); srand((unsigned) time(NULL)); // intitialisation the volume if (argc > 2) { if (argv[2] != NULL && strcmp(argv[2], "--prompt") != 0) { t->volume_first = malloc(sizeof(*t->volume_first)); if ((result = init_volume(t->memory_mappings, t->volume_first, argv[2])) != 0) return (result); } else if (argv[3] != NULL && strcmp(argv[3], "--prompt") != 0) { t->volume_first = malloc(sizeof(*t->volume_first)); if ((result = init_volume(t->memory_mappings, t->volume_first, argv[3])) != 0) return (result); } } else t->volume_first = NULL; // lunch thread_pool t->tp = malloc(sizeof(*t->tp)); thread_pool_init(t->tp, 16); nr_of_plugins = sizeof(PLUGINS) / sizeof(PLUGINS[0]); for (x = 0; x < nr_of_plugins; x++) { sprintf(load_command, "load %s %s/%s", PLUGINS[x][0], PLUGINS_PATH, PLUGINS[x][1]); plugin_load_from_string(load_command, t); DEBUG("Loading: %s\n", load_command); } sprintf(serv_command, "start serv %s", argv[1]); // start plugins (t->plug_actions)(t, serv_command, NULL); (t->plug_actions)(t, "start comm", NULL); task_clean_up(t); task_lunch(t); signal_manager(t); if ((argv[2] != NULL && strcmp(argv[2], "--prompt") == 0) || (argv[3] != NULL && strcmp(argv[3], "--prompt") == 0)) prompt_start(t); else { INFO("TissueStackImageServer Running!"); pthread_mutex_lock(&t->main_mutex); pthread_cond_wait(&t->main_cond, &t->main_mutex); pthread_mutex_unlock(&t->main_mutex); } // free all the stuff mallocked INFO("Shutting down TissueStackImageServer!"); t->tp->loop = 0; thread_pool_destroy(t->tp); free_core_struct(t); return (0); }