From bafb9b22715313ef0049630228a744d7f2c9363b Mon Sep 17 00:00:00 2001 From: wm4 Date: Thu, 1 Jan 2015 15:10:42 +0100 Subject: win32: add native wrappers for pthread functions Off by default, use --enable-win32-internal-pthreads . This probably still needs a lot more testing. It also won't work on Windows XP. --- osdep/win32/include/pthread.h | 82 +++++++++++++++++++++++++++++++++++++++++ osdep/win32/include/semaphore.h | 29 +++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 osdep/win32/include/pthread.h create mode 100644 osdep/win32/include/semaphore.h (limited to 'osdep/win32/include') diff --git a/osdep/win32/include/pthread.h b/osdep/win32/include/pthread.h new file mode 100644 index 0000000000..2e9436d80b --- /dev/null +++ b/osdep/win32/include/pthread.h @@ -0,0 +1,82 @@ +#ifndef MP_WRAP_PTHREAD_H_ +#define MP_WRAP_PTHREAD_H_ + +#include + +#include + +// Note: all pthread functions are mangled to make static linking easier. +#define pthread_once m_pthread_once +#define pthread_mutex_destroy m_pthread_mutex_destroy +#define pthread_mutex_init m_pthread_mutex_init +#define pthread_mutex_lock m_pthread_mutex_lock +#define pthread_mutex_unlock m_pthread_mutex_unlock +#define pthread_cond_timedwait m_pthread_cond_timedwait +#define pthread_cond_wait m_pthread_cond_wait +#define pthread_self m_pthread_self +#define pthread_exit m_pthread_exit +#define pthread_join m_pthread_join +#define pthread_detach m_pthread_detach +#define pthread_create m_pthread_create + +#define pthread_once_t INIT_ONCE +#define PTHREAD_ONCE_INIT INIT_ONCE_STATIC_INIT + +int pthread_once(pthread_once_t *once_control, void (*init_routine)(void)); + +typedef struct { + volatile LONG requires_init; + CRITICAL_SECTION cs; +} pthread_mutex_t; + +#define PTHREAD_MUTEX_INITIALIZER {1} + +#define pthread_mutexattr_t int +#define pthread_mutexattr_destroy(attr) (void)0 +#define pthread_mutexattr_init(attr) (*(attr) = 0) +#define pthread_mutexattr_settype(attr, type) (void)0 +// CRITICAL_SECTION is always recursive +#define PTHREAD_MUTEX_RECURSIVE 0 + +int pthread_mutex_destroy(pthread_mutex_t *mutex); +int pthread_mutex_init(pthread_mutex_t *restrict mutex, + const pthread_mutexattr_t *restrict attr); + +int pthread_mutex_lock(pthread_mutex_t *mutex); +int pthread_mutex_unlock(pthread_mutex_t *mutex); + +#define pthread_cond_t CONDITION_VARIABLE +#define pthread_condattr_t int + +#define PTHREAD_COND_INITIALIZER CONDITION_VARIABLE_INIT + +#define pthread_cond_init(cond, attr) InitializeConditionVariable(cond) +#define pthread_cond_destroy(c) (void)0 +#define pthread_cond_broadcast(cond) WakeAllConditionVariable(cond) +#define pthread_cond_signal(cond) WakeConditionVariable(cond) + +int pthread_cond_timedwait(pthread_cond_t *restrict cond, + pthread_mutex_t *restrict mutex, + const struct timespec *restrict abstime); +int pthread_cond_wait(pthread_cond_t *restrict cond, + pthread_mutex_t *restrict mutex); + +// Unusual, but allowed by POSIX. +typedef struct { + DWORD id; + struct m_thread_info *info; +} pthread_t; + +#define pthread_equal(a, b) ((a).id == (b).id) + +pthread_t pthread_self(void); +void pthread_exit(void *retval); +int pthread_join(pthread_t thread, void **retval); +int pthread_detach(pthread_t thread); + +#define pthread_attr_t int + +int pthread_create(pthread_t *thread, const pthread_attr_t *attr, + void *(*start_routine) (void *), void *arg); + +#endif diff --git a/osdep/win32/include/semaphore.h b/osdep/win32/include/semaphore.h new file mode 100644 index 0000000000..49670fc7a5 --- /dev/null +++ b/osdep/win32/include/semaphore.h @@ -0,0 +1,29 @@ +#ifndef MP_WRAP_SEMAPHORE_H_ +#define MP_WRAP_SEMAPHORE_H_ + +#include + +// See pthread.h for rationale. +#define sem_init m_sem_init +#define sem_destroy m_sem_destroy +#define sem_wait m_sem_wait +#define sem_trywait m_sem_trywait +#define sem_timedwait m_sem_timedwait +#define sem_post m_sem_post + +#define SEM_VALUE_MAX 100 + +typedef struct { + pthread_mutex_t lock; + pthread_cond_t wakeup; + unsigned int value; +} sem_t; + +int sem_init(sem_t *sem, int pshared, unsigned int value); +int sem_destroy(sem_t *sem); +int sem_wait(sem_t *sem); +int sem_trywait(sem_t *sem); +int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout); +int sem_post(sem_t *sem); + +#endif -- cgit v1.2.3