UCommon
access.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
2 // Copyright (C) 2015 Cherokees of Idaho.
3 //
4 // This file is part of GNU uCommon C++.
5 //
6 // GNU uCommon C++ is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Lesser General Public License as published
8 // by the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // GNU uCommon C++ 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 Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
18 
32 // we do this twice because of some bizarre issue in just this file that
33 // otherwise breaks doxygen and lists all items outside the namespace...
34 #include <ucommon/platform.h>
35 
36 #ifndef _UCOMMON_ACCESS_H_
37 #define _UCOMMON_ACCESS_H_
38 
39 #ifndef _UCOMMON_CPR_H_
40 #include <ucommon/cpr.h>
41 #endif
42 
43 #ifndef _UCOMMON_PROTOCOLS_H_
44 #include <ucommon/protocols.h>
45 #endif
46 
47 namespace ucommon {
48 
54 class __EXPORT UnlockAccess
55 {
56 public:
57  virtual ~UnlockAccess();
58 
59 protected:
60  virtual void _unlock(void) = 0;
61 };
62 
69 class __EXPORT ExclusiveAccess : public UnlockAccess
70 {
71 protected:
72  virtual ~ExclusiveAccess();
73 
74  virtual void _lock(void) = 0;
75 
76 public:
80  inline void exclusive_lock(void) {
81  return _lock();
82  }
83 
87  inline void release_exclusive(void) {
88  return _unlock();
89  }
90 };
91 
98 class __EXPORT SharedAccess : protected UnlockAccess
99 {
100 protected:
101  virtual ~SharedAccess();
102 
106  virtual void _share(void) = 0;
107 
108 public:
115  virtual void share(void);
116 
124  virtual void exclusive(void);
125 
126  inline void shared_lock(void) {
127  return _share();
128  }
129 
130  inline void release_share(void) {
131  return _unlock();
132  }
133 };
134 
142 class __EXPORT exclusive_access
143 {
144 private:
146 
147  __DELETE_COPY(exclusive_access);
148 
149 public:
155 
159  ~exclusive_access();
160 
165  inline bool operator!() const {
166  return lock == NULL;
167  }
168 
173  inline operator bool() const {
174  return lock != NULL;
175  }
176 
182  void release(void);
183 };
184 
192 class __EXPORT shared_access
193 {
194 private:
196  int state;
197  bool modify;
198 
199 public:
204  shared_access(SharedAccess *object);
205 
207 
208  shared_access& operator=(const shared_access& copy);
209 
213  ~shared_access();
214 
219  inline bool operator!() const {
220  return lock == NULL;
221  }
222 
227  inline operator bool() const {
228  return lock != NULL;
229  }
230 
236  void release(void);
237 
241  void exclusive(void);
242 
246  void share(void);
247 };
248 
253 inline void lock(ExclusiveAccess& object)
254 {
255  object.exclusive_lock();
256 }
257 
262 inline void unlock(ExclusiveAccess& object)
263 {
264  object.release_exclusive();
265 }
266 
271 inline void access(SharedAccess& object)
272 {
273  object.shared_lock();
274 }
275 
280 inline void release(SharedAccess& object)
281 {
282  object.release_share();
283 }
284 
289 inline void exclusive(SharedAccess& object)
290 {
291  object.exclusive();
292 }
293 
298 inline void share(SharedAccess& object)
299 {
300  object.share();
301 }
302 
307 
312 
317 inline void release(exlock_t &reference)
318 {
319  reference.release();
320 }
321 
326 inline void release(shlock_t &reference)
327 {
328  reference.release();
329 }
330 
331 // Special macros to allow member functions of an object with a protocol
332 // to create self locking states while the member functions are called by
333 // placing an exclusive_lock or shared_lock smart object on their stack
334 // frame to reference their self.
335 
336 #define exclusive_object() exlock_t __autolock__ = this
337 #define protected_object() shlock_t __autolock__ = this
338 #define exclusive_locking(x) exlock_t __autolock__ = &x
339 #define protected_locking(x) shlock_t __autolock__ = &x
340 
341 } // namespace ucommon
342 
343 #endif
void access(SharedAccess &object)
Convenience function to access (lock) shared object through it's protocol.
Definition: access.h:271
void exclusive(SharedAccess &object)
Convenience function to exclusive lock shared object through it's protocol.
Definition: access.h:289
bool operator!() const
Test if the reference holds an active lock.
Definition: access.h:165
void release(SharedAccess &object)
Convenience function to unlock shared object through it's protocol.
Definition: access.h:280
A kind of smart pointer object to support exclusive locking protocol.
Definition: access.h:142
Various miscellaneous platform specific headers and defines.
void release(void)
Release a held lock programmatically.
An exclusive locking access interface base.
Definition: access.h:98
ObjectProtocol * copy(ObjectProtocol *object)
Convenience function to access object copy.
Definition: object.h:510
void modify(accesslock_t &lock)
Convenience function to exclusively schedule conditional access.
Definition: thread.h:1957
void exclusive_lock(void)
Access interface to exclusive lock the object.
Definition: access.h:80
exclusive_access exlock_t
Convenience type to use for object referencing an exclusive object.
Definition: access.h:306
shared_access shlock_t
Convenience type to use for object referencing a shared object.
Definition: access.h:311
Common unlock protocol for locking protocol interface classes.
Definition: access.h:54
Common namespace for all ucommon objects.
Definition: access.h:47
bool operator!() const
Test if the reference holds an active lock.
Definition: access.h:219
void lock(ExclusiveAccess &object)
Convenience function to exclusively lock an object through it's protocol.
Definition: access.h:253
void release_exclusive(void)
Access interface to release a lock.
Definition: access.h:87
An exclusive locking protocol interface base.
Definition: access.h:69
void unlock(ExclusiveAccess &object)
Convenience function to unlock an exclusive object through it's protocol.
Definition: access.h:262
Abstract interfaces and support.
void share(SharedAccess &object)
Convenience function to restore shared locking for object through it's protocol.
Definition: access.h:298
Runtime functions.
void release(void)
Release a held lock programmatically.
A kind of smart pointer object to support shared locking protocol.
Definition: access.h:192