]> git.sesse.net Git - casparcg/blob - dependencies64/tbb/include/tbb/internal/_x86_eliding_mutex_impl.h
Updated some libraries to newer versions and/or versions compiled for vc12 (freeimage...
[casparcg] / dependencies64 / tbb / include / tbb / internal / _x86_eliding_mutex_impl.h
1 /*
2     Copyright 2005-2014 Intel Corporation.  All Rights Reserved.
3
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
12
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.
19 */
20
21 #ifndef __TBB__x86_eliding_mutex_impl_H
22 #define __TBB__x86_eliding_mutex_impl_H
23
24 #ifndef __TBB_spin_mutex_H
25 #error Do not #include this internal file directly; use public TBB headers instead.
26 #endif
27
28 #if ( __TBB_x86_32 || __TBB_x86_64 )
29
30 namespace tbb {
31 namespace interface7 {
32 namespace internal {
33
34 template<typename Mutex, bool is_rw>
35 class padded_mutex;
36
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;
47
48     friend class padded_mutex<x86_eliding_mutex, false>;
49
50 public:
51     //! Construct unacquired lock.
52     /** Equivalent to zero-initialization of *this. */
53     x86_eliding_mutex() : flag(0) {}
54
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
58 #else
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.
62 private:
63 #endif
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 {
67     private:
68         //! Points to currently held mutex, or NULL if no lock is held.
69         x86_eliding_mutex* my_mutex;
70
71     public:
72         //! Construct without acquiring a mutex.
73         scoped_lock() : my_mutex(NULL) {}
74
75         //! Construct and acquire lock on a mutex.
76         scoped_lock( x86_eliding_mutex& m ) : my_mutex(NULL) { acquire(m); }
77
78         //! Acquire lock.
79         void acquire( x86_eliding_mutex& m ) {
80             __TBB_ASSERT( !my_mutex, "already holding a lock" );
81
82             my_mutex=&m;
83             my_mutex->lock();
84         }
85
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" );
90
91             bool result = m.try_lock();
92             if( result ) {
93                 my_mutex = &m;
94             }
95             return result;
96         }
97
98         //! Release lock
99         void release() {
100             __TBB_ASSERT( my_mutex, "release on scoped_lock that is not holding a lock" );
101
102             my_mutex->unlock();
103             my_mutex = NULL;
104         }
105
106         //! Destroy lock.  If holding a lock, releases the lock first.
107         ~scoped_lock() {
108             if( my_mutex ) {
109                 release();
110             }
111         }
112     };
113 #if __TBB_USE_X86_ELIDING_MUTEX || __TBB_GCC_VERSION < 40000
114 #else
115 public:
116 #endif  /* __TBB_USE_X86_ELIDING_MUTEX */
117
118     // Mutex traits
119     static const bool is_rw_mutex = false;
120     static const bool is_recursive_mutex = false;
121     static const bool is_fair_mutex = false;
122
123     // ISO C++0x compatibility methods
124
125     //! Acquire lock
126     void lock() {
127         __TBB_LockByteElided(flag);
128     }
129
130     //! Try acquiring lock (non-blocking)
131     /** Return true if lock acquired; false otherwise. */
132     bool try_lock() {
133         return __TBB_TryLockByteElided(flag);
134     }
135
136     //! Release lock
137     void unlock() {
138         __TBB_UnlockByteElided( flag );
139     }
140 }; // end of x86_eliding_mutex
141
142 } // namespace internal
143 } // namespace interface7
144 } // namespace tbb
145
146 #endif /* ( __TBB_x86_32 || __TBB_x86_64 ) */
147
148 #endif /* __TBB__x86_eliding_mutex_impl_H */