Intel(R) Threading Building Blocks Doxygen Documentation  version 4.2.3
queuing_rw_mutex.h
Go to the documentation of this file.
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 
18 
19 */
20 
21 #ifndef __TBB_queuing_rw_mutex_H
22 #define __TBB_queuing_rw_mutex_H
23 
24 #include <cstring>
25 #include "atomic.h"
26 #include "tbb_profiling.h"
27 
28 namespace tbb {
29 
31 
34 class queuing_rw_mutex : internal::mutex_copy_deprecated_and_disabled {
35 public:
38  q_tail = NULL;
39 #if TBB_USE_THREADING_TOOLS
41 #endif
42  }
43 
46 #if TBB_USE_ASSERT
47  __TBB_ASSERT( !q_tail, "destruction of an acquired mutex");
48 #endif
49  }
50 
52 
54  class scoped_lock: internal::no_copy {
56  void initialize() {
57  my_mutex = NULL;
58  my_internal_lock = 0;
59  my_going = 0;
60 #if TBB_USE_ASSERT
61  my_state = 0xFF; // Set to invalid state
64 #endif /* TBB_USE_ASSERT */
65  }
66 
67  public:
69 
71 
73  scoped_lock( queuing_rw_mutex& m, bool write=true ) {
74  initialize();
75  acquire(m,write);
76  }
77 
80  if( my_mutex ) release();
81  }
82 
84  void acquire( queuing_rw_mutex& m, bool write=true );
85 
87  bool try_acquire( queuing_rw_mutex& m, bool write=true );
88 
90  void release();
91 
93 
94  bool upgrade_to_writer();
95 
97  bool downgrade_to_reader();
98 
99  private:
102 
105 
106  typedef unsigned char state_t;
107 
110 
112 
113  unsigned char __TBB_atomic my_going;
114 
116  unsigned char my_internal_lock;
117 
119  void acquire_internal_lock();
120 
122 
124 
126  void release_internal_lock();
127 
130 
132  void unblock_or_wait_on_internal_lock( uintptr_t );
133  };
134 
136 
137  // Mutex traits
138  static const bool is_rw_mutex = true;
139  static const bool is_recursive_mutex = false;
140  static const bool is_fair_mutex = true;
141 
142 private:
145 
146 };
147 
149 
150 } // namespace tbb
151 
152 #endif /* __TBB_queuing_rw_mutex_H */
void unblock_or_wait_on_internal_lock(uintptr_t)
A helper function.
~queuing_rw_mutex()
Destructor asserts if the mutex is acquired, i.e. q_tail is non-NULL.
unsigned char my_internal_lock
A tiny internal lock.
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:169
static const bool is_recursive_mutex
The scoped locking pattern.
void acquire(queuing_rw_mutex &m, bool write=true)
Acquire lock on given mutex.
Queuing reader-writer mutex with local-only spinning.
bool downgrade_to_reader()
Downgrade writer to become a reader.
scoped_lock(queuing_rw_mutex &m, bool write=true)
Acquire lock on given mutex.
atomic< scoped_lock * > q_tail
The last competitor requesting the lock.
bool try_acquire_internal_lock()
Try to acquire the internal lock.
static const bool is_rw_mutex
bool upgrade_to_writer()
Upgrade reader to become a writer.
#define __TBB_EXPORTED_METHOD
Definition: tbb_stddef.h:102
~scoped_lock()
Release lock (if lock is held).
The graph class.
#define __TBB_DEFINE_PROFILING_SET_NAME(sync_object_type)
scoped_lock *__TBB_atomic my_prev
The pointer to the previous and next competitors for a mutex.
void acquire_internal_lock()
Acquire the internal lock.
void poison_pointer(T *__TBB_atomic &)
Definition: tbb_stddef.h:309
atomic< state_t > my_state
State of the request: reader, writer, active reader, other service states.
scoped_lock()
Construct lock that has not acquired a mutex.
queuing_rw_mutex * my_mutex
The pointer to the mutex owned, or NULL if not holding a mutex.
#define __TBB_atomic
Definition: tbb_stddef.h:241
queuing_rw_mutex()
Construct unacquired mutex.
void release_internal_lock()
Release the internal lock.
void wait_for_release_of_internal_lock()
Wait for internal lock to be released.
void __TBB_EXPORTED_METHOD internal_construct()
static const bool is_fair_mutex
scoped_lock *__TBB_atomic *__TBB_atomic my_next
unsigned char __TBB_atomic my_going
The local spin-wait variable.
void initialize()
Initialize fields to mean "no lock held".
bool try_acquire(queuing_rw_mutex &m, bool write=true)
Acquire lock on given mutex if free (i.e. non-blocking)

Copyright © 2005-2019 Intel Corporation. All Rights Reserved.

Intel, Pentium, Intel Xeon, Itanium, Intel XScale and VTune are registered trademarks or trademarks of Intel Corporation or its subsidiaries in the United States and other countries.

* Other names and brands may be claimed as the property of others.