]> git.sesse.net Git - vlc/blobdiff - src/misc/w32thread.c
Implement thread semaphores
[vlc] / src / misc / w32thread.c
index 7c722c74ff0a3181fc577d477c467e18f8175c25..ee83a9f48fc92c93d6ab0033911cc7a2151f8b81 100644 (file)
@@ -9,6 +9,7 @@
  *          Gildas Bazin <gbazin@netcourrier.com>
  *          Clément Sténac
  *          Rémi Denis-Courmont
+ *          Pierre Ynard
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -35,6 +36,9 @@
 #include <stdarg.h>
 #include <assert.h>
 #include <limits.h>
+#ifdef UNDER_CE
+# include <mmsystem.h>
+#endif
 
 static vlc_threadvar_t cancel_key;
 
@@ -292,6 +296,37 @@ int vlc_cond_timedwait (vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex,
     return (result == WAIT_OBJECT_0) ? 0 : ETIMEDOUT;
 }
 
+/*** Semaphore ***/
+void vlc_sem_init (vlc_sem_t *sem, unsigned value)
+{
+    *sem = CreateSemaphore (NULL, value, 0x7fffffff, NULL);
+    if (*sem == NULL)
+        abort ();
+}
+
+void vlc_sem_destroy (vlc_sem_t *sem)
+{
+    CloseHandle (*sem);
+}
+
+int vlc_sem_post (vlc_sem_t *sem)
+{
+    ReleaseSemaphore (*sem, 1, NULL);
+    return 0; /* FIXME */
+}
+
+void vlc_sem_wait (vlc_sem_t *sem)
+{
+    DWORD result;
+
+    do
+    {
+        vlc_testcancel ();
+        result = WaitForSingleObjectEx (*sem, INFINITE, TRUE);
+    }
+    while (result == WAIT_IO_COMPLETION);
+}
+
 /*** Read/write locks */
 /* SRW (Slim Read Write) locks are available in Vista+ only */
 void vlc_rwlock_init (vlc_rwlock_t *lock)
@@ -320,7 +355,7 @@ void vlc_rwlock_destroy (vlc_rwlock_t *lock)
 void vlc_rwlock_rdlock (vlc_rwlock_t *lock)
 {
     vlc_mutex_lock (&lock->mutex);
-    while (lock->writers > 0) /* Favor writers to avoid starving */
+    while (lock->writer != 0)
         vlc_cond_wait (&lock->read_wait, &lock->mutex);
     if (lock->readers == ULONG_MAX)
         abort ();
@@ -339,6 +374,7 @@ void vlc_rwlock_wrlock (vlc_rwlock_t *lock)
     lock->writers++;
     while ((lock->readers > 0) || (lock->writer != 0))
         vlc_cond_wait (&lock->write_wait, &lock->mutex);
+    lock->writers--;
     lock->writer = GetCurrentThreadId ();
     vlc_mutex_unlock (&lock->mutex);
 }
@@ -352,11 +388,7 @@ void vlc_rwlock_unlock (vlc_rwlock_t *lock)
     if (lock->readers > 0)
         lock->readers--; /* Read unlock */
     else
-    {
         lock->writer = 0; /* Write unlock */
-        assert (lock->writers > 0);
-        lock->writers--;
-    }
 
     if (lock->writers > 0)
     {
@@ -372,6 +404,8 @@ void vlc_rwlock_unlock (vlc_rwlock_t *lock)
 int vlc_threadvar_create (vlc_threadvar_t *p_tls, void (*destr) (void *))
 {
 #warning FIXME: use destr() callback and stop leaking!
+    VLC_UNUSED( destr );
+
     *p_tls = TlsAlloc();
     return (*p_tls == TLS_OUT_OF_INDEXES) ? EAGAIN : 0;
 }
@@ -405,91 +439,140 @@ void *vlc_threadvar_get (vlc_threadvar_t key)
 
 
 /*** Threads ***/
-static unsigned __stdcall vlc_entry (void *data)
+void vlc_threads_setup (libvlc_int_t *p_libvlc)
+{
+    (void) p_libvlc;
+}
+
+struct vlc_entry_data
+{
+    void * (*func) (void *);
+    void *  data;
+#ifdef UNDER_CE
+    HANDLE  cancel_event;
+#endif
+};
+
+static unsigned __stdcall vlc_entry (void *p)
 {
     vlc_cancel_t cancel_data = VLC_CANCEL_INIT;
-    vlc_thread_t self = data;
+    struct vlc_entry_data data;
+
+    memcpy (&data, p, sizeof (data));
+    free (p);
+
 #ifdef UNDER_CE
-    cancel_data.cancel_event = self->cancel_event;
+    cancel_data.cancel_event = data.cancel_event;
 #endif
 
     vlc_threadvar_set (cancel_key, &cancel_data);
-    self->data = self->entry (self->data);
+    data.func (data.data);
     return 0;
 }
 
 int vlc_clone (vlc_thread_t *p_handle, void * (*entry) (void *), void *data,
                int priority)
 {
+    int err = ENOMEM;
+    HANDLE hThread;
+
+    struct vlc_entry_data *entry_data = malloc (sizeof (*entry_data));
+    if (entry_data == NULL)
+        return ENOMEM;
+    entry_data->func = entry;
+    entry_data->data = data;
+
+#ifndef UNDER_CE
     /* When using the MSVCRT C library you have to use the _beginthreadex
      * function instead of CreateThread, otherwise you'll end up with
      * memory leaks and the signal functions not working (see Microsoft
      * Knowledge Base, article 104641) */
-    HANDLE hThread;
-    vlc_thread_t th = malloc (sizeof (*th));
+    hThread = (HANDLE)(uintptr_t)
+        _beginthreadex (NULL, 0, vlc_entry, entry_data, CREATE_SUSPENDED, NULL);
+    if (! hThread)
+    {
+        err = errno;
+        goto error;
+    }
 
-    if (th == NULL)
-        return ENOMEM;
+    /* Thread closes the handle when exiting, duplicate it here
+     * to be on the safe side when joining. */
+    if (!DuplicateHandle (GetCurrentProcess (), hThread,
+                          GetCurrentProcess (), p_handle, 0, FALSE,
+                          DUPLICATE_SAME_ACCESS))
+    {
+        CloseHandle (hThread);
+        goto error;
+    }
 
-    th->data = data;
-    th->entry = entry;
-#if defined( UNDER_CE )
+#else
+    vlc_thread_t th = malloc (sizeof (*th));
+    if (th == NULL)
+        goto error;
     th->cancel_event = CreateEvent (NULL, FALSE, FALSE, NULL);
     if (th->cancel_event == NULL)
     {
-        free(th);
-        return errno;
+        free (th);
+        goto error;
     }
-    hThread = CreateThread (NULL, 128*1024, vlc_entry, th, CREATE_SUSPENDED, NULL);
-#else
-    hThread = (HANDLE)(uintptr_t)
-        _beginthreadex (NULL, 0, vlc_entry, th, CREATE_SUSPENDED, NULL);
-#endif
+    entry_data->cancel_event = th->cancel_event;
 
-    if (hThread)
+    /* Not sure if CREATE_SUSPENDED + ResumeThread() is any useful on WinCE.
+     * Thread handles act up, too. */
+    th->handle = CreateThread (NULL, 128*1024, vlc_entry, entry_data,
+                               CREATE_SUSPENDED, NULL);
+    if (th->handle == NULL)
     {
-#ifndef UNDER_CE
-        /* Thread closes the handle when exiting, duplicate it here
-         * to be on the safe side when joining. */
-        if (!DuplicateHandle (GetCurrentProcess (), hThread,
-                              GetCurrentProcess (), &th->handle, 0, FALSE,
-                              DUPLICATE_SAME_ACCESS))
-        {
-            CloseHandle (hThread);
-            free (th);
-            return ENOMEM;
-        }
-#else
-        th->handle = hThread;
+        CloseHandle (th->cancel_event);
+        free (th);
+        goto error;
+    }
+
+    *p_handle = th;
+    hThread = th->handle;
+
 #endif
 
-        ResumeThread (hThread);
-        if (priority)
-            SetThreadPriority (hThread, priority);
+    ResumeThread (hThread);
+    if (priority)
+        SetThreadPriority (hThread, priority);
 
-        *p_handle = th;
-        return 0;
-    }
-    free (th);
-    return errno;
+    return 0;
+
+error:
+    free (entry_data);
+    return err;
 }
 
 void vlc_join (vlc_thread_t handle, void **result)
 {
+#ifdef UNDER_CE
+# define handle handle->handle
+#endif
     do
         vlc_testcancel ();
-    while (WaitForSingleObjectEx (handle->handle, INFINITE, TRUE)
+    while (WaitForSingleObjectEx (handle, INFINITE, TRUE)
                                                         == WAIT_IO_COMPLETION);
 
-    CloseHandle (handle->handle);
-    if (result)
-        *result = handle->data;
+    CloseHandle (handle);
+    assert (result == NULL); /* <- FIXME if ever needed */
 #ifdef UNDER_CE
+# undef handle
     CloseHandle (handle->cancel_event);
-#endif
     free (handle);
+#endif
 }
 
+void vlc_detach (vlc_thread_t handle)
+{
+#ifndef UNDER_CE
+    CloseHandle (handle);
+#else
+    /* FIXME: handle->cancel_event leak */
+    CloseHandle (handle->handle);
+    free (handle);
+#endif
+}
 
 /*** Thread cancellation ***/
 
@@ -503,7 +586,7 @@ static void CALLBACK vlc_cancel_self (ULONG_PTR dummy)
 void vlc_cancel (vlc_thread_t thread_id)
 {
 #ifndef UNDER_CE
-    QueueUserAPC (vlc_cancel_self, thread_id->handle, 0);
+    QueueUserAPC (vlc_cancel_self, thread_id, 0);
 #else
     SetEvent (thread_id->cancel_event);
 #endif
@@ -590,46 +673,95 @@ void vlc_control_cancel (int cmd, ...)
 
 
 /*** Timers ***/
+struct vlc_timer
+{
+#ifndef UNDER_CE
+    HANDLE handle;
+#else
+    unsigned id;
+    unsigned interval;
+#endif
+    void (*func) (void *);
+    void *data;
+};
+
+#ifndef UNDER_CE
 static void CALLBACK vlc_timer_do (void *val, BOOLEAN timeout)
 {
-    vlc_timer_t *id = val;
+    struct vlc_timer *timer = val;
 
     assert (timeout);
-    if (TryEnterCriticalSection (&id->serializer))
+    timer->func (timer->data);
+}
+#else
+static void CALLBACK vlc_timer_do (unsigned timer_id, unsigned msg,
+                                   DWORD_PTR user, DWORD_PTR unused1,
+                                   DWORD_PTR unused2)
+{
+    struct vlc_timer *timer = (struct vlc_timer *) user;
+    assert (timer_id == timer->id);
+    (void) msg;
+    (void) unused1;
+    (void) unused2;
+
+    timer->func (timer->data);
+
+    if (timer->interval)
     {
-        id->overrun = InterlockedExchange (&id->counter, 0);
-        id->func (id->data);
-        LeaveCriticalSection (&id->serializer);
+        mtime_t interval = timer->interval * 1000;
+        vlc_timer_schedule (timer, false, interval, interval);
     }
-    else /* Overrun */
-        InterlockedIncrement (&id->counter);
 }
+#endif
 
 int vlc_timer_create (vlc_timer_t *id, void (*func) (void *), void *data)
 {
-    id->func = func;
-    id->data = data;
-    id->overrun = 0;
-    id->handle = INVALID_HANDLE_VALUE;
-    InitializeCriticalSection (&id->serializer);
+    struct vlc_timer *timer = malloc (sizeof (*timer));
+
+    if (timer == NULL)
+        return ENOMEM;
+    timer->func = func;
+    timer->data = data;
+#ifndef UNDER_CE
+    timer->handle = INVALID_HANDLE_VALUE;
+#else
+    timer->id = 0;
+    timer->interval = 0;
+#endif
+    *id = timer;
     return 0;
 }
 
-void vlc_timer_destroy (vlc_timer_t *id)
+void vlc_timer_destroy (vlc_timer_t timer)
 {
-    if (id->handle != INVALID_HANDLE_VALUE)
-        DeleteTimerQueueTimer (NULL, id->handle, NULL);
-    DeleteCriticalSection (&id->serializer);
+#ifndef UNDER_CE
+    if (timer->handle != INVALID_HANDLE_VALUE)
+        DeleteTimerQueueTimer (NULL, timer->handle, INVALID_HANDLE_VALUE);
+#else
+    if (timer->id)
+        timeKillEvent (timer->id);
+    /* FIXME: timers that have not yet completed will trigger use-after-free */
+#endif
+    free (timer);
 }
 
-void vlc_timer_schedule (vlc_timer_t *id, bool absolute,
+void vlc_timer_schedule (vlc_timer_t timer, bool absolute,
                          mtime_t value, mtime_t interval)
 {
-    if (id->handle != INVALID_HANDLE_VALUE)
+#ifndef UNDER_CE
+    if (timer->handle != INVALID_HANDLE_VALUE)
     {
-        DeleteTimerQueueTimer (NULL, id->handle, NULL);
-        id->handle = INVALID_HANDLE_VALUE;
+        DeleteTimerQueueTimer (NULL, timer->handle, NULL);
+        timer->handle = INVALID_HANDLE_VALUE;
     }
+#else
+    if (timer->id)
+    {
+        timeKillEvent (timer->id);
+        timer->id = 0;
+        timer->interval = 0;
+    }
+#endif
     if (value == 0)
         return; /* Disarm */
 
@@ -637,12 +769,34 @@ void vlc_timer_schedule (vlc_timer_t *id, bool absolute,
         value -= mdate ();
     value = (value + 999) / 1000;
     interval = (interval + 999) / 1000;
-    if (!CreateTimerQueueTimer (&id->handle, NULL, vlc_timer_do, id, value,
-                                interval, WT_EXECUTEDEFAULT))
+
+#ifndef UNDER_CE
+    if (!CreateTimerQueueTimer (&timer->handle, NULL, vlc_timer_do, timer,
+                                value, interval, WT_EXECUTEDEFAULT))
+#else
+    TIMECAPS caps;
+    timeGetDevCaps (&caps, sizeof(caps));
+
+    unsigned delay = value;
+    delay = __MAX(delay, caps.wPeriodMin);
+    delay = __MIN(delay, caps.wPeriodMax);
+
+    unsigned event = TIME_ONESHOT;
+
+    if (interval == delay)
+        event = TIME_PERIODIC;
+    else if (interval)
+        timer->interval = interval;
+
+    timer->id = timeSetEvent (delay, delay / 20, vlc_timer_do, (DWORD) timer,
+                              event);
+    if (!timer->id)
+#endif
         abort ();
 }
 
-unsigned vlc_timer_getoverrun (const vlc_timer_t *id)
+unsigned vlc_timer_getoverrun (vlc_timer_t timer)
 {
-    return id->overrun;
+    (void)timer;
+    return 0;
 }