Index: src/threading.h ================================================================== --- src/threading.h +++ src/threading.h @@ -31,10 +31,14 @@ #elif defined(_WIN32) # include typedef HANDLE of_thread_t; typedef DWORD of_tlskey_t; typedef CRITICAL_SECTION of_mutex_t; +typedef struct { + HANDLE event; + int count; +} of_condition_t; #endif #if defined(OF_ATOMIC_OPS) # import "atomic.h" typedef volatile int of_spinlock_t; @@ -155,51 +159,80 @@ of_condition_new(of_condition_t *condition) { #if defined(OF_HAVE_PTHREADS) return (pthread_cond_init(condition, NULL) ? NO : YES); #elif defined(_WIN32) - // XXX + condition->count = 0; + + if ((condition->event = CreateEvent(NULL, FALSE, 0, NULL)) == NULL) + return NO; + + return YES; #endif } static OF_INLINE BOOL of_condition_wait(of_condition_t *condition, of_mutex_t *mutex) { #if defined(OF_HAVE_PTHREADS) return (pthread_cond_wait(condition, mutex) ? NO : YES); #elif defined(_WIN32) - // XXX + if (!of_mutex_unlock(mutex)) + return NO; + + of_atomic_inc_int(&condition->count); + + if (WaitForSingleObject(condition->event, INFINITE) != WAIT_OBJECT_0) { + of_mutex_lock(mutex); + return NO; + } + + of_atomic_dec_int(&condition->count); + + if (!of_mutex_lock(mutex)) + return NO; + + return YES; #endif } static OF_INLINE BOOL of_condition_signal(of_condition_t *condition) { #if defined(OF_HAVE_PTHREADS) return (pthread_cond_signal(condition) ? NO : YES); #elif defined(_WIN32) - // XXX + return (SetEvent(condition->event) ? YES : NO); #endif } static OF_INLINE BOOL of_condition_broadcast(of_condition_t *condition) { #if defined(OF_HAVE_PTHREADS) return (pthread_cond_broadcast(condition) ? NO : YES); #elif defined(_WIN32) - // XXX + size_t i; + + for (i = 0; i < condition->count; i++) + if (!SetEvent(condition->event)) + return NO; + + return YES; #endif } static OF_INLINE BOOL of_condition_free(of_condition_t *condition) { #if defined(OF_HAVE_PTHREADS) return (pthread_cond_destroy(condition) ? NO : YES); #elif defined(_WIN32) - // XXX + if (condition->count) + return NO; + + return (CloseHandle(condition->event) ? YES : NO); #endif } static OF_INLINE BOOL of_tlskey_new(of_tlskey_t *key)