Apache Qpid - AMQP Messaging for Java JMS, C++, Python, Ruby, and .NET | Apache Qpid Documentation |
00001 #ifndef _sys_Mutex_h 00002 #define _sys_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 namespace qpid { 00023 namespace sys { 00024 00029 template <class L> 00030 class ScopedLock 00031 { 00032 public: 00033 ScopedLock(L& l) : mutex(l) { l.lock(); } 00034 ~ScopedLock() { mutex.unlock(); } 00035 private: 00036 L& mutex; 00037 }; 00038 00039 template <class L> 00040 class ScopedUnlock 00041 { 00042 public: 00043 ScopedUnlock(L& l) : mutex(l) { l.unlock(); } 00044 ~ScopedUnlock() { mutex.lock(); } 00045 private: 00046 L& mutex; 00047 }; 00048 00049 template <class L> 00050 class ScopedRlock 00051 { 00052 public: 00053 ScopedRlock(L& l) : mutex(l) { l.rlock(); } 00054 ~ScopedRlock() { mutex.unlock(); } 00055 private: 00056 L& mutex; 00057 }; 00058 00059 template <class L> 00060 class ScopedWlock 00061 { 00062 public: 00063 ScopedWlock(L& l) : mutex(l) { l.wlock(); } 00064 ~ScopedWlock() { mutex.unlock(); } 00065 private: 00066 L& mutex; 00067 }; 00068 00069 template <class L> 00070 class ConditionalScopedLock 00071 { 00072 public: 00073 ConditionalScopedLock(L& l) : mutex(l) { acquired = l.trylock(); } 00074 ~ConditionalScopedLock() { if (acquired) mutex.unlock(); } 00075 bool lockAcquired() { return acquired; } 00076 private: 00077 L& mutex; 00078 bool acquired; 00079 }; 00080 00081 }} 00082 00083 #ifdef USE_APR_PLATFORM 00084 #include "apr/Mutex.h" 00085 #elif defined (_WIN32) 00086 #include "windows/Mutex.h" 00087 #else 00088 #include "posix/Mutex.h" 00089 #endif 00090 00091 #endif