Intel(R) Threading Building Blocks Doxygen Documentation  version 4.2.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
_x86_eliding_mutex_impl.h
Go to the documentation of this file.
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 
34 
40 class x86_eliding_mutex : tbb::internal::mutex_copy_deprecated_and_disabled {
42  __TBB_atomic_flag flag;
43 
44  friend class padded_mutex<x86_eliding_mutex, false>;
45 
46 public:
48 
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.
63  private:
65  x86_eliding_mutex* my_mutex;
66 
67  public:
69  scoped_lock() : my_mutex(NULL) {}
70 
72  scoped_lock( x86_eliding_mutex& m ) : my_mutex(NULL) { acquire(m); }
73 
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 
83 
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 
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 
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 
122  void lock() {
123  __TBB_LockByteElided(flag);
124  }
125 
127 
128  bool try_lock() {
129  return __TBB_TryLockByteElided(flag);
130  }
131 
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 */
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:165
static const bool is_recursive_mutex
Definition: mutex.h:151
Base class for types that should not be copied or assigned.
Definition: tbb_stddef.h:330
__TBB_atomic __TBB_Flag __TBB_atomic_flag
Definition: gcc_generic.h:106
static const bool is_rw_mutex
Definition: mutex.h:150
friend class scoped_lock
Definition: mutex.h:79
void lock()
Acquire lock.
Definition: mutex.h:157
static const bool is_fair_mutex
Definition: mutex.h:152
Acquire.
Definition: atomic.h:57
bool try_lock()
Try acquiring lock (non-blocking)
Definition: mutex.h:174
Release.
Definition: atomic.h:59
void unlock()
Release lock.
Definition: mutex.h:190

Copyright © 2005-2020 Intel Corporation. All Rights Reserved.

Intel, Pentium, Intel Xeon, Itanium, Intel XScale and VTune are registered trademarks or trademarks of Intel Corporation or its subsidiaries in the United States and other countries.

* Other names and brands may be claimed as the property of others.