michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * vim: set ts=8 sts=4 et sw=4 tw=99: michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef vm_Monitor_h michael@0: #define vm_Monitor_h michael@0: michael@0: #ifdef JS_THREADSAFE michael@0: #include "mozilla/DebugOnly.h" michael@0: #endif michael@0: michael@0: #include michael@0: michael@0: #include "jslock.h" michael@0: michael@0: #include "js/Utility.h" michael@0: michael@0: namespace js { michael@0: michael@0: // A base class used for types intended to be used in a parallel michael@0: // fashion, such as the workers in the |ThreadPool| class. Combines a michael@0: // lock and a condition variable. You can acquire the lock or signal michael@0: // the condition variable using the |AutoLockMonitor| type. michael@0: michael@0: class Monitor michael@0: { michael@0: protected: michael@0: friend class AutoLockMonitor; michael@0: friend class AutoUnlockMonitor; michael@0: michael@0: PRLock *lock_; michael@0: PRCondVar *condVar_; michael@0: michael@0: public: michael@0: Monitor() michael@0: : lock_(nullptr), michael@0: condVar_(nullptr) michael@0: { } michael@0: michael@0: ~Monitor() { michael@0: #ifdef JS_THREADSAFE michael@0: if (lock_) michael@0: PR_DestroyLock(lock_); michael@0: if (condVar_) michael@0: PR_DestroyCondVar(condVar_); michael@0: #endif michael@0: } michael@0: michael@0: bool init(); michael@0: }; michael@0: michael@0: class AutoLockMonitor michael@0: { michael@0: private: michael@0: #ifdef JS_THREADSAFE michael@0: Monitor &monitor; michael@0: #endif michael@0: michael@0: public: michael@0: AutoLockMonitor(Monitor &monitor) michael@0: #ifdef JS_THREADSAFE michael@0: : monitor(monitor) michael@0: { michael@0: PR_Lock(monitor.lock_); michael@0: } michael@0: #else michael@0: {} michael@0: #endif michael@0: michael@0: ~AutoLockMonitor() { michael@0: #ifdef JS_THREADSAFE michael@0: PR_Unlock(monitor.lock_); michael@0: #endif michael@0: } michael@0: michael@0: bool isFor(Monitor &other) const { michael@0: #ifdef JS_THREADSAFE michael@0: return monitor.lock_ == other.lock_; michael@0: #else michael@0: return true; michael@0: #endif michael@0: } michael@0: michael@0: void wait(PRCondVar *condVar) { michael@0: #ifdef JS_THREADSAFE michael@0: mozilla::DebugOnly status = michael@0: PR_WaitCondVar(condVar, PR_INTERVAL_NO_TIMEOUT); michael@0: MOZ_ASSERT(status == PR_SUCCESS); michael@0: #endif michael@0: } michael@0: michael@0: void wait() { michael@0: #ifdef JS_THREADSAFE michael@0: wait(monitor.condVar_); michael@0: #endif michael@0: } michael@0: michael@0: void notify(PRCondVar *condVar) { michael@0: #ifdef JS_THREADSAFE michael@0: mozilla::DebugOnly status = PR_NotifyCondVar(condVar); michael@0: MOZ_ASSERT(status == PR_SUCCESS); michael@0: #endif michael@0: } michael@0: michael@0: void notify() { michael@0: #ifdef JS_THREADSAFE michael@0: notify(monitor.condVar_); michael@0: #endif michael@0: } michael@0: michael@0: void notifyAll(PRCondVar *condVar) { michael@0: #ifdef JS_THREADSAFE michael@0: mozilla::DebugOnly status = PR_NotifyAllCondVar(monitor.condVar_); michael@0: MOZ_ASSERT(status == PR_SUCCESS); michael@0: #endif michael@0: } michael@0: michael@0: void notifyAll() { michael@0: #ifdef JS_THREADSAFE michael@0: notifyAll(monitor.condVar_); michael@0: #endif michael@0: } michael@0: }; michael@0: michael@0: class AutoUnlockMonitor michael@0: { michael@0: private: michael@0: #ifdef JS_THREADSAFE michael@0: Monitor &monitor; michael@0: #endif michael@0: michael@0: public: michael@0: AutoUnlockMonitor(Monitor &monitor) michael@0: #ifdef JS_THREADSAFE michael@0: : monitor(monitor) michael@0: { michael@0: PR_Unlock(monitor.lock_); michael@0: } michael@0: #else michael@0: {} michael@0: #endif michael@0: michael@0: ~AutoUnlockMonitor() { michael@0: #ifdef JS_THREADSAFE michael@0: PR_Lock(monitor.lock_); michael@0: #endif michael@0: } michael@0: michael@0: bool isFor(Monitor &other) const { michael@0: #ifdef JS_THREADSAFE michael@0: return monitor.lock_ == other.lock_; michael@0: #else michael@0: return true; michael@0: #endif michael@0: } michael@0: }; michael@0: michael@0: } // namespace js michael@0: michael@0: #endif /* vm_Monitor_h */