initial check in of cross platform thread and mutex abstraction (just recursive mutexes so far).
git-svn-id: http://svn.openzap.org/svn/openzap/trunk@55 a93c3328-9c30-0410-af19-c9cd2b2d52af
This commit is contained in:
parent
16811e4358
commit
ff24de2bb8
|
@ -204,6 +204,10 @@
|
|||
/>
|
||||
</FileConfiguration>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\zap_threadmutex.c"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\zap_wanpipe.c"
|
||||
>
|
||||
|
@ -294,6 +298,10 @@
|
|||
RelativePath=".\src\include\zap_skel.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\include\zap_threadmutex.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\include\zap_wanpipe.h"
|
||||
>
|
||||
|
|
|
@ -30,7 +30,7 @@
|
|||
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
|
||||
OBJS=hashtable.o hashtable_itr.o openzap.o zap_config.o g711.o libteletone_detect.o libteletone_generate.o zap_buffer.o
|
||||
OBJS=hashtable.o hashtable_itr.o openzap.o zap_config.o g711.o libteletone_detect.o libteletone_generate.o zap_buffer.o zap_threadmutex.o
|
||||
CFLAGS=$(ZAP_CFLAGS) -Iinclude
|
||||
MYLIB=libopenzap.a
|
||||
TMP=-I../../libpri-1.2.4 -Iinclude -I. -w
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _ZAP_THREADMUTEX_H
|
||||
#define _ZAP_THREADMUTEX_H
|
||||
|
||||
typedef struct mutex mutex_t;
|
||||
|
||||
typedef enum mutex_status {
|
||||
MUTEX_SUCCESS,
|
||||
MUTEX_FAILURE
|
||||
} mutex_status_t;
|
||||
|
||||
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);
|
||||
|
||||
#endif
|
|
@ -0,0 +1,124 @@
|
|||
/*
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#define _XOPEN_SOURCE 600
|
||||
#include <stdlib.h>
|
||||
#include "zap_threadmutex.h"
|
||||
|
||||
|
||||
#ifdef WIN32
|
||||
#define _WIN32_WINNT 0x0400
|
||||
#include <windows.h>
|
||||
struct mutex {
|
||||
CRITICAL_SECTION mutex;
|
||||
};
|
||||
|
||||
#else
|
||||
|
||||
#include <pthread.h>
|
||||
struct mutex {
|
||||
pthread_mutex_t mutex;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
mutex_status_t zap_mutex_create(mutex_t **mutex)
|
||||
{
|
||||
mutex_status_t status = MUTEX_FAILURE;
|
||||
#ifndef WIN32
|
||||
pthread_mutexattr_t attr;
|
||||
#endif
|
||||
mutex_t *check = NULL;
|
||||
|
||||
check = (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 = MUTEX_SUCCESS;
|
||||
|
||||
done:
|
||||
return status;
|
||||
}
|
||||
|
||||
mutex_status_t zap_mutex_destroy(mutex_t *mutex)
|
||||
{
|
||||
#ifdef WIN32
|
||||
DeleteCriticalSection(&mutex->mutex);
|
||||
#else
|
||||
if (pthread_mutex_destroy(&mutex->mutex))
|
||||
return MUTEX_FAILURE;
|
||||
#endif
|
||||
free(mutex);
|
||||
return MUTEX_SUCCESS;
|
||||
}
|
||||
|
||||
mutex_status_t zap_mutex_lock(mutex_t *mutex)
|
||||
{
|
||||
#ifdef WIN32
|
||||
EnterCriticalSection(&mutex->mutex);
|
||||
#else
|
||||
if (pthread_mutex_lock(&mutex->mutex))
|
||||
return MUTEX_FAILURE;
|
||||
#endif
|
||||
return MUTEX_SUCCESS;
|
||||
}
|
||||
|
||||
mutex_status_t zap_mutex_trylock(mutex_t *mutex)
|
||||
{
|
||||
#ifdef WIN32
|
||||
if (!TryEnterCriticalSection(&mutex->mutex))
|
||||
return MUTEX_FAILURE;
|
||||
#else
|
||||
if (pthread_mutex_trylock(&mutex->mutex))
|
||||
return MUTEX_FAILURE;
|
||||
#endif
|
||||
return MUTEX_SUCCESS;
|
||||
}
|
||||
|
||||
mutex_status_t zap_mutex_unlock(mutex_t *mutex)
|
||||
{
|
||||
#ifdef WIN32
|
||||
LeaveCriticalSection(&mutex->mutex);
|
||||
#else
|
||||
if (pthread_mutex_unlock(&mutex->mutex))
|
||||
return MUTEX_FAILURE;
|
||||
#endif
|
||||
return MUTEX_SUCCESS;
|
||||
}
|
Loading…
Reference in New Issue