BRE12
_x86_eliding_mutex_impl.h
1 /*
2  Copyright 2005-2016 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 
38 
44 class x86_eliding_mutex : tbb::internal::mutex_copy_deprecated_and_disabled {
46  __TBB_atomic_flag flag;
47 
48  friend class padded_mutex<x86_eliding_mutex, false>;
49 
50 public:
52 
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.
66  class scoped_lock : tbb::internal::no_copy {
67  private:
69  x86_eliding_mutex* my_mutex;
70 
71  public:
73  scoped_lock() : my_mutex(NULL) {}
74 
76  scoped_lock( x86_eliding_mutex& m ) : my_mutex(NULL) { acquire(m); }
77 
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 
87 
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 
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 
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 
126  void lock() {
127  __TBB_LockByteElided(flag);
128  }
129 
131 
132  bool try_lock() {
133  return __TBB_TryLockByteElided(flag);
134  }
135 
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 */
Acquire.
Definition: atomic.h:47
Definition: _flow_graph_async_msg_impl.h:32
Release.
Definition: atomic.h:49
The namespace tbb contains all components of the library.
Definition: parallel_for.h:44