cpu: privatize vlc_CPU_init()
[vlc.git] / src / os2 / thread.c
blob8f7b335ea4189ba172a125e73aca56be5f84cfaf
1 /*****************************************************************************
2 * thread.c : OS/2 back-end for LibVLC
3 *****************************************************************************
4 * Copyright (C) 1999-2011 VLC authors and VideoLAN
6 * Authors: KO Myung-Hun <komh@chollian.net>
7 * 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
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU Lesser General Public License as published by
16 * the Free Software Foundation; either version 2.1 of the License, or
17 * (at your option) any later version.
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 Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public License
25 * along with this program; if not, write to the Free Software Foundation,
26 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
27 *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
33 #include <vlc_common.h>
35 #include "libvlc.h"
36 #include <stdarg.h>
37 #include <assert.h>
38 #include <limits.h>
39 #include <errno.h>
40 #include <time.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
45 #include <sys/time.h>
46 #include <sys/select.h>
48 #include <sys/builtin.h>
50 #include <sys/stat.h>
52 static vlc_threadvar_t thread_key;
54 struct vlc_thread
56 TID tid;
57 HEV cancel_event;
58 HEV done_event;
59 int cancel_sock;
61 bool detached;
62 bool killable;
63 bool killed;
64 vlc_cleanup_t *cleaners;
66 void *(*entry) (void *);
67 void *data;
70 static void vlc_cancel_self (PVOID dummy);
72 static ULONG vlc_DosWaitEventSemEx( HEV hev, ULONG ulTimeout )
74 HMUX hmux;
75 SEMRECORD asr[ 2 ];
76 ULONG ulUser;
77 int n;
78 ULONG rc;
80 struct vlc_thread *th = vlc_thread_self ();
81 if( th == NULL || !th->killable )
83 /* Main thread - cannot be cancelled anyway
84 * Alien thread - out of our control
85 * Cancel disabled thread - ignore cancel
87 if( hev != NULLHANDLE )
88 return DosWaitEventSem( hev, ulTimeout );
90 return DosSleep( ulTimeout );
93 n = 0;
94 if( hev != NULLHANDLE )
96 asr[ n ].hsemCur = ( HSEM )hev;
97 asr[ n ].ulUser = 0;
98 n++;
100 asr[ n ].hsemCur = ( HSEM )th->cancel_event;
101 asr[ n ].ulUser = 0xFFFF;
102 n++;
104 DosCreateMuxWaitSem( NULL, &hmux, n, asr, DCMW_WAIT_ANY );
105 rc = DosWaitMuxWaitSem( hmux, ulTimeout, &ulUser );
106 DosCloseMuxWaitSem( hmux );
107 if( rc )
108 return rc;
110 if( ulUser == 0xFFFF )
112 vlc_cancel_self( th );
113 return ERROR_INTERRUPT;
116 return NO_ERROR;
119 static ULONG vlc_WaitForSingleObject (HEV hev, ULONG ulTimeout)
121 return vlc_DosWaitEventSemEx( hev, ulTimeout );
124 static ULONG vlc_Sleep (ULONG ulTimeout)
126 ULONG rc = vlc_DosWaitEventSemEx( NULLHANDLE, ulTimeout );
128 return ( rc != ERROR_TIMEOUT ) ? rc : 0;
131 static vlc_mutex_t super_mutex;
132 static vlc_cond_t super_variable;
133 extern vlc_rwlock_t config_lock;
135 static void vlc_static_cond_destroy_all(void);
137 int _CRT_init(void);
138 void _CRT_term(void);
140 unsigned long _System _DLL_InitTerm(unsigned long, unsigned long);
142 unsigned long _System _DLL_InitTerm(unsigned long hmod, unsigned long flag)
144 VLC_UNUSED (hmod);
146 switch (flag)
148 case 0 : /* Initialization */
149 if(_CRT_init() == -1)
150 return 0;
152 vlc_mutex_init (&super_mutex);
153 vlc_cond_init (&super_variable);
154 vlc_threadvar_create (&thread_key, NULL);
155 vlc_rwlock_init (&config_lock);
157 return 1;
159 case 1 : /* Termination */
160 vlc_rwlock_destroy (&config_lock);
161 vlc_threadvar_delete (&thread_key);
162 vlc_cond_destroy (&super_variable);
163 vlc_mutex_destroy (&super_mutex);
164 vlc_static_cond_destroy_all ();
166 _CRT_term();
168 return 1;
171 return 0; /* Failed */
174 /*** Mutexes ***/
175 void vlc_mutex_init( vlc_mutex_t *p_mutex )
177 /* This creates a recursive mutex. This is OK as fast mutexes have
178 * no defined behavior in case of recursive locking. */
179 DosCreateMutexSem( NULL, &p_mutex->hmtx, 0, FALSE );
180 p_mutex->dynamic = true;
183 void vlc_mutex_init_recursive( vlc_mutex_t *p_mutex )
185 DosCreateMutexSem( NULL, &p_mutex->hmtx, 0, FALSE );
186 p_mutex->dynamic = true;
190 void vlc_mutex_destroy (vlc_mutex_t *p_mutex)
192 assert (p_mutex->dynamic);
193 DosCloseMutexSem( p_mutex->hmtx );
196 void vlc_mutex_lock (vlc_mutex_t *p_mutex)
198 if (!p_mutex->dynamic)
199 { /* static mutexes */
200 int canc = vlc_savecancel ();
201 assert (p_mutex != &super_mutex); /* this one cannot be static */
203 vlc_mutex_lock (&super_mutex);
204 while (p_mutex->locked)
206 p_mutex->contention++;
207 vlc_cond_wait (&super_variable, &super_mutex);
208 p_mutex->contention--;
210 p_mutex->locked = true;
211 vlc_mutex_unlock (&super_mutex);
212 vlc_restorecancel (canc);
213 return;
216 DosRequestMutexSem(p_mutex->hmtx, SEM_INDEFINITE_WAIT);
219 int vlc_mutex_trylock (vlc_mutex_t *p_mutex)
221 if (!p_mutex->dynamic)
222 { /* static mutexes */
223 int ret = EBUSY;
225 assert (p_mutex != &super_mutex); /* this one cannot be static */
226 vlc_mutex_lock (&super_mutex);
227 if (!p_mutex->locked)
229 p_mutex->locked = true;
230 ret = 0;
232 vlc_mutex_unlock (&super_mutex);
233 return ret;
236 return DosRequestMutexSem( p_mutex->hmtx, 0 ) ? EBUSY : 0;
239 void vlc_mutex_unlock (vlc_mutex_t *p_mutex)
241 if (!p_mutex->dynamic)
242 { /* static mutexes */
243 assert (p_mutex != &super_mutex); /* this one cannot be static */
245 vlc_mutex_lock (&super_mutex);
246 assert (p_mutex->locked);
247 p_mutex->locked = false;
248 if (p_mutex->contention)
249 vlc_cond_broadcast (&super_variable);
250 vlc_mutex_unlock (&super_mutex);
251 return;
254 DosReleaseMutexSem( p_mutex->hmtx );
257 /*** Condition variables ***/
258 typedef struct vlc_static_cond_t vlc_static_cond_t;
260 struct vlc_static_cond_t
262 vlc_cond_t condvar;
263 vlc_static_cond_t *next;
266 static vlc_static_cond_t *static_condvar_start = NULL;
268 static void vlc_static_cond_init (vlc_cond_t *p_condvar)
270 vlc_mutex_lock (&super_mutex);
272 if (p_condvar->hev == NULLHANDLE)
274 vlc_cond_init (p_condvar);
276 vlc_static_cond_t *new_static_condvar;
278 new_static_condvar = malloc (sizeof (*new_static_condvar));
279 if (unlikely (!new_static_condvar))
280 abort();
282 memcpy (&new_static_condvar->condvar, p_condvar, sizeof (*p_condvar));
283 new_static_condvar->next = static_condvar_start;
284 static_condvar_start = new_static_condvar;
287 vlc_mutex_unlock (&super_mutex);
290 static void vlc_static_cond_destroy_all (void)
292 vlc_static_cond_t *static_condvar;
293 vlc_static_cond_t *static_condvar_next;
296 for (static_condvar = static_condvar_start; static_condvar;
297 static_condvar = static_condvar_next)
299 static_condvar_next = static_condvar->next;
301 vlc_cond_destroy (&static_condvar->condvar);
302 free (static_condvar);
306 void vlc_cond_init (vlc_cond_t *p_condvar)
308 if (DosCreateEventSem (NULL, &p_condvar->hev, 0, FALSE) ||
309 DosCreateEventSem (NULL, &p_condvar->hevAck, 0, FALSE))
310 abort();
312 p_condvar->waiters = 0;
313 p_condvar->signaled = 0;
316 void vlc_cond_init_daytime (vlc_cond_t *p_condvar)
318 vlc_cond_init (p_condvar);
321 void vlc_cond_destroy (vlc_cond_t *p_condvar)
323 DosCloseEventSem( p_condvar->hev );
324 DosCloseEventSem( p_condvar->hevAck );
327 void vlc_cond_signal (vlc_cond_t *p_condvar)
329 if (p_condvar->hev == NULLHANDLE)
330 vlc_static_cond_init (p_condvar);
332 if (!__atomic_cmpxchg32 (&p_condvar->waiters, 0, 0))
334 ULONG ulPost;
336 __atomic_xchg (&p_condvar->signaled, 1);
337 DosPostEventSem (p_condvar->hev);
339 DosWaitEventSem (p_condvar->hevAck, SEM_INDEFINITE_WAIT);
340 DosResetEventSem (p_condvar->hevAck, &ulPost);
344 void vlc_cond_broadcast (vlc_cond_t *p_condvar)
346 if (p_condvar->hev == NULLHANDLE)
347 vlc_static_cond_init (p_condvar);
349 while (!__atomic_cmpxchg32 (&p_condvar->waiters, 0, 0))
350 vlc_cond_signal (p_condvar);
353 static int vlc_cond_wait_common (vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex,
354 ULONG ulTimeout)
356 ULONG ulPost;
357 ULONG rc;
359 assert(p_condvar->hev != NULLHANDLE);
363 vlc_testcancel();
365 __atomic_increment (&p_condvar->waiters);
367 vlc_mutex_unlock (p_mutex);
371 rc = vlc_WaitForSingleObject( p_condvar->hev, ulTimeout );
372 if (rc == NO_ERROR)
373 DosResetEventSem (p_condvar->hev, &ulPost);
374 } while (rc == NO_ERROR &&
375 __atomic_cmpxchg32 (&p_condvar->signaled, 0, 1) == 0);
377 __atomic_decrement (&p_condvar->waiters);
379 DosPostEventSem (p_condvar->hevAck);
381 vlc_mutex_lock (p_mutex);
382 } while( rc == ERROR_INTERRUPT );
384 return rc ? ETIMEDOUT : 0;
387 void vlc_cond_wait (vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex)
389 if (p_condvar->hev == NULLHANDLE)
390 vlc_static_cond_init (p_condvar);
392 vlc_cond_wait_common (p_condvar, p_mutex, SEM_INDEFINITE_WAIT);
395 int vlc_cond_timedwait (vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex,
396 mtime_t deadline)
398 ULONG ulTimeout;
400 mtime_t total = mdate();
401 total = (deadline - total) / 1000;
402 if( total < 0 )
403 total = 0;
405 ulTimeout = ( total > 0x7fffffff ) ? 0x7fffffff : total;
407 return vlc_cond_wait_common (p_condvar, p_mutex, ulTimeout);
410 int vlc_cond_timedwait_daytime (vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex,
411 time_t deadline)
413 ULONG ulTimeout;
414 mtime_t total;
415 struct timeval tv;
417 gettimeofday (&tv, NULL);
419 total = CLOCK_FREQ * tv.tv_sec +
420 CLOCK_FREQ * tv.tv_usec / 1000000L;
421 total = (deadline - total) / 1000;
422 if( total < 0 )
423 total = 0;
425 ulTimeout = ( total > 0x7fffffff ) ? 0x7fffffff : total;
427 return vlc_cond_wait_common (p_condvar, p_mutex, ulTimeout);
430 /*** Thread-specific variables (TLS) ***/
431 struct vlc_threadvar
433 PULONG id;
434 void (*destroy) (void *);
435 struct vlc_threadvar *prev;
436 struct vlc_threadvar *next;
437 } *vlc_threadvar_last = NULL;
439 int vlc_threadvar_create (vlc_threadvar_t *p_tls, void (*destr) (void *))
441 ULONG rc;
443 struct vlc_threadvar *var = malloc (sizeof (*var));
444 if (unlikely(var == NULL))
445 return errno;
447 rc = DosAllocThreadLocalMemory( 1, &var->id );
448 if( rc )
450 free (var);
451 return EAGAIN;
454 var->destroy = destr;
455 var->next = NULL;
456 *p_tls = var;
458 vlc_mutex_lock (&super_mutex);
459 var->prev = vlc_threadvar_last;
460 if (var->prev)
461 var->prev->next = var;
463 vlc_threadvar_last = var;
464 vlc_mutex_unlock (&super_mutex);
465 return 0;
468 void vlc_threadvar_delete (vlc_threadvar_t *p_tls)
470 struct vlc_threadvar *var = *p_tls;
472 vlc_mutex_lock (&super_mutex);
473 if (var->prev != NULL)
474 var->prev->next = var->next;
476 if (var->next != NULL)
477 var->next->prev = var->prev;
478 else
479 vlc_threadvar_last = var->prev;
481 vlc_mutex_unlock (&super_mutex);
483 DosFreeThreadLocalMemory( var->id );
484 free (var);
487 int vlc_threadvar_set (vlc_threadvar_t key, void *value)
489 *key->id = ( ULONG )value;
490 return 0;
493 void *vlc_threadvar_get (vlc_threadvar_t key)
495 return ( void * )*key->id;
499 /*** Threads ***/
500 void vlc_threads_setup (libvlc_int_t *p_libvlc)
502 (void) p_libvlc;
505 static void vlc_thread_cleanup (struct vlc_thread *th)
507 vlc_threadvar_t key;
509 retry:
510 /* TODO: use RW lock or something similar */
511 vlc_mutex_lock (&super_mutex);
512 for (key = vlc_threadvar_last; key != NULL; key = key->prev)
514 void *value = vlc_threadvar_get (key);
515 if (value != NULL && key->destroy != NULL)
517 vlc_mutex_unlock (&super_mutex);
518 vlc_threadvar_set (key, NULL);
519 key->destroy (value);
520 goto retry;
523 vlc_mutex_unlock (&super_mutex);
525 if (th->detached)
527 DosCloseEventSem (th->cancel_event);
528 DosCloseEventSem (th->done_event );
530 soclose (th->cancel_sock);
532 free (th);
536 static void vlc_entry( void *p )
538 struct vlc_thread *th = p;
540 vlc_threadvar_set (thread_key, th);
541 th->killable = true;
542 th->data = th->entry (th->data);
543 DosPostEventSem( th->done_event );
544 vlc_thread_cleanup (th);
547 static int vlc_clone_attr (vlc_thread_t *p_handle, bool detached,
548 void *(*entry) (void *), void *data, int priority)
550 struct vlc_thread *th = malloc (sizeof (*th));
551 if (unlikely(th == NULL))
552 return ENOMEM;
553 th->entry = entry;
554 th->data = data;
555 th->detached = detached;
556 th->killable = false; /* not until vlc_entry() ! */
557 th->killed = false;
558 th->cleaners = NULL;
560 if( DosCreateEventSem (NULL, &th->cancel_event, 0, FALSE))
561 goto error;
562 if( DosCreateEventSem (NULL, &th->done_event, 0, FALSE))
563 goto error;
565 th->cancel_sock = socket (AF_LOCAL, SOCK_STREAM, 0);
566 if( th->cancel_sock < 0 )
567 goto error;
569 th->tid = _beginthread (vlc_entry, NULL, 1024 * 1024, th);
570 if((int)th->tid == -1)
571 goto error;
573 if (p_handle != NULL)
574 *p_handle = th;
576 if (priority)
577 DosSetPriority(PRTYS_THREAD,
578 HIBYTE(priority),
579 LOBYTE(priority),
580 th->tid);
582 return 0;
584 error:
585 soclose (th->cancel_sock);
586 DosCloseEventSem (th->cancel_event);
587 DosCloseEventSem (th->done_event);
588 free (th);
590 return ENOMEM;
593 int vlc_clone (vlc_thread_t *p_handle, void *(*entry) (void *),
594 void *data, int priority)
596 return vlc_clone_attr (p_handle, false, entry, data, priority);
599 void vlc_join (vlc_thread_t th, void **result)
601 ULONG rc;
605 vlc_testcancel();
606 rc = vlc_WaitForSingleObject( th->done_event, SEM_INDEFINITE_WAIT );
607 } while( rc == ERROR_INTERRUPT );
609 if (result != NULL)
610 *result = th->data;
612 DosCloseEventSem( th->cancel_event );
613 DosCloseEventSem( th->done_event );
615 soclose( th->cancel_sock );
617 free( th );
620 int vlc_clone_detach (vlc_thread_t *p_handle, void *(*entry) (void *),
621 void *data, int priority)
623 vlc_thread_t th;
624 if (p_handle == NULL)
625 p_handle = &th;
627 return vlc_clone_attr (p_handle, true, entry, data, priority);
630 int vlc_set_priority (vlc_thread_t th, int priority)
632 if (DosSetPriority(PRTYS_THREAD,
633 HIBYTE(priority),
634 LOBYTE(priority),
635 th->tid))
636 return VLC_EGENERIC;
637 return VLC_SUCCESS;
640 vlc_thread_t vlc_thread_self (void)
642 return vlc_threadvar_get (thread_key);
645 unsigned long vlc_thread_id (void)
647 return _gettid();
650 /*** Thread cancellation ***/
652 /* APC procedure for thread cancellation */
653 static void vlc_cancel_self (PVOID self)
655 struct vlc_thread *th = self;
657 if (likely(th != NULL))
658 th->killed = true;
661 void vlc_cancel (vlc_thread_t thread_id)
663 DosPostEventSem( thread_id->cancel_event );
664 so_cancel( thread_id->cancel_sock );
667 int vlc_savecancel (void)
669 int state;
671 struct vlc_thread *th = vlc_thread_self ();
672 if (th == NULL)
673 return false; /* Main thread - cannot be cancelled anyway */
675 state = th->killable;
676 th->killable = false;
677 return state;
680 void vlc_restorecancel (int state)
682 struct vlc_thread *th = vlc_thread_self ();
683 assert (state == false || state == true);
685 if (th == NULL)
686 return; /* Main thread - cannot be cancelled anyway */
688 assert (!th->killable);
689 th->killable = state != 0;
692 void vlc_testcancel (void)
694 struct vlc_thread *th = vlc_thread_self ();
695 if (th == NULL)
696 return; /* Main thread - cannot be cancelled anyway */
698 /* This check is needed for the case that vlc_cancel() is followed by
699 * vlc_testcancel() without any cancellation point */
700 if( DosWaitEventSem( th->cancel_event, 0 ) == NO_ERROR )
701 vlc_cancel_self( th );
703 if (th->killable && th->killed)
705 for (vlc_cleanup_t *p = th->cleaners; p != NULL; p = p->next)
706 p->proc (p->data);
708 DosPostEventSem( th->done_event );
709 th->data = NULL; /* TODO: special value? */
710 vlc_thread_cleanup (th);
711 _endthread();
715 void vlc_control_cancel (int cmd, ...)
717 /* NOTE: This function only modifies thread-specific data, so there is no
718 * need to lock anything. */
719 va_list ap;
721 struct vlc_thread *th = vlc_thread_self ();
722 if (th == NULL)
723 return; /* Main thread - cannot be cancelled anyway */
725 va_start (ap, cmd);
726 switch (cmd)
728 case VLC_CLEANUP_PUSH:
730 /* cleaner is a pointer to the caller stack, no need to allocate
731 * and copy anything. As a nice side effect, this cannot fail. */
732 vlc_cleanup_t *cleaner = va_arg (ap, vlc_cleanup_t *);
733 cleaner->next = th->cleaners;
734 th->cleaners = cleaner;
735 break;
738 case VLC_CLEANUP_POP:
740 th->cleaners = th->cleaners->next;
741 break;
744 va_end (ap);
747 static int vlc_select( int nfds, fd_set *rdset, fd_set *wrset, fd_set *exset,
748 struct timeval *timeout )
750 struct vlc_thread *th = vlc_thread_self( );
752 int rc;
754 if( th )
756 FD_SET( th->cancel_sock, rdset );
758 nfds = MAX( nfds, th->cancel_sock + 1 );
761 rc = select( nfds, rdset, wrset, exset, timeout );
763 vlc_testcancel();
765 return rc;
769 /* Export vlc_poll_os2 directly regardless of EXPORTS of .def */
770 __declspec(dllexport)
771 int vlc_poll_os2( struct pollfd *fds, unsigned nfds, int timeout );
773 __declspec(dllexport)
774 int vlc_poll_os2( struct pollfd *fds, unsigned nfds, int timeout )
776 fd_set rdset, wrset, exset;
778 int non_sockets = 0;
780 struct timeval tv = { 0, 0 };
782 int val = -1;
784 FD_ZERO( &rdset );
785 FD_ZERO( &wrset );
786 FD_ZERO( &exset );
787 for( unsigned i = 0; i < nfds; i++ )
789 int fd = fds[ i ].fd;
790 struct stat stbuf;
792 fds[ i ].revents = 0;
794 if( fstat( fd, &stbuf ) == -1 ||
795 (errno = 0, !S_ISSOCK( stbuf.st_mode )))
797 if( fd >= 0 )
799 /* If regular files, assume readiness for requested modes */
800 fds[ i ].revents = ( !errno && S_ISREG( stbuf.st_mode ))
801 ? ( fds[ i ].events &
802 ( POLLIN | POLLOUT | POLLPRI ))
803 : POLLNVAL;
805 non_sockets++;
808 continue;
811 if( val < fd )
812 val = fd;
814 if(( unsigned )fd >= FD_SETSIZE )
816 errno = EINVAL;
817 return -1;
820 if( fds[ i ].events & POLLIN )
821 FD_SET( fd, &rdset );
822 if( fds[ i ].events & POLLOUT )
823 FD_SET( fd, &wrset );
824 if( fds[ i ].events & POLLPRI )
825 FD_SET( fd, &exset );
828 if( non_sockets > 0 )
829 timeout = 0; /* Just check pending sockets */
831 /* Sockets included ? */
832 if( val != -1)
834 struct timeval *ptv = NULL;
836 if( timeout >= 0 )
838 div_t d = div( timeout, 1000 );
839 tv.tv_sec = d.quot;
840 tv.tv_usec = d.rem * 1000;
842 ptv = &tv;
845 if (vlc_select( val + 1, &rdset, &wrset, &exset, ptv ) == -1)
846 return -1;
849 val = 0;
850 for( unsigned i = 0; i < nfds; i++ )
852 int fd = fds[ i ].fd;
854 if( fd >= 0 && fds[ i ].revents == 0 )
856 fds[ i ].revents = ( FD_ISSET( fd, &rdset ) ? POLLIN : 0 )
857 | ( FD_ISSET( fd, &wrset ) ? POLLOUT : 0 )
858 | ( FD_ISSET( fd, &exset ) ? POLLPRI : 0 );
861 if( fds[ i ].revents != 0 )
862 val++;
865 return val;
868 #define Q2LL( q ) ( *( long long * )&( q ))
870 /*** Clock ***/
871 mtime_t mdate (void)
873 /* We don't need the real date, just the value of a high precision timer */
874 QWORD counter;
875 ULONG freq;
876 if (DosTmrQueryTime(&counter) || DosTmrQueryFreq(&freq))
877 abort();
879 /* Convert to from (1/freq) to microsecond resolution */
880 /* We need to split the division to avoid 63-bits overflow */
881 lldiv_t d = lldiv (Q2LL(counter), freq);
883 return (d.quot * 1000000) + ((d.rem * 1000000) / freq);
886 #undef mwait
887 void mwait (mtime_t deadline)
889 mtime_t delay;
891 vlc_testcancel();
892 while ((delay = (deadline - mdate())) > 0)
894 delay /= 1000;
895 if (unlikely(delay > 0x7fffffff))
896 delay = 0x7fffffff;
897 vlc_Sleep (delay);
898 vlc_testcancel();
902 #undef msleep
903 void msleep (mtime_t delay)
905 mwait (mdate () + delay);
908 /*** Timers ***/
909 struct vlc_timer
911 TID tid;
912 HEV hev;
913 HTIMER htimer;
914 ULONG interval;
915 bool quit;
916 void (*func) (void *);
917 void *data;
920 static void vlc_timer_do (void *arg)
922 struct vlc_timer *timer = arg;
924 while (1)
926 ULONG count;
928 DosWaitEventSem (timer->hev, SEM_INDEFINITE_WAIT);
929 DosResetEventSem (timer->hev, &count);
931 if (timer->quit)
932 break;
934 timer->func (timer->data);
936 if (timer->interval)
937 DosAsyncTimer (timer->interval, (HSEM)timer->hev, &timer->htimer);
941 int vlc_timer_create (vlc_timer_t *id, void (*func) (void *), void *data)
943 struct vlc_timer *timer = malloc (sizeof (*timer));
945 if (timer == NULL)
946 return ENOMEM;
948 timer->func = func;
949 timer->data = data;
951 DosCreateEventSem (NULL, &timer->hev, DC_SEM_SHARED, FALSE);
952 timer->htimer = NULLHANDLE;
953 timer->interval = 0;
954 timer->quit = false;
955 timer->tid = _beginthread (vlc_timer_do, NULL, 1024 * 1024, timer);
957 *id = timer;
958 return 0;
961 void vlc_timer_destroy (vlc_timer_t timer)
963 if (timer->htimer != NULLHANDLE)
964 DosStopTimer (timer->htimer);
966 timer->quit = true;
967 DosPostEventSem (timer->hev);
968 DosWaitThread (&timer->tid, DCWW_WAIT);
969 DosCloseEventSem (timer->hev);
971 free (timer);
974 void vlc_timer_schedule (vlc_timer_t timer, bool absolute,
975 mtime_t value, mtime_t interval)
977 if (timer->htimer != NULLHANDLE)
979 DosStopTimer (timer->htimer);
980 timer->htimer = NULLHANDLE;
981 timer->interval = 0;
984 if (value == 0)
985 return; /* Disarm */
987 if (absolute)
988 value -= mdate ();
989 value = (value + 999) / 1000;
990 interval = (interval + 999) / 1000;
992 timer->interval = interval;
993 if (DosAsyncTimer (value, (HSEM)timer->hev, &timer->htimer))
994 abort ();
997 unsigned vlc_timer_getoverrun (vlc_timer_t timer)
999 (void)timer;
1000 return 0;
1003 /*** CPU ***/
1004 unsigned vlc_GetCPUCount (void)
1006 ULONG numprocs = 1;
1008 DosQuerySysInfo(QSV_NUMPROCESSORS, QSV_NUMPROCESSORS,
1009 &numprocs, sizeof(numprocs));
1011 return numprocs;