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>
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 *****************************************************************************/
33 #include <vlc_common.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
46 #include <sys/select.h>
48 static vlc_threadvar_t thread_key;
63 vlc_cleanup_t *cleaners;
65 void *(*entry) (void *);
69 static void vlc_cancel_self (PVOID dummy);
71 static ULONG vlc_DosWaitEventSemEx( HEV hev, ULONG ulTimeout, BOOL fCancelable )
79 struct vlc_thread *th = vlc_threadvar_get( thread_key );
80 if( th == NULL || !fCancelable )
82 /* Main thread - cannot be cancelled anyway
83 * Alien thread - out of our control
85 if( hev != NULLHANDLE )
86 return DosWaitEventSem( hev, ulTimeout );
88 return DosSleep( ulTimeout );
92 if( hev != NULLHANDLE )
94 asr[ n ].hsemCur = ( HSEM )hev;
98 asr[ n ].hsemCur = ( HSEM )th->cancel_event;
99 asr[ n ].ulUser = 0xFFFF;
102 DosCreateMuxWaitSem( NULL, &hmux, n, asr, DCMW_WAIT_ANY );
103 rc = DosWaitMuxWaitSem( hmux, ulTimeout, &ulUser );
104 DosCloseMuxWaitSem( hmux );
108 if( ulUser == 0xFFFF )
110 vlc_cancel_self( th );
111 return ERROR_INTERRUPT;
117 static ULONG vlc_WaitForSingleObject (HEV hev, ULONG ulTimeout)
119 return vlc_DosWaitEventSemEx( hev, ulTimeout, TRUE );
122 static ULONG vlc_Sleep (ULONG ulTimeout)
124 ULONG rc = vlc_DosWaitEventSemEx( NULLHANDLE, ulTimeout, TRUE );
126 return ( rc != ERROR_TIMEOUT ) ? rc : 0;
129 static vlc_mutex_t super_mutex;
130 static vlc_cond_t super_variable;
131 extern vlc_rwlock_t config_lock;
134 void _CRT_term(void);
136 unsigned long _System _DLL_InitTerm(unsigned long, unsigned long);
138 unsigned long _System _DLL_InitTerm(unsigned long hmod, unsigned long flag)
144 case 0 : /* Initialization */
145 if(_CRT_init() == -1)
148 vlc_mutex_init (&super_mutex);
149 vlc_cond_init (&super_variable);
150 vlc_threadvar_create (&thread_key, NULL);
151 vlc_rwlock_init (&config_lock);
156 case 1 : /* Termination */
157 vlc_rwlock_destroy (&config_lock);
158 vlc_threadvar_delete (&thread_key);
159 vlc_cond_destroy (&super_variable);
160 vlc_mutex_destroy (&super_mutex);
167 return 0; /* Failed */
171 void vlc_mutex_init( vlc_mutex_t *p_mutex )
173 /* This creates a recursive mutex. This is OK as fast mutexes have
174 * no defined behavior in case of recursive locking. */
175 DosCreateMutexSem( NULL, &p_mutex->hmtx, 0, FALSE );
176 p_mutex->dynamic = true;
179 void vlc_mutex_init_recursive( vlc_mutex_t *p_mutex )
181 DosCreateMutexSem( NULL, &p_mutex->hmtx, 0, FALSE );
182 p_mutex->dynamic = true;
186 void vlc_mutex_destroy (vlc_mutex_t *p_mutex)
188 assert (p_mutex->dynamic);
189 DosCloseMutexSem( p_mutex->hmtx );
192 void vlc_mutex_lock (vlc_mutex_t *p_mutex)
194 if (!p_mutex->dynamic)
195 { /* static mutexes */
196 int canc = vlc_savecancel ();
197 assert (p_mutex != &super_mutex); /* this one cannot be static */
199 vlc_mutex_lock (&super_mutex);
200 while (p_mutex->locked)
202 p_mutex->contention++;
203 vlc_cond_wait (&super_variable, &super_mutex);
204 p_mutex->contention--;
206 p_mutex->locked = true;
207 vlc_mutex_unlock (&super_mutex);
208 vlc_restorecancel (canc);
212 DosRequestMutexSem(p_mutex->hmtx, SEM_INDEFINITE_WAIT);
215 int vlc_mutex_trylock (vlc_mutex_t *p_mutex)
217 if (!p_mutex->dynamic)
218 { /* static mutexes */
221 assert (p_mutex != &super_mutex); /* this one cannot be static */
222 vlc_mutex_lock (&super_mutex);
223 if (!p_mutex->locked)
225 p_mutex->locked = true;
228 vlc_mutex_unlock (&super_mutex);
232 return DosRequestMutexSem( p_mutex->hmtx, 0 ) ? EBUSY : 0;
235 void vlc_mutex_unlock (vlc_mutex_t *p_mutex)
237 if (!p_mutex->dynamic)
238 { /* static mutexes */
239 assert (p_mutex != &super_mutex); /* this one cannot be static */
241 vlc_mutex_lock (&super_mutex);
242 assert (p_mutex->locked);
243 p_mutex->locked = false;
244 if (p_mutex->contention)
245 vlc_cond_broadcast (&super_variable);
246 vlc_mutex_unlock (&super_mutex);
250 DosReleaseMutexSem( p_mutex->hmtx );
253 /*** Condition variables ***/
254 #undef CLOCK_REALTIME
255 #undef CLOCK_MONOTONIC
258 CLOCK_REALTIME=0, /* must be zero for VLC_STATIC_COND */
262 static void vlc_cond_init_common (vlc_cond_t *p_condvar, unsigned clock)
264 /* Create a manual-reset event (manual reset is needed for broadcast). */
265 if (DosCreateEventSem (NULL, &p_condvar->hev, 0, FALSE))
267 p_condvar->clock = clock;
270 void vlc_cond_init (vlc_cond_t *p_condvar)
272 vlc_cond_init_common (p_condvar, CLOCK_MONOTONIC);
275 void vlc_cond_init_daytime (vlc_cond_t *p_condvar)
277 vlc_cond_init_common (p_condvar, CLOCK_REALTIME);
280 void vlc_cond_destroy (vlc_cond_t *p_condvar)
282 DosCloseEventSem( p_condvar->hev );
285 void vlc_cond_signal (vlc_cond_t *p_condvar)
290 /* This is suboptimal but works. */
291 vlc_cond_broadcast (p_condvar);
294 void vlc_cond_broadcast (vlc_cond_t *p_condvar)
299 /* Wake all threads up (as the event HANDLE has manual reset) */
300 DosPostEventSem( p_condvar->hev );
303 void vlc_cond_wait (vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex)
309 { /* FIXME FIXME FIXME */
318 vlc_mutex_unlock (p_mutex);
319 rc = vlc_WaitForSingleObject( p_condvar->hev, SEM_INDEFINITE_WAIT );
320 vlc_mutex_lock (p_mutex);
321 } while( rc == ERROR_INTERRUPT );
323 DosResetEventSem( p_condvar->hev, &ulPost );
326 int vlc_cond_timedwait (vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex,
334 { /* FIXME FIXME FIXME */
344 switch (p_condvar->clock)
346 case CLOCK_REALTIME: /* FIXME? sub-second precision */
347 total = CLOCK_FREQ * time (NULL);
350 assert (p_condvar->clock == CLOCK_MONOTONIC);
354 total = (deadline - total) / 1000;
358 ulTimeout = ( total > 0x7fffffff ) ? 0x7fffffff : total;
360 vlc_mutex_unlock (p_mutex);
361 rc = vlc_WaitForSingleObject( p_condvar->hev, ulTimeout );
362 vlc_mutex_lock (p_mutex);
363 } while( rc == ERROR_INTERRUPT );
365 DosResetEventSem( p_condvar->hev, &ulPost );
367 return rc ? ETIMEDOUT : 0;
370 /*** Thread-specific variables (TLS) ***/
374 void (*destroy) (void *);
375 struct vlc_threadvar *prev;
376 struct vlc_threadvar *next;
377 } *vlc_threadvar_last = NULL;
379 int vlc_threadvar_create (vlc_threadvar_t *p_tls, void (*destr) (void *))
383 struct vlc_threadvar *var = malloc (sizeof (*var));
384 if (unlikely(var == NULL))
387 rc = DosAllocThreadLocalMemory( 1, &var->id );
394 var->destroy = destr;
398 vlc_mutex_lock (&super_mutex);
399 var->prev = vlc_threadvar_last;
401 var->prev->next = var;
403 vlc_threadvar_last = var;
404 vlc_mutex_unlock (&super_mutex);
408 void vlc_threadvar_delete (vlc_threadvar_t *p_tls)
410 struct vlc_threadvar *var = *p_tls;
412 vlc_mutex_lock (&super_mutex);
413 if (var->prev != NULL)
414 var->prev->next = var->next;
416 if (var->next != NULL)
417 var->next->prev = var->prev;
419 vlc_threadvar_last = var->prev;
421 vlc_mutex_unlock (&super_mutex);
423 DosFreeThreadLocalMemory( var->id );
427 int vlc_threadvar_set (vlc_threadvar_t key, void *value)
429 *key->id = ( ULONG )value;
433 void *vlc_threadvar_get (vlc_threadvar_t key)
435 return ( void * )*key->id;
440 void vlc_threads_setup (libvlc_int_t *p_libvlc)
445 static void vlc_thread_cleanup (struct vlc_thread *th)
450 /* TODO: use RW lock or something similar */
451 vlc_mutex_lock (&super_mutex);
452 for (key = vlc_threadvar_last; key != NULL; key = key->prev)
454 void *value = vlc_threadvar_get (key);
455 if (value != NULL && key->destroy != NULL)
457 vlc_mutex_unlock (&super_mutex);
458 vlc_threadvar_set (key, NULL);
459 key->destroy (value);
463 vlc_mutex_unlock (&super_mutex);
467 DosCloseEventSem (th->cancel_event);
468 DosCloseEventSem (th->done_event );
470 soclose (th->cancel_sock);
476 static void vlc_entry( void *p )
478 struct vlc_thread *th = p;
480 vlc_threadvar_set (thread_key, th);
482 th->data = th->entry (th->data);
483 DosPostEventSem( th->done_event );
484 vlc_thread_cleanup (th);
487 static int vlc_clone_attr (vlc_thread_t *p_handle, bool detached,
488 void *(*entry) (void *), void *data, int priority)
490 struct vlc_thread *th = malloc (sizeof (*th));
491 if (unlikely(th == NULL))
495 th->detached = detached;
496 th->killable = false; /* not until vlc_entry() ! */
500 if( DosCreateEventSem (NULL, &th->cancel_event, 0, FALSE))
502 if( DosCreateEventSem (NULL, &th->done_event, 0, FALSE))
505 th->cancel_sock = socket (AF_LOCAL, SOCK_STREAM, 0);
506 if( th->cancel_sock < 0 )
509 th->tid = _beginthread (vlc_entry, NULL, 1024 * 1024, th);
510 if((int)th->tid == -1)
513 if (p_handle != NULL)
517 DosSetPriority(PRTYS_THREAD,
525 soclose (th->cancel_sock);
526 DosCloseEventSem (th->cancel_event);
527 DosCloseEventSem (th->done_event);
533 int vlc_clone (vlc_thread_t *p_handle, void *(*entry) (void *),
534 void *data, int priority)
536 return vlc_clone_attr (p_handle, false, entry, data, priority);
539 void vlc_join (vlc_thread_t th, void **result)
546 rc = vlc_WaitForSingleObject( th->done_event, SEM_INDEFINITE_WAIT );
547 } while( rc == ERROR_INTERRUPT );
552 DosCloseEventSem( th->cancel_event );
553 DosCloseEventSem( th->done_event );
555 soclose( th->cancel_sock );
560 int vlc_clone_detach (vlc_thread_t *p_handle, void *(*entry) (void *),
561 void *data, int priority)
564 if (p_handle == NULL)
567 return vlc_clone_attr (p_handle, true, entry, data, priority);
570 int vlc_set_priority (vlc_thread_t th, int priority)
572 if (DosSetPriority(PRTYS_THREAD,
580 /*** Thread cancellation ***/
582 /* APC procedure for thread cancellation */
583 static void vlc_cancel_self (PVOID self)
585 struct vlc_thread *th = self;
587 if (likely(th != NULL))
591 void vlc_cancel (vlc_thread_t thread_id)
593 DosPostEventSem( thread_id->cancel_event );
594 so_cancel( thread_id->cancel_sock );
597 int vlc_savecancel (void)
601 struct vlc_thread *th = vlc_threadvar_get (thread_key);
603 return false; /* Main thread - cannot be cancelled anyway */
605 state = th->killable;
606 th->killable = false;
610 void vlc_restorecancel (int state)
612 struct vlc_thread *th = vlc_threadvar_get (thread_key);
613 assert (state == false || state == true);
616 return; /* Main thread - cannot be cancelled anyway */
618 assert (!th->killable);
619 th->killable = state != 0;
622 void vlc_testcancel (void)
624 struct vlc_thread *th = vlc_threadvar_get (thread_key);
626 return; /* Main thread - cannot be cancelled anyway */
628 /* This check is needed for the case that vlc_cancel() is followed by
629 * vlc_testcancel() without any cancellation point */
630 if( DosWaitEventSem( th->cancel_event, 0 ) == NO_ERROR )
631 vlc_cancel_self( th );
633 if (th->killable && th->killed)
635 for (vlc_cleanup_t *p = th->cleaners; p != NULL; p = p->next)
638 DosPostEventSem( th->done_event );
639 th->data = NULL; /* TODO: special value? */
640 vlc_thread_cleanup (th);
645 void vlc_control_cancel (int cmd, ...)
647 /* NOTE: This function only modifies thread-specific data, so there is no
648 * need to lock anything. */
651 struct vlc_thread *th = vlc_threadvar_get (thread_key);
653 return; /* Main thread - cannot be cancelled anyway */
658 case VLC_CLEANUP_PUSH:
660 /* cleaner is a pointer to the caller stack, no need to allocate
661 * and copy anything. As a nice side effect, this cannot fail. */
662 vlc_cleanup_t *cleaner = va_arg (ap, vlc_cleanup_t *);
663 cleaner->next = th->cleaners;
664 th->cleaners = cleaner;
668 case VLC_CLEANUP_POP:
670 th->cleaners = th->cleaners->next;
677 static int vlc_select( int nfds, fd_set *rdset, fd_set *wrset, fd_set *exset,
678 struct timeval *timeout )
680 struct vlc_thread *th = vlc_threadvar_get( thread_key );
686 FD_SET( th->cancel_sock, rdset );
688 nfds = MAX( nfds, th->cancel_sock + 1 );
691 rc = select( nfds, rdset, wrset, exset, timeout );
699 int vlc_poll( struct pollfd *fds, unsigned nfds, int timeout )
701 fd_set rdset, wrset, exset;
703 struct timeval tv = { 0, 0 };
710 for( unsigned i = 0; i < nfds; i++ )
712 int fd = fds[ i ].fd;
716 if(( unsigned )fd >= FD_SETSIZE )
722 if( fds[ i ].events & POLLIN )
723 FD_SET( fd, &rdset );
724 if( fds[ i ].events & POLLOUT )
725 FD_SET( fd, &wrset );
726 if( fds[ i ].events & POLLPRI )
727 FD_SET( fd, &exset );
732 div_t d = div( timeout, 1000 );
734 tv.tv_usec = d.rem * 1000;
737 val = vlc_select( val + 1, &rdset, &wrset, &exset,
738 ( timeout >= 0 ) ? &tv : NULL );
742 for( unsigned i = 0; i < nfds; i++ )
744 int fd = fds[ i ].fd;
745 fds[ i ].revents = ( FD_ISSET( fd, &rdset ) ? POLLIN : 0 )
746 | ( FD_ISSET( fd, &wrset ) ? POLLOUT : 0 )
747 | ( FD_ISSET( fd, &exset ) ? POLLPRI : 0 );
753 #define Q2LL( q ) ( *( long long * )&( q ))
758 /* We don't need the real date, just the value of a high precision timer */
761 if (DosTmrQueryTime(&counter) || DosTmrQueryFreq(&freq))
764 /* Convert to from (1/freq) to microsecond resolution */
765 /* We need to split the division to avoid 63-bits overflow */
766 lldiv_t d = lldiv (Q2LL(counter), freq);
768 return (d.quot * 1000000) + ((d.rem * 1000000) / freq);
772 void mwait (mtime_t deadline)
777 while ((delay = (deadline - mdate())) > 0)
780 if (unlikely(delay > 0x7fffffff))
788 void msleep (mtime_t delay)
790 mwait (mdate () + delay);
801 void (*func) (void *);
805 static void vlc_timer_do (void *arg)
807 struct vlc_timer *timer = arg;
813 DosWaitEventSem (timer->hev, SEM_INDEFINITE_WAIT);
814 DosResetEventSem (timer->hev, &count);
819 timer->func (timer->data);
822 DosAsyncTimer (timer->interval, (HSEM)timer->hev, &timer->htimer);
826 int vlc_timer_create (vlc_timer_t *id, void (*func) (void *), void *data)
828 struct vlc_timer *timer = malloc (sizeof (*timer));
836 DosCreateEventSem (NULL, &timer->hev, DC_SEM_SHARED, FALSE);
837 timer->htimer = NULLHANDLE;
840 timer->tid = _beginthread (vlc_timer_do, NULL, 1024 * 1024, timer);
846 void vlc_timer_destroy (vlc_timer_t timer)
848 if (timer->htimer != NULLHANDLE)
849 DosStopTimer (timer->htimer);
852 DosPostEventSem (timer->hev);
853 DosWaitThread (&timer->tid, DCWW_WAIT);
854 DosCloseEventSem (timer->hev);
859 void vlc_timer_schedule (vlc_timer_t timer, bool absolute,
860 mtime_t value, mtime_t interval)
862 if (timer->htimer != NULLHANDLE)
864 DosStopTimer (timer->htimer);
865 timer->htimer = NULLHANDLE;
874 value = (value + 999) / 1000;
875 interval = (interval + 999) / 1000;
877 timer->interval = interval;
878 if (DosAsyncTimer (value, (HSEM)timer->hev, &timer->htimer))
882 unsigned vlc_timer_getoverrun (vlc_timer_t timer)
889 unsigned vlc_GetCPUCount (void)
893 DosQuerySysInfo(QSV_NUMPROCESSORS, QSV_NUMPROCESSORS,
894 &numprocs, sizeof(numprocs));