Intel(R) Threading Building Blocks Doxygen Documentation  version 4.2.3
spin_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 #ifndef __TBB_spin_mutex_H
18 #define __TBB_spin_mutex_H
19 
20 #define __TBB_spin_mutex_H_include_area
22 
23 #include <cstddef>
24 #include <new>
25 #include "aligned_space.h"
26 #include "tbb_stddef.h"
27 #include "tbb_machine.h"
28 #include "tbb_profiling.h"
30 
31 namespace tbb {
32 
34 
42 
43 public:
45 
46  spin_mutex() : flag(0) {
47 #if TBB_USE_THREADING_TOOLS
49 #endif
50  }
51 
54  private:
57 
59 
63 
66 
69 
72 
73  friend class spin_mutex;
74 
75  public:
78 
82 #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
83  my_mutex=NULL;
85 #else
86  my_mutex=&m;
88 #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT*/
89  }
90 
92  void acquire( spin_mutex& m ) {
93 #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
95 #else
96  my_mutex = &m;
98 #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT*/
99  }
100 
102 
103  bool try_acquire( spin_mutex& m ) {
104 #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
105  return internal_try_acquire(m);
106 #else
107  bool result = __TBB_TryLockByte(m.flag);
108  if( result )
109  my_mutex = &m;
110  return result;
111 #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT*/
112  }
113 
115  void release() {
116 #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
118 #else
120  my_mutex = NULL;
121 #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT */
122  }
123 
126  if( my_mutex ) {
127 #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
129 #else
131 #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT */
132  }
133  }
134  };
135 
138 
139  // Mutex traits
140  static const bool is_rw_mutex = false;
141  static const bool is_recursive_mutex = false;
142  static const bool is_fair_mutex = false;
143 
144  // ISO C++0x compatibility methods
145 
147  void lock() {
148 #if TBB_USE_THREADING_TOOLS
149  aligned_space<scoped_lock> tmp;
150  new(tmp.begin()) scoped_lock(*this);
151 #else
153 #endif /* TBB_USE_THREADING_TOOLS*/
154  }
155 
157 
158  bool try_lock() {
159 #if TBB_USE_THREADING_TOOLS
160  aligned_space<scoped_lock> tmp;
161  return (new(tmp.begin()) scoped_lock)->internal_try_acquire(*this);
162 #else
163  return __TBB_TryLockByte(flag);
164 #endif /* TBB_USE_THREADING_TOOLS*/
165  }
166 
168  void unlock() {
169 #if TBB_USE_THREADING_TOOLS
170  aligned_space<scoped_lock> tmp;
171  scoped_lock& s = *tmp.begin();
172  s.my_mutex = this;
173  s.internal_release();
174 #else
176 #endif /* TBB_USE_THREADING_TOOLS */
177  }
178 
179  friend class scoped_lock;
180 }; // end of spin_mutex
181 
183 
184 } // namespace tbb
185 
186 #if ( __TBB_x86_32 || __TBB_x86_64 )
188 #endif
189 
190 namespace tbb {
192 
202 #if ( __TBB_x86_32 || __TBB_x86_64 )
203 typedef interface7::internal::padded_mutex<interface7::internal::x86_eliding_mutex,false> speculative_spin_mutex;
204 #else
206 #endif
208 
209 } // namespace tbb
210 
212 #undef __TBB_spin_mutex_H_include_area
213 
214 #endif /* __TBB_spin_mutex_H */
tbb::spin_mutex::scoped_lock::my_unlock_value
__TBB_Flag my_unlock_value
Value to store into spin_mutex::flag to unlock the mutex.
Definition: spin_mutex.h:62
tbb::spin_mutex::scoped_lock::internal_release
void __TBB_EXPORTED_METHOD internal_release()
Like release, but with ITT instrumentation.
Definition: spin_mutex.cpp:32
tbb::spin_mutex::scoped_lock::my_mutex
spin_mutex * my_mutex
Points to currently held mutex, or NULL if no lock is held.
Definition: spin_mutex.h:56
tbb::spin_mutex::is_recursive_mutex
static const bool is_recursive_mutex
Definition: spin_mutex.h:141
tbb
The graph class.
Definition: serial/tbb/parallel_for.h:46
tbb::spin_mutex::lock
void lock()
Acquire lock.
Definition: spin_mutex.h:147
__TBB_LockByte
__TBB_Flag __TBB_LockByte(__TBB_atomic_flag &flag)
Definition: tbb_machine.h:922
tbb::spin_mutex::scoped_lock::~scoped_lock
~scoped_lock()
Destroy lock. If holding a lock, releases the lock first.
Definition: spin_mutex.h:125
tbb::internal::suppress_unused_warning
void suppress_unused_warning(const T1 &)
Utility template function to prevent "unused" warnings by various compilers.
Definition: tbb_stddef.h:398
tbb_profiling.h
aligned_space.h
tbb::spin_mutex::is_rw_mutex
static const bool is_rw_mutex
Definition: spin_mutex.h:140
__TBB_TryLockByte
bool __TBB_TryLockByte(__TBB_atomic_flag &flag)
Definition: tbb_machine.h:916
tbb::interface7::internal::padded_mutex
Definition: _mutex_padding.h:32
_mutex_padding.h
_x86_eliding_mutex_impl.h
tbb::spin_mutex::scoped_lock::release
void release()
Release lock.
Definition: spin_mutex.h:115
tbb::spin_mutex::scoped_lock::internal_try_acquire
bool __TBB_EXPORTED_METHOD internal_try_acquire(spin_mutex &m)
Like try_acquire, but with ITT instrumentation.
Definition: spin_mutex.cpp:40
tbb::spin_mutex::is_fair_mutex
static const bool is_fair_mutex
Definition: spin_mutex.h:142
tbb::spin_mutex::scoped_lock::internal_acquire
void __TBB_EXPORTED_METHOD internal_acquire(spin_mutex &m)
Like acquire, but with ITT instrumentation.
Definition: spin_mutex.cpp:24
_warning_suppress_disable_notice.h
__TBB_UnlockByte
#define __TBB_UnlockByte
Definition: gcc_generic.h:211
tbb::spin_mutex::scoped_lock::try_acquire
bool try_acquire(spin_mutex &m)
Try acquiring lock (non-blocking)
Definition: spin_mutex.h:103
tbb::spin_mutex::scoped_lock
Represents acquisition of a mutex.
Definition: spin_mutex.h:53
tbb_machine.h
__TBB_atomic_flag
__TBB_atomic __TBB_Flag __TBB_atomic_flag
Definition: gcc_generic.h:106
tbb::spin_mutex::try_lock
bool try_lock()
Try acquiring lock (non-blocking)
Definition: spin_mutex.h:158
tbb::internal::mutex_copy_deprecated_and_disabled
Definition: tbb_stddef.h:365
s
void const char const char int ITT_FORMAT __itt_group_sync s
Definition: ittnotify_static.h:76
tbb::spin_mutex::scoped_lock::scoped_lock
scoped_lock(spin_mutex &m)
Construct and acquire lock on a mutex.
Definition: spin_mutex.h:80
tbb::spin_mutex
A lock that occupies a single byte.
Definition: spin_mutex.h:39
tbb::speculative_spin_mutex
interface7::internal::padded_mutex< spin_mutex, false > speculative_spin_mutex
A cross-platform spin mutex with speculative lock acquisition.
Definition: spin_mutex.h:205
tbb_stddef.h
__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::spin_mutex::scoped_lock::scoped_lock
scoped_lock()
Construct without acquiring a mutex.
Definition: spin_mutex.h:77
__TBB_Flag
unsigned char __TBB_Flag
Definition: gcc_generic.h:105
tbb::spin_mutex::scoped_lock::acquire
void acquire(spin_mutex &m)
Acquire lock.
Definition: spin_mutex.h:92
tbb::internal::no_copy
Base class for types that should not be copied or assigned.
Definition: tbb_stddef.h:330
tbb::spin_mutex::scoped_lock
friend class scoped_lock
Definition: spin_mutex.h:179
tbb::spin_mutex::internal_construct
void __TBB_EXPORTED_METHOD internal_construct()
Internal constructor with ITT instrumentation.
Definition: spin_mutex.cpp:50
tbb::spin_mutex::flag
__TBB_atomic_flag flag
0 if lock is released, 1 if lock is acquired.
Definition: spin_mutex.h:41
tbb::spin_mutex::unlock
void unlock()
Release lock.
Definition: spin_mutex.h:168
tbb::spin_mutex::spin_mutex
spin_mutex()
Construct unacquired lock.
Definition: spin_mutex.h:46

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.