First draft of thread abstraction.
This commit is contained in:
@@ -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("""
|
||||
|
||||
114
src/thread.c
Normal file
114
src/thread.c
Normal file
@@ -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 <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <pthread.h>
|
||||
#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;
|
||||
}
|
||||
39
src/thread.h
Normal file
39
src/thread.h
Normal file
@@ -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 <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <pthread.h>
|
||||
#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__ */
|
||||
Reference in New Issue
Block a user