00001 /*************************************************************************** 00002 LOW_thread_mutex_POSIX.h - description 00003 ------------------- 00004 begin : Fri Oct 3 2003 00005 copyright : (C) 2003 by Harald Roelle 00006 email : roelle@informatik.uni-muenchen.de 00007 ***************************************************************************/ 00008 00009 /*************************************************************************** 00010 * * 00011 * This program is free software; you can redistribute it and/or modify * 00012 * it under the terms of the GNU General Public License as published by * 00013 * the Free Software Foundation; either version 2 of the License, or * 00014 * (at your option) any later version. * 00015 * * 00016 ***************************************************************************/ 00017 00018 #ifndef LOW_THREAD_MUTEX_POSIX_H 00019 #define LOW_THREAD_MUTEX_POSIX_H 00020 00021 00022 #include "LOW_thread_mutex.h" 00023 00024 00025 #include <pthread.h> 00026 00027 00028 00029 /** Platform specific LOW_thread_mutex for POSIX platforms. 00030 00031 This class is thread-safe. 00032 00033 @see LOW_thread_mutex 00034 @see IEEE Standard 1003.1-2003 (http://www.opengroup.org/onlinepubs/007904975/toc.htm) 00035 00036 @author Harald Roelle 00037 @author Parts of the documentation taken from Linux man pages by Xavier Leroy. 00038 @author Parts of the documentation taken from IEEE Standard 1003.1-2003. 00039 */ 00040 class LOW_thread_mutex_POSIX : public LOW_thread_mutex { 00041 00042 //======================================================================================= 00043 public: 00044 00045 //===================================================================================== 00046 // 00047 // constructors 00048 // 00049 00050 /** Destructor. 00051 @throw thread_mutex_busy The mutex is still locked. 00052 @throw thread_mutex_error Any other error. 00053 */ 00054 virtual ~LOW_thread_mutex_POSIX(); 00055 00056 00057 //===================================================================================== 00058 // 00059 // methods 00060 // 00061 00062 /** Obtain a lock on the mutex (blocking). 00063 00064 Blocks the calling thread if another thread has already locked the mutex. 00065 00066 If the mutex is already locked by the calling thread, behaviour depends on the mutex kind: 00067 00068 - <B>mutexKind_fast:</B> The calling thread is suspended until the mutex is unlocked, 00069 thus effectively causing the calling thread to deadlock. 00070 - <B>mutexKind_recursive:</B> The call succeeds and returns immediately, recording 00071 the number of times the calling thread has locked the mutex. An equal number of 00072 unlock() calls must be performed before the mutex returns to the unlocked state. 00073 - <B>mutexKind_errorCheck:</B> A thread_mutex_locked exception is thrown. 00074 00075 @throw thread_mutex_locked Calling thread has already locked the mutex and 00076 mutex kind is mutexKind_errorCheck. 00077 @throw thread_mutex_error Any other error. 00078 */ 00079 virtual void lock(); 00080 00081 00082 /** Obtain a lock on the mutex (non-blocking). 00083 00084 Behaves identically to lock(), except that it does not block the calling thread 00085 if the mutex is already locked by another thread. 00086 Instead an thread_mutex_busy exception is thrown. 00087 00088 @throw thread_mutex_busy The mutex is already locked by another thread. 00089 @throw thread_mutex_locked Calling thread has already locked the mutex and 00090 mutex kind is mutexKind_errorCheck. 00091 @throw thread_mutex_error Any other error. 00092 */ 00093 virtual void tryLock(); 00094 00095 00096 /** Release a lock on the mutex. 00097 Behaviour depends on the mutex kind: 00098 00099 - <B>mutexKind_fast:</B> The mutex is assumed to be locked and owned by the calling 00100 thread on entrance and always returns it to the unlocked state. 00101 - <B>mutexKind_recursive:</B> The mutex is assumed to be locked and owned by the calling 00102 thread on entrance. Decrements the locking count of the mutex (number of 00103 lock() calls performed on it by the calling thread), and only when this count reaches 00104 zero is the mutex actually unlocked. 00105 - <B>mutexKind_errorCheck:</B> Actually checks at run-time that the mutex is locked on 00106 entrance, and that it was locked by the same thread that is now calling. 00107 00108 @throw thread_mutex_busy The mutex is already by another thread. 00109 @throw thread_mutex_error Any other error. 00110 */ 00111 virtual void unlock(); 00112 00113 00114 //======================================================================================= 00115 protected: 00116 00117 //===================================================================================== 00118 // 00119 // friend classes 00120 // 00121 00122 friend class LOW_thread_Factory; /**< To allow construction. */ 00123 00124 00125 //===================================================================================== 00126 // 00127 // constructors 00128 // 00129 00130 /** Constructor. 00131 Not publicly constructable. Use LOW_thread_Factory. 00132 @param inMutexKind Kind of the new mutex. 00133 @throw thread_mutex_error Any error. 00134 */ 00135 LOW_thread_mutex_POSIX( const mutexKind_t inMutexKind); 00136 00137 00138 //======================================================================================= 00139 private: 00140 00141 //===================================================================================== 00142 // 00143 // attributes 00144 // 00145 00146 pthread_mutexattr_t mutexAttr; /**< Initial mutex attributes. */ 00147 pthread_mutex_t theMutex; /**< The mutex itself. */ 00148 00149 }; 00150 00151 #endif