soui 5.0.0.1
Soui5 Doc
 
Loading...
Searching...
No Matches
SRwLock.h
1#ifndef _SRW_LOCK_H_
2#define _SRW_LOCK_H_
3
4#include <utilities-def.h>
5#include <helper/SNoCopyable.hpp>
6
7SNSBEGIN
8
9struct IRwLock;
10
11/**
12 * @class SRwLock
13 * @brief A read-write lock class.
14 * @details This class provides methods to acquire and release shared and exclusive locks.
15 * @note This class is non-copyable.
16 */
17class UTILITIES_API SRwLock : public SNoCopyable {
18public:
19 /**
20 * @brief Constructor.
21 * Initializes a new read-write lock.
22 */
23 SRwLock();
24
25 /**
26 * @brief Destructor.
27 * Destroys the read-write lock.
28 */
29 ~SRwLock();
30
31 /**
32 * @brief Acquires an exclusive lock.
33 * @details This method blocks the calling thread until the exclusive lock is available.
34 */
35 void LockExclusive();
36
37 /**
38 * @brief Releases an exclusive lock.
39 * @details This method releases the exclusive lock, allowing other threads to acquire it.
40 */
41 void UnlockExclusive();
42
43 /**
44 * @brief Acquires a shared lock.
45 * @details This method blocks the calling thread until the shared lock is available.
46 */
47 void LockShared();
48
49 /**
50 * @brief Releases a shared lock.
51 * @details This method releases the shared lock, allowing other threads to acquire it.
52 */
53 void UnlockShared();
54
55private:
56 IRwLock* impl; /**< Pointer to the read-write lock implementation. */
57};
58
59/**
60 * @class SAutoReadLock
61 * @brief Auto-lock class for managing shared locks.
62 * @details This class automatically acquires a shared lock upon construction and releases it upon destruction.
63 * @note This class is non-copyable.
64 */
65class UTILITIES_API SAutoReadLock : public SNoCopyable {
66public:
67 /**
68 * @brief Constructor.
69 * @param plock Pointer to the SRwLock object to manage.
70 * @details Acquires a shared lock.
71 */
72 SAutoReadLock(SRwLock *plock) : m_pLock(plock) {
73 m_pLock->LockShared();
74 }
75
76 /**
77 * @brief Destructor.
78 * @details Releases the shared lock.
79 */
81 m_pLock->UnlockShared();
82 }
83
84private:
85 SRwLock *m_pLock; /**< Pointer to the managed SRwLock object. */
86};
87
88/**
89 * @class SAutoWriteLock
90 * @brief Auto-lock class for managing exclusive locks.
91 * @details This class automatically acquires an exclusive lock upon construction and releases it upon destruction.
92 * @note This class is non-copyable.
93 */
94class UTILITIES_API SAutoWriteLock : public SNoCopyable {
95public:
96 /**
97 * @brief Constructor.
98 * @param plock Pointer to the SRwLock object to manage.
99 * @details Acquires an exclusive lock.
100 */
101 SAutoWriteLock(SRwLock *plock) : m_pLock(plock) {
102 m_pLock->LockExclusive();
103 }
104
105 /**
106 * @brief Destructor.
107 * @details Releases the exclusive lock.
108 */
110 m_pLock->UnlockExclusive();
111 }
112
113private:
114 SRwLock *m_pLock; /**< Pointer to the managed SRwLock object. */
115};
116
117SNSEND
118
119#endif // _SRW_LOCK_H_
~SAutoReadLock()
Destructor.
Definition SRwLock.h:80
SAutoReadLock(SRwLock *plock)
Constructor.
Definition SRwLock.h:72
~SAutoWriteLock()
Destructor.
Definition SRwLock.h:109
SAutoWriteLock(SRwLock *plock)
Constructor.
Definition SRwLock.h:101
A read-write lock class.
Definition SRwLock.h:17
SRwLock()
Constructor. Initializes a new read-write lock.
Definition SRwLock.cpp:189
void LockShared()
Acquires a shared lock.
Definition SRwLock.cpp:218
void UnlockExclusive()
Releases an exclusive lock.
Definition SRwLock.cpp:213
void UnlockShared()
Releases a shared lock.
Definition SRwLock.cpp:223
void LockExclusive()
Acquires an exclusive lock.
Definition SRwLock.cpp:208