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
29namespace fawkes {
30
31class ReadWriteLock;
32
34{
35public:
36 /** What to lock for. */
37 typedef enum {
38 LOCK_WRITE, ///< Lock for writing
39 LOCK_READ ///< Lock for reading
41
43 LockType lock_type = LOCK_WRITE,
44 bool initially_lock = true);
45 ScopedRWLock(ReadWriteLock *rwlock, LockType lock_type = LOCK_WRITE, bool initially_lock = true);
47
48 void relock();
49 void unlock();
50
51private:
52 LockType lock_type_;
53 bool locked_;
54 RefPtr<ReadWriteLock> refrwlock_;
55 ReadWriteLock * rawrwlock_;
56};
57
58} // end namespace fawkes
59
60#endif
Read/write lock to allow multiple readers but only a single writer on the resource at a time.
RefPtr<> is a reference-counting shared smartpointer.
Definition: refptr.h:50
Scoped read/write lock.
Definition: scoped_rwlock.h:34
LockType
What to lock for.
Definition: scoped_rwlock.h:37
@ LOCK_READ
Lock for reading.
Definition: scoped_rwlock.h:39
@ LOCK_WRITE
Lock for writing.
Definition: scoped_rwlock.h:38
void relock()
Lock this rwlock, again.
ScopedRWLock(RefPtr< ReadWriteLock > rwlock, LockType lock_type=LOCK_WRITE, bool initially_lock=true)
Constructor.
void unlock()
Unlock the rwlock.
~ScopedRWLock()
Destructor.
Fawkes library namespace.