Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

/ray/src/lib/threads/tsemaphore.h

Go to the documentation of this file.
00001 /*
00002  * lib/threads/tsemaphore.h
00003  * 
00004  * Implementing semaphore class for thread synchronisation. 
00005  * 
00006  * Copyright (c) 2004 by Wolfgang Wieser ] wwieser (a) gmx <*> de [ 
00007  * 
00008  * This file may be distributed and/or modified under the terms of the 
00009  * GNU General Public License version 2 as published by the Free Software 
00010  * Foundation. (See COPYING.GPL for details.)
00011  * 
00012  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00013  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00014  * 
00015  */
00016 
00017 #ifndef _ThreadLibrary_Semaphore_H_
00018 #define _ThreadLibrary_Semaphore_H_ 1
00019 
00030 #include <lib/sconfig.h>    /* MUST be first */
00031 
00032 /* Can we use our own pthreads-based implementation? This should be faster.*/
00033 #if USE_PRIVATE__POSIX_SEMAPHORES
00034 #include <errno.h>   /* for EGAGIN */
00035 #include <semaphore.h>
00036 #else
00037 #include <lib/threads/mutex.h>   /* emulate semaphore using mutex */
00038 #include <lib/threads/condition.h>   /* emulate semaphore using mutex */
00039 #endif  /* USE_PRIVATE__POSIX_SEMAPHORES */
00040 
00041 
00051 class /*_packed_*/ Semaphore
00052 {
00053     private:
00054 #if USE_PRIVATE__POSIX_SEMAPHORES
00055         sem_t s;
00056 #else
00057         ConditionMutex m;
00058         WaitCondition c;
00059         volatile int v;    
00060 #endif
00061         
00062     public:
00064         inline Semaphore(int initial_value=0)
00065 #if USE_PRIVATE__POSIX_SEMAPHORES
00066             {  sem_init(&s,0,initial_value);  }
00067 #else
00068             : m(),c(),v(initial_value)  { }
00069 #endif
00070         
00073         inline ~Semaphore()
00074 #if USE_PRIVATE__POSIX_SEMAPHORES
00075             {  sem_destroy(&s);  }
00076 #else
00077             {  /* empty */  }
00078 #endif
00079         
00081         /*inline*/ int value()  // inline deliberately commented out (otherwise inlining forced) (WW)
00082 #if USE_PRIVATE__POSIX_SEMAPHORES
00083             {  int val;  sem_getvalue(&s,&val);  return(val);  }
00084 #else
00085             {  m.lock();  int val=v;  m.unlock();  return(val);  }
00086 #endif
00087         
00089 #if USE_PRIVATE__POSIX_SEMAPHORES
00090         inline void post()  {  sem_post(&s);  }
00091 #else
00092         void post();  // external
00093 #endif
00094         
00097 #if USE_PRIVATE__POSIX_SEMAPHORES
00098         inline void wait()  {  sem_wait(&s);  }
00099 #else
00100         void wait();  // external
00101 #endif
00102         
00105 #if USE_PRIVATE__POSIX_SEMAPHORES
00106         inline bool test()  {  return(sem_trywait(&s)==EAGAIN);  }
00107 #else
00108         bool test();  // external
00109 #endif
00110         
00111 };
00112 
00113 #endif  /* _ThreadLibrary_Semaphore_H_ */

Generated on Sat Feb 19 22:33:46 2005 for Ray by doxygen 1.3.5