]> git.sesse.net Git - casparcg/blob - tbb/include/tbb/spin_mutex.h
2.0.0.2: Updated tbb version.
[casparcg] / tbb / include / tbb / spin_mutex.h
1 /*
2     Copyright 2005-2011 Intel Corporation.  All Rights Reserved.
3
4     This file is part of Threading Building Blocks.
5
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.
9
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.
14
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
18
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.
27 */
28
29 #ifndef __TBB_spin_mutex_H
30 #define __TBB_spin_mutex_H
31
32 #include <cstddef>
33 #include <new>
34 #include "aligned_space.h"
35 #include "tbb_stddef.h"
36 #include "tbb_machine.h"
37 #include "tbb_profiling.h"
38
39 namespace tbb {
40
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 */
47 class spin_mutex {
48     //! 0 if lock is released, 1 if lock is acquired.
49     __TBB_Byte flag;
50
51 public:
52     //! Construct unacquired lock.
53     /** Equivalent to zero-initialization of *this. */
54     spin_mutex() : flag(0) {
55 #if TBB_USE_THREADING_TOOLS
56         internal_construct();
57 #endif
58     }
59
60     //! Represents acquisition of a mutex.
61     class scoped_lock : internal::no_copy {
62     private:
63         //! Points to currently held mutex, or NULL if no lock is held.
64         spin_mutex* my_mutex; 
65
66         //! Value to store into spin_mutex::flag to unlock the mutex.
67         uintptr_t my_unlock_value;
68
69         //! Like acquire, but with ITT instrumentation.
70         void __TBB_EXPORTED_METHOD internal_acquire( spin_mutex& m );
71
72         //! Like try_acquire, but with ITT instrumentation.
73         bool __TBB_EXPORTED_METHOD internal_try_acquire( spin_mutex& m );
74
75         //! Like release, but with ITT instrumentation.
76         void __TBB_EXPORTED_METHOD internal_release();
77
78         friend class spin_mutex;
79
80     public:
81         //! Construct without acquiring a mutex.
82         scoped_lock() : my_mutex(NULL), my_unlock_value(0) {}
83
84         //! Construct and acquire lock on a mutex.
85         scoped_lock( spin_mutex& m ) { 
86 #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
87             my_mutex=NULL;
88             internal_acquire(m);
89 #else
90             my_unlock_value = __TBB_LockByte(m.flag);
91             my_mutex=&m;
92 #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT*/
93         }
94
95         //! Acquire lock.
96         void acquire( spin_mutex& m ) {
97 #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
98             internal_acquire(m);
99 #else
100             my_unlock_value = __TBB_LockByte(m.flag);
101             my_mutex = &m;
102 #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT*/
103         }
104
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);
110 #else
111             bool result = __TBB_TryLockByte(m.flag);
112             if( result ) {
113                 my_unlock_value = 0;
114                 my_mutex = &m;
115             }
116             return result;
117 #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT*/
118         }
119
120         //! Release lock
121         void release() {
122 #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
123             internal_release();
124 #else
125             __TBB_UnlockByte(my_mutex->flag, static_cast<__TBB_Byte>(my_unlock_value));
126             my_mutex = NULL;
127 #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT */
128         }
129
130         //! Destroy lock.  If holding a lock, releases the lock first.
131         ~scoped_lock() {
132             if( my_mutex ) {
133 #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
134                 internal_release();
135 #else
136                 __TBB_UnlockByte(my_mutex->flag, static_cast<__TBB_Byte>(my_unlock_value));
137 #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT */
138             }
139         }
140     };
141
142     void __TBB_EXPORTED_METHOD internal_construct();
143
144     // Mutex traits
145     static const bool is_rw_mutex = false;
146     static const bool is_recursive_mutex = false;
147     static const bool is_fair_mutex = false;
148
149     // ISO C++0x compatibility methods
150
151     //! Acquire lock
152     void lock() {
153 #if TBB_USE_THREADING_TOOLS
154         aligned_space<scoped_lock,1> tmp;
155         new(tmp.begin()) scoped_lock(*this);
156 #else
157         __TBB_LockByte(flag);
158 #endif /* TBB_USE_THREADING_TOOLS*/
159     }
160
161     //! Try acquiring lock (non-blocking)
162     /** Return true if lock acquired; false otherwise. */
163     bool try_lock() {
164 #if TBB_USE_THREADING_TOOLS
165         aligned_space<scoped_lock,1> tmp;
166         return (new(tmp.begin()) scoped_lock)->internal_try_acquire(*this);
167 #else
168         return __TBB_TryLockByte(flag);
169 #endif /* TBB_USE_THREADING_TOOLS*/
170     }
171
172     //! Release lock
173     void unlock() {
174 #if TBB_USE_THREADING_TOOLS
175         aligned_space<scoped_lock,1> tmp;
176         scoped_lock& s = *tmp.begin();
177         s.my_mutex = this;
178         s.my_unlock_value = 0;
179         s.internal_release();
180 #else
181         __TBB_store_with_release(flag, 0);
182 #endif /* TBB_USE_THREADING_TOOLS */
183     }
184
185     friend class scoped_lock;
186 };
187
188 __TBB_DEFINE_PROFILING_SET_NAME(spin_mutex)
189
190 } // namespace tbb
191
192 #endif /* __TBB_spin_mutex_H */