1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/build/IOInterposerPrivate.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,167 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ 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 xpcom_build_IOInterposerPrivate_h 1.11 +#define xpcom_build_IOInterposerPrivate_h 1.12 + 1.13 +/* This header file contains declarations for helper classes that are 1.14 + to be used exclusively by IOInterposer and its observers. This header 1.15 + file is not to be used by anything else and MUST NOT be exported! */ 1.16 + 1.17 +#include <prcvar.h> 1.18 +#include <prlock.h> 1.19 + 1.20 +namespace mozilla { 1.21 +namespace IOInterposer { 1.22 + 1.23 +/** 1.24 + * The following classes are simple wrappers for PRLock and PRCondVar. 1.25 + * IOInterposer and friends use these instead of Mozilla::Mutex et al because 1.26 + * of the fact that IOInterposer is permitted to run until the process 1.27 + * terminates; we can't use anything that plugs into leak checkers or deadlock 1.28 + * detectors because IOInterposer will outlive those and generate false 1.29 + * positives. 1.30 + */ 1.31 + 1.32 +class Monitor 1.33 +{ 1.34 +public: 1.35 + Monitor() 1.36 + : mLock(PR_NewLock()) 1.37 + , mCondVar(PR_NewCondVar(mLock)) 1.38 + { 1.39 + } 1.40 + 1.41 + ~Monitor() 1.42 + { 1.43 + PR_DestroyCondVar(mCondVar); 1.44 + mCondVar = nullptr; 1.45 + PR_DestroyLock(mLock); 1.46 + mLock = nullptr; 1.47 + } 1.48 + 1.49 + void Lock() 1.50 + { 1.51 + PR_Lock(mLock); 1.52 + } 1.53 + 1.54 + void Unlock() 1.55 + { 1.56 + PR_Unlock(mLock); 1.57 + } 1.58 + 1.59 + bool Wait(PRIntervalTime aTimeout = PR_INTERVAL_NO_TIMEOUT) 1.60 + { 1.61 + return PR_WaitCondVar(mCondVar, aTimeout) == PR_SUCCESS; 1.62 + } 1.63 + 1.64 + bool Notify() 1.65 + { 1.66 + return PR_NotifyCondVar(mCondVar) == PR_SUCCESS; 1.67 + } 1.68 + 1.69 +private: 1.70 + PRLock* mLock; 1.71 + PRCondVar* mCondVar; 1.72 +}; 1.73 + 1.74 +class MonitorAutoLock 1.75 +{ 1.76 +public: 1.77 + MonitorAutoLock(Monitor &aMonitor) 1.78 + : mMonitor(aMonitor) 1.79 + { 1.80 + mMonitor.Lock(); 1.81 + } 1.82 + 1.83 + ~MonitorAutoLock() 1.84 + { 1.85 + mMonitor.Unlock(); 1.86 + } 1.87 + 1.88 + bool Wait(PRIntervalTime aTimeout = PR_INTERVAL_NO_TIMEOUT) 1.89 + { 1.90 + return mMonitor.Wait(aTimeout); 1.91 + } 1.92 + 1.93 + bool Notify() 1.94 + { 1.95 + return mMonitor.Notify(); 1.96 + } 1.97 + 1.98 +private: 1.99 + Monitor& mMonitor; 1.100 +}; 1.101 + 1.102 +class MonitorAutoUnlock 1.103 +{ 1.104 +public: 1.105 + MonitorAutoUnlock(Monitor &aMonitor) 1.106 + : mMonitor(aMonitor) 1.107 + { 1.108 + mMonitor.Unlock(); 1.109 + } 1.110 + 1.111 + ~MonitorAutoUnlock() 1.112 + { 1.113 + mMonitor.Lock(); 1.114 + } 1.115 + 1.116 +private: 1.117 + Monitor& mMonitor; 1.118 +}; 1.119 + 1.120 +class Mutex 1.121 +{ 1.122 +public: 1.123 + Mutex() 1.124 + : mPRLock(PR_NewLock()) 1.125 + { 1.126 + } 1.127 + 1.128 + ~Mutex() 1.129 + { 1.130 + PR_DestroyLock(mPRLock); 1.131 + mPRLock = nullptr; 1.132 + } 1.133 + 1.134 + void Lock() 1.135 + { 1.136 + PR_Lock(mPRLock); 1.137 + } 1.138 + 1.139 + void Unlock() 1.140 + { 1.141 + PR_Unlock(mPRLock); 1.142 + } 1.143 + 1.144 +private: 1.145 + PRLock* mPRLock; 1.146 +}; 1.147 + 1.148 +class AutoLock 1.149 +{ 1.150 +public: 1.151 + AutoLock(Mutex& aLock) 1.152 + : mLock(aLock) 1.153 + { 1.154 + mLock.Lock(); 1.155 + } 1.156 + 1.157 + ~AutoLock() 1.158 + { 1.159 + mLock.Unlock(); 1.160 + } 1.161 + 1.162 +private: 1.163 + Mutex& mLock; 1.164 +}; 1.165 + 1.166 +} // namespace IOInterposer 1.167 +} // namespace mozilla 1.168 + 1.169 +#endif // xpcom_build_IOInterposerPrivate_h 1.170 +