[vlc-commits] win32: use generic semaphore (Windows 8+)

Rémi Denis-Courmont git at videolan.org
Thu Jun 2 19:54:39 CEST 2016


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Thu Jun  2 20:46:50 2016 +0300| [5d4c72af36bafc1130ea607aa10dc3523bf63f2f] | committer: Rémi Denis-Courmont

win32: use generic semaphore (Windows 8+)

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=5d4c72af36bafc1130ea607aa10dc3523bf63f2f
---

 include/vlc_threads.h |    5 +++--
 src/misc/threads.c    |   11 ++++++-----
 src/win32/thread.c    |   32 ++++++++++++++++++++++++++------
 3 files changed, 35 insertions(+), 13 deletions(-)

diff --git a/include/vlc_threads.h b/include/vlc_threads.h
index 33f2a8a..33b9b53 100644
--- a/include/vlc_threads.h
+++ b/include/vlc_threads.h
@@ -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;
diff --git a/src/misc/threads.c b/src/misc/threads.c
index 5767120..ce3756c 100644
--- a/src/misc/threads.c
+++ b/src/misc/threads.c
@@ -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
diff --git a/src/win32/thread.c b/src/win32/thread.c
index bb9eee9..9e08c97 100644
--- a/src/win32/thread.c
+++ b/src/win32/thread.c
@@ -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



More information about the vlc-commits mailing list