Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/mozilla/Monitor.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef mozilla_Monitor_h
8
#define mozilla_Monitor_h
9
10
#include "mozilla/CondVar.h"
11
#include "mozilla/Mutex.h"
12
13
namespace mozilla {
14
15
/**
16
 * Monitor provides a *non*-reentrant monitor: *not* a Java-style
17
 * monitor.  If your code needs support for reentrancy, use
18
 * ReentrantMonitor instead.  (Rarely should reentrancy be needed.)
19
 *
20
 * Instead of directly calling Monitor methods, it's safer and simpler
21
 * to instead use the RAII wrappers MonitorAutoLock and
22
 * MonitorAutoUnlock.
23
 */
24
class Monitor
25
{
26
public:
27
  explicit Monitor(const char* aName,
28
                   recordreplay::Behavior aRecorded = recordreplay::Behavior::Preserve)
29
    : mMutex(aName, aRecorded)
30
    , mCondVar(mMutex, "[Monitor.mCondVar]")
31
25
  {
32
25
  }
33
34
0
  ~Monitor() {}
35
36
538
  void Lock() { mMutex.Lock(); }
37
535
  void Unlock() { mMutex.Unlock(); }
38
39
1
  void Wait() { mCondVar.Wait(); }
40
180
  CVStatus Wait(TimeDuration aDuration) { return mCondVar.Wait(aDuration); }
41
42
90
  nsresult Notify() { return mCondVar.Notify(); }
43
3
  nsresult NotifyAll() { return mCondVar.NotifyAll(); }
44
45
  void AssertCurrentThreadOwns() const
46
958
  {
47
958
    mMutex.AssertCurrentThreadOwns();
48
958
  }
49
50
  void AssertNotCurrentThreadOwns() const
51
0
  {
52
0
    mMutex.AssertNotCurrentThreadOwns();
53
0
  }
54
55
private:
56
  Monitor();
57
  Monitor(const Monitor&);
58
  Monitor& operator=(const Monitor&);
59
60
  Mutex mMutex;
61
  CondVar mCondVar;
62
};
63
64
/**
65
 * Lock the monitor for the lexical scope instances of this class are
66
 * bound to (except for MonitorAutoUnlock in nested scopes).
67
 *
68
 * The monitor must be unlocked when instances of this class are
69
 * created.
70
 */
71
class MOZ_STACK_CLASS MonitorAutoLock
72
{
73
public:
74
  explicit MonitorAutoLock(Monitor& aMonitor)
75
    : mMonitor(&aMonitor)
76
453
  {
77
453
    mMonitor->Lock();
78
453
  }
79
80
  ~MonitorAutoLock()
81
449
  {
82
449
    mMonitor->Unlock();
83
449
  }
84
85
1
  void Wait() { mMonitor->Wait(); }
86
6
  CVStatus Wait(TimeDuration aDuration) { return mMonitor->Wait(aDuration); }
87
88
3
  nsresult Notify() { return mMonitor->Notify(); }
89
3
  nsresult NotifyAll() { return mMonitor->NotifyAll(); }
90
91
private:
92
  MonitorAutoLock();
93
  MonitorAutoLock(const MonitorAutoLock&);
94
  MonitorAutoLock& operator=(const MonitorAutoLock&);
95
  static void* operator new(size_t) CPP_THROW_NEW;
96
97
  friend class MonitorAutoUnlock;
98
99
  Monitor* mMonitor;
100
};
101
102
/**
103
 * Unlock the monitor for the lexical scope instances of this class
104
 * are bound to (except for MonitorAutoLock in nested scopes).
105
 *
106
 * The monitor must be locked by the current thread when instances of
107
 * this class are created.
108
 */
109
class MOZ_STACK_CLASS MonitorAutoUnlock
110
{
111
public:
112
  explicit MonitorAutoUnlock(Monitor& aMonitor)
113
    : mMonitor(&aMonitor)
114
86
  {
115
86
    mMonitor->Unlock();
116
86
  }
117
118
  explicit MonitorAutoUnlock(MonitorAutoLock& aMonitorLock)
119
    : mMonitor(aMonitorLock.mMonitor)
120
0
  {
121
0
    mMonitor->Unlock();
122
0
  }
123
124
  ~MonitorAutoUnlock()
125
86
  {
126
86
    mMonitor->Lock();
127
86
  }
128
129
private:
130
  MonitorAutoUnlock();
131
  MonitorAutoUnlock(const MonitorAutoUnlock&);
132
  MonitorAutoUnlock& operator=(const MonitorAutoUnlock&);
133
  static void* operator new(size_t) CPP_THROW_NEW;
134
135
  Monitor* mMonitor;
136
};
137
138
} // namespace mozilla
139
140
#endif // mozilla_Monitor_h