reference-libobs-util-threading.rst 4.4 KB
Newer Older
J
jp9000 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
Threading
=========

Libobs provides a number of helper functions/types specifically for
threading.  The threading header will additionally provide access to
pthread functions even on windows.

.. code:: cpp

   #include <util/threading.h>


Threading Types
---------------

.. type:: os_event_t
.. type:: os_sem_t


General Thread Functions
------------------------

.. function:: void os_set_thread_name(const char *name)

   Sets the name of the current thread.

----------------------


Event Functions
---------------

.. function:: int  os_event_init(os_event_t **event, enum os_event_type type)

   Creates an event object.

   :param event: Pointer that receives a pointer to a new event object
   :param type:  Can be one of the following values:

                 - OS_EVENT_TYPE_AUTO - Automatically resets when
                   signaled
                 - OS_EVENT_TYPE_MANUAL - Stays signaled until the
                   :c:func:`os_event_reset()` function is called

   :return:      0 if successful, negative otherwise

----------------------

.. function:: void os_event_destroy(os_event_t *event)

   Destroys an event.

   :param event: An event object

----------------------

.. function:: int  os_event_wait(os_event_t *event)

   Waits for an event to signal.

   :param event: An event object
   :return:      0 if successful, negative otherwise

----------------------

.. function:: int  os_event_timedwait(os_event_t *event, unsigned long milliseconds)

   Waits a specific duration for an event to signal.

   :param event:        An event object
   :param milliseconds: Milliseconds to wait
   :return:             Can be one of the following values:
   
                        - 0 - successful
                        - ETIMEDOUT - Timed out
L
luz.paz 已提交
76
                        - EINVAL - An unexpected error occurred
J
jp9000 已提交
77 78 79 80 81 82 83 84 85 86 87 88

----------------------

.. function:: int  os_event_try(os_event_t *event)

   Checks for a signaled state without waiting.

   :param event: An event object
   :return:             Can be one of the following values:
   
                        - 0 - successful
                        - EAGAIN - The event is not signaled
L
luz.paz 已提交
89
                        - EINVAL - An unexpected error occurred
J
jp9000 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

----------------------

.. function:: int  os_event_signal(os_event_t *event)

   Signals the event.

   :param event: An event object
   :return:      0 if successful, negative otherwise

----------------------

.. function:: void os_event_reset(os_event_t *event)

   Resets the signaled state of the event.

   :param event: An event object

----------------------


Semaphore Functions
-------------------

.. function:: int  os_sem_init(os_sem_t **sem, int value)

   Creates a semaphore object.

   :param sem:   Pointer that receives a pointer to the semaphore object
   :param value: Initial value of the semaphore
   :return:      0 if successful, negative otherwise

----------------------

.. function:: void os_sem_destroy(os_sem_t *sem)

L
luz.paz 已提交
126
   Destroys a semaphore object.
J
jp9000 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

   :param sem:   Semaphore object

----------------------

.. function:: int  os_sem_post(os_sem_t *sem)

   Increments the semaphore.

   :param sem:   Semaphore object
   :return:      0 if successful, negative otherwise

----------------------

.. function:: int  os_sem_wait(os_sem_t *sem)

L
luz.paz 已提交
143
   Decrements the semaphore or waits until the semaphore has been
J
jp9000 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
   incremented.

   :param sem:   Semaphore object
   :return:      0 if successful, negative otherwise

---------------------


Atomic Inline Functions
-----------------------

.. function:: long os_atomic_inc_long(volatile long *val)

   Increments a long variable atomically.

---------------------

.. function:: long os_atomic_dec_long(volatile long *val)

   Decrements a long variable atomically.

---------------------

.. function:: long os_atomic_set_long(volatile long *ptr, long val)

   Sets the value of a long variable atomically.

---------------------

.. function:: long os_atomic_load_long(const volatile long *ptr)

   Gets the value of a long variable atomically.

---------------------

.. function:: bool os_atomic_compare_swap_long(volatile long *val, long old_val, long new_val)

   Swaps the value of a long variable atomically if its value matches.

---------------------

.. function:: bool os_atomic_set_bool(volatile bool *ptr, bool val)

   Sets the value of a boolean variable atomically.

---------------------

.. function:: bool os_atomic_load_bool(const volatile bool *ptr)

   Gets the value of a boolean variable atomically.