2 Copyright 2005-2011 Intel Corporation. All Rights Reserved.
4 This file is part of Threading Building Blocks.
6 Threading Building Blocks is free software; you can redistribute it
7 and/or modify it under the terms of the GNU General Public License
8 version 2 as published by the Free Software Foundation.
10 Threading Building Blocks is distributed in the hope that it will be
11 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with Threading Building Blocks; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 As a special exception, you may use this file as part of a free software
20 library without restriction. Specifically, if other files instantiate
21 templates or use macros or inline functions from this file, or you compile
22 this file and link it with other files to produce an executable, this
23 file does not by itself cause the resulting executable to be covered by
24 the GNU General Public License. This exception does not however
25 invalidate any other reasons why the executable file might be covered by
26 the GNU General Public License.
29 #ifndef __TBB_spin_mutex_H
30 #define __TBB_spin_mutex_H
34 #include "aligned_space.h"
35 #include "tbb_stddef.h"
36 #include "tbb_machine.h"
37 #include "tbb_profiling.h"
41 //! A lock that occupies a single byte.
42 /** A spin_mutex is a spin mutex that fits in a single byte.
43 It should be used only for locking short critical sections
44 (typically less than 20 instructions) when fairness is not an issue.
45 If zero-initialized, the mutex is considered unheld.
46 @ingroup synchronization */
48 //! 0 if lock is released, 1 if lock is acquired.
49 __TBB_atomic_flag flag;
52 //! Construct unacquired lock.
53 /** Equivalent to zero-initialization of *this. */
54 spin_mutex() : flag(0) {
55 #if TBB_USE_THREADING_TOOLS
60 //! Represents acquisition of a mutex.
61 class scoped_lock : internal::no_copy {
63 //! Points to currently held mutex, or NULL if no lock is held.
66 //! Value to store into spin_mutex::flag to unlock the mutex.
67 __TBB_Flag my_unlock_value;
69 //! Like acquire, but with ITT instrumentation.
70 void __TBB_EXPORTED_METHOD internal_acquire( spin_mutex& m );
72 //! Like try_acquire, but with ITT instrumentation.
73 bool __TBB_EXPORTED_METHOD internal_try_acquire( spin_mutex& m );
75 //! Like release, but with ITT instrumentation.
76 void __TBB_EXPORTED_METHOD internal_release();
78 friend class spin_mutex;
81 //! Construct without acquiring a mutex.
82 scoped_lock() : my_mutex(NULL), my_unlock_value(0) {}
84 //! Construct and acquire lock on a mutex.
85 scoped_lock( spin_mutex& m ) {
86 #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
90 my_unlock_value = __TBB_LockByte(m.flag);
92 #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT*/
96 void acquire( spin_mutex& m ) {
97 #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
100 my_unlock_value = __TBB_LockByte(m.flag);
102 #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT*/
105 //! Try acquiring lock (non-blocking)
106 /** Return true if lock acquired; false otherwise. */
107 bool try_acquire( spin_mutex& m ) {
108 #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
109 return internal_try_acquire(m);
111 bool result = __TBB_TryLockByte(m.flag);
117 #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT*/
122 #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
125 __TBB_UnlockByte(my_mutex->flag, my_unlock_value);
127 #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT */
130 //! Destroy lock. If holding a lock, releases the lock first.
133 #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
136 __TBB_UnlockByte(my_mutex->flag, my_unlock_value);
137 #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT */
142 void __TBB_EXPORTED_METHOD internal_construct();
145 static const bool is_rw_mutex = false;
146 static const bool is_recursive_mutex = false;
147 static const bool is_fair_mutex = false;
149 // ISO C++0x compatibility methods
153 #if TBB_USE_THREADING_TOOLS
154 aligned_space<scoped_lock,1> tmp;
155 new(tmp.begin()) scoped_lock(*this);
157 __TBB_LockByte(flag);
158 #endif /* TBB_USE_THREADING_TOOLS*/
161 //! Try acquiring lock (non-blocking)
162 /** Return true if lock acquired; false otherwise. */
164 #if TBB_USE_THREADING_TOOLS
165 aligned_space<scoped_lock,1> tmp;
166 return (new(tmp.begin()) scoped_lock)->internal_try_acquire(*this);
168 return __TBB_TryLockByte(flag);
169 #endif /* TBB_USE_THREADING_TOOLS*/
174 #if TBB_USE_THREADING_TOOLS
175 aligned_space<scoped_lock,1> tmp;
176 scoped_lock& s = *tmp.begin();
178 s.my_unlock_value = 0;
179 s.internal_release();
181 __TBB_store_with_release(flag, 0);
182 #endif /* TBB_USE_THREADING_TOOLS */
185 friend class scoped_lock;
188 __TBB_DEFINE_PROFILING_SET_NAME(spin_mutex)
192 #endif /* __TBB_spin_mutex_H */