1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/vm/Monitor.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,162 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 1.5 + * vim: set ts=8 sts=4 et sw=4 tw=99: 1.6 + * This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#ifndef vm_Monitor_h 1.11 +#define vm_Monitor_h 1.12 + 1.13 +#ifdef JS_THREADSAFE 1.14 +#include "mozilla/DebugOnly.h" 1.15 +#endif 1.16 + 1.17 +#include <stddef.h> 1.18 + 1.19 +#include "jslock.h" 1.20 + 1.21 +#include "js/Utility.h" 1.22 + 1.23 +namespace js { 1.24 + 1.25 +// A base class used for types intended to be used in a parallel 1.26 +// fashion, such as the workers in the |ThreadPool| class. Combines a 1.27 +// lock and a condition variable. You can acquire the lock or signal 1.28 +// the condition variable using the |AutoLockMonitor| type. 1.29 + 1.30 +class Monitor 1.31 +{ 1.32 + protected: 1.33 + friend class AutoLockMonitor; 1.34 + friend class AutoUnlockMonitor; 1.35 + 1.36 + PRLock *lock_; 1.37 + PRCondVar *condVar_; 1.38 + 1.39 + public: 1.40 + Monitor() 1.41 + : lock_(nullptr), 1.42 + condVar_(nullptr) 1.43 + { } 1.44 + 1.45 + ~Monitor() { 1.46 +#ifdef JS_THREADSAFE 1.47 + if (lock_) 1.48 + PR_DestroyLock(lock_); 1.49 + if (condVar_) 1.50 + PR_DestroyCondVar(condVar_); 1.51 +#endif 1.52 + } 1.53 + 1.54 + bool init(); 1.55 +}; 1.56 + 1.57 +class AutoLockMonitor 1.58 +{ 1.59 + private: 1.60 +#ifdef JS_THREADSAFE 1.61 + Monitor &monitor; 1.62 +#endif 1.63 + 1.64 + public: 1.65 + AutoLockMonitor(Monitor &monitor) 1.66 +#ifdef JS_THREADSAFE 1.67 + : monitor(monitor) 1.68 + { 1.69 + PR_Lock(monitor.lock_); 1.70 + } 1.71 +#else 1.72 + {} 1.73 +#endif 1.74 + 1.75 + ~AutoLockMonitor() { 1.76 +#ifdef JS_THREADSAFE 1.77 + PR_Unlock(monitor.lock_); 1.78 +#endif 1.79 + } 1.80 + 1.81 + bool isFor(Monitor &other) const { 1.82 +#ifdef JS_THREADSAFE 1.83 + return monitor.lock_ == other.lock_; 1.84 +#else 1.85 + return true; 1.86 +#endif 1.87 + } 1.88 + 1.89 + void wait(PRCondVar *condVar) { 1.90 +#ifdef JS_THREADSAFE 1.91 + mozilla::DebugOnly<PRStatus> status = 1.92 + PR_WaitCondVar(condVar, PR_INTERVAL_NO_TIMEOUT); 1.93 + MOZ_ASSERT(status == PR_SUCCESS); 1.94 +#endif 1.95 + } 1.96 + 1.97 + void wait() { 1.98 +#ifdef JS_THREADSAFE 1.99 + wait(monitor.condVar_); 1.100 +#endif 1.101 + } 1.102 + 1.103 + void notify(PRCondVar *condVar) { 1.104 +#ifdef JS_THREADSAFE 1.105 + mozilla::DebugOnly<PRStatus> status = PR_NotifyCondVar(condVar); 1.106 + MOZ_ASSERT(status == PR_SUCCESS); 1.107 +#endif 1.108 + } 1.109 + 1.110 + void notify() { 1.111 +#ifdef JS_THREADSAFE 1.112 + notify(monitor.condVar_); 1.113 +#endif 1.114 + } 1.115 + 1.116 + void notifyAll(PRCondVar *condVar) { 1.117 +#ifdef JS_THREADSAFE 1.118 + mozilla::DebugOnly<PRStatus> status = PR_NotifyAllCondVar(monitor.condVar_); 1.119 + MOZ_ASSERT(status == PR_SUCCESS); 1.120 +#endif 1.121 + } 1.122 + 1.123 + void notifyAll() { 1.124 +#ifdef JS_THREADSAFE 1.125 + notifyAll(monitor.condVar_); 1.126 +#endif 1.127 + } 1.128 +}; 1.129 + 1.130 +class AutoUnlockMonitor 1.131 +{ 1.132 + private: 1.133 +#ifdef JS_THREADSAFE 1.134 + Monitor &monitor; 1.135 +#endif 1.136 + 1.137 + public: 1.138 + AutoUnlockMonitor(Monitor &monitor) 1.139 +#ifdef JS_THREADSAFE 1.140 + : monitor(monitor) 1.141 + { 1.142 + PR_Unlock(monitor.lock_); 1.143 + } 1.144 +#else 1.145 + {} 1.146 +#endif 1.147 + 1.148 + ~AutoUnlockMonitor() { 1.149 +#ifdef JS_THREADSAFE 1.150 + PR_Lock(monitor.lock_); 1.151 +#endif 1.152 + } 1.153 + 1.154 + bool isFor(Monitor &other) const { 1.155 +#ifdef JS_THREADSAFE 1.156 + return monitor.lock_ == other.lock_; 1.157 +#else 1.158 + return true; 1.159 +#endif 1.160 + } 1.161 +}; 1.162 + 1.163 +} // namespace js 1.164 + 1.165 +#endif /* vm_Monitor_h */