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-2020 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
22 
23 #include <cstring>
24 #include "atomic.h"
25 #include "tbb_profiling.h"
26 
27 namespace tbb {
28 
30 
34 public:
37  q_tail = NULL;
38 #if TBB_USE_THREADING_TOOLS
40 #endif
41  }
42 
45 #if TBB_USE_ASSERT
46  __TBB_ASSERT( !q_tail, "destruction of an acquired mutex");
47 #endif
48  }
49 
51 
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
63 #endif /* TBB_USE_ASSERT */
64  }
65 
66  public:
68 
70 
72  scoped_lock( queuing_rw_mutex& m, bool write=true ) {
73  initialize();
74  acquire(m,write);
75  }
76 
79  if( my_mutex ) release();
80  }
81 
83  void acquire( queuing_rw_mutex& m, bool write=true );
84 
86  bool try_acquire( queuing_rw_mutex& m, bool write=true );
87 
89  void release();
90 
92 
93  bool upgrade_to_writer();
94 
96  bool downgrade_to_reader();
97 
98  private:
101 
104 
105  typedef unsigned char state_t;
106 
108  atomic<state_t> my_state;
109 
111 
112  unsigned char __TBB_atomic my_going;
113 
115  unsigned char my_internal_lock;
116 
118  void acquire_internal_lock();
119 
121 
123 
125  void release_internal_lock();
126 
129 
131  void unblock_or_wait_on_internal_lock( uintptr_t );
132  };
133 
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:
143  atomic<scoped_lock*> q_tail;
144 
145 };
146 
148 
149 } // namespace tbb
150 
152 #undef __TBB_queuing_rw_mutex_H_include_area
153 
154 #endif /* __TBB_queuing_rw_mutex_H */
tbb::queuing_rw_mutex::scoped_lock::~scoped_lock
~scoped_lock()
Release lock (if lock is held).
Definition: queuing_rw_mutex.h:78
tbb::internal::mutex_copy_deprecated_and_disabled
Definition: tbb_stddef.h:365
tbb::queuing_rw_mutex::scoped_lock::acquire_internal_lock
void acquire_internal_lock()
Acquire the internal lock.
Definition: queuing_rw_mutex.cpp:55
__TBB_ASSERT
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:165
tbb::queuing_rw_mutex::is_rw_mutex
static const bool is_rw_mutex
Definition: queuing_rw_mutex.h:137
tbb::queuing_rw_mutex
Queuing reader-writer mutex with local-only spinning.
Definition: queuing_rw_mutex.h:33
tbb::queuing_rw_mutex::scoped_lock::my_mutex
queuing_rw_mutex * my_mutex
The pointer to the mutex owned, or NULL if not holding a mutex.
Definition: queuing_rw_mutex.h:100
tbb::internal::poison_pointer
void poison_pointer(T *__TBB_atomic &)
Definition: tbb_stddef.h:305
tbb::queuing_rw_mutex::~queuing_rw_mutex
~queuing_rw_mutex()
Destructor asserts if the mutex is acquired, i.e. q_tail is non-NULL.
Definition: queuing_rw_mutex.h:44
tbb::queuing_rw_mutex::scoped_lock::unblock_or_wait_on_internal_lock
void unblock_or_wait_on_internal_lock(uintptr_t)
A helper function.
Definition: queuing_rw_mutex.cpp:74
tbb::queuing_rw_mutex::scoped_lock::my_prev
scoped_lock *__TBB_atomic my_prev
The pointer to the previous and next competitors for a mutex.
Definition: queuing_rw_mutex.h:103
tbb
The graph class.
Definition: serial/tbb/parallel_for.h:46
tbb::queuing_rw_mutex::internal_construct
void __TBB_EXPORTED_METHOD internal_construct()
Definition: queuing_rw_mutex.cpp:484
tbb::queuing_rw_mutex::scoped_lock::initialize
void initialize()
Initialize fields to mean "no lock held".
Definition: queuing_rw_mutex.h:55
tbb::queuing_rw_mutex::is_fair_mutex
static const bool is_fair_mutex
Definition: queuing_rw_mutex.h:139
tbb::queuing_rw_mutex::q_tail
atomic< scoped_lock * > q_tail
The last competitor requesting the lock.
Definition: queuing_rw_mutex.h:143
tbb::queuing_rw_mutex::scoped_lock::try_acquire
bool try_acquire(queuing_rw_mutex &m, bool write=true)
Acquire lock on given mutex if free (i.e. non-blocking)
Definition: queuing_rw_mutex.cpp:226
tbb_profiling.h
__TBB_atomic
#define __TBB_atomic
Definition: tbb_stddef.h:237
tbb::queuing_rw_mutex::scoped_lock::my_next
scoped_lock *__TBB_atomic *__TBB_atomic my_next
Definition: queuing_rw_mutex.h:103
tbb::queuing_rw_mutex::scoped_lock::state_t
unsigned char state_t
Definition: queuing_rw_mutex.h:105
tbb::queuing_rw_mutex::scoped_lock::my_going
unsigned char __TBB_atomic my_going
The local spin-wait variable.
Definition: queuing_rw_mutex.h:112
tbb::queuing_rw_mutex::scoped_lock::scoped_lock
scoped_lock(queuing_rw_mutex &m, bool write=true)
Acquire lock on given mutex.
Definition: queuing_rw_mutex.h:72
tbb::queuing_rw_mutex::scoped_lock::downgrade_to_reader
bool downgrade_to_reader()
Downgrade writer to become a reader.
Definition: queuing_rw_mutex.cpp:360
atomic.h
tbb::queuing_rw_mutex::scoped_lock::release
void release()
Release lock.
Definition: queuing_rw_mutex.cpp:254
tbb::queuing_rw_mutex::is_recursive_mutex
static const bool is_recursive_mutex
Definition: queuing_rw_mutex.h:138
tbb::queuing_rw_mutex::scoped_lock::upgrade_to_writer
bool upgrade_to_writer()
Upgrade reader to become a writer.
Definition: queuing_rw_mutex.cpp:387
tbb::queuing_rw_mutex::scoped_lock
The scoped locking pattern.
Definition: queuing_rw_mutex.h:53
tbb::internal::no_copy
Base class for types that should not be copied or assigned.
Definition: tbb_stddef.h:330
_warning_suppress_disable_notice.h
tbb::queuing_rw_mutex::scoped_lock::my_internal_lock
unsigned char my_internal_lock
A tiny internal lock.
Definition: queuing_rw_mutex.h:115
tbb::queuing_rw_mutex::scoped_lock::release_internal_lock
void release_internal_lock()
Release the internal lock.
Definition: queuing_rw_mutex.cpp:64
tbb::queuing_rw_mutex::scoped_lock::try_acquire_internal_lock
bool try_acquire_internal_lock()
Try to acquire the internal lock.
Definition: queuing_rw_mutex.cpp:50
tbb::queuing_rw_mutex::scoped_lock::wait_for_release_of_internal_lock
void wait_for_release_of_internal_lock()
Wait for internal lock to be released.
Definition: queuing_rw_mutex.cpp:69
__TBB_DEFINE_PROFILING_SET_NAME
#define __TBB_DEFINE_PROFILING_SET_NAME(sync_object_type)
Definition: tbb_profiling.h:115
_warning_suppress_enable_notice.h
__TBB_EXPORTED_METHOD
#define __TBB_EXPORTED_METHOD
Definition: tbb_stddef.h:98
tbb::queuing_rw_mutex::queuing_rw_mutex
queuing_rw_mutex()
Construct unacquired mutex.
Definition: queuing_rw_mutex.h:36
tbb::queuing_rw_mutex::scoped_lock::scoped_lock
scoped_lock()
Construct lock that has not acquired a mutex.
Definition: queuing_rw_mutex.h:69
tbb::queuing_rw_mutex::scoped_lock::acquire
void acquire(queuing_rw_mutex &m, bool write=true)
Acquire lock on given mutex.
Definition: queuing_rw_mutex.cpp:140
tbb::queuing_rw_mutex::scoped_lock::my_state
atomic< state_t > my_state
State of the request: reader, writer, active reader, other service states.
Definition: queuing_rw_mutex.h:108

Copyright © 2005-2020 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.