sem.c (1117B)
1 /* Copyright 1998-1999, Brian J. Swetland. All rights reserved. 2 ** Distributed under the terms of the OpenBLT License 3 */ 4 5 #include "kernel.h" 6 #include "resource.h" 7 #include "memory.h" 8 9 int sem_create(int count, const char *name) 10 { 11 sem_t *s = (sem_t *) kmalloc(sem_t); 12 s->count = count; 13 rsrc_bind(&s->rsrc, RSRC_SEM, current->rsrc.owner); 14 rsrc_set_name(&s->rsrc, name); 15 return s->rsrc.id; 16 } 17 18 int sem_destroy(int id) 19 { 20 sem_t *s; 21 if(!(s = rsrc_find_sem(id))) { 22 return ERR_RESOURCE; 23 } 24 rsrc_release(&s->rsrc); 25 kfree(sem_t,s); 26 return ERR_NONE; 27 } 28 29 int sem_acquire(int id) 30 { 31 int status; 32 sem_t *s; 33 34 if(!(s = rsrc_find_sem(id))) { 35 return ERR_RESOURCE; 36 } 37 38 if(s->count > 0 ){ 39 s->count--; 40 } else { 41 s->count--; 42 if(status = wait_on(&s->rsrc)) return status; 43 } 44 return ERR_NONE; 45 } 46 47 int sem_release(int id) 48 { 49 int x; 50 sem_t *s; 51 task_t *t; 52 53 if(!(s = rsrc_find_sem(id))) { 54 return ERR_RESOURCE; 55 } 56 57 s->count++; 58 59 if(t = rsrc_dequeue(&s->rsrc)){ 60 preempt(t,ERR_NONE); 61 } 62 63 return ERR_NONE; 64 }