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 
18 
19 */
20 
21 #ifndef __TBB_spin_mutex_H
22 #define __TBB_spin_mutex_H
23 
24 #include <cstddef>
25 #include <new>
26 #include "aligned_space.h"
27 #include "tbb_stddef.h"
28 #include "tbb_machine.h"
29 #include "tbb_profiling.h"
31 
32 namespace tbb {
33 
35 
40 class spin_mutex : internal::mutex_copy_deprecated_and_disabled {
43 
44 public:
46 
47  spin_mutex() : flag(0) {
48 #if TBB_USE_THREADING_TOOLS
50 #endif
51  }
52 
54  class scoped_lock : internal::no_copy {
55  private:
58 
60 
64 
67 
70 
73 
74  friend class spin_mutex;
75 
76  public:
78  scoped_lock() : my_mutex(NULL), my_unlock_value(0) {}
79 
81  scoped_lock( spin_mutex& m ) : my_unlock_value(0) {
82  internal::suppress_unused_warning(my_unlock_value);
83 #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
84  my_mutex=NULL;
86 #else
87  my_mutex=&m;
89 #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT*/
90  }
91 
93  void acquire( spin_mutex& m ) {
94 #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
96 #else
97  my_mutex = &m;
99 #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT*/
100  }
101 
103 
104  bool try_acquire( spin_mutex& m ) {
105 #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
106  return internal_try_acquire(m);
107 #else
108  bool result = __TBB_TryLockByte(m.flag);
109  if( result )
110  my_mutex = &m;
111  return result;
112 #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT*/
113  }
114 
116  void release() {
117 #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
119 #else
120  __TBB_UnlockByte(my_mutex->flag);
121  my_mutex = NULL;
122 #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT */
123  }
124 
127  if( my_mutex ) {
128 #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
130 #else
131  __TBB_UnlockByte(my_mutex->flag);
132 #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT */
133  }
134  }
135  };
136 
139 
140  // Mutex traits
141  static const bool is_rw_mutex = false;
142  static const bool is_recursive_mutex = false;
143  static const bool is_fair_mutex = false;
144 
145  // ISO C++0x compatibility methods
146 
148  void lock() {
149 #if TBB_USE_THREADING_TOOLS
151  new(tmp.begin()) scoped_lock(*this);
152 #else
153  __TBB_LockByte(flag);
154 #endif /* TBB_USE_THREADING_TOOLS*/
155  }
156 
158 
159  bool try_lock() {
160 #if TBB_USE_THREADING_TOOLS
162  return (new(tmp.begin()) scoped_lock)->internal_try_acquire(*this);
163 #else
164  return __TBB_TryLockByte(flag);
165 #endif /* TBB_USE_THREADING_TOOLS*/
166  }
167 
169  void unlock() {
170 #if TBB_USE_THREADING_TOOLS
172  scoped_lock& s = *tmp.begin();
173  s.my_mutex = this;
174  s.internal_release();
175 #else
176  __TBB_UnlockByte(flag);
177 #endif /* TBB_USE_THREADING_TOOLS */
178  }
179 
180  friend class scoped_lock;
181 }; // end of spin_mutex
182 
184 
185 } // namespace tbb
186 
187 #if ( __TBB_x86_32 || __TBB_x86_64 )
189 #endif
190 
191 namespace tbb {
193 
203 #if ( __TBB_x86_32 || __TBB_x86_64 )
205 #else
207 #endif
208 __TBB_DEFINE_PROFILING_SET_NAME(speculative_spin_mutex)
209 
210 } // namespace tbb
211 
212 #endif /* __TBB_spin_mutex_H */
#define __TBB_UnlockByte
Definition: gcc_generic.h:215
~scoped_lock()
Destroy lock. If holding a lock, releases the lock first.
Definition: spin_mutex.h:126
__TBB_atomic __TBB_Flag __TBB_atomic_flag
Definition: gcc_generic.h:110
static const bool is_recursive_mutex
Definition: spin_mutex.h:142
bool __TBB_TryLockByte(__TBB_atomic_flag &flag)
Definition: tbb_machine.h:920
T * begin() const
Pointer to beginning of array.
Definition: aligned_space.h:39
bool try_lock()
Try acquiring lock (non-blocking)
Definition: spin_mutex.h:159
scoped_lock()
Construct without acquiring a mutex.
Definition: spin_mutex.h:78
unsigned char __TBB_Flag
Definition: gcc_generic.h:109
void suppress_unused_warning(const T1 &)
Utility template function to prevent "unused" warnings by various compilers.
Definition: tbb_stddef.h:381
Block of space aligned sufficiently to construct an array T with N elements.
Definition: aligned_space.h:33
void acquire(spin_mutex &m)
Acquire lock.
Definition: spin_mutex.h:93
static const bool is_fair_mutex
Definition: spin_mutex.h:143
Represents acquisition of a mutex.
Definition: spin_mutex.h:54
scoped_lock(spin_mutex &m)
Construct and acquire lock on a mutex.
Definition: spin_mutex.h:81
interface7::internal::padded_mutex< spin_mutex, false > speculative_spin_mutex
A cross-platform spin mutex with speculative lock acquisition.
Definition: spin_mutex.h:206
void unlock()
Release lock.
Definition: spin_mutex.h:169
bool try_acquire(spin_mutex &m)
Try acquiring lock (non-blocking)
Definition: spin_mutex.h:104
static const bool is_rw_mutex
Definition: spin_mutex.h:141
#define __TBB_EXPORTED_METHOD
Definition: tbb_stddef.h:102
void __TBB_EXPORTED_METHOD internal_release()
Like release, but with ITT instrumentation.
Definition: spin_mutex.cpp:36
spin_mutex()
Construct unacquired lock.
Definition: spin_mutex.h:47
The graph class.
__TBB_Flag my_unlock_value
Value to store into spin_mutex::flag to unlock the mutex.
Definition: spin_mutex.h:63
void lock()
Acquire lock.
Definition: spin_mutex.h:148
#define __TBB_DEFINE_PROFILING_SET_NAME(sync_object_type)
void __TBB_EXPORTED_METHOD internal_construct()
Internal constructor with ITT instrumentation.
Definition: spin_mutex.cpp:54
void __TBB_EXPORTED_METHOD internal_acquire(spin_mutex &m)
Like acquire, but with ITT instrumentation.
Definition: spin_mutex.cpp:28
bool __TBB_EXPORTED_METHOD internal_try_acquire(spin_mutex &m)
Like try_acquire, but with ITT instrumentation.
Definition: spin_mutex.cpp:44
__TBB_atomic_flag flag
0 if lock is released, 1 if lock is acquired.
Definition: spin_mutex.h:42
spin_mutex * my_mutex
Points to currently held mutex, or NULL if no lock is held.
Definition: spin_mutex.h:57
void const char const char int ITT_FORMAT __itt_group_sync s
void release()
Release lock.
Definition: spin_mutex.h:116
A lock that occupies a single byte.
Definition: spin_mutex.h:40
__TBB_Flag __TBB_LockByte(__TBB_atomic_flag &flag)
Definition: tbb_machine.h:926

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.