spin_mutex.h
Go to the documentation of this file.
/*
Copyright (c) 2005-2020 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef __TBB_spin_mutex_H
#define __TBB_spin_mutex_H
#define __TBB_spin_mutex_H_include_area
#include "internal/_warning_suppress_enable_notice.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 {
class spin_mutex : internal::mutex_copy_deprecated_and_disabled {
__TBB_atomic_flag flag;
public:
spin_mutex() : flag(0) {
#if TBB_USE_THREADING_TOOLS
internal_construct();
#endif
}
class scoped_lock : internal::no_copy {
private:
spin_mutex* my_mutex;
__TBB_Flag my_unlock_value;
void __TBB_EXPORTED_METHOD internal_acquire( spin_mutex& m );
bool __TBB_EXPORTED_METHOD internal_try_acquire( spin_mutex& m );
void __TBB_EXPORTED_METHOD internal_release();
friend class spin_mutex;
public:
scoped_lock() : my_mutex(NULL), my_unlock_value(0) {}
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*/
}
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*/
}
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*/
}
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 */
}
~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 */
}
}
};
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
void lock() {
#if TBB_USE_THREADING_TOOLS
aligned_space<scoped_lock> tmp;
new(tmp.begin()) scoped_lock(*this);
#else
__TBB_LockByte(flag);
#endif /* TBB_USE_THREADING_TOOLS*/
}
bool try_lock() {
#if TBB_USE_THREADING_TOOLS
aligned_space<scoped_lock> tmp;
return (new(tmp.begin()) scoped_lock)->internal_try_acquire(*this);
#else
return __TBB_TryLockByte(flag);
#endif /* TBB_USE_THREADING_TOOLS*/
}
void unlock() {
#if TBB_USE_THREADING_TOOLS
aligned_space<scoped_lock> tmp;
scoped_lock& s = *tmp.begin();
s.my_mutex = this;
s.internal_release();
#else
__TBB_UnlockByte(flag);
#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 {
#if ( __TBB_x86_32 || __TBB_x86_64 )
typedef interface7::internal::padded_mutex<interface7::internal::x86_eliding_mutex,false> speculative_spin_mutex;
#else
typedef interface7::internal::padded_mutex<spin_mutex,false> speculative_spin_mutex;
#endif
__TBB_DEFINE_PROFILING_SET_NAME(speculative_spin_mutex)
} // namespace tbb
#include "internal/_warning_suppress_disable_notice.h"
#undef __TBB_spin_mutex_H_include_area
#endif /* __TBB_spin_mutex_H */
aligned_space.h
_mutex_padding.h
_warning_suppress_disable_notice.h
_warning_suppress_enable_notice.h
_x86_eliding_mutex_impl.h
__TBB_Flag
unsigned char __TBB_Flag
Definition: gcc_generic.h:105
__TBB_UnlockByte
#define __TBB_UnlockByte
Definition: gcc_generic.h:211
__TBB_atomic_flag
__TBB_atomic __TBB_Flag __TBB_atomic_flag
Definition: gcc_generic.h:106
tbb_machine.h
__TBB_LockByte
__TBB_Flag __TBB_LockByte(__TBB_atomic_flag &flag)
Definition: tbb_machine.h:922
__TBB_TryLockByte
bool __TBB_TryLockByte(__TBB_atomic_flag &flag)
Definition: tbb_machine.h:916
tbb_profiling.h
__TBB_DEFINE_PROFILING_SET_NAME
#define __TBB_DEFINE_PROFILING_SET_NAME(sync_object_type)
Definition: tbb_profiling.h:115
tbb_stddef.h
__TBB_EXPORTED_METHOD
#define __TBB_EXPORTED_METHOD
Definition: tbb_stddef.h:98
s
void const char const char int ITT_FORMAT __itt_group_sync s
Definition: ittnotify_static.h:91
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
The graph class.
Definition: serial/tbb/parallel_for.h:46
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::interface7::internal::padded_mutex
Definition: _mutex_padding.h:32
tbb::spin_mutex
A lock that occupies a single byte.
Definition: spin_mutex.h:39
tbb::spin_mutex::try_lock
bool try_lock()
Try acquiring lock (non-blocking)
Definition: spin_mutex.h:158
tbb::spin_mutex::is_recursive_mutex
static const bool is_recursive_mutex
Definition: spin_mutex.h:141
tbb::spin_mutex::internal_construct
void __TBB_EXPORTED_METHOD internal_construct()
Internal constructor with ITT instrumentation.
Definition: spin_mutex.cpp:50
tbb::spin_mutex::spin_mutex
spin_mutex()
Construct unacquired lock.
Definition: spin_mutex.h:46
tbb::spin_mutex::is_rw_mutex
static const bool is_rw_mutex
Definition: spin_mutex.h:140
tbb::spin_mutex::lock
void lock()
Acquire lock.
Definition: spin_mutex.h:147
tbb::spin_mutex::is_fair_mutex
static const bool is_fair_mutex
Definition: spin_mutex.h:142
tbb::spin_mutex::unlock
void unlock()
Release lock.
Definition: spin_mutex.h:168
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::scoped_lock
friend class scoped_lock
Definition: spin_mutex.h:179
tbb::spin_mutex::scoped_lock
Represents acquisition of a mutex.
Definition: spin_mutex.h:53
tbb::spin_mutex::scoped_lock::scoped_lock
scoped_lock()
Construct without acquiring a mutex.
Definition: spin_mutex.h:77
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::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::scoped_lock::internal_acquire
void __TBB_EXPORTED_METHOD internal_acquire(spin_mutex &m)
Like acquire, but with ITT instrumentation.
Definition: spin_mutex.cpp:24
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::scoped_lock
scoped_lock(spin_mutex &m)
Construct and acquire lock on a mutex.
Definition: spin_mutex.h:80
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::acquire
void acquire(spin_mutex &m)
Acquire lock.
Definition: spin_mutex.h:92
tbb::spin_mutex::scoped_lock::release
void release()
Release lock.
Definition: spin_mutex.h:115
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::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::internal::no_copy
Base class for types that should not be copied or assigned.
Definition: tbb_stddef.h:330
tbb::internal::mutex_copy_deprecated_and_disabled
Definition: tbb_stddef.h:365

Copyright © 2005-2020 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.