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 #include "sir/mutex.h"
33 #include "sir/internal.h"
34 #include "sir/platform.h"
35
36 #if !defined(__WIN__)
37 bool _sir_mutexcreate(sir_mutex* mutex) {
38 if (_sir_validptr(mutex)) {
39 pthread_mutexattr_t attr;
40 int op = pthread_mutexattr_init(&attr);
41 if (0 == op) {
42 op = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
43 if (0 == op) {
44 op = pthread_mutex_init(mutex, &attr);
45 if (0 == op)
46 return true;
47 }
48 }
49
50 (void)_sir_handleerr(op);
51 }
52
53 return false;
54 }
55
56 bool _sir_mutexlock(sir_mutex* mutex) {
57 if (_sir_validptr(mutex)) {
58 int op = pthread_mutex_lock(mutex);
59 return 0 == op ? true : _sir_handleerr(op);
60 }
61
62 return false;
63 }
64
65 bool _sir_mutextrylock(sir_mutex* mutex) {
66 if (_sir_validptr(mutex)) {
67 int op = pthread_mutex_trylock(mutex);
68 return 0 == op ? true : _sir_handleerr(op);
69 }
70
71 return false;
72 }
73
74 bool _sir_mutexunlock(sir_mutex* mutex) {
75 if (_sir_validptr(mutex)) {
76 int op = pthread_mutex_unlock(mutex);
77 return 0 == op ? true : _sir_handleerr(op);
78 }
79
80 return false;
81 }
82
83 bool _sir_mutexdestroy(sir_mutex* mutex) {
84 if (_sir_validptr(mutex)) {
85 int op = pthread_mutex_destroy(mutex);
86 return 0 == op ? true : _sir_handleerr(op);
87 }
88
89 return false;
90 }
91 #else
92 bool _sir_mutexcreate(sir_mutex* mutex) {
93 if (_sir_validptr(mutex)) {
94 InitializeCriticalSection(mutex);
95 return true;
96 }
97
98 return false;
99 }
100
101 bool _sir_mutexlock(sir_mutex* mutex) {
102 if (_sir_validptr(mutex)) {
103 EnterCriticalSection(mutex);
104 return true;
105 }
106
107 return false;
108 }
109
110 bool _sir_mutextrylock(sir_mutex* mutex) {
111 if (_sir_validptr(mutex))
112 return FALSE != TryEnterCriticalSection(mutex);
113
114 return false;
115 }
116
117 bool _sir_mutexunlock(sir_mutex* mutex) {
118 if (_sir_validptr(mutex)) {
119 LeaveCriticalSection(mutex);
120 return true;
121 }
122
123 return false;
124 }
125
126 bool _sir_mutexdestroy(sir_mutex* mutex) {
127 if (_sir_validptr(mutex)) {
128 DeleteCriticalSection(mutex);
129 return true;
130 }
131
132 return false;
133 }
134 #endif