Fawkes API  Fawkes Development Version
scoped_rwlock.h
1 
2 /***************************************************************************
3  * scoped_rwlock.h - Scoped read/write lock
4  *
5  * Created: Mon Jan 10 11:42:56 2011
6  * Copyright 2006-2010 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #ifndef __CORE_THREADING_SCOPED_RWLOCK_H_
25 #define __CORE_THREADING_SCOPED_RWLOCK_H_
26 
27 #include <core/utils/refptr.h>
28 
29 namespace fawkes {
30 
31 class ReadWriteLock;
32 
34 {
35  public:
36  /** What to lock for. */
37  typedef enum {
38  LOCK_WRITE, ///< Lock for writing
39  LOCK_READ ///< Lock for reading
40  } LockType;
41 
42  ScopedRWLock(RefPtr<ReadWriteLock> rwlock, LockType lock_type = LOCK_WRITE,
43  bool initially_lock = true);
44  ScopedRWLock(ReadWriteLock *rwlock, LockType lock_type = LOCK_WRITE,
45  bool initially_lock = true);
46  ~ScopedRWLock();
47 
48  void relock();
49  void unlock();
50 
51  private:
52  LockType __lock_type;
53  bool __locked;
54  RefPtr<ReadWriteLock> __refrwlock;
55  ReadWriteLock *__rawrwlock;
56 };
57 
58 
59 } // end namespace fawkes
60 
61 #endif
Fawkes library namespace.
Scoped read/write lock.
Definition: scoped_rwlock.h:33
ScopedRWLock(RefPtr< ReadWriteLock > rwlock, LockType lock_type=LOCK_WRITE, bool initially_lock=true)
Constructor.
Read/write lock to allow multiple readers but only a single writer on the resource at a time...
void relock()
Lock this rwlock, again.
LockType
What to lock for.
Definition: scoped_rwlock.h:37
RefPtr<> is a reference-counting shared smartpointer.
Definition: refptr.h:49
~ScopedRWLock()
Destructor.
void unlock()
Unlock the rwlock.