2023-02-08 11:04:11 +00:00
|
|
|
//
|
|
|
|
// RWLock_POSIX.h
|
|
|
|
//
|
|
|
|
// Library: Foundation
|
|
|
|
// Package: Threading
|
|
|
|
// Module: RWLock
|
|
|
|
//
|
|
|
|
// Definition of the RWLockImpl class for POSIX Threads.
|
|
|
|
//
|
|
|
|
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
|
|
|
|
// and Contributors.
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: BSL-1.0
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef Foundation_RWLock_POSIX_INCLUDED
|
|
|
|
#define Foundation_RWLock_POSIX_INCLUDED
|
|
|
|
|
|
|
|
|
|
|
|
#include <errno.h>
|
2023-02-13 09:22:33 +00:00
|
|
|
#include <pthread.h>
|
|
|
|
#include "Poco/Exception.h"
|
|
|
|
#include "Poco/Foundation.h"
|
2023-02-08 11:04:11 +00:00
|
|
|
|
|
|
|
|
2023-02-13 09:22:33 +00:00
|
|
|
namespace Poco
|
|
|
|
{
|
2023-02-08 11:04:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Foundation_API RWLockImpl
|
|
|
|
{
|
|
|
|
protected:
|
2023-02-13 09:22:33 +00:00
|
|
|
RWLockImpl();
|
|
|
|
~RWLockImpl();
|
|
|
|
void readLockImpl();
|
|
|
|
bool tryReadLockImpl();
|
|
|
|
void writeLockImpl();
|
|
|
|
bool tryWriteLockImpl();
|
|
|
|
void unlockImpl();
|
|
|
|
|
2023-02-08 11:04:11 +00:00
|
|
|
private:
|
2023-02-13 09:22:33 +00:00
|
|
|
pthread_rwlock_t _rwl;
|
2023-02-08 11:04:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// inlines
|
|
|
|
//
|
|
|
|
inline void RWLockImpl::readLockImpl()
|
|
|
|
{
|
2023-02-13 09:22:33 +00:00
|
|
|
if (pthread_rwlock_rdlock(&_rwl))
|
|
|
|
throw SystemException("cannot lock reader/writer lock");
|
2023-02-08 11:04:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline bool RWLockImpl::tryReadLockImpl()
|
|
|
|
{
|
2023-02-13 09:22:33 +00:00
|
|
|
int rc = pthread_rwlock_tryrdlock(&_rwl);
|
|
|
|
if (rc == 0)
|
|
|
|
return true;
|
|
|
|
else if (rc == EBUSY)
|
|
|
|
return false;
|
|
|
|
else
|
|
|
|
throw SystemException("cannot lock reader/writer lock");
|
2023-02-08 11:04:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void RWLockImpl::writeLockImpl()
|
|
|
|
{
|
2023-02-13 09:22:33 +00:00
|
|
|
if (pthread_rwlock_wrlock(&_rwl))
|
|
|
|
throw SystemException("cannot lock reader/writer lock");
|
2023-02-08 11:04:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline bool RWLockImpl::tryWriteLockImpl()
|
|
|
|
{
|
2023-02-13 09:22:33 +00:00
|
|
|
int rc = pthread_rwlock_trywrlock(&_rwl);
|
|
|
|
if (rc == 0)
|
|
|
|
return true;
|
|
|
|
else if (rc == EBUSY)
|
|
|
|
return false;
|
|
|
|
else
|
|
|
|
throw SystemException("cannot lock reader/writer lock");
|
2023-02-08 11:04:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void RWLockImpl::unlockImpl()
|
|
|
|
{
|
2023-02-13 09:22:33 +00:00
|
|
|
if (pthread_rwlock_unlock(&_rwl))
|
|
|
|
throw SystemException("cannot unlock mutex");
|
2023-02-08 11:04:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace Poco
|
|
|
|
|
|
|
|
|
|
|
|
#endif // Foundation_RWLock_POSIX_INCLUDED
|