/usr/share/cruisecontrol-bin-2.6.1/projects/qpid-trunk/cpp/src/qpid/sys/apr/Mutex.h

00001 #ifndef _sys_apr_Mutex_h
00002 #define _sys_apr_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 "APRBase.h"
00023 #include "APRPool.h"
00024 
00025 #include <boost/noncopyable.hpp>
00026 #include <apr_thread_mutex.h>
00027 
00028 namespace qpid {
00029 namespace sys {
00030 
00031 class Condition;
00032 
00036 class Mutex : private boost::noncopyable {
00037   public:
00038     typedef ScopedLock<Mutex> ScopedLock;
00039     typedef ScopedUnlock<Mutex> ScopedUnlock;
00040     
00041     inline Mutex();
00042     inline ~Mutex();
00043     inline void lock();
00044     inline void unlock();
00045     inline bool trylock();
00046 
00047   protected:
00048     apr_thread_mutex_t* mutex;
00049   friend class Condition;
00050 };
00051 
00052 Mutex::Mutex() {
00053     CHECK_APR_SUCCESS(apr_thread_mutex_create(&mutex, APR_THREAD_MUTEX_NESTED, APRPool::get()));
00054 }
00055 
00056 Mutex::~Mutex(){
00057     CHECK_APR_SUCCESS(apr_thread_mutex_destroy(mutex));
00058 }
00059 
00060 void Mutex::lock() {
00061     CHECK_APR_SUCCESS(apr_thread_mutex_lock(mutex));
00062 }
00063 void Mutex::unlock() {
00064     CHECK_APR_SUCCESS(apr_thread_mutex_unlock(mutex));
00065 }
00066 
00067 bool Mutex::trylock() {
00068     return apr_thread_mutex_trylock(mutex) == 0;
00069 }
00070 
00071 
00075 class RWlock : private boost::noncopyable {
00076     friend class Condition;
00077 
00078 public:
00079     typedef ScopedRlock<RWlock> ScopedRlock;
00080     typedef ScopedWlock<RWlock> ScopedWlock;
00081     
00082     inline RWlock();
00083     inline ~RWlock();
00084     inline void wlock();  // will write-lock
00085     inline void rlock();  // will read-lock
00086     inline void unlock();
00087     inline bool trywlock();  // will write-try
00088     inline bool tryrlock();  // will read-try
00089 
00090   protected:
00091     apr_thread_mutex_t* mutex;
00092 };
00093 
00094 RWlock::RWlock() {
00095     CHECK_APR_SUCCESS(apr_thread_mutex_create(&mutex, APR_THREAD_MUTEX_NESTED, APRPool::get()));
00096 }
00097 
00098 RWlock::~RWlock(){
00099     CHECK_APR_SUCCESS(apr_thread_mutex_destroy(mutex));
00100 }
00101 
00102 void RWlock::wlock() {
00103     CHECK_APR_SUCCESS(apr_thread_mutex_lock(mutex));
00104 }
00105 
00106 void RWlock::rlock() {
00107     CHECK_APR_SUCCESS(apr_thread_mutex_lock(mutex));
00108 }
00109 
00110 void RWlock::unlock() {
00111     CHECK_APR_SUCCESS(apr_thread_mutex_unlock(mutex));
00112 }
00113 
00114 bool RWlock::trywlock() {
00115     return apr_thread_mutex_trylock(mutex) == 0;
00116 }
00117 
00118 bool RWlock::tryrlock() {
00119     return apr_thread_mutex_trylock(mutex) == 0;
00120 }
00121 
00122 
00123 }}
00124 #endif  

Generated on Thu Apr 10 11:08:18 2008 for Qpid by  doxygen 1.4.7