2007-05-20 20:30:31 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-05-21 02:40:06 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
/* required for TryEnterCriticalSection definition. Must be defined before windows.h include */
|
|
|
|
#define _WIN32_WINNT 0x0400
|
|
|
|
#endif
|
2007-05-20 20:30:31 +00:00
|
|
|
|
2007-05-21 02:40:06 +00:00
|
|
|
#include "openzap.h"
|
|
|
|
#include "zap_threadmutex.h"
|
2007-05-20 20:30:31 +00:00
|
|
|
|
|
|
|
#ifdef WIN32
|
2007-05-21 02:40:06 +00:00
|
|
|
#include <process.h>
|
|
|
|
|
|
|
|
#define ZAP_THREAD_CALLING_CONVENTION __stdcall
|
|
|
|
|
|
|
|
struct zap_mutex {
|
2007-05-20 20:30:31 +00:00
|
|
|
CRITICAL_SECTION mutex;
|
|
|
|
};
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#include <pthread.h>
|
2007-05-21 02:40:06 +00:00
|
|
|
|
|
|
|
#define ZAP_THREAD_CALLING_CONVENTION
|
|
|
|
|
|
|
|
struct zap_mutex {
|
2007-05-21 02:41:36 +00:00
|
|
|
pthread_mutex_t mutex;
|
2007-05-21 02:40:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct zap_thread {
|
|
|
|
#ifdef WIN32
|
|
|
|
void *handle;
|
|
|
|
#else
|
|
|
|
pthread_t handle;
|
|
|
|
#endif
|
|
|
|
void *private_data;
|
|
|
|
zap_thread_function_t function;
|
|
|
|
zap_size_t stack_size;
|
|
|
|
#ifndef WIN32
|
|
|
|
pthread_attr_t attribute;
|
|
|
|
#endif
|
2007-05-20 20:30:31 +00:00
|
|
|
};
|
|
|
|
|
2007-05-21 02:40:06 +00:00
|
|
|
zap_size_t thread_default_stacksize = 0;
|
|
|
|
|
|
|
|
void zap_thread_override_default_stacksize(zap_size_t size)
|
|
|
|
{
|
|
|
|
thread_default_stacksize = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void * ZAP_THREAD_CALLING_CONVENTION thread_launch(void *args)
|
|
|
|
{
|
|
|
|
void *exit_val;
|
|
|
|
zap_thread_t *thread = (zap_thread_t *)args;
|
|
|
|
exit_val = thread->function(thread, thread->private_data);
|
|
|
|
#ifndef WIN32
|
|
|
|
pthread_attr_destroy(&thread->attribute);
|
|
|
|
#endif
|
|
|
|
free(thread);
|
|
|
|
|
|
|
|
return exit_val;
|
|
|
|
}
|
|
|
|
|
|
|
|
zap_status_t zap_thread_create_detached(zap_thread_function_t func, void *data)
|
|
|
|
{
|
|
|
|
return zap_thread_create_detached_ex(func, data, thread_default_stacksize);
|
|
|
|
}
|
|
|
|
|
|
|
|
zap_status_t zap_thread_create_detached_ex(zap_thread_function_t func, void *data, zap_size_t stack_size)
|
|
|
|
{
|
|
|
|
zap_thread_t *thread = NULL;
|
|
|
|
zap_status_t status = ZAP_FAIL;
|
|
|
|
|
|
|
|
if (!func || !(thread = (zap_thread_t *)malloc(sizeof(zap_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);
|
2007-05-21 15:38:35 +00:00
|
|
|
|
|
|
|
status = ZAP_SUCCESS;
|
|
|
|
goto done;
|
2007-05-21 02:40:06 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
if (pthread_attr_init(&thread->attribute) != 0) goto fail;
|
|
|
|
|
2007-05-21 15:38:35 +00:00
|
|
|
if (pthread_attr_setdetachstate(&thread->attribute, PTHREAD_CREATE_DETACHED) != 0) goto failpthread;
|
2007-05-21 02:40:06 +00:00
|
|
|
|
2007-05-21 15:38:35 +00:00
|
|
|
if (thread->stack_size && pthread_attr_setstacksize(&thread->attribute, thread->stack_size) != 0) goto failpthread;
|
2007-05-21 02:40:06 +00:00
|
|
|
|
2007-05-21 15:38:35 +00:00
|
|
|
if (pthread_create(&thread->handle, &thread->attribute, thread_launch, thread) != 0) goto failpthread;
|
2007-05-21 02:40:06 +00:00
|
|
|
|
|
|
|
status = ZAP_SUCCESS;
|
|
|
|
goto done;
|
2007-06-04 14:53:12 +00:00
|
|
|
failpthread:
|
2007-05-21 15:38:35 +00:00
|
|
|
pthread_attr_destroy(&thread->attribute);
|
|
|
|
#endif
|
2007-05-21 02:40:06 +00:00
|
|
|
|
2007-06-04 14:53:12 +00:00
|
|
|
fail:
|
2007-05-21 02:40:06 +00:00
|
|
|
if (thread) {
|
|
|
|
free(thread);
|
|
|
|
}
|
2007-06-04 14:53:12 +00:00
|
|
|
done:
|
2007-05-21 02:40:06 +00:00
|
|
|
return status;
|
|
|
|
}
|
2007-05-20 20:30:31 +00:00
|
|
|
|
|
|
|
|
2007-05-21 02:40:06 +00:00
|
|
|
zap_status_t zap_mutex_create(zap_mutex_t **mutex)
|
2007-05-20 20:30:31 +00:00
|
|
|
{
|
2007-05-21 02:40:06 +00:00
|
|
|
zap_status_t status = ZAP_FAIL;
|
2007-05-20 20:30:31 +00:00
|
|
|
#ifndef WIN32
|
|
|
|
pthread_mutexattr_t attr;
|
|
|
|
#endif
|
2007-05-21 02:40:06 +00:00
|
|
|
zap_mutex_t *check = NULL;
|
2007-05-20 20:30:31 +00:00
|
|
|
|
2007-05-21 02:40:06 +00:00
|
|
|
check = (zap_mutex_t *)malloc(sizeof(**mutex));
|
2007-05-20 20:30:31 +00:00
|
|
|
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;
|
|
|
|
|
2007-06-04 14:53:12 +00:00
|
|
|
fail:
|
|
|
|
pthread_mutexattr_destroy(&attr);
|
|
|
|
goto done;
|
2007-05-20 20:30:31 +00:00
|
|
|
|
2007-06-04 14:53:12 +00:00
|
|
|
success:
|
2007-05-20 20:30:31 +00:00
|
|
|
#endif
|
|
|
|
*mutex = check;
|
2007-05-21 02:40:06 +00:00
|
|
|
status = ZAP_SUCCESS;
|
2007-05-20 20:30:31 +00:00
|
|
|
|
2007-06-04 14:53:12 +00:00
|
|
|
done:
|
2007-05-20 20:30:31 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2007-05-21 17:48:13 +00:00
|
|
|
zap_status_t zap_mutex_destroy(zap_mutex_t **mutex)
|
2007-05-20 20:30:31 +00:00
|
|
|
{
|
2007-05-21 17:48:13 +00:00
|
|
|
zap_mutex_t *mp = *mutex;
|
|
|
|
*mutex = NULL;
|
2008-03-19 00:26:27 +00:00
|
|
|
if (!mp) {
|
|
|
|
return ZAP_FAIL;
|
|
|
|
}
|
2007-05-20 20:30:31 +00:00
|
|
|
#ifdef WIN32
|
2007-05-21 17:48:13 +00:00
|
|
|
DeleteCriticalSection(&mp->mutex);
|
2007-05-20 20:30:31 +00:00
|
|
|
#else
|
2007-05-21 17:48:13 +00:00
|
|
|
if (pthread_mutex_destroy(&mp->mutex))
|
2007-05-21 02:40:06 +00:00
|
|
|
return ZAP_FAIL;
|
2007-05-20 20:30:31 +00:00
|
|
|
#endif
|
2007-05-21 17:48:13 +00:00
|
|
|
free(mp);
|
2007-05-21 02:40:06 +00:00
|
|
|
return ZAP_SUCCESS;
|
2007-05-20 20:30:31 +00:00
|
|
|
}
|
|
|
|
|
2008-02-18 16:15:24 +00:00
|
|
|
zap_status_t _zap_mutex_lock(zap_mutex_t *mutex)
|
2007-05-20 20:30:31 +00:00
|
|
|
{
|
|
|
|
#ifdef WIN32
|
|
|
|
EnterCriticalSection(&mutex->mutex);
|
|
|
|
#else
|
|
|
|
if (pthread_mutex_lock(&mutex->mutex))
|
2007-05-21 02:40:06 +00:00
|
|
|
return ZAP_FAIL;
|
2007-05-20 20:30:31 +00:00
|
|
|
#endif
|
2007-05-21 02:40:06 +00:00
|
|
|
return ZAP_SUCCESS;
|
2007-05-20 20:30:31 +00:00
|
|
|
}
|
|
|
|
|
2008-02-18 16:15:24 +00:00
|
|
|
zap_status_t _zap_mutex_trylock(zap_mutex_t *mutex)
|
2007-05-20 20:30:31 +00:00
|
|
|
{
|
|
|
|
#ifdef WIN32
|
|
|
|
if (!TryEnterCriticalSection(&mutex->mutex))
|
2007-05-21 02:40:06 +00:00
|
|
|
return ZAP_FAIL;
|
2007-05-20 20:30:31 +00:00
|
|
|
#else
|
2007-05-21 02:47:32 +00:00
|
|
|
if (pthread_mutex_trylock(&mutex->mutex))
|
2007-05-21 02:40:06 +00:00
|
|
|
return ZAP_FAIL;
|
2007-05-20 20:30:31 +00:00
|
|
|
#endif
|
2007-05-21 02:40:06 +00:00
|
|
|
return ZAP_SUCCESS;
|
2007-05-20 20:30:31 +00:00
|
|
|
}
|
|
|
|
|
2008-02-18 16:15:24 +00:00
|
|
|
zap_status_t _zap_mutex_unlock(zap_mutex_t *mutex)
|
2007-05-20 20:30:31 +00:00
|
|
|
{
|
|
|
|
#ifdef WIN32
|
|
|
|
LeaveCriticalSection(&mutex->mutex);
|
|
|
|
#else
|
|
|
|
if (pthread_mutex_unlock(&mutex->mutex))
|
2007-05-21 02:40:06 +00:00
|
|
|
return ZAP_FAIL;
|
2007-05-20 20:30:31 +00:00
|
|
|
#endif
|
2007-05-21 02:40:06 +00:00
|
|
|
return ZAP_SUCCESS;
|
2007-05-20 20:30:31 +00:00
|
|
|
}
|
2007-06-04 14:53:12 +00:00
|
|
|
|
2008-02-18 16:15:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-06-04 14:53:12 +00:00
|
|
|
/* 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 expandtab:
|
|
|
|
*/
|