SP_IocpLFServer :: ~SP_IocpLFServer() { shutdown(); for( ; mIsRunning; ) sleep( 1 ); if( NULL != mThreadPool ) delete mThreadPool; mThreadPool = NULL; if( NULL != mCompletionHandler ) delete mCompletionHandler; mCompletionHandler = NULL; delete mAcceptArg->mHandlerFactory; free( mAcceptArg->mRefusedMsg ); free( mAcceptArg ); mAcceptArg = NULL; delete mEventArg; mEventArg = NULL; sp_thread_mutex_destroy( &mMutex ); mCompletionPort = NULL; }
sp_thread_result_t SP_THREAD_CALL wrapper_fn( void * arg ) { _thread * thread = (_thread*)arg; _threadpool * pool = (_threadpool*)thread->parent; for( ; 0 == ((_threadpool*)thread->parent)->tp_stop; ) { thread->fn( thread->arg ); if( 0 != ((_threadpool*)thread->parent)->tp_stop ) break; sp_thread_mutex_lock( &thread->mutex ); if( 0 == save_thread( thread->parent, thread ) ) { sp_thread_cond_wait( &thread->cond, &thread->mutex ); sp_thread_mutex_unlock( &thread->mutex ); } else { sp_thread_mutex_unlock( &thread->mutex ); sp_thread_cond_destroy( &thread->cond ); sp_thread_mutex_destroy( &thread->mutex ); free( thread ); break; } } sp_thread_mutex_lock( &pool->tp_mutex ); pool->tp_total--; if( pool->tp_total <= 0 ) sp_thread_cond_signal( &pool->tp_empty ); sp_thread_mutex_unlock( &pool->tp_mutex ); return 0; }
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; }
SP_CacheItem :: ~SP_CacheItem() { if( NULL != mKey ) free( mKey ); mKey = NULL; if( NULL != mDataBlock ) free( mDataBlock ); mDataBlock = NULL; sp_thread_mutex_destroy( &mMutex ); }
void destroy_threadpool(threadpool destroyme) { _threadpool *pool = (_threadpool *) destroyme; // add your code here to kill a threadpool int i = 0; sp_thread_mutex_lock( &pool->tp_mutex ); if( pool->tp_index < pool->tp_total ) { printf( "waiting for %d thread(s) to finish\n", pool->tp_total - pool->tp_index ); sp_thread_cond_wait( &pool->tp_full, &pool->tp_mutex ); } pool->tp_stop = 1; for( i = 0; i < pool->tp_index; i++ ) { _thread * thread = pool->tp_list[ i ]; sp_thread_mutex_lock( &thread->mutex ); sp_thread_cond_signal( &thread->cond ) ; sp_thread_mutex_unlock ( &thread->mutex ); } if( pool->tp_total > 0 ) { printf( "waiting for %d thread(s) to exit\n", pool->tp_total ); sp_thread_cond_wait( &pool->tp_empty, &pool->tp_mutex ); } for( i = 0; i < pool->tp_index; i++ ) { free( pool->tp_list[ i ] ); pool->tp_list[ i ] = NULL; } sp_thread_mutex_unlock( &pool->tp_mutex ); pool->tp_index = 0; sp_thread_mutex_destroy( &pool->tp_mutex ); sp_thread_cond_destroy( &pool->tp_idle ); sp_thread_cond_destroy( &pool->tp_full ); sp_thread_cond_destroy( &pool->tp_empty ); free( pool->tp_list ); free( pool ); }
SP_OnlineManager :: ~SP_OnlineManager() { sp_thread_mutex_destroy( &mMutex ); }
SP_BlockingQueue :: ~SP_BlockingQueue() { delete mQueue; sp_thread_mutex_destroy( &mMutex ); sp_thread_cond_destroy( &mCond ); }
SP_OnlineSidList :: ~SP_OnlineSidList() { sp_thread_mutex_destroy( &mMutex ); }