2 Copyright 2005-2010 Intel Corporation. All Rights Reserved.
4 This file is part of Threading Building Blocks.
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.
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.
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
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.
29 #ifndef __TBB_queuing_mutex_H
30 #define __TBB_queuing_mutex_H
32 #include "tbb_config.h"
34 #if !TBB_USE_EXCEPTIONS && _MSC_VER
35 // Suppress "C++ exception handler used, but unwind semantics are not enabled" warning in STL headers
36 #pragma warning (push)
37 #pragma warning (disable: 4530)
42 #if !TBB_USE_EXCEPTIONS && _MSC_VER
47 #include "tbb_profiling.h"
51 //! Queuing lock with local-only spinning.
52 /** @ingroup synchronization */
55 //! Construct unacquired mutex.
58 #if TBB_USE_THREADING_TOOLS
63 //! The scoped locking pattern
64 /** It helps to avoid the common problem of forgetting to release lock.
65 It also nicely provides the "node" for queuing locks. */
66 class scoped_lock: internal::no_copy {
67 //! Initialize fields to mean "no lock held".
71 internal::poison_pointer(next);
72 #endif /* TBB_USE_ASSERT */
75 //! Construct lock that has not acquired a mutex.
76 /** Equivalent to zero-initialization of *this. */
77 scoped_lock() {initialize();}
79 //! Acquire lock on given mutex.
80 scoped_lock( queuing_mutex& m ) {
85 //! Release lock (if lock is held).
87 if( mutex ) release();
90 //! Acquire lock on given mutex.
91 void __TBB_EXPORTED_METHOD acquire( queuing_mutex& m );
93 //! Acquire lock on given mutex if free (i.e. non-blocking)
94 bool __TBB_EXPORTED_METHOD try_acquire( queuing_mutex& m );
97 void __TBB_EXPORTED_METHOD release();
100 //! The pointer to the mutex owned, or NULL if not holding a mutex.
101 queuing_mutex* mutex;
103 //! The pointer to the next competitor for a mutex
106 //! The local spin-wait variable
107 /** Inverted (0 - blocked, 1 - acquired the mutex) for the sake of
108 zero-initialization. Defining it as an entire word instead of
109 a byte seems to help performance slightly. */
113 void __TBB_EXPORTED_METHOD internal_construct();
116 static const bool is_rw_mutex = false;
117 static const bool is_recursive_mutex = false;
118 static const bool is_fair_mutex = true;
120 friend class scoped_lock;
122 //! The last competitor requesting the lock
123 atomic<scoped_lock*> q_tail;
127 __TBB_DEFINE_PROFILING_SET_NAME(queuing_mutex)
131 #endif /* __TBB_queuing_mutex_H */