Commit 5d4c72af authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont
Browse files

win32: use generic semaphore (Windows 8+)

parent c554ca3e
......@@ -53,7 +53,8 @@ VLC_API void vlc_testcancel(void);
# endif
typedef struct vlc_thread *vlc_thread_t;
#define VLC_THREAD_CANCELED NULL
# define VLC_THREAD_CANCELED NULL
# define LIBVLC_NEED_SLEEP
typedef struct
{
bool dynamic;
......@@ -69,7 +70,7 @@ typedef struct
} vlc_mutex_t;
#define VLC_STATIC_MUTEX { false, { { false, 0 } } }
#define LIBVLC_NEED_CONDVAR
typedef HANDLE vlc_sem_t;
#define LIBVLC_NEED_SEMAPHORE
#define LIBVLC_NEED_RWLOCK
typedef struct vlc_threadvar *vlc_threadvar_t;
typedef struct vlc_timer *vlc_timer_t;
......
......@@ -49,6 +49,12 @@ void vlc_global_mutex (unsigned n, bool acquire)
vlc_mutex_unlock (lock);
}
#if defined (_WIN32) && (_WIN32_WINNT < _WIN32_WINNT_WIN8)
/* Cannot define OS version-dependent stuff in public headers */
# undef LIBVLC_NEED_SLEEP
# undef LIBVLC_NEED_SEMAPHORE
#endif
#if defined(LIBVLC_NEED_SLEEP) || defined(LIBVLC_NEED_CONDVAR)
#include <vlc_atomic.h>
......@@ -68,11 +74,6 @@ static void vlc_cancel_addr_finish(void *addr)
/* Act on cancellation as potential wake-up source */
vlc_testcancel();
}
# if defined (_WIN32) && (_WIN32_WINNT >= _WIN32_WINNT_WIN8)
/* Cannot define OS version-dependent stuff in public headers */
# define LIBVLC_NEED_SLEEP
# endif
#endif
#ifdef LIBVLC_NEED_SLEEP
......
......@@ -182,35 +182,55 @@ void vlc_mutex_unlock (vlc_mutex_t *p_mutex)
}
/*** Semaphore ***/
#if (_WIN32_WINNT < _WIN32_WINNT_WIN8)
# include <stdalign.h>
static inline HANDLE *vlc_sem_handle_p(vlc_sem_t *sem)
{
/* NOTE: vlc_sem_t layout cannot easily depend on Windows version */
static_assert (sizeof (HANDLE) <= sizeof (vlc_sem_t), "Size mismatch!");
static_assert ((alignof (HANDLE) % alignof (vlc_sem_t)) == 0,
"Alignment mismatch");
return (HANDLE *)sem;
}
#define vlc_sem_handle(sem) (*vlc_sem_handle_p(sem))
void vlc_sem_init (vlc_sem_t *sem, unsigned value)
{
*sem = CreateSemaphore (NULL, value, 0x7fffffff, NULL);
if (*sem == NULL)
HANDLE handle = CreateSemaphore(NULL, value, 0x7fffffff, NULL);
if (handle == NULL)
abort ();
vlc_sem_handle(sem) = handle;
}
void vlc_sem_destroy (vlc_sem_t *sem)
{
CloseHandle (*sem);
CloseHandle(vlc_sem_handle(sem));
}
int vlc_sem_post (vlc_sem_t *sem)
{
ReleaseSemaphore (*sem, 1, NULL);
ReleaseSemaphore(vlc_sem_handle(sem), 1, NULL);
return 0; /* FIXME */
}
void vlc_sem_wait (vlc_sem_t *sem)
{
HANDLE handle = vlc_sem_handle(sem);
DWORD result;
do
{
vlc_testcancel ();
result = vlc_WaitForSingleObject (*sem, INFINITE);
result = WaitForSingleObjectEx(handle, INFINITE, TRUE);
/* Semaphore abandoned would be a bug. */
assert(result != WAIT_ABANDONED_0);
}
while (result == WAIT_IO_COMPLETION);
while (result == WAIT_IO_COMPLETION || result == WAIT_FAILED);
}
#endif
/*** Thread-specific variables (TLS) ***/
struct vlc_threadvar
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment