2 * Cross Platform Thread/Mutex abstraction
3 * Copyright(C) 2007 Michael Jerris
5 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
6 * copies of the Software, and permit persons to whom the Software is
9 * This work is provided under this license on an "as is" basis, without warranty of any kind,
10 * either expressed or implied, including, without limitation, warranties that the covered code
11 * is free of defects, merchantable, fit for a particular purpose or non-infringing. The entire
12 * risk as to the quality and performance of the covered code is with you. Should any covered
13 * code prove defective in any respect, you (not the initial developer or any other contributor)
14 * assume the cost of any necessary servicing, repair or correction. This disclaimer of warranty
15 * constitutes an essential part of this license. No use of any covered code is authorized hereunder
16 * except under this disclaimer.
21 /* required for TryEnterCriticalSection definition. Must be defined before windows.h include */
22 #define _WIN32_WINNT 0x0400
26 #include "esl_threadmutex.h"
31 #define ESL_THREAD_CALLING_CONVENTION __stdcall
34 CRITICAL_SECTION mutex;
41 #define ESL_THREAD_CALLING_CONVENTION
44 pthread_mutex_t mutex;
56 esl_thread_function_t function;
59 pthread_attr_t attribute;
63 size_t thread_default_stacksize = 0;
65 void esl_thread_override_default_stacksize(size_t size)
67 thread_default_stacksize = size;
70 static void * ESL_THREAD_CALLING_CONVENTION thread_launch(void *args)
73 esl_thread_t *thread = (esl_thread_t *)args;
74 exit_val = thread->function(thread, thread->private_data);
76 pthread_attr_destroy(&thread->attribute);
83 ESL_DECLARE(esl_status_t) esl_thread_create_detached(esl_thread_function_t func, void *data)
85 return esl_thread_create_detached_ex(func, data, thread_default_stacksize);
88 esl_status_t esl_thread_create_detached_ex(esl_thread_function_t func, void *data, size_t stack_size)
90 esl_thread_t *thread = NULL;
91 esl_status_t status = ESL_FAIL;
93 if (!func || !(thread = (esl_thread_t *)malloc(sizeof(esl_thread_t)))) {
97 thread->private_data = data;
98 thread->function = func;
99 thread->stack_size = stack_size;
102 thread->handle = (void *)_beginthreadex(NULL, (unsigned)thread->stack_size, (unsigned int (__stdcall *)(void *))thread_launch, thread, 0, NULL);
103 if (!thread->handle) {
106 CloseHandle(thread->handle);
108 status = ESL_SUCCESS;
112 if (pthread_attr_init(&thread->attribute) != 0) goto fail;
114 if (pthread_attr_setdetachstate(&thread->attribute, PTHREAD_CREATE_DETACHED) != 0) goto failpthread;
116 if (thread->stack_size && pthread_attr_setstacksize(&thread->attribute, thread->stack_size) != 0) goto failpthread;
118 if (pthread_create(&thread->handle, &thread->attribute, thread_launch, thread) != 0) goto failpthread;
120 status = ESL_SUCCESS;
123 pthread_attr_destroy(&thread->attribute);
135 ESL_DECLARE(esl_status_t) esl_mutex_create(esl_mutex_t **mutex)
137 esl_status_t status = ESL_FAIL;
139 pthread_mutexattr_t attr;
141 esl_mutex_t *check = NULL;
143 check = (esl_mutex_t *)malloc(sizeof(**mutex));
147 InitializeCriticalSection(&check->mutex);
149 if (pthread_mutexattr_init(&attr))
152 if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE))
155 if (pthread_mutex_init(&check->mutex, &attr))
161 pthread_mutexattr_destroy(&attr);
167 status = ESL_SUCCESS;
173 ESL_DECLARE(esl_status_t) esl_mutex_destroy(esl_mutex_t **mutex)
175 esl_mutex_t *mp = *mutex;
181 DeleteCriticalSection(&mp->mutex);
183 if (pthread_mutex_destroy(&mp->mutex))
190 ESL_DECLARE(esl_status_t) esl_mutex_lock(esl_mutex_t *mutex)
193 EnterCriticalSection(&mutex->mutex);
195 if (pthread_mutex_lock(&mutex->mutex))
201 ESL_DECLARE(esl_status_t) esl_mutex_trylock(esl_mutex_t *mutex)
204 if (!TryEnterCriticalSection(&mutex->mutex))
207 if (pthread_mutex_trylock(&mutex->mutex))
213 ESL_DECLARE(esl_status_t) esl_mutex_unlock(esl_mutex_t *mutex)
216 LeaveCriticalSection(&mutex->mutex);
218 if (pthread_mutex_unlock(&mutex->mutex))
236 * vim:set softtabstop=4 shiftwidth=4 tabstop=4 expandtab: