diff --git a/SConstruct b/SConstruct index 4e947cc..691c9ba 100644 --- a/SConstruct +++ b/SConstruct @@ -45,6 +45,7 @@ Sources = Split(""" sha1.c md5.c util.c + thread.c """) Headers = Split(""" @@ -55,6 +56,7 @@ Headers = Split(""" sha1.h md5.h util.h + thread.h """) Examples = Split(""" diff --git a/src/thread.c b/src/thread.c new file mode 100644 index 0000000..38159a7 --- /dev/null +++ b/src/thread.c @@ -0,0 +1,114 @@ +/* thread.c +** XMPP client library -- thread abstraction +** +** Copyright (C) 2005 Online Game Group. All rights reserved. +** +** This software is provided AS-IS with no warranty, either express +** or implied. +** +** This software is distributed under license and may not be copied, +** modified or distributed except as expressly authorized under the +** terms of the license contained in the file LICENSE.txt in this +** distribution. +*/ + +#include +#include + +#ifdef _WIN32 +#include +#else +#include +#endif + +#include "strophe.h" +#include "common.h" +#include "thread.h" + +struct _mutex_t { + const xmpp_ctx_t *ctx; + +#ifdef _WIN32 + HANDLE mutex; +#else + pthread_mutex_t *mutex; +#endif +}; + +/* mutex functions */ + +mutex_t *mutex_create(const xmpp_ctx_t * ctx) +{ + mutex_t *mutex; + + mutex = xmpp_alloc(ctx, sizeof(mutex_t)); + if (mutex) { + mutex->ctx = ctx; +#ifdef _WIN32 + mutex->mutex = CreateMutex(NULL, FALSE, NULL); +#else + mutex->mutex = xmpp_alloc(ctx, sizeof(pthread_mutex_t)); + if (mutex->mutex) + if (pthread_mutex_init(mutex->mutex, NULL) != 0) { + xmpp_free(ctx, mutex->mutex); + mutex->mutex = NULL; + } +#endif + if (!mutex->mutex) { + xmpp_free(ctx, mutex); + mutex = NULL; + } + } + + return mutex; +} + +int mutex_destroy(mutex_t *mutex) +{ + int ret = 1; + const xmpp_ctx_t *ctx; + +#ifdef _WIN32 + if (mutex->mutex) + ret = CloseHandle(mutex->mutex); +#else + if (mutex->mutex) + ret = pthread_mutex_destroy(mutex->mutex) == 0; +#endif + ctx = mutex->ctx; + xmpp_free(ctx, mutex); + + return ret; +} + +int mutex_lock(mutex_t *mutex) +{ + int ret; + +#ifdef _WIN32 + ret = WaitForSingleObject(mutex->mutex, INFINITE) == 0; +#else + ret = pthread_mutex_lock(mutex->mutex) == 0; +#endif + + return ret; +} + +int mutex_trylock(mutex_t *mutex) +{ + /* TODO */ + return 0; +} + +int mutex_unlock(mutex_t *mutex) +{ + int ret; + +#ifdef _WIN32 + ret = ReleaseMutex(mutex->mutex); +#else + ret = pthread_mutex_unlock(mutex->mutex) == 0; +#endif + + return ret; +} diff --git a/src/thread.h b/src/thread.h new file mode 100644 index 0000000..8b5d938 --- /dev/null +++ b/src/thread.h @@ -0,0 +1,39 @@ +/* thread.h +** XMPP client library -- thread abstraction header +** +** Copyright (C) 2005 Online Game Group. All rights reserved. +** +** This software is provided AS-IS with no warranty, either express +** or implied. +** +** This software is distributed under license and may not be copied, +** modified or distributed except as expressly authorized under the +** terms of the license contained in the file LICENSE.txt in this +** distribution. +*/ + +#ifndef __LIBSTROPHE_THREAD_H__ +#define __LIBSTROPHE_THREAD_H__ + +#include +#include + +#ifdef _WIN32 +#include +#else +#include +#endif + +#include "strophe.h" + +typedef struct _mutex_t mutex_t; + +/* mutex functions */ + +mutex_t *mutex_create(const xmpp_ctx_t *ctx); +int mutex_destroy(mutex_t *mutex); +int mutex_lock(mutex_t *mutex); +int mutex_trylock(mutex_t *mutex); +int mutex_unlock(mutex_t *mutex); + +#endif /* __LIBSTROPHE_THREAD_H__ */