BRE12
queuing_rw_mutex.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_queuing_rw_mutex_H
22 #define __TBB_queuing_rw_mutex_H
23 
24 #include "tbb_config.h"
25 
26 #if !TBB_USE_EXCEPTIONS && _MSC_VER
27  // Suppress "C++ exception handler used, but unwind semantics are not enabled" warning in STL headers
28  #pragma warning (push)
29  #pragma warning (disable: 4530)
30 #endif
31 
32 #include <cstring>
33 
34 #if !TBB_USE_EXCEPTIONS && _MSC_VER
35  #pragma warning (pop)
36 #endif
37 
38 #include "atomic.h"
39 #include "tbb_profiling.h"
40 
41 namespace tbb {
42 
44 
47 class queuing_rw_mutex : internal::mutex_copy_deprecated_and_disabled {
48 public:
51  q_tail = NULL;
52 #if TBB_USE_THREADING_TOOLS
53  internal_construct();
54 #endif
55  }
56 
59 #if TBB_USE_ASSERT
60  __TBB_ASSERT( !q_tail, "destruction of an acquired mutex");
61 #endif
62  }
63 
65 
67  class scoped_lock: internal::no_copy {
69  void initialize() {
70  my_mutex = NULL;
71 #if TBB_USE_ASSERT
72  my_state = 0xFF; // Set to invalid state
73  internal::poison_pointer(my_next);
74  internal::poison_pointer(my_prev);
75 #endif /* TBB_USE_ASSERT */
76  }
77 
78  public:
80 
81  scoped_lock() {initialize();}
82 
84  scoped_lock( queuing_rw_mutex& m, bool write=true ) {
85  initialize();
86  acquire(m,write);
87  }
88 
91  if( my_mutex ) release();
92  }
93 
95  void acquire( queuing_rw_mutex& m, bool write=true );
96 
98  bool try_acquire( queuing_rw_mutex& m, bool write=true );
99 
101  void release();
102 
104 
105  bool upgrade_to_writer();
106 
108  bool downgrade_to_reader();
109 
110  private:
112  queuing_rw_mutex* my_mutex;
113 
115  scoped_lock *__TBB_atomic my_prev, *__TBB_atomic my_next;
116 
117  typedef unsigned char state_t;
118 
120  atomic<state_t> my_state;
121 
123 
124  unsigned char __TBB_atomic my_going;
125 
127  unsigned char my_internal_lock;
128 
130  void acquire_internal_lock();
131 
133 
134  bool try_acquire_internal_lock();
135 
137  void release_internal_lock();
138 
140  void wait_for_release_of_internal_lock();
141 
143  void unblock_or_wait_on_internal_lock( uintptr_t );
144  };
145 
146  void __TBB_EXPORTED_METHOD internal_construct();
147 
148  // Mutex traits
149  static const bool is_rw_mutex = true;
150  static const bool is_recursive_mutex = false;
151  static const bool is_fair_mutex = true;
152 
153 private:
155  atomic<scoped_lock*> q_tail;
156 
157 };
158 
159 __TBB_DEFINE_PROFILING_SET_NAME(queuing_rw_mutex)
160 
161 } // namespace tbb
162 
163 #endif /* __TBB_queuing_rw_mutex_H */
bool downgrade_to_reader()
Downgrade writer to become a reader.
~scoped_lock()
Release lock (if lock is held).
Definition: queuing_rw_mutex.h:90
The scoped locking pattern.
Definition: queuing_rw_mutex.h:67
scoped_lock(queuing_rw_mutex &m, bool write=true)
Acquire lock on given mutex.
Definition: queuing_rw_mutex.h:84
bool upgrade_to_writer()
Upgrade reader to become a writer.
queuing_rw_mutex()
Construct unacquired mutex.
Definition: queuing_rw_mutex.h:50
The namespace tbb contains all components of the library.
Definition: parallel_for.h:44
bool try_acquire(queuing_rw_mutex &m, bool write=true)
Acquire lock on given mutex if free (i.e. non-blocking)
void acquire(queuing_rw_mutex &m, bool write=true)
Acquire lock on given mutex.
~queuing_rw_mutex()
Destructor asserts if the mutex is acquired, i.e. q_tail is non-NULL.
Definition: queuing_rw_mutex.h:58
Queuing reader-writer mutex with local-only spinning.
Definition: queuing_rw_mutex.h:47
scoped_lock()
Construct lock that has not acquired a mutex.
Definition: queuing_rw_mutex.h:81