Committing TBB 2019 Update 9 source code
[platform/upstream/tbb.git] / include / tbb / queuing_rw_mutex.h
1 /*
2     Copyright (c) 2005-2019 Intel Corporation
3
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
7
8         http://www.apache.org/licenses/LICENSE-2.0
9
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.
15 */
16
17 #ifndef __TBB_queuing_rw_mutex_H
18 #define __TBB_queuing_rw_mutex_H
19
20 #define __TBB_queuing_rw_mutex_H_include_area
21 #include "internal/_warning_suppress_enable_notice.h"
22
23 #include <cstring>
24 #include "atomic.h"
25 #include "tbb_profiling.h"
26
27 namespace tbb {
28
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 {
34 public:
35     //! Construct unacquired mutex.
36     queuing_rw_mutex() {
37         q_tail = NULL;
38 #if TBB_USE_THREADING_TOOLS
39         internal_construct();
40 #endif
41     }
42
43     //! Destructor asserts if the mutex is acquired, i.e. q_tail is non-NULL
44     ~queuing_rw_mutex() {
45 #if TBB_USE_ASSERT
46         __TBB_ASSERT( !q_tail, "destruction of an acquired mutex");
47 #endif
48     }
49
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".
55         void initialize() {
56             my_mutex = NULL;
57             my_internal_lock = 0;
58             my_going = 0;
59 #if TBB_USE_ASSERT
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 */
64         }
65
66     public:
67         //! Construct lock that has not acquired a mutex.
68         /** Equivalent to zero-initialization of *this. */
69         scoped_lock() {initialize();}
70
71         //! Acquire lock on given mutex.
72         scoped_lock( queuing_rw_mutex& m, bool write=true ) {
73             initialize();
74             acquire(m,write);
75         }
76
77         //! Release lock (if lock is held).
78         ~scoped_lock() {
79             if( my_mutex ) release();
80         }
81
82         //! Acquire lock on given mutex.
83         void acquire( queuing_rw_mutex& m, bool write=true );
84
85         //! Acquire lock on given mutex if free (i.e. non-blocking)
86         bool try_acquire( queuing_rw_mutex& m, bool write=true );
87
88         //! Release lock.
89         void release();
90
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();
94
95         //! Downgrade writer to become a reader.
96         bool downgrade_to_reader();
97
98     private:
99         //! The pointer to the mutex owned, or NULL if not holding a mutex.
100         queuing_rw_mutex* my_mutex;
101
102         //! The pointer to the previous and next competitors for a mutex
103         scoped_lock *__TBB_atomic my_prev, *__TBB_atomic my_next;
104
105         typedef unsigned char state_t;
106
107         //! State of the request: reader, writer, active reader, other service states
108         atomic<state_t> my_state;
109
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;
113
114         //! A tiny internal lock
115         unsigned char my_internal_lock;
116
117         //! Acquire the internal lock
118         void acquire_internal_lock();
119
120         //! Try to acquire the internal lock
121         /** Returns true if lock was successfully acquired. */
122         bool try_acquire_internal_lock();
123
124         //! Release the internal lock
125         void release_internal_lock();
126
127         //! Wait for internal lock to be released
128         void wait_for_release_of_internal_lock();
129
130         //! A helper function
131         void unblock_or_wait_on_internal_lock( uintptr_t );
132     };
133
134     void __TBB_EXPORTED_METHOD internal_construct();
135
136     // Mutex traits
137     static const bool is_rw_mutex = true;
138     static const bool is_recursive_mutex = false;
139     static const bool is_fair_mutex = true;
140
141 private:
142     //! The last competitor requesting the lock
143     atomic<scoped_lock*> q_tail;
144
145 };
146
147 __TBB_DEFINE_PROFILING_SET_NAME(queuing_rw_mutex)
148
149 } // namespace tbb
150
151 #include "internal/_warning_suppress_disable_notice.h"
152 #undef __TBB_queuing_rw_mutex_H_include_area
153
154 #endif /* __TBB_queuing_rw_mutex_H */