Exiv2
rwlock.hpp
1 // ***************************************************************** -*- C++ -*-
2 /*
3  * Copyright (C) 2004-2018 Exiv2 authors
4  * This program is part of the Exiv2 distribution.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
19  */
20 
21 #ifndef RW_LOCK_HPP
22 #define RW_LOCK_HPP
23 
24 #ifdef _MSC_VER
25 #include <windows.h>
26 #else
27 #include <pthread.h>
28 #endif
29 
30 namespace Exiv2 {
31 #ifdef _MSC_VER
32 
35  class RWLock
36  {
37  public:
38  RWLock()
39  {
40  InitializeCriticalSection(&lock_);
41  }
42 
43  ~RWLock()
44  {
45  DeleteCriticalSection(&lock_);
46  }
47 
48  void wrlock() { enter(); }
49 
50  bool trywrlock() { return tryenter(); }
51 
52  void rdlock() { enter(); }
53 
54  bool tryrdlock() { return tryenter(); }
55 
56  void rdunlock() { leave(); }
57 
58  void wrunlock() { leave(); }
59 
60  private:
61  void enter()
62  {
63  EnterCriticalSection(&lock_);
64  }
65 
66  void leave()
67  {
68  LeaveCriticalSection(&lock_);
69  }
70 
71  bool tryenter()
72  {
73 #if defined(MSDEV_2003) || defined(MSDEV_2005)
74  EnterCriticalSection(&lock_);
75  return true;
76 #else
77  return 0 != TryEnterCriticalSection(&lock_);
78 #endif
79  }
80 
81  private:
82  CRITICAL_SECTION lock_;
83  };
84 #else
85 
88  // UNIX systems (including MinGW and Cygwin)
89  class RWLock
90  {
91  public:
93  explicit RWLock(const pthread_rwlockattr_t *attr = 0)
94  {
95  pthread_rwlock_init(&rwlock_, attr);
96  }
97 
100  {
101  pthread_rwlock_destroy(&rwlock_);
102  }
103 
105  void wrlock()
106  {
107  pthread_rwlock_wrlock(&rwlock_);
108  }
109 
111  bool trywrlock()
112  {
113  return 0 == pthread_rwlock_trywrlock(&rwlock_);
114  }
115 
117  void rdlock()
118  {
119  pthread_rwlock_rdlock(&rwlock_);
120  }
121 
123  bool tryrdlock()
124  {
125  return 0 == pthread_rwlock_tryrdlock(&rwlock_);
126  }
127 
129  void unlock()
130  {
131  pthread_rwlock_unlock(&rwlock_);
132  }
133 
135  void rdunlock() { unlock(); }
136 
138  void wrunlock() { unlock(); }
139 
140  private:
142  pthread_rwlock_t rwlock_;
143  };
144 #endif
145 
151  {
152  public:
154  explicit ScopedReadLock(RWLock &rwlock):
155  rwlock_(rwlock)
156  {
157  rwlock_.rdlock();
158  }
159 
161  ~ScopedReadLock() { rwlock_.rdunlock(); }
162 
163  private:
165  RWLock &rwlock_;
166  };
167 
173  {
174  public:
176  explicit ScopedWriteLock(RWLock &rwlock):
177  rwlock_(rwlock)
178  {
179  rwlock_.wrlock();
180  }
181 
183  ~ScopedWriteLock() { rwlock_.wrunlock(); }
184 
185  private:
187  RWLock &rwlock_;
188  };
189 }
190 
191 #endif // RW_LOCK_HPP
RWLock(const pthread_rwlockattr_t *attr=0)
constructor (acquires the lock)
Definition: rwlock.hpp:93
ScopedReadLock(RWLock &rwlock)
constructor - locks the object
Definition: rwlock.hpp:154
void wrlock()
acquire rw lock
Definition: rwlock.hpp:105
ScopedWriteLock(RWLock &rwlock)
constructor - locks the object
Definition: rwlock.hpp:176
~ScopedReadLock()
destructor - unlocks the object used in constructor
Definition: rwlock.hpp:161
bool trywrlock()
test to see if the rw lock can be acquired
Definition: rwlock.hpp:111
Class to provide a Read-Write Lock.
Definition: rwlock.hpp:89
~ScopedWriteLock()
destructor - unlocks the object used in constructor
Definition: rwlock.hpp:183
void unlock()
release rw lock
Definition: rwlock.hpp:129
void rdlock()
acquire rd lock
Definition: rwlock.hpp:117
~RWLock()
constructor (releases lock)
Definition: rwlock.hpp:99
void wrunlock()
unlock rw lock
Definition: rwlock.hpp:138
Provides classes and functions to encode and decode Exif and Iptc data. The libexiv2 API consists of ...
Definition: asfvideo.hpp:36
bool tryrdlock()
test to see if the rd lock can be acquired
Definition: rwlock.hpp:123
void rdunlock()
unlock rd lock
Definition: rwlock.hpp:135
Class to provide a ScopedWriteLock. The lock is applied by the constructor and released by the destru...
Definition: rwlock.hpp:172
Class to provide a ScopedReadLock. The lock is applied by the constructor and released by the destruc...
Definition: rwlock.hpp:150