rwlock.hpp
// ***************************************************************** -*- C++ -*-
/*
* Copyright (C) 2004-2021 Exiv2 authors
* This program is part of the Exiv2 distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
*/
#ifndef RW_LOCK_HPP
#define RW_LOCK_HPP
#ifdef _MSC_VER
#include <windows.h>
#else
#include <pthread.h>
#endif
namespace Exiv2 {
#ifdef _MSC_VER
class RWLock
{
public:
RWLock()
{
InitializeCriticalSection(&lock_);
}
~RWLock()
{
DeleteCriticalSection(&lock_);
}
void wrlock() { enter(); }
bool trywrlock() { return tryenter(); }
void rdlock() { enter(); }
bool tryrdlock() { return tryenter(); }
void rdunlock() { leave(); }
void wrunlock() { leave(); }
private:
void enter()
{
EnterCriticalSection(&lock_);
}
void leave()
{
LeaveCriticalSection(&lock_);
}
bool tryenter()
{
#if defined(MSDEV_2003) || defined(MSDEV_2005)
EnterCriticalSection(&lock_);
return true;
#else
return 0 != TryEnterCriticalSection(&lock_);
#endif
}
private:
CRITICAL_SECTION lock_;
};
#else
// UNIX systems (including MinGW and Cygwin)
class RWLock
{
public:
explicit RWLock(const pthread_rwlockattr_t *attr = 0)
{
pthread_rwlock_init(&rwlock_, attr);
}
~RWLock()
{
pthread_rwlock_destroy(&rwlock_);
}
void wrlock()
{
pthread_rwlock_wrlock(&rwlock_);
}
bool trywrlock()
{
return 0 == pthread_rwlock_trywrlock(&rwlock_);
}
void rdlock()
{
pthread_rwlock_rdlock(&rwlock_);
}
bool tryrdlock()
{
return 0 == pthread_rwlock_tryrdlock(&rwlock_);
}
void unlock()
{
pthread_rwlock_unlock(&rwlock_);
}
void rdunlock() { unlock(); }
void wrunlock() { unlock(); }
private:
pthread_rwlock_t rwlock_;
};
#endif
class ScopedReadLock
{
public:
explicit ScopedReadLock(RWLock &rwlock):
rwlock_(rwlock)
{
rwlock_.rdlock();
}
~ScopedReadLock() { rwlock_.rdunlock(); }
private:
RWLock &rwlock_;
};
class ScopedWriteLock
{
public:
explicit ScopedWriteLock(RWLock &rwlock):
rwlock_(rwlock)
{
rwlock_.wrlock();
}
~ScopedWriteLock() { rwlock_.wrunlock(); }
private:
RWLock &rwlock_;
};
}
#endif // RW_LOCK_HPP
Exiv2::RWLock
Class to provide a Read-Write Lock.
Definition: rwlock.hpp:90
Exiv2::RWLock::wrlock
void wrlock()
acquire rw lock
Definition: rwlock.hpp:105
Exiv2::RWLock::RWLock
RWLock(const pthread_rwlockattr_t *attr=0)
constructor (acquires the lock)
Definition: rwlock.hpp:93
Exiv2::RWLock::unlock
void unlock()
release rw lock
Definition: rwlock.hpp:129
Exiv2::RWLock::trywrlock
bool trywrlock()
test to see if the rw lock can be acquired
Definition: rwlock.hpp:111
Exiv2::RWLock::tryrdlock
bool tryrdlock()
test to see if the rd lock can be acquired
Definition: rwlock.hpp:123
Exiv2::RWLock::rdlock
void rdlock()
acquire rd lock
Definition: rwlock.hpp:117
Exiv2::RWLock::~RWLock
~RWLock()
constructor (releases lock)
Definition: rwlock.hpp:99
Exiv2::RWLock::wrunlock
void wrunlock()
unlock rw lock
Definition: rwlock.hpp:138
Exiv2::RWLock::rdunlock
void rdunlock()
unlock rd lock
Definition: rwlock.hpp:135
Exiv2::ScopedReadLock
Class to provide a ScopedReadLock. The lock is applied by the constructor and released by the destruc...
Definition: rwlock.hpp:151
Exiv2::ScopedReadLock::~ScopedReadLock
~ScopedReadLock()
destructor - unlocks the object used in constructor
Definition: rwlock.hpp:161
Exiv2::ScopedReadLock::ScopedReadLock
ScopedReadLock(RWLock &rwlock)
constructor - locks the object
Definition: rwlock.hpp:154
Exiv2::ScopedWriteLock
Class to provide a ScopedWriteLock. The lock is applied by the constructor and released by the destru...
Definition: rwlock.hpp:173
Exiv2::ScopedWriteLock::ScopedWriteLock
ScopedWriteLock(RWLock &rwlock)
constructor - locks the object
Definition: rwlock.hpp:176
Exiv2::ScopedWriteLock::~ScopedWriteLock
~ScopedWriteLock()
destructor - unlocks the object used in constructor
Definition: rwlock.hpp:183
Exiv2
Provides classes and functions to encode and decode Exif and Iptc data. The libexiv2 API consists of ...
Definition: asfvideo.hpp:36