2023-02-08 11:04:11 +00:00
|
|
|
//
|
|
|
|
// RWLock_Android.h
|
|
|
|
//
|
|
|
|
// Library: Foundation
|
|
|
|
// Package: Threading
|
|
|
|
// Module: RWLock
|
|
|
|
//
|
|
|
|
// Definition of the RWLockImpl class for Android Threads.
|
|
|
|
//
|
|
|
|
// Copyright (c) 2004-2011, Applied Informatics Software Engineering GmbH.
|
|
|
|
// and Contributors.
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: BSL-1.0
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef Foundation_RWLock_Android_INCLUDED
|
|
|
|
#define Foundation_RWLock_Android_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_mutex_t _mutex;
|
2023-02-08 11:04:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// inlines
|
|
|
|
//
|
|
|
|
inline void RWLockImpl::readLockImpl()
|
|
|
|
{
|
2023-02-13 09:22:33 +00:00
|
|
|
if (pthread_mutex_lock(&_mutex))
|
|
|
|
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_mutex_trylock(&_mutex);
|
|
|
|
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_mutex_lock(&_mutex))
|
|
|
|
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_mutex_trylock(&_mutex);
|
|
|
|
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_mutex_unlock(&_mutex))
|
|
|
|
throw SystemException("cannot unlock reader/writer lock");
|
2023-02-08 11:04:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace Poco
|
|
|
|
|
|
|
|
|
|
|
|
#endif // Foundation_RWLock_Android_INCLUDED
|