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:
77  scoped_lock() : my_mutex(NULL), my_unlock_value(0) {}
78 
80  scoped_lock( spin_mutex& m ) : my_unlock_value(0) {
81  internal::suppress_unused_warning(my_unlock_value);
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
119  __TBB_UnlockByte(my_mutex->flag);
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
130  __TBB_UnlockByte(my_mutex->flag);
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
152  __TBB_LockByte(flag);
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
175  __TBB_UnlockByte(flag);
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 )
204 #else
206 #endif
207 __TBB_DEFINE_PROFILING_SET_NAME(speculative_spin_mutex)
208 
209 } // namespace tbb
210 
212 #undef __TBB_spin_mutex_H_include_area
213 
214 #endif /* __TBB_spin_mutex_H */
static const bool is_fair_mutex
Definition: spin_mutex.h:142
#define __TBB_DEFINE_PROFILING_SET_NAME(sync_object_type)
static const bool is_rw_mutex
Definition: spin_mutex.h:140
bool try_acquire(spin_mutex &m)
Try acquiring lock (non-blocking)
Definition: spin_mutex.h:103
__TBB_atomic __TBB_Flag __TBB_atomic_flag
Definition: gcc_generic.h:106
Base class for types that should not be copied or assigned.
Definition: tbb_stddef.h:330
The graph class.
static const bool is_recursive_mutex
Definition: spin_mutex.h:141
void __TBB_EXPORTED_METHOD internal_acquire(spin_mutex &m)
Like acquire, but with ITT instrumentation.
Definition: spin_mutex.cpp:24
void unlock()
Release lock.
Definition: spin_mutex.h:168
bool __TBB_TryLockByte(__TBB_atomic_flag &flag)
Definition: tbb_machine.h:916
void __TBB_EXPORTED_METHOD internal_construct()
Internal constructor with ITT instrumentation.
Definition: spin_mutex.cpp:50
#define __TBB_UnlockByte
Definition: gcc_generic.h:211
void release()
Release lock.
Definition: spin_mutex.h:115
__TBB_Flag __TBB_LockByte(__TBB_atomic_flag &flag)
Definition: tbb_machine.h:922
bool __TBB_EXPORTED_METHOD internal_try_acquire(spin_mutex &m)
Like try_acquire, but with ITT instrumentation.
Definition: spin_mutex.cpp:40
A lock that occupies a single byte.
Definition: spin_mutex.h:39
void const char const char int ITT_FORMAT __itt_group_sync s
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_atomic_flag flag
0 if lock is released, 1 if lock is acquired.
Definition: spin_mutex.h:41
bool try_lock()
Try acquiring lock (non-blocking)
Definition: spin_mutex.h:158
spin_mutex()
Construct unacquired lock.
Definition: spin_mutex.h:46
scoped_lock()
Construct without acquiring a mutex.
Definition: spin_mutex.h:77
Represents acquisition of a mutex.
Definition: spin_mutex.h:53
unsigned char __TBB_Flag
Definition: gcc_generic.h:105
#define __TBB_EXPORTED_METHOD
Definition: tbb_stddef.h:98
void suppress_unused_warning(const T1 &)
Utility template function to prevent "unused" warnings by various compilers.
Definition: tbb_stddef.h:398
spin_mutex * my_mutex
Points to currently held mutex, or NULL if no lock is held.
Definition: spin_mutex.h:56
void lock()
Acquire lock.
Definition: spin_mutex.h:147
void __TBB_EXPORTED_METHOD internal_release()
Like release, but with ITT instrumentation.
Definition: spin_mutex.cpp:32
__TBB_Flag my_unlock_value
Value to store into spin_mutex::flag to unlock the mutex.
Definition: spin_mutex.h:62
scoped_lock(spin_mutex &m)
Construct and acquire lock on a mutex.
Definition: spin_mutex.h:80
~scoped_lock()
Destroy lock. If holding a lock, releases the lock first.
Definition: spin_mutex.h:125
void acquire(spin_mutex &m)
Acquire lock.
Definition: spin_mutex.h:92

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.