libcommonc++  0.7
ScopedReadWriteLock.h++
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------------
2  commonc++ - A C++ Common Class Library
3  Copyright (C) 2005-2014 Mark A Lindner
4 
5  This file is part of commonc++.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public
18  License along with this library; if not, write to the Free
19  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  ---------------------------------------------------------------------------
21 */
22 
23 #ifndef __ccxx_ScopedReadWriteLock_hxx
24 #define __ccxx_ScopedReadWriteLock_hxx
25 
26 #include <commonc++/Common.h++>
28 
29 namespace ccxx {
30 
50 class /* COMMONCPP_API */ ScopedReadLock
51 {
52  public:
53 
59  : _rwlock(rwlock), _once(false)
60  {
61  _rwlock.lockRead();
62  }
63 
66  {
67  _rwlock.unlock();
68  }
69 
71  inline bool testOnce()
72  { bool f = _once; _once = false; return(f); }
75  private:
76 
77  ReadWriteLock& _rwlock;
78  bool _once;
79 
80  CCXX_COPY_DECLS(ScopedReadLock);
81 };
82 
102 class /* COMMONCPP_API */ ScopedWriteLock
103 {
104  public:
105 
111  : _rwlock(rwlock), _once(true)
112  {
113  _rwlock.lockWrite();
114  }
115 
118  {
119  _rwlock.unlock();
120  }
121 
123  inline bool testOnce()
124  { bool f = _once; _once = false; return(f); }
127  private:
128 
129  ReadWriteLock& _rwlock;
130  bool _once;
131 
133 };
134 
135 } // namespace ccxx
136 
141 #if (defined CCXX_OS_WINDOWS) && (defined _MSC_VER) && (_MSC_VER < 1400)
142 
143 #define synchronized_read(LOCK) \
144  for(commoncpp::ScopedReadLock LOCK ## _locker(LOCK); \
145  LOCK ## _locker.testOnce();)
146 
147 #else
148 
149 #define synchronized_read(LOCK) \
150  for(commoncpp::ScopedReadLock \
151  CCXX_OS_UNIQUE_VARNAME(LOCK, __LINE__, locker)(LOCK); \
152  CCXX_OS_UNIQUE_VARNAME(LOCK, __LINE__, locker).testOnce();)
153 
154 #endif
155 
160 #if (defined CCXX_OS_WINDOWS) && (defined _MSC_VER) && (_MSC_VER < 1400)
161 
162 #define synchronized_write(LOCK) \
163  for(commoncpp::ScopedWriteLock LOCK ## _locker(LOCK); \
164  LOCK ## _locker.testOnce();)
165 
166 #else
167 
168 #define synchronized_write(LOCK) \
169  for(commoncpp::ScopedWriteLock \
170  CCXX_OS_UNIQUE_VARNAME(LOCK, __LINE__, locker)(LOCK); \
171  CCXX_OS_UNIQUE_VARNAME(LOCK, __LINE__, locker).testOnce();)
172 
173 #endif
174 
175 #endif // __ccxx_ScopedReadWriteLock_hxx
ScopedReadLock(ReadWriteLock &rwlock)
Construct a new ScopedReadLock for the given ReadWriteLock.
Definition: ScopedReadWriteLock.h++:58
void unlock()
Release the lock.
Definition: ReadWriteLock.c++:226
ScopedWriteLock(ReadWriteLock &rwlock)
Construct a new ScopedWriteLock for the given ReadWriteLock.
Definition: ScopedReadWriteLock.h++:110
~ScopedReadLock()
Destructor.
Definition: ScopedReadWriteLock.h++:65
A Read/Write lock – a synchronization primitive that allows multiple threads to coordinate access to...
Definition: ReadWriteLock.h++:50
A convenience object for lexical scope based synchronization.
Definition: ScopedReadWriteLock.h++:50
void lockRead()
Acquire a read lock, blocking until the lock is acquired.
Definition: ReadWriteLock.c++:67
A convenience object for lexical scope based synchronization.
Definition: ScopedReadWriteLock.h++:102
#define CCXX_COPY_DECLS(CLASS)
Inlines declarations of a copy constructor and assignment operator for the class CLASS.
Definition: Common.h++:295
Definition: AllocationMap.c++:25
~ScopedWriteLock()
Destructor.
Definition: ScopedReadWriteLock.h++:117