/* screate.c - screate, newsem */ #include <conf.h> #include <kernel.h> #include <proc.h> #include <q.h> #include <sem.h> /*------------------------------------------------------------------------ * screate -- create and initialize a semaphore, returning its id *------------------------------------------------------------------------ */ SYSCALL screate(count) int count; /* initial count (>=0) */ { char ps; int sem; disable(ps); if ( count<0 || (sem=newsem())==SYSERR ) { restore(ps); return(SYSERR); } semaph[sem].semcnt = count; /* sqhead and sqtail were initialized at system startup */ restore(ps); return(sem); } /*------------------------------------------------------------------------ * newsem -- allocate an unused semaphore and return its index *------------------------------------------------------------------------ */ LOCAL newsem() { int sem; int i; for (i=0 ; i<NSEM ; i++) { sem=nextsem--; if (nextsem < 0) nextsem = NSEM-1; if (semaph[sem].sstate==SFREE) { semaph[sem].sstate = SUSED; return(sem); } } return(SYSERR); }