int main(void) { unsigned int i = 0; unsigned int j = 0; char msgbuf[STRINGBUFLEN]; int numthreads = NUM_THREADS; int tc = 0; int tj = 0; pthread_t thread_id[MAX_THREADS]; Ports th_data[MAX_THREADS]; pthread_mutex_init(&msg_mutex, NULL); for(i=0; i<MAX_THREADS; ++i) { init_thread_state(&th_data[i], i); } snprintf(msgbuf, STRINGBUFLEN, "Number of processors: %d", get_numprocs()); log_msg(msgbuf); snprintf(th_data[SENSOR_LISTENER].host_listen_port, PORT_STRING_LEN , "%d", FC_LISTEN_PORT_IMU_A); snprintf(th_data[SENSOR_LISTENER].client_addr , INET6_ADDRSTRLEN, "%s", IMU_A_IP_ADDR_STRING); snprintf(th_data[SENSOR_LISTENER].client_port , PORT_STRING_LEN , "%d", IMU_A_LISTEN_PORT); // snprintf(th_data[RNET_LISTENER].host_listen_port, PORT_STRING_LEN , "%d", FC_LISTEN_PORT_RNET_A); // snprintf(th_data[RNET_LISTENER].client_addr , INET6_ADDRSTRLEN, "%s", RNET_A_IP_ADDR_STRING); // snprintf(th_data[RNET_LISTENER].client_port , PORT_STRING_LEN , "%d", RNET_A_LISTEN_PORT); printf("start threads\n"); for(i=0; i<numthreads; ++i) { tc = pthread_create( &thread_id[i], NULL, &datap_io_thread, (void*) &th_data[i] ); if (tc){ printf("== Error=> pthread_create() fail with code: %d\n", tc); exit(EXIT_FAILURE); } } // Things happen...then join threads as they return. for(j=0; j<numthreads; ++j) { tj = pthread_join(thread_id[j], NULL); if (tj){ printf("== Error=> pthread_join() fail with code: %d\n", tj); exit(EXIT_FAILURE); } } pthread_mutex_destroy(&msg_mutex); pthread_exit(NULL); return 0; }
struct thread_pool *thread_pool_create(int count, struct packet_module *packet_module, bool attach_debugger, bool dissector_graph) { int i; struct thread_pool *pool; assert(count > 0); engine_prepare(count); pool = malloc(sizeof(struct thread_pool)); if (!pool) { error("memory error"); return NULL; } memset(pool, 0, sizeof(struct thread_pool)); pool->threads = malloc(sizeof(struct thread_state*)*count); if (!pool) { error("memory error"); thread_pool_cleanup(pool); return NULL; } memset(pool->threads, 0, sizeof(struct thread_state*)*count); pool->count = count; pool->single = count == 1; pool->stop = false; if (!barrier_init(&pool->thread_sync, count+1)) { thread_pool_cleanup(pool); return NULL; } if (!barrier_init(&pool->thread_start_sync, 2)) { thread_pool_cleanup(pool); return NULL; } if (attach_debugger) { thread_pool_attachdebugger(pool); } for (i=0; i<count; ++i) { pool->threads[i] = init_thread_state(packet_module, i, dissector_graph); if (!pool->threads[i]) { error("thread initialization error"); thread_pool_cleanup(pool); return NULL; } pool->threads[i]->pool = pool; if (pool->single) { if (!init_thread_lua_state(pool->threads[i])) { error("thread initialization error"); thread_pool_cleanup(pool); return NULL; } } else { if (!thread_create(&pool->threads[i]->thread, thread_main_loop, pool->threads[i])) { thread_pool_cleanup(pool); return NULL; } pool->threads[i]->state = STATE_RUNNING; if (!barrier_wait(&pool->thread_start_sync)) { thread_pool_cleanup(pool); return NULL; } if (pool->threads[i]->state == STATE_ERROR) { error("thread initialization error"); thread_pool_cleanup(pool); return NULL; } } } return pool; }
int main(void) { unsigned int i = 0; unsigned int j = 0; char msgbuf[STRINGBUFLEN]; int numthreads = NUM_THREADS; int tc = 0; int tj = 0; pthread_t thread_id[MAX_THREADS]; Ports th_data[MAX_THREADS]; Usertalk th_talk; pthread_mutex_init(&msg_mutex, NULL); pthread_mutex_init(&exit_request_mutex, NULL); pthread_mutex_init(&log_enable_mutex, NULL); if(!host_ip_setup()) { die_nice("host ip setup"); } /* USER IO THREAD */ th_talk.thread_id = 33; snprintf(th_talk.host_listen_port, PORT_STRING_LEN , "%d", FC_LISTEN_PORT_IMU_A); snprintf(th_talk.client_a_addr , INET6_ADDRSTRLEN, "%s", IMU_A_IP_ADDR_STRING); snprintf(th_talk.client_a_port , PORT_STRING_LEN , "%d", IMU_A_LISTEN_PORT); snprintf(th_talk.client_b_addr , INET6_ADDRSTRLEN, "%s", ROLL_CTL_IP_ADDR_STRING); snprintf(th_talk.client_b_port , PORT_STRING_LEN , "%d", ROLL_CTL_LISTEN_PORT); tc = pthread_create( &thread_id[i], NULL, &user_io_thread, &th_talk ); if (tc){ printf("== Error=> pthread_create() fail with code: %d\n", tc); exit(EXIT_FAILURE); } for(i=0; i<MAX_THREADS; ++i) { init_thread_state(&th_data[i], i); } snprintf(msgbuf, STRINGBUFLEN, "Number of processors: %d", get_numprocs()); log_msg(msgbuf); snprintf(th_data[ADIS_LISTENER].host_listen_port, PORT_STRING_LEN , "%d", FC_LISTEN_PORT_IMU_A); snprintf(th_data[ADIS_LISTENER].client_addr , INET6_ADDRSTRLEN, "%s", IMU_A_IP_ADDR_STRING); snprintf(th_data[ADIS_LISTENER].client_port , PORT_STRING_LEN , "%d", IMU_A_TX_PORT_ADIS); snprintf(th_data[MPL_LISTENER].host_listen_port, PORT_STRING_LEN , "%d", FC_LISTEN_PORT_IMU_A); snprintf(th_data[MPL_LISTENER].client_addr , INET6_ADDRSTRLEN, "%s", IMU_A_IP_ADDR_STRING); snprintf(th_data[MPL_LISTENER].client_port , PORT_STRING_LEN , "%d", IMU_A_TX_PORT_MPL); snprintf(th_data[MPU_LISTENER].host_listen_port, PORT_STRING_LEN , "%d", FC_LISTEN_PORT_IMU_A); snprintf(th_data[MPU_LISTENER].client_addr , INET6_ADDRSTRLEN, "%s", IMU_A_IP_ADDR_STRING); snprintf(th_data[MPU_LISTENER].client_port , PORT_STRING_LEN , "%d", IMU_A_TX_PORT_MPU); for(i=0; i<numthreads; ++i) { tc = pthread_create( &thread_id[i], NULL, &datap_io_thread, (void*) &th_data[i] ); if (tc){ printf("== Error=> pthread_create() fail with code: %d\n", tc); exit(EXIT_FAILURE); } } // Things happen...then join threads as they return. for(j=0; j<numthreads; ++j) { tj = pthread_join(thread_id[j], NULL); if (tj){ printf("== Error=> pthread_join() fail with code: %d\n", tj); exit(EXIT_FAILURE); } } pthread_mutex_destroy(&msg_mutex); pthread_mutex_destroy(&exit_request_mutex); close(hostsocket_fd); pthread_exit(NULL); return 0; }