2 An example that shows how to implement the monitor synchronization concept.
3 See also http://en.wikipedia.org/wiki/Monitor_(synchronization) for more
4 information about this concept.
6 ----------------------------------------------------------------
8 Notice that the following BSD-style license applies to this one
9 file (monitor_example.cpp) only. The rest of Valgrind is licensed
10 under the terms of the GNU General Public License, version 2,
11 unless otherwise indicated. See the COPYING file in the source
12 distribution for details.
14 ----------------------------------------------------------------
16 This file is part of DRD, a heavyweight Valgrind tool for detecting
17 errors in multithreaded programs.
19 Copyright (C) 2008-2017 Bart Van Assche. All rights reserved.
21 Redistribution and use in source and binary forms, with or without
22 modification, are permitted provided that the following conditions
25 1. Redistributions of source code must retain the above copyright
26 notice, this list of conditions and the following disclaimer.
28 2. The origin of this software must not be misrepresented; you must
29 not claim that you wrote the original software. If you use this
30 software in a product, an acknowledgment in the product
31 documentation would be appreciated but is not required.
33 3. Altered source versions must be plainly marked as such, and must
34 not be misrepresented as being the original software.
36 4. The name of the author may not be used to endorse or promote
37 products derived from this software without specific prior written
40 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
41 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
42 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
44 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
46 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
47 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
48 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
49 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
50 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 ----------------------------------------------------------------
54 Notice that the above BSD-style license applies to this one
55 file (monitor_example.cpp) only. The rest of Valgrind is licensed
56 under the terms of the GNU General Public License, version 2,
57 unless otherwise indicated. See the COPYING file in the source
58 distribution for details.
60 ----------------------------------------------------------------
82 pthread_mutexattr_t mutexattr
;
83 pthread_mutexattr_init(&mutexattr
);
84 pthread_mutexattr_settype(&mutexattr
, PTHREAD_MUTEX_RECURSIVE
);
85 pthread_mutex_init(&m_mutex
, &mutexattr
);
86 pthread_mutexattr_destroy(&mutexattr
);
87 pthread_condattr_t condattr
;
88 pthread_condattr_init(&condattr
);
89 #if defined(HAVE_PTHREAD_CONDATTR_SETCLOCK)
90 pthread_condattr_setclock(&condattr
, CLOCK_MONOTONIC
);
92 pthread_cond_init(&m_cond
, &condattr
);
93 pthread_condattr_destroy(&condattr
);
97 assert(m_recursion_count
== 0);
98 pthread_cond_destroy(&m_cond
);
99 pthread_mutex_destroy(&m_mutex
);
103 pthread_mutex_lock(&m_mutex
);
104 assert(m_recursion_count
>= 0);
105 if (++m_recursion_count
== 1)
107 m_owner
= pthread_self();
113 assert(m_recursion_count
>= 0);
114 pthread_mutex_unlock(&m_mutex
);
118 assert(m_recursion_count
== 1);
119 assert(m_owner
== pthread_self());
121 pthread_cond_wait(&m_cond
, &m_mutex
);
123 m_owner
= pthread_self();
127 assert(m_recursion_count
> 0);
128 pthread_cond_signal(&m_cond
);
130 void broadcast_signal()
132 assert(m_recursion_count
> 0);
133 pthread_cond_broadcast(&m_cond
);
135 bool is_locked_by_self()
138 pthread_mutex_lock(&m_mutex
);
139 result
= m_recursion_count
> 0 && m_owner
== pthread_self();
140 pthread_mutex_unlock(&m_mutex
);
145 Monitor(const Monitor
&);
146 Monitor
& operator=(const Monitor
&);
148 pthread_mutex_t m_mutex
;
149 pthread_cond_t m_cond
;
151 int m_recursion_count
;
158 ScopedLock(Monitor
& m
)
163 { if (m_locked
) unlock(); }
165 { assert(! m_locked
); m_monitor
.lock(); m_locked
= true; }
167 { assert(m_locked
); m_locked
= false; m_monitor
.unlock(); }
170 ScopedLock(const ScopedLock
&);
171 ScopedLock
& operator=(const ScopedLock
&);
186 ScopedLock
sl(m_monitor
);
189 void set(const int state
)
191 ScopedLock
sl(m_monitor
);
195 void wait(const int state
)
197 ScopedLock
sl(m_monitor
);
198 while (m_state
!= state
)
208 static StateVariable s_sv
;
211 static void* thread_func(void*)
220 int main(int, char**)
223 pthread_create(&tid
, 0, thread_func
, 0);
228 pthread_join(tid
, 0);
229 std::cerr
<< "Finished successfully.\n";