| 1 | // | 
|---|
| 2 | // SynchronizedObject.h | 
|---|
| 3 | // | 
|---|
| 4 | // Library: Foundation | 
|---|
| 5 | // Package: Threading | 
|---|
| 6 | // Module:  SynchronizedObject | 
|---|
| 7 | // | 
|---|
| 8 | // Definition of the SynchronizedObject class. | 
|---|
| 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_SynchronizedObject_INCLUDED | 
|---|
| 18 | #define Foundation_SynchronizedObject_INCLUDED | 
|---|
| 19 |  | 
|---|
| 20 |  | 
|---|
| 21 | #include "Poco/Foundation.h" | 
|---|
| 22 | #include "Poco/Mutex.h" | 
|---|
| 23 | #include "Poco/Event.h" | 
|---|
| 24 |  | 
|---|
| 25 |  | 
|---|
| 26 | namespace Poco { | 
|---|
| 27 |  | 
|---|
| 28 |  | 
|---|
| 29 | class Foundation_API SynchronizedObject | 
|---|
| 30 | /// This class aggregates a Mutex and an Event | 
|---|
| 31 | /// and can act as a base class for all objects | 
|---|
| 32 | /// requiring synchronization in a multithreaded | 
|---|
| 33 | /// scenario. | 
|---|
| 34 | { | 
|---|
| 35 | public: | 
|---|
| 36 | typedef Poco::ScopedLock<SynchronizedObject> ScopedLock; | 
|---|
| 37 |  | 
|---|
| 38 | SynchronizedObject(); | 
|---|
| 39 | /// Creates the object. | 
|---|
| 40 |  | 
|---|
| 41 | virtual ~SynchronizedObject(); | 
|---|
| 42 | /// Destroys the object. | 
|---|
| 43 |  | 
|---|
| 44 | void lock() const; | 
|---|
| 45 | /// Locks the object. Blocks if the object | 
|---|
| 46 | /// is locked by another thread. | 
|---|
| 47 |  | 
|---|
| 48 | bool tryLock() const; | 
|---|
| 49 | /// Tries to lock the object. Returns false immediately | 
|---|
| 50 | /// if the object is already locked by another thread | 
|---|
| 51 | /// Returns true if the object was successfully locked. | 
|---|
| 52 |  | 
|---|
| 53 | void unlock() const; | 
|---|
| 54 | /// Unlocks the object so that it can be locked by | 
|---|
| 55 | /// other threads. | 
|---|
| 56 |  | 
|---|
| 57 | void notify() const; | 
|---|
| 58 | /// Signals the object. | 
|---|
| 59 | /// Exactly only one thread waiting for the object | 
|---|
| 60 | /// can resume execution. | 
|---|
| 61 |  | 
|---|
| 62 | void wait() const; | 
|---|
| 63 | /// Waits for the object to become signalled. | 
|---|
| 64 |  | 
|---|
| 65 | void wait(long milliseconds) const; | 
|---|
| 66 | /// Waits for the object to become signalled. | 
|---|
| 67 | /// Throws a TimeoutException if the object | 
|---|
| 68 | /// does not become signalled within the specified | 
|---|
| 69 | /// time interval. | 
|---|
| 70 |  | 
|---|
| 71 | bool tryWait(long milliseconds) const; | 
|---|
| 72 | /// Waits for the object to become signalled. | 
|---|
| 73 | /// Returns true if the object | 
|---|
| 74 | /// became signalled within the specified | 
|---|
| 75 | /// time interval, false otherwise. | 
|---|
| 76 |  | 
|---|
| 77 | private: | 
|---|
| 78 | mutable Mutex _mutex; | 
|---|
| 79 | mutable Event _event; | 
|---|
| 80 | }; | 
|---|
| 81 |  | 
|---|
| 82 |  | 
|---|
| 83 | // | 
|---|
| 84 | // inlines | 
|---|
| 85 | // | 
|---|
| 86 | inline void SynchronizedObject::lock() const | 
|---|
| 87 | { | 
|---|
| 88 | _mutex.lock(); | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 |  | 
|---|
| 92 | inline bool SynchronizedObject::tryLock() const | 
|---|
| 93 | { | 
|---|
| 94 | return _mutex.tryLock(); | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 |  | 
|---|
| 98 | inline void SynchronizedObject::unlock() const | 
|---|
| 99 | { | 
|---|
| 100 | _mutex.unlock(); | 
|---|
| 101 | } | 
|---|
| 102 |  | 
|---|
| 103 |  | 
|---|
| 104 | inline void SynchronizedObject::notify() const | 
|---|
| 105 | { | 
|---|
| 106 | _event.set(); | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 |  | 
|---|
| 110 | inline void SynchronizedObject::wait() const | 
|---|
| 111 | { | 
|---|
| 112 | _event.wait(); | 
|---|
| 113 | } | 
|---|
| 114 |  | 
|---|
| 115 |  | 
|---|
| 116 | inline void SynchronizedObject::wait(long milliseconds) const | 
|---|
| 117 | { | 
|---|
| 118 | _event.wait(milliseconds); | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 |  | 
|---|
| 122 | inline bool SynchronizedObject::tryWait(long milliseconds) const | 
|---|
| 123 | { | 
|---|
| 124 | return _event.tryWait(milliseconds); | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 |  | 
|---|
| 128 | } // namespace Poco | 
|---|
| 129 |  | 
|---|
| 130 |  | 
|---|
| 131 | #endif // Foundation_SynchronizedObject_INCLUDED | 
|---|
| 132 |  | 
|---|