00001 00007 /* Embedded XINU, Copyright (C) 2007. All rights reserved. */ 00008 00009 #include <kernel.h> 00010 #include <semaphore.h> 00011 00012 static semaphore allocsem(void); 00013 00020 semaphore newsem(short count) 00021 { 00022 irqmask ps; 00023 semaphore sem; 00024 00025 ps = disable(); /* disable interrupts */ 00026 sem = allocsem(); /* request new semaphore */ 00027 if ( sem != SYSERR && count >= 0 ) /* safety check */ 00028 { 00029 semtab[sem].count = count; /* initialize count */ 00030 restore(ps); /* restore interrupts */ 00031 return sem; /* return semaphore id */ 00032 } 00033 00034 restore(ps); 00035 return SYSERR; 00036 } 00037 00044 static semaphore allocsem(void) 00045 { 00046 int i = 0; 00047 while(i < NSEM) /* loop through semaphore table */ 00048 { /* to find SFREE semaphore */ 00049 if( semtab[i].state == SFREE ) 00050 { 00051 semtab[i].state = SUSED; 00052 return i; 00053 } 00054 i++; 00055 } 00056 return SYSERR; 00057 }