Debian Codegraph @

js/src/vm/Monitor.h

raw
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 vm_Monitor_h
8 #define vm_Monitor_h
9
10 #include "threading/ConditionVariable.h"
11 #include "threading/Mutex.h"
12
13 namespace js {
14
15 // A base class used for types intended to be used in a parallel
16 // fashion. Combines a lock and a condition variable. You can
17 // acquire the lock or signal the condition variable using the
18 // |AutoLockMonitor| type.
19
20 class Monitor {
21 protected:
22 friend class AutoLockMonitor;
23
24 Mutex lock_ MOZ_UNANNOTATED;
25 ConditionVariable condVar_;
26
27 public:
28 explicit Monitor(const MutexId& id) : lock_(id) {}
29 };
30
31 class AutoLockMonitor : public LockGuard<Mutex> {
32 private:
33 using Base = LockGuard<Mutex>;
34 Monitor& monitor;
35
36 public:
37 explicit AutoLockMonitor(Monitor& monitor)
38 : Base(monitor.lock_), monitor(monitor) {}
39
40 bool isFor(Monitor& other) const { return &monitor.lock_ == &other.lock_; }
41
42 void wait(ConditionVariable& condVar) { condVar.wait(*this); }
43
44 void wait() { wait(monitor.condVar_); }
45
46 void notify(ConditionVariable& condVar) { condVar.notify_one(); }
47
48 void notify() { notify(monitor.condVar_); }
49
50 void notifyAll(ConditionVariable& condVar) { condVar.notify_all(); }
51
52 void notifyAll() { notifyAll(monitor.condVar_); }
53 };
54
55 } // namespace js
56
57 #endif /* vm_Monitor_h */