DaoQueue* DaoQueue_New( DaoType *type, int capacity ) { DaoQueue *res = (DaoQueue*)dao_malloc( sizeof(DaoQueue) ); DaoCstruct_Init( (DaoCstruct*)res, type ); res->head = res->tail = NULL; res->size = 0; res->capacity = ( ( capacity < 0 )? 0 : capacity ); res->mtx = DaoMutex_New(); res->pushvar = DaoCondVar_New(); res->popvar = DaoCondVar_New(); DaoGC_IncRC( (DaoValue*)res->mtx ); DaoGC_IncRC( (DaoValue*)res->pushvar ); DaoGC_IncRC( (DaoValue*)res->popvar ); return res; }
static void DaoState_WaitFor2( DaoProcess *proc, DaoValue *p[], int N ) { DaoState *self = (DaoState*)DaoValue_CastCstruct( p[0], NULL ); int eq = 0, res = 1; DaoValue *state = p[1]; float timeout; DaoCondVar *condvar = NULL; DaoMutex_Lock( self->lock ); if( !DaoValue_Compare( self->state, state ) ) eq = 1; else{ condvar = (DaoCondVar*)DaoMap_GetValue( self->demands, state ); if( !condvar ){ condvar = DaoCondVar_New(); DaoMap_Insert( self->demands, state, (DaoValue*)condvar ); } } DaoMutex_Unlock( self->lock ); if( !eq ){ DaoMutex_Lock( self->defmtx ); timeout = DaoValue_TryGetFloat( p[2] ); if( timeout > 0 ) do res = !DaoCondVar_TimedWait( condvar, self->defmtx, timeout ); while( res && DaoValue_Compare( self->state, state ) ); else if( timeout == 0 ) res = 0; else do DaoCondVar_Wait( condvar, self->defmtx ); while( DaoValue_Compare( self->state, state ) ); DaoMutex_Unlock( self->defmtx ); } DaoProcess_PutInteger( proc, res ); }
/* Condition variable */ static void DaoCondV_Lib_CondVar( DaoProcess *proc, DaoValue *par[], int N ) { DaoProcess_PutValue( proc, (DaoValue*)DaoCondVar_New() ); }