esl_threadmutex.c
5.1 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* Cross Platform Thread/Mutex abstraction
* Copyright(C) 2007 Michael Jerris
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so.
*
* This work is provided under this license on an "as is" basis, without warranty of any kind,
* either expressed or implied, including, without limitation, warranties that the covered code
* is free of defects, merchantable, fit for a particular purpose or non-infringing. The entire
* risk as to the quality and performance of the covered code is with you. Should any covered
* code prove defective in any respect, you (not the initial developer or any other contributor)
* assume the cost of any necessary servicing, repair or correction. This disclaimer of warranty
* constitutes an essential part of this license. No use of any covered code is authorized hereunder
* except under this disclaimer.
*
*/
#ifdef WIN32
/* required for TryEnterCriticalSection definition. Must be defined before windows.h include */
#define _WIN32_WINNT 0x0400
#endif
#include "esl.h"
#include "esl_threadmutex.h"
#ifdef WIN32
#include <process.h>
#define ESL_THREAD_CALLING_CONVENTION __stdcall
struct esl_mutex {
CRITICAL_SECTION mutex;
};
#else
#include <pthread.h>
#define ESL_THREAD_CALLING_CONVENTION
struct esl_mutex {
pthread_mutex_t mutex;
};
#endif
struct esl_thread {
#ifdef WIN32
void *handle;
#else
pthread_t handle;
#endif
void *private_data;
esl_thread_function_t function;
size_t stack_size;
#ifndef WIN32
pthread_attr_t attribute;
#endif
};
size_t thread_default_stacksize = 0;
void esl_thread_override_default_stacksize(size_t size)
{
thread_default_stacksize = size;
}
static void * ESL_THREAD_CALLING_CONVENTION thread_launch(void *args)
{
void *exit_val;
esl_thread_t *thread = (esl_thread_t *)args;
exit_val = thread->function(thread, thread->private_data);
#ifndef WIN32
pthread_attr_destroy(&thread->attribute);
#endif
free(thread);
return exit_val;
}
ESL_DECLARE(esl_status_t) esl_thread_create_detached(esl_thread_function_t func, void *data)
{
return esl_thread_create_detached_ex(func, data, thread_default_stacksize);
}
esl_status_t esl_thread_create_detached_ex(esl_thread_function_t func, void *data, size_t stack_size)
{
esl_thread_t *thread = NULL;
esl_status_t status = ESL_FAIL;
if (!func || !(thread = (esl_thread_t *)malloc(sizeof(esl_thread_t)))) {
goto done;
}
thread->private_data = data;
thread->function = func;
thread->stack_size = stack_size;
#if defined(WIN32)
thread->handle = (void *)_beginthreadex(NULL, (unsigned)thread->stack_size, (unsigned int (__stdcall *)(void *))thread_launch, thread, 0, NULL);
if (!thread->handle) {
goto fail;
}
CloseHandle(thread->handle);
status = ESL_SUCCESS;
goto done;
#else
if (pthread_attr_init(&thread->attribute) != 0) goto fail;
if (pthread_attr_setdetachstate(&thread->attribute, PTHREAD_CREATE_DETACHED) != 0) goto failpthread;
if (thread->stack_size && pthread_attr_setstacksize(&thread->attribute, thread->stack_size) != 0) goto failpthread;
if (pthread_create(&thread->handle, &thread->attribute, thread_launch, thread) != 0) goto failpthread;
status = ESL_SUCCESS;
goto done;
failpthread:
pthread_attr_destroy(&thread->attribute);
#endif
fail:
if (thread) {
free(thread);
}
done:
return status;
}
ESL_DECLARE(esl_status_t) esl_mutex_create(esl_mutex_t **mutex)
{
esl_status_t status = ESL_FAIL;
#ifndef WIN32
pthread_mutexattr_t attr;
#endif
esl_mutex_t *check = NULL;
check = (esl_mutex_t *)malloc(sizeof(**mutex));
if (!check)
goto done;
#ifdef WIN32
InitializeCriticalSection(&check->mutex);
#else
if (pthread_mutexattr_init(&attr))
goto done;
if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE))
goto fail;
if (pthread_mutex_init(&check->mutex, &attr))
goto fail;
goto success;
fail:
pthread_mutexattr_destroy(&attr);
goto done;
success:
#endif
*mutex = check;
status = ESL_SUCCESS;
done:
return status;
}
ESL_DECLARE(esl_status_t) esl_mutex_destroy(esl_mutex_t **mutex)
{
esl_mutex_t *mp = *mutex;
*mutex = NULL;
if (!mp) {
return ESL_FAIL;
}
#ifdef WIN32
DeleteCriticalSection(&mp->mutex);
#else
if (pthread_mutex_destroy(&mp->mutex))
return ESL_FAIL;
#endif
free(mp);
return ESL_SUCCESS;
}
ESL_DECLARE(esl_status_t) esl_mutex_lock(esl_mutex_t *mutex)
{
#ifdef WIN32
EnterCriticalSection(&mutex->mutex);
#else
if (pthread_mutex_lock(&mutex->mutex))
return ESL_FAIL;
#endif
return ESL_SUCCESS;
}
ESL_DECLARE(esl_status_t) esl_mutex_trylock(esl_mutex_t *mutex)
{
#ifdef WIN32
if (!TryEnterCriticalSection(&mutex->mutex))
return ESL_FAIL;
#else
if (pthread_mutex_trylock(&mutex->mutex))
return ESL_FAIL;
#endif
return ESL_SUCCESS;
}
ESL_DECLARE(esl_status_t) esl_mutex_unlock(esl_mutex_t *mutex)
{
#ifdef WIN32
LeaveCriticalSection(&mutex->mutex);
#else
if (pthread_mutex_unlock(&mutex->mutex))
return ESL_FAIL;
#endif
return ESL_SUCCESS;
}
/* For Emacs:
* Local Variables:
* mode:c
* indent-tabs-mode:t
* tab-width:4
* c-basic-offset:4
* End:
* For VIM:
* vim:set softtabstop=4 shiftwidth=4 tabstop=4:
*/