add detached thread abstraction.

git-svn-id: http://svn.openzap.org/svn/openzap/trunk@56 a93c3328-9c30-0410-af19-c9cd2b2d52af
This commit is contained in:
Michael Jerris 2007-05-21 02:40:06 +00:00
parent 89e4d886b2
commit 424f6107a6
4 changed files with 156 additions and 41 deletions

View File

@ -321,6 +321,22 @@
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}" UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
> >
</Filter> </Filter>
<Filter
Name="Makefile"
>
<File
RelativePath=".\src\Makefile"
>
</File>
<File
RelativePath=".\src\wanpipe"
>
</File>
<File
RelativePath=".\src\zt"
>
</File>
</Filter>
</Files> </Files>
<Globals> <Globals>
</Globals> </Globals>

View File

@ -21,17 +21,19 @@
#ifndef _ZAP_THREADMUTEX_H #ifndef _ZAP_THREADMUTEX_H
#define _ZAP_THREADMUTEX_H #define _ZAP_THREADMUTEX_H
typedef struct mutex mutex_t; #include "openzap.h"
typedef enum mutex_status { typedef struct zap_mutex zap_mutex_t;
MUTEX_SUCCESS, typedef struct zap_thread zap_thread_t;
MUTEX_FAILURE typedef void *(*zap_thread_function_t) (zap_thread_t *, void *);
} mutex_status_t;
mutex_status_t zap_mutex_create(mutex_t **mutex); zap_status_t zap_thread_create_detached(zap_thread_function_t func, void *data);
mutex_status_t zap_mutex_destroy(mutex_t *mutex); zap_status_t zap_thread_create_detached_ex(zap_thread_function_t func, void *data, zap_size_t stack_size);
mutex_status_t zap_mutex_lock(mutex_t *mutex); void zap_thread_override_default_stacksize(zap_size_t size);
mutex_status_t zap_mutex_trylock(mutex_t *mutex); zap_status_t zap_mutex_create(zap_mutex_t **mutex);
mutex_status_t zap_mutex_unlock(mutex_t *mutex); zap_status_t zap_mutex_destroy(zap_mutex_t *mutex);
zap_status_t zap_mutex_lock(zap_mutex_t *mutex);
zap_status_t zap_mutex_trylock(zap_mutex_t *mutex);
zap_status_t zap_mutex_unlock(zap_mutex_t *mutex);
#endif #endif

View File

@ -58,7 +58,10 @@ typedef enum {
ZAP_SUCCESS, ZAP_SUCCESS,
ZAP_FAIL, ZAP_FAIL,
ZAP_MEMERR, ZAP_MEMERR,
ZAP_TIMEOUT ZAP_TIMEOUT,
ZAP_NOTIMPL,
ZAP_STATUS_COUNT
} zap_status_t; } zap_status_t;
typedef enum { typedef enum {
@ -91,7 +94,9 @@ typedef enum {
ZAP_COMMAND_SET_DTMF_ON_PERIOD, ZAP_COMMAND_SET_DTMF_ON_PERIOD,
ZAP_COMMAND_GET_DTMF_ON_PERIOD, ZAP_COMMAND_GET_DTMF_ON_PERIOD,
ZAP_COMMAND_SET_DTMF_OFF_PERIOD, ZAP_COMMAND_SET_DTMF_OFF_PERIOD,
ZAP_COMMAND_GET_DTMF_OFF_PERIOD ZAP_COMMAND_GET_DTMF_OFF_PERIOD,
ZAP_COMMAND_COUNT
} zap_command_t; } zap_command_t;
typedef enum { typedef enum {
@ -104,7 +109,9 @@ typedef enum {
ZAP_CHAN_TYPE_DQ921, ZAP_CHAN_TYPE_DQ921,
ZAP_CHAN_TYPE_DQ931, ZAP_CHAN_TYPE_DQ931,
ZAP_CHAN_TYPE_FXS, ZAP_CHAN_TYPE_FXS,
ZAP_CHAN_TYPE_FXO ZAP_CHAN_TYPE_FXO,
ZAP_CHAN_TYPE_COUNT
} zap_chan_type_t; } zap_chan_type_t;
typedef enum { typedef enum {
@ -186,7 +193,9 @@ typedef zap_status_t (*zint_write_t) ZINT_WRITE_ARGS ;
typedef enum { typedef enum {
ZAP_EVENT_NONE, ZAP_EVENT_NONE,
ZAP_EVENT_DTMF ZAP_EVENT_DTMF,
ZAP_EVENT_COUNT
} zap_event_type_t; } zap_event_type_t;
typedef struct zap_span zap_span_t; typedef struct zap_span zap_span_t;

View File

@ -17,37 +17,125 @@
* *
*/ */
#define _XOPEN_SOURCE 600 #ifdef WIN32
#include <stdlib.h> /* required for TryEnterCriticalSection definition. Must be defined before windows.h include */
#define _WIN32_WINNT 0x0400
#endif
#include "openzap.h"
#include "zap_threadmutex.h" #include "zap_threadmutex.h"
#ifdef WIN32 #ifdef WIN32
#define _WIN32_WINNT 0x0400 #include <process.h>
#include <windows.h>
struct mutex { #define ZAP_THREAD_CALLING_CONVENTION __stdcall
struct zap_mutex {
CRITICAL_SECTION mutex; CRITICAL_SECTION mutex;
}; };
#else #else
#include <pthread.h> #include <pthread.h>
struct mutex {
pthread_mutex_t mutex; #define ZAP_THREAD_CALLING_CONVENTION
struct zap_mutex {
pthread_zap_mutex_t mutex;
}; };
#endif #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
};
mutex_status_t zap_mutex_create(mutex_t **mutex) zap_size_t thread_default_stacksize = 0;
void zap_thread_override_default_stacksize(zap_size_t size)
{ {
mutex_status_t status = MUTEX_FAILURE; 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);
#else
if (pthread_attr_init(&thread->attribute) != 0) goto fail;
if (pthread_attr_setdetachstate(&thread->attribute, PTHREAD_CREATE_DETACHED) != 0) goto fail;
if (stacksize && pthread_attr_setstacksize(&thread->attribute, stacksize) != 0) goto fail;
if (pthread_create(&thread->handle, &thread->attribute, thread_launch, thread) != 0) goto fail;
#endif
status = ZAP_SUCCESS;
goto done;
fail:
if (thread) {
free(thread);
}
done:
return status;
}
zap_status_t zap_mutex_create(zap_mutex_t **mutex)
{
zap_status_t status = ZAP_FAIL;
#ifndef WIN32 #ifndef WIN32
pthread_mutexattr_t attr; pthread_mutexattr_t attr;
#endif #endif
mutex_t *check = NULL; zap_mutex_t *check = NULL;
check = (mutex_t *)malloc(sizeof(**mutex)); check = (zap_mutex_t *)malloc(sizeof(**mutex));
if (!check) if (!check)
goto done; goto done;
#ifdef WIN32 #ifdef WIN32
@ -71,54 +159,54 @@ fail:
success: success:
#endif #endif
*mutex = check; *mutex = check;
status = MUTEX_SUCCESS; status = ZAP_SUCCESS;
done: done:
return status; return status;
} }
mutex_status_t zap_mutex_destroy(mutex_t *mutex) zap_status_t zap_mutex_destroy(zap_mutex_t *mutex)
{ {
#ifdef WIN32 #ifdef WIN32
DeleteCriticalSection(&mutex->mutex); DeleteCriticalSection(&mutex->mutex);
#else #else
if (pthread_mutex_destroy(&mutex->mutex)) if (pthread_mutex_destroy(&mutex->mutex))
return MUTEX_FAILURE; return ZAP_FAIL;
#endif #endif
free(mutex); free(mutex);
return MUTEX_SUCCESS; return ZAP_SUCCESS;
} }
mutex_status_t zap_mutex_lock(mutex_t *mutex) zap_status_t zap_mutex_lock(zap_mutex_t *mutex)
{ {
#ifdef WIN32 #ifdef WIN32
EnterCriticalSection(&mutex->mutex); EnterCriticalSection(&mutex->mutex);
#else #else
if (pthread_mutex_lock(&mutex->mutex)) if (pthread_mutex_lock(&mutex->mutex))
return MUTEX_FAILURE; return ZAP_FAIL;
#endif #endif
return MUTEX_SUCCESS; return ZAP_SUCCESS;
} }
mutex_status_t zap_mutex_trylock(mutex_t *mutex) zap_status_t zap_zap_mutex_trylock(zap_mutex_t *mutex)
{ {
#ifdef WIN32 #ifdef WIN32
if (!TryEnterCriticalSection(&mutex->mutex)) if (!TryEnterCriticalSection(&mutex->mutex))
return MUTEX_FAILURE; return ZAP_FAIL;
#else #else
if (pthread_mutex_trylock(&mutex->mutex)) if (pthread_zap_mutex_trylock(&mutex->mutex))
return MUTEX_FAILURE; return ZAP_FAIL;
#endif #endif
return MUTEX_SUCCESS; return ZAP_SUCCESS;
} }
mutex_status_t zap_mutex_unlock(mutex_t *mutex) zap_status_t zap_mutex_unlock(zap_mutex_t *mutex)
{ {
#ifdef WIN32 #ifdef WIN32
LeaveCriticalSection(&mutex->mutex); LeaveCriticalSection(&mutex->mutex);
#else #else
if (pthread_mutex_unlock(&mutex->mutex)) if (pthread_mutex_unlock(&mutex->mutex))
return MUTEX_FAILURE; return ZAP_FAIL;
#endif #endif
return MUTEX_SUCCESS; return ZAP_SUCCESS;
} }