/usr/include/tbb/spin_mutex.h is in libtbb-dev 4.2~20130725-1.1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | /*
Copyright 2005-2013 Intel Corporation. All Rights Reserved.
This file is part of Threading Building Blocks.
Threading Building Blocks is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
Threading Building Blocks is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Threading Building Blocks; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
As a special exception, you may use this file as part of a free software
library without restriction. Specifically, if other files instantiate
templates or use macros or inline functions from this file, or you compile
this file and link it with other files to produce an executable, this
file does not by itself cause the resulting executable to be covered by
the GNU General Public License. This exception does not however
invalidate any other reasons why the executable file might be covered by
the GNU General Public License.
*/
#ifndef __TBB_spin_mutex_H
#define __TBB_spin_mutex_H
#include <cstddef>
#include <new>
#include "aligned_space.h"
#include "tbb_stddef.h"
#include "tbb_machine.h"
#include "tbb_profiling.h"
#include "internal/_mutex_padding.h"
namespace tbb {
//! A lock that occupies a single byte.
/** A spin_mutex is a spin mutex that fits in a single byte.
It should be used only for locking short critical sections
(typically less than 20 instructions) when fairness is not an issue.
If zero-initialized, the mutex is considered unheld.
@ingroup synchronization */
class spin_mutex {
//! 0 if lock is released, 1 if lock is acquired.
__TBB_atomic_flag flag;
public:
//! Construct unacquired lock.
/** Equivalent to zero-initialization of *this. */
spin_mutex() : flag(0) {
#if TBB_USE_THREADING_TOOLS
internal_construct();
#endif
}
//! Represents acquisition of a mutex.
class scoped_lock : internal::no_copy {
private:
//! Points to currently held mutex, or NULL if no lock is held.
spin_mutex* my_mutex;
//! Value to store into spin_mutex::flag to unlock the mutex.
/** This variable is no longer used. Instead, 0 and 1 are used to
represent that the lock is free and acquired, respectively.
We keep the member variable here to ensure backward compatibility */
__TBB_Flag my_unlock_value;
//! Like acquire, but with ITT instrumentation.
void __TBB_EXPORTED_METHOD internal_acquire( spin_mutex& m );
//! Like try_acquire, but with ITT instrumentation.
bool __TBB_EXPORTED_METHOD internal_try_acquire( spin_mutex& m );
//! Like release, but with ITT instrumentation.
void __TBB_EXPORTED_METHOD internal_release();
friend class spin_mutex;
public:
//! Construct without acquiring a mutex.
scoped_lock() : my_mutex(NULL), my_unlock_value(0) {}
//! Construct and acquire lock on a mutex.
scoped_lock( spin_mutex& m ) : my_unlock_value(0) {
internal::suppress_unused_warning(my_unlock_value);
#if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
my_mutex=NULL;
internal_acquire(m);
#else
my_mutex=&m;
__TBB_LockByte(m.flag);
#endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT*/
}
//! Acquire lock.
void acquire( spin_mutex& m ) {
#if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
internal_acquire(m);
#else
my_mutex = &m;
__TBB_LockByte(m.flag);
#endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT*/
}
//! Try acquiring lock (non-blocking)
/** Return true if lock acquired; false otherwise. */
bool try_acquire( spin_mutex& m ) {
#if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
return internal_try_acquire(m);
#else
bool result = __TBB_TryLockByte(m.flag);
if( result )
my_mutex = &m;
return result;
#endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT*/
}
//! Release lock
void release() {
#if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
internal_release();
#else
__TBB_UnlockByte(my_mutex->flag);
my_mutex = NULL;
#endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT */
}
//! Destroy lock. If holding a lock, releases the lock first.
~scoped_lock() {
if( my_mutex ) {
#if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
internal_release();
#else
__TBB_UnlockByte(my_mutex->flag);
#endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT */
}
}
};
//! Internal constructor with ITT instrumentation.
void __TBB_EXPORTED_METHOD internal_construct();
// Mutex traits
static const bool is_rw_mutex = false;
static const bool is_recursive_mutex = false;
static const bool is_fair_mutex = false;
// ISO C++0x compatibility methods
//! Acquire lock
void lock() {
#if TBB_USE_THREADING_TOOLS
aligned_space<scoped_lock,1> tmp;
new(tmp.begin()) scoped_lock(*this);
#else
__TBB_LockByte(flag);
#endif /* TBB_USE_THREADING_TOOLS*/
}
//! Try acquiring lock (non-blocking)
/** Return true if lock acquired; false otherwise. */
bool try_lock() {
#if TBB_USE_THREADING_TOOLS
aligned_space<scoped_lock,1> tmp;
return (new(tmp.begin()) scoped_lock)->internal_try_acquire(*this);
#else
return __TBB_TryLockByte(flag);
#endif /* TBB_USE_THREADING_TOOLS*/
}
//! Release lock
void unlock() {
#if TBB_USE_THREADING_TOOLS
aligned_space<scoped_lock,1> tmp;
scoped_lock& s = *tmp.begin();
s.my_mutex = this;
s.internal_release();
#else
__TBB_store_with_release(flag, 0);
#endif /* TBB_USE_THREADING_TOOLS */
}
friend class scoped_lock;
}; // end of spin_mutex
__TBB_DEFINE_PROFILING_SET_NAME(spin_mutex)
} // namespace tbb
#if ( __TBB_x86_32 || __TBB_x86_64 )
#include "internal/_x86_eliding_mutex_impl.h"
#endif
namespace tbb {
//! A cross-platform spin mutex with speculative lock acquisition.
/** On platforms with proper HW support, this lock may speculatively execute
its critical sections, using HW mechanisms to detect real data races and
ensure atomicity of the critical sections. In particular, it uses
Intel(R) Transactional Synchronization Extensions (Intel(R) TSX).
Without such HW support, it behaves like a spin_mutex.
It should be used for locking short critical sections where the lock is
contended but the data it protects are not. If zero-initialized, the
mutex is considered unheld.
@ingroup synchronization */
#if ( __TBB_x86_32 || __TBB_x86_64 )
typedef interface7::internal::padded_mutex<interface7::internal::x86_eliding_mutex> speculative_spin_mutex;
#else
typedef interface7::internal::padded_mutex<spin_mutex> speculative_spin_mutex;
#endif
__TBB_DEFINE_PROFILING_SET_NAME(speculative_spin_mutex)
} // namespace tbb
#endif /* __TBB_spin_mutex_H */
|