Sam Hocevar committed Feb 13, 2000 1 /*****************************************************************************  Christophe Massiot committed Aug 29, 2002 2 3  * vlc_threads.h : threads implementation for the VideoLAN client * This header provides portable declarations for mutexes & conditions  Sam Hocevar committed Mar 03, 2000 4  *****************************************************************************  Jean-Baptiste Kempf committed Nov 27, 2011 5  * Copyright (C) 1999, 2002 VLC authors and VideoLAN  Rémi Denis-Courmont committed May 28, 2016 6  * Copyright © 2007-2016 Rémi Denis-Courmont  Sam Hocevar committed Mar 03, 2000 7  *  Sam Hocevar committed Mar 09, 2000 8 9  * Authors: Jean-Marc Dressler * Samuel Hocevar  Gildas Bazin committed Apr 27, 2002 10  * Gildas Bazin  Christophe Massiot committed Aug 29, 2002 11  * Christophe Massiot  Sam Hocevar committed Mar 03, 2000 12  *  Jean-Baptiste Kempf committed Nov 27, 2011 13 14 15  * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or  Sam Hocevar committed Mar 03, 2000 16  * (at your option) any later version.  Eric Petit committed Nov 22, 2003 17  *  Sam Hocevar committed Mar 03, 2000 18 19  * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of  Jean-Baptiste Kempf committed Nov 27, 2011 20 21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details.  Sam Hocevar committed Mar 03, 2000 22  *  Jean-Baptiste Kempf committed Nov 27, 2011 23 24 25  * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.  Sam Hocevar committed Feb 13, 2000 26  *****************************************************************************/  Sam Hocevar committed Mar 03, 2000 27   Rémi Denis-Courmont committed Aug 11, 2008 28 29 #ifndef VLC_THREADS_H_ #define VLC_THREADS_H_  Clément Stenac committed Dec 22, 2006 30   Jean-Paul Saman committed Aug 13, 2008 31 /**  Rémi Denis-Courmont committed Nov 09, 2015 32 33 34  * \ingroup os * \defgroup thread Threads and synchronization primitives * @{  Jean-Paul Saman committed Aug 13, 2008 35  * \file  Rémi Denis-Courmont committed Nov 09, 2015 36  * Thread primitive declarations  Jean-Paul Saman committed Aug 13, 2008 37 38  */  Rémi Denis-Courmont committed Jul 01, 2015 39 40 41 42 43 44 /** * Issues an explicit deferred cancellation point. * * This has no effects if thread cancellation is disabled. * This can be called when there is a rather slow non-sleeping operation. * This is also used to force a cancellation point in a function that would  Hugo Beauzée-Luyssen committed Mar 08, 2017 45  * otherwise not always be one (block_FifoGet() is an example).  Rémi Denis-Courmont committed Jul 01, 2015 46  */  Rémi Denis-Courmont committed May 17, 2015 47 48 VLC_API void vlc_testcancel(void);  Rafaël Carré committed Jun 05, 2013 49 #if defined (_WIN32)  Rémi Denis-Courmont committed Oct 13, 2012 50 51 52 53 # include # ifndef ETIMEDOUT # define ETIMEDOUT 10060 /* This is the value in winsock.h. */ # endif  Juho Vähä-Herttua committed May 07, 2010 54   Rémi Denis-Courmont committed Nov 29, 2010 55 typedef struct vlc_thread *vlc_thread_t;  Rémi Denis-Courmont committed Jun 02, 2016 56 57 # define VLC_THREAD_CANCELED NULL # define LIBVLC_NEED_SLEEP  Rémi Denis-Courmont committed Sep 07, 2008 58 59 typedef struct {  Rémi Denis-Courmont committed Dec 30, 2009 60 61 62  bool dynamic; union {  Rémi Denis-Courmont committed Dec 30, 2009 63 64 65 66 67  struct { bool locked; unsigned long contention; };  Rémi Denis-Courmont committed Dec 30, 2009 68 69  CRITICAL_SECTION mutex; };  Rémi Denis-Courmont committed Oct 05, 2008 70 } vlc_mutex_t;  Rémi Denis-Courmont committed Dec 30, 2009 71 #define VLC_STATIC_MUTEX { false, { { false, 0 } } }  Rémi Denis-Courmont committed May 28, 2016 72 #define LIBVLC_NEED_CONDVAR  Rémi Denis-Courmont committed Jun 02, 2016 73 #define LIBVLC_NEED_SEMAPHORE  Rémi Denis-Courmont committed Sep 11, 2012 74 #define LIBVLC_NEED_RWLOCK  Rémi Denis-Courmont committed Feb 11, 2018 75 76 typedef INIT_ONCE vlc_once_t; #define VLC_STATIC_ONCE INIT_ONCE_STATIC_INIT  Rémi Denis-Courmont committed Nov 23, 2010 77 typedef struct vlc_threadvar *vlc_threadvar_t;  Rémi Denis-Courmont committed Aug 02, 2009 78 typedef struct vlc_timer *vlc_timer_t;  KO Myung-Hun committed Oct 14, 2011 79   Rémi Denis-Courmont committed Oct 13, 2012 80 81 82 83 84 85 86 # define VLC_THREAD_PRIORITY_LOW 0 # define VLC_THREAD_PRIORITY_INPUT THREAD_PRIORITY_ABOVE_NORMAL # define VLC_THREAD_PRIORITY_AUDIO THREAD_PRIORITY_HIGHEST # define VLC_THREAD_PRIORITY_VIDEO 0 # define VLC_THREAD_PRIORITY_OUTPUT THREAD_PRIORITY_ABOVE_NORMAL # define VLC_THREAD_PRIORITY_HIGHEST THREAD_PRIORITY_TIME_CRITICAL  Rémi Denis-Courmont committed May 17, 2015 87 88 89 90 static inline int vlc_poll(struct pollfd *fds, unsigned nfds, int timeout) { int val;  Rémi Denis-Courmont committed May 17, 2015 91 92 93 94  vlc_testcancel(); val = poll(fds, nfds, timeout); if (val < 0) vlc_testcancel();  Rémi Denis-Courmont committed May 17, 2015 95 96 97 98  return val; } # define poll(u,n,t) vlc_poll(u, n, t)  Rémi Denis-Courmont committed Oct 13, 2012 99 100 #elif defined (__OS2__) # include  KO Myung-Hun committed Oct 14, 2011 101   Rémi Denis-Courmont committed Oct 13, 2012 102 typedef struct vlc_thread *vlc_thread_t;  Rémi Denis-Courmont committed Dec 13, 2015 103 #define VLC_THREAD_CANCELED NULL  KO Myung-Hun committed Oct 14, 2011 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 typedef struct { bool dynamic; union { struct { bool locked; unsigned long contention; }; HMTX hmtx; }; } vlc_mutex_t; #define VLC_STATIC_MUTEX { false, { { false, 0 } } } typedef struct { HEV hev;  KO Myung-Hun committed Jun 12, 2015 121 122 123  unsigned waiters; HEV hevAck; unsigned signaled;  KO Myung-Hun committed Oct 14, 2011 124 } vlc_cond_t;  Rémi Denis-Courmont committed May 24, 2016 125 #define VLC_STATIC_COND { NULLHANDLE, 0, NULLHANDLE, 0 }  Rémi Denis-Courmont committed Sep 11, 2012 126 127 #define LIBVLC_NEED_SEMAPHORE #define LIBVLC_NEED_RWLOCK  KO Myung-Hun committed May 18, 2018 128 129 130 131 132 133 typedef struct { unsigned done; vlc_mutex_t mutex; } vlc_once_t; #define VLC_STATIC_ONCE { 0, VLC_STATIC_MUTEX }  KO Myung-Hun committed Oct 14, 2011 134 135 136 typedef struct vlc_threadvar *vlc_threadvar_t; typedef struct vlc_timer *vlc_timer_t;  Rémi Denis-Courmont committed Oct 13, 2012 137 138 139 140 141 142 143 144 145 146 147 # define VLC_THREAD_PRIORITY_LOW 0 # define VLC_THREAD_PRIORITY_INPUT \ MAKESHORT(PRTYD_MAXIMUM / 2, PRTYC_REGULAR) # define VLC_THREAD_PRIORITY_AUDIO MAKESHORT(PRTYD_MAXIMUM, PRTYC_REGULAR) # define VLC_THREAD_PRIORITY_VIDEO 0 # define VLC_THREAD_PRIORITY_OUTPUT \ MAKESHORT(PRTYD_MAXIMUM / 2, PRTYC_REGULAR) # define VLC_THREAD_PRIORITY_HIGHEST MAKESHORT(0, PRTYC_TIMECRITICAL) # define pthread_sigmask sigprocmask  Rémi Denis-Courmont committed May 17, 2015 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 static inline int vlc_poll (struct pollfd *fds, unsigned nfds, int timeout) { static int (*vlc_poll_os2)(struct pollfd *, unsigned, int) = NULL; if (!vlc_poll_os2) { HMODULE hmod; CHAR szFailed[CCHMAXPATH]; if (DosLoadModule(szFailed, sizeof(szFailed), "vlccore", &hmod)) return -1; if (DosQueryProcAddr(hmod, 0, "_vlc_poll_os2", (PFN *)&vlc_poll_os2)) return -1; } return (*vlc_poll_os2)(fds, nfds, timeout); } # define poll(u,n,t) vlc_poll(u, n, t)  Rémi Denis-Courmont committed Oct 13, 2012 168 169 170 171 172 #elif defined (__ANDROID__) /* pthreads subset without pthread_cancel() */ # include # include # include # define LIBVLC_USE_PTHREAD_CLEANUP 1  Rémi Denis-Courmont committed May 28, 2016 173 174 # define LIBVLC_NEED_SLEEP # define LIBVLC_NEED_CONDVAR  Rémi Denis-Courmont committed Oct 13, 2012 175 176 177 178 # define LIBVLC_NEED_SEMAPHORE # define LIBVLC_NEED_RWLOCK typedef struct vlc_thread *vlc_thread_t;  Rémi Denis-Courmont committed Dec 13, 2015 179 #define VLC_THREAD_CANCELED NULL  Rémi Denis-Courmont committed Oct 13, 2012 180 181 typedef pthread_mutex_t vlc_mutex_t; #define VLC_STATIC_MUTEX PTHREAD_MUTEX_INITIALIZER  Rémi Denis-Courmont committed Feb 11, 2018 182 183 typedef pthread_once_t vlc_once_t; #define VLC_STATIC_ONCE PTHREAD_ONCE_INIT  Rémi Denis-Courmont committed Oct 13, 2012 184 185 186 187 188 189 190 191 192 193 typedef pthread_key_t vlc_threadvar_t; typedef struct vlc_timer *vlc_timer_t; # define VLC_THREAD_PRIORITY_LOW 0 # define VLC_THREAD_PRIORITY_INPUT 0 # define VLC_THREAD_PRIORITY_AUDIO 0 # define VLC_THREAD_PRIORITY_VIDEO 0 # define VLC_THREAD_PRIORITY_OUTPUT 0 # define VLC_THREAD_PRIORITY_HIGHEST 0  Rémi Denis-Courmont committed May 17, 2015 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 static inline int vlc_poll (struct pollfd *fds, unsigned nfds, int timeout) { int val; do { int ugly_timeout = ((unsigned)timeout >= 50) ? 50 : timeout; if (timeout >= 0) timeout -= ugly_timeout; vlc_testcancel (); val = poll (fds, nfds, ugly_timeout); } while (val == 0 && timeout != 0); return val; } # define poll(u,n,t) vlc_poll(u, n, t)  Rémi Denis-Courmont committed Oct 13, 2012 214 215 216 217 218 219 220 221 222 223 224 #elif defined (__APPLE__) # define _APPLE_C_SOURCE 1 /* Proper pthread semantics on OSX */ # include # include /* Unnamed POSIX semaphores not supported on Mac OS X */ # include # include # define LIBVLC_USE_PTHREAD 1 # define LIBVLC_USE_PTHREAD_CLEANUP 1 typedef pthread_t vlc_thread_t;  Rémi Denis-Courmont committed Dec 13, 2015 225 #define VLC_THREAD_CANCELED PTHREAD_CANCELED  Rémi Denis-Courmont committed Oct 13, 2012 226 227 typedef pthread_mutex_t vlc_mutex_t; #define VLC_STATIC_MUTEX PTHREAD_MUTEX_INITIALIZER  Rémi Denis-Courmont committed May 24, 2016 228 229 typedef pthread_cond_t vlc_cond_t; #define VLC_STATIC_COND PTHREAD_COND_INITIALIZER  Rémi Denis-Courmont committed Oct 13, 2012 230 231 232 typedef semaphore_t vlc_sem_t; typedef pthread_rwlock_t vlc_rwlock_t; #define VLC_STATIC_RWLOCK PTHREAD_RWLOCK_INITIALIZER  Rémi Denis-Courmont committed Feb 11, 2018 233 234 typedef pthread_once_t vlc_once_t; #define VLC_STATIC_ONCE PTHREAD_ONCE_INIT  Rémi Denis-Courmont committed Oct 13, 2012 235 236 237 238 239 240 241 242 243 244 245 246 247 248 typedef pthread_key_t vlc_threadvar_t; typedef struct vlc_timer *vlc_timer_t; # define VLC_THREAD_PRIORITY_LOW 0 # define VLC_THREAD_PRIORITY_INPUT 22 # define VLC_THREAD_PRIORITY_AUDIO 22 # define VLC_THREAD_PRIORITY_VIDEO 0 # define VLC_THREAD_PRIORITY_OUTPUT 22 # define VLC_THREAD_PRIORITY_HIGHEST 22 #else /* POSIX threads */ # include /* _POSIX_SPIN_LOCKS */ # include # include  Rémi Denis-Courmont committed Jul 01, 2015 249 250 251 252  /** * Whether LibVLC threads are based on POSIX threads. */  Rémi Denis-Courmont committed Oct 13, 2012 253 # define LIBVLC_USE_PTHREAD 1  Rémi Denis-Courmont committed Jul 01, 2015 254 255 256 257  /** * Whether LibVLC thread cancellation is based on POSIX threads. */  Rémi Denis-Courmont committed Oct 13, 2012 258 259 # define LIBVLC_USE_PTHREAD_CLEANUP 1  Rémi Denis-Courmont committed Jul 01, 2015 260 261 262 /** * Thread handle. */  Rémi Denis-Courmont committed Jul 25, 2016 263 264 265 266 typedef struct { pthread_t handle; } vlc_thread_t;  Rémi Denis-Courmont committed Jul 01, 2015 267   Rémi Denis-Courmont committed Dec 13, 2015 268 269 270 271 272 /** * Return value of a canceled thread. */ #define VLC_THREAD_CANCELED PTHREAD_CANCELED  Rémi Denis-Courmont committed Jul 01, 2015 273 274 275 276 277 /** * Mutex. * * Storage space for a mutual exclusion lock. */  Rémi Denis-Courmont committed Oct 13, 2012 278 typedef pthread_mutex_t vlc_mutex_t;  Rémi Denis-Courmont committed Jul 01, 2015 279 280 281 282  /** * Static initializer for (static) mutex. */  Rémi Denis-Courmont committed Oct 13, 2012 283 #define VLC_STATIC_MUTEX PTHREAD_MUTEX_INITIALIZER  Rémi Denis-Courmont committed Jul 01, 2015 284 285 286 287 288 289  /** * Condition variable. * * Storage space for a thread condition variable. */  Rémi Denis-Courmont committed Oct 13, 2012 290 typedef pthread_cond_t vlc_cond_t;  Rémi Denis-Courmont committed Jul 01, 2015 291 292 293  /** * Static initializer for (static) condition variable.  Rémi Denis-Courmont committed May 24, 2016 294 295 296 297 298 299  * * \note * The condition variable will use the default clock, which is OS-dependent. * Therefore, where timed waits are necessary the condition variable should * always be initialized dynamically explicit instead of using this * initializer.  Rémi Denis-Courmont committed Jul 01, 2015 300  */  Rémi Denis-Courmont committed Oct 13, 2012 301 #define VLC_STATIC_COND PTHREAD_COND_INITIALIZER  Rémi Denis-Courmont committed Jul 01, 2015 302 303 304 305 306 307  /** * Semaphore. * * Storage space for a thread-safe semaphore. */  Rémi Denis-Courmont committed Oct 13, 2012 308 typedef sem_t vlc_sem_t;  Rémi Denis-Courmont committed Jul 01, 2015 309 310 311 312 313 314  /** * Read/write lock. * * Storage space for a slim reader/writer lock. */  Rémi Denis-Courmont committed Oct 13, 2012 315 typedef pthread_rwlock_t vlc_rwlock_t;  Rémi Denis-Courmont committed Jul 01, 2015 316 317 318 319  /** * Static initializer for (static) read/write lock. */  Rémi Denis-Courmont committed Oct 13, 2012 320 #define VLC_STATIC_RWLOCK PTHREAD_RWLOCK_INITIALIZER  Rémi Denis-Courmont committed Jul 01, 2015 321   Rémi Denis-Courmont committed Feb 11, 2018 322 323 324 325 326 327 328 329 330 331 332 333 334 /** * One-time initialization. * * A one-time initialization object must always be initialized assigned to * \ref VLC_STATIC_ONCE before use. */ typedef pthread_once_t vlc_once_t; /** * Static initializer for one-time initialization. */ #define VLC_STATIC_ONCE PTHREAD_ONCE_INIT  Rémi Denis-Courmont committed Jul 01, 2015 335 336 337 /** * Thread-local key handle. */  Rémi Denis-Courmont committed Oct 13, 2012 338 typedef pthread_key_t vlc_threadvar_t;  Rémi Denis-Courmont committed Jul 01, 2015 339 340 341 342  /** * Threaded timer handle. */  Rémi Denis-Courmont committed Oct 13, 2012 343 344 345 346 347 348 349 350 351 typedef struct vlc_timer *vlc_timer_t; # define VLC_THREAD_PRIORITY_LOW 0 # define VLC_THREAD_PRIORITY_INPUT 10 # define VLC_THREAD_PRIORITY_AUDIO 5 # define VLC_THREAD_PRIORITY_VIDEO 0 # define VLC_THREAD_PRIORITY_OUTPUT 15 # define VLC_THREAD_PRIORITY_HIGHEST 20  Rémi Denis-Courmont committed May 08, 2008 352 353 #endif  Rémi Denis-Courmont committed May 28, 2016 354 355 356 #ifdef LIBVLC_NEED_CONDVAR typedef struct {  Rémi Denis-Courmont committed Jun 02, 2016 357  unsigned value;  Rémi Denis-Courmont committed May 28, 2016 358 359 360 361 } vlc_cond_t; # define VLC_STATIC_COND { 0 } #endif  Rémi Denis-Courmont committed Sep 11, 2012 362 363 364 365 366 367 368 369 370 371 372 #ifdef LIBVLC_NEED_SEMAPHORE typedef struct vlc_sem { vlc_mutex_t lock; vlc_cond_t wait; unsigned value; } vlc_sem_t; #endif #ifdef LIBVLC_NEED_RWLOCK typedef struct vlc_rwlock  Rémi Denis-Courmont committed Sep 10, 2012 373 374 375 376 { vlc_mutex_t mutex; vlc_cond_t wait; long state;  Rémi Denis-Courmont committed Sep 11, 2012 377 } vlc_rwlock_t;  Rémi Denis-Courmont committed Sep 10, 2012 378 379 380 # define VLC_STATIC_RWLOCK { VLC_STATIC_MUTEX, VLC_STATIC_COND, 0 } #endif  Rémi Denis-Courmont committed Jul 01, 2015 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 /** * Initializes a fast mutex. * * Recursive locking of a fast mutex is undefined behaviour. (In debug builds, * recursive locking will cause an assertion failure.) */ VLC_API void vlc_mutex_init(vlc_mutex_t *); /** * Initializes a recursive mutex. * \warning This is strongly discouraged. Please use normal mutexes. */ VLC_API void vlc_mutex_init_recursive(vlc_mutex_t *); /** * Deinitializes a mutex. * * The mutex must not be locked, otherwise behaviour is undefined. */ VLC_API void vlc_mutex_destroy(vlc_mutex_t *); /** * Acquires a mutex. * * If needed, this waits for any other thread to release it. * * \warning Beware of deadlocks when locking multiple mutexes at the same time, * or when using mutexes from callbacks. *  Rémi Denis-Courmont committed Aug 04, 2016 410  * \note This function is not a cancellation point.  Rémi Denis-Courmont committed Jul 01, 2015 411 412 413 414 415 416 417 418 419 420  */ VLC_API void vlc_mutex_lock(vlc_mutex_t *); /** * Tries to acquire a mutex. * * This function acquires the mutex if and only if it is not currently held by * another thread. This function never sleeps and can be used in delay-critical * code paths. *  Rémi Denis-Courmont committed Aug 04, 2016 421  * \note This function is not a cancellation point.  Rémi Denis-Courmont committed Jul 01, 2015 422 423 424 425 426 427 428 429 430  * * \warning If this function fails, then the mutex is held... by another * thread. The calling thread must deal with the error appropriately. That * typically implies postponing the operations that would have required the * mutex. If the thread cannot defer those operations, then it must use * vlc_mutex_lock(). If in doubt, use vlc_mutex_lock() instead. * * @return 0 if the mutex could be acquired, an error code otherwise. */  Rémi Denis-Courmont committed May 07, 2011 431 VLC_API int vlc_mutex_trylock( vlc_mutex_t * ) VLC_USED;  Rémi Denis-Courmont committed Jul 01, 2015 432 433 434 435 436  /** * Releases a mutex. * * If the mutex is not held by the calling thread, the behaviour is undefined.  Rémi Denis-Courmont committed Aug 04, 2016 437 438  * * \note This function is not a cancellation point.  Rémi Denis-Courmont committed Jul 01, 2015 439 440 441 442 443 444 445 446 447 448 449  */ VLC_API void vlc_mutex_unlock(vlc_mutex_t *); /** * Initializes a condition variable. */ VLC_API void vlc_cond_init(vlc_cond_t *); /** * Initializes a condition variable (wall clock). *  Rémi Denis-Courmont committed May 24, 2016 450 451 452 453 454  * This function initializes a condition variable for timed waiting using the * UTC wall clock time. The time reference is the same as with time() and with * timespec_get() and TIME_UTC. * vlc_cond_timedwait_daytime() must be instead of * vlc_cond_timedwait() for actual waiting.  Rémi Denis-Courmont committed Jul 01, 2015 455  */  Rémi Denis-Courmont committed May 24, 2016 456 void vlc_cond_init_daytime(vlc_cond_t *);  Rémi Denis-Courmont committed Jul 01, 2015 457 458 459 460 461 462 463 464 465 466 467 468 469 470  /** * Deinitializes a condition variable. * * No threads shall be waiting or signaling the condition, otherwise the * behavior is undefined. */ VLC_API void vlc_cond_destroy(vlc_cond_t *); /** * Wakes up one thread waiting on a condition variable. * * If any thread is currently waiting on the condition variable, at least one * of those threads will be woken up. Otherwise, this function has no effects.  Rémi Denis-Courmont committed Aug 04, 2016 471 472  * * \note This function is not a cancellation point.  Rémi Denis-Courmont committed Jul 01, 2015 473  */  Rémi Denis-Courmont committed May 07, 2011 474 VLC_API void vlc_cond_signal(vlc_cond_t *);  Rémi Denis-Courmont committed Jul 01, 2015 475 476 477  /** * Wakes up all threads waiting on a condition variable.  Rémi Denis-Courmont committed Aug 04, 2016 478 479  * * \note This function is not a cancellation point.  Rémi Denis-Courmont committed Jul 01, 2015 480  */  Rémi Denis-Courmont committed May 07, 2011 481 VLC_API void vlc_cond_broadcast(vlc_cond_t *);  Rémi Denis-Courmont committed Jul 01, 2015 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505  /** * Waits on a condition variable. * * The calling thread will be suspended until another thread calls * vlc_cond_signal() or vlc_cond_broadcast() on the same condition variable, * the thread is cancelled with vlc_cancel(), or the system causes a * spurious unsolicited wake-up. * * A mutex is needed to wait on a condition variable. It must not be * a recursive mutex. Although it is possible to use the same mutex for * multiple condition, it is not valid to use different mutexes for the same * condition variable at the same time from different threads. * * The canonical way to use a condition variable to wait for event foobar is: @code vlc_mutex_lock(&lock); mutex_cleanup_push(&lock); // release the mutex in case of cancellation while (!foobar) vlc_cond_wait(&wait, &lock); // -- foobar is now true, do something about it here --  Rémi Denis-Courmont committed Jul 08, 2015 506 507  vlc_cleanup_pop(); vlc_mutex_unlock(&lock);  Rémi Denis-Courmont committed Jul 01, 2015 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522  @endcode * * \note This function is a cancellation point. In case of thread cancellation, * the mutex is always locked before cancellation proceeds. * * \param cond condition variable to wait on * \param mutex mutex which is unlocked while waiting, * then locked again when waking up. */ VLC_API void vlc_cond_wait(vlc_cond_t *cond, vlc_mutex_t *mutex); /** * Waits on a condition variable up to a certain date. * * This works like vlc_cond_wait() but with an additional time-out.  Rémi Denis-Courmont committed May 24, 2016 523 524  * The time-out is expressed as an absolute timestamp using the same arbitrary * time reference as the mdate() and mwait() functions.  Rémi Denis-Courmont committed Jul 01, 2015 525  *  Rémi Denis-Courmont committed Aug 04, 2016 526 527 528  * \note This function is a cancellation point. In case of thread cancellation, * the mutex is always locked before cancellation proceeds. *  Rémi Denis-Courmont committed May 24, 2016 529 530 531  * \param cond condition variable to wait on * \param mutex mutex which is unlocked while waiting, * then locked again when waking up  Rémi Denis-Courmont committed Jul 01, 2015 532 533  * \param deadline absolute timeout *  Rémi Denis-Courmont committed May 24, 2016 534 535 536 537 538  * \warning If the variable was initialized with vlc_cond_init_daytime(), or * was statically initialized with \ref VLC_STATIC_COND, the time reference * used by this function is unspecified (depending on the implementation, it * might be the Unix epoch or the mdate() clock). *  Rémi Denis-Courmont committed Jul 01, 2015 539 540 541 542 543  * \return 0 if the condition was signaled, an error code in case of timeout. */ VLC_API int vlc_cond_timedwait(vlc_cond_t *cond, vlc_mutex_t *mutex, mtime_t deadline);  Rémi Denis-Courmont committed May 24, 2016 544 545 int vlc_cond_timedwait_daytime(vlc_cond_t *, vlc_mutex_t *, time_t);  Rémi Denis-Courmont committed Jul 01, 2015 546 547 548 549 550 551 552 553 554 555 /** * Initializes a semaphore. * * @param count initial semaphore value (typically 0) */ VLC_API void vlc_sem_init(vlc_sem_t *, unsigned count); /** * Deinitializes a semaphore. */  Rémi Denis-Courmont committed May 07, 2011 556 VLC_API void vlc_sem_destroy(vlc_sem_t *);  Rémi Denis-Courmont committed Jul 01, 2015 557 558 559  /** * Increments the value of a semaphore.  Rémi Denis-Courmont committed Aug 04, 2016 560 561 562 563  * * \note This function is not a cancellation point. * * \return 0 on success, EOVERFLOW in case of integer overflow.  Rémi Denis-Courmont committed Jul 01, 2015 564  */  Rémi Denis-Courmont committed May 07, 2011 565 VLC_API int vlc_sem_post(vlc_sem_t *);  Rémi Denis-Courmont committed Jul 01, 2015 566 567 568 569 570 571 572 573 574 575  /** * Waits on a semaphore. * * This function atomically waits for the semaphore to become non-zero then * decrements it, and returns. If the semaphore is non-zero on entry, it is * immediately decremented. * * \note This function may be a point of cancellation. */  Rémi Denis-Courmont committed May 07, 2011 576 577 VLC_API void vlc_sem_wait(vlc_sem_t *);  Rémi Denis-Courmont committed Jul 01, 2015 578 579 580 /** * Initializes a read/write lock. */  Rémi Denis-Courmont committed May 07, 2011 581 VLC_API void vlc_rwlock_init(vlc_rwlock_t *);  Rémi Denis-Courmont committed Jul 01, 2015 582 583 584 585  /** * Destroys an initialized unused read/write lock. */  Rémi Denis-Courmont committed May 07, 2011 586 VLC_API void vlc_rwlock_destroy(vlc_rwlock_t *);  Rémi Denis-Courmont committed Jul 01, 2015 587 588 589 590 591 592 593  /** * Acquires a read/write lock for reading. * * \note Recursion is allowed. * \note This function may be a point of cancellation. */  Rémi Denis-Courmont committed May 07, 2011 594 VLC_API void vlc_rwlock_rdlock(vlc_rwlock_t *);  Rémi Denis-Courmont committed Jul 01, 2015 595 596 597 598 599  /** * Acquires a read/write lock for writing. Recursion is not allowed. * \note This function may be a point of cancellation. */  Rémi Denis-Courmont committed May 07, 2011 600 VLC_API void vlc_rwlock_wrlock(vlc_rwlock_t *);  Rémi Denis-Courmont committed Jul 01, 2015 601 602 603 604 605  /** * Releases a read/write lock. * * The calling thread must hold the lock. Otherwise behaviour is undefined.  Rémi Denis-Courmont committed Aug 04, 2016 606 607  * * \note This function is not a cancellation point.  Rémi Denis-Courmont committed Jul 01, 2015 608  */  Rémi Denis-Courmont committed May 07, 2011 609 VLC_API void vlc_rwlock_unlock(vlc_rwlock_t *);  Rémi Denis-Courmont committed Jul 01, 2015 610   Rémi Denis-Courmont committed Feb 11, 2018 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 /** * Executes a function one time. * * The first time this function is called with a given one-time initialization * object, it executes the provided callback. * Any further call with the same object will be a no-op. * * In the corner case that the first time execution is ongoing in another * thread, then the function will wait for completion on the other thread * (and then synchronize memory) before it returns. * This ensures that, no matter what, the callback has been executed exactly * once and its side effects are visible after the function returns. * * \param once a one-time initialization object * \param cb callback to execute (the first time) */ VLC_API void vlc_once(vlc_once_t *restrict once, void (*cb)(void));  Rémi Denis-Courmont committed Jul 01, 2015 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 /** * Allocates a thread-specific variable. * * @param key where to store the thread-specific variable handle * @param destr a destruction callback. It is called whenever a thread exits * and the thread-specific variable has a non-NULL value. * * @return 0 on success, a system error code otherwise. * This function can actually fail: on most systems, there is a fixed limit to * the number of thread-specific variables in a given process. */ VLC_API int vlc_threadvar_create(vlc_threadvar_t *key, void (*destr) (void *)); /** * Deallocates a thread-specific variable. */  Rémi Denis-Courmont committed May 07, 2011 645 VLC_API void vlc_threadvar_delete(vlc_threadvar_t *);  Laurent Aimar committed May 22, 2011 646   Rémi Denis-Courmont committed Jul 01, 2015 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 /** * Sets a thread-specific variable. * \param key thread-local variable key (created with vlc_threadvar_create()) * \param value new value for the variable for the calling thread * \return 0 on success, a system error code otherwise. */ VLC_API int vlc_threadvar_set(vlc_threadvar_t key, void *value); /** * Gets the value of a thread-local variable for the calling thread. * This function cannot fail. * * \return the value associated with the given variable for the calling * or NULL if no value was set. */ VLC_API void *vlc_threadvar_get(vlc_threadvar_t);  Rémi Denis-Courmont committed May 28, 2016 665 666 667 668 /** * Waits on an address. * * Puts the calling thread to sleep if a specific value is stored at a  Rémi Denis-Courmont committed Aug 05, 2016 669 670 671 672  * specified address. The thread will sleep until it is woken up by a call to * vlc_addr_signal() or vlc_addr_broadcast() in another thread, or spuriously. * * If the value does not match, do nothing and return immediately.  Rémi Denis-Courmont committed May 28, 2016 673 674 675 676  * * \param addr address to check for * \param val value to match at the address */  Rémi Denis-Courmont committed Jun 01, 2016 677 void vlc_addr_wait(void *addr, unsigned val);  Rémi Denis-Courmont committed May 28, 2016 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692  /** * Waits on an address with a time-out. * * This function operates as vlc_addr_wait() but provides an additional * time-out. If the time-out elapses, the thread resumes and the function * returns. * * \param addr address to check for * \param val value to match at the address * \param delay time-out duration * * \return true if the function was woken up before the time-out, * false if the time-out elapsed. */  Rémi Denis-Courmont committed Jun 01, 2016 693 bool vlc_addr_timedwait(void *addr, unsigned val, mtime_t delay);  Rémi Denis-Courmont committed May 28, 2016 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717  /** * Wakes up one thread on an address. * * Wakes up (at least) one of the thread sleeping on the specified address. * The address must be equal to the first parameter given by at least one * thread sleeping within the vlc_addr_wait() or vlc_addr_timedwait() * functions. If no threads are found, this function does nothing. * * \param addr address identifying which threads may be woken up */ void vlc_addr_signal(void *addr); /** * Wakes up all thread on an address. * * Wakes up all threads sleeping on the specified address (if any). * Any thread sleeping within a call to vlc_addr_wait() or vlc_addr_timedwait() * with the specified address as first call parameter will be woken up. * * \param addr address identifying which threads to wake up */ void vlc_addr_broadcast(void *addr);  Rémi Denis-Courmont committed Jul 01, 2015 718 719 720 721 722 723 /** * Creates and starts a new thread. * * The thread must be joined with vlc_join() to reclaim resources * when it is not needed anymore. *  Rémi Denis-Courmont committed Jul 25, 2016 724 725  * @param th storage space for the handle of the new thread (cannot be NULL) * [OUT]  Rémi Denis-Courmont committed Jul 01, 2015 726 727 728 729  * @param entry entry point for the thread * @param data data parameter given to the entry point * @param priority thread priority value * @return 0 on success, a standard error code on error.  Rémi Denis-Courmont committed Jul 25, 2016 730  * @note In case of error, the value of *th is undefined.  Rémi Denis-Courmont committed Jul 01, 2015 731 732 733 734 735 736 737 738 739 740 741 742 743 744  */ VLC_API int vlc_clone(vlc_thread_t *th, void *(*entry)(void *), void *data, int priority) VLC_USED; /** * Marks a thread as cancelled. * * Next time the target thread reaches a cancellation point (while not having * disabled cancellation), it will run its cancellation cleanup handler, the * thread variable destructors, and terminate. * * vlc_join() must be used regardless of a thread being cancelled or not, to * avoid leaking resources. */  Rémi Denis-Courmont committed May 07, 2011 745 746 VLC_API void vlc_cancel(vlc_thread_t);  Rémi Denis-Courmont committed Jul 01, 2015 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 /** * Waits for a thread to complete (if needed), then destroys it. * * \note This is a cancellation point. In case of cancellation, the thread is * not joined. * \warning A thread cannot join itself (normally VLC will abort if this is * attempted). Also a detached thread cannot be joined. * * @param th thread handle * @param result [OUT] pointer to write the thread return value or NULL */ VLC_API void vlc_join(vlc_thread_t th, void **result); /** * Disables thread cancellation. * * This functions saves the current cancellation state (enabled or disabled), * then disables cancellation for the calling thread. It must be called before * entering a piece of code that is not cancellation-safe, unless it can be * proven that the calling thread will not be cancelled. * * \note This function is not a cancellation point. * * \return Previous cancellation state (opaque value for vlc_restorecancel()). */ VLC_API int vlc_savecancel(void); /** * Restores the cancellation state. * * This function restores the cancellation state of the calling thread to * a state previously saved by vlc_savecancel(). * * \note This function is not a cancellation point. * * \param state previous state as returned by vlc_savecancel(). */ VLC_API void vlc_restorecancel(int state); /** * Internal handler for thread cancellation. * * Do not call this function directly. Use wrapper macros instead:  Rémi Denis-Courmont committed Jul 08, 2015 791  * vlc_cleanup_push(), vlc_cleanup_pop().  Rémi Denis-Courmont committed Jul 01, 2015 792 793 794  */ VLC_API void vlc_control_cancel(int cmd, ...);  Rémi Denis-Courmont committed May 25, 2016 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 /** * Thread handle. * * This function returns the thread handle of the calling thread. * * \note The exact type of the thread handle depends on the platform, * including an integer type, a pointer type or a compound type of any size. * If you need an integer identifier, use vlc_thread_id() instead. * * \note vlc_join(vlc_thread_self(), NULL) is undefined, * as it obviously does not make any sense (it might result in a deadlock, but * there are no warranties that it will). * * \return the thread handle */ VLC_API vlc_thread_t vlc_thread_self(void) VLC_USED; /** * Thread identifier. * * This function returns the identifier of the calling thread. The identifier * cannot change for the entire duration of the thread, and no other thread can * have the same identifier at the same time in the same process. Typically, * the identifier is also unique across all running threads of all existing * processes, but that depends on the operating system. * * There are no particular semantics to the thread ID with LibVLC. * It is provided mainly for tracing and debugging. * * \warning This function is not currently implemented on all supported * platforms. Where not implemented, it returns (unsigned long)-1. * * \return the thread identifier (or -1 if unimplemented) */ VLC_API unsigned long vlc_thread_id(void) VLC_USED;  Rémi Denis-Courmont committed Jul 01, 2015 831 832 833 834 835 836 837 838 839 840 841 842 843 844 /** * Precision monotonic clock. * * In principles, the clock has a precision of 1 MHz. But the actual resolution * may be much lower, especially when it comes to sleeping with mwait() or * msleep(). Most general-purpose operating systems provide a resolution of * only 100 to 1000 Hz. * * \warning The origin date (time value "zero") is not specified. It is * typically the time the kernel started, but this is platform-dependent. * If you need wall clock time, use gettimeofday() instead. * * \return a timestamp in microseconds. */  Rémi Denis-Courmont committed Jul 12, 2011 845 VLC_API mtime_t mdate(void);  Rémi Denis-Courmont committed Jul 01, 2015 846 847 848 849 850 851 852  /** * Waits until a deadline. * * \param deadline timestamp to wait for (\ref mdate()) * * \note The deadline may be exceeded due to OS scheduling.  Rémi Denis-Courmont committed Aug 04, 2016 853  * \note This function is a cancellation point.  Rémi Denis-Courmont committed Jul 01, 2015 854  */  Rémi Denis-Courmont committed Jul 12, 2011 855 VLC_API void mwait(mtime_t deadline);  Rémi Denis-Courmont committed Jul 01, 2015 856 857 858 859  /** * Waits for an interval of time. *  Rémi Denis-Courmont committed Aug 04, 2016 860 861 862 863  * \param delay how long to wait (in microseconds) * * \note The delay may be exceeded due to OS scheduling. * \note This function is a cancellation point.  Rémi Denis-Courmont committed Jul 01, 2015 864  */  Rémi Denis-Courmont committed Jul 12, 2011 865 866 VLC_API void msleep(mtime_t delay);  Steve Lhomme committed May 05, 2018 867 868 #define VLC_HARD_MIN_SLEEP (CLOCK_FREQ/100) /* 10 milliseconds = 1 tick at 100Hz */ #define VLC_SOFT_MIN_SLEEP (9*CLOCK_FREQ) /* 9 seconds */  Rémi Denis-Courmont committed Jul 12, 2011 869   Rémi Denis-Courmont committed Feb 26, 2017 870 #if defined (__GNUC__) && !defined (__clang__)  Rémi Denis-Courmont committed Jul 12, 2011 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 /* Linux has 100, 250, 300 or 1000Hz * * HZ=100 by default on FreeBSD, but some architectures use a 1000Hz timer */ static __attribute__((unused)) __attribute__((noinline)) __attribute__((error("sorry, cannot sleep for such short a time"))) mtime_t impossible_delay( mtime_t delay ) { (void) delay; return VLC_HARD_MIN_SLEEP; } static __attribute__((unused)) __attribute__((noinline)) __attribute__((warning("use proper event handling instead of short delay"))) mtime_t harmful_delay( mtime_t delay ) { return delay; } # define check_delay( d ) \ ((__builtin_constant_p(d < VLC_HARD_MIN_SLEEP) \ && (d < VLC_HARD_MIN_SLEEP)) \ ? impossible_delay(d) \ : ((__builtin_constant_p(d < VLC_SOFT_MIN_SLEEP) \ && (d < VLC_SOFT_MIN_SLEEP)) \ ? harmful_delay(d) \ : d)) static __attribute__((unused)) __attribute__((noinline)) __attribute__((error("deadlines can not be constant"))) mtime_t impossible_deadline( mtime_t deadline ) { return deadline; } # define check_deadline( d ) \ (__builtin_constant_p(d) ? impossible_deadline(d) : d) #else # define check_delay(d) (d) # define check_deadline(d) (d) #endif #define msleep(d) msleep(check_delay(d)) #define mwait(d) mwait(check_deadline(d))  Rémi Denis-Courmont committed Jul 01, 2015 923 924 925 926 927 928 929 930 931 /** * Initializes an asynchronous timer. * * \param id pointer to timer to be initialized * \param func function that the timer will call * \param data parameter for the timer function * \return 0 on success, a system error code otherwise. * * \warning Asynchronous timers are processed from an unspecified thread.  Eric Engestrom committed Apr 02, 2016 932  * \note Multiple occurrences of a single interval timer are serialized:  Rémi Denis-Courmont committed Jul 01, 2015 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981  * they cannot run concurrently. */ VLC_API int vlc_timer_create(vlc_timer_t *id, void (*func)(void *), void *data) VLC_USED; /** * Destroys an initialized timer. * * If needed, the timer is first disarmed. Behaviour is undefined if the * specified timer is not initialized. * * \warning This function must be called before the timer data can be * freed and before the timer callback function can be unmapped/unloaded. * * \param timer timer to destroy */ VLC_API void vlc_timer_destroy(vlc_timer_t timer); /** * Arms or disarms an initialized timer. * * This functions overrides any previous call to itself. * * \note A timer can fire later than requested due to system scheduling * limitations. An interval timer can fail to trigger sometimes, either because * the system is busy or suspended, or because a previous iteration of the * timer is still running. See also vlc_timer_getoverrun(). * * \param timer initialized timer * \param absolute the timer value origin is the same as mdate() if true, * the timer value is relative to now if false. * \param value zero to disarm the timer, otherwise the initial time to wait * before firing the timer. * \param interval zero to fire the timer just once, otherwise the timer * repetition interval. */ VLC_API void vlc_timer_schedule(vlc_timer_t timer, bool absolute, mtime_t value, mtime_t interval); /** * Fetches and resets the overrun counter for a timer. * * This functions returns the number of times that the interval timer should * have fired, but the callback was not invoked due to scheduling problems. * The call resets the counter to zero. * * \param timer initialized timer * \return the timer overrun counter (typically zero) */  Rémi Denis-Courmont committed May 07, 2011 982 VLC_API unsigned vlc_timer_getoverrun(vlc_timer_t) VLC_USED;  Rémi Denis-Courmont committed Jun 02, 2009 983   Rémi Denis-Courmont committed Jul 01, 2015 984 985 986 987 988 /** * Count CPUs. * * \return number of available (logical) CPUs. */  Rémi Denis-Courmont committed Jun 29, 2011 989 990 VLC_API unsigned vlc_GetCPUCount(void);  Rémi Denis-Courmont committed May 28, 2016 991 992 993 994 995 996 997 998 enum { VLC_CLEANUP_PUSH, VLC_CLEANUP_POP, VLC_CANCEL_ADDR_SET, VLC_CANCEL_ADDR_CLEAR, };  Rémi Denis-Courmont committed Oct 13, 2012 999 #if defined (LIBVLC_USE_PTHREAD_CLEANUP)  Rémi Denis-Courmont committed Aug 27, 2008 1000 /**  Rémi Denis-Courmont committed Jul 01, 2015 1001 1002 1003 1004 1005 1006  * Registers a thread cancellation handler. * * This pushes a function to run if the thread is cancelled (or otherwise * exits prematurely). * * If multiple procedures are registered,  Rémi Denis-Courmont committed Aug 27, 2008 1007 1008  * they are handled in last-in first-out order. *  Rémi Denis-Courmont committed Jul 01, 2015 1009  * \note Any call to vlc_cleanup_push() must paired with a call to  Rémi Denis-Courmont committed Jul 08, 2015 1010  * vlc_cleanup_pop().  Rémi Denis-Courmont committed Jul 01, 2015 1011 1012 1013 1014 1015  * \warning Branching into or out of the block between these two function calls * is not allowed (read: it will likely crash the whole process). * * \param routine procedure to call if the thread ends * \param arg argument for the procedure  Rémi Denis-Courmont committed Aug 27, 2008 1016 1017 1018 1019  */ # define vlc_cleanup_push( routine, arg ) pthread_cleanup_push (routine, arg) /**  Rémi Denis-Courmont committed Jul 01, 2015 1020 1021 1022 1023  * Unregisters the last cancellation handler. * * This pops the cancellation handler that was last pushed with * vlc_cleanup_push() in the calling thread.  Rémi Denis-Courmont committed Aug 27, 2008 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037  */ # define vlc_cleanup_pop( ) pthread_cleanup_pop (0) #else typedef struct vlc_cleanup_t vlc_cleanup_t; struct vlc_cleanup_t { vlc_cleanup_t *next; void (*proc) (void *); void *data; }; /* This macros opens a code block on purpose. This is needed for multiple  Rémi Denis-Courmont committed Jul 08, 2009 1038  * calls within a single function. This also prevent Win32 developers from  Rémi Denis-Courmont committed Aug 27, 2008 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048  * writing code that would break on POSIX (POSIX opens a block as well). */ # define vlc_cleanup_push( routine, arg ) \ do { \ vlc_cleanup_t vlc_cleanup_data = { NULL, routine, arg, }; \ vlc_control_cancel (VLC_CLEANUP_PUSH, &vlc_cleanup_data) # define vlc_cleanup_pop( ) \ vlc_control_cancel (VLC_CLEANUP_POP); \ } while (0)  Rémi Denis-Courmont committed Aug 18, 2013 1049 #endif /* !LIBVLC_USE_PTHREAD_CLEANUP */  Rémi Denis-Courmont committed Oct 13, 2012 1050   Rémi Denis-Courmont committed Aug 27, 2008 1051 1052 1053 1054 1055 1056 static inline void vlc_cleanup_lock (void *lock) { vlc_mutex_unlock ((vlc_mutex_t *)lock); } #define mutex_cleanup_push( lock ) vlc_cleanup_push (vlc_cleanup_lock, lock)  Rémi Denis-Courmont committed May 28, 2016 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 static inline void vlc_cancel_addr_set(void *addr) { vlc_control_cancel(VLC_CANCEL_ADDR_SET, addr); } static inline void vlc_cancel_addr_clear(void *addr) { vlc_control_cancel(VLC_CANCEL_ADDR_CLEAR, addr); }  Rémi Denis-Courmont committed Oct 20, 2008 1067 1068 1069 #ifdef __cplusplus /** * Helper C++ class to lock a mutex.  Rémi Denis-Courmont committed Jul 01, 2015 1070  *  Rémi Denis-Courmont committed Oct 20, 2008 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090  * The mutex is locked when the object is created, and unlocked when the object * is destroyed. */ class vlc_mutex_locker { private: vlc_mutex_t *lock; public: vlc_mutex_locker (vlc_mutex_t *m) : lock (m) { vlc_mutex_lock (lock); } ~vlc_mutex_locker (void) { vlc_mutex_unlock (lock); } }; #endif  Rémi Denis-Courmont committed Oct 13, 2012 1091 1092 enum {  Rémi Denis-Courmont committed Jun 03, 2010 1093 1094  VLC_AVCODEC_MUTEX = 0, VLC_GCRYPT_MUTEX,  Rémi Denis-Courmont committed Jun 03, 2010 1095  VLC_XLIB_MUTEX,  Rémi Denis-Courmont committed Jul 21, 2011 1096  VLC_MOSAIC_MUTEX,  Rémi Denis-Courmont committed Jul 21, 2011 1097  VLC_HIGHLIGHT_MUTEX,  Hugo Beauzée-Luyssen committed Jun 26, 2017 1098 1099 1100 #ifdef _WIN32 VLC_MTA_MUTEX, #endif  Rémi Denis-Courmont committed Jun 03, 2010 1101  /* Insert new entry HERE */  Rémi Denis-Courmont committed Jun 03, 2010 1102 1103 1104  VLC_MAX_MUTEX };  Rémi Denis-Courmont committed Jul 01, 2015 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 /** * Internal handler for global mutexes. * * Do not use this function directly. Use helper macros instead: * vlc_global_lock(), vlc_global_unlock(). */ VLC_API void vlc_global_mutex(unsigned, bool); /** * Acquires a global mutex. */ #define vlc_global_lock( n ) vlc_global_mutex(n, true) /** * Releases a global mutex. */ #define vlc_global_unlock( n ) vlc_global_mutex(n, false)  Rémi Denis-Courmont committed Jun 03, 2010 1122   Rémi Denis-Courmont committed Nov 09, 2015 1123 1124 /** @} */  Rémi Denis-Courmont committed Jan 22, 2008 1125 #endif /* !_VLC_THREADS_H */