1*617e2443SMark Logan /******************************************************************************* 2*617e2443SMark Logan * Copyright (C) 2004-2008 Intel Corp. All rights reserved. 3*617e2443SMark Logan * 4*617e2443SMark Logan * Redistribution and use in source and binary forms, with or without 5*617e2443SMark Logan * modification, are permitted provided that the following conditions are met: 6*617e2443SMark Logan * 7*617e2443SMark Logan * - Redistributions of source code must retain the above copyright notice, 8*617e2443SMark Logan * this list of conditions and the following disclaimer. 9*617e2443SMark Logan * 10*617e2443SMark Logan * - Redistributions in binary form must reproduce the above copyright notice, 11*617e2443SMark Logan * this list of conditions and the following disclaimer in the documentation 12*617e2443SMark Logan * and/or other materials provided with the distribution. 13*617e2443SMark Logan * 14*617e2443SMark Logan * - Neither the name of Intel Corp. nor the names of its 15*617e2443SMark Logan * contributors may be used to endorse or promote products derived from this 16*617e2443SMark Logan * software without specific prior written permission. 17*617e2443SMark Logan * 18*617e2443SMark Logan * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' 19*617e2443SMark Logan * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20*617e2443SMark Logan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21*617e2443SMark Logan * ARE DISCLAIMED. IN NO EVENT SHALL Intel Corp. OR THE CONTRIBUTORS 22*617e2443SMark Logan * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23*617e2443SMark Logan * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24*617e2443SMark Logan * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25*617e2443SMark Logan * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26*617e2443SMark Logan * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27*617e2443SMark Logan * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28*617e2443SMark Logan * POSSIBILITY OF SUCH DAMAGE. 29*617e2443SMark Logan *******************************************************************************/ 30*617e2443SMark Logan 31*617e2443SMark Logan ////////////////////////////////////////////////////////////////////////// 32*617e2443SMark Logan // Lock.h 33*617e2443SMark Logan // 34*617e2443SMark Logan // This file contains the definition and implementation of the Lock class 35*617e2443SMark Logan // and the TryLock class 36*617e2443SMark Logan ////////////////////////////////////////////////////////////////////////// 37*617e2443SMark Logan #ifndef _LAD_LOCK_H 38*617e2443SMark Logan #define _LAD_LOCK_H 39*617e2443SMark Logan #include "RWLock.h" 40*617e2443SMark Logan 41*617e2443SMark Logan #ifndef NULL 42*617e2443SMark Logan #define NULL 0 43*617e2443SMark Logan #endif 44*617e2443SMark Logan 45*617e2443SMark Logan class Lock 46*617e2443SMark Logan { 47*617e2443SMark Logan public: Lock(Semaphore & sem)48*617e2443SMark Logan Lock(Semaphore &sem) : _sem(&sem), _rw_lock(NULL) 49*617e2443SMark Logan { 50*617e2443SMark Logan _sem->acquire(); 51*617e2443SMark Logan } 52*617e2443SMark Logan 53*617e2443SMark Logan Lock(RWLock &rw_lock, RWLock::RWMode mode = RWLock::READ_ONLY) : _sem(NULL)54*617e2443SMark Logan _sem(NULL), _rw_lock(&rw_lock) 55*617e2443SMark Logan { 56*617e2443SMark Logan _rw_lock->acquire(mode); 57*617e2443SMark Logan } 58*617e2443SMark Logan ~Lock()59*617e2443SMark Logan ~Lock() 60*617e2443SMark Logan { 61*617e2443SMark Logan if (_sem) { 62*617e2443SMark Logan _sem->release(); 63*617e2443SMark Logan } 64*617e2443SMark Logan if (_rw_lock) { 65*617e2443SMark Logan _rw_lock->release(); 66*617e2443SMark Logan } 67*617e2443SMark Logan 68*617e2443SMark Logan } 69*617e2443SMark Logan 70*617e2443SMark Logan private: 71*617e2443SMark Logan Semaphore *_sem; 72*617e2443SMark Logan RWLock *_rw_lock; 73*617e2443SMark Logan }; 74*617e2443SMark Logan 75*617e2443SMark Logan class TryLock 76*617e2443SMark Logan { 77*617e2443SMark Logan public: TryLock(Semaphore & sem,bool & is_locked)78*617e2443SMark Logan TryLock(Semaphore &sem, bool &is_locked) : _sem(&sem) 79*617e2443SMark Logan { 80*617e2443SMark Logan _locked = _sem->acquireTry(); 81*617e2443SMark Logan is_locked = _locked; 82*617e2443SMark Logan } 83*617e2443SMark Logan ~TryLock()84*617e2443SMark Logan ~TryLock() 85*617e2443SMark Logan { 86*617e2443SMark Logan if (_locked) { 87*617e2443SMark Logan _sem->release(); 88*617e2443SMark Logan } 89*617e2443SMark Logan } 90*617e2443SMark Logan 91*617e2443SMark Logan private: 92*617e2443SMark Logan bool _locked; 93*617e2443SMark Logan Semaphore *_sem; 94*617e2443SMark Logan }; 95*617e2443SMark Logan 96*617e2443SMark Logan #endif //_LAD_LOCK_H 97*617e2443SMark Logan 98