spin_rw_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_rw_mutex_H
#define __TBB_spin_rw_mutex_H
#include "tbb_stddef.h"
#include "tbb_machine.h"
#include "tbb_profiling.h"
#include "internal/_mutex_padding.h"
namespace tbb {
#if __TBB_TSX_AVAILABLE
namespace interface8 { namespace internal {
class x86_rtm_rw_mutex;
}}
#endif
class spin_rw_mutex_v3;
typedef spin_rw_mutex_v3 spin_rw_mutex;
class spin_rw_mutex_v3 : internal::mutex_copy_deprecated_and_disabled {
bool __TBB_EXPORTED_METHOD internal_acquire_writer();
void __TBB_EXPORTED_METHOD internal_release_writer();
void __TBB_EXPORTED_METHOD internal_acquire_reader();
bool __TBB_EXPORTED_METHOD internal_upgrade();
void __TBB_EXPORTED_METHOD internal_downgrade();
void __TBB_EXPORTED_METHOD internal_release_reader();
bool __TBB_EXPORTED_METHOD internal_try_acquire_writer();
bool __TBB_EXPORTED_METHOD internal_try_acquire_reader();
public:
spin_rw_mutex_v3() : state(0) {
#if TBB_USE_THREADING_TOOLS
internal_construct();
#endif
}
#if TBB_USE_ASSERT
~spin_rw_mutex_v3() {
__TBB_ASSERT( !state, "destruction of an acquired mutex");
};
#endif /* TBB_USE_ASSERT */
class scoped_lock : internal::no_copy {
#if __TBB_TSX_AVAILABLE
friend class tbb::interface8::internal::x86_rtm_rw_mutex;
#endif
public:
scoped_lock() : mutex(NULL), is_writer(false) {}
scoped_lock( spin_rw_mutex& m, bool write = true ) : mutex(NULL) {
acquire(m, write);
}
~scoped_lock() {
if( mutex ) release();
}
void acquire( spin_rw_mutex& m, bool write = true ) {
__TBB_ASSERT( !mutex, "holding mutex already" );
is_writer = write;
mutex = &m;
if( write ) mutex->internal_acquire_writer();
else mutex->internal_acquire_reader();
}
bool upgrade_to_writer() {
__TBB_ASSERT( mutex, "mutex is not acquired" );
if (is_writer) return true; // Already a writer
is_writer = true;
return mutex->internal_upgrade();
}
void release() {
__TBB_ASSERT( mutex, "mutex is not acquired" );
spin_rw_mutex *m = mutex;
mutex = NULL;
#if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
if( is_writer ) m->internal_release_writer();
else m->internal_release_reader();
#else
if( is_writer ) __TBB_AtomicAND( &m->state, READERS );
else __TBB_FetchAndAddWrelease( &m->state, -(intptr_t)ONE_READER);
#endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT */
}
bool downgrade_to_reader() {
__TBB_ASSERT( mutex, "mutex is not acquired" );
if (!is_writer) return true; // Already a reader
#if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
mutex->internal_downgrade();
#else
__TBB_FetchAndAddW( &mutex->state, ((intptr_t)ONE_READER-WRITER));
#endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT */
is_writer = false;
return true;
}
bool try_acquire( spin_rw_mutex& m, bool write = true ) {
__TBB_ASSERT( !mutex, "holding mutex already" );
bool result;
is_writer = write;
result = write? m.internal_try_acquire_writer()
: m.internal_try_acquire_reader();
if( result )
mutex = &m;
return result;
}
protected:
spin_rw_mutex* mutex;
bool is_writer;
};
// Mutex traits
static const bool is_rw_mutex = true;
static const bool is_recursive_mutex = false;
static const bool is_fair_mutex = false;
// ISO C++0x compatibility methods
void lock() {internal_acquire_writer();}
bool try_lock() {return internal_try_acquire_writer();}
void unlock() {
#if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
if( state&WRITER ) internal_release_writer();
else internal_release_reader();
#else
if( state&WRITER ) __TBB_AtomicAND( &state, READERS );
else __TBB_FetchAndAddWrelease( &state, -(intptr_t)ONE_READER);
#endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT */
}
// Methods for reader locks that resemble ISO C++0x compatibility methods.
void lock_read() {internal_acquire_reader();}
bool try_lock_read() {return internal_try_acquire_reader();}
protected:
typedef intptr_t state_t;
static const state_t WRITER = 1;
static const state_t WRITER_PENDING = 2;
static const state_t READERS = ~(WRITER | WRITER_PENDING);
static const state_t ONE_READER = 4;
static const state_t BUSY = WRITER | READERS;
state_t state;
private:
void __TBB_EXPORTED_METHOD internal_construct();
};
__TBB_DEFINE_PROFILING_SET_NAME(spin_rw_mutex)
} // namespace tbb
#if __TBB_TSX_AVAILABLE
#include "internal/_x86_rtm_rw_mutex_impl.h"
#endif
namespace tbb {
namespace interface8 {
#if __TBB_TSX_AVAILABLE
typedef interface7::internal::padded_mutex<tbb::interface8::internal::x86_rtm_rw_mutex,true> speculative_spin_rw_mutex;
#else
typedef interface7::internal::padded_mutex<tbb::spin_rw_mutex,true> speculative_spin_rw_mutex;
#endif
} // namespace interface8
using interface8::speculative_spin_rw_mutex;
__TBB_DEFINE_PROFILING_SET_NAME(speculative_spin_rw_mutex)
} // namespace tbb
#endif /* __TBB_spin_rw_mutex_H */
tbb::spin_rw_mutex_v3::scoped_lock::release
void release()
Release lock.
Definition: spin_rw_mutex.h:124
internal
Definition: _flow_graph_async_msg_impl.h:24
tbb::spin_rw_mutex_v3::internal_acquire_writer
bool __TBB_EXPORTED_METHOD internal_acquire_writer()
Internal acquire write lock.
Definition: spin_rw_mutex.cpp:37
tbb::spin_rw_mutex_v3::scoped_lock::scoped_lock
scoped_lock(spin_rw_mutex &m, bool write=true)
Acquire lock on given mutex.
Definition: spin_rw_mutex.h:96
__TBB_ASSERT
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:165
tbb::internal::no_copy
Base class for types that should not be copied or assigned.
Definition: tbb_stddef.h:330
tbb::spin_rw_mutex_v3::scoped_lock::~scoped_lock
~scoped_lock()
Release lock (if lock is held).
Definition: spin_rw_mutex.h:101
_x86_rtm_rw_mutex_impl.h
tbb::spin_rw_mutex_v3::scoped_lock::upgrade_to_writer
bool upgrade_to_writer()
Upgrade reader to become a writer.
Definition: spin_rw_mutex.h:116
tbb::spin_rw_mutex_v3::WRITER
static const state_t WRITER
Definition: spin_rw_mutex.h:208
tbb::spin_rw_mutex_v3::is_rw_mutex
static const bool is_rw_mutex
Definition: spin_rw_mutex.h:173
tbb
The graph class.
Definition: serial/tbb/parallel_for.h:46
tbb::spin_rw_mutex_v3::READERS
static const state_t READERS
Definition: spin_rw_mutex.h:210
tbb::spin_rw_mutex_v3::state_t
intptr_t state_t
Definition: spin_rw_mutex.h:207
tbb::spin_rw_mutex_v3::scoped_lock::try_acquire
bool try_acquire(spin_rw_mutex &m, bool write=true)
Try acquire lock on given mutex.
Definition: spin_rw_mutex.h:151
tbb::spin_rw_mutex
spin_rw_mutex_v3 spin_rw_mutex
Definition: spin_rw_mutex.h:33
tbb::spin_rw_mutex_v3::scoped_lock::acquire
void acquire(spin_rw_mutex &m, bool write=true)
Acquire lock on given mutex.
Definition: spin_rw_mutex.h:106
tbb::spin_rw_mutex_v3::scoped_lock::is_writer
bool is_writer
If mutex!=NULL, then is_writer is true if holding a writer lock, false if holding a reader lock.
Definition: spin_rw_mutex.h:169
tbb::spin_rw_mutex_v3::unlock
void unlock()
Release lock.
Definition: spin_rw_mutex.h:187
tbb::spin_rw_mutex_v3::internal_construct
void __TBB_EXPORTED_METHOD internal_construct()
Definition: spin_rw_mutex.cpp:152
tbb::interface7::internal::padded_mutex
Definition: _mutex_padding.h:32
tbb::spin_rw_mutex_v3
Fast, unfair, spinning reader-writer lock with backoff and writer-preference.
Definition: spin_rw_mutex.h:38
tbb::interface8::speculative_spin_rw_mutex
interface7::internal::padded_mutex< tbb::spin_rw_mutex, true > speculative_spin_rw_mutex
A cross-platform spin reader/writer mutex with speculative lock acquisition.
Definition: spin_rw_mutex.h:245
tbb_profiling.h
tbb::spin_rw_mutex_v3::internal_acquire_reader
void __TBB_EXPORTED_METHOD internal_acquire_reader()
Internal acquire read lock.
Definition: spin_rw_mutex.cpp:62
tbb::spin_rw_mutex_v3::internal_release_reader
void __TBB_EXPORTED_METHOD internal_release_reader()
Internal release read lock.
Definition: spin_rw_mutex.cpp:115
tbb::spin_rw_mutex_v3::scoped_lock::scoped_lock
scoped_lock()
Construct lock that has not acquired a mutex.
Definition: spin_rw_mutex.h:93
tbb::spin_rw_mutex_v3::try_lock_read
bool try_lock_read()
Try acquiring reader lock (non-blocking)
Definition: spin_rw_mutex.h:204
tbb::spin_rw_mutex_v3::is_fair_mutex
static const bool is_fair_mutex
Definition: spin_rw_mutex.h:175
_mutex_padding.h
tbb::spin_rw_mutex_v3::internal_downgrade
void __TBB_EXPORTED_METHOD internal_downgrade()
Out of line code for downgrading a writer to a reader.
Definition: spin_rw_mutex.cpp:108
tbb::spin_rw_mutex_v3::WRITER_PENDING
static const state_t WRITER_PENDING
Definition: spin_rw_mutex.h:209
tbb::spin_rw_mutex_v3::internal_upgrade
bool __TBB_EXPORTED_METHOD internal_upgrade()
Internal upgrade reader to become a writer.
Definition: spin_rw_mutex.cpp:82
__TBB_AtomicAND
void __TBB_AtomicAND(volatile void *operand, uintptr_t addend)
Definition: tbb_machine.h:891
tbb::spin_rw_mutex_v3::is_recursive_mutex
static const bool is_recursive_mutex
Definition: spin_rw_mutex.h:174
tbb::internal::mutex_copy_deprecated_and_disabled
Definition: tbb_stddef.h:365
tbb::spin_rw_mutex_v3::scoped_lock::downgrade_to_reader
bool downgrade_to_reader()
Downgrade writer to become a reader.
Definition: spin_rw_mutex.h:138
tbb_machine.h
tbb::spin_rw_mutex_v3::internal_try_acquire_writer
bool __TBB_EXPORTED_METHOD internal_try_acquire_writer()
Internal try_acquire write lock.
Definition: spin_rw_mutex.cpp:123
tbb::spin_rw_mutex_v3::scoped_lock
The scoped locking pattern.
Definition: spin_rw_mutex.h:86
tbb::spin_rw_mutex_v3::lock_read
void lock_read()
Acquire reader lock.
Definition: spin_rw_mutex.h:200
tbb::spin_rw_mutex_v3::internal_try_acquire_reader
bool __TBB_EXPORTED_METHOD internal_try_acquire_reader()
Internal try_acquire read lock.
Definition: spin_rw_mutex.cpp:136
tbb_stddef.h
__TBB_DEFINE_PROFILING_SET_NAME
#define __TBB_DEFINE_PROFILING_SET_NAME(sync_object_type)
Definition: tbb_profiling.h:115
tbb::spin_rw_mutex_v3::ONE_READER
static const state_t ONE_READER
Definition: spin_rw_mutex.h:211
tbb::spin_rw_mutex_v3::internal_release_writer
void __TBB_EXPORTED_METHOD internal_release_writer()
Out of line code for releasing a write lock.
Definition: spin_rw_mutex.cpp:55
__TBB_EXPORTED_METHOD
#define __TBB_EXPORTED_METHOD
Definition: tbb_stddef.h:98
tbb::spin_rw_mutex_v3::BUSY
static const state_t BUSY
Definition: spin_rw_mutex.h:212
tbb::spin_rw_mutex_v3::scoped_lock::mutex
spin_rw_mutex * mutex
The pointer to the current mutex that is held, or NULL if no mutex is held.
Definition: spin_rw_mutex.h:165
__TBB_FetchAndAddWrelease
#define __TBB_FetchAndAddWrelease(P, V)
Definition: tbb_machine.h:312
tbb::spin_rw_mutex_v3::try_lock
bool try_lock()
Try acquiring writer lock (non-blocking)
Definition: spin_rw_mutex.h:184
tbb::spin_rw_mutex_v3::lock
void lock()
Acquire writer lock.
Definition: spin_rw_mutex.h:180
tbb::spin_rw_mutex_v3::spin_rw_mutex_v3
spin_rw_mutex_v3()
Construct unacquired mutex.
Definition: spin_rw_mutex.h:70
tbb::spin_rw_mutex_v3::state
state_t state
State of lock.
Definition: spin_rw_mutex.h:217

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.