File: monitor_example.cpp

package info (click to toggle)
valgrind 1%3A3.12.0~svn20160714-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 120,428 kB
  • ctags: 70,855
  • sloc: ansic: 674,645; exp: 26,134; xml: 21,574; asm: 7,570; cpp: 7,567; makefile: 7,380; sh: 6,188; perl: 5,855; haskell: 195
file content (231 lines) | stat: -rw-r--r-- 6,036 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
   An example that shows how to implement the monitor synchronization concept.
   See also http://en.wikipedia.org/wiki/Monitor_(synchronization) for more
   information about this concept.
  
   ----------------------------------------------------------------

   Notice that the following BSD-style license applies to this one
   file (monitor_example.cpp) only.  The rest of Valgrind is licensed
   under the terms of the GNU General Public License, version 2,
   unless otherwise indicated.  See the COPYING file in the source
   distribution for details.

   ----------------------------------------------------------------

   This file is part of DRD, a heavyweight Valgrind tool for detecting
   errors in multithreaded programs.

   Copyright (C) 2008-2009 Bart Van Assche. All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:

   1. Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.

   2. The origin of this software must not be misrepresented; you must 
      not claim that you wrote the original software.  If you use this 
      software in a product, an acknowledgment in the product 
      documentation would be appreciated but is not required.

   3. Altered source versions must be plainly marked as such, and must
      not be misrepresented as being the original software.

   4. The name of the author may not be used to endorse or promote 
      products derived from this software without specific prior written 
      permission.

   THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
   OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

   ----------------------------------------------------------------

   Notice that the above BSD-style license applies to this one
   file (monitor_example.cpp) only.  The rest of Valgrind is licensed
   under the terms of the GNU General Public License, version 2,
   unless otherwise indicated.  See the COPYING file in the source
   distribution for details.

   ---------------------------------------------------------------- 
*/


#define _GNU_SOURCE 1


#include "config.h"
#include <cassert>
#include <iostream>
#include <pthread.h>


class Monitor
{
public:
  Monitor()
    : m_mutex()
    , m_cond()
    , m_owner()
    , m_recursion_count()
  {
    pthread_mutexattr_t mutexattr;
    pthread_mutexattr_init(&mutexattr);
    pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE);
    pthread_mutex_init(&m_mutex, &mutexattr);
    pthread_mutexattr_destroy(&mutexattr);
    pthread_condattr_t condattr;
    pthread_condattr_init(&condattr);
#if defined(HAVE_PTHREAD_CONDATTR_SETCLOCK)
    pthread_condattr_setclock(&condattr, CLOCK_MONOTONIC);
#endif
    pthread_cond_init(&m_cond, &condattr);
    pthread_condattr_destroy(&condattr);
  }
  ~Monitor()
  {
    assert(m_recursion_count == 0);
    pthread_cond_destroy(&m_cond);
    pthread_mutex_destroy(&m_mutex);
  }
  void lock()
  {
    pthread_mutex_lock(&m_mutex);
    assert(m_recursion_count >= 0);
    if (++m_recursion_count == 1)
    {
      m_owner = pthread_self();
    }
  }
  void unlock()
  {
    m_recursion_count--;
    assert(m_recursion_count >= 0);
    pthread_mutex_unlock(&m_mutex);
  }
  void wait()
  {
    assert(m_recursion_count == 1);
    assert(m_owner == pthread_self());
    m_recursion_count--;
    pthread_cond_wait(&m_cond, &m_mutex);
    m_recursion_count++;
    m_owner = pthread_self();
  }
  void signal()
  {
    assert(m_recursion_count > 0);
    pthread_cond_signal(&m_cond);
  }
  void broadcast_signal()
  {
    assert(m_recursion_count > 0);
    pthread_cond_broadcast(&m_cond);
  }
  bool is_locked_by_self()
  {
    bool result;
    pthread_mutex_lock(&m_mutex);
    result = m_recursion_count > 0 && m_owner == pthread_self();
    pthread_mutex_unlock(&m_mutex);
    return result;
  }

private:
  Monitor(const Monitor&);
  Monitor& operator=(const Monitor&);

  pthread_mutex_t m_mutex;
  pthread_cond_t  m_cond;
  pthread_t       m_owner;
  int             m_recursion_count;
};


class ScopedLock
{
public:
  ScopedLock(Monitor& m)
    : m_monitor(m)
    , m_locked(false)
  { lock(); }
  ~ScopedLock()
  { if (m_locked) unlock(); }
  void lock()
  { assert(! m_locked); m_monitor.lock(); m_locked = true; }
  void unlock()
  { assert(m_locked); m_locked = false; m_monitor.unlock(); }

private:
  ScopedLock(const ScopedLock&);
  ScopedLock& operator=(const ScopedLock&);

  Monitor& m_monitor;
  bool     m_locked;
};


class StateVariable
{
public:
  StateVariable()
    : m_state()
  { }
  int get()
  {
    ScopedLock sl(m_monitor);
    return m_state;
  }
  void set(const int state)
  {
    ScopedLock sl(m_monitor);
    m_state = state;
    m_monitor.signal();
  }
  void wait(const int state)
  {
    ScopedLock sl(m_monitor);
    while (m_state != state)
      m_monitor.wait();
  }

private:
  Monitor m_monitor;
  int     m_state;
};


static StateVariable s_sv;


static void* thread_func(void*)
{
  s_sv.wait(1);
  s_sv.set(2);
  s_sv.wait(3);
  s_sv.set(4);
  return 0;
}

int main(int, char**)
{
  pthread_t tid;
  pthread_create(&tid, 0, thread_func, 0);
  s_sv.set(1);
  s_sv.wait(2);
  s_sv.set(3);
  s_sv.wait(4);
  pthread_join(tid, 0);
  std::cerr << "Finished successfully.\n";
  return 0;
}