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:
parent
89e4d886b2
commit
424f6107a6
|
@ -321,6 +321,22 @@
|
|||
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
|
||||
>
|
||||
</Filter>
|
||||
<Filter
|
||||
Name="Makefile"
|
||||
>
|
||||
<File
|
||||
RelativePath=".\src\Makefile"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\wanpipe"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\zt"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
</Files>
|
||||
<Globals>
|
||||
</Globals>
|
||||
|
|
|
@ -21,17 +21,19 @@
|
|||
#ifndef _ZAP_THREADMUTEX_H
|
||||
#define _ZAP_THREADMUTEX_H
|
||||
|
||||
typedef struct mutex mutex_t;
|
||||
#include "openzap.h"
|
||||
|
||||
typedef enum mutex_status {
|
||||
MUTEX_SUCCESS,
|
||||
MUTEX_FAILURE
|
||||
} mutex_status_t;
|
||||
typedef struct zap_mutex zap_mutex_t;
|
||||
typedef struct zap_thread zap_thread_t;
|
||||
typedef void *(*zap_thread_function_t) (zap_thread_t *, void *);
|
||||
|
||||
mutex_status_t zap_mutex_create(mutex_t **mutex);
|
||||
mutex_status_t zap_mutex_destroy(mutex_t *mutex);
|
||||
mutex_status_t zap_mutex_lock(mutex_t *mutex);
|
||||
mutex_status_t zap_mutex_trylock(mutex_t *mutex);
|
||||
mutex_status_t zap_mutex_unlock(mutex_t *mutex);
|
||||
zap_status_t zap_thread_create_detached(zap_thread_function_t func, void *data);
|
||||
zap_status_t zap_thread_create_detached_ex(zap_thread_function_t func, void *data, zap_size_t stack_size);
|
||||
void zap_thread_override_default_stacksize(zap_size_t size);
|
||||
zap_status_t zap_mutex_create(zap_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
|
||||
|
|
|
@ -58,7 +58,10 @@ typedef enum {
|
|||
ZAP_SUCCESS,
|
||||
ZAP_FAIL,
|
||||
ZAP_MEMERR,
|
||||
ZAP_TIMEOUT
|
||||
ZAP_TIMEOUT,
|
||||
ZAP_NOTIMPL,
|
||||
|
||||
ZAP_STATUS_COUNT
|
||||
} zap_status_t;
|
||||
|
||||
typedef enum {
|
||||
|
@ -91,7 +94,9 @@ typedef enum {
|
|||
ZAP_COMMAND_SET_DTMF_ON_PERIOD,
|
||||
ZAP_COMMAND_GET_DTMF_ON_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;
|
||||
|
||||
typedef enum {
|
||||
|
@ -104,7 +109,9 @@ typedef enum {
|
|||
ZAP_CHAN_TYPE_DQ921,
|
||||
ZAP_CHAN_TYPE_DQ931,
|
||||
ZAP_CHAN_TYPE_FXS,
|
||||
ZAP_CHAN_TYPE_FXO
|
||||
ZAP_CHAN_TYPE_FXO,
|
||||
|
||||
ZAP_CHAN_TYPE_COUNT
|
||||
} zap_chan_type_t;
|
||||
|
||||
typedef enum {
|
||||
|
@ -186,7 +193,9 @@ typedef zap_status_t (*zint_write_t) ZINT_WRITE_ARGS ;
|
|||
|
||||
typedef enum {
|
||||
ZAP_EVENT_NONE,
|
||||
ZAP_EVENT_DTMF
|
||||
ZAP_EVENT_DTMF,
|
||||
|
||||
ZAP_EVENT_COUNT
|
||||
} zap_event_type_t;
|
||||
|
||||
typedef struct zap_span zap_span_t;
|
||||
|
|
|
@ -17,37 +17,125 @@
|
|||
*
|
||||
*/
|
||||
|
||||
#define _XOPEN_SOURCE 600
|
||||
#include <stdlib.h>
|
||||
#ifdef WIN32
|
||||
/* required for TryEnterCriticalSection definition. Must be defined before windows.h include */
|
||||
#define _WIN32_WINNT 0x0400
|
||||
#endif
|
||||
|
||||
#include "openzap.h"
|
||||
#include "zap_threadmutex.h"
|
||||
|
||||
|
||||
#ifdef WIN32
|
||||
#define _WIN32_WINNT 0x0400
|
||||
#include <windows.h>
|
||||
struct mutex {
|
||||
#include <process.h>
|
||||
|
||||
#define ZAP_THREAD_CALLING_CONVENTION __stdcall
|
||||
|
||||
struct zap_mutex {
|
||||
CRITICAL_SECTION mutex;
|
||||
};
|
||||
|
||||
#else
|
||||
|
||||
#include <pthread.h>
|
||||
struct mutex {
|
||||
pthread_mutex_t mutex;
|
||||
|
||||
#define ZAP_THREAD_CALLING_CONVENTION
|
||||
|
||||
struct zap_mutex {
|
||||
pthread_zap_mutex_t mutex;
|
||||
};
|
||||
|
||||
#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
|
||||
pthread_mutexattr_t attr;
|
||||
#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)
|
||||
goto done;
|
||||
#ifdef WIN32
|
||||
|
@ -71,54 +159,54 @@ fail:
|
|||
success:
|
||||
#endif
|
||||
*mutex = check;
|
||||
status = MUTEX_SUCCESS;
|
||||
status = ZAP_SUCCESS;
|
||||
|
||||
done:
|
||||
return status;
|
||||
}
|
||||
|
||||
mutex_status_t zap_mutex_destroy(mutex_t *mutex)
|
||||
zap_status_t zap_mutex_destroy(zap_mutex_t *mutex)
|
||||
{
|
||||
#ifdef WIN32
|
||||
DeleteCriticalSection(&mutex->mutex);
|
||||
#else
|
||||
if (pthread_mutex_destroy(&mutex->mutex))
|
||||
return MUTEX_FAILURE;
|
||||
return ZAP_FAIL;
|
||||
#endif
|
||||
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
|
||||
EnterCriticalSection(&mutex->mutex);
|
||||
#else
|
||||
if (pthread_mutex_lock(&mutex->mutex))
|
||||
return MUTEX_FAILURE;
|
||||
return ZAP_FAIL;
|
||||
#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
|
||||
if (!TryEnterCriticalSection(&mutex->mutex))
|
||||
return MUTEX_FAILURE;
|
||||
return ZAP_FAIL;
|
||||
#else
|
||||
if (pthread_mutex_trylock(&mutex->mutex))
|
||||
return MUTEX_FAILURE;
|
||||
if (pthread_zap_mutex_trylock(&mutex->mutex))
|
||||
return ZAP_FAIL;
|
||||
#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
|
||||
LeaveCriticalSection(&mutex->mutex);
|
||||
#else
|
||||
if (pthread_mutex_unlock(&mutex->mutex))
|
||||
return MUTEX_FAILURE;
|
||||
return ZAP_FAIL;
|
||||
#endif
|
||||
return MUTEX_SUCCESS;
|
||||
return ZAP_SUCCESS;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue