/src/poco/Foundation/include/Poco/Mutex_POSIX.h
Line | Count | Source |
1 | | // |
2 | | // Mutex_POSIX.h |
3 | | // |
4 | | // Library: Foundation |
5 | | // Package: Threading |
6 | | // Module: Mutex |
7 | | // |
8 | | // Definition of the MutexImpl and FastMutexImpl classes for POSIX Threads. |
9 | | // |
10 | | // Copyright (c) 2004-2008, Applied Informatics Software Engineering GmbH. |
11 | | // and Contributors. |
12 | | // |
13 | | // SPDX-License-Identifier: BSL-1.0 |
14 | | // |
15 | | |
16 | | |
17 | | #ifndef Foundation_Mutex_POSIX_INCLUDED |
18 | | #define Foundation_Mutex_POSIX_INCLUDED |
19 | | |
20 | | |
21 | | #include "Poco/Foundation.h" |
22 | | #include "Poco/Exception.h" |
23 | | #include "Poco/Error.h" |
24 | | #include <pthread.h> |
25 | | |
26 | | |
27 | | namespace Poco { |
28 | | |
29 | | |
30 | | class Foundation_API MutexImpl |
31 | | { |
32 | | protected: |
33 | | MutexImpl(); |
34 | | MutexImpl(bool fast); |
35 | | ~MutexImpl(); |
36 | | void lockImpl(); |
37 | | bool tryLockImpl(); |
38 | | bool tryLockImpl(long milliseconds); |
39 | | void unlockImpl(); |
40 | | |
41 | | private: |
42 | | pthread_mutex_t _mutex; |
43 | | }; |
44 | | |
45 | | |
46 | | class Foundation_API FastMutexImpl: public MutexImpl |
47 | | { |
48 | | protected: |
49 | | FastMutexImpl(); |
50 | | ~FastMutexImpl(); |
51 | | }; |
52 | | |
53 | | |
54 | | // |
55 | | // inlines |
56 | | // |
57 | | inline void MutexImpl::lockImpl() |
58 | 728k | { |
59 | 728k | int rc; |
60 | 728k | if ((rc = pthread_mutex_lock(&_mutex))) |
61 | 0 | throw SystemException("cannot lock mutex", Error::getMessage(rc)); |
62 | 728k | } |
63 | | |
64 | | |
65 | | inline bool MutexImpl::tryLockImpl() |
66 | 0 | { |
67 | 0 | int rc = pthread_mutex_trylock(&_mutex); |
68 | 0 | if (rc == 0) |
69 | 0 | return true; |
70 | 0 | else if (rc == EBUSY) |
71 | 0 | return false; |
72 | 0 | else |
73 | 0 | throw SystemException("cannot lock mutex", Error::getMessage(rc)); |
74 | 0 | } |
75 | | |
76 | | |
77 | | inline void MutexImpl::unlockImpl() |
78 | 728k | { |
79 | 728k | int rc; |
80 | 728k | if ((rc = pthread_mutex_unlock(&_mutex))) |
81 | 0 | throw SystemException("cannot unlock mutex", Error::getMessage(rc)); |
82 | 728k | } |
83 | | |
84 | | |
85 | | } // namespace Poco |
86 | | |
87 | | |
88 | | #endif // Foundation_Mutex_POSIX_INCLUDED |