From 733f720ee46d74aba47214dabc281a5e901bf883 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B3zef=20Kucia?= Date: Wed, 5 Oct 2016 15:56:27 +0200 Subject: [PATCH] libs/vkd3d: Add simple replacement for event objects. This naming convention for exported functions is used to be consistent with other D3D12* functions(). --- include/vkd3d_windows.h | 10 ++++ libs/vkd3d/vkd3d_main.c | 108 +++++++++++++++++++++++++++++++++++++ libs/vkd3d/vkd3d_private.h | 7 +++ 3 files changed, 125 insertions(+) diff --git a/include/vkd3d_windows.h b/include/vkd3d_windows.h index 855b3240..7376dcbe 100644 --- a/include/vkd3d_windows.h +++ b/include/vkd3d_windows.h @@ -171,6 +171,16 @@ typedef struct SECURITY_ATTRIBUTES SECURITY_ATTRIBUTES; # define IsEqualGUID(guid1, guid2) (!memcmp(guid1, guid2, sizeof(GUID))) #endif +#define WAIT_OBJECT_0 (0) +#define WAIT_TIMEOUT (1) +#define WAIT_FAILED (~0u) +#define INFINITE (~0u) + +HANDLE WINAPI VKD3DCreateEvent(void); +BOOL WINAPI VKD3DSignalEvent(HANDLE event); +unsigned int WINAPI VKD3DWaitEvent(HANDLE event, unsigned int milliseconds); +void WINAPI VKD3DDestroyEvent(HANDLE event); + #elif !defined(__WIDL__) # include diff --git a/libs/vkd3d/vkd3d_main.c b/libs/vkd3d/vkd3d_main.c index 81c4eff8..373ed9a5 100644 --- a/libs/vkd3d/vkd3d_main.c +++ b/libs/vkd3d/vkd3d_main.c @@ -61,3 +61,111 @@ HRESULT WINAPI D3D12GetDebugInterface(REFIID riid, void **debug) return E_NOTIMPL; } + +/* Events */ +HANDLE WINAPI VKD3DCreateEvent(void) +{ + struct vkd3d_event *event; + int rc; + + TRACE(".\n"); + + if (!(event = vkd3d_malloc(sizeof(*event)))) + return NULL; + + if ((rc = pthread_mutex_init(&event->mutex, NULL))) + { + ERR("Failed to initialize mutex, error %d.\n", rc); + vkd3d_free(event); + return NULL; + } + if ((rc = pthread_cond_init(&event->cond, NULL))) + { + ERR("Failed to initialize condition variable, error %d.\n", rc); + pthread_mutex_destroy(&event->mutex); + vkd3d_free(event); + return NULL; + } + + event->is_signaled = FALSE; + + TRACE("Created event %p.\n", event); + + return event; +} + +unsigned int WINAPI VKD3DWaitEvent(HANDLE event, unsigned int milliseconds) +{ + struct vkd3d_event *impl = event; + int rc; + + TRACE("event %p, milliseconds %u.\n", event, milliseconds); + + if ((rc = pthread_mutex_lock(&impl->mutex))) + { + ERR("Failed to lock mutex, error %d.\n", rc); + return WAIT_FAILED; + } + + if (impl->is_signaled || !milliseconds) + { + BOOL is_signaled = impl->is_signaled; + impl->is_signaled = FALSE; + pthread_mutex_unlock(&impl->mutex); + return is_signaled ? WAIT_OBJECT_0 : WAIT_TIMEOUT; + } + + if (milliseconds == INFINITE) + { + do + { + if ((rc = pthread_cond_wait(&impl->cond, &impl->mutex))) + { + ERR("Failed to wait on condition variable, error %d.\n", rc); + pthread_mutex_unlock(&impl->mutex); + return WAIT_FAILED; + } + } while (!impl->is_signaled); + + impl->is_signaled = FALSE; + pthread_mutex_unlock(&impl->mutex); + return WAIT_OBJECT_0; + } + + pthread_mutex_unlock(&impl->mutex); + FIXME("Timed wait not implemented yet.\n"); + return WAIT_FAILED; +} + +BOOL WINAPI VKD3DSignalEvent(HANDLE event) +{ + struct vkd3d_event *impl = event; + int rc; + + TRACE("event %p.\n", event); + + if ((rc = pthread_mutex_lock(&impl->mutex))) + { + ERR("Failed to lock mutex, error %d.\n", rc); + return FALSE; + } + impl->is_signaled = TRUE; + pthread_cond_signal(&impl->cond); + pthread_mutex_unlock(&impl->mutex); + + return TRUE; +} + +void WINAPI VKD3DDestroyEvent(HANDLE event) +{ + struct vkd3d_event *impl = event; + int rc; + + TRACE("event %p.\n", event); + + if ((rc = pthread_mutex_destroy(&impl->mutex))) + ERR("Failed to destroy mutex, error %d.\n", rc); + if ((rc = pthread_cond_destroy(&impl->cond))) + ERR("Failed to destroy condition variable, error %d.\n", rc); + vkd3d_free(impl); +} diff --git a/libs/vkd3d/vkd3d_private.h b/libs/vkd3d/vkd3d_private.h index 78694e93..371ce781 100644 --- a/libs/vkd3d/vkd3d_private.h +++ b/libs/vkd3d/vkd3d_private.h @@ -46,6 +46,13 @@ struct vkd3d_instance struct vkd3d_vk_instance_procs vk_procs; }; +struct vkd3d_event +{ + pthread_mutex_t mutex; + pthread_cond_t cond; + BOOL is_signaled; +}; + /* ID3D12Fence */ struct d3d12_fence {