This source file includes following definitions.
- _sir_mutexcreate
- _sir_mutexlock
- _sir_mutextrylock
- _sir_mutexunlock
- _sir_mutexdestroy
- _sir_mutexcreate
- _sir_mutexlock
- _sir_mutextrylock
- _sir_mutexunlock
- _sir_mutexdestroy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 #include "sir/mutex.h"
34 #include "sir/internal.h"
35 #include "sir/platform.h"
36
37 #if !defined(__WIN__)
38 bool _sir_mutexcreate(sir_mutex* mutex) {
39 if (_sir_validptr(mutex)) {
40 pthread_mutexattr_t attr;
41 int op = pthread_mutexattr_init(&attr);
42 if (0 == op) {
43 op = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
44 if (0 == op) {
45 op = pthread_mutex_init(mutex, &attr);
46 if (0 == op)
47 return true;
48 }
49 }
50
51 (void)_sir_handleerr(op);
52 }
53
54 return false;
55 }
56
57 bool _sir_mutexlock(sir_mutex* mutex) {
58 if (_sir_validptr(mutex)) {
59 int op = pthread_mutex_lock(mutex);
60 return 0 == op ? true : _sir_handleerr(op);
61 }
62
63 return false;
64 }
65
66 bool _sir_mutextrylock(sir_mutex* mutex) {
67 if (_sir_validptr(mutex)) {
68 int op = pthread_mutex_trylock(mutex);
69 return 0 == op ? true : _sir_handleerr(op);
70 }
71
72 return false;
73 }
74
75 bool _sir_mutexunlock(sir_mutex* mutex) {
76 if (_sir_validptr(mutex)) {
77 int op = pthread_mutex_unlock(mutex);
78 return 0 == op ? true : _sir_handleerr(op);
79 }
80
81 return false;
82 }
83
84 bool _sir_mutexdestroy(sir_mutex* mutex) {
85 if (_sir_validptr(mutex)) {
86 int op = pthread_mutex_destroy(mutex);
87 return 0 == op ? true : _sir_handleerr(op);
88 }
89
90 return false;
91 }
92 #else
93 bool _sir_mutexcreate(sir_mutex* mutex) {
94 if (_sir_validptr(mutex)) {
95 InitializeCriticalSection(mutex);
96 return true;
97 }
98
99 return false;
100 }
101
102 bool _sir_mutexlock(sir_mutex* mutex) {
103 if (_sir_validptr(mutex)) {
104 EnterCriticalSection(mutex);
105 return true;
106 }
107
108 return false;
109 }
110
111 bool _sir_mutextrylock(sir_mutex* mutex) {
112 if (_sir_validptr(mutex))
113 return FALSE != TryEnterCriticalSection(mutex);
114
115 return false;
116 }
117
118 bool _sir_mutexunlock(sir_mutex* mutex) {
119 if (_sir_validptr(mutex)) {
120 LeaveCriticalSection(mutex);
121 return true;
122 }
123
124 return false;
125 }
126
127 bool _sir_mutexdestroy(sir_mutex* mutex) {
128 if (_sir_validptr(mutex)) {
129 DeleteCriticalSection(mutex);
130 return true;
131 }
132
133 return false;
134 }
135 #endif