1 /*****************************************************************************
2 * thread.c : Win32 back-end for LibVLC
3 *****************************************************************************
4 * Copyright (C) 1999-2009 the VideoLAN team
6 * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
7 * Samuel Hocevar <sam@zoy.org>
8 * Gildas Bazin <gbazin@netcourrier.com>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 *****************************************************************************/
32 #include <vlc_common.h>
40 # include <mmsystem.h>
43 static vlc_threadvar_t thread_key;
58 vlc_cleanup_t *cleaners;
60 void *(*entry) (void *);
64 static CRITICAL_SECTION super_mutex;
65 static HANDLE super_cond;
66 extern vlc_rwlock_t config_lock, msg_lock;
68 BOOL WINAPI DllMain (HINSTANCE, DWORD, LPVOID);
70 BOOL WINAPI DllMain (HINSTANCE hinstDll, DWORD fdwReason, LPVOID lpvReserved)
77 case DLL_PROCESS_ATTACH:
78 super_cond = CreateEvent (NULL, TRUE, FALSE, NULL);
79 if (unlikely(!super_cond))
81 InitializeCriticalSection (&super_mutex);
82 vlc_threadvar_create (&thread_key, NULL);
83 vlc_rwlock_init (&config_lock);
84 vlc_rwlock_init (&msg_lock);
88 case DLL_PROCESS_DETACH:
89 vlc_rwlock_destroy (&msg_lock);
90 vlc_rwlock_destroy (&config_lock);
91 vlc_threadvar_delete (&thread_key);
92 DeleteCriticalSection (&super_mutex);
93 CloseHandle (super_cond);
99 static void CALLBACK vlc_cancel_self (ULONG_PTR);
101 static DWORD vlc_WaitForMultipleObjects (DWORD count, const HANDLE *handles,
106 HANDLE buf[count + 1];
108 struct vlc_thread *th = vlc_threadvar_get (thread_key);
111 memcpy (buf, handles, count * sizeof(HANDLE));
112 buf[count++] = th->cancel_event;
122 ret = WaitForMultipleObjects (count, handles, FALSE, delay);
124 if ((th != NULL) && (ret == WAIT_OBJECT_0 + count - 1))
126 vlc_cancel_self ((uintptr_t)th);
127 ret = WAIT_IO_COMPLETION;
132 ret = SleepEx (delay, TRUE);
137 ret = WaitForMultipleObjectsEx (count, handles, FALSE, delay, TRUE);
139 /* We do not abandon objects... this would be a bug */
140 assert (ret < WAIT_ABANDONED_0 || WAIT_ABANDONED_0 + count - 1 < ret);
142 if (unlikely(ret == WAIT_FAILED))
143 abort (); /* We are screwed! */
147 static DWORD vlc_WaitForSingleObject (HANDLE handle, DWORD delay)
149 return vlc_WaitForMultipleObjects (1, &handle, delay);
152 static DWORD vlc_Sleep (DWORD delay)
154 DWORD ret = vlc_WaitForMultipleObjects (0, NULL, delay);
155 return (ret != WAIT_TIMEOUT) ? ret : 0;
160 void vlc_mutex_init( vlc_mutex_t *p_mutex )
162 /* This creates a recursive mutex. This is OK as fast mutexes have
163 * no defined behavior in case of recursive locking. */
164 InitializeCriticalSection (&p_mutex->mutex);
165 p_mutex->dynamic = true;
168 void vlc_mutex_init_recursive( vlc_mutex_t *p_mutex )
170 InitializeCriticalSection( &p_mutex->mutex );
171 p_mutex->dynamic = true;
175 void vlc_mutex_destroy (vlc_mutex_t *p_mutex)
177 assert (p_mutex->dynamic);
178 DeleteCriticalSection (&p_mutex->mutex);
181 void vlc_mutex_lock (vlc_mutex_t *p_mutex)
183 if (!p_mutex->dynamic)
184 { /* static mutexes (inefficient on Windows) */
185 EnterCriticalSection (&super_mutex);
186 while (p_mutex->locked)
188 p_mutex->contention++;
189 LeaveCriticalSection (&super_mutex);
190 WaitForSingleObject (super_cond, INFINITE);
191 EnterCriticalSection (&super_mutex);
192 assert (p_mutex->contention > 0);
193 p_mutex->contention--;
195 p_mutex->locked = true;
196 LeaveCriticalSection (&super_mutex);
200 EnterCriticalSection (&p_mutex->mutex);
203 int vlc_mutex_trylock (vlc_mutex_t *p_mutex)
205 if (!p_mutex->dynamic)
206 { /* static mutexes */
209 EnterCriticalSection (&super_mutex);
210 if (!p_mutex->locked)
212 p_mutex->locked = true;
215 LeaveCriticalSection (&super_mutex);
219 return TryEnterCriticalSection (&p_mutex->mutex) ? 0 : EBUSY;
222 void vlc_mutex_unlock (vlc_mutex_t *p_mutex)
224 if (!p_mutex->dynamic)
225 { /* static mutexes */
226 EnterCriticalSection (&super_mutex);
227 assert (p_mutex->locked);
228 p_mutex->locked = false;
229 if (p_mutex->contention > 0)
230 SetEvent (super_cond);
231 LeaveCriticalSection (&super_mutex);
235 LeaveCriticalSection (&p_mutex->mutex);
238 /*** Condition variables ***/
241 CLOCK_REALTIME=0, /* must be zero for VLC_STATIC_COND */
245 static void vlc_cond_init_common (vlc_cond_t *p_condvar, unsigned clock)
247 /* Create a manual-reset event (manual reset is needed for broadcast). */
248 p_condvar->handle = CreateEvent (NULL, TRUE, FALSE, NULL);
249 if (!p_condvar->handle)
251 p_condvar->clock = clock;
254 void vlc_cond_init (vlc_cond_t *p_condvar)
256 vlc_cond_init_common (p_condvar, CLOCK_MONOTONIC);
259 void vlc_cond_init_daytime (vlc_cond_t *p_condvar)
261 vlc_cond_init_common (p_condvar, CLOCK_REALTIME);
264 void vlc_cond_destroy (vlc_cond_t *p_condvar)
266 CloseHandle (p_condvar->handle);
269 void vlc_cond_signal (vlc_cond_t *p_condvar)
271 if (!p_condvar->handle)
274 /* This is suboptimal but works. */
275 vlc_cond_broadcast (p_condvar);
278 void vlc_cond_broadcast (vlc_cond_t *p_condvar)
280 if (!p_condvar->handle)
283 /* Wake all threads up (as the event HANDLE has manual reset) */
284 SetEvent (p_condvar->handle);
287 void vlc_cond_wait (vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex)
291 if (!p_condvar->handle)
292 { /* FIXME FIXME FIXME */
300 vlc_mutex_unlock (p_mutex);
301 result = vlc_WaitForSingleObject (p_condvar->handle, INFINITE);
302 vlc_mutex_lock (p_mutex);
304 while (result == WAIT_IO_COMPLETION);
306 ResetEvent (p_condvar->handle);
309 int vlc_cond_timedwait (vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex,
314 if (!p_condvar->handle)
315 { /* FIXME FIXME FIXME */
325 switch (p_condvar->clock)
327 case CLOCK_REALTIME: /* FIXME? sub-second precision */
328 total = CLOCK_FREQ * time (NULL);
331 assert (p_condvar->clock == CLOCK_MONOTONIC);
335 total = (deadline - total) / 1000;
339 DWORD delay = (total > 0x7fffffff) ? 0x7fffffff : total;
340 vlc_mutex_unlock (p_mutex);
341 result = vlc_WaitForSingleObject (p_condvar->handle, delay);
342 vlc_mutex_lock (p_mutex);
344 while (result == WAIT_IO_COMPLETION);
346 ResetEvent (p_condvar->handle);
348 return (result == WAIT_OBJECT_0) ? 0 : ETIMEDOUT;
352 void vlc_sem_init (vlc_sem_t *sem, unsigned value)
354 *sem = CreateSemaphore (NULL, value, 0x7fffffff, NULL);
359 void vlc_sem_destroy (vlc_sem_t *sem)
364 int vlc_sem_post (vlc_sem_t *sem)
366 ReleaseSemaphore (*sem, 1, NULL);
367 return 0; /* FIXME */
370 void vlc_sem_wait (vlc_sem_t *sem)
377 result = vlc_WaitForSingleObject (*sem, INFINITE);
379 while (result == WAIT_IO_COMPLETION);
382 /*** Read/write locks */
383 /* SRW (Slim Read Write) locks are available in Vista+ only */
384 void vlc_rwlock_init (vlc_rwlock_t *lock)
386 vlc_mutex_init (&lock->mutex);
387 vlc_cond_init (&lock->wait);
388 lock->readers = 0; /* active readers */
389 lock->writers = 0; /* waiting writers */
390 lock->writer = 0; /* ID of active writer */
393 void vlc_rwlock_destroy (vlc_rwlock_t *lock)
395 vlc_cond_destroy (&lock->wait);
396 vlc_mutex_destroy (&lock->mutex);
399 void vlc_rwlock_rdlock (vlc_rwlock_t *lock)
401 vlc_mutex_lock (&lock->mutex);
402 /* Recursive read-locking is allowed. With the infos available:
403 * - the loosest possible condition (no active writer) is:
404 * (lock->writer != 0)
405 * - the strictest possible condition is:
406 * (lock->writer != 0 || (lock->readers == 0 && lock->writers > 0))
407 * or (lock->readers == 0 && (lock->writer != 0 || lock->writers > 0))
409 while (lock->writer != 0)
411 assert (lock->readers == 0);
412 vlc_cond_wait (&lock->wait, &lock->mutex);
414 if (unlikely(lock->readers == ULONG_MAX))
417 vlc_mutex_unlock (&lock->mutex);
420 static void vlc_rwlock_rdunlock (vlc_rwlock_t *lock)
422 vlc_mutex_lock (&lock->mutex);
423 assert (lock->readers > 0);
425 /* If there are no readers left, wake up a writer. */
426 if (--lock->readers == 0 && lock->writers > 0)
427 vlc_cond_signal (&lock->wait);
428 vlc_mutex_unlock (&lock->mutex);
431 void vlc_rwlock_wrlock (vlc_rwlock_t *lock)
433 vlc_mutex_lock (&lock->mutex);
434 if (unlikely(lock->writers == ULONG_MAX))
437 /* Wait until nobody owns the lock in either way. */
438 while ((lock->readers > 0) || (lock->writer != 0))
439 vlc_cond_wait (&lock->wait, &lock->mutex);
441 assert (lock->writer == 0);
442 lock->writer = GetCurrentThreadId ();
443 vlc_mutex_unlock (&lock->mutex);
446 static void vlc_rwlock_wrunlock (vlc_rwlock_t *lock)
448 vlc_mutex_lock (&lock->mutex);
449 assert (lock->writer == GetCurrentThreadId ());
450 assert (lock->readers == 0);
451 lock->writer = 0; /* Write unlock */
453 /* Let reader and writer compete. Scheduler decides who wins. */
454 vlc_cond_broadcast (&lock->wait);
455 vlc_mutex_unlock (&lock->mutex);
458 void vlc_rwlock_unlock (vlc_rwlock_t *lock)
460 /* Note: If the lock is held for reading, lock->writer is nul.
461 * If the lock is held for writing, only this thread can store a value to
462 * lock->writer. Either way, lock->writer is safe to fetch here. */
463 if (lock->writer != 0)
464 vlc_rwlock_wrunlock (lock);
466 vlc_rwlock_rdunlock (lock);
469 /*** Thread-specific variables (TLS) ***/
473 void (*destroy) (void *);
474 struct vlc_threadvar *prev;
475 struct vlc_threadvar *next;
476 } *vlc_threadvar_last = NULL;
478 int vlc_threadvar_create (vlc_threadvar_t *p_tls, void (*destr) (void *))
480 struct vlc_threadvar *var = malloc (sizeof (*var));
481 if (unlikely(var == NULL))
484 var->id = TlsAlloc();
485 if (var->id == TLS_OUT_OF_INDEXES)
490 var->destroy = destr;
494 EnterCriticalSection (&super_mutex);
495 var->prev = vlc_threadvar_last;
496 vlc_threadvar_last = var;
497 LeaveCriticalSection (&super_mutex);
501 void vlc_threadvar_delete (vlc_threadvar_t *p_tls)
503 struct vlc_threadvar *var = *p_tls;
505 EnterCriticalSection (&super_mutex);
506 if (var->prev != NULL)
507 var->prev->next = var->next;
509 vlc_threadvar_last = var->next;
510 if (var->next != NULL)
511 var->next->prev = var->prev;
512 LeaveCriticalSection (&super_mutex);
518 int vlc_threadvar_set (vlc_threadvar_t key, void *value)
520 return TlsSetValue (key->id, value) ? ENOMEM : 0;
523 void *vlc_threadvar_get (vlc_threadvar_t key)
525 return TlsGetValue (key->id);
529 void vlc_threads_setup (libvlc_int_t *p_libvlc)
534 static void vlc_thread_cleanup (struct vlc_thread *th)
539 /* TODO: use RW lock or something similar */
540 EnterCriticalSection (&super_mutex);
541 for (key = vlc_threadvar_last; key != NULL; key = key->prev)
543 void *value = vlc_threadvar_get (key);
544 if (value != NULL && key->destroy != NULL)
546 EnterCriticalSection (&super_mutex);
547 vlc_threadvar_set (key, NULL);
548 key->destroy (value);
552 EnterCriticalSection (&super_mutex);
556 CloseHandle (th->id);
558 CloseHandle (th->cancel_event);
564 static unsigned __stdcall vlc_entry (void *p)
566 struct vlc_thread *th = p;
568 vlc_threadvar_set (thread_key, th);
570 th->data = th->entry (th->data);
571 vlc_thread_cleanup (th);
575 static int vlc_clone_attr (vlc_thread_t *p_handle, bool detached,
576 void *(*entry) (void *), void *data, int priority)
578 struct vlc_thread *th = malloc (sizeof (*th));
579 if (unlikely(th == NULL))
583 th->detached = detached;
584 th->killable = false; /* not until vlc_entry() ! */
590 /* When using the MSVCRT C library you have to use the _beginthreadex
591 * function instead of CreateThread, otherwise you'll end up with
592 * memory leaks and the signal functions not working (see Microsoft
593 * Knowledge Base, article 104641) */
596 h = _beginthreadex (NULL, 0, vlc_entry, th, CREATE_SUSPENDED, NULL);
606 th->cancel_event = CreateEvent (NULL, FALSE, FALSE, NULL);
607 if (th->cancel_event == NULL)
613 /* Not sure if CREATE_SUSPENDED + ResumeThread() is any useful on WinCE.
614 * Thread handles act up, too. */
615 hThread = CreateThread (NULL, 128*1024, vlc_entry, th,
616 CREATE_SUSPENDED, NULL);
619 CloseHandle (th->cancel_event);
625 /* Thread is suspended, so we can safely set th->id */
627 if (p_handle != NULL)
631 SetThreadPriority (hThread, priority);
633 ResumeThread (hThread);
638 int vlc_clone (vlc_thread_t *p_handle, void *(*entry) (void *),
639 void *data, int priority)
641 return vlc_clone_attr (p_handle, false, entry, data, priority);
644 void vlc_join (vlc_thread_t th, void **result)
648 while (vlc_WaitForSingleObject (th->id, INFINITE)
649 == WAIT_IO_COMPLETION);
653 CloseHandle (th->id);
655 CloseHandle (th->cancel_event);
660 int vlc_clone_detach (vlc_thread_t *p_handle, void *(*entry) (void *),
661 void *data, int priority)
664 if (p_handle == NULL)
667 return vlc_clone_attr (p_handle, true, entry, data, priority);
670 int vlc_set_priority (vlc_thread_t th, int priority)
672 if (!SetThreadPriority (th->id, priority))
677 /*** Thread cancellation ***/
679 /* APC procedure for thread cancellation */
680 static void CALLBACK vlc_cancel_self (ULONG_PTR self)
682 struct vlc_thread *th = (void *)self;
684 if (likely(th != NULL))
688 void vlc_cancel (vlc_thread_t th)
691 QueueUserAPC (vlc_cancel_self, th->id, (uintptr_t)th);
693 SetEvent (th->cancel_event);
697 int vlc_savecancel (void)
699 struct vlc_thread *th = vlc_threadvar_get (thread_key);
701 return false; /* Main thread - cannot be cancelled anyway */
703 int state = th->killable;
704 th->killable = false;
708 void vlc_restorecancel (int state)
710 struct vlc_thread *th = vlc_threadvar_get (thread_key);
711 assert (state == false || state == true);
714 return; /* Main thread - cannot be cancelled anyway */
716 assert (!th->killable);
717 th->killable = state != 0;
720 void vlc_testcancel (void)
722 struct vlc_thread *th = vlc_threadvar_get (thread_key);
724 return; /* Main thread - cannot be cancelled anyway */
726 if (th->killable && th->killed)
728 for (vlc_cleanup_t *p = th->cleaners; p != NULL; p = p->next)
731 th->data = NULL; /* TODO: special value? */
732 vlc_thread_cleanup (th);
741 void vlc_control_cancel (int cmd, ...)
743 /* NOTE: This function only modifies thread-specific data, so there is no
744 * need to lock anything. */
747 struct vlc_thread *th = vlc_threadvar_get (thread_key);
749 return; /* Main thread - cannot be cancelled anyway */
754 case VLC_CLEANUP_PUSH:
756 /* cleaner is a pointer to the caller stack, no need to allocate
757 * and copy anything. As a nice side effect, this cannot fail. */
758 vlc_cleanup_t *cleaner = va_arg (ap, vlc_cleanup_t *);
759 cleaner->next = th->cleaners;
760 th->cleaners = cleaner;
764 case VLC_CLEANUP_POP:
766 th->cleaners = th->cleaners->next;
777 /* We don't need the real date, just the value of a high precision timer */
778 LARGE_INTEGER counter, freq;
779 if (!QueryPerformanceCounter (&counter)
780 || !QueryPerformanceFrequency (&freq))
783 /* Convert to from (1/freq) to microsecond resolution */
784 /* We need to split the division to avoid 63-bits overflow */
785 lldiv_t d = lldiv (counter.QuadPart, freq.QuadPart);
787 return (d.quot * 1000000) + ((d.rem * 1000000) / freq.QuadPart);
791 void mwait (mtime_t deadline)
796 while ((delay = (deadline - mdate())) > 0)
799 if (unlikely(delay > 0x7fffffff))
807 void msleep (mtime_t delay)
809 mwait (mdate () + delay);
822 void (*func) (void *);
827 static void CALLBACK vlc_timer_do (void *val, BOOLEAN timeout)
829 struct vlc_timer *timer = val;
832 timer->func (timer->data);
835 static void CALLBACK vlc_timer_do (unsigned timer_id, unsigned msg,
836 DWORD_PTR user, DWORD_PTR unused1,
839 struct vlc_timer *timer = (struct vlc_timer *) user;
840 assert (timer_id == timer->id);
845 timer->func (timer->data);
849 mtime_t interval = timer->interval * 1000;
850 vlc_timer_schedule (timer, false, interval, interval);
855 int vlc_timer_create (vlc_timer_t *id, void (*func) (void *), void *data)
857 struct vlc_timer *timer = malloc (sizeof (*timer));
864 timer->handle = INVALID_HANDLE_VALUE;
873 void vlc_timer_destroy (vlc_timer_t timer)
876 if (timer->handle != INVALID_HANDLE_VALUE)
877 DeleteTimerQueueTimer (NULL, timer->handle, INVALID_HANDLE_VALUE);
880 timeKillEvent (timer->id);
881 /* FIXME: timers that have not yet completed will trigger use-after-free */
886 void vlc_timer_schedule (vlc_timer_t timer, bool absolute,
887 mtime_t value, mtime_t interval)
890 if (timer->handle != INVALID_HANDLE_VALUE)
892 DeleteTimerQueueTimer (NULL, timer->handle, NULL);
893 timer->handle = INVALID_HANDLE_VALUE;
898 timeKillEvent (timer->id);
908 value = (value + 999) / 1000;
909 interval = (interval + 999) / 1000;
912 if (!CreateTimerQueueTimer (&timer->handle, NULL, vlc_timer_do, timer,
913 value, interval, WT_EXECUTEDEFAULT))
916 timeGetDevCaps (&caps, sizeof(caps));
918 unsigned delay = value;
919 delay = __MAX(delay, caps.wPeriodMin);
920 delay = __MIN(delay, caps.wPeriodMax);
922 unsigned event = TIME_ONESHOT;
924 if (interval == delay)
925 event = TIME_PERIODIC;
927 timer->interval = interval;
929 timer->id = timeSetEvent (delay, delay / 20, vlc_timer_do, (DWORD) timer,
936 unsigned vlc_timer_getoverrun (vlc_timer_t timer)
944 unsigned vlc_GetCPUCount (void)
950 if (GetProcessAffinityMask (GetCurrentProcess(), &process, &system))
951 return popcount (system);