// 向网络线程组中指定的线程提交任务 // index - 指定网络线程的编号 // type - 提交的任务类型 // task - 提交的任务数据 // size - 任务数据的长度, 默认设置为0 int32_t iothreads_post( iothreads_t self, uint8_t index, int16_t type, void * task, uint8_t size ) { int16_t intype = eTaskType_Data; struct iothreads * iothreads = (struct iothreads *)(self); if ( index >= iothreads->nthreads ) { return -1; } if ( iothreads->runflags != 1 ) { return -2; } if ( size > TASK_PADDING_SIZE ) { intype = eTaskType_Null; } else if ( size == 0 ) { intype = eTaskType_User; } if ( intype != eTaskType_Null ) { return iothread_post( iothreads->threadgroup+index, intype, type, task, size ); } return -3; }
// 向网络线程组中指定的线程提交任务 // index - 指定网络线程的编号 // type - 提交的任务类型 // task - 提交的任务数据 // size - 任务数据的长度, 默认设置为0 int32_t iothreads_post( iothreads_t self, uint8_t index, int16_t type, void * task, uint8_t size ) { struct iothreads * iothreads = (struct iothreads *)(self); assert( size <= TASK_PADDING_SIZE ); assert( index < iothreads->nthreads ); if ( unlikely( iothreads->runflags != 1 ) ) { return -1; } return iothread_post( iothreads->threads+index, (size == 0 ? eTaskType_User : eTaskType_Data), type, task, size ); }
void iothreads_stop( iothreads_t self ) { uint8_t i = 0; struct iothreads * iothreads = (struct iothreads *)(self); // 向所有线程发送停止命令 iothreads->runflags = 0; for ( i = 0; i < iothreads->nthreads; ++i ) { iothread_post( iothreads->threadgroup+i, eTaskType_Null, 0, NULL, 0 ); } // 等待线程退出 pthread_mutex_lock( &iothreads->lock ); while ( iothreads->nrunthreads > 0 ) { pthread_cond_wait( &iothreads->cond, &iothreads->lock ); } pthread_mutex_unlock( &iothreads->lock ); // 销毁所有网络线程 for ( i = 0; i < iothreads->nthreads; ++i ) { iothread_stop( iothreads->threadgroup + i ); } pthread_cond_destroy( &iothreads->cond ); pthread_mutex_destroy( &iothreads->lock ); if ( iothreads->threadgroup ) { free( iothreads->threadgroup ); iothreads->threadgroup = NULL; } free ( iothreads ); return; }