static DaoCallServer* DaoCallServer_New( DaoVmSpace *vms ) { DaoCallServer *self = (DaoCallServer*)dao_malloc( sizeof(DaoCallServer) ); DMutex_Init( & self->mutex ); DCondVar_Init( & self->condv ); DCondVar_Init( & self->condv2 ); DThread_Init( & self->timer ); self->finishing = 0; self->timing = 0; self->total = 0; self->vacant = 0; self->idle = 0; self->stopped = 0; self->threads = DList_New(0); self->functions = DList_New(0); self->parameters = DList_New(0); self->owners = DList_New(0); self->events = DList_New(0); self->events2 = DList_New(0); self->waitings = DMap_New( DAO_DATA_COMPLEX, 0 ); self->pending = DHash_New(0,0); self->active = DHash_New(0,0); self->caches = DList_New(0); self->vmspace = vms; self->timestamp.real = 0.0; self->timestamp.imag = 0.0; return self; }
DaoMutex* DaoMutex_New() { DaoMutex* self = (DaoMutex*) dao_calloc( 1, sizeof(DaoMutex) ); DaoCstruct_Init( (DaoCstruct*) self, dao_type_mutex ); DMutex_Init( & self->myMutex ); return self; }
static DaoCallServer* DaoCallServer_New( DaoVmSpace *vms ) { DaoComplex com = {DAO_COMPLEX,0,0,0,1,{0.0,0.0}}; DaoCallServer *self = (DaoCallServer*)dao_malloc( sizeof(DaoCallServer) ); DMutex_Init( & self->mutex ); DCondVar_Init( & self->condv ); DCondVar_Init( & self->condv2 ); DThread_Init( & self->timer ); self->finishing = 0; self->timing = 0; self->total = 0; self->idle = 0; self->stopped = 0; self->threads = DArray_New(0); self->functions = DArray_New(0); self->parameters = DArray_New(0); self->events = DArray_New(0); self->events2 = DArray_New(0); self->waitings = DMap_New(D_VALUE,0); self->pending = DHash_New(0,0); self->active = DHash_New(0,0); self->caches = DArray_New(0); self->vmspace = vms; self->timestamp = com; return self; }
void DCondVar_Init( DCondVar *self ) { self->thdWaiting = DArray_New(0); DMutex_Init( & self->thdMutex ); /* manual reset, when signaled, all waiting threads will be waked up: */ self->myCondVar = CreateEvent( NULL, TRUE, FALSE, 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 }
DAO_DLL int DaoBase_OnLoad( DaoVmSpace *vmSpace, DaoNamespace *ns ) { DaoNamespace *testns = DaoNamespace_GetNamespace( ns, "test" ); DaoNamespace *basens = DaoNamespace_GetNamespace( testns, "base" ); DaoNamespace_WrapFunctions( testns, testFuncs ); DaoNamespace_WrapFunctions( basens, baseFuncs ); DMutex_Init( &test_mtx ); return 0; }
DaoxProfiler* DaoxProfiler_New() { DaoxProfiler *self = (DaoxProfiler*) dao_calloc(1,sizeof(DaoxProfiler)); self->profile = DHash_New( 0, DAO_DATA_MAP ); self->one = DHash_New( 0, DAO_DATA_VALUE ); self->base.EnterFrame = DaoProfiler_EnterFrame; self->base.LeaveFrame = DaoProfiler_LeaveFrame; self->base.Report = DaoProfiler_Report; self->base.Reset = DaoProfiler_Reset; DMutex_Init( & self->mutex ); return self; }
void DThread_Init( DThread *self ) { self->running = 0; self->state = 0; self->vmpause = 0; self->vmpaused = 0; self->vmstop = 0; self->vmstopped = 0; self->myThread = 0; self->thdSpecData = NULL; self->cleaner = NULL; self->taskFunc = NULL; self->taskArg = NULL; DCondVar_Init( & self->condv ); DMutex_Init( & self->mutex ); }
static DMutex* DaoMT_GetMutex( DaoRoutine *routine, void *key ) { DNode *it; DMap *mutexes; DMutex *mutex = NULL; DMutex_Lock( & mainVmSpace->miscMutex ); it = DMap_Find( routine->body->aux, DaoMT_RoutMutexSet ); if( it == NULL ) it = DMap_Insert( routine->body->aux, DaoMT_RoutMutexSet, DHash_New(0,0) ); mutexes = (DMap*) it->value.pVoid; it = DMap_Find( mutexes, key ); if( it == NULL ){ mutex = (DMutex*) dao_calloc(1,sizeof(DMutex)); DMutex_Init( mutex ); DMap_Insert( mutexes, key, mutex ); }else{ mutex = (DMutex*) it->value.pVoid; } DMutex_Unlock( & mainVmSpace->miscMutex ); return mutex; }
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 ); }