1 /*
2     Copyright (c) 2005-2020 Intel Corporation
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8         http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 
17 #ifndef __TBB__x86_eliding_mutex_impl_H
18 #define __TBB__x86_eliding_mutex_impl_H
19 
20 #ifndef __TBB_spin_mutex_H
21 #error Do not #include this internal file directly; use public TBB headers instead.
22 #endif
23 
24 #if ( __TBB_x86_32 || __TBB_x86_64 )
25 
26 namespace tbb {
27 namespace interface7 {
28 namespace internal {
29 
30 template<typename Mutex, bool is_rw>
31 class padded_mutex;
32 
33 //! An eliding lock that occupies a single byte.
34 /** A x86_eliding_mutex is an HLE-enabled spin mutex. It is recommended to
35     put the mutex on a cache line that is not shared by the data it protects.
36     It should be used for locking short critical sections where the lock is
37     contended but the data it protects are not.  If zero-initialized, the
38     mutex is considered unheld.
39     @ingroup synchronization */
40 class x86_eliding_mutex : tbb::internal::mutex_copy_deprecated_and_disabled {
41     //! 0 if lock is released, 1 if lock is acquired.
42     __TBB_atomic_flag flag;
43 
44     friend class padded_mutex<x86_eliding_mutex, false>;
45 
46 public:
47     //! Construct unacquired lock.
48     /** Equivalent to zero-initialization of *this. */
x86_eliding_mutex()49     x86_eliding_mutex() : flag(0) {}
50 
51 // bug in gcc 3.x.x causes syntax error in spite of the friend declaration above.
52 // Make the scoped_lock public in that case.
53 #if __TBB_USE_X86_ELIDING_MUTEX || __TBB_GCC_VERSION < 40000
54 #else
55     // by default we will not provide the scoped_lock interface.  The user
56     // should use the padded version of the mutex.  scoped_lock is used in
57     // padded_mutex template.
58 private:
59 #endif
60     // scoped_lock in padded_mutex<> is the interface to use.
61     //! Represents acquisition of a mutex.
62     class scoped_lock : tbb::internal::no_copy {
63     private:
64         //! Points to currently held mutex, or NULL if no lock is held.
65         x86_eliding_mutex* my_mutex;
66 
67     public:
68         //! Construct without acquiring a mutex.
scoped_lock()69         scoped_lock() : my_mutex(NULL) {}
70 
71         //! Construct and acquire lock on a mutex.
scoped_lock(x86_eliding_mutex & m)72         scoped_lock( x86_eliding_mutex& m ) : my_mutex(NULL) { acquire(m); }
73 
74         //! Acquire lock.
acquire(x86_eliding_mutex & m)75         void acquire( x86_eliding_mutex& m ) {
76             __TBB_ASSERT( !my_mutex, "already holding a lock" );
77 
78             my_mutex=&m;
79             my_mutex->lock();
80         }
81 
82         //! Try acquiring lock (non-blocking)
83         /** Return true if lock acquired; false otherwise. */
try_acquire(x86_eliding_mutex & m)84         bool try_acquire( x86_eliding_mutex& m ) {
85             __TBB_ASSERT( !my_mutex, "already holding a lock" );
86 
87             bool result = m.try_lock();
88             if( result ) {
89                 my_mutex = &m;
90             }
91             return result;
92         }
93 
94         //! Release lock
release()95         void release() {
96             __TBB_ASSERT( my_mutex, "release on scoped_lock that is not holding a lock" );
97 
98             my_mutex->unlock();
99             my_mutex = NULL;
100         }
101 
102         //! Destroy lock.  If holding a lock, releases the lock first.
~scoped_lock()103         ~scoped_lock() {
104             if( my_mutex ) {
105                 release();
106             }
107         }
108     };
109 #if __TBB_USE_X86_ELIDING_MUTEX || __TBB_GCC_VERSION < 40000
110 #else
111 public:
112 #endif  /* __TBB_USE_X86_ELIDING_MUTEX */
113 
114     // Mutex traits
115     static const bool is_rw_mutex = false;
116     static const bool is_recursive_mutex = false;
117     static const bool is_fair_mutex = false;
118 
119     // ISO C++0x compatibility methods
120 
121     //! Acquire lock
lock()122     void lock() {
123         __TBB_LockByteElided(flag);
124     }
125 
126     //! Try acquiring lock (non-blocking)
127     /** Return true if lock acquired; false otherwise. */
try_lock()128     bool try_lock() {
129         return __TBB_TryLockByteElided(flag);
130     }
131 
132     //! Release lock
unlock()133     void unlock() {
134         __TBB_UnlockByteElided( flag );
135     }
136 }; // end of x86_eliding_mutex
137 
138 } // namespace internal
139 } // namespace interface7
140 } // namespace tbb
141 
142 #endif /* ( __TBB_x86_32 || __TBB_x86_64 ) */
143 
144 #endif /* __TBB__x86_eliding_mutex_impl_H */
145