1*7f2fe78bSCy Schubert /* ccapi/common/win/OldCC/autolock.hxx */ 2*7f2fe78bSCy Schubert /* 3*7f2fe78bSCy Schubert * Copyright (C) 1998 by Danilo Almeida. All rights reserved. 4*7f2fe78bSCy Schubert * 5*7f2fe78bSCy Schubert * Redistribution and use in source and binary forms, with or without 6*7f2fe78bSCy Schubert * modification, are permitted provided that the following conditions 7*7f2fe78bSCy Schubert * are met: 8*7f2fe78bSCy Schubert * 9*7f2fe78bSCy Schubert * * Redistributions of source code must retain the above copyright 10*7f2fe78bSCy Schubert * notice, this list of conditions and the following disclaimer. 11*7f2fe78bSCy Schubert * 12*7f2fe78bSCy Schubert * * Redistributions in binary form must reproduce the above copyright 13*7f2fe78bSCy Schubert * notice, this list of conditions and the following disclaimer in 14*7f2fe78bSCy Schubert * the documentation and/or other materials provided with the 15*7f2fe78bSCy Schubert * distribution. 16*7f2fe78bSCy Schubert * 17*7f2fe78bSCy Schubert * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18*7f2fe78bSCy Schubert * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19*7f2fe78bSCy Schubert * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 20*7f2fe78bSCy Schubert * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 21*7f2fe78bSCy Schubert * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 22*7f2fe78bSCy Schubert * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23*7f2fe78bSCy Schubert * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24*7f2fe78bSCy Schubert * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25*7f2fe78bSCy Schubert * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 26*7f2fe78bSCy Schubert * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27*7f2fe78bSCy Schubert * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 28*7f2fe78bSCy Schubert * OF THE POSSIBILITY OF SUCH DAMAGE. 29*7f2fe78bSCy Schubert */ 30*7f2fe78bSCy Schubert 31*7f2fe78bSCy Schubert #ifndef __AUTOLOCK_HXX__ 32*7f2fe78bSCy Schubert #define __AUTOLOCK_HXX__ 33*7f2fe78bSCy Schubert 34*7f2fe78bSCy Schubert #include <windows.h> 35*7f2fe78bSCy Schubert 36*7f2fe78bSCy Schubert class CcOsLock { 37*7f2fe78bSCy Schubert CRITICAL_SECTION cs; 38*7f2fe78bSCy Schubert bool valid; 39*7f2fe78bSCy Schubert public: CcOsLock()40*7f2fe78bSCy Schubert CcOsLock() {InitializeCriticalSection(&cs); valid = true; } ~CcOsLock()41*7f2fe78bSCy Schubert ~CcOsLock() {DeleteCriticalSection(&cs); valid = false;} lock()42*7f2fe78bSCy Schubert void lock() {if (valid) EnterCriticalSection(&cs);} unlock()43*7f2fe78bSCy Schubert void unlock() {if (valid) LeaveCriticalSection(&cs);} trylock()44*7f2fe78bSCy Schubert bool trylock() {return valid ? (TryEnterCriticalSection(&cs) ? true : false) 45*7f2fe78bSCy Schubert : false; } 46*7f2fe78bSCy Schubert }; 47*7f2fe78bSCy Schubert 48*7f2fe78bSCy Schubert class CcAutoLock { 49*7f2fe78bSCy Schubert CcOsLock& m_lock; 50*7f2fe78bSCy Schubert public: Start(CcAutoLock * & a,CcOsLock & lock)51*7f2fe78bSCy Schubert static void Start(CcAutoLock*& a, CcOsLock& lock) { a = new CcAutoLock(lock); }; Stop(CcAutoLock * & a)52*7f2fe78bSCy Schubert static void Stop (CcAutoLock*& a) { delete a; a = 0; }; CcAutoLock(CcOsLock & lock)53*7f2fe78bSCy Schubert CcAutoLock(CcOsLock& lock):m_lock(lock) { m_lock.lock(); } ~CcAutoLock()54*7f2fe78bSCy Schubert ~CcAutoLock() { m_lock.unlock(); } 55*7f2fe78bSCy Schubert }; 56*7f2fe78bSCy Schubert 57*7f2fe78bSCy Schubert class CcAutoTryLock { 58*7f2fe78bSCy Schubert CcOsLock& m_lock; 59*7f2fe78bSCy Schubert bool m_locked; 60*7f2fe78bSCy Schubert public: CcAutoTryLock(CcOsLock & lock)61*7f2fe78bSCy Schubert CcAutoTryLock(CcOsLock& lock):m_lock(lock) { m_locked = m_lock.trylock(); } ~CcAutoTryLock()62*7f2fe78bSCy Schubert ~CcAutoTryLock() { if (m_locked) m_lock.unlock(); m_locked = false; } IsLocked() const63*7f2fe78bSCy Schubert bool IsLocked() const { return m_locked; } 64*7f2fe78bSCy Schubert }; 65*7f2fe78bSCy Schubert 66*7f2fe78bSCy Schubert #endif /* __AUTOLOCK_HXX */ 67