1 |
|
---|
2 | #include "CRXMutex.h"
|
---|
3 |
|
---|
4 | CRXMutex::CRXMutex (void)
|
---|
5 | : mIsCreated (false),
|
---|
6 | #ifdef _WIN32
|
---|
7 | mMutex (NULL)
|
---|
8 | #endif
|
---|
9 | {
|
---|
10 | /*----------------------------------------------------------------*/
|
---|
11 | /*----------------------------------------------------------------*/
|
---|
12 | }
|
---|
13 |
|
---|
14 | CRXMutex::~CRXMutex (void)
|
---|
15 | {
|
---|
16 | /*----------------------------------------------------------------*/
|
---|
17 | this->Unlock ();
|
---|
18 | this->Destroy ();
|
---|
19 | /*----------------------------------------------------------------*/
|
---|
20 | }
|
---|
21 |
|
---|
22 | bool
|
---|
23 | CRXMutex::IsCreated (void) const
|
---|
24 | {
|
---|
25 | /*----------------------------------------------------------------*/
|
---|
26 | /*----------------------------------------------------------------*/
|
---|
27 |
|
---|
28 | return mIsCreated;
|
---|
29 | }
|
---|
30 |
|
---|
31 | int
|
---|
32 | CRXMutex::Create (void)
|
---|
33 | {
|
---|
34 | int aResult = 0;
|
---|
35 |
|
---|
36 | /*----------------------------------------------------------------*/
|
---|
37 | #ifdef _WIN32
|
---|
38 | if ((mMutex = ::CreateMutexA (NULL, FALSE, NULL)) == NULL)
|
---|
39 | aResult = GetLastError ();
|
---|
40 | #else
|
---|
41 | aResult = pthread_mutex_init (&mMutex, NULL);
|
---|
42 | #endif
|
---|
43 |
|
---|
44 | mIsCreated = true;
|
---|
45 | /*----------------------------------------------------------------*/
|
---|
46 |
|
---|
47 | return aResult;
|
---|
48 | }
|
---|
49 |
|
---|
50 | int
|
---|
51 | CRXMutex::Destroy (void)
|
---|
52 | {
|
---|
53 | int aResult = 0;
|
---|
54 |
|
---|
55 | /*----------------------------------------------------------------*/
|
---|
56 | #ifdef _WIN32
|
---|
57 | if (::CloseHandle (mMutex))
|
---|
58 | aResult = GetLastError ();
|
---|
59 | #else
|
---|
60 | aResult = pthread_mutex_destroy (&mMutex);
|
---|
61 | #endif
|
---|
62 |
|
---|
63 | mIsCreated = false;
|
---|
64 | /*----------------------------------------------------------------*/
|
---|
65 |
|
---|
66 | return aResult;
|
---|
67 | }
|
---|
68 |
|
---|
69 | void
|
---|
70 | CRXMutex::Lock (void)
|
---|
71 | {
|
---|
72 | /*----------------------------------------------------------------*/
|
---|
73 | #ifdef _WIN32
|
---|
74 | ::WaitForSingleObject (mMutex, INFINITE);
|
---|
75 | #else
|
---|
76 | pthread_mutex_lock (&mMutex);
|
---|
77 | #endif
|
---|
78 | /*----------------------------------------------------------------*/
|
---|
79 | }
|
---|
80 |
|
---|
81 | void
|
---|
82 | CRXMutex::Unlock (void)
|
---|
83 | {
|
---|
84 | /*----------------------------------------------------------------*/
|
---|
85 | #ifdef _WIN32
|
---|
86 | if (mMutex != NULL)
|
---|
87 | ::ReleaseMutex(mMutex);
|
---|
88 | #else
|
---|
89 | pthread_mutex_unlock (&mMutex);
|
---|
90 | #endif
|
---|
91 | /*----------------------------------------------------------------*/
|
---|
92 | }
|
---|