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); }
int V(int sid) { struct Semaphore* sema = (&s_semaphoreList)->head; struct Mutex* mutex; struct Condition* cond; //find sem by sid while (sema != 0) { if (sema->sid == sid) { mutex = &(sema->mutex); cond = &(sema->cond); Enable_Interrupts(); Mutex_Lock(mutex); sema->count = sema->count + 1; //Print("wait queue : %d", cond->waitQueue); Cond_Broadcast(cond); Mutex_Unlock(mutex); Disable_Interrupts(); return 0; } sema = Get_Next_In_Semaphore_List(sema); } return -1; }
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; }
/* * Release given buffer. */ int Release_FS_Buffer(struct FS_Buffer_Cache *cache, struct FS_Buffer *buf) { int rc = 0; KASSERT(buf->flags & FS_BUFFER_INUSE); Mutex_Lock(&cache->lock); /* * If the buffer is OK to release, * mark it as no longer in use and notify any * thread waiting to use it. */ if (rc == 0) { buf->flags &= ~(FS_BUFFER_INUSE); Cond_Broadcast(&cache->cond); } Debug("Released block %lu\n", buf->fsBlockNum); Mutex_Unlock(&cache->lock); return rc; }