1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
/*
* Pthreads compat.
*
* Copyright (c) 2007-2009 Marko Kreen
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <usual/pthread.h>
#ifndef HAVE_PTHREAD_H
#ifdef WIN32
/*
* basic pthreads for win32.
*/
struct _w32thread {
void *(*fn)(void *);
void *arg;
};
static DWORD WINAPI w32launcher(LPVOID arg)
{
struct _w32thread *info = arg;
info->fn(info->arg);
free(info);
return 0;
}
int pthread_create(pthread_t *t, pthread_attr_t *attr, void *(*fn)(void *), void *arg)
{
struct _w32thread *info = calloc(1, sizeof(*info));
if (!info)
return -1;
info->fn = fn;
info->arg = arg;
*t = CreateThread(NULL, 0, w32launcher, info, 0, NULL);
if (*t == NULL)
return -1;
return 0;
}
int pthread_join(pthread_t *t, void **ret)
{
if (WaitForSingleObject(*t, INFINITE) != WAIT_OBJECT_0)
return -1;
CloseHandle(*t);
return 0;
}
int pthread_mutex_init(pthread_mutex_t *lock, void *unused)
{
*lock = CreateMutex(NULL, FALSE, NULL);
if (*lock == NULL)
return -1;
return 0;
}
int pthread_mutex_destroy(pthread_mutex_t *lock)
{
if (*lock) {
CloseHandle(*lock);
*lock = NULL;
}
return 0;
}
int pthread_mutex_lock(pthread_mutex_t *lock)
{
if (WaitForSingleObject(*lock, INFINITE) != WAIT_OBJECT_0)
return -1;
return 0;
}
int pthread_mutex_unlock(pthread_mutex_t *lock)
{
if (!ReleaseMutex(*lock))
return -1;
return 0;
}
#ifdef INIT_ONCE_STATIC_INIT
typedef void (*once_posix_cb_t)(void);
static BOOL once_wrapper(PINIT_ONCE once, void *arg, void **ctx)
{
once_posix_cb_t cb = arg;
arg();
return TRUE;
}
int pthread_once(pthread_once_t *once, void (*once_func)(void))
{
return InitOnceExecuteOnce(once, once_wrapper, once_func, NULL) ? 0 : -1;
}
#endif
#endif /* win32 */
#endif /* !HAVE_PTHREAD_H */
|