2 Copyright 2005-2011 Intel Corporation. All Rights Reserved.
4 This file is part of Threading Building Blocks.
6 Threading Building Blocks is free software; you can redistribute it
7 and/or modify it under the terms of the GNU General Public License
8 version 2 as published by the Free Software Foundation.
10 Threading Building Blocks is distributed in the hope that it will be
11 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with Threading Building Blocks; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 As a special exception, you may use this file as part of a free software
20 library without restriction. Specifically, if other files instantiate
21 templates or use macros or inline functions from this file, or you compile
22 this file and link it with other files to produce an executable, this
23 file does not by itself cause the resulting executable to be covered by
24 the GNU General Public License. This exception does not however
25 invalidate any other reasons why the executable file might be covered by
26 the GNU General Public License.
29 #ifndef __TBB_queuing_rw_mutex_H
30 #define __TBB_queuing_rw_mutex_H
32 #include "tbb_config.h"
34 #if !TBB_USE_EXCEPTIONS && _MSC_VER
35 // Suppress "C++ exception handler used, but unwind semantics are not enabled" warning in STL headers
36 #pragma warning (push)
37 #pragma warning (disable: 4530)
42 #if !TBB_USE_EXCEPTIONS && _MSC_VER
47 #include "tbb_profiling.h"
51 //! Reader-writer lock with local-only spinning.
52 /** Adapted from Krieger, Stumm, et al. pseudocode at
53 http://www.eecg.toronto.edu/parallel/pubs_abs.html#Krieger_etal_ICPP93
54 @ingroup synchronization */
55 class queuing_rw_mutex {
57 //! Construct unacquired mutex.
60 #if TBB_USE_THREADING_TOOLS
65 //! Destructor asserts if the mutex is acquired, i.e. q_tail is non-NULL
68 __TBB_ASSERT( !q_tail, "destruction of an acquired mutex");
73 friend class scoped_lock;
75 //! The scoped locking pattern
76 /** It helps to avoid the common problem of forgetting to release lock.
77 It also nicely provides the "node" for queuing locks. */
78 class scoped_lock: internal::no_copy {
83 my_state = 0xFF; // Set to invalid state
84 internal::poison_pointer(my_next);
85 internal::poison_pointer(my_prev);
86 #endif /* TBB_USE_ASSERT */
89 //! Construct lock that has not acquired a mutex.
90 /** Equivalent to zero-initialization of *this. */
91 scoped_lock() {initialize();}
93 //! Acquire lock on given mutex.
94 scoped_lock( queuing_rw_mutex& m, bool write=true ) {
99 //! Release lock (if lock is held).
101 if( my_mutex ) release();
104 //! Acquire lock on given mutex.
105 void acquire( queuing_rw_mutex& m, bool write=true );
107 //! Try acquire lock on given mutex.
108 bool try_acquire( queuing_rw_mutex& m, bool write=true );
113 //! Upgrade reader to become a writer.
114 /** Returns true if the upgrade happened without re-acquiring the lock and false if opposite */
115 bool upgrade_to_writer();
117 //! Downgrade writer to become a reader.
118 bool downgrade_to_reader();
121 //! The pointer to the current mutex to work
122 queuing_rw_mutex* my_mutex;
124 //! The pointer to the previous and next competitors for a mutex
125 scoped_lock *__TBB_atomic my_prev, *__TBB_atomic my_next;
127 typedef unsigned char state_t;
129 //! State of the request: reader, writer, active reader, other service states
130 atomic<state_t> my_state;
132 //! The local spin-wait variable
133 /** Corresponds to "spin" in the pseudocode but inverted for the sake of zero-initialization */
134 unsigned char __TBB_atomic my_going;
136 //! A tiny internal lock
137 unsigned char my_internal_lock;
139 //! Acquire the internal lock
140 void acquire_internal_lock();
142 //! Try to acquire the internal lock
143 /** Returns true if lock was successfully acquired. */
144 bool try_acquire_internal_lock();
146 //! Release the internal lock
147 void release_internal_lock();
149 //! Wait for internal lock to be released
150 void wait_for_release_of_internal_lock();
152 //! A helper function
153 void unblock_or_wait_on_internal_lock( uintptr_t );
156 void __TBB_EXPORTED_METHOD internal_construct();
159 static const bool is_rw_mutex = true;
160 static const bool is_recursive_mutex = false;
161 static const bool is_fair_mutex = true;
164 //! The last competitor requesting the lock
165 atomic<scoped_lock*> q_tail;
169 __TBB_DEFINE_PROFILING_SET_NAME(queuing_rw_mutex)
173 #endif /* __TBB_queuing_rw_mutex_H */