void PC_Destroy(PC *pc) { Lock_Acquire(&pc->lock); assert(List_IsEmpty(&(pc->list))); Lock_Release(&pc->lock); }
void PC_Put(PC *pc, int color) { Lock_Acquire(&pc->lock); assert(color <= pc->maxColor); while(pc->capacity == pc->used){ pc->waitingP++; Cond_Wait(&pc->spaceAvail, &pc->lock); pc->waitingP--; } pc->used++; addItem(color, &(pc->list)); assert(((Item *)List_Last(&(pc->list)))->color == color); if(pc->used == 1){ /* * Went from empty to non-empty. Signal */ if(pc->maxColor == 0){ Cond_Signal(&pc->stuffAvail, &pc->lock); } else{ Cond_Broadcast(&pc->stuffAvail, &pc->lock); } } assert(((Item *)List_Last(&(pc->list)))->color == color); Lock_Release(&pc->lock); }
void Condition_Wait( Condition *c ) { /* x.P( ); */ xSemaphoreTake( c->x, portMAX_DELAY ); /* waiters++; */ { c->waiters++; } /* x.V( ); */ xSemaphoreGive( c->x ); /* m.Release() */ Lock_Release( c->m ); /* s.P( ); */ xSemaphoreTake( c->s, portMAX_DELAY ); /* h.V( ); */ xSemaphoreGive( c->h ); /* m.Acquire( ); */ Lock_Acquire( c->m ); }
_Return_type_success_(return == 0) int CachedLock_Init_Checked( _Out_ CachedLock* self, unsigned long flags, NitsCallSite cs ) { CachedLock_Pool* pool; int index; ReadWriteLock temp = READWRITELOCK_INITIALIZER; /* One-time initialization. Doesn't matter if called several times. */ if (s_cpuMask == CPU_MASK_UNINITIALIZED) InitializeCachePool(); if (flags & CACHEDLOCK_FLAG_SHARED) { Lock_Acquire(&s_latchPoolLock); if (NitsShouldFault(cs, NitsAutomatic)) return -1; if (s_currentPool == NULL || s_currentPool->mask == POOL_FULL_MASK) { /* The current pool is full. */ s_currentPool = Pool_New(); if (s_currentPool == NULL) return -1; } /* Search the pool for a zero bit. */ pool = s_currentPool; for (index = 0; index < POOL_LINES; index++) if ((pool->mask & ((ptrdiff_t)1 << index)) == 0) break; /* Take ownership of this index. */ pool->mask |= ((ptrdiff_t)1 << index); Lock_Release(&s_latchPoolLock); } else { pool = Pool_New(); if (pool == NULL) return -1; pool->mask = 1; index = 0; } self->pool = pool; self->latches = pool->latches + index; self->lock = temp; self->master = 0; return 0; }
void CachedLock_Destroy( _Inout_ CachedLock* self ) { CachedLock_Pool* pool = self->pool; ptrdiff_t index = self->latches - self->pool->latches; Lock_Acquire(&s_latchPoolLock); /* Release ownership of our bit. */ pool->mask &= ~((ptrdiff_t)1 << index); /* Determine if this pool has been orphaned. */ if (pool->mask == 0 && pool != s_currentPool) Pool_Delete(pool); Lock_Release(&s_latchPoolLock); }
static PyObject * enter(PyObject *self, PyObject *args) { PyObject *obj, *opname = NULL; SharedObject *shobj; /* Parse the arguments, which should be the object and the name of the operation to be performed */ if (!PyArg_ParseTuple(args, "O|S", &obj, &opname)) return NULL; shobj = SharedObject_FROM_PYOBJECT(obj); /* Lock the object's lock */ if (Lock_Acquire(&shobj->lock)) return NULL; Py_INCREF(Py_None); return Py_None; }
int PC_Get(PC *pc, int color) { Lock_Acquire(&pc->lock); assert(color <= pc->maxColor); while(List_IsEmpty(&(pc->list)) || (((Item *)List_First(&(pc->list)))->color != color)){ pc->waitingC++; Cond_Wait(&pc->stuffAvail, &pc->lock); pc->waitingC--; } removeItem(color, &(pc->list)); pc->used--; Cond_Signal(&pc->spaceAvail, &pc->lock); /* * We just took top item off -- another getter may be * able to proceed */ Cond_Broadcast(&pc->stuffAvail, &pc->lock); Lock_Release(&pc->lock); return color; }
void Condition_Lock( Condition *c ) { Lock_Acquire( c->m ); }