2 Copyright (c) 2005-2019 Intel Corporation
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
17 #ifndef __TBB_queuing_rw_mutex_H
18 #define __TBB_queuing_rw_mutex_H
20 #define __TBB_queuing_rw_mutex_H_include_area
21 #include "internal/_warning_suppress_enable_notice.h"
25 #include "tbb_profiling.h"
29 //! Queuing reader-writer mutex with local-only spinning.
30 /** Adapted from Krieger, Stumm, et al. pseudocode at
31 http://www.eecg.toronto.edu/parallel/pubs_abs.html#Krieger_etal_ICPP93
32 @ingroup synchronization */
33 class queuing_rw_mutex : internal::mutex_copy_deprecated_and_disabled {
35 //! Construct unacquired mutex.
38 #if TBB_USE_THREADING_TOOLS
43 //! Destructor asserts if the mutex is acquired, i.e. q_tail is non-NULL
46 __TBB_ASSERT( !q_tail, "destruction of an acquired mutex");
50 //! The scoped locking pattern
51 /** It helps to avoid the common problem of forgetting to release lock.
52 It also nicely provides the "node" for queuing locks. */
53 class scoped_lock: internal::no_copy {
54 //! Initialize fields to mean "no lock held".
60 my_state = 0xFF; // Set to invalid state
61 internal::poison_pointer(my_next);
62 internal::poison_pointer(my_prev);
63 #endif /* TBB_USE_ASSERT */
67 //! Construct lock that has not acquired a mutex.
68 /** Equivalent to zero-initialization of *this. */
69 scoped_lock() {initialize();}
71 //! Acquire lock on given mutex.
72 scoped_lock( queuing_rw_mutex& m, bool write=true ) {
77 //! Release lock (if lock is held).
79 if( my_mutex ) release();
82 //! Acquire lock on given mutex.
83 void acquire( queuing_rw_mutex& m, bool write=true );
85 //! Acquire lock on given mutex if free (i.e. non-blocking)
86 bool try_acquire( queuing_rw_mutex& m, bool write=true );
91 //! Upgrade reader to become a writer.
92 /** Returns whether the upgrade happened without releasing and re-acquiring the lock */
93 bool upgrade_to_writer();
95 //! Downgrade writer to become a reader.
96 bool downgrade_to_reader();
99 //! The pointer to the mutex owned, or NULL if not holding a mutex.
100 queuing_rw_mutex* my_mutex;
102 //! The pointer to the previous and next competitors for a mutex
103 scoped_lock *__TBB_atomic my_prev, *__TBB_atomic my_next;
105 typedef unsigned char state_t;
107 //! State of the request: reader, writer, active reader, other service states
108 atomic<state_t> my_state;
110 //! The local spin-wait variable
111 /** Corresponds to "spin" in the pseudocode but inverted for the sake of zero-initialization */
112 unsigned char __TBB_atomic my_going;
114 //! A tiny internal lock
115 unsigned char my_internal_lock;
117 //! Acquire the internal lock
118 void acquire_internal_lock();
120 //! Try to acquire the internal lock
121 /** Returns true if lock was successfully acquired. */
122 bool try_acquire_internal_lock();
124 //! Release the internal lock
125 void release_internal_lock();
127 //! Wait for internal lock to be released
128 void wait_for_release_of_internal_lock();
130 //! A helper function
131 void unblock_or_wait_on_internal_lock( uintptr_t );
134 void __TBB_EXPORTED_METHOD internal_construct();
137 static const bool is_rw_mutex = true;
138 static const bool is_recursive_mutex = false;
139 static const bool is_fair_mutex = true;
142 //! The last competitor requesting the lock
143 atomic<scoped_lock*> q_tail;
147 __TBB_DEFINE_PROFILING_SET_NAME(queuing_rw_mutex)
151 #include "internal/_warning_suppress_disable_notice.h"
152 #undef __TBB_queuing_rw_mutex_H_include_area
154 #endif /* __TBB_queuing_rw_mutex_H */