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 #include <cstddef>
21 #include <new>
22 #include "aligned_space.h"
23 #include "tbb_stddef.h"
24 #include "tbb_machine.h"
25 #include "tbb_profiling.h"
27 
28 namespace tbb {
29 
31 
36 class spin_mutex : internal::mutex_copy_deprecated_and_disabled {
39 
40 public:
42 
43  spin_mutex() : flag(0) {
44 #if TBB_USE_THREADING_TOOLS
46 #endif
47  }
48 
50  class scoped_lock : internal::no_copy {
51  private:
54 
56 
60 
63 
66 
69 
70  friend class spin_mutex;
71 
72  public:
75 
79 #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
80  my_mutex=NULL;
82 #else
83  my_mutex=&m;
85 #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT*/
86  }
87 
89  void acquire( spin_mutex& m ) {
90 #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
92 #else
93  my_mutex = &m;
95 #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT*/
96  }
97 
99 
100  bool try_acquire( spin_mutex& m ) {
101 #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
102  return internal_try_acquire(m);
103 #else
104  bool result = __TBB_TryLockByte(m.flag);
105  if( result )
106  my_mutex = &m;
107  return result;
108 #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT*/
109  }
110 
112  void release() {
113 #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
115 #else
117  my_mutex = NULL;
118 #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT */
119  }
120 
123  if( my_mutex ) {
124 #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
126 #else
128 #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT */
129  }
130  }
131  };
132 
135 
136  // Mutex traits
137  static const bool is_rw_mutex = false;
138  static const bool is_recursive_mutex = false;
139  static const bool is_fair_mutex = false;
140 
141  // ISO C++0x compatibility methods
142 
144  void lock() {
145 #if TBB_USE_THREADING_TOOLS
147  new(tmp.begin()) scoped_lock(*this);
148 #else
150 #endif /* TBB_USE_THREADING_TOOLS*/
151  }
152 
154 
155  bool try_lock() {
156 #if TBB_USE_THREADING_TOOLS
158  return (new(tmp.begin()) scoped_lock)->internal_try_acquire(*this);
159 #else
160  return __TBB_TryLockByte(flag);
161 #endif /* TBB_USE_THREADING_TOOLS*/
162  }
163 
165  void unlock() {
166 #if TBB_USE_THREADING_TOOLS
168  scoped_lock& s = *tmp.begin();
169  s.my_mutex = this;
170  s.internal_release();
171 #else
173 #endif /* TBB_USE_THREADING_TOOLS */
174  }
175 
176  friend class scoped_lock;
177 }; // end of spin_mutex
178 
180 
181 } // namespace tbb
182 
183 #if ( __TBB_x86_32 || __TBB_x86_64 )
185 #endif
186 
187 namespace tbb {
189 
199 #if ( __TBB_x86_32 || __TBB_x86_64 )
200 typedef interface7::internal::padded_mutex<interface7::internal::x86_eliding_mutex,false> speculative_spin_mutex;
201 #else
203 #endif
205 
206 } // namespace tbb
207 
208 #endif /* __TBB_spin_mutex_H */
void __TBB_EXPORTED_METHOD internal_release()
Like release, but with ITT instrumentation.
Definition: spin_mutex.cpp:32
interface7::internal::padded_mutex< spin_mutex, false > speculative_spin_mutex
A cross-platform spin mutex with speculative lock acquisition.
Definition: spin_mutex.h:202
Represents acquisition of a mutex.
Definition: spin_mutex.h:50
spin_mutex()
Construct unacquired lock.
Definition: spin_mutex.h:43
void const char const char int ITT_FORMAT __itt_group_sync s
~scoped_lock()
Destroy lock. If holding a lock, releases the lock first.
Definition: spin_mutex.h:122
spin_mutex * my_mutex
Points to currently held mutex, or NULL if no lock is held.
Definition: spin_mutex.h:53
#define __TBB_EXPORTED_METHOD
Definition: tbb_stddef.h:98
void release()
Release lock.
Definition: spin_mutex.h:112
__TBB_atomic_flag flag
0 if lock is released, 1 if lock is acquired.
Definition: spin_mutex.h:38
static const bool is_rw_mutex
Definition: spin_mutex.h:137
void unlock()
Release lock.
Definition: spin_mutex.h:165
scoped_lock()
Construct without acquiring a mutex.
Definition: spin_mutex.h:74
__TBB_Flag __TBB_LockByte(__TBB_atomic_flag &flag)
Definition: tbb_machine.h:919
unsigned char __TBB_Flag
Definition: gcc_generic.h:105
The graph class.
A lock that occupies a single byte.
Definition: spin_mutex.h:36
void acquire(spin_mutex &m)
Acquire lock.
Definition: spin_mutex.h:89
scoped_lock(spin_mutex &m)
Construct and acquire lock on a mutex.
Definition: spin_mutex.h:77
Block of space aligned sufficiently to construct an array T with N elements.
Definition: aligned_space.h:29
void lock()
Acquire lock.
Definition: spin_mutex.h:144
bool __TBB_TryLockByte(__TBB_atomic_flag &flag)
Definition: tbb_machine.h:913
static const bool is_fair_mutex
Definition: spin_mutex.h:139
void __TBB_EXPORTED_METHOD internal_construct()
Internal constructor with ITT instrumentation.
Definition: spin_mutex.cpp:50
void __TBB_EXPORTED_METHOD internal_acquire(spin_mutex &m)
Like acquire, but with ITT instrumentation.
Definition: spin_mutex.cpp:24
void suppress_unused_warning(const T1 &)
Utility template function to prevent "unused" warnings by various compilers.
Definition: tbb_stddef.h:377
static const bool is_recursive_mutex
Definition: spin_mutex.h:138
T * begin() const
Pointer to beginning of array.
Definition: aligned_space.h:35
bool try_lock()
Try acquiring lock (non-blocking)
Definition: spin_mutex.h:155
__TBB_atomic __TBB_Flag __TBB_atomic_flag
Definition: gcc_generic.h:106
friend class scoped_lock
Definition: spin_mutex.h:176
bool __TBB_EXPORTED_METHOD internal_try_acquire(spin_mutex &m)
Like try_acquire, but with ITT instrumentation.
Definition: spin_mutex.cpp:40
#define __TBB_UnlockByte
Definition: gcc_generic.h:211
bool try_acquire(spin_mutex &m)
Try acquiring lock (non-blocking)
Definition: spin_mutex.h:100
__TBB_Flag my_unlock_value
Value to store into spin_mutex::flag to unlock the mutex.
Definition: spin_mutex.h:59
#define __TBB_DEFINE_PROFILING_SET_NAME(sync_object_type)

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.