Apache Qpid - AMQP Messaging for Java JMS, C++, Python, Ruby, and .NET | Apache Qpid Documentation |
00001 #ifndef _sys_posix_Mutex_h 00002 #define _sys_posix_Mutex_h 00003 00004 /* 00005 * 00006 * Copyright (c) 2006 The Apache Software Foundation 00007 * 00008 * Licensed under the Apache License, Version 2.0 (the "License"); 00009 * you may not use this file except in compliance with the License. 00010 * You may obtain a copy of the License at 00011 * 00012 * http://www.apache.org/licenses/LICENSE-2.0 00013 * 00014 * Unless required by applicable law or agreed to in writing, software 00015 * distributed under the License is distributed on an "AS IS" BASIS, 00016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00017 * See the License for the specific language governing permissions and 00018 * limitations under the License. 00019 * 00020 */ 00021 00022 #include "qpid/sys/posix/check.h" 00023 00024 #include <pthread.h> 00025 #include <boost/noncopyable.hpp> 00026 00027 namespace qpid { 00028 namespace sys { 00029 00030 class Condition; 00031 00035 class Mutex : private boost::noncopyable { 00036 friend class Condition; 00037 static const pthread_mutexattr_t* getAttribute(); 00038 00039 public: 00040 typedef ::qpid::sys::ScopedLock<Mutex> ScopedLock; 00041 typedef ::qpid::sys::ScopedUnlock<Mutex> ScopedUnlock; 00042 00043 inline Mutex(); 00044 inline ~Mutex(); 00045 inline void lock(); 00046 inline void unlock(); 00047 inline bool trylock(); 00048 00049 00050 protected: 00051 pthread_mutex_t mutex; 00052 }; 00053 00057 class RWlock : private boost::noncopyable { 00058 friend class Condition; 00059 00060 public: 00061 typedef ::qpid::sys::ScopedRlock<RWlock> ScopedRlock; 00062 typedef ::qpid::sys::ScopedWlock<RWlock> ScopedWlock; 00063 00064 inline RWlock(); 00065 inline ~RWlock(); 00066 inline void wlock(); // will write-lock 00067 inline void rlock(); // will read-lock 00068 inline void unlock(); 00069 inline void trywlock(); // will write-try 00070 inline void tryrlock(); // will read-try 00071 00072 protected: 00073 pthread_rwlock_t rwlock; 00074 }; 00075 00076 00081 struct PODMutex 00082 { 00083 typedef ::qpid::sys::ScopedLock<PODMutex> ScopedLock; 00084 00085 inline void lock(); 00086 inline void unlock(); 00087 inline bool trylock(); 00088 00089 // Must be public to be a POD: 00090 pthread_mutex_t mutex; 00091 }; 00092 00093 #define QPID_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER } 00094 00095 void PODMutex::lock() { 00096 QPID_POSIX_ASSERT_THROW_IF(pthread_mutex_lock(&mutex)); 00097 } 00098 00099 void PODMutex::unlock() { 00100 QPID_POSIX_ASSERT_THROW_IF(pthread_mutex_unlock(&mutex)); 00101 } 00102 00103 bool PODMutex::trylock() { 00104 return pthread_mutex_trylock(&mutex) == 0; 00105 } 00106 00107 Mutex::Mutex() { 00108 QPID_POSIX_ASSERT_THROW_IF(pthread_mutex_init(&mutex, getAttribute())); 00109 } 00110 00111 Mutex::~Mutex(){ 00112 QPID_POSIX_ABORT_IF(pthread_mutex_destroy(&mutex)); 00113 } 00114 00115 void Mutex::lock() { 00116 QPID_POSIX_ASSERT_THROW_IF(pthread_mutex_lock(&mutex)); 00117 } 00118 00119 void Mutex::unlock() { 00120 QPID_POSIX_ASSERT_THROW_IF(pthread_mutex_unlock(&mutex)); 00121 } 00122 00123 bool Mutex::trylock() { 00124 return pthread_mutex_trylock(&mutex) == 0; 00125 } 00126 00127 00128 RWlock::RWlock() { 00129 QPID_POSIX_ASSERT_THROW_IF(pthread_rwlock_init(&rwlock, NULL)); 00130 } 00131 00132 RWlock::~RWlock(){ 00133 QPID_POSIX_ABORT_IF(pthread_rwlock_destroy(&rwlock)); 00134 } 00135 00136 void RWlock::wlock() { 00137 QPID_POSIX_ASSERT_THROW_IF(pthread_rwlock_wrlock(&rwlock)); 00138 } 00139 00140 void RWlock::rlock() { 00141 QPID_POSIX_ASSERT_THROW_IF(pthread_rwlock_rdlock(&rwlock)); 00142 } 00143 00144 void RWlock::unlock() { 00145 QPID_POSIX_ASSERT_THROW_IF(pthread_rwlock_unlock(&rwlock)); 00146 } 00147 00148 void RWlock::trywlock() { 00149 QPID_POSIX_ASSERT_THROW_IF(pthread_rwlock_trywrlock(&rwlock)); 00150 } 00151 00152 void RWlock::tryrlock() { 00153 QPID_POSIX_ASSERT_THROW_IF(pthread_rwlock_tryrdlock(&rwlock)); 00154 } 00155 00156 00157 }} 00158 #endif