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

/ray/src/lib/threads/mutex.h

Go to the documentation of this file.
00001 /*
00002  * lib/threads/mutex.h
00003  * 
00004  * Implementing mutual exclusion classes 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_Mutex_H_
00018 #define _ThreadLibrary_Mutex_H_ 1
00019 
00026 #include <lib/sconfig.h>    /* MUST be first */
00027 
00028 /* Can we use our own pthreads-based implementation? This should be faster.*/
00029 #if USE_PRIVATE__PTHREADS
00030 # include <errno.h>   /* for EGAGIN */
00031 # include <pthread.h>
00032 #else
00033 # include <glib/gthread.h>
00034 #endif  /* USE_PRIVATE__PTHREADS */
00035 
00037 class WaitCondition;
00038 
00054 class /*_packed_*/ FastMutex
00055 {
00056     friend class WaitCondition;
00057     private:
00058 #if USE_PRIVATE__PTHREADS
00059         pthread_mutex_t m;
00060 #else
00061         GStaticMutex m;
00062 #endif
00063         
00064     public:
00066         FastMutex();
00067         
00070         inline ~FastMutex()
00071 #if USE_PRIVATE__PTHREADS
00072             {  pthread_mutex_destroy(&m);  }
00073 #else
00074             {  g_static_mutex_free(&m);  }
00075 #endif
00076         
00089         inline void lock()
00090 #if USE_PRIVATE__PTHREADS
00091             {  pthread_mutex_lock(&m);  }
00092 #else
00093             {  g_static_mutex_lock(&m);  }
00094 #endif
00095         
00102         inline bool test()
00103 #if USE_PRIVATE__PTHREADS
00104             {  return(pthread_mutex_trylock(&m)!=EBUSY);  }
00105 #else
00106             {  return(g_static_mutex_trylock(&m));  }
00107 #endif
00108         
00115         inline void unlock()
00116 #if USE_PRIVATE__PTHREADS
00117             {  pthread_mutex_unlock(&m);  }
00118 #else
00119             {  g_static_mutex_unlock(&m);  }
00120 #endif
00121         
00122 };
00123 
00124 
00129 class /*_packed_*/ RecursiveMutex
00130 {
00131     private:
00132 #if USE_PRIVATE__PTHREADS
00133         pthread_mutex_t m;
00134 #else
00135         GStaticRecMutex m;
00136 #endif
00137         
00138     public:
00140         RecursiveMutex();
00143         inline ~RecursiveMutex()
00144 #if USE_PRIVATE__PTHREADS
00145             {  pthread_mutex_destroy(&m);  }
00146 #else
00147             {  g_static_rec_mutex_free(&m);  }
00148 #endif
00149         
00163         inline void lock()
00164 #if USE_PRIVATE__PTHREADS
00165             {  pthread_mutex_lock(&m);  }
00166 #else
00167             {  g_static_rec_mutex_lock(&m);  }
00168 #endif
00169         
00175         inline bool test()
00176 #if USE_PRIVATE__PTHREADS
00177             {  return(pthread_mutex_trylock(&m)!=EBUSY);  }
00178 #else
00179             {  return(g_static_rec_mutex_trylock(&m));  }
00180 #endif
00181         
00190         inline void unlock()
00191 #if USE_PRIVATE__PTHREADS
00192             {  pthread_mutex_unlock(&m);  }
00193 #else
00194             {  g_static_rec_mutex_unlock(&m);  }
00195 #endif
00196         
00197 };
00198 
00199 
00200 #if USE_PRIVATE__PTHREADS
00201 typedef FastMutex ConditionMutex;   // This rocks!
00202 #else
00203 
00216 class ConditionMutex
00217 {
00218     friend class WaitCondition;
00219     private:
00220         GMutex *m;
00221         
00222     public:
00223         ConditionMutex()  {  m=g_mutex_new();  }
00224         ~ConditionMutex()  {  g_mutex_free(m);  }
00225         
00226         inline void lock()  {  g_mutex_lock(m);  }
00227         inline bool test()  {  return(g_mutex_trylock(m));  }
00228         inline void unlock()  {  g_mutex_unlock(m);  }
00229 };
00230 #endif  /* !USE_PRIVATE__PTHREADS */
00231 
00232 
00242 template<typename MUTEX>class MutexLock
00243 {
00244     private:
00245         MUTEX &m;
00246     
00247     public:
00249         inline MutexLock(MUTEX &mutex) : m(mutex)  {  m.lock();  }
00251         inline ~MutexLock()  {  m.unlock();  }
00252         
00254         inline MUTEX *operator->()  {  return(&m);  }
00255 };
00256 
00257 
00258 #endif  /* _ThreadLibrary_Semaphore_H_ */

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