thread.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * The copyright in this software is being made available under the 2-clauses
  3. * BSD License, included below. This software may be subject to other third
  4. * party and contributor rights, including patent rights, and no such rights
  5. * are granted under this license.
  6. *
  7. * Copyright (c) 2016, Even Rouault
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #ifndef THREAD_H
  32. #define THREAD_H
  33. #include "openjpeg.h"
  34. /**
  35. @file thread.h
  36. @brief Thread API
  37. The functions in thread.c have for goal to manage mutex, conditions, thread
  38. creation and thread pools that accept jobs.
  39. */
  40. /** @defgroup THREAD THREAD - Mutex, conditions, threads and thread pools */
  41. /*@{*/
  42. /** @name Mutex */
  43. /*@{*/
  44. /** Opaque type for a mutex */
  45. typedef struct opj_mutex_t opj_mutex_t;
  46. /** Creates a mutex.
  47. * @return the mutex or NULL in case of error (can for example happen if the library
  48. * is built without thread support)
  49. */
  50. opj_mutex_t* opj_mutex_create(void);
  51. /** Lock/acquire the mutex.
  52. * @param mutex the mutex to acquire.
  53. */
  54. void opj_mutex_lock(opj_mutex_t* mutex);
  55. /** Unlock/release the mutex.
  56. * @param mutex the mutex to release.
  57. */
  58. void opj_mutex_unlock(opj_mutex_t* mutex);
  59. /** Destroy a mutex
  60. * @param mutex the mutex to destroy.
  61. */
  62. void opj_mutex_destroy(opj_mutex_t* mutex);
  63. /*@}*/
  64. /** @name Condition */
  65. /*@{*/
  66. /** Opaque type for a condition */
  67. typedef struct opj_cond_t opj_cond_t;
  68. /** Creates a condition.
  69. * @return the condition or NULL in case of error (can for example happen if the library
  70. * is built without thread support)
  71. */
  72. opj_cond_t* opj_cond_create(void);
  73. /** Wait for the condition to be signaled.
  74. * The semantics is the same as the POSIX pthread_cond_wait.
  75. * The provided mutex *must* be acquired before calling this function, and
  76. * released afterwards.
  77. * The mutex will be released by this function while it must wait for the condition
  78. * and reacquired afterwards.
  79. * In some particular situations, the function might return even if the condition is not signaled
  80. * with opj_cond_signal(), hence the need to check with an application level
  81. * mechanism.
  82. *
  83. * Waiting thread :
  84. * \code
  85. * opj_mutex_lock(mutex);
  86. * while( !some_application_level_condition )
  87. * {
  88. * opj_cond_wait(cond, mutex);
  89. * }
  90. * opj_mutex_unlock(mutex);
  91. * \endcode
  92. *
  93. * Signaling thread :
  94. * \code
  95. * opj_mutex_lock(mutex);
  96. * some_application_level_condition = TRUE;
  97. * opj_cond_signal(cond);
  98. * opj_mutex_unlock(mutex);
  99. * \endcode
  100. *
  101. * @param cond the condition to wait.
  102. * @param mutex the mutex (in acquired state before calling this function)
  103. */
  104. void opj_cond_wait(opj_cond_t* cond, opj_mutex_t* mutex);
  105. /** Signal waiting threads on a condition.
  106. * One of the thread waiting with opj_cond_wait() will be waken up.
  107. * It is strongly advised that this call is done with the mutex that is used
  108. * by opj_cond_wait(), in a acquired state.
  109. * @param cond the condition to signal.
  110. */
  111. void opj_cond_signal(opj_cond_t* cond);
  112. /** Destroy a condition
  113. * @param cond the condition to destroy.
  114. */
  115. void opj_cond_destroy(opj_cond_t* cond);
  116. /*@}*/
  117. /** @name Thread */
  118. /*@{*/
  119. /** Opaque type for a thread handle */
  120. typedef struct opj_thread_t opj_thread_t;
  121. /** User function to execute in a thread
  122. * @param user_data user data provided with opj_thread_create()
  123. */
  124. typedef void (*opj_thread_fn)(void* user_data);
  125. /** Creates a new thread.
  126. * @param thread_fn Function to run in the new thread.
  127. * @param user_data user data provided to the thread function. Might be NULL.
  128. * @return a thread handle or NULL in case of failure (can for example happen if the library
  129. * is built without thread support)
  130. */
  131. opj_thread_t* opj_thread_create(opj_thread_fn thread_fn, void* user_data);
  132. /** Wait for a thread to be finished and release associated resources to the
  133. * thread handle.
  134. * @param thread the thread to wait for being finished.
  135. */
  136. void opj_thread_join(opj_thread_t* thread);
  137. /*@}*/
  138. /** @name Thread local storage */
  139. /*@{*/
  140. /** Opaque type for a thread local storage */
  141. typedef struct opj_tls_t opj_tls_t;
  142. /** Get a thread local value corresponding to the provided key.
  143. * @param tls thread local storage handle
  144. * @param key key whose value to retrieve.
  145. * @return value associated with the key, or NULL is missing.
  146. */
  147. void* opj_tls_get(opj_tls_t* tls, int key);
  148. /** Type of the function used to free a TLS value */
  149. typedef void (*opj_tls_free_func)(void* value);
  150. /** Set a thread local value corresponding to the provided key.
  151. * @param tls thread local storage handle
  152. * @param key key whose value to set.
  153. * @param value value to set (may be NULL).
  154. * @param free_func function to call currently installed value.
  155. * @return OPJ_TRUE if successful.
  156. */
  157. OPJ_BOOL opj_tls_set(opj_tls_t* tls, int key, void* value,
  158. opj_tls_free_func free_func);
  159. /*@}*/
  160. /** @name Thread pool */
  161. /*@{*/
  162. /** Opaque type for a thread pool */
  163. typedef struct opj_thread_pool_t opj_thread_pool_t;
  164. /** Create a new thread pool.
  165. * num_thread must nominally be >= 1 to create a real thread pool. If num_threads
  166. * is negative or null, then a dummy thread pool will be created. All functions
  167. * operating on the thread pool will work, but job submission will be run
  168. * synchronously in the calling thread.
  169. *
  170. * @param num_threads the number of threads to allocate for this thread pool.
  171. * @return a thread pool handle, or NULL in case of failure (can for example happen if the library
  172. * is built without thread support)
  173. */
  174. opj_thread_pool_t* opj_thread_pool_create(int num_threads);
  175. /** User function to execute in a thread
  176. * @param user_data user data provided with opj_thread_create()
  177. * @param tls handle to thread local storage
  178. */
  179. typedef void (*opj_job_fn)(void* user_data, opj_tls_t* tls);
  180. /** Submit a new job to be run by one of the thread in the thread pool.
  181. * The job ( thread_fn, user_data ) will be added in the queue of jobs managed
  182. * by the thread pool, and run by the first thread that is no longer busy.
  183. *
  184. * @param tp the thread pool handle.
  185. * @param job_fn Function to run. Must not be NULL.
  186. * @param user_data User data provided to thread_fn.
  187. * @return OPJ_TRUE if the job was successfully submitted.
  188. */
  189. OPJ_BOOL opj_thread_pool_submit_job(opj_thread_pool_t* tp, opj_job_fn job_fn,
  190. void* user_data);
  191. /** Wait that no more than max_remaining_jobs jobs are remaining in the queue of
  192. * the thread pool. The aim of this function is to avoid submitting too many
  193. * jobs while the thread pool cannot cope fast enough with them, which would
  194. * result potentially in out-of-memory situations with too many job descriptions
  195. * being queued.
  196. *
  197. * @param tp the thread pool handle
  198. * @param max_remaining_jobs maximum number of jobs allowed to be queued without waiting.
  199. */
  200. void opj_thread_pool_wait_completion(opj_thread_pool_t* tp,
  201. int max_remaining_jobs);
  202. /** Return the number of threads associated with the thread pool.
  203. *
  204. * @param tp the thread pool handle.
  205. * @return number of threads associated with the thread pool.
  206. */
  207. int opj_thread_pool_get_thread_count(opj_thread_pool_t* tp);
  208. /** Destroy a thread pool.
  209. * @param tp the thread pool handle.
  210. */
  211. void opj_thread_pool_destroy(opj_thread_pool_t* tp);
  212. /*@}*/
  213. /*@}*/
  214. #endif /* THREAD_H */