BRE12
reader_writer_lock.h
1 /*
2  Copyright 2005-2016 Intel Corporation. All Rights Reserved.
3 
4  This file is part of Threading Building Blocks. Threading Building Blocks is free software;
5  you can redistribute it and/or modify it under the terms of the GNU General Public License
6  version 2 as published by the Free Software Foundation. Threading Building Blocks is
7  distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
8  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9  See the GNU General Public License for more details. You should have received a copy of
10  the GNU General Public License along with Threading Building Blocks; if not, write to the
11  Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
12 
13  As a special exception, you may use this file as part of a free software library without
14  restriction. Specifically, if other files instantiate templates or use macros or inline
15  functions from this file, or you compile this file and link it with other files to produce
16  an executable, this file does not by itself cause the resulting executable to be covered
17  by the GNU General Public License. This exception does not however invalidate any other
18  reasons why the executable file might be covered by the GNU General Public License.
19 */
20 
21 #ifndef __TBB_reader_writer_lock_H
22 #define __TBB_reader_writer_lock_H
23 
24 #include "tbb_thread.h"
25 #include "tbb_allocator.h"
26 #include "atomic.h"
27 
28 namespace tbb {
29 namespace interface5 {
31 
34  class reader_writer_lock : tbb::internal::no_copy {
35  public:
36  friend class scoped_lock;
37  friend class scoped_lock_read;
39 
74  enum status_t { waiting_nonblocking, waiting, active, invalid };
75 
78  internal_construct();
79  }
80 
83  internal_destroy();
84  }
85 
87 
89  class scoped_lock : tbb::internal::no_copy {
90  public:
91  friend class reader_writer_lock;
92 
95  internal_construct(lock);
96  }
97 
100  internal_destroy();
101  }
102 
103  void* operator new(size_t s) {
104  return tbb::internal::allocate_via_handler_v3(s);
105  }
106  void operator delete(void* p) {
107  tbb::internal::deallocate_via_handler_v3(p);
108  }
109 
110  private:
114  scoped_lock* next;
116  atomic<status_t> status;
117 
119  scoped_lock();
120 
121  void __TBB_EXPORTED_METHOD internal_construct(reader_writer_lock&);
122  void __TBB_EXPORTED_METHOD internal_destroy();
123  };
124 
126  class scoped_lock_read : tbb::internal::no_copy {
127  public:
128  friend class reader_writer_lock;
129 
132  internal_construct(lock);
133  }
134 
137  internal_destroy();
138  }
139 
140  void* operator new(size_t s) {
141  return tbb::internal::allocate_via_handler_v3(s);
142  }
143  void operator delete(void* p) {
144  tbb::internal::deallocate_via_handler_v3(p);
145  }
146 
147  private:
151  scoped_lock_read *next;
153  atomic<status_t> status;
154 
157 
158  void __TBB_EXPORTED_METHOD internal_construct(reader_writer_lock&);
159  void __TBB_EXPORTED_METHOD internal_destroy();
160  };
161 
163 
168  void __TBB_EXPORTED_METHOD lock();
169 
171 
175  bool __TBB_EXPORTED_METHOD try_lock();
176 
178 
182  void __TBB_EXPORTED_METHOD lock_read();
183 
185 
187  bool __TBB_EXPORTED_METHOD try_lock_read();
188 
190  void __TBB_EXPORTED_METHOD unlock();
191 
192  private:
193  void __TBB_EXPORTED_METHOD internal_construct();
194  void __TBB_EXPORTED_METHOD internal_destroy();
195 
197 
198  bool start_write(scoped_lock *);
200  void set_next_writer(scoped_lock *w);
202  void end_write(scoped_lock *);
204  bool is_current_writer();
205 
207 
208  void start_read(scoped_lock_read *);
210  void unblock_readers();
212  void end_read();
213 
215  atomic<scoped_lock_read*> reader_head;
217  atomic<scoped_lock*> writer_head;
219  atomic<scoped_lock*> writer_tail;
221  tbb_thread::id my_current_writer;
223  atomic<uintptr_t> rdr_count_and_flags; // used with __TBB_AtomicOR, which assumes uintptr_t
224 };
225 
226 } // namespace interface5
227 
229 
230 } // namespace tbb
231 
232 #endif /* __TBB_reader_writer_lock_H */
void __TBB_EXPORTED_METHOD lock()
Acquires the reader_writer_lock for write.
~scoped_lock_read()
Destructor, releases the read lock.
Definition: reader_writer_lock.h:136
void __TBB_EXPORTED_METHOD unlock()
Releases the reader_writer_lock.
status_t
Status type for nodes associated with lock instances.
Definition: reader_writer_lock.h:74
bool __TBB_EXPORTED_METHOD try_lock_read()
Tries to acquire the reader_writer_lock for read.
reader_writer_lock()
Constructs a new reader_writer_lock.
Definition: reader_writer_lock.h:77
The scoped lock pattern for read locks.
Definition: reader_writer_lock.h:126
The scoped lock pattern for write locks.
Definition: reader_writer_lock.h:89
void __TBB_EXPORTED_METHOD lock_read()
Acquires the reader_writer_lock for read.
~scoped_lock()
Destructor, releases the write lock.
Definition: reader_writer_lock.h:99
~reader_writer_lock()
Destructs a reader_writer_lock object.
Definition: reader_writer_lock.h:82
scoped_lock(reader_writer_lock &lock)
Construct with blocking attempt to acquire write lock on the passed-in lock.
Definition: reader_writer_lock.h:94
bool __TBB_EXPORTED_METHOD try_lock()
Tries to acquire the reader_writer_lock for write.
Definition: tbb_thread.h:233
scoped_lock_read(reader_writer_lock &lock)
Construct with blocking attempt to acquire read lock on the passed-in lock.
Definition: reader_writer_lock.h:131
The namespace tbb contains all components of the library.
Definition: parallel_for.h:44
Writer-preference reader-writer lock with local-only spinning on readers.
Definition: reader_writer_lock.h:34
Wrapper around the platform&#39;s native reader-writer lock.
Definition: mutex.h:40