1 /*****************************************************************************
2 * pthread.c : pthread 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>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
31 #include <vlc_common.h>
36 #include <unistd.h> /* fsync() */
42 # include <sys/syscall.h> /* SYS_gettid */
45 #ifdef HAVE_EXECINFO_H
46 # include <execinfo.h>
50 # include <sys/time.h> /* gettimeofday in vlc_cond_timedwait */
54 * Print a backtrace to the standard error for debugging purpose.
56 void vlc_trace (const char *fn, const char *file, unsigned line)
58 fprintf (stderr, "at %s:%u in %s\n", file, line, fn);
59 fflush (stderr); /* needed before switch to low-level I/O */
62 int len = backtrace (stack, sizeof (stack) / sizeof (stack[0]));
63 backtrace_symbols_fd (stack, len, 2);
68 static inline unsigned long vlc_threadid (void)
70 #if defined (__linux__)
71 /* glibc does not provide a call for this */
72 return syscall (SYS_gettid);
75 union { pthread_t th; unsigned long int i; } v = { };
76 v.th = pthread_self ();
83 /*****************************************************************************
84 * vlc_thread_fatal: Report an error from the threading layer
85 *****************************************************************************
86 * This is mostly meant for debugging.
87 *****************************************************************************/
89 vlc_thread_fatal (const char *action, int error,
90 const char *function, const char *file, unsigned line)
92 int canc = vlc_savecancel ();
93 fprintf (stderr, "LibVLC fatal error %s (%d) in thread %lu ",
94 action, error, vlc_threadid ());
95 vlc_trace (function, file, line);
97 /* Sometimes strerror_r() crashes too, so make sure we print an error
98 * message before we invoke it */
100 /* Avoid the strerror_r() prototype brain damage in glibc */
102 fprintf (stderr, " Error message: %m\n");
107 switch (strerror_r (error, buf, sizeof (buf)))
112 case ERANGE: /* should never happen */
113 msg = "unknwon (too big to display)";
116 msg = "unknown (invalid error number)";
119 fprintf (stderr, " Error message: %s\n", msg);
123 vlc_restorecancel (canc);
127 # define VLC_THREAD_ASSERT( action ) \
129 vlc_thread_fatal (action, val, __func__, __FILE__, __LINE__)
131 # define VLC_THREAD_ASSERT( action ) ((void)val)
134 #if defined (__GLIBC__) && (__GLIBC_MINOR__ < 6)
135 /* This is not prototyped under glibc, though it exists. */
136 int pthread_mutexattr_setkind_np( pthread_mutexattr_t *attr, int kind );
139 /*****************************************************************************
140 * vlc_mutex_init: initialize a mutex
141 *****************************************************************************/
142 void vlc_mutex_init( vlc_mutex_t *p_mutex )
144 pthread_mutexattr_t attr;
146 if (unlikely(pthread_mutexattr_init (&attr)))
149 pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_NORMAL );
151 /* Create error-checking mutex to detect problems more easily. */
152 # if defined (__GLIBC__) && (__GLIBC__ == 2) && (__GLIBC_MINOR__ < 6)
153 pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_ERRORCHECK_NP );
155 pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_ERRORCHECK );
158 if (unlikely(pthread_mutex_init (p_mutex, &attr)))
160 pthread_mutexattr_destroy( &attr );
163 /*****************************************************************************
164 * vlc_mutex_init: initialize a recursive mutex (Do not use)
165 *****************************************************************************/
166 void vlc_mutex_init_recursive( vlc_mutex_t *p_mutex )
168 pthread_mutexattr_t attr;
170 if (unlikely(pthread_mutexattr_init (&attr)))
172 #if defined (__GLIBC__) && (__GLIBC__ == 2) && (__GLIBC_MINOR__ < 6)
173 pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_RECURSIVE_NP );
175 pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_RECURSIVE );
177 if (unlikely(pthread_mutex_init (p_mutex, &attr)))
179 pthread_mutexattr_destroy( &attr );
184 * Destroys a mutex. The mutex must not be locked.
186 * @param p_mutex mutex to destroy
187 * @return always succeeds
189 void vlc_mutex_destroy (vlc_mutex_t *p_mutex)
191 int val = pthread_mutex_destroy( p_mutex );
192 VLC_THREAD_ASSERT ("destroying mutex");
196 # ifdef HAVE_VALGRIND_VALGRIND_H
197 # include <valgrind/valgrind.h>
199 # define RUNNING_ON_VALGRIND (0)
202 void vlc_assert_locked (vlc_mutex_t *p_mutex)
204 if (RUNNING_ON_VALGRIND > 0)
206 assert (pthread_mutex_lock (p_mutex) == EDEADLK);
211 * Acquires a mutex. If needed, waits for any other thread to release it.
212 * Beware of deadlocks when locking multiple mutexes at the same time,
213 * or when using mutexes from callbacks.
214 * This function is not a cancellation-point.
216 * @param p_mutex mutex initialized with vlc_mutex_init() or
217 * vlc_mutex_init_recursive()
219 void vlc_mutex_lock (vlc_mutex_t *p_mutex)
221 int val = pthread_mutex_lock( p_mutex );
222 VLC_THREAD_ASSERT ("locking mutex");
226 * Acquires a mutex if and only if it is not currently held by another thread.
227 * This function never sleeps and can be used in delay-critical code paths.
228 * This function is not a cancellation-point.
230 * <b>Beware</b>: If this function fails, then the mutex is held... by another
231 * thread. The calling thread must deal with the error appropriately. That
232 * typically implies postponing the operations that would have required the
233 * mutex. If the thread cannot defer those operations, then it must use
234 * vlc_mutex_lock(). If in doubt, use vlc_mutex_lock() instead.
236 * @param p_mutex mutex initialized with vlc_mutex_init() or
237 * vlc_mutex_init_recursive()
238 * @return 0 if the mutex could be acquired, an error code otherwise.
240 int vlc_mutex_trylock (vlc_mutex_t *p_mutex)
242 int val = pthread_mutex_trylock( p_mutex );
245 VLC_THREAD_ASSERT ("locking mutex");
250 * Releases a mutex (or crashes if the mutex is not locked by the caller).
251 * @param p_mutex mutex locked with vlc_mutex_lock().
253 void vlc_mutex_unlock (vlc_mutex_t *p_mutex)
255 int val = pthread_mutex_unlock( p_mutex );
256 VLC_THREAD_ASSERT ("unlocking mutex");
259 /*****************************************************************************
260 * vlc_cond_init: initialize a condition variable
261 *****************************************************************************/
262 void vlc_cond_init( vlc_cond_t *p_condvar )
264 pthread_condattr_t attr;
266 if (unlikely(pthread_condattr_init (&attr)))
268 #if !defined (_POSIX_CLOCK_SELECTION)
269 /* Fairly outdated POSIX support (that was defined in 2001) */
270 # define _POSIX_CLOCK_SELECTION (-1)
272 #if (_POSIX_CLOCK_SELECTION >= 0)
273 /* NOTE: This must be the same clock as the one in mtime.c */
274 pthread_condattr_setclock (&attr, CLOCK_MONOTONIC);
277 if (unlikely(pthread_cond_init (p_condvar, &attr)))
279 pthread_condattr_destroy (&attr);
283 * Destroys a condition variable. No threads shall be waiting or signaling the
285 * @param p_condvar condition variable to destroy
287 void vlc_cond_destroy (vlc_cond_t *p_condvar)
289 int val = pthread_cond_destroy( p_condvar );
290 VLC_THREAD_ASSERT ("destroying condition");
294 * Wakes up one thread waiting on a condition variable, if any.
295 * @param p_condvar condition variable
297 void vlc_cond_signal (vlc_cond_t *p_condvar)
299 int val = pthread_cond_signal( p_condvar );
300 VLC_THREAD_ASSERT ("signaling condition variable");
304 * Wakes up all threads (if any) waiting on a condition variable.
305 * @param p_cond condition variable
307 void vlc_cond_broadcast (vlc_cond_t *p_condvar)
309 pthread_cond_broadcast (p_condvar);
313 * Waits for a condition variable. The calling thread will be suspended until
314 * another thread calls vlc_cond_signal() or vlc_cond_broadcast() on the same
315 * condition variable, the thread is cancelled with vlc_cancel(), or the
316 * system causes a "spurious" unsolicited wake-up.
318 * A mutex is needed to wait on a condition variable. It must <b>not</b> be
319 * a recursive mutex. Although it is possible to use the same mutex for
320 * multiple condition, it is not valid to use different mutexes for the same
321 * condition variable at the same time from different threads.
323 * In case of thread cancellation, the mutex is always locked before
324 * cancellation proceeds.
326 * The canonical way to use a condition variable to wait for event foobar is:
328 vlc_mutex_lock (&lock);
329 mutex_cleanup_push (&lock); // release the mutex in case of cancellation
332 vlc_cond_wait (&wait, &lock);
334 --- foobar is now true, do something about it here --
336 vlc_cleanup_run (); // release the mutex
339 * @param p_condvar condition variable to wait on
340 * @param p_mutex mutex which is unlocked while waiting,
341 * then locked again when waking up.
342 * @param deadline <b>absolute</b> timeout
344 * @return 0 if the condition was signaled, an error code in case of timeout.
346 void vlc_cond_wait (vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex)
348 int val = pthread_cond_wait( p_condvar, p_mutex );
349 VLC_THREAD_ASSERT ("waiting on condition");
353 * Waits for a condition variable up to a certain date.
354 * This works like vlc_cond_wait(), except for the additional timeout.
356 * @param p_condvar condition variable to wait on
357 * @param p_mutex mutex which is unlocked while waiting,
358 * then locked again when waking up.
359 * @param deadline <b>absolute</b> timeout
361 * @return 0 if the condition was signaled, an error code in case of timeout.
363 int vlc_cond_timedwait (vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex,
366 #if defined(__APPLE__) && !defined(__powerpc__) && !defined( __ppc__ ) && !defined( __ppc64__ )
367 /* mdate() is mac_absolute_time on OSX, which we must convert to do
368 * the same base than gettimeofday() which pthread_cond_timedwait
370 mtime_t oldbase = mdate();
372 gettimeofday(&tv, NULL);
373 mtime_t newbase = (mtime_t)tv.tv_sec * 1000000 + (mtime_t) tv.tv_usec;
374 deadline = deadline - oldbase + newbase;
376 lldiv_t d = lldiv( deadline, CLOCK_FREQ );
377 struct timespec ts = { d.quot, d.rem * (1000000000 / CLOCK_FREQ) };
379 int val = pthread_cond_timedwait (p_condvar, p_mutex, &ts);
380 if (val != ETIMEDOUT)
381 VLC_THREAD_ASSERT ("timed-waiting on condition");
386 * Initializes a semaphore.
388 void vlc_sem_init (vlc_sem_t *sem, unsigned value)
390 if (unlikely(sem_init (sem, 0, value)))
395 * Destroys a semaphore.
397 void vlc_sem_destroy (vlc_sem_t *sem)
399 if (likely(sem_destroy (sem) == 0))
403 VLC_THREAD_ASSERT ("destroying semaphore");
407 * Increments the value of a semaphore.
408 * @return 0 on success, EOVERFLOW in case of integer overflow
410 int vlc_sem_post (vlc_sem_t *sem)
412 if (likely(sem_post (sem) == 0))
416 if (unlikely(val != EOVERFLOW))
417 VLC_THREAD_ASSERT ("unlocking semaphore");
422 * Atomically wait for the semaphore to become non-zero (if needed),
423 * then decrements it.
425 void vlc_sem_wait (vlc_sem_t *sem)
430 if (likely(sem_wait (sem) == 0))
432 while ((val = errno) == EINTR);
434 VLC_THREAD_ASSERT ("locking semaphore");
438 * Initializes a read/write lock.
440 void vlc_rwlock_init (vlc_rwlock_t *lock)
442 if (unlikely(pthread_rwlock_init (lock, NULL)))
447 * Destroys an initialized unused read/write lock.
449 void vlc_rwlock_destroy (vlc_rwlock_t *lock)
451 int val = pthread_rwlock_destroy (lock);
452 VLC_THREAD_ASSERT ("destroying R/W lock");
456 * Acquires a read/write lock for reading. Recursion is allowed.
458 void vlc_rwlock_rdlock (vlc_rwlock_t *lock)
460 int val = pthread_rwlock_rdlock (lock);
461 VLC_THREAD_ASSERT ("acquiring R/W lock for reading");
465 * Acquires a read/write lock for writing. Recursion is not allowed.
467 void vlc_rwlock_wrlock (vlc_rwlock_t *lock)
469 int val = pthread_rwlock_wrlock (lock);
470 VLC_THREAD_ASSERT ("acquiring R/W lock for writing");
474 * Releases a read/write lock.
476 void vlc_rwlock_unlock (vlc_rwlock_t *lock)
478 int val = pthread_rwlock_unlock (lock);
479 VLC_THREAD_ASSERT ("releasing R/W lock");
483 * Allocates a thread-specific variable.
484 * @param key where to store the thread-specific variable handle
485 * @param destr a destruction callback. It is called whenever a thread exits
486 * and the thread-specific variable has a non-NULL value.
487 * @return 0 on success, a system error code otherwise. This function can
488 * actually fail because there is a fixed limit on the number of
489 * thread-specific variable in a process on most systems.
491 int vlc_threadvar_create (vlc_threadvar_t *key, void (*destr) (void *))
493 return pthread_key_create (key, destr);
496 void vlc_threadvar_delete (vlc_threadvar_t *p_tls)
498 pthread_key_delete (*p_tls);
502 * Sets a thread-specific variable.
503 * @param key thread-local variable key (created with vlc_threadvar_create())
504 * @param value new value for the variable for the calling thread
505 * @return 0 on success, a system error code otherwise.
507 int vlc_threadvar_set (vlc_threadvar_t key, void *value)
509 return pthread_setspecific (key, value);
513 * Gets the value of a thread-local variable for the calling thread.
514 * This function cannot fail.
515 * @return the value associated with the given variable for the calling
516 * or NULL if there is no value.
518 void *vlc_threadvar_get (vlc_threadvar_t key)
520 return pthread_getspecific (key);
523 static bool rt_priorities = false;
524 static int rt_offset;
526 void vlc_threads_setup (libvlc_int_t *p_libvlc)
528 static vlc_mutex_t lock = VLC_STATIC_MUTEX;
529 static bool initialized = false;
531 vlc_mutex_lock (&lock);
532 /* Initializes real-time priorities before any thread is created,
533 * just once per process. */
537 if (config_GetInt (p_libvlc, "rt-priority"))
540 rt_offset = config_GetInt (p_libvlc, "rt-offset");
541 rt_priorities = true;
545 vlc_mutex_unlock (&lock);
549 * Creates and starts new thread.
551 * @param p_handle [OUT] pointer to write the handle of the created thread to
552 * @param entry entry point for the thread
553 * @param data data parameter given to the entry point
554 * @param priority thread priority value
555 * @return 0 on success, a standard error code on error.
557 int vlc_clone (vlc_thread_t *p_handle, void * (*entry) (void *), void *data,
563 pthread_attr_init (&attr);
565 /* Block the signals that signals interface plugin handles.
566 * If the LibVLC caller wants to handle some signals by itself, it should
567 * block these before whenever invoking LibVLC. And it must obviously not
568 * start the VLC signals interface plugin.
570 * LibVLC will normally ignore any interruption caused by an asynchronous
571 * signal during a system call. But there may well be some buggy cases
572 * where it fails to handle EINTR (bug reports welcome). Some underlying
573 * libraries might also not handle EINTR properly.
579 sigdelset (&set, SIGHUP);
580 sigaddset (&set, SIGINT);
581 sigaddset (&set, SIGQUIT);
582 sigaddset (&set, SIGTERM);
584 sigaddset (&set, SIGPIPE); /* We don't want this one, really! */
585 pthread_sigmask (SIG_BLOCK, &set, &oldset);
588 #if defined (_POSIX_PRIORITY_SCHEDULING) && (_POSIX_PRIORITY_SCHEDULING >= 0) \
589 && defined (_POSIX_THREAD_PRIORITY_SCHEDULING) \
590 && (_POSIX_THREAD_PRIORITY_SCHEDULING >= 0)
593 struct sched_param sp = { .sched_priority = priority + rt_offset, };
596 if (sp.sched_priority <= 0)
597 sp.sched_priority += sched_get_priority_max (policy = SCHED_OTHER);
599 sp.sched_priority += sched_get_priority_min (policy = SCHED_RR);
601 pthread_attr_setschedpolicy (&attr, policy);
602 pthread_attr_setschedparam (&attr, &sp);
608 /* The thread stack size.
609 * The lower the value, the less address space per thread, the highest
610 * maximum simultaneous threads per process. Too low values will cause
611 * stack overflows and weird crashes. Set with caution. Also keep in mind
612 * that 64-bits platforms consume more stack than 32-bits one.
614 * Thanks to on-demand paging, thread stack size only affects address space
615 * consumption. In terms of memory, threads only use what they need
616 * (rounded up to the page boundary).
618 * For example, on Linux i386, the default is 2 mega-bytes, which supports
619 * about 320 threads per processes. */
620 #define VLC_STACKSIZE (128 * sizeof (void *) * 1024)
623 ret = pthread_attr_setstacksize (&attr, VLC_STACKSIZE);
624 assert (ret == 0); /* fails iif VLC_STACKSIZE is invalid */
627 ret = pthread_create (p_handle, &attr, entry, data);
628 pthread_sigmask (SIG_SETMASK, &oldset, NULL);
629 pthread_attr_destroy (&attr);
634 * Marks a thread as cancelled. Next time the target thread reaches a
635 * cancellation point (while not having disabled cancellation), it will
636 * run its cancellation cleanup handler, the thread variable destructors, and
637 * terminate. vlc_join() must be used afterward regardless of a thread being
640 void vlc_cancel (vlc_thread_t thread_id)
642 pthread_cancel (thread_id);
646 * Waits for a thread to complete (if needed), then destroys it.
647 * This is a cancellation point; in case of cancellation, the join does _not_
650 * A thread cannot join itself (normally VLC will abort if this is attempted).
651 * Also, a detached thread <b>cannot</b> be joined.
653 * @param handle thread handle
654 * @param p_result [OUT] pointer to write the thread return value or NULL
656 void vlc_join (vlc_thread_t handle, void **result)
658 int val = pthread_join (handle, result);
659 VLC_THREAD_ASSERT ("joining thread");
663 * Detaches a thread. When the specified thread completes, it will be
664 * automatically destroyed (in particular, its stack will be reclaimed),
665 * instead of waiting for another thread to call vlc_join(). If the thread has
666 * already completed, it will be destroyed immediately.
668 * When a thread performs some work asynchronously and may complete much
669 * earlier than it can be joined, detaching the thread can save memory.
670 * However, care must be taken that any resources used by a detached thread
671 * remains valid until the thread completes. This will typically involve some
672 * kind of thread-safe signaling.
674 * A thread may detach itself.
676 * @param handle thread handle
678 void vlc_detach (vlc_thread_t handle)
680 int val = pthread_detach (handle);
681 VLC_THREAD_ASSERT ("detaching thread");
685 * Save the current cancellation state (enabled or disabled), then disable
686 * cancellation for the calling thread.
687 * This function must be called before entering a piece of code that is not
688 * cancellation-safe, unless it can be proven that the calling thread will not
690 * @return Previous cancellation state (opaque value for vlc_restorecancel()).
692 int vlc_savecancel (void)
695 int val = pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &state);
697 VLC_THREAD_ASSERT ("saving cancellation");
702 * Restore the cancellation state for the calling thread.
703 * @param state previous state as returned by vlc_savecancel().
704 * @return Nothing, always succeeds.
706 void vlc_restorecancel (int state)
711 val = pthread_setcancelstate (state, &oldstate);
712 /* This should fail if an invalid value for given for state */
713 VLC_THREAD_ASSERT ("restoring cancellation");
715 if (unlikely(oldstate != PTHREAD_CANCEL_DISABLE))
716 vlc_thread_fatal ("restoring cancellation while not disabled", EINVAL,
717 __func__, __FILE__, __LINE__);
719 pthread_setcancelstate (state, NULL);
724 * Issues an explicit deferred cancellation point.
725 * This has no effect if thread cancellation is disabled.
726 * This can be called when there is a rather slow non-sleeping operation.
727 * This is also used to force a cancellation point in a function that would
728 * otherwise "not always" be a one (block_FifoGet() is an example).
730 void vlc_testcancel (void)
732 pthread_testcancel ();
735 void vlc_control_cancel (int cmd, ...)
747 void (*func) (void *);
749 mtime_t value, interval;
754 static void *vlc_timer_do (void *data)
756 struct vlc_timer *timer = data;
758 timer->func (timer->data);
760 vlc_mutex_lock (&timer->lock);
761 assert (timer->users > 0);
762 if (--timer->users == 0)
763 vlc_cond_signal (&timer->wait);
764 vlc_mutex_unlock (&timer->lock);
768 static void *vlc_timer_thread (void *data)
770 struct vlc_timer *timer = data;
771 mtime_t value, interval;
773 vlc_mutex_lock (&timer->lock);
774 value = timer->value;
775 interval = timer->interval;
776 vlc_mutex_unlock (&timer->lock);
784 vlc_mutex_lock (&timer->lock);
785 if (vlc_clone (&th, vlc_timer_do, timer, VLC_THREAD_PRIORITY_INPUT))
792 vlc_mutex_unlock (&timer->lock);
802 * Initializes an asynchronous timer.
803 * @warning Asynchronous timers are processed from an unspecified thread.
804 * Also, multiple occurences of an interval timer can run concurrently.
806 * @param id pointer to timer to be initialized
807 * @param func function that the timer will call
808 * @param data parameter for the timer function
809 * @return 0 on success, a system error code otherwise.
811 int vlc_timer_create (vlc_timer_t *id, void (*func) (void *), void *data)
813 struct vlc_timer *timer = malloc (sizeof (*timer));
815 if (unlikely(timer == NULL))
817 vlc_mutex_init (&timer->lock);
818 vlc_cond_init (&timer->wait);
831 * Destroys an initialized timer. If needed, the timer is first disarmed.
832 * This function is undefined if the specified timer is not initialized.
834 * @warning This function <b>must</b> be called before the timer data can be
835 * freed and before the timer callback function can be unloaded.
837 * @param timer timer to destroy
839 void vlc_timer_destroy (vlc_timer_t timer)
841 vlc_timer_schedule (timer, false, 0, 0);
842 vlc_mutex_lock (&timer->lock);
843 while (timer->users != 0)
844 vlc_cond_wait (&timer->wait, &timer->lock);
845 vlc_mutex_unlock (&timer->lock);
847 vlc_cond_destroy (&timer->wait);
848 vlc_mutex_destroy (&timer->lock);
853 * Arm or disarm an initialized timer.
854 * This functions overrides any previous call to itself.
856 * @note A timer can fire later than requested due to system scheduling
857 * limitations. An interval timer can fail to trigger sometimes, either because
858 * the system is busy or suspended, or because a previous iteration of the
859 * timer is still running. See also vlc_timer_getoverrun().
861 * @param timer initialized timer
862 * @param absolute the timer value origin is the same as mdate() if true,
863 * the timer value is relative to now if false.
864 * @param value zero to disarm the timer, otherwise the initial time to wait
865 * before firing the timer.
866 * @param interval zero to fire the timer just once, otherwise the timer
867 * repetition interval.
869 void vlc_timer_schedule (vlc_timer_t timer, bool absolute,
870 mtime_t value, mtime_t interval)
872 static vlc_mutex_t lock = VLC_STATIC_MUTEX;
874 vlc_mutex_lock (&lock);
875 vlc_mutex_lock (&timer->lock);
878 vlc_mutex_unlock (&timer->lock);
879 vlc_cancel (timer->thread);
880 vlc_join (timer->thread, NULL);
881 vlc_mutex_lock (&timer->lock);
885 && (vlc_clone (&timer->thread, vlc_timer_thread, timer,
886 VLC_THREAD_PRIORITY_INPUT) == 0))
888 timer->value = (absolute ? 0 : mdate ()) + value;
889 timer->interval = interval;
891 vlc_mutex_unlock (&timer->lock);
892 vlc_mutex_unlock (&lock);
896 * Fetch and reset the overrun counter for a timer.
897 * @param timer initialized timer
898 * @return the timer overrun counter, i.e. the number of times that the timer
899 * should have run but did not since the last actual run. If all is well, this
902 unsigned vlc_timer_getoverrun (vlc_timer_t timer)
906 vlc_mutex_lock (&timer->lock);
907 ret = timer->overruns;
909 vlc_mutex_unlock (&timer->lock);