00001 00007 /* Embedded XINU, Copyright (C) 2007. All rights reserved. */ 00008 00009 #include <kernel.h> 00010 #include <proc.h> 00011 #include <queue.h> 00012 #include <semaphore.h> 00013 00019 syscall wait(semaphore sem) 00020 { 00021 irqmask ps; 00022 struct sentry *psem; 00023 pcb *ppcb; 00024 00025 ps = disable(); /* disable interrupts */ 00026 if ( isbadsem(sem) ) /* safety check */ 00027 { 00028 restore(ps); 00029 return SYSERR; 00030 } 00031 ppcb = &proctab[currpid]; /* retrieve pcb from process table */ 00032 psem = &semtab[sem]; /* retrieve semaphore entry */ 00033 if( --(psem->count) < 0 ) /* if requested resource is unavailable */ 00034 { 00035 ppcb->state = PRWAIT; /* set process state to PRWAIT */ 00036 ppcb->sem = sem; /* record semaphore id in pcb */ 00037 enqueue(currpid, psem->queue); 00038 resched(); /* place in wait queue and reschedule */ 00039 } 00040 restore(ps); /* restore interrupts */ 00041 return OK; 00042 }