1//
2// RWLock_POSIX.h
3//
4// Library: Foundation
5// Package: Threading
6// Module: RWLock
7//
8// Definition of the RWLockImpl class for POSIX Threads.
9//
10// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
11// and Contributors.
12//
13// SPDX-License-Identifier: BSL-1.0
14//
15
16
17#ifndef Foundation_RWLock_POSIX_INCLUDED
18#define Foundation_RWLock_POSIX_INCLUDED
19
20
21#include "Poco/Foundation.h"
22#include "Poco/Exception.h"
23#include <pthread.h>
24#include <errno.h>
25
26
27namespace Poco {
28
29
30class Foundation_API RWLockImpl
31{
32protected:
33 RWLockImpl();
34 ~RWLockImpl();
35 void readLockImpl();
36 bool tryReadLockImpl();
37 void writeLockImpl();
38 bool tryWriteLockImpl();
39 void unlockImpl();
40
41private:
42 pthread_rwlock_t _rwl;
43};
44
45
46//
47// inlines
48//
49inline void RWLockImpl::readLockImpl()
50{
51 if (pthread_rwlock_rdlock(&_rwl))
52 throw SystemException("cannot lock reader/writer lock");
53}
54
55
56inline bool RWLockImpl::tryReadLockImpl()
57{
58 int rc = pthread_rwlock_tryrdlock(&_rwl);
59 if (rc == 0)
60 return true;
61 else if (rc == EBUSY)
62 return false;
63 else
64 throw SystemException("cannot lock reader/writer lock");
65
66}
67
68
69inline void RWLockImpl::writeLockImpl()
70{
71 if (pthread_rwlock_wrlock(&_rwl))
72 throw SystemException("cannot lock reader/writer lock");
73}
74
75
76inline bool RWLockImpl::tryWriteLockImpl()
77{
78 int rc = pthread_rwlock_trywrlock(&_rwl);
79 if (rc == 0)
80 return true;
81 else if (rc == EBUSY)
82 return false;
83 else
84 throw SystemException("cannot lock reader/writer lock");
85
86}
87
88
89inline void RWLockImpl::unlockImpl()
90{
91 if (pthread_rwlock_unlock(&_rwl))
92 throw SystemException("cannot unlock mutex");
93}
94
95
96} // namespace Poco
97
98
99#endif // Foundation_RWLock_POSIX_INCLUDED
100