]> git.sesse.net Git - casparcg/blob - tbb30_20100406oss/include/tbb/queuing_mutex.h
2.0.2: Updated to boost 1.48.
[casparcg] / tbb30_20100406oss / include / tbb / queuing_mutex.h
1 /*
2     Copyright 2005-2010 Intel Corporation.  All Rights Reserved.
3
4     This file is part of Threading Building Blocks.
5
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.
9
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.
14
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
18
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.
27 */
28
29 #ifndef __TBB_queuing_mutex_H
30 #define __TBB_queuing_mutex_H
31
32 #include "tbb_config.h"
33
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)
38 #endif
39
40 #include <cstring>
41
42 #if !TBB_USE_EXCEPTIONS && _MSC_VER
43     #pragma warning (pop)
44 #endif
45
46 #include "atomic.h"
47 #include "tbb_profiling.h"
48
49 namespace tbb {
50
51 //! Queuing lock with local-only spinning.
52 /** @ingroup synchronization */
53 class queuing_mutex {
54 public:
55     //! Construct unacquired mutex.
56     queuing_mutex() {
57         q_tail = NULL;
58 #if TBB_USE_THREADING_TOOLS
59         internal_construct();
60 #endif
61     }
62
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".
68         void initialize() {
69             mutex = NULL;
70 #if TBB_USE_ASSERT
71             internal::poison_pointer(next);
72 #endif /* TBB_USE_ASSERT */
73         }
74     public:
75         //! Construct lock that has not acquired a mutex.
76         /** Equivalent to zero-initialization of *this. */
77         scoped_lock() {initialize();}
78
79         //! Acquire lock on given mutex.
80         scoped_lock( queuing_mutex& m ) {
81             initialize();
82             acquire(m);
83         }
84
85         //! Release lock (if lock is held).
86         ~scoped_lock() {
87             if( mutex ) release();
88         }
89
90         //! Acquire lock on given mutex.
91         void __TBB_EXPORTED_METHOD acquire( queuing_mutex& m );
92
93         //! Acquire lock on given mutex if free (i.e. non-blocking)
94         bool __TBB_EXPORTED_METHOD try_acquire( queuing_mutex& m );
95
96         //! Release lock.
97         void __TBB_EXPORTED_METHOD release();
98
99     private:
100         //! The pointer to the mutex owned, or NULL if not holding a mutex.
101         queuing_mutex* mutex;
102
103         //! The pointer to the next competitor for a mutex
104         scoped_lock *next;
105
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. */
110         uintptr_t going;
111     };
112
113     void __TBB_EXPORTED_METHOD internal_construct();
114
115     // Mutex traits
116     static const bool is_rw_mutex = false;
117     static const bool is_recursive_mutex = false;
118     static const bool is_fair_mutex = true;
119
120     friend class scoped_lock;
121 private:
122     //! The last competitor requesting the lock
123     atomic<scoped_lock*> q_tail;
124
125 };
126
127 __TBB_DEFINE_PROFILING_SET_NAME(queuing_mutex)
128
129 } // namespace tbb
130
131 #endif /* __TBB_queuing_mutex_H */