1 /*****************************************************************************
2 * vlc_threads.h : threads implementation for the VideoLAN client
3 * This header provides portable declarations for mutexes & conditions
4 *****************************************************************************
5 * Copyright (C) 1999, 2002 VLC authors and VideoLAN
6 * Copyright © 2007-2016 Rémi Denis-Courmont
8 * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
9 * Samuel Hocevar <sam@via.ecp.fr>
10 * Gildas Bazin <gbazin@netcourrier.com>
11 * Christophe Massiot <massiot@via.ecp.fr>
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU Lesser General Public License as published by
15 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public License
24 * along with this program; if not, write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 *****************************************************************************/
28 #ifndef VLC_THREADS_H_
29 #define VLC_THREADS_H_
33 * \defgroup thread Threads and synchronization primitives
36 * Thread primitive declarations
40 * Issues an explicit deferred cancellation point.
42 * This has no effects if thread cancellation is disabled.
43 * This can be called when there is a rather slow non-sleeping operation.
44 * This is also used to force a cancellation point in a function that would
45 * otherwise <em>not always</em> be one (block_FifoGet() is an example).
47 VLC_API
void vlc_testcancel(void);
52 # define ETIMEDOUT 10060 /* This is the value in winsock.h. */
55 typedef struct vlc_thread
*vlc_thread_t
;
56 # define VLC_THREAD_CANCELED NULL
57 # define LIBVLC_NEED_SLEEP
66 unsigned long contention
;
68 CRITICAL_SECTION mutex
;
71 #define VLC_STATIC_MUTEX { false, { { false, 0 } } }
72 #define LIBVLC_NEED_CONDVAR
73 #define LIBVLC_NEED_SEMAPHORE
74 #define LIBVLC_NEED_RWLOCK
75 typedef INIT_ONCE vlc_once_t
;
76 #define VLC_STATIC_ONCE INIT_ONCE_STATIC_INIT
77 typedef struct vlc_threadvar
*vlc_threadvar_t
;
78 typedef struct vlc_timer
*vlc_timer_t
;
80 # define VLC_THREAD_PRIORITY_LOW 0
81 # define VLC_THREAD_PRIORITY_INPUT THREAD_PRIORITY_ABOVE_NORMAL
82 # define VLC_THREAD_PRIORITY_AUDIO THREAD_PRIORITY_HIGHEST
83 # define VLC_THREAD_PRIORITY_VIDEO 0
84 # define VLC_THREAD_PRIORITY_OUTPUT THREAD_PRIORITY_ABOVE_NORMAL
85 # define VLC_THREAD_PRIORITY_HIGHEST THREAD_PRIORITY_TIME_CRITICAL
87 static inline int vlc_poll(struct pollfd
*fds
, unsigned nfds
, int timeout
)
92 val
= poll(fds
, nfds
, timeout
);
97 # define poll(u,n,t) vlc_poll(u, n, t)
99 #elif defined (__OS2__)
102 typedef struct vlc_thread
*vlc_thread_t
;
103 #define VLC_THREAD_CANCELED NULL
112 unsigned long contention
;
117 #define VLC_STATIC_MUTEX { false, { { false, 0 } } }
125 #define VLC_STATIC_COND { NULLHANDLE, 0, NULLHANDLE, 0 }
126 #define LIBVLC_NEED_SEMAPHORE
127 #define LIBVLC_NEED_RWLOCK
128 typedef struct vlc_threadvar
*vlc_threadvar_t
;
129 typedef struct vlc_timer
*vlc_timer_t
;
131 # define VLC_THREAD_PRIORITY_LOW 0
132 # define VLC_THREAD_PRIORITY_INPUT \
133 MAKESHORT(PRTYD_MAXIMUM / 2, PRTYC_REGULAR)
134 # define VLC_THREAD_PRIORITY_AUDIO MAKESHORT(PRTYD_MAXIMUM, PRTYC_REGULAR)
135 # define VLC_THREAD_PRIORITY_VIDEO 0
136 # define VLC_THREAD_PRIORITY_OUTPUT \
137 MAKESHORT(PRTYD_MAXIMUM / 2, PRTYC_REGULAR)
138 # define VLC_THREAD_PRIORITY_HIGHEST MAKESHORT(0, PRTYC_TIMECRITICAL)
140 # define pthread_sigmask sigprocmask
142 static inline int vlc_poll (struct pollfd
*fds
, unsigned nfds
, int timeout
)
144 static int (*vlc_poll_os2
)(struct pollfd
*, unsigned, int) = NULL
;
149 CHAR szFailed
[CCHMAXPATH
];
151 if (DosLoadModule(szFailed
, sizeof(szFailed
), "vlccore", &hmod
))
154 if (DosQueryProcAddr(hmod
, 0, "_vlc_poll_os2", (PFN
*)&vlc_poll_os2
))
158 return (*vlc_poll_os2
)(fds
, nfds
, timeout
);
160 # define poll(u,n,t) vlc_poll(u, n, t)
162 #elif defined (__ANDROID__) /* pthreads subset without pthread_cancel() */
164 # include <pthread.h>
166 # define LIBVLC_USE_PTHREAD_CLEANUP 1
167 # define LIBVLC_NEED_SLEEP
168 # define LIBVLC_NEED_CONDVAR
169 # define LIBVLC_NEED_SEMAPHORE
170 # define LIBVLC_NEED_RWLOCK
172 typedef struct vlc_thread
*vlc_thread_t
;
173 #define VLC_THREAD_CANCELED NULL
174 typedef pthread_mutex_t vlc_mutex_t
;
175 #define VLC_STATIC_MUTEX PTHREAD_MUTEX_INITIALIZER
176 typedef pthread_once_t vlc_once_t
;
177 #define VLC_STATIC_ONCE PTHREAD_ONCE_INIT
178 typedef pthread_key_t vlc_threadvar_t
;
179 typedef struct vlc_timer
*vlc_timer_t
;
181 # define VLC_THREAD_PRIORITY_LOW 0
182 # define VLC_THREAD_PRIORITY_INPUT 0
183 # define VLC_THREAD_PRIORITY_AUDIO 0
184 # define VLC_THREAD_PRIORITY_VIDEO 0
185 # define VLC_THREAD_PRIORITY_OUTPUT 0
186 # define VLC_THREAD_PRIORITY_HIGHEST 0
188 static inline int vlc_poll (struct pollfd
*fds
, unsigned nfds
, int timeout
)
194 int ugly_timeout
= ((unsigned)timeout
>= 50) ? 50 : timeout
;
196 timeout
-= ugly_timeout
;
199 val
= poll (fds
, nfds
, ugly_timeout
);
201 while (val
== 0 && timeout
!= 0);
206 # define poll(u,n,t) vlc_poll(u, n, t)
208 #elif defined (__APPLE__)
209 # define _APPLE_C_SOURCE 1 /* Proper pthread semantics on OSX */
211 # include <pthread.h>
212 /* Unnamed POSIX semaphores not supported on Mac OS X */
213 # include <mach/semaphore.h>
214 # include <mach/task.h>
215 # define LIBVLC_USE_PTHREAD 1
216 # define LIBVLC_USE_PTHREAD_CLEANUP 1
218 typedef pthread_t vlc_thread_t
;
219 #define VLC_THREAD_CANCELED PTHREAD_CANCELED
220 typedef pthread_mutex_t vlc_mutex_t
;
221 #define VLC_STATIC_MUTEX PTHREAD_MUTEX_INITIALIZER
222 typedef pthread_cond_t vlc_cond_t
;
223 #define VLC_STATIC_COND PTHREAD_COND_INITIALIZER
224 typedef semaphore_t vlc_sem_t
;
225 typedef pthread_rwlock_t vlc_rwlock_t
;
226 #define VLC_STATIC_RWLOCK PTHREAD_RWLOCK_INITIALIZER
227 typedef pthread_once_t vlc_once_t
;
228 #define VLC_STATIC_ONCE PTHREAD_ONCE_INIT
229 typedef pthread_key_t vlc_threadvar_t
;
230 typedef struct vlc_timer
*vlc_timer_t
;
232 # define VLC_THREAD_PRIORITY_LOW 0
233 # define VLC_THREAD_PRIORITY_INPUT 22
234 # define VLC_THREAD_PRIORITY_AUDIO 22
235 # define VLC_THREAD_PRIORITY_VIDEO 0
236 # define VLC_THREAD_PRIORITY_OUTPUT 22
237 # define VLC_THREAD_PRIORITY_HIGHEST 22
239 #else /* POSIX threads */
240 # include <unistd.h> /* _POSIX_SPIN_LOCKS */
241 # include <pthread.h>
242 # include <semaphore.h>
245 * Whether LibVLC threads are based on POSIX threads.
247 # define LIBVLC_USE_PTHREAD 1
250 * Whether LibVLC thread cancellation is based on POSIX threads.
252 # define LIBVLC_USE_PTHREAD_CLEANUP 1
263 * Return value of a canceled thread.
265 #define VLC_THREAD_CANCELED PTHREAD_CANCELED
270 * Storage space for a mutual exclusion lock.
272 typedef pthread_mutex_t vlc_mutex_t
;
275 * Static initializer for (static) mutex.
277 #define VLC_STATIC_MUTEX PTHREAD_MUTEX_INITIALIZER
280 * Condition variable.
282 * Storage space for a thread condition variable.
284 typedef pthread_cond_t vlc_cond_t
;
287 * Static initializer for (static) condition variable.
290 * The condition variable will use the default clock, which is OS-dependent.
291 * Therefore, where timed waits are necessary the condition variable should
292 * always be initialized dynamically explicit instead of using this
295 #define VLC_STATIC_COND PTHREAD_COND_INITIALIZER
300 * Storage space for a thread-safe semaphore.
302 typedef sem_t vlc_sem_t
;
307 * Storage space for a slim reader/writer lock.
309 typedef pthread_rwlock_t vlc_rwlock_t
;
312 * Static initializer for (static) read/write lock.
314 #define VLC_STATIC_RWLOCK PTHREAD_RWLOCK_INITIALIZER
317 * One-time initialization.
319 * A one-time initialization object must always be initialized assigned to
320 * \ref VLC_STATIC_ONCE before use.
322 typedef pthread_once_t vlc_once_t
;
325 * Static initializer for one-time initialization.
327 #define VLC_STATIC_ONCE PTHREAD_ONCE_INIT
330 * Thread-local key handle.
332 typedef pthread_key_t vlc_threadvar_t
;
335 * Threaded timer handle.
337 typedef struct vlc_timer
*vlc_timer_t
;
339 # define VLC_THREAD_PRIORITY_LOW 0
340 # define VLC_THREAD_PRIORITY_INPUT 10
341 # define VLC_THREAD_PRIORITY_AUDIO 5
342 # define VLC_THREAD_PRIORITY_VIDEO 0
343 # define VLC_THREAD_PRIORITY_OUTPUT 15
344 # define VLC_THREAD_PRIORITY_HIGHEST 20
348 #ifdef LIBVLC_NEED_CONDVAR
353 # define VLC_STATIC_COND { 0 }
356 #ifdef LIBVLC_NEED_SEMAPHORE
357 typedef struct vlc_sem
365 #ifdef LIBVLC_NEED_RWLOCK
366 typedef struct vlc_rwlock
372 # define VLC_STATIC_RWLOCK { VLC_STATIC_MUTEX, VLC_STATIC_COND, 0 }
376 * Initializes a fast mutex.
378 * Recursive locking of a fast mutex is undefined behaviour. (In debug builds,
379 * recursive locking will cause an assertion failure.)
381 VLC_API
void vlc_mutex_init(vlc_mutex_t
*);
384 * Initializes a recursive mutex.
385 * \warning This is strongly discouraged. Please use normal mutexes.
387 VLC_API
void vlc_mutex_init_recursive(vlc_mutex_t
*);
390 * Deinitializes a mutex.
392 * The mutex must not be locked, otherwise behaviour is undefined.
394 VLC_API
void vlc_mutex_destroy(vlc_mutex_t
*);
399 * If needed, this waits for any other thread to release it.
401 * \warning Beware of deadlocks when locking multiple mutexes at the same time,
402 * or when using mutexes from callbacks.
404 * \note This function is not a cancellation point.
406 VLC_API
void vlc_mutex_lock(vlc_mutex_t
*);
409 * Tries to acquire a mutex.
411 * This function acquires the mutex if and only if it is not currently held by
412 * another thread. This function never sleeps and can be used in delay-critical
415 * \note This function is not a cancellation point.
417 * \warning If this function fails, then the mutex is held... by another
418 * thread. The calling thread must deal with the error appropriately. That
419 * typically implies postponing the operations that would have required the
420 * mutex. If the thread cannot defer those operations, then it must use
421 * vlc_mutex_lock(). If in doubt, use vlc_mutex_lock() instead.
423 * @return 0 if the mutex could be acquired, an error code otherwise.
425 VLC_API
int vlc_mutex_trylock( vlc_mutex_t
* ) VLC_USED
;
430 * If the mutex is not held by the calling thread, the behaviour is undefined.
432 * \note This function is not a cancellation point.
434 VLC_API
void vlc_mutex_unlock(vlc_mutex_t
*);
437 * Initializes a condition variable.
439 VLC_API
void vlc_cond_init(vlc_cond_t
*);
442 * Initializes a condition variable (wall clock).
444 * This function initializes a condition variable for timed waiting using the
445 * UTC wall clock time. The time reference is the same as with time() and with
446 * timespec_get() and TIME_UTC.
447 * vlc_cond_timedwait_daytime() must be instead of
448 * vlc_cond_timedwait() for actual waiting.
450 void vlc_cond_init_daytime(vlc_cond_t
*);
453 * Deinitializes a condition variable.
455 * No threads shall be waiting or signaling the condition, otherwise the
456 * behavior is undefined.
458 VLC_API
void vlc_cond_destroy(vlc_cond_t
*);
461 * Wakes up one thread waiting on a condition variable.
463 * If any thread is currently waiting on the condition variable, at least one
464 * of those threads will be woken up. Otherwise, this function has no effects.
466 * \note This function is not a cancellation point.
468 VLC_API
void vlc_cond_signal(vlc_cond_t
*);
471 * Wakes up all threads waiting on a condition variable.
473 * \note This function is not a cancellation point.
475 VLC_API
void vlc_cond_broadcast(vlc_cond_t
*);
478 * Waits on a condition variable.
480 * The calling thread will be suspended until another thread calls
481 * vlc_cond_signal() or vlc_cond_broadcast() on the same condition variable,
482 * the thread is cancelled with vlc_cancel(), or the system causes a
483 * <em>spurious</em> unsolicited wake-up.
485 * A mutex is needed to wait on a condition variable. It must <b>not</b> be
486 * a recursive mutex. Although it is possible to use the same mutex for
487 * multiple condition, it is not valid to use different mutexes for the same
488 * condition variable at the same time from different threads.
490 * The canonical way to use a condition variable to wait for event foobar is:
492 vlc_mutex_lock(&lock);
493 mutex_cleanup_push(&lock); // release the mutex in case of cancellation
496 vlc_cond_wait(&wait, &lock);
498 // -- foobar is now true, do something about it here --
501 vlc_mutex_unlock(&lock);
504 * \note This function is a cancellation point. In case of thread cancellation,
505 * the mutex is always locked before cancellation proceeds.
507 * \param cond condition variable to wait on
508 * \param mutex mutex which is unlocked while waiting,
509 * then locked again when waking up.
511 VLC_API
void vlc_cond_wait(vlc_cond_t
*cond
, vlc_mutex_t
*mutex
);
514 * Waits on a condition variable up to a certain date.
516 * This works like vlc_cond_wait() but with an additional time-out.
517 * The time-out is expressed as an absolute timestamp using the same arbitrary
518 * time reference as the mdate() and mwait() functions.
520 * \note This function is a cancellation point. In case of thread cancellation,
521 * the mutex is always locked before cancellation proceeds.
523 * \param cond condition variable to wait on
524 * \param mutex mutex which is unlocked while waiting,
525 * then locked again when waking up
526 * \param deadline <b>absolute</b> timeout
528 * \warning If the variable was initialized with vlc_cond_init_daytime(), or
529 * was statically initialized with \ref VLC_STATIC_COND, the time reference
530 * used by this function is unspecified (depending on the implementation, it
531 * might be the Unix epoch or the mdate() clock).
533 * \return 0 if the condition was signaled, an error code in case of timeout.
535 VLC_API
int vlc_cond_timedwait(vlc_cond_t
*cond
, vlc_mutex_t
*mutex
,
538 int vlc_cond_timedwait_daytime(vlc_cond_t
*, vlc_mutex_t
*, time_t);
541 * Initializes a semaphore.
543 * @param count initial semaphore value (typically 0)
545 VLC_API
void vlc_sem_init(vlc_sem_t
*, unsigned count
);
548 * Deinitializes a semaphore.
550 VLC_API
void vlc_sem_destroy(vlc_sem_t
*);
553 * Increments the value of a semaphore.
555 * \note This function is not a cancellation point.
557 * \return 0 on success, EOVERFLOW in case of integer overflow.
559 VLC_API
int vlc_sem_post(vlc_sem_t
*);
562 * Waits on a semaphore.
564 * This function atomically waits for the semaphore to become non-zero then
565 * decrements it, and returns. If the semaphore is non-zero on entry, it is
566 * immediately decremented.
568 * \note This function may be a point of cancellation.
570 VLC_API
void vlc_sem_wait(vlc_sem_t
*);
573 * Initializes a read/write lock.
575 VLC_API
void vlc_rwlock_init(vlc_rwlock_t
*);
578 * Destroys an initialized unused read/write lock.
580 VLC_API
void vlc_rwlock_destroy(vlc_rwlock_t
*);
583 * Acquires a read/write lock for reading.
585 * \note Recursion is allowed.
586 * \note This function may be a point of cancellation.
588 VLC_API
void vlc_rwlock_rdlock(vlc_rwlock_t
*);
591 * Acquires a read/write lock for writing. Recursion is not allowed.
592 * \note This function may be a point of cancellation.
594 VLC_API
void vlc_rwlock_wrlock(vlc_rwlock_t
*);
597 * Releases a read/write lock.
599 * The calling thread must hold the lock. Otherwise behaviour is undefined.
601 * \note This function is not a cancellation point.
603 VLC_API
void vlc_rwlock_unlock(vlc_rwlock_t
*);
606 * Executes a function one time.
608 * The first time this function is called with a given one-time initialization
609 * object, it executes the provided callback.
610 * Any further call with the same object will be a no-op.
612 * In the corner case that the first time execution is ongoing in another
613 * thread, then the function will wait for completion on the other thread
614 * (and then synchronize memory) before it returns.
615 * This ensures that, no matter what, the callback has been executed exactly
616 * once and its side effects are visible after the function returns.
618 * \param once a one-time initialization object
619 * \param cb callback to execute (the first time)
621 VLC_API
void vlc_once(vlc_once_t
*restrict once
, void (*cb
)(void));
624 * Allocates a thread-specific variable.
626 * @param key where to store the thread-specific variable handle
627 * @param destr a destruction callback. It is called whenever a thread exits
628 * and the thread-specific variable has a non-NULL value.
630 * @return 0 on success, a system error code otherwise.
631 * This function can actually fail: on most systems, there is a fixed limit to
632 * the number of thread-specific variables in a given process.
634 VLC_API
int vlc_threadvar_create(vlc_threadvar_t
*key
, void (*destr
) (void *));
637 * Deallocates a thread-specific variable.
639 VLC_API
void vlc_threadvar_delete(vlc_threadvar_t
*);
642 * Sets a thread-specific variable.
644 * \param key thread-local variable key (created with vlc_threadvar_create())
645 * \param value new value for the variable for the calling thread
646 * \return 0 on success, a system error code otherwise.
648 VLC_API
int vlc_threadvar_set(vlc_threadvar_t key
, void *value
);
651 * Gets the value of a thread-local variable for the calling thread.
652 * This function cannot fail.
654 * \return the value associated with the given variable for the calling
655 * or NULL if no value was set.
657 VLC_API
void *vlc_threadvar_get(vlc_threadvar_t
);
660 * Waits on an address.
662 * Puts the calling thread to sleep if a specific value is stored at a
663 * specified address. The thread will sleep until it is woken up by a call to
664 * vlc_addr_signal() or vlc_addr_broadcast() in another thread, or spuriously.
666 * If the value does not match, do nothing and return immediately.
668 * \param addr address to check for
669 * \param val value to match at the address
671 void vlc_addr_wait(void *addr
, unsigned val
);
674 * Waits on an address with a time-out.
676 * This function operates as vlc_addr_wait() but provides an additional
677 * time-out. If the time-out elapses, the thread resumes and the function
680 * \param addr address to check for
681 * \param val value to match at the address
682 * \param delay time-out duration
684 * \return true if the function was woken up before the time-out,
685 * false if the time-out elapsed.
687 bool vlc_addr_timedwait(void *addr
, unsigned val
, mtime_t delay
);
690 * Wakes up one thread on an address.
692 * Wakes up (at least) one of the thread sleeping on the specified address.
693 * The address must be equal to the first parameter given by at least one
694 * thread sleeping within the vlc_addr_wait() or vlc_addr_timedwait()
695 * functions. If no threads are found, this function does nothing.
697 * \param addr address identifying which threads may be woken up
699 void vlc_addr_signal(void *addr
);
702 * Wakes up all thread on an address.
704 * Wakes up all threads sleeping on the specified address (if any).
705 * Any thread sleeping within a call to vlc_addr_wait() or vlc_addr_timedwait()
706 * with the specified address as first call parameter will be woken up.
708 * \param addr address identifying which threads to wake up
710 void vlc_addr_broadcast(void *addr
);
713 * Creates and starts a new thread.
715 * The thread must be <i>joined</i> with vlc_join() to reclaim resources
716 * when it is not needed anymore.
718 * @param th storage space for the handle of the new thread (cannot be NULL)
720 * @param entry entry point for the thread
721 * @param data data parameter given to the entry point
722 * @param priority thread priority value
723 * @return 0 on success, a standard error code on error.
724 * @note In case of error, the value of *th is undefined.
726 VLC_API
int vlc_clone(vlc_thread_t
*th
, void *(*entry
)(void *), void *data
,
727 int priority
) VLC_USED
;
730 * Marks a thread as cancelled.
732 * Next time the target thread reaches a cancellation point (while not having
733 * disabled cancellation), it will run its cancellation cleanup handler, the
734 * thread variable destructors, and terminate.
736 * vlc_join() must be used regardless of a thread being cancelled or not, to
737 * avoid leaking resources.
739 VLC_API
void vlc_cancel(vlc_thread_t
);
742 * Waits for a thread to complete (if needed), then destroys it.
744 * \note This is a cancellation point. In case of cancellation, the thread is
747 * \warning A thread cannot join itself (normally VLC will abort if this is
748 * attempted). Also a detached thread <b>cannot</b> be joined.
750 * @param th thread handle
751 * @param result [OUT] pointer to write the thread return value or NULL
753 VLC_API
void vlc_join(vlc_thread_t th
, void **result
);
756 * Disables thread cancellation.
758 * This functions saves the current cancellation state (enabled or disabled),
759 * then disables cancellation for the calling thread. It must be called before
760 * entering a piece of code that is not cancellation-safe, unless it can be
761 * proven that the calling thread will not be cancelled.
763 * \note This function is not a cancellation point.
765 * \return Previous cancellation state (opaque value for vlc_restorecancel()).
767 VLC_API
int vlc_savecancel(void);
770 * Restores the cancellation state.
772 * This function restores the cancellation state of the calling thread to
773 * a state previously saved by vlc_savecancel().
775 * \note This function is not a cancellation point.
777 * \param state previous state as returned by vlc_savecancel().
779 VLC_API
void vlc_restorecancel(int state
);
782 * Internal handler for thread cancellation.
784 * Do not call this function directly. Use wrapper macros instead:
785 * vlc_cleanup_push(), vlc_cleanup_pop().
787 VLC_API
void vlc_control_cancel(int cmd
, ...);
792 * This function returns the thread handle of the calling thread.
794 * \note The exact type of the thread handle depends on the platform,
795 * including an integer type, a pointer type or a compound type of any size.
796 * If you need an integer identifier, use vlc_thread_id() instead.
798 * \note vlc_join(vlc_thread_self(), NULL) is undefined,
799 * as it obviously does not make any sense (it might result in a deadlock, but
800 * there are no warranties that it will).
802 * \return the thread handle
804 VLC_API vlc_thread_t
vlc_thread_self(void) VLC_USED
;
809 * This function returns the identifier of the calling thread. The identifier
810 * cannot change for the entire duration of the thread, and no other thread can
811 * have the same identifier at the same time in the same process. Typically,
812 * the identifier is also unique across all running threads of all existing
813 * processes, but that depends on the operating system.
815 * There are no particular semantics to the thread ID with LibVLC.
816 * It is provided mainly for tracing and debugging.
818 * \warning This function is not currently implemented on all supported
819 * platforms. Where not implemented, it returns (unsigned long)-1.
821 * \return the thread identifier (or -1 if unimplemented)
823 VLC_API
unsigned long vlc_thread_id(void) VLC_USED
;
826 * Precision monotonic clock.
828 * In principles, the clock has a precision of 1 MHz. But the actual resolution
829 * may be much lower, especially when it comes to sleeping with mwait() or
830 * msleep(). Most general-purpose operating systems provide a resolution of
831 * only 100 to 1000 Hz.
833 * \warning The origin date (time value "zero") is not specified. It is
834 * typically the time the kernel started, but this is platform-dependent.
835 * If you need wall clock time, use gettimeofday() instead.
837 * \return a timestamp in microseconds.
839 VLC_API mtime_t
mdate(void);
842 * Waits until a deadline.
844 * \param deadline timestamp to wait for (\ref mdate())
846 * \note The deadline may be exceeded due to OS scheduling.
847 * \note This function is a cancellation point.
849 VLC_API
void mwait(mtime_t deadline
);
852 * Waits for an interval of time.
854 * \param delay how long to wait (in microseconds)
856 * \note The delay may be exceeded due to OS scheduling.
857 * \note This function is a cancellation point.
859 VLC_API
void msleep(mtime_t delay
);
861 #define VLC_HARD_MIN_SLEEP (CLOCK_FREQ/100) /* 10 milliseconds = 1 tick at 100Hz */
862 #define VLC_SOFT_MIN_SLEEP (9*CLOCK_FREQ) /* 9 seconds */
864 #if defined (__GNUC__) && !defined (__clang__)
865 /* Linux has 100, 250, 300 or 1000Hz
867 * HZ=100 by default on FreeBSD, but some architectures use a 1000Hz timer
871 __attribute__((unused
))
872 __attribute__((noinline
))
873 __attribute__((error("sorry, cannot sleep for such short a time")))
874 mtime_t
impossible_delay( mtime_t delay
)
877 return VLC_HARD_MIN_SLEEP
;
881 __attribute__((unused
))
882 __attribute__((noinline
))
883 __attribute__((warning("use proper event handling instead of short delay")))
884 mtime_t
harmful_delay( mtime_t delay
)
889 # define check_delay( d ) \
890 ((__builtin_constant_p(d < VLC_HARD_MIN_SLEEP) \
891 && (d < VLC_HARD_MIN_SLEEP)) \
892 ? impossible_delay(d) \
893 : ((__builtin_constant_p(d < VLC_SOFT_MIN_SLEEP) \
894 && (d < VLC_SOFT_MIN_SLEEP)) \
899 __attribute__((unused
))
900 __attribute__((noinline
))
901 __attribute__((error("deadlines can not be constant")))
902 mtime_t
impossible_deadline( mtime_t deadline
)
907 # define check_deadline( d ) \
908 (__builtin_constant_p(d) ? impossible_deadline(d) : d)
910 # define check_delay(d) (d)
911 # define check_deadline(d) (d)
914 #define msleep(d) msleep(check_delay(d))
915 #define mwait(d) mwait(check_deadline(d))
918 * Initializes an asynchronous timer.
920 * \param id pointer to timer to be initialized
921 * \param func function that the timer will call
922 * \param data parameter for the timer function
923 * \return 0 on success, a system error code otherwise.
925 * \warning Asynchronous timers are processed from an unspecified thread.
926 * \note Multiple occurrences of a single interval timer are serialized:
927 * they cannot run concurrently.
929 VLC_API
int vlc_timer_create(vlc_timer_t
*id
, void (*func
)(void *), void *data
)
933 * Destroys an initialized timer.
935 * If needed, the timer is first disarmed. Behaviour is undefined if the
936 * specified timer is not initialized.
938 * \warning This function <b>must</b> be called before the timer data can be
939 * freed and before the timer callback function can be unmapped/unloaded.
941 * \param timer timer to destroy
943 VLC_API
void vlc_timer_destroy(vlc_timer_t timer
);
946 * Arms or disarms an initialized timer.
948 * This functions overrides any previous call to itself.
950 * \note A timer can fire later than requested due to system scheduling
951 * limitations. An interval timer can fail to trigger sometimes, either because
952 * the system is busy or suspended, or because a previous iteration of the
953 * timer is still running. See also vlc_timer_getoverrun().
955 * \param timer initialized timer
956 * \param absolute the timer value origin is the same as mdate() if true,
957 * the timer value is relative to now if false.
958 * \param value zero to disarm the timer, otherwise the initial time to wait
959 * before firing the timer.
960 * \param interval zero to fire the timer just once, otherwise the timer
961 * repetition interval.
963 VLC_API
void vlc_timer_schedule(vlc_timer_t timer
, bool absolute
,
964 mtime_t value
, mtime_t interval
);
967 * Fetches and resets the overrun counter for a timer.
969 * This functions returns the number of times that the interval timer should
970 * have fired, but the callback was not invoked due to scheduling problems.
971 * The call resets the counter to zero.
973 * \param timer initialized timer
974 * \return the timer overrun counter (typically zero)
976 VLC_API
unsigned vlc_timer_getoverrun(vlc_timer_t
) VLC_USED
;
981 * \return number of available (logical) CPUs.
983 VLC_API
unsigned vlc_GetCPUCount(void);
990 VLC_CANCEL_ADDR_CLEAR
,
993 #if defined (LIBVLC_USE_PTHREAD_CLEANUP)
995 * Registers a thread cancellation handler.
997 * This pushes a function to run if the thread is cancelled (or otherwise
998 * exits prematurely).
1000 * If multiple procedures are registered,
1001 * they are handled in last-in first-out order.
1003 * \note Any call to vlc_cleanup_push() <b>must</b> paired with a call to
1004 * vlc_cleanup_pop().
1005 * \warning Branching into or out of the block between these two function calls
1006 * is not allowed (read: it will likely crash the whole process).
1008 * \param routine procedure to call if the thread ends
1009 * \param arg argument for the procedure
1011 # define vlc_cleanup_push( routine, arg ) pthread_cleanup_push (routine, arg)
1014 * Unregisters the last cancellation handler.
1016 * This pops the cancellation handler that was last pushed with
1017 * vlc_cleanup_push() in the calling thread.
1019 # define vlc_cleanup_pop( ) pthread_cleanup_pop (0)
1022 typedef struct vlc_cleanup_t vlc_cleanup_t
;
1024 struct vlc_cleanup_t
1026 vlc_cleanup_t
*next
;
1027 void (*proc
) (void *);
1031 /* This macros opens a code block on purpose. This is needed for multiple
1032 * calls within a single function. This also prevent Win32 developers from
1033 * writing code that would break on POSIX (POSIX opens a block as well). */
1034 # define vlc_cleanup_push( routine, arg ) \
1036 vlc_cleanup_t vlc_cleanup_data = { NULL, routine, arg, }; \
1037 vlc_control_cancel (VLC_CLEANUP_PUSH, &vlc_cleanup_data)
1039 # define vlc_cleanup_pop( ) \
1040 vlc_control_cancel (VLC_CLEANUP_POP); \
1043 #endif /* !LIBVLC_USE_PTHREAD_CLEANUP */
1045 static inline void vlc_cleanup_lock (void *lock
)
1047 vlc_mutex_unlock ((vlc_mutex_t
*)lock
);
1049 #define mutex_cleanup_push( lock ) vlc_cleanup_push (vlc_cleanup_lock, lock)
1051 static inline void vlc_cancel_addr_set(void *addr
)
1053 vlc_control_cancel(VLC_CANCEL_ADDR_SET
, addr
);
1056 static inline void vlc_cancel_addr_clear(void *addr
)
1058 vlc_control_cancel(VLC_CANCEL_ADDR_CLEAR
, addr
);
1063 * Helper C++ class to lock a mutex.
1065 * The mutex is locked when the object is created, and unlocked when the object
1068 class vlc_mutex_locker
1073 vlc_mutex_locker (vlc_mutex_t
*m
) : lock (m
)
1075 vlc_mutex_lock (lock
);
1078 ~vlc_mutex_locker (void)
1080 vlc_mutex_unlock (lock
);
1087 VLC_AVCODEC_MUTEX
= 0,
1091 VLC_HIGHLIGHT_MUTEX
,
1095 /* Insert new entry HERE */
1100 * Internal handler for global mutexes.
1102 * Do not use this function directly. Use helper macros instead:
1103 * vlc_global_lock(), vlc_global_unlock().
1105 VLC_API
void vlc_global_mutex(unsigned, bool);
1108 * Acquires a global mutex.
1110 #define vlc_global_lock( n ) vlc_global_mutex(n, true)
1113 * Releases a global mutex.
1115 #define vlc_global_unlock( n ) vlc_global_mutex(n, false)
1119 #endif /* !_VLC_THREADS_H */