input: add an input_item_t arg to input_CreateFilename()
[vlc.git] / include / vlc_threads.h
blobb751893321991a40233f4aaefec81d6873b14ebb
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_
31 /**
32 * \ingroup os
33 * \defgroup thread Threads and synchronization primitives
34 * @{
35 * \file
36 * Thread primitive declarations
39 /**
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);
49 #if defined (_WIN32)
50 # include <process.h>
51 # ifndef ETIMEDOUT
52 # define ETIMEDOUT 10060 /* This is the value in winsock.h. */
53 # endif
55 typedef struct vlc_thread *vlc_thread_t;
56 # define VLC_THREAD_CANCELED NULL
57 # define LIBVLC_NEED_SLEEP
58 typedef struct
60 bool dynamic;
61 union
63 struct
65 bool locked;
66 unsigned long contention;
68 CRITICAL_SECTION mutex;
70 } vlc_mutex_t;
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)
89 int val;
91 vlc_testcancel();
92 val = poll(fds, nfds, timeout);
93 if (val < 0)
94 vlc_testcancel();
95 return val;
97 # define poll(u,n,t) vlc_poll(u, n, t)
99 #elif defined (__OS2__)
100 # include <errno.h>
102 typedef struct vlc_thread *vlc_thread_t;
103 #define VLC_THREAD_CANCELED NULL
104 typedef struct
106 bool dynamic;
107 union
109 struct
111 bool locked;
112 unsigned long contention;
114 HMTX hmtx;
116 } vlc_mutex_t;
117 #define VLC_STATIC_MUTEX { false, { { false, 0 } } }
118 typedef struct
120 HEV hev;
121 unsigned waiters;
122 HEV hevAck;
123 unsigned signaled;
124 } vlc_cond_t;
125 #define VLC_STATIC_COND { NULLHANDLE, 0, NULLHANDLE, 0 }
126 #define LIBVLC_NEED_SEMAPHORE
127 #define LIBVLC_NEED_RWLOCK
128 typedef struct
130 unsigned done;
131 vlc_mutex_t mutex;
132 } vlc_once_t;
133 #define VLC_STATIC_ONCE { 0, VLC_STATIC_MUTEX }
134 typedef struct vlc_threadvar *vlc_threadvar_t;
135 typedef struct vlc_timer *vlc_timer_t;
137 # define VLC_THREAD_PRIORITY_LOW 0
138 # define VLC_THREAD_PRIORITY_INPUT \
139 MAKESHORT(PRTYD_MAXIMUM / 2, PRTYC_REGULAR)
140 # define VLC_THREAD_PRIORITY_AUDIO MAKESHORT(PRTYD_MAXIMUM, PRTYC_REGULAR)
141 # define VLC_THREAD_PRIORITY_VIDEO 0
142 # define VLC_THREAD_PRIORITY_OUTPUT \
143 MAKESHORT(PRTYD_MAXIMUM / 2, PRTYC_REGULAR)
144 # define VLC_THREAD_PRIORITY_HIGHEST MAKESHORT(0, PRTYC_TIMECRITICAL)
146 # define pthread_sigmask sigprocmask
148 static inline int vlc_poll (struct pollfd *fds, unsigned nfds, int timeout)
150 static int (*vlc_poll_os2)(struct pollfd *, unsigned, int) = NULL;
152 if (!vlc_poll_os2)
154 HMODULE hmod;
155 CHAR szFailed[CCHMAXPATH];
157 if (DosLoadModule(szFailed, sizeof(szFailed), "vlccore", &hmod))
158 return -1;
160 if (DosQueryProcAddr(hmod, 0, "_vlc_poll_os2", (PFN *)&vlc_poll_os2))
161 return -1;
164 return (*vlc_poll_os2)(fds, nfds, timeout);
166 # define poll(u,n,t) vlc_poll(u, n, t)
168 #elif defined (__ANDROID__) /* pthreads subset without pthread_cancel() */
169 # include <unistd.h>
170 # include <pthread.h>
171 # include <poll.h>
172 # define LIBVLC_USE_PTHREAD_CLEANUP 1
173 # define LIBVLC_NEED_SLEEP
174 # define LIBVLC_NEED_CONDVAR
175 # define LIBVLC_NEED_SEMAPHORE
176 # define LIBVLC_NEED_RWLOCK
178 typedef struct vlc_thread *vlc_thread_t;
179 #define VLC_THREAD_CANCELED NULL
180 typedef pthread_mutex_t vlc_mutex_t;
181 #define VLC_STATIC_MUTEX PTHREAD_MUTEX_INITIALIZER
182 typedef pthread_once_t vlc_once_t;
183 #define VLC_STATIC_ONCE PTHREAD_ONCE_INIT
184 typedef pthread_key_t vlc_threadvar_t;
185 typedef struct vlc_timer *vlc_timer_t;
187 # define VLC_THREAD_PRIORITY_LOW 0
188 # define VLC_THREAD_PRIORITY_INPUT 0
189 # define VLC_THREAD_PRIORITY_AUDIO 0
190 # define VLC_THREAD_PRIORITY_VIDEO 0
191 # define VLC_THREAD_PRIORITY_OUTPUT 0
192 # define VLC_THREAD_PRIORITY_HIGHEST 0
194 static inline int vlc_poll (struct pollfd *fds, unsigned nfds, int timeout)
196 int val;
200 int ugly_timeout = ((unsigned)timeout >= 50) ? 50 : timeout;
201 if (timeout >= 0)
202 timeout -= ugly_timeout;
204 vlc_testcancel ();
205 val = poll (fds, nfds, ugly_timeout);
207 while (val == 0 && timeout != 0);
209 return val;
212 # define poll(u,n,t) vlc_poll(u, n, t)
214 #elif defined (__APPLE__)
215 # define _APPLE_C_SOURCE 1 /* Proper pthread semantics on OSX */
216 # include <unistd.h>
217 # include <pthread.h>
218 /* Unnamed POSIX semaphores not supported on Mac OS X */
219 # include <mach/semaphore.h>
220 # include <mach/task.h>
221 # define LIBVLC_USE_PTHREAD 1
222 # define LIBVLC_USE_PTHREAD_CLEANUP 1
224 typedef pthread_t vlc_thread_t;
225 #define VLC_THREAD_CANCELED PTHREAD_CANCELED
226 typedef pthread_mutex_t vlc_mutex_t;
227 #define VLC_STATIC_MUTEX PTHREAD_MUTEX_INITIALIZER
228 typedef pthread_cond_t vlc_cond_t;
229 #define VLC_STATIC_COND PTHREAD_COND_INITIALIZER
230 typedef semaphore_t vlc_sem_t;
231 typedef pthread_rwlock_t vlc_rwlock_t;
232 #define VLC_STATIC_RWLOCK PTHREAD_RWLOCK_INITIALIZER
233 typedef pthread_once_t vlc_once_t;
234 #define VLC_STATIC_ONCE PTHREAD_ONCE_INIT
235 typedef pthread_key_t vlc_threadvar_t;
236 typedef struct vlc_timer *vlc_timer_t;
238 # define VLC_THREAD_PRIORITY_LOW 0
239 # define VLC_THREAD_PRIORITY_INPUT 22
240 # define VLC_THREAD_PRIORITY_AUDIO 22
241 # define VLC_THREAD_PRIORITY_VIDEO 0
242 # define VLC_THREAD_PRIORITY_OUTPUT 22
243 # define VLC_THREAD_PRIORITY_HIGHEST 22
245 #else /* POSIX threads */
246 # include <unistd.h> /* _POSIX_SPIN_LOCKS */
247 # include <pthread.h>
248 # include <semaphore.h>
251 * Whether LibVLC threads are based on POSIX threads.
253 # define LIBVLC_USE_PTHREAD 1
256 * Whether LibVLC thread cancellation is based on POSIX threads.
258 # define LIBVLC_USE_PTHREAD_CLEANUP 1
261 * Thread handle.
263 typedef struct
265 pthread_t handle;
266 } vlc_thread_t;
269 * Return value of a canceled thread.
271 #define VLC_THREAD_CANCELED PTHREAD_CANCELED
274 * Mutex.
276 * Storage space for a mutual exclusion lock.
278 typedef pthread_mutex_t vlc_mutex_t;
281 * Static initializer for (static) mutex.
283 #define VLC_STATIC_MUTEX PTHREAD_MUTEX_INITIALIZER
286 * Condition variable.
288 * Storage space for a thread condition variable.
290 typedef pthread_cond_t vlc_cond_t;
293 * Static initializer for (static) condition variable.
295 * \note
296 * The condition variable will use the default clock, which is OS-dependent.
297 * Therefore, where timed waits are necessary the condition variable should
298 * always be initialized dynamically explicit instead of using this
299 * initializer.
301 #define VLC_STATIC_COND PTHREAD_COND_INITIALIZER
304 * Semaphore.
306 * Storage space for a thread-safe semaphore.
308 typedef sem_t vlc_sem_t;
311 * Read/write lock.
313 * Storage space for a slim reader/writer lock.
315 typedef pthread_rwlock_t vlc_rwlock_t;
318 * Static initializer for (static) read/write lock.
320 #define VLC_STATIC_RWLOCK PTHREAD_RWLOCK_INITIALIZER
323 * One-time initialization.
325 * A one-time initialization object must always be initialized assigned to
326 * \ref VLC_STATIC_ONCE before use.
328 typedef pthread_once_t vlc_once_t;
331 * Static initializer for one-time initialization.
333 #define VLC_STATIC_ONCE PTHREAD_ONCE_INIT
336 * Thread-local key handle.
338 typedef pthread_key_t vlc_threadvar_t;
341 * Threaded timer handle.
343 typedef struct vlc_timer *vlc_timer_t;
345 # define VLC_THREAD_PRIORITY_LOW 0
346 # define VLC_THREAD_PRIORITY_INPUT 10
347 # define VLC_THREAD_PRIORITY_AUDIO 5
348 # define VLC_THREAD_PRIORITY_VIDEO 0
349 # define VLC_THREAD_PRIORITY_OUTPUT 15
350 # define VLC_THREAD_PRIORITY_HIGHEST 20
352 #endif
354 #ifdef LIBVLC_NEED_CONDVAR
355 typedef struct
357 unsigned value;
358 } vlc_cond_t;
359 # define VLC_STATIC_COND { 0 }
360 #endif
362 #ifdef LIBVLC_NEED_SEMAPHORE
363 typedef struct vlc_sem
365 vlc_mutex_t lock;
366 vlc_cond_t wait;
367 unsigned value;
368 } vlc_sem_t;
369 #endif
371 #ifdef LIBVLC_NEED_RWLOCK
372 typedef struct vlc_rwlock
374 vlc_mutex_t mutex;
375 vlc_cond_t wait;
376 long state;
377 } vlc_rwlock_t;
378 # define VLC_STATIC_RWLOCK { VLC_STATIC_MUTEX, VLC_STATIC_COND, 0 }
379 #endif
382 * Initializes a fast mutex.
384 * Recursive locking of a fast mutex is undefined behaviour. (In debug builds,
385 * recursive locking will cause an assertion failure.)
387 VLC_API void vlc_mutex_init(vlc_mutex_t *);
390 * Initializes a recursive mutex.
391 * \warning This is strongly discouraged. Please use normal mutexes.
393 VLC_API void vlc_mutex_init_recursive(vlc_mutex_t *);
396 * Deinitializes a mutex.
398 * The mutex must not be locked, otherwise behaviour is undefined.
400 VLC_API void vlc_mutex_destroy(vlc_mutex_t *);
403 * Acquires a mutex.
405 * If needed, this waits for any other thread to release it.
407 * \warning Beware of deadlocks when locking multiple mutexes at the same time,
408 * or when using mutexes from callbacks.
410 * \note This function is not a cancellation point.
412 VLC_API void vlc_mutex_lock(vlc_mutex_t *);
415 * Tries to acquire a mutex.
417 * This function acquires the mutex if and only if it is not currently held by
418 * another thread. This function never sleeps and can be used in delay-critical
419 * code paths.
421 * \note This function is not a cancellation point.
423 * \warning If this function fails, then the mutex is held... by another
424 * thread. The calling thread must deal with the error appropriately. That
425 * typically implies postponing the operations that would have required the
426 * mutex. If the thread cannot defer those operations, then it must use
427 * vlc_mutex_lock(). If in doubt, use vlc_mutex_lock() instead.
429 * @return 0 if the mutex could be acquired, an error code otherwise.
431 VLC_API int vlc_mutex_trylock( vlc_mutex_t * ) VLC_USED;
434 * Releases a mutex.
436 * If the mutex is not held by the calling thread, the behaviour is undefined.
438 * \note This function is not a cancellation point.
440 VLC_API void vlc_mutex_unlock(vlc_mutex_t *);
443 * Initializes a condition variable.
445 VLC_API void vlc_cond_init(vlc_cond_t *);
448 * Initializes a condition variable (wall clock).
450 * This function initializes a condition variable for timed waiting using the
451 * UTC wall clock time. The time reference is the same as with time() and with
452 * timespec_get() and TIME_UTC.
453 * vlc_cond_timedwait_daytime() must be instead of
454 * vlc_cond_timedwait() for actual waiting.
456 void vlc_cond_init_daytime(vlc_cond_t *);
459 * Deinitializes a condition variable.
461 * No threads shall be waiting or signaling the condition, otherwise the
462 * behavior is undefined.
464 VLC_API void vlc_cond_destroy(vlc_cond_t *);
467 * Wakes up one thread waiting on a condition variable.
469 * If any thread is currently waiting on the condition variable, at least one
470 * of those threads will be woken up. Otherwise, this function has no effects.
472 * \note This function is not a cancellation point.
474 VLC_API void vlc_cond_signal(vlc_cond_t *);
477 * Wakes up all threads waiting on a condition variable.
479 * \note This function is not a cancellation point.
481 VLC_API void vlc_cond_broadcast(vlc_cond_t *);
484 * Waits on a condition variable.
486 * The calling thread will be suspended until another thread calls
487 * vlc_cond_signal() or vlc_cond_broadcast() on the same condition variable,
488 * the thread is cancelled with vlc_cancel(), or the system causes a
489 * <em>spurious</em> unsolicited wake-up.
491 * A mutex is needed to wait on a condition variable. It must <b>not</b> be
492 * a recursive mutex. Although it is possible to use the same mutex for
493 * multiple condition, it is not valid to use different mutexes for the same
494 * condition variable at the same time from different threads.
496 * The canonical way to use a condition variable to wait for event foobar is:
497 @code
498 vlc_mutex_lock(&lock);
499 mutex_cleanup_push(&lock); // release the mutex in case of cancellation
501 while (!foobar)
502 vlc_cond_wait(&wait, &lock);
504 // -- foobar is now true, do something about it here --
506 vlc_cleanup_pop();
507 vlc_mutex_unlock(&lock);
508 @endcode
510 * \note This function is a cancellation point. In case of thread cancellation,
511 * the mutex is always locked before cancellation proceeds.
513 * \param cond condition variable to wait on
514 * \param mutex mutex which is unlocked while waiting,
515 * then locked again when waking up.
517 VLC_API void vlc_cond_wait(vlc_cond_t *cond, vlc_mutex_t *mutex);
520 * Waits on a condition variable up to a certain date.
522 * This works like vlc_cond_wait() but with an additional time-out.
523 * The time-out is expressed as an absolute timestamp using the same arbitrary
524 * time reference as the vlc_tick_now() and vlc_tick_wait() functions.
526 * \note This function is a cancellation point. In case of thread cancellation,
527 * the mutex is always locked before cancellation proceeds.
529 * \param cond condition variable to wait on
530 * \param mutex mutex which is unlocked while waiting,
531 * then locked again when waking up
532 * \param deadline <b>absolute</b> timeout
534 * \warning If the variable was initialized with vlc_cond_init_daytime(), or
535 * was statically initialized with \ref VLC_STATIC_COND, the time reference
536 * used by this function is unspecified (depending on the implementation, it
537 * might be the Unix epoch or the vlc_tick_now() clock).
539 * \return 0 if the condition was signaled, an error code in case of timeout.
541 VLC_API int vlc_cond_timedwait(vlc_cond_t *cond, vlc_mutex_t *mutex,
542 vlc_tick_t deadline);
544 int vlc_cond_timedwait_daytime(vlc_cond_t *, vlc_mutex_t *, time_t);
547 * Initializes a semaphore.
549 * @param count initial semaphore value (typically 0)
551 VLC_API void vlc_sem_init(vlc_sem_t *, unsigned count);
554 * Deinitializes a semaphore.
556 VLC_API void vlc_sem_destroy(vlc_sem_t *);
559 * Increments the value of a semaphore.
561 * \note This function is not a cancellation point.
563 * \return 0 on success, EOVERFLOW in case of integer overflow.
565 VLC_API int vlc_sem_post(vlc_sem_t *);
568 * Waits on a semaphore.
570 * This function atomically waits for the semaphore to become non-zero then
571 * decrements it, and returns. If the semaphore is non-zero on entry, it is
572 * immediately decremented.
574 * \note This function may be a point of cancellation.
576 VLC_API void vlc_sem_wait(vlc_sem_t *);
579 * Initializes a read/write lock.
581 VLC_API void vlc_rwlock_init(vlc_rwlock_t *);
584 * Destroys an initialized unused read/write lock.
586 VLC_API void vlc_rwlock_destroy(vlc_rwlock_t *);
589 * Acquires a read/write lock for reading.
591 * \note Recursion is allowed.
592 * \note This function may be a point of cancellation.
594 VLC_API void vlc_rwlock_rdlock(vlc_rwlock_t *);
597 * Acquires a read/write lock for writing. Recursion is not allowed.
598 * \note This function may be a point of cancellation.
600 VLC_API void vlc_rwlock_wrlock(vlc_rwlock_t *);
603 * Releases a read/write lock.
605 * The calling thread must hold the lock. Otherwise behaviour is undefined.
607 * \note This function is not a cancellation point.
609 VLC_API void vlc_rwlock_unlock(vlc_rwlock_t *);
612 * Executes a function one time.
614 * The first time this function is called with a given one-time initialization
615 * object, it executes the provided callback.
616 * Any further call with the same object will be a no-op.
618 * In the corner case that the first time execution is ongoing in another
619 * thread, then the function will wait for completion on the other thread
620 * (and then synchronize memory) before it returns.
621 * This ensures that, no matter what, the callback has been executed exactly
622 * once and its side effects are visible after the function returns.
624 * \param once a one-time initialization object
625 * \param cb callback to execute (the first time)
627 VLC_API void vlc_once(vlc_once_t *restrict once, void (*cb)(void));
630 * Allocates a thread-specific variable.
632 * @param key where to store the thread-specific variable handle
633 * @param destr a destruction callback. It is called whenever a thread exits
634 * and the thread-specific variable has a non-NULL value.
636 * @return 0 on success, a system error code otherwise.
637 * This function can actually fail: on most systems, there is a fixed limit to
638 * the number of thread-specific variables in a given process.
640 VLC_API int vlc_threadvar_create(vlc_threadvar_t *key, void (*destr) (void *));
643 * Deallocates a thread-specific variable.
645 VLC_API void vlc_threadvar_delete(vlc_threadvar_t *);
648 * Sets a thread-specific variable.
650 * \param key thread-local variable key (created with vlc_threadvar_create())
651 * \param value new value for the variable for the calling thread
652 * \return 0 on success, a system error code otherwise.
654 VLC_API int vlc_threadvar_set(vlc_threadvar_t key, void *value);
657 * Gets the value of a thread-local variable for the calling thread.
658 * This function cannot fail.
660 * \return the value associated with the given variable for the calling
661 * or NULL if no value was set.
663 VLC_API void *vlc_threadvar_get(vlc_threadvar_t);
666 * Waits on an address.
668 * Puts the calling thread to sleep if a specific value is stored at a
669 * specified address. The thread will sleep until it is woken up by a call to
670 * vlc_addr_signal() or vlc_addr_broadcast() in another thread, or spuriously.
672 * If the value does not match, do nothing and return immediately.
674 * \param addr address to check for
675 * \param val value to match at the address
677 void vlc_addr_wait(void *addr, unsigned val);
680 * Waits on an address with a time-out.
682 * This function operates as vlc_addr_wait() but provides an additional
683 * time-out. If the time-out elapses, the thread resumes and the function
684 * returns.
686 * \param addr address to check for
687 * \param val value to match at the address
688 * \param delay time-out duration
690 * \return true if the function was woken up before the time-out,
691 * false if the time-out elapsed.
693 bool vlc_addr_timedwait(void *addr, unsigned val, vlc_tick_t delay);
696 * Wakes up one thread on an address.
698 * Wakes up (at least) one of the thread sleeping on the specified address.
699 * The address must be equal to the first parameter given by at least one
700 * thread sleeping within the vlc_addr_wait() or vlc_addr_timedwait()
701 * functions. If no threads are found, this function does nothing.
703 * \param addr address identifying which threads may be woken up
705 void vlc_addr_signal(void *addr);
708 * Wakes up all thread on an address.
710 * Wakes up all threads sleeping on the specified address (if any).
711 * Any thread sleeping within a call to vlc_addr_wait() or vlc_addr_timedwait()
712 * with the specified address as first call parameter will be woken up.
714 * \param addr address identifying which threads to wake up
716 void vlc_addr_broadcast(void *addr);
719 * Creates and starts a new thread.
721 * The thread must be <i>joined</i> with vlc_join() to reclaim resources
722 * when it is not needed anymore.
724 * @param th storage space for the handle of the new thread (cannot be NULL)
725 * [OUT]
726 * @param entry entry point for the thread
727 * @param data data parameter given to the entry point
728 * @param priority thread priority value
729 * @return 0 on success, a standard error code on error.
730 * @note In case of error, the value of *th is undefined.
732 VLC_API int vlc_clone(vlc_thread_t *th, void *(*entry)(void *), void *data,
733 int priority) VLC_USED;
736 * Marks a thread as cancelled.
738 * Next time the target thread reaches a cancellation point (while not having
739 * disabled cancellation), it will run its cancellation cleanup handler, the
740 * thread variable destructors, and terminate.
742 * vlc_join() must be used regardless of a thread being cancelled or not, to
743 * avoid leaking resources.
745 VLC_API void vlc_cancel(vlc_thread_t);
748 * Waits for a thread to complete (if needed), then destroys it.
750 * \note This is a cancellation point. In case of cancellation, the thread is
751 * <b>not</b> joined.
753 * \warning A thread cannot join itself (normally VLC will abort if this is
754 * attempted). Also a detached thread <b>cannot</b> be joined.
756 * @param th thread handle
757 * @param result [OUT] pointer to write the thread return value or NULL
759 VLC_API void vlc_join(vlc_thread_t th, void **result);
762 * Disables thread cancellation.
764 * This functions saves the current cancellation state (enabled or disabled),
765 * then disables cancellation for the calling thread. It must be called before
766 * entering a piece of code that is not cancellation-safe, unless it can be
767 * proven that the calling thread will not be cancelled.
769 * \note This function is not a cancellation point.
771 * \return Previous cancellation state (opaque value for vlc_restorecancel()).
773 VLC_API int vlc_savecancel(void);
776 * Restores the cancellation state.
778 * This function restores the cancellation state of the calling thread to
779 * a state previously saved by vlc_savecancel().
781 * \note This function is not a cancellation point.
783 * \param state previous state as returned by vlc_savecancel().
785 VLC_API void vlc_restorecancel(int state);
788 * Internal handler for thread cancellation.
790 * Do not call this function directly. Use wrapper macros instead:
791 * vlc_cleanup_push(), vlc_cleanup_pop().
793 VLC_API void vlc_control_cancel(int cmd, ...);
796 * Thread handle.
798 * This function returns the thread handle of the calling thread.
800 * \note The exact type of the thread handle depends on the platform,
801 * including an integer type, a pointer type or a compound type of any size.
802 * If you need an integer identifier, use vlc_thread_id() instead.
804 * \note vlc_join(vlc_thread_self(), NULL) is undefined,
805 * as it obviously does not make any sense (it might result in a deadlock, but
806 * there are no warranties that it will).
808 * \return the thread handle
810 VLC_API vlc_thread_t vlc_thread_self(void) VLC_USED;
813 * Thread identifier.
815 * This function returns the identifier of the calling thread. The identifier
816 * cannot change for the entire duration of the thread, and no other thread can
817 * have the same identifier at the same time in the same process. Typically,
818 * the identifier is also unique across all running threads of all existing
819 * processes, but that depends on the operating system.
821 * There are no particular semantics to the thread ID with LibVLC.
822 * It is provided mainly for tracing and debugging.
824 * \warning This function is not currently implemented on all supported
825 * platforms. Where not implemented, it returns (unsigned long)-1.
827 * \return the thread identifier (or -1 if unimplemented)
829 VLC_API unsigned long vlc_thread_id(void) VLC_USED;
832 * Precision monotonic clock.
834 * In principles, the clock has a precision of 1 MHz. But the actual resolution
835 * may be much lower, especially when it comes to sleeping with vlc_tick_wait() or
836 * vlc_tick_sleep(). Most general-purpose operating systems provide a resolution of
837 * only 100 to 1000 Hz.
839 * \warning The origin date (time value "zero") is not specified. It is
840 * typically the time the kernel started, but this is platform-dependent.
841 * If you need wall clock time, use gettimeofday() instead.
843 * \return a timestamp in microseconds.
845 VLC_API vlc_tick_t vlc_tick_now(void);
848 * Waits until a deadline.
850 * \param deadline timestamp to wait for (\ref vlc_tick_now())
852 * \note The deadline may be exceeded due to OS scheduling.
853 * \note This function is a cancellation point.
855 VLC_API void vlc_tick_wait(vlc_tick_t deadline);
858 * Waits for an interval of time.
860 * \param delay how long to wait (in microseconds)
862 * \note The delay may be exceeded due to OS scheduling.
863 * \note This function is a cancellation point.
865 VLC_API void vlc_tick_sleep(vlc_tick_t delay);
867 #define VLC_HARD_MIN_SLEEP VLC_TICK_FROM_MS(10) /* 10 milliseconds = 1 tick at 100Hz */
868 #define VLC_SOFT_MIN_SLEEP VLC_TICK_FROM_SEC(9) /* 9 seconds */
870 #if defined (__GNUC__) && !defined (__clang__)
871 /* Linux has 100, 250, 300 or 1000Hz
873 * HZ=100 by default on FreeBSD, but some architectures use a 1000Hz timer
876 static
877 __attribute__((unused))
878 __attribute__((noinline))
879 __attribute__((error("sorry, cannot sleep for such short a time")))
880 vlc_tick_t impossible_delay( vlc_tick_t delay )
882 (void) delay;
883 return VLC_HARD_MIN_SLEEP;
886 static
887 __attribute__((unused))
888 __attribute__((noinline))
889 __attribute__((warning("use proper event handling instead of short delay")))
890 vlc_tick_t harmful_delay( vlc_tick_t delay )
892 return delay;
895 # define check_delay( d ) \
896 ((__builtin_constant_p(d < VLC_HARD_MIN_SLEEP) \
897 && (d < VLC_HARD_MIN_SLEEP)) \
898 ? impossible_delay(d) \
899 : ((__builtin_constant_p(d < VLC_SOFT_MIN_SLEEP) \
900 && (d < VLC_SOFT_MIN_SLEEP)) \
901 ? harmful_delay(d) \
902 : d))
904 static
905 __attribute__((unused))
906 __attribute__((noinline))
907 __attribute__((error("deadlines can not be constant")))
908 vlc_tick_t impossible_deadline( vlc_tick_t deadline )
910 return deadline;
913 # define check_deadline( d ) \
914 (__builtin_constant_p(d) ? impossible_deadline(d) : d)
915 #else
916 # define check_delay(d) (d)
917 # define check_deadline(d) (d)
918 #endif
920 #define vlc_tick_sleep(d) vlc_tick_sleep(check_delay(d))
921 #define vlc_tick_wait(d) vlc_tick_wait(check_deadline(d))
924 * Initializes an asynchronous timer.
926 * \param id pointer to timer to be initialized
927 * \param func function that the timer will call
928 * \param data parameter for the timer function
929 * \return 0 on success, a system error code otherwise.
931 * \warning Asynchronous timers are processed from an unspecified thread.
932 * \note Multiple occurrences of a single interval timer are serialized:
933 * they cannot run concurrently.
935 VLC_API int vlc_timer_create(vlc_timer_t *id, void (*func)(void *), void *data)
936 VLC_USED;
939 * Destroys an initialized timer.
941 * If needed, the timer is first disarmed. Behaviour is undefined if the
942 * specified timer is not initialized.
944 * \warning This function <b>must</b> be called before the timer data can be
945 * freed and before the timer callback function can be unmapped/unloaded.
947 * \param timer timer to destroy
949 VLC_API void vlc_timer_destroy(vlc_timer_t timer);
951 #define VLC_TIMER_DISARM (0)
952 #define VLC_TIMER_FIRE_ONCE (0)
955 * Arms or disarms an initialized timer.
957 * This functions overrides any previous call to itself.
959 * \note A timer can fire later than requested due to system scheduling
960 * limitations. An interval timer can fail to trigger sometimes, either because
961 * the system is busy or suspended, or because a previous iteration of the
962 * timer is still running. See also vlc_timer_getoverrun().
964 * \param timer initialized timer
965 * \param absolute the timer value origin is the same as vlc_tick_now() if true,
966 * the timer value is relative to now if false.
967 * \param value zero to disarm the timer, otherwise the initial time to wait
968 * before firing the timer.
969 * \param interval zero to fire the timer just once, otherwise the timer
970 * repetition interval.
972 VLC_API void vlc_timer_schedule(vlc_timer_t timer, bool absolute,
973 vlc_tick_t value, vlc_tick_t interval);
975 static inline void vlc_timer_disarm(vlc_timer_t timer)
977 vlc_timer_schedule( timer, false, VLC_TIMER_DISARM, 0 );
980 static inline void vlc_timer_schedule_asap(vlc_timer_t timer, vlc_tick_t interval)
982 vlc_timer_schedule(timer, false, 1, interval);
986 * Fetches and resets the overrun counter for a timer.
988 * This functions returns the number of times that the interval timer should
989 * have fired, but the callback was not invoked due to scheduling problems.
990 * The call resets the counter to zero.
992 * \param timer initialized timer
993 * \return the timer overrun counter (typically zero)
995 VLC_API unsigned vlc_timer_getoverrun(vlc_timer_t) VLC_USED;
998 * Count CPUs.
1000 * \return number of available (logical) CPUs.
1002 VLC_API unsigned vlc_GetCPUCount(void);
1004 enum
1006 VLC_CLEANUP_PUSH,
1007 VLC_CLEANUP_POP,
1008 VLC_CANCEL_ADDR_SET,
1009 VLC_CANCEL_ADDR_CLEAR,
1012 #if defined (LIBVLC_USE_PTHREAD_CLEANUP)
1014 * Registers a thread cancellation handler.
1016 * This pushes a function to run if the thread is cancelled (or otherwise
1017 * exits prematurely).
1019 * If multiple procedures are registered,
1020 * they are handled in last-in first-out order.
1022 * \note Any call to vlc_cleanup_push() <b>must</b> paired with a call to
1023 * vlc_cleanup_pop().
1024 * \warning Branching into or out of the block between these two function calls
1025 * is not allowed (read: it will likely crash the whole process).
1027 * \param routine procedure to call if the thread ends
1028 * \param arg argument for the procedure
1030 # define vlc_cleanup_push( routine, arg ) pthread_cleanup_push (routine, arg)
1033 * Unregisters the last cancellation handler.
1035 * This pops the cancellation handler that was last pushed with
1036 * vlc_cleanup_push() in the calling thread.
1038 # define vlc_cleanup_pop( ) pthread_cleanup_pop (0)
1040 #else
1041 typedef struct vlc_cleanup_t vlc_cleanup_t;
1043 struct vlc_cleanup_t
1045 vlc_cleanup_t *next;
1046 void (*proc) (void *);
1047 void *data;
1050 /* This macros opens a code block on purpose. This is needed for multiple
1051 * calls within a single function. This also prevent Win32 developers from
1052 * writing code that would break on POSIX (POSIX opens a block as well). */
1053 # define vlc_cleanup_push( routine, arg ) \
1054 do { \
1055 vlc_cleanup_t vlc_cleanup_data = { NULL, routine, arg, }; \
1056 vlc_control_cancel (VLC_CLEANUP_PUSH, &vlc_cleanup_data)
1058 # define vlc_cleanup_pop( ) \
1059 vlc_control_cancel (VLC_CLEANUP_POP); \
1060 } while (0)
1062 #endif /* !LIBVLC_USE_PTHREAD_CLEANUP */
1064 static inline void vlc_cleanup_lock (void *lock)
1066 vlc_mutex_unlock ((vlc_mutex_t *)lock);
1068 #define mutex_cleanup_push( lock ) vlc_cleanup_push (vlc_cleanup_lock, lock)
1070 static inline void vlc_cancel_addr_set(void *addr)
1072 vlc_control_cancel(VLC_CANCEL_ADDR_SET, addr);
1075 static inline void vlc_cancel_addr_clear(void *addr)
1077 vlc_control_cancel(VLC_CANCEL_ADDR_CLEAR, addr);
1080 #ifdef __cplusplus
1082 * Helper C++ class to lock a mutex.
1084 * The mutex is locked when the object is created, and unlocked when the object
1085 * is destroyed.
1087 class vlc_mutex_locker
1089 private:
1090 vlc_mutex_t *lock;
1091 public:
1092 vlc_mutex_locker (vlc_mutex_t *m) : lock (m)
1094 vlc_mutex_lock (lock);
1097 ~vlc_mutex_locker (void)
1099 vlc_mutex_unlock (lock);
1102 #endif
1104 enum
1106 VLC_AVCODEC_MUTEX = 0,
1107 VLC_GCRYPT_MUTEX,
1108 VLC_XLIB_MUTEX,
1109 VLC_MOSAIC_MUTEX,
1110 #ifdef _WIN32
1111 VLC_MTA_MUTEX,
1112 #endif
1113 /* Insert new entry HERE */
1114 VLC_MAX_MUTEX
1118 * Internal handler for global mutexes.
1120 * Do not use this function directly. Use helper macros instead:
1121 * vlc_global_lock(), vlc_global_unlock().
1123 VLC_API void vlc_global_mutex(unsigned, bool);
1126 * Acquires a global mutex.
1128 #define vlc_global_lock( n ) vlc_global_mutex(n, true)
1131 * Releases a global mutex.
1133 #define vlc_global_unlock( n ) vlc_global_mutex(n, false)
1135 /** @} */
1137 #endif /* !_VLC_THREADS_H */