00001 /* src_experimental/kernel/seminl.h 00002 CubeOS Version 0.4.90 experimental 00003 Copyright (C) 1999,2000 Holger Kenn 00004 00005 CubeOS is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or any later version. 00009 00010 CubeOS is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 */ 00016 #ifndef _SEMINL_H 00017 #define _SEMINL_H 00018 00024 #ifdef MULTI_SEM 00025 00030 typedef struct sem_s { 00031 int init; 00032 int c; 00033 int task; 00034 } sem_t; 00035 00036 00037 00045 static inline void sem_post(sem_t x) 00046 { 00047 if (!x.init) return; 00048 disable(); 00049 x.c++; 00050 if (x.task!=NO_TASK) 00051 { 00052 wakeup(x.task); // this resets the task state, 00053 // but we're still in disable()d state! 00054 x.task=_KERN_ptable[x.task].next; 00055 } 00056 enable(); 00057 } 00058 00059 00060 #define sem_signal(x) sem_post(x) 00061 00062 00069 static inline void sem_wait(sem_t x) 00070 { 00071 if (!x.init) return; 00072 disable(); 00073 x.c--; 00074 if (x.c<0){ 00075 // put ourself to sleep 00076 if (x.task == NO_TASK) 00077 { 00078 _KERN_ptable[getpid()].next=NO_TASK; 00079 } 00080 else 00081 { 00082 _KERN_ptable[getpid()].next=x.task; 00083 } 00084 x.task=getpid(); 00085 enable(); 00086 KERN_suspend(getpid()); 00087 } 00088 else 00089 { 00090 enable(); // the semaphore was free 00091 } 00092 } 00093 00094 #define sem_trywait(x) (x.c<0) 00095 #define sem_getvalue(x,i) (*i=x.c) 00096 00097 00102 static inline int sem_init(sem_t x,int i,int v) 00103 { 00104 if (x.init) return (-1); 00105 disable(); 00106 x.c=v; 00107 x.task=NO_TASK; 00108 x.init=1; 00109 enable(); 00110 return (0); 00111 } 00112 00118 static inline int sem_destroy(sem_t x) 00119 { 00120 if (!x.init) return (-1); 00121 disable(); 00122 while(x.task!=NO_TASK) 00123 { 00124 wakeup(x.task); // this resets the task state, 00125 // but we're still in disable()d state! 00126 x.task=_KERN_ptable[x.task].next; 00127 } 00128 enable(); 00129 return(0); 00130 } 00131 #endif // of ifdef MULTI_SEM 00132 #endif // of ifndef _SEMINL_H