2 Copyright 2005-2014 Intel Corporation. All Rights Reserved.
4 This file is part of Threading Building Blocks. Threading Building Blocks is free software;
5 you can redistribute it and/or modify it under the terms of the GNU General Public License
6 version 2 as published by the Free Software Foundation. Threading Building Blocks is
7 distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
8 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9 See the GNU General Public License for more details. You should have received a copy of
10 the GNU General Public License along with Threading Building Blocks; if not, write to the
11 Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
13 As a special exception, you may use this file as part of a free software library without
14 restriction. Specifically, if other files instantiate templates or use macros or inline
15 functions from this file, or you compile this file and link it with other files to produce
16 an executable, this file does not by itself cause the resulting executable to be covered
17 by the GNU General Public License. This exception does not however invalidate any other
18 reasons why the executable file might be covered by the GNU General Public License.
21 #ifndef __TBB__x86_eliding_mutex_impl_H
22 #define __TBB__x86_eliding_mutex_impl_H
24 #ifndef __TBB_spin_mutex_H
25 #error Do not #include this internal file directly; use public TBB headers instead.
28 #if ( __TBB_x86_32 || __TBB_x86_64 )
31 namespace interface7 {
34 template<typename Mutex, bool is_rw>
37 //! An eliding lock that occupies a single byte.
38 /** A x86_eliding_mutex is an HLE-enabled spin mutex. It is recommended to
39 put the mutex on a cache line that is not shared by the data it protects.
40 It should be used for locking short critical sections where the lock is
41 contended but the data it protects are not. If zero-initialized, the
42 mutex is considered unheld.
43 @ingroup synchronization */
44 class x86_eliding_mutex : tbb::internal::mutex_copy_deprecated_and_disabled {
45 //! 0 if lock is released, 1 if lock is acquired.
46 __TBB_atomic_flag flag;
48 friend class padded_mutex<x86_eliding_mutex, false>;
51 //! Construct unacquired lock.
52 /** Equivalent to zero-initialization of *this. */
53 x86_eliding_mutex() : flag(0) {}
55 // bug in gcc 3.x.x causes syntax error in spite of the friend declaration above.
56 // Make the scoped_lock public in that case.
57 #if __TBB_USE_X86_ELIDING_MUTEX || __TBB_GCC_VERSION < 40000
59 // by default we will not provide the scoped_lock interface. The user
60 // should use the padded version of the mutex. scoped_lock is used in
61 // padded_mutex template.
64 // scoped_lock in padded_mutex<> is the interface to use.
65 //! Represents acquisition of a mutex.
66 class scoped_lock : tbb::internal::no_copy {
68 //! Points to currently held mutex, or NULL if no lock is held.
69 x86_eliding_mutex* my_mutex;
72 //! Construct without acquiring a mutex.
73 scoped_lock() : my_mutex(NULL) {}
75 //! Construct and acquire lock on a mutex.
76 scoped_lock( x86_eliding_mutex& m ) : my_mutex(NULL) { acquire(m); }
79 void acquire( x86_eliding_mutex& m ) {
80 __TBB_ASSERT( !my_mutex, "already holding a lock" );
86 //! Try acquiring lock (non-blocking)
87 /** Return true if lock acquired; false otherwise. */
88 bool try_acquire( x86_eliding_mutex& m ) {
89 __TBB_ASSERT( !my_mutex, "already holding a lock" );
91 bool result = m.try_lock();
100 __TBB_ASSERT( my_mutex, "release on scoped_lock that is not holding a lock" );
106 //! Destroy lock. If holding a lock, releases the lock first.
113 #if __TBB_USE_X86_ELIDING_MUTEX || __TBB_GCC_VERSION < 40000
116 #endif /* __TBB_USE_X86_ELIDING_MUTEX */
119 static const bool is_rw_mutex = false;
120 static const bool is_recursive_mutex = false;
121 static const bool is_fair_mutex = false;
123 // ISO C++0x compatibility methods
127 __TBB_LockByteElided(flag);
130 //! Try acquiring lock (non-blocking)
131 /** Return true if lock acquired; false otherwise. */
133 return __TBB_TryLockByteElided(flag);
138 __TBB_UnlockByteElided( flag );
140 }; // end of x86_eliding_mutex
142 } // namespace internal
143 } // namespace interface7
146 #endif /* ( __TBB_x86_32 || __TBB_x86_64 ) */
148 #endif /* __TBB__x86_eliding_mutex_impl_H */