static void DaoCallServer_Delete( DaoCallServer *self ) { daoint i; for(i=0; i<self->threads->size; i++){ DaoCallThread_Delete( (DaoCallThread*)self->threads->items.pVoid[i] ); } for(i=0; i<self->caches->size; ++i){ DaoTaskEvent_Delete( (DaoTaskEvent*) self->caches->items.pVoid[i] ); } DList_Delete( self->threads ); DList_Delete( self->functions ); DList_Delete( self->parameters ); DList_Delete( self->owners ); DList_Delete( self->events ); DList_Delete( self->events2 ); DList_Delete( self->caches ); DMap_Delete( self->waitings ); DMap_Delete( self->pending ); DMap_Delete( self->active ); DMutex_Destroy( & self->mutex ); DCondVar_Destroy( & self->condv ); DCondVar_Destroy( & self->condv2 ); DThread_Destroy( & self->timer ); dao_free( self ); }
void DaoCallServer_Stop() { DCondVar condv; DaoCallThread *calth; if( daoCallServer == NULL ) return; DCondVar_Init( & condv ); daoCallServer->finishing = 1; calth = DaoCallThread_New( NULL, NULL ); DMutex_Lock( & daoCallServer->mutex ); daoCallServer->total += 1; DMutex_Unlock( & daoCallServer->mutex ); DaoCallThread_Run( calth ); /* process tasks in the main thread; */ DMutex_Lock( & daoCallServer->mutex ); while( daoCallServer->stopped != daoCallServer->total || daoCallServer->timing ){ DCondVar_TimedWait( & condv, & daoCallServer->mutex, 0.01 ); } DMutex_Unlock( & daoCallServer->mutex ); DCondVar_Destroy( & condv ); DaoCallThread_Delete( calth ); DaoCallServer_Delete( daoCallServer ); daoCallServer = NULL; }
void DThread_Destroy( DThread *self ) { if( self->myThread ) CloseHandle( self->myThread ); DCondVar_Destroy( & self->condv ); GlobalFree( self->thdSpecData ); self->thdSpecData = NULL; }
void DaoVmSpace_StopTasklets( DaoVmSpace *self ) { DaoTaskletServer *server = (DaoTaskletServer*) self->taskletServer; DaoTaskletThread *taskthd; DCondVar condv; if( server == NULL ) return; DCondVar_Init( & condv ); server->finishing = 1; taskthd = DaoTaskletThread_New( server, NULL, NULL ); taskthd->thdData = DThread_GetCurrent(); DMutex_Lock( & server->mutex ); server->total += 1; DMutex_Unlock( & server->mutex ); DaoTaskletThread_Run( taskthd ); /* process tasks in the main thread; */ DMutex_Lock( & server->mutex ); while( server->stopped != server->total || server->timing ){ DCondVar_TimedWait( & condv, & server->mutex, 0.01 ); } DMutex_Unlock( & server->mutex ); DCondVar_Destroy( & condv ); DaoTaskletThread_Delete( taskthd ); DaoTaskletServer_Delete( server ); self->taskletServer = NULL; }
static void SYS_Sleep( DaoProcess *proc, DaoValue *p[], int N ) { #ifdef DAO_WITH_THREAD DMutex mutex; DCondVar condv; #endif double s = p[0]->xFloat.value; if( s < 0 ){ DaoProcess_RaiseException( proc, DAO_WARNING_VALUE, "expecting positive value" ); return; } #ifdef DAO_WITH_THREAD /* sleep only the current thread: */ DMutex_Init( & mutex ); DCondVar_Init( & condv ); DMutex_Lock( & mutex ); DCondVar_TimedWait( & condv, & mutex, s ); DMutex_Unlock( & mutex ); DMutex_Destroy( & mutex ); DCondVar_Destroy( & condv ); #elif UNIX sleep( (int)s ); /* This may cause the whole process to sleep. */ #else Sleep( s * 1000 ); #endif }
void DaoCallServer_Join() { DCondVar condv; if( daoCallServer == NULL ) return; DCondVar_Init( & condv ); DMutex_Lock( & daoCallServer->mutex ); while( daoCallServer->pending->size || daoCallServer->vacant != daoCallServer->total ){ DCondVar_TimedWait( & condv, & daoCallServer->mutex, 0.01 ); } DMutex_Unlock( & daoCallServer->mutex ); DCondVar_Destroy( & condv ); }
void DaoVmSpace_JoinTasklets( DaoVmSpace *self ) { DaoTaskletServer *server = (DaoTaskletServer*) self->taskletServer; DCondVar condv; if( server == NULL ) return; DCondVar_Init( & condv ); DMutex_Lock( & server->mutex ); while( server->pending->size || server->vacant != server->total ){ DCondVar_TimedWait( & condv, & server->mutex, 0.01 ); } DMutex_Unlock( & server->mutex ); DCondVar_Destroy( & condv ); }
void DaoCallServer_Stop() { DCondVar condv; if( daoCallServer == NULL ) return; DCondVar_Init( & condv ); daoCallServer->finishing = 1; DMutex_Lock( & daoCallServer->mutex ); while( daoCallServer->pending->size || daoCallServer->idle != daoCallServer->total ){ /* printf( "finalizing: %3i %3i\n", daoCallServer->idle, daoCallServer->total ); */ DCondVar_TimedWait( & condv, & daoCallServer->mutex, 0.01 ); } while( daoCallServer->stopped != daoCallServer->total || daoCallServer->timing ){ DCondVar_TimedWait( & condv, & daoCallServer->mutex, 0.01 ); } DMutex_Unlock( & daoCallServer->mutex ); DCondVar_Destroy( & condv ); DaoCallServer_Delete( daoCallServer ); daoCallServer = NULL; }
static void DaoMT_Functional( DaoProcess *proc, DaoValue *P[], int N, int F ) { DMutex mutex; DCondVar condv; DaoTaskData *tasks; DaoValue *param = P[0]; DaoValue *result = NULL; DaoList *list = NULL; DaoArray *array = NULL; DaoVmCode *sect = DaoGetSectionCode( proc->activeCode ); int i, entry, threads = P[1]->xInteger.value; daoint index = -1, status = 0, joined = 0; DNode *node = NULL; switch( F ){ case DVM_FUNCT_MAP : if( param->type == DAO_ARRAY ){ array = DaoProcess_PutArray( proc ); result = (DaoValue*) array; }else{ list = DaoProcess_PutList( proc ); result = (DaoValue*) list; } break; case DVM_FUNCT_APPLY : DaoProcess_PutValue( proc, param ); break; case DVM_FUNCT_FIND : DaoProcess_PutValue( proc, dao_none_value ); break; } if( threads <= 0 ) threads = 2; if( sect == NULL || DaoMT_PushSectionFrame( proc ) == 0 ) return; if( list ){ DArray_Clear( & list->items ); if( param->type == DAO_LIST ) DArray_Resize( & list->items, param->xList.items.size, NULL ); if( param->type == DAO_MAP ) DArray_Resize( & list->items, param->xMap.items->size, NULL ); #ifdef DAO_WITH_NUMARRAY }else if( array && F == DVM_FUNCT_MAP ){ DaoArray_GetSliceShape( (DaoArray*) param, & array->dims, & array->ndim ); DaoArray_ResizeArray( array, array->dims, array->ndim ); #endif } DMutex_Init( & mutex ); DCondVar_Init( & condv ); entry = proc->topFrame->entry; tasks = (DaoTaskData*) dao_calloc( threads, sizeof(DaoTaskData) ); DaoProcess_PopFrame( proc ); for(i=0; i<threads; i++){ DaoTaskData *task = tasks + i; task->param = param; task->result = result; task->proto = proc; task->sect = sect; task->funct = F; task->entry = entry; task->first = i; task->step = threads; task->index = & index; task->node = & node; task->joined = & joined; task->condv = & condv; task->mutex = & mutex; task->clone = DaoVmSpace_AcquireProcess( proc->vmSpace ); task->clone->mutex = & mutex; if( i ) DaoCallServer_AddTask( DaoMT_RunFunctional, task, 1 ); } DaoMT_RunFunctional( tasks ); DMutex_Lock( & mutex ); while( joined < threads ) DCondVar_TimedWait( & condv, & mutex, 0.01 ); DMutex_Unlock( & mutex ); for(i=0; i<threads; i++){ DaoTaskData *task = tasks + i; DaoVmSpace_ReleaseProcess( proc->vmSpace, task->clone ); status |= task->status; } if( F == DVM_FUNCT_FIND ){ DaoTuple *tuple = DaoProcess_PutTuple( proc, 0 ); if( param->type == DAO_LIST && index != -1 ){ DaoValue **items = param->xList.items.items.pValue; GC_ShiftRC( items[index], tuple->items[1] ); tuple->items[1] = items[index]; tuple->items[0]->xInteger.value = index; }else if( param->type == DAO_MAP && node ){ GC_ShiftRC( node->key.pValue, tuple->items[0] ); GC_ShiftRC( node->value.pValue, tuple->items[1] ); tuple->items[0] = node->key.pValue; tuple->items[1] = node->value.pValue; } } if( status ) DaoProcess_RaiseException( proc, DAO_ERROR, "code section execution failed!" ); DMutex_Destroy( & mutex ); DCondVar_Destroy( & condv ); dao_free( tasks ); }
void DaoCondVar_Delete( DaoCondVar *self ) { DaoCstruct_Free( (DaoCstruct*) self ); DCondVar_Destroy( & self->myCondVar ); dao_free( self ); }
void DThread_Destroy( DThread *self ) { DCondVar_Destroy( & self->condv ); }
void DThread_Destroy( DThread *self ) { DMutex_Destroy( & self->mutex ); DCondVar_Destroy( & self->condv ); pthread_setspecific( thdSpecKey, NULL ); }