Fawkes API  Fawkes Development Version
read_write_lock.cpp
1 
2 /***************************************************************************
3  * read_write_lock.cpp - Read Write Lock
4  *
5  * Generated: Thu Sep 15 00:10:54 2006
6  * Copyright 2006 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 #include <core/threading/read_write_lock.h>
25 
26 #include <cstring>
27 #include <pthread.h>
28 
29 namespace fawkes {
30 
31 /// @cond INTERNALS
32 class ReadWriteLockData
33 {
34 public:
35  pthread_rwlock_t rwlock;
36 };
37 /// @endcond
38 
39 /** @class ReadWriteLock core/threading/read_write_lock.h
40  * Read/write lock to allow multiple readers but only a single writer
41  * on the resource at a time.
42  * This can be used if you have a value that only a few writers modify but
43  * several readers use. In this case the readers can read all at the same
44  * time as long as there is no writer modifying the value.
45  *
46  * @ingroup Threading
47  * @ingroup FCL
48  * @see example_rwlock.cpp
49  *
50  * @author Tim Niemueller
51  */
52 
53 /** Constructor
54  * @param policy The read/write lock policy to use. The default is to
55  * prefer writers.
56  */
58 {
59  rwlock_data = new ReadWriteLockData();
60 
61 #if defined __USE_UNIX98 || defined __USE_XOPEN2K
62  pthread_rwlockattr_t attr;
63  pthread_rwlockattr_init(&attr);
64 
65  switch (policy) {
67  pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
68  break;
70  pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_READER_NP);
71  break;
72  }
73 
74  pthread_rwlock_init(&(rwlock_data->rwlock), &attr);
75 #else
76  pthread_rwlock_init(&(rwlock_data->rwlock), NULL);
77 #endif
78 }
79 
80 /** Destructor */
82 {
83  pthread_rwlock_destroy(&(rwlock_data->rwlock));
84  delete rwlock_data;
85 }
86 
87 /** Aquire a reader lock.
88  * This will aquire the lock for reading. Multiple readers can aquire the
89  * lock at the same time. But never when a writer has the lock.
90  * This method will block until the lock has been aquired.
91  */
92 void
94 {
95  pthread_rwlock_rdlock(&(rwlock_data->rwlock));
96 }
97 
98 /** Aquire a writer lock.
99  * This will aquire the lock for writing. Only a single writer at a time
100  * will be allowed to aquire the lock.
101  * This method will block until the lock has been aquired.
102  */
103 void
105 {
106  pthread_rwlock_wrlock(&(rwlock_data->rwlock));
107 }
108 
109 /** Tries to aquire a reader lock.
110  * This will try to aquire the lock for reading. This will succeed if
111  * no writer has aquired the lock already. Multiple readers may aquire the
112  * lock.
113  * @return true, if the lock could be aquired, false otherwise.
114  */
115 bool
117 {
118  return (pthread_rwlock_tryrdlock(&(rwlock_data->rwlock)) == 0);
119 }
120 
121 /** Tries to aquire a writer lock.
122  * This will try to aquire the lock for writing. This will succeed if the
123  * read/write lock is currently unlocked. No other threads may hold this lock
124  * at the same time. Neither for writing nor for reading.
125  * @return true, if the lock has been aquired, false otherwise.
126  */
127 bool
129 {
130  return (pthread_rwlock_trywrlock(&(rwlock_data->rwlock)) == 0);
131 }
132 
133 /** Release the lock.
134  * Releases the lock, no matter whether it was locked for reading or writing.
135  */
136 void
138 {
139  pthread_rwlock_unlock(&(rwlock_data->rwlock));
140 }
141 
142 } // end namespace fawkes
bool try_lock_for_read()
Tries to aquire a reader lock.
void unlock()
Release the lock.
ReadWriteLockPolicy
The policy to use for the read/write lock.
@ RWLockPolicyPreferWriter
Prefer writers over readers.
@ RWLockPolicyPreferReader
Prefer readers over writers.
void lock_for_read()
Aquire a reader lock.
void lock_for_write()
Aquire a writer lock.
ReadWriteLock(ReadWriteLockPolicy policy=RWLockPolicyPreferWriter)
Constructor.
bool try_lock_for_write()
Tries to aquire a writer lock.
virtual ~ReadWriteLock()
Destructor.
Fawkes library namespace.