int dispatch_threadpool(threadpool from_me, dispatch_fn dispatch_to_here, void *arg) { int ret = 0; _threadpool *pool = (_threadpool *) from_me; sp_thread_attr_t attr; _thread * thread = NULL; // add your code here to dispatch a thread sp_thread_mutex_lock( &pool->tp_mutex ); // printf("==========dispatch_threadpool\n"); while( pool->tp_index <= 0 && pool->tp_total >= pool->tp_max_index ) { //printf("all thread is busy!~~\n"); sp_thread_cond_wait( &pool->tp_idle, &pool->tp_mutex ); } if( pool->tp_index <= 0 ) { _thread * thread = ( _thread * )malloc( sizeof( _thread ) ); memset( &( thread->id ), 0, sizeof( thread->id ) ); sp_thread_mutex_init( &thread->mutex, NULL ); sp_thread_cond_init( &thread->cond, NULL ); thread->fn = dispatch_to_here; thread->arg = arg; thread->parent = pool; sp_thread_attr_init( &attr ); sp_thread_attr_setdetachstate( &attr, SP_THREAD_CREATE_DETACHED ); if( 0 == sp_thread_create( &thread->id, &attr, wrapper_fn, thread ) ) { pool->tp_total++; // printf( "create thread#%ld\n", thread->id ); } else { ret = -1; //printf( "cannot create thread\n" ); sp_thread_mutex_destroy( &thread->mutex ); sp_thread_cond_destroy( &thread->cond ); free( thread ); } } else { pool->tp_index--; thread = pool->tp_list[ pool->tp_index ]; pool->tp_list[ pool->tp_index ] = NULL; thread->fn = dispatch_to_here; thread->arg = arg; thread->parent = pool; sp_thread_mutex_lock( &thread->mutex ); sp_thread_cond_signal( &thread->cond ) ; sp_thread_mutex_unlock ( &thread->mutex ); } sp_thread_mutex_unlock( &pool->tp_mutex ); return ret; }
int SP_IocpServer :: run() { int ret = -1; sp_thread_attr_t attr; sp_thread_attr_init( &attr ); assert( sp_thread_attr_setstacksize( &attr, 1024 * 1024 ) == 0 ); sp_thread_attr_setdetachstate( &attr, SP_THREAD_CREATE_DETACHED ); sp_thread_t thread; ret = sp_thread_create( &thread, &attr, eventLoop, this ); sp_thread_attr_destroy( &attr ); if( 0 == ret ) { sp_syslog( LOG_NOTICE, "Thread #%ld has been created to listen on port [%d]", thread, mPort ); } else { mIsRunning = 0; sp_syslog( LOG_WARNING, "Unable to create a thread for TCP server on port [%d], %s", mPort, strerror( errno ) ) ; } return ret; }
int SP_IocpDispatcher :: dispatch() { int ret = -1; sp_thread_attr_t attr; sp_thread_attr_init( &attr ); assert( sp_thread_attr_setstacksize( &attr, 1024 * 1024 ) == 0 ); sp_thread_attr_setdetachstate( &attr, SP_THREAD_CREATE_DETACHED ); sp_thread_t thread; ret = sp_thread_create( &thread, &attr, eventLoop, this ); sp_thread_attr_destroy( &attr ); if( 0 == ret ) { sp_syslog( LOG_NOTICE, "Thread #%ld has been created for dispatcher", thread ); } else { mIsRunning = 0; sp_syslog( LOG_WARNING, "Unable to create a thread for dispatcher, %s", strerror( errno ) ) ; } return ret; }