]> git.sesse.net Git - vlc/blob - src/misc/w32thread.c
Win32: fix previous commit plus small optimization
[vlc] / src / misc / w32thread.c
1 /*****************************************************************************
2  * w32thread.c : Win32 back-end for LibVLC
3  *****************************************************************************
4  * Copyright (C) 1999-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          Gildas Bazin <gbazin@netcourrier.com>
10  *          Clément Sténac
11  *          Rémi Denis-Courmont
12  *          Pierre Ynard
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34
35 #include "libvlc.h"
36 #include <stdarg.h>
37 #include <assert.h>
38 #include <limits.h>
39 #ifdef UNDER_CE
40 # include <mmsystem.h>
41 #endif
42
43 static vlc_threadvar_t cancel_key;
44
45 /**
46  * Per-thread cancellation data
47  */
48 typedef struct vlc_cancel_t
49 {
50     vlc_cleanup_t *cleaners;
51 #ifdef UNDER_CE
52     HANDLE         cancel_event;
53 #endif
54     bool           killable;
55     bool           killed;
56 } vlc_cancel_t;
57
58 #ifndef UNDER_CE
59 # define VLC_CANCEL_INIT { NULL, true, false }
60 #else
61 # define VLC_CANCEL_INIT { NULL, NULL, true, false }
62 #endif
63
64 #ifdef UNDER_CE
65 static void CALLBACK vlc_cancel_self (ULONG_PTR dummy);
66
67 static DWORD vlc_cancelable_wait (DWORD count, const HANDLE *handles,
68                                   DWORD delay)
69 {
70     vlc_cancel_t *nfo = vlc_threadvar_get (cancel_key);
71     if (nfo == NULL)
72     {
73         /* Main thread - cannot be cancelled anyway */
74         return WaitForMultipleObjects (count, handles, FALSE, delay);
75     }
76     HANDLE new_handles[count + 1];
77     memcpy(new_handles, handles, count * sizeof(HANDLE));
78     new_handles[count] = nfo->cancel_event;
79     DWORD result = WaitForMultipleObjects (count + 1, new_handles, FALSE,
80                                            delay);
81     if (result == WAIT_OBJECT_0 + count)
82     {
83         vlc_cancel_self (NULL);
84         return WAIT_IO_COMPLETION;
85     }
86     else
87     {
88         return result;
89     }
90 }
91
92 DWORD SleepEx (DWORD dwMilliseconds, BOOL bAlertable)
93 {
94     if (bAlertable)
95     {
96         DWORD result = vlc_cancelable_wait (0, NULL, dwMilliseconds);
97         return (result == WAIT_TIMEOUT) ? 0 : WAIT_IO_COMPLETION;
98     }
99     else
100     {
101         Sleep(dwMilliseconds);
102         return 0;
103     }
104 }
105
106 DWORD WaitForSingleObjectEx (HANDLE hHandle, DWORD dwMilliseconds,
107                              BOOL bAlertable)
108 {
109     if (bAlertable)
110     {
111         /* The MSDN documentation specifies different return codes,
112          * but in practice they are the same. We just check that it
113          * remains so. */
114 #if WAIT_ABANDONED != WAIT_ABANDONED_0
115 # error Windows headers changed, code needs to be rewritten!
116 #endif
117         return vlc_cancelable_wait (1, &hHandle, dwMilliseconds);
118     }
119     else
120     {
121         return WaitForSingleObject (hHandle, dwMilliseconds);
122     }
123 }
124
125 DWORD WaitForMultipleObjectsEx (DWORD nCount, const HANDLE *lpHandles,
126                                 BOOL bWaitAll, DWORD dwMilliseconds,
127                                 BOOL bAlertable)
128 {
129     if (bAlertable)
130     {
131         /* We do not support the bWaitAll case */
132         assert (! bWaitAll);
133         return vlc_cancelable_wait (nCount, lpHandles, dwMilliseconds);
134     }
135     else
136     {
137         return WaitForMultipleObjects (nCount, lpHandles, bWaitAll,
138                                        dwMilliseconds);
139     }
140 }
141 #endif
142
143 static vlc_mutex_t super_mutex;
144 static vlc_cond_t  super_variable;
145
146 BOOL WINAPI DllMain (HINSTANCE hinstDll, DWORD fdwReason, LPVOID lpvReserved)
147 {
148     (void) hinstDll;
149     (void) lpvReserved;
150
151     switch (fdwReason)
152     {
153         case DLL_PROCESS_ATTACH:
154             vlc_mutex_init (&super_mutex);
155             vlc_cond_init (&super_variable);
156             vlc_threadvar_create (&cancel_key, free);
157             break;
158
159         case DLL_PROCESS_DETACH:
160             vlc_threadvar_delete( &cancel_key );
161             vlc_cond_destroy (&super_variable);
162             vlc_mutex_destroy (&super_mutex);
163             break;
164     }
165     return TRUE;
166 }
167
168 /*** Mutexes ***/
169 void vlc_mutex_init( vlc_mutex_t *p_mutex )
170 {
171     /* This creates a recursive mutex. This is OK as fast mutexes have
172      * no defined behavior in case of recursive locking. */
173     InitializeCriticalSection (&p_mutex->mutex);
174     p_mutex->dynamic = true;
175 }
176
177 void vlc_mutex_init_recursive( vlc_mutex_t *p_mutex )
178 {
179     InitializeCriticalSection( &p_mutex->mutex );
180     p_mutex->dynamic = true;
181 }
182
183
184 void vlc_mutex_destroy (vlc_mutex_t *p_mutex)
185 {
186     assert (p_mutex->dynamic);
187     DeleteCriticalSection (&p_mutex->mutex);
188 }
189
190 void vlc_mutex_lock (vlc_mutex_t *p_mutex)
191 {
192     if (!p_mutex->dynamic)
193     {   /* static mutexes */
194         assert (p_mutex != &super_mutex); /* this one cannot be static */
195
196         vlc_mutex_lock (&super_mutex);
197         while (p_mutex->locked)
198         {
199             p_mutex->contention++;
200             vlc_cond_wait (&super_variable, &super_mutex);
201             p_mutex->contention--;
202         }
203         p_mutex->locked = true;
204         vlc_mutex_unlock (&super_mutex);
205         return;
206     }
207
208     EnterCriticalSection (&p_mutex->mutex);
209 }
210
211 int vlc_mutex_trylock (vlc_mutex_t *p_mutex)
212 {
213     if (!p_mutex->dynamic)
214     {   /* static mutexes */
215         int ret = EBUSY;
216
217         assert (p_mutex != &super_mutex); /* this one cannot be static */
218         vlc_mutex_lock (&super_mutex);
219         if (!p_mutex->locked)
220         {
221             p_mutex->locked = true;
222             ret = 0;
223         }
224         vlc_mutex_unlock (&super_mutex);
225         return ret;
226     }
227
228     return TryEnterCriticalSection (&p_mutex->mutex) ? 0 : EBUSY;
229 }
230
231 void vlc_mutex_unlock (vlc_mutex_t *p_mutex)
232 {
233     if (!p_mutex->dynamic)
234     {   /* static mutexes */
235         assert (p_mutex != &super_mutex); /* this one cannot be static */
236
237         vlc_mutex_lock (&super_mutex);
238         assert (p_mutex->locked);
239         p_mutex->locked = false;
240         if (p_mutex->contention)
241             vlc_cond_broadcast (&super_variable);
242         vlc_mutex_unlock (&super_mutex);
243         return;
244     }
245
246     LeaveCriticalSection (&p_mutex->mutex);
247 }
248
249 /*** Condition variables ***/
250 void vlc_cond_init( vlc_cond_t *p_condvar )
251 {
252     /* Create a manual-reset event (manual reset is needed for broadcast). */
253     *p_condvar = CreateEvent (NULL, TRUE, FALSE, NULL);
254     if (!*p_condvar)
255         abort();
256 }
257
258 void vlc_cond_destroy (vlc_cond_t *p_condvar)
259 {
260     CloseHandle (*p_condvar);
261 }
262
263 void vlc_cond_signal (vlc_cond_t *p_condvar)
264 {
265     /* NOTE: This will cause a broadcast, that is wrong.
266      * This will also wake up the next waiting thread if no threads are yet
267      * waiting, which is also wrong. However both of these issues are allowed
268      * by the provision for spurious wakeups. Better have too many wakeups
269      * than too few (= deadlocks). */
270     SetEvent (*p_condvar);
271 }
272
273 void vlc_cond_broadcast (vlc_cond_t *p_condvar)
274 {
275     SetEvent (*p_condvar);
276 }
277
278 void vlc_cond_wait (vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex)
279 {
280     DWORD result;
281
282     do
283     {
284         vlc_testcancel ();
285         LeaveCriticalSection (&p_mutex->mutex);
286         result = WaitForSingleObjectEx (*p_condvar, INFINITE, TRUE);
287         EnterCriticalSection (&p_mutex->mutex);
288     }
289     while (result == WAIT_IO_COMPLETION);
290
291     assert (result != WAIT_ABANDONED); /* another thread failed to cleanup! */
292     assert (result != WAIT_FAILED);
293     ResetEvent (*p_condvar);
294 }
295
296 int vlc_cond_timedwait (vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex,
297                         mtime_t deadline)
298 {
299     DWORD result;
300
301     do
302     {
303         vlc_testcancel ();
304
305         mtime_t total = (deadline - mdate ())/1000;
306         if( total < 0 )
307             total = 0;
308
309         DWORD delay = (total > 0x7fffffff) ? 0x7fffffff : total;
310         LeaveCriticalSection (&p_mutex->mutex);
311         result = WaitForSingleObjectEx (*p_condvar, delay, TRUE);
312         EnterCriticalSection (&p_mutex->mutex);
313     }
314     while (result == WAIT_IO_COMPLETION);
315
316     assert (result != WAIT_ABANDONED);
317     assert (result != WAIT_FAILED);
318     ResetEvent (*p_condvar);
319
320     return (result == WAIT_OBJECT_0) ? 0 : ETIMEDOUT;
321 }
322
323 /*** Semaphore ***/
324 void vlc_sem_init (vlc_sem_t *sem, unsigned value)
325 {
326     *sem = CreateSemaphore (NULL, value, 0x7fffffff, NULL);
327     if (*sem == NULL)
328         abort ();
329 }
330
331 void vlc_sem_destroy (vlc_sem_t *sem)
332 {
333     CloseHandle (*sem);
334 }
335
336 int vlc_sem_post (vlc_sem_t *sem)
337 {
338     ReleaseSemaphore (*sem, 1, NULL);
339     return 0; /* FIXME */
340 }
341
342 void vlc_sem_wait (vlc_sem_t *sem)
343 {
344     DWORD result;
345
346     do
347     {
348         vlc_testcancel ();
349         result = WaitForSingleObjectEx (*sem, INFINITE, TRUE);
350     }
351     while (result == WAIT_IO_COMPLETION);
352 }
353
354 /*** Read/write locks */
355 /* SRW (Slim Read Write) locks are available in Vista+ only */
356 void vlc_rwlock_init (vlc_rwlock_t *lock)
357 {
358     vlc_mutex_init (&lock->mutex);
359     vlc_cond_init (&lock->read_wait);
360     vlc_cond_init (&lock->write_wait);
361     lock->readers = 0; /* active readers */
362     lock->writers = 0; /* waiting or active writers */
363     lock->writer = 0; /* ID of active writer */
364 }
365
366 /**
367  * Destroys an initialized unused read/write lock.
368  */
369 void vlc_rwlock_destroy (vlc_rwlock_t *lock)
370 {
371     vlc_cond_destroy (&lock->read_wait);
372     vlc_cond_destroy (&lock->write_wait);
373     vlc_mutex_destroy (&lock->mutex);
374 }
375
376 /**
377  * Acquires a read/write lock for reading. Recursion is allowed.
378  */
379 void vlc_rwlock_rdlock (vlc_rwlock_t *lock)
380 {
381     vlc_mutex_lock (&lock->mutex);
382     while (lock->writer != 0)
383         vlc_cond_wait (&lock->read_wait, &lock->mutex);
384     if (lock->readers == ULONG_MAX)
385         abort ();
386     lock->readers++;
387     vlc_mutex_unlock (&lock->mutex);
388 }
389
390 /**
391  * Acquires a read/write lock for writing. Recursion is not allowed.
392  */
393 void vlc_rwlock_wrlock (vlc_rwlock_t *lock)
394 {
395     vlc_mutex_lock (&lock->mutex);
396     if (lock->writers == ULONG_MAX)
397         abort ();
398     lock->writers++;
399     while ((lock->readers > 0) || (lock->writer != 0))
400         vlc_cond_wait (&lock->write_wait, &lock->mutex);
401     lock->writers--;
402     lock->writer = GetCurrentThreadId ();
403     vlc_mutex_unlock (&lock->mutex);
404 }
405
406 /**
407  * Releases a read/write lock.
408  */
409 void vlc_rwlock_unlock (vlc_rwlock_t *lock)
410 {
411     vlc_mutex_lock (&lock->mutex);
412     if (lock->readers > 0)
413         lock->readers--; /* Read unlock */
414     else
415         lock->writer = 0; /* Write unlock */
416
417     if (lock->writers > 0)
418     {
419         if (lock->readers == 0)
420             vlc_cond_signal (&lock->write_wait);
421     }
422     else
423         vlc_cond_broadcast (&lock->read_wait);
424     vlc_mutex_unlock (&lock->mutex);
425 }
426
427 /*** Thread-specific variables (TLS) ***/
428 int vlc_threadvar_create (vlc_threadvar_t *p_tls, void (*destr) (void *))
429 {
430 #warning FIXME: use destr() callback and stop leaking!
431     VLC_UNUSED( destr );
432
433     *p_tls = TlsAlloc();
434     return (*p_tls == TLS_OUT_OF_INDEXES) ? EAGAIN : 0;
435 }
436
437 void vlc_threadvar_delete (vlc_threadvar_t *p_tls)
438 {
439     TlsFree (*p_tls);
440 }
441
442 /**
443  * Sets a thread-local variable.
444  * @param key thread-local variable key (created with vlc_threadvar_create())
445  * @param value new value for the variable for the calling thread
446  * @return 0 on success, a system error code otherwise.
447  */
448 int vlc_threadvar_set (vlc_threadvar_t key, void *value)
449 {
450     return TlsSetValue (key, value) ? ENOMEM : 0;
451 }
452
453 /**
454  * Gets the value of a thread-local variable for the calling thread.
455  * This function cannot fail.
456  * @return the value associated with the given variable for the calling
457  * or NULL if there is no value.
458  */
459 void *vlc_threadvar_get (vlc_threadvar_t key)
460 {
461     return TlsGetValue (key);
462 }
463
464
465 /*** Threads ***/
466 void vlc_threads_setup (libvlc_int_t *p_libvlc)
467 {
468     (void) p_libvlc;
469 }
470
471 struct vlc_entry_data
472 {
473     void * (*func) (void *);
474     void *  data;
475 #ifdef UNDER_CE
476     HANDLE  cancel_event;
477 #endif
478 };
479
480 static unsigned __stdcall vlc_entry (void *p)
481 {
482     vlc_cancel_t cancel_data = VLC_CANCEL_INIT;
483     struct vlc_entry_data data;
484
485     memcpy (&data, p, sizeof (data));
486     free (p);
487
488 #ifdef UNDER_CE
489     cancel_data.cancel_event = data.cancel_event;
490 #endif
491
492     vlc_threadvar_set (cancel_key, &cancel_data);
493     data.func (data.data);
494     return 0;
495 }
496
497 int vlc_clone (vlc_thread_t *p_handle, void * (*entry) (void *), void *data,
498                int priority)
499 {
500     int err = ENOMEM;
501     HANDLE hThread;
502
503     struct vlc_entry_data *entry_data = malloc (sizeof (*entry_data));
504     if (entry_data == NULL)
505         return ENOMEM;
506     entry_data->func = entry;
507     entry_data->data = data;
508
509 #ifndef UNDER_CE
510     /* When using the MSVCRT C library you have to use the _beginthreadex
511      * function instead of CreateThread, otherwise you'll end up with
512      * memory leaks and the signal functions not working (see Microsoft
513      * Knowledge Base, article 104641) */
514     hThread = (HANDLE)(uintptr_t)
515         _beginthreadex (NULL, 0, vlc_entry, entry_data, CREATE_SUSPENDED, NULL);
516     if (! hThread)
517     {
518         err = errno;
519         goto error;
520     }
521
522     /* Thread closes the handle when exiting, duplicate it here
523      * to be on the safe side when joining. */
524     if (!DuplicateHandle (GetCurrentProcess (), hThread,
525                           GetCurrentProcess (), p_handle, 0, FALSE,
526                           DUPLICATE_SAME_ACCESS))
527     {
528         CloseHandle (hThread);
529         goto error;
530     }
531
532 #else
533     vlc_thread_t th = malloc (sizeof (*th));
534     if (th == NULL)
535         goto error;
536     th->cancel_event = CreateEvent (NULL, FALSE, FALSE, NULL);
537     if (th->cancel_event == NULL)
538     {
539         free (th);
540         goto error;
541     }
542     entry_data->cancel_event = th->cancel_event;
543
544     /* Not sure if CREATE_SUSPENDED + ResumeThread() is any useful on WinCE.
545      * Thread handles act up, too. */
546     th->handle = CreateThread (NULL, 128*1024, vlc_entry, entry_data,
547                                CREATE_SUSPENDED, NULL);
548     if (th->handle == NULL)
549     {
550         CloseHandle (th->cancel_event);
551         free (th);
552         goto error;
553     }
554
555     *p_handle = th;
556     hThread = th->handle;
557
558 #endif
559
560     ResumeThread (hThread);
561     if (priority)
562         SetThreadPriority (hThread, priority);
563
564     return 0;
565
566 error:
567     free (entry_data);
568     return err;
569 }
570
571 void vlc_join (vlc_thread_t handle, void **result)
572 {
573 #ifdef UNDER_CE
574 # define handle handle->handle
575 #endif
576     do
577         vlc_testcancel ();
578     while (WaitForSingleObjectEx (handle, INFINITE, TRUE)
579                                                         == WAIT_IO_COMPLETION);
580
581     CloseHandle (handle);
582     assert (result == NULL); /* <- FIXME if ever needed */
583 #ifdef UNDER_CE
584 # undef handle
585     CloseHandle (handle->cancel_event);
586     free (handle);
587 #endif
588 }
589
590 void vlc_detach (vlc_thread_t handle)
591 {
592 #ifndef UNDER_CE
593     CloseHandle (handle);
594 #else
595     /* FIXME: handle->cancel_event leak */
596     CloseHandle (handle->handle);
597     free (handle);
598 #endif
599 }
600
601 /*** Thread cancellation ***/
602
603 /* APC procedure for thread cancellation */
604 static void CALLBACK vlc_cancel_self (ULONG_PTR dummy)
605 {
606     (void)dummy;
607     vlc_control_cancel (VLC_DO_CANCEL);
608 }
609
610 void vlc_cancel (vlc_thread_t thread_id)
611 {
612 #ifndef UNDER_CE
613     QueueUserAPC (vlc_cancel_self, thread_id, 0);
614 #else
615     SetEvent (thread_id->cancel_event);
616 #endif
617 }
618
619 int vlc_savecancel (void)
620 {
621     int state;
622
623     vlc_cancel_t *nfo = vlc_threadvar_get (cancel_key);
624     if (nfo == NULL)
625         return false; /* Main thread - cannot be cancelled anyway */
626
627     state = nfo->killable;
628     nfo->killable = false;
629     return state;
630 }
631
632 void vlc_restorecancel (int state)
633 {
634     vlc_cancel_t *nfo = vlc_threadvar_get (cancel_key);
635     assert (state == false || state == true);
636
637     if (nfo == NULL)
638         return; /* Main thread - cannot be cancelled anyway */
639
640     assert (!nfo->killable);
641     nfo->killable = state != 0;
642 }
643
644 void vlc_testcancel (void)
645 {
646     vlc_cancel_t *nfo = vlc_threadvar_get (cancel_key);
647     if (nfo == NULL)
648         return; /* Main thread - cannot be cancelled anyway */
649
650     if (nfo->killable && nfo->killed)
651     {
652         for (vlc_cleanup_t *p = nfo->cleaners; p != NULL; p = p->next)
653              p->proc (p->data);
654 #ifndef UNDER_CE
655         _endthread ();
656 #else
657         ExitThread(0);
658 #endif
659     }
660 }
661
662 void vlc_control_cancel (int cmd, ...)
663 {
664     /* NOTE: This function only modifies thread-specific data, so there is no
665      * need to lock anything. */
666     va_list ap;
667
668     vlc_cancel_t *nfo = vlc_threadvar_get (cancel_key);
669     if (nfo == NULL)
670         return; /* Main thread - cannot be cancelled anyway */
671
672     va_start (ap, cmd);
673     switch (cmd)
674     {
675         case VLC_DO_CANCEL:
676             nfo->killed = true;
677             break;
678
679         case VLC_CLEANUP_PUSH:
680         {
681             /* cleaner is a pointer to the caller stack, no need to allocate
682              * and copy anything. As a nice side effect, this cannot fail. */
683             vlc_cleanup_t *cleaner = va_arg (ap, vlc_cleanup_t *);
684             cleaner->next = nfo->cleaners;
685             nfo->cleaners = cleaner;
686             break;
687         }
688
689         case VLC_CLEANUP_POP:
690         {
691             nfo->cleaners = nfo->cleaners->next;
692             break;
693         }
694     }
695     va_end (ap);
696 }
697
698
699 /*** Timers ***/
700 struct vlc_timer
701 {
702 #ifndef UNDER_CE
703     HANDLE handle;
704 #else
705     unsigned id;
706     unsigned interval;
707 #endif
708     void (*func) (void *);
709     void *data;
710 };
711
712 #ifndef UNDER_CE
713 static void CALLBACK vlc_timer_do (void *val, BOOLEAN timeout)
714 {
715     struct vlc_timer *timer = val;
716
717     assert (timeout);
718     timer->func (timer->data);
719 }
720 #else
721 static void CALLBACK vlc_timer_do (unsigned timer_id, unsigned msg,
722                                    DWORD_PTR user, DWORD_PTR unused1,
723                                    DWORD_PTR unused2)
724 {
725     struct vlc_timer *timer = (struct vlc_timer *) user;
726     assert (timer_id == timer->id);
727     (void) msg;
728     (void) unused1;
729     (void) unused2;
730
731     timer->func (timer->data);
732
733     if (timer->interval)
734     {
735         mtime_t interval = timer->interval * 1000;
736         vlc_timer_schedule (timer, false, interval, interval);
737     }
738 }
739 #endif
740
741 int vlc_timer_create (vlc_timer_t *id, void (*func) (void *), void *data)
742 {
743     struct vlc_timer *timer = malloc (sizeof (*timer));
744
745     if (timer == NULL)
746         return ENOMEM;
747     timer->func = func;
748     timer->data = data;
749 #ifndef UNDER_CE
750     timer->handle = INVALID_HANDLE_VALUE;
751 #else
752     timer->id = 0;
753     timer->interval = 0;
754 #endif
755     *id = timer;
756     return 0;
757 }
758
759 void vlc_timer_destroy (vlc_timer_t timer)
760 {
761 #ifndef UNDER_CE
762     if (timer->handle != INVALID_HANDLE_VALUE)
763         DeleteTimerQueueTimer (NULL, timer->handle, INVALID_HANDLE_VALUE);
764 #else
765     if (timer->id)
766         timeKillEvent (timer->id);
767     /* FIXME: timers that have not yet completed will trigger use-after-free */
768 #endif
769     free (timer);
770 }
771
772 void vlc_timer_schedule (vlc_timer_t timer, bool absolute,
773                          mtime_t value, mtime_t interval)
774 {
775 #ifndef UNDER_CE
776     if (timer->handle != INVALID_HANDLE_VALUE)
777     {
778         DeleteTimerQueueTimer (NULL, timer->handle, NULL);
779         timer->handle = INVALID_HANDLE_VALUE;
780     }
781 #else
782     if (timer->id)
783     {
784         timeKillEvent (timer->id);
785         timer->id = 0;
786         timer->interval = 0;
787     }
788 #endif
789     if (value == 0)
790         return; /* Disarm */
791
792     if (absolute)
793         value -= mdate ();
794     value = (value + 999) / 1000;
795     interval = (interval + 999) / 1000;
796
797 #ifndef UNDER_CE
798     if (!CreateTimerQueueTimer (&timer->handle, NULL, vlc_timer_do, timer,
799                                 value, interval, WT_EXECUTEDEFAULT))
800 #else
801     TIMECAPS caps;
802     timeGetDevCaps (&caps, sizeof(caps));
803
804     unsigned delay = value;
805     delay = __MAX(delay, caps.wPeriodMin);
806     delay = __MIN(delay, caps.wPeriodMax);
807
808     unsigned event = TIME_ONESHOT;
809
810     if (interval == delay)
811         event = TIME_PERIODIC;
812     else if (interval)
813         timer->interval = interval;
814
815     timer->id = timeSetEvent (delay, delay / 20, vlc_timer_do, (DWORD) timer,
816                               event);
817     if (!timer->id)
818 #endif
819         abort ();
820 }
821
822 unsigned vlc_timer_getoverrun (vlc_timer_t timer)
823 {
824     (void)timer;
825     return 0;
826 }