Apache Qpid - AMQP Messaging for Java JMS, C++, Python, Ruby, and .NET Apache Qpid Documentation
Mutex.h
Go to the documentation of this file.
1 #ifndef _sys_windows_Mutex_h
2 #define _sys_windows_Mutex_h
3 
4 /*
5  *
6  * Copyright (c) 2008 The Apache Software Foundation
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 
22 #include "qpid/sys/windows/check.h"
23 
24 #include <boost/version.hpp>
25 #if (BOOST_VERSION < 103500)
26 #error The Windows port requires Boost version 1.35.0 or later
27 #endif
28 
29 #include <boost/noncopyable.hpp>
30 #include <boost/thread/recursive_mutex.hpp>
31 #include <boost/thread/shared_mutex.hpp>
32 #include <boost/thread/thread_time.hpp>
33 #include <boost/thread/tss.hpp>
34 
35 namespace qpid {
36 namespace sys {
37 
38 class Condition;
39 
43 class Mutex : private boost::noncopyable {
44  friend class Condition;
45 
46 public:
47  typedef ::qpid::sys::ScopedLock<Mutex> ScopedLock;
48  typedef ::qpid::sys::ScopedUnlock<Mutex> ScopedUnlock;
49 
50  inline Mutex();
51  inline ~Mutex();
52  inline void lock();
53  inline void unlock();
54  inline bool trylock();
55 
56 
57 protected:
58  boost::recursive_mutex mutex;
59 };
60 
64 class RWlock : private boost::noncopyable {
65  friend class Condition;
66 
67 public:
68  typedef ::qpid::sys::ScopedRlock<RWlock> ScopedRlock;
69  typedef ::qpid::sys::ScopedWlock<RWlock> ScopedWlock;
70 
71  inline RWlock();
72  inline ~RWlock();
73  inline void wlock(); // will write-lock
74  inline void rlock(); // will read-lock
75  inline void unlock();
76  inline void trywlock(); // will write-try
77  inline void tryrlock(); // will read-try
78 
79 protected:
80  boost::shared_mutex rwMutex;
81  boost::thread_specific_ptr<bool> haveWrite;
82 
83  inline bool &write (void);
84 };
85 
86 
91 struct PODMutex
92 {
93  typedef ::qpid::sys::ScopedLock<PODMutex> ScopedLock;
94 
95  inline void lock();
96  inline void unlock();
97  inline bool trylock();
98 
99  // Must be public to be a POD:
100  boost::recursive_mutex mutex;
101 };
102 
103 #define QPID_MUTEX_INITIALIZER 0
104 
105 void PODMutex::lock() {
106  mutex.lock();
107 }
108 
109 void PODMutex::unlock() {
110  mutex.unlock();
111 }
112 
113 bool PODMutex::trylock() {
114  return mutex.try_lock();
115 }
116 
117 Mutex::Mutex() {
118 }
119 
120 Mutex::~Mutex(){
121 }
122 
123 void Mutex::lock() {
124  mutex.lock();
125 }
126 
127 void Mutex::unlock() {
128  mutex.unlock();
129 }
130 
131 bool Mutex::trylock() {
132  return mutex.try_lock();
133 }
134 
135 
136 RWlock::RWlock() {
137 }
138 
140 }
141 
142 void RWlock::wlock() {
143  bool &writer = write();
144  rwMutex.lock();
145  writer = true; // Remember this thread has write lock held.
146 }
147 
148 void RWlock::rlock() {
149  bool &writer = write();
150  rwMutex.lock_shared();
151  writer = false; // Remember this thread has shared lock held.
152 }
153 
154 void RWlock::unlock() {
155  bool &writer = write();
156  if (writer)
157  rwMutex.unlock();
158  else
159  rwMutex.unlock_shared();
160 }
161 
162 void RWlock::trywlock() {
163  bool &writer = write();
164  // shared_mutex::try_lock() seems to not be available... emulate it with
165  // a timed lock().
166  boost::system_time now = boost::get_system_time();
167  if (rwMutex.timed_lock(now))
168  writer = true;
169 }
170 
171 void RWlock::tryrlock() {
172  bool &writer = write();
173  if (rwMutex.try_lock_shared())
174  writer = false;
175 }
176 
177 bool & RWlock::write (void) {
178  // Accessing thread-specific and stack-local info, so no locks needed.
179  bool *writePtr = haveWrite.get();
180  if (writePtr == 0) {
181  writePtr = new bool(false);
182  haveWrite.reset(writePtr);
183  }
184  return *writePtr;
185 }
186 
187 }}
188 #endif
bool trylock()
Definition: Mutex.h:103
::qpid::sys::ScopedLock< Mutex > ScopedLock
Definition: Mutex.h:47
friend class Condition
Definition: Mutex.h:36
::qpid::sys::ScopedUnlock< Mutex > ScopedUnlock
Definition: Mutex.h:48
void unlock()
Definition: Mutex.h:119
void unlock()
Definition: Mutex.h:99
void tryrlock()
Definition: Mutex.h:152
void wlock()
Definition: Mutex.h:136
void lock()
Definition: Mutex.h:115
AbsTime now()
Definition: Time.h:130
pthread_mutex_t mutex
Definition: Mutex.h:90
A condition variable for thread synchronization.
Definition: Condition.h:40
RW lock.
Definition: Mutex.h:57
void rlock()
Definition: Mutex.h:140
::qpid::sys::ScopedLock< PODMutex > ScopedLock
Definition: Mutex.h:93
pthread_mutex_t mutex
Definition: Mutex.h:51
boost::recursive_mutex mutex
Definition: Mutex.h:100
void trywlock()
Definition: Mutex.h:148
void unlock()
Definition: Mutex.h:144
bool trylock()
Definition: Mutex.h:123
::qpid::sys::ScopedWlock< RWlock > ScopedWlock
Definition: Mutex.h:69
boost::thread_specific_ptr< bool > haveWrite
Definition: Mutex.h:81
PODMutex is a POD, can be static-initialized with PODMutex m = QPID_PODMUTEX_INITIALIZER.
Definition: Mutex.h:81
boost::recursive_mutex mutex
Definition: Mutex.h:58
bool & write(void)
Definition: Mutex.h:177
boost::shared_mutex rwMutex
Definition: Mutex.h:80
::qpid::sys::ScopedRlock< RWlock > ScopedRlock
Definition: Mutex.h:68

Qpid C++ API Reference
Generated on Mon Sep 22 2014 for Qpid C++ Client API by doxygen 1.8.7