1
0

reference-libobs-util-threading.rst 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. Threading
  2. =========
  3. Libobs provides a number of helper functions/types specifically for
  4. threading. The threading header will additionally provide access to
  5. pthread functions even on windows.
  6. .. code:: cpp
  7. #include <util/threading.h>
  8. Threading Types
  9. ---------------
  10. .. type:: os_event_t
  11. .. type:: os_sem_t
  12. General Thread Functions
  13. ------------------------
  14. .. function:: void os_set_thread_name(const char *name)
  15. Sets the name of the current thread.
  16. ----------------------
  17. Event Functions
  18. ---------------
  19. .. function:: int os_event_init(os_event_t **event, enum os_event_type type)
  20. Creates an event object.
  21. :param event: Pointer that receives a pointer to a new event object
  22. :param type: Can be one of the following values:
  23. - OS_EVENT_TYPE_AUTO - Automatically resets when
  24. signaled
  25. - OS_EVENT_TYPE_MANUAL - Stays signaled until the
  26. :c:func:`os_event_reset()` function is called
  27. :return: 0 if successful, negative otherwise
  28. ----------------------
  29. .. function:: void os_event_destroy(os_event_t *event)
  30. Destroys an event.
  31. :param event: An event object
  32. ----------------------
  33. .. function:: int os_event_wait(os_event_t *event)
  34. Waits for an event to signal.
  35. :param event: An event object
  36. :return: 0 if successful, negative otherwise
  37. ----------------------
  38. .. function:: int os_event_timedwait(os_event_t *event, unsigned long milliseconds)
  39. Waits a specific duration for an event to signal.
  40. :param event: An event object
  41. :param milliseconds: Milliseconds to wait
  42. :return: Can be one of the following values:
  43. - 0 - successful
  44. - ETIMEDOUT - Timed out
  45. - EINVAL - An unexpected error occured
  46. ----------------------
  47. .. function:: int os_event_try(os_event_t *event)
  48. Checks for a signaled state without waiting.
  49. :param event: An event object
  50. :return: Can be one of the following values:
  51. - 0 - successful
  52. - EAGAIN - The event is not signaled
  53. - EINVAL - An unexpected error occured
  54. ----------------------
  55. .. function:: int os_event_signal(os_event_t *event)
  56. Signals the event.
  57. :param event: An event object
  58. :return: 0 if successful, negative otherwise
  59. ----------------------
  60. .. function:: void os_event_reset(os_event_t *event)
  61. Resets the signaled state of the event.
  62. :param event: An event object
  63. ----------------------
  64. Semaphore Functions
  65. -------------------
  66. .. function:: int os_sem_init(os_sem_t **sem, int value)
  67. Creates a semaphore object.
  68. :param sem: Pointer that receives a pointer to the semaphore object
  69. :param value: Initial value of the semaphore
  70. :return: 0 if successful, negative otherwise
  71. ----------------------
  72. .. function:: void os_sem_destroy(os_sem_t *sem)
  73. Destroys a sempahore object.
  74. :param sem: Semaphore object
  75. ----------------------
  76. .. function:: int os_sem_post(os_sem_t *sem)
  77. Increments the semaphore.
  78. :param sem: Semaphore object
  79. :return: 0 if successful, negative otherwise
  80. ----------------------
  81. .. function:: int os_sem_wait(os_sem_t *sem)
  82. Decrements the semphore or waits until the semaphore has been
  83. incremented.
  84. :param sem: Semaphore object
  85. :return: 0 if successful, negative otherwise
  86. ---------------------
  87. Atomic Inline Functions
  88. -----------------------
  89. .. function:: long os_atomic_inc_long(volatile long *val)
  90. Increments a long variable atomically.
  91. ---------------------
  92. .. function:: long os_atomic_dec_long(volatile long *val)
  93. Decrements a long variable atomically.
  94. ---------------------
  95. .. function:: long os_atomic_set_long(volatile long *ptr, long val)
  96. Sets the value of a long variable atomically.
  97. ---------------------
  98. .. function:: long os_atomic_load_long(const volatile long *ptr)
  99. Gets the value of a long variable atomically.
  100. ---------------------
  101. .. function:: bool os_atomic_compare_swap_long(volatile long *val, long old_val, long new_val)
  102. Swaps the value of a long variable atomically if its value matches.
  103. ---------------------
  104. .. function:: bool os_atomic_set_bool(volatile bool *ptr, bool val)
  105. Sets the value of a boolean variable atomically.
  106. ---------------------
  107. .. function:: bool os_atomic_load_bool(const volatile bool *ptr)
  108. Gets the value of a boolean variable atomically.