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