64 lines
1.1 KiB
C++

// Copyright (C) 2003 Intel Corporation
//////////////////////////////////////////////////////////////////////////
// Lock.h
//
// This file contains the definition and implementation of the Lock class
// and the TryLock class
//////////////////////////////////////////////////////////////////////////
#ifndef _LAD_LOCK_H
#define _LAD_LOCK_H
#include "RWLock.h"
#ifndef NULL
#define NULL 0
#endif
class Lock {
public:
Lock(Semaphore &sem) : _sem(&sem), _rw_lock(NULL)
{
_sem->acquire();
}
Lock(RWLock &rw_lock, RWLock::RWMode mode = RWLock::READ_ONLY) :
_sem(NULL), _rw_lock(&rw_lock)
{
_rw_lock->acquire(mode);
}
~Lock()
{
if (_sem) {
_sem->release();
}
if (_rw_lock) {
_rw_lock->release();
}
}
private:
Semaphore *_sem;
RWLock *_rw_lock;
};
class TryLock {
public:
TryLock(Semaphore &sem, bool &is_locked) : _sem(&sem)
{
_locked = _sem->acquireTry();
is_locked = _locked;
}
~TryLock()
{
if (_locked) {
_sem->release();
}
}
private:
bool _locked;
Semaphore *_sem;
};
#endif //_LAD_LOCK_H