threading_impl.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*************************************************************************
  2. * *
  3. * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith. *
  4. * All rights reserved. Email: [email protected] Web: www.q12.org *
  5. * *
  6. * Builtin ODE threading implementation header. *
  7. * Copyright (C) 2011-2020 Oleh Derevenko. All rights reserved. *
  8. * e-mail: [email protected] (change all "a" to "e") *
  9. * *
  10. * This library is free software; you can redistribute it and/or *
  11. * modify it under the terms of EITHER: *
  12. * (1) The GNU Lesser General Public License as published by the Free *
  13. * Software Foundation; either version 2.1 of the License, or (at *
  14. * your option) any later version. The text of the GNU Lesser *
  15. * General Public License is included with this library in the *
  16. * file LICENSE.TXT. *
  17. * (2) The BSD-style license that is included with this library in *
  18. * the file LICENSE-BSD.TXT. *
  19. * *
  20. * This library is distributed in the hope that it will be useful, *
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files *
  23. * LICENSE.TXT and LICENSE-BSD.TXT for more details. *
  24. * *
  25. *************************************************************************/
  26. /*
  27. * A threading implementation built into ODE for those who does not care to
  28. * or can't implement an own one.
  29. */
  30. #ifndef _ODE_THREADING_IMPL_H_
  31. #define _ODE_THREADING_IMPL_H_
  32. #include <ode/odeconfig.h>
  33. #include <ode/threading.h>
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. struct dxThreadingThreadPool;
  38. typedef struct dxThreadingThreadPool *dThreadingThreadPoolID;
  39. /**
  40. * @brief Allocates built-in self-threaded threading implementation object.
  41. *
  42. * A self-threaded implementation is a type of implementation that performs
  43. * processing of posted calls by means of caller thread itself. This type of
  44. * implementation does not need thread pool to serve it.
  45. *
  46. * Note that since May 9th, 2017 (rev. #2066) the Self-Threaded implementation
  47. * returns 0 rather than 1 as available thread count to distinguish from
  48. * thread pools with just one thread in them.
  49. *
  50. * The processing is arranged in a way to prevent call stack depth growth
  51. * as more and more nested calls are posted.
  52. *
  53. * Note that it is not necessary to create and assign a self-threaded
  54. * implementation to a world, as there is a global one used by default
  55. * if no implementation is explicitly assigned. You should only assign
  56. * each world an individual threading implementation instance if simulations
  57. * need to be run in parallel in multiple threads for the worlds.
  58. *
  59. * @returns ID of object allocated or NULL on failure
  60. *
  61. * @ingroup threading
  62. * @see dThreadingAllocateMultiThreadedImplementation
  63. * @see dThreadingFreeImplementation
  64. */
  65. ODE_API dThreadingImplementationID dThreadingAllocateSelfThreadedImplementation();
  66. /**
  67. * @brief Allocates built-in multi-threaded threading implementation object.
  68. *
  69. * A multi-threaded implementation is a type of implementation that has to be
  70. * served with a thread pool. The thread pool can be either the built-in ODE object
  71. * or set of external threads that dedicate themselves to this purpose and stay
  72. * in ODE until implementation releases them.
  73. *
  74. * @returns ID of object allocated or NULL on failure
  75. *
  76. * @ingroup threading
  77. * @see dThreadingThreadPoolServeMultiThreadedImplementation
  78. * @see dExternalThreadingServeMultiThreadedImplementation
  79. * @see dThreadingFreeImplementation
  80. */
  81. ODE_API dThreadingImplementationID dThreadingAllocateMultiThreadedImplementation();
  82. /**
  83. * @brief Retrieves the functions record of a built-in threading implementation.
  84. *
  85. * The implementation can be the one allocated by ODE (from @c dThreadingAllocateMultiThreadedImplementation).
  86. * Do not use this function with self-made custom implementations -
  87. * they should be bundled with their own set of functions.
  88. *
  89. * @param impl Threading implementation ID
  90. * @returns Pointer to associated functions structure
  91. *
  92. * @ingroup threading
  93. * @see dThreadingAllocateMultiThreadedImplementation
  94. */
  95. ODE_API const dThreadingFunctionsInfo *dThreadingImplementationGetFunctions(dThreadingImplementationID impl);
  96. /**
  97. * @brief Requests a built-in implementation to release threads serving it.
  98. *
  99. * The function unblocks threads employed in implementation serving and lets them
  100. * return to from where they originate. It's the responsibility of external code
  101. * to make sure all the calls to ODE that might be dependent on given threading
  102. * implementation object had already returned before this call is made. If threading
  103. * implementation is still processing some posted calls while this function is
  104. * invoked the behavior is implementation dependent.
  105. *
  106. * This call is to be used to request the threads to be released before waiting
  107. * for them in host pool or before waiting for them to exit. Implementation object
  108. * must not be destroyed before it is known that all the serving threads have already
  109. * returned from it. If implementation needs to be reused after this function is called
  110. * and all the threads have exited from it a call to @c dThreadingImplementationCleanupForRestart
  111. * must be made to restore internal state of the object.
  112. *
  113. * If this function is called for self-threaded built-in threading implementation
  114. * the call has no effect.
  115. *
  116. * @param impl Threading implementation ID
  117. *
  118. * @ingroup threading
  119. * @see dThreadingAllocateMultiThreadedImplementation
  120. * @see dThreadingImplementationCleanupForRestart
  121. */
  122. ODE_API void dThreadingImplementationShutdownProcessing(dThreadingImplementationID impl);
  123. /**
  124. * @brief Restores built-in implementation's state to let it be reused after shutdown.
  125. *
  126. * If a multi-threaded built-in implementation needs to be reused after a call
  127. * to @c dThreadingImplementationShutdownProcessing this call is to be made to
  128. * restore object's internal state. After that the implementation can be served again.
  129. *
  130. * If this function is called for self-threaded built-in threading implementation
  131. * the call has no effect.
  132. *
  133. * @param impl Threading implementation ID
  134. *
  135. * @ingroup threading
  136. * @see dThreadingAllocateMultiThreadedImplementation
  137. * @see dThreadingImplementationShutdownProcessing
  138. */
  139. ODE_API void dThreadingImplementationCleanupForRestart(dThreadingImplementationID impl);
  140. /**
  141. * @brief Deletes an instance of built-in threading implementation.
  142. *
  143. * @warning A care must be taken to make sure the implementation is unassigned
  144. * from all the objects it was assigned to and that there are no more threads
  145. * serving it before attempting to call this function.
  146. *
  147. * @param impl Threading implementation ID
  148. *
  149. * @ingroup threading
  150. * @see dThreadingAllocateMultiThreadedImplementation
  151. */
  152. ODE_API void dThreadingFreeImplementation(dThreadingImplementationID impl);
  153. typedef void (dThreadReadyToServeCallback)(void *callback_context);
  154. /**
  155. * @brief An entry point for external threads that would like to serve a built-in
  156. * threading implementation object.
  157. *
  158. * A thread that calls this function remains blocked in ODE and serves implementation
  159. * object @p impl until being released with @c dThreadingImplementationShutdownProcessing call.
  160. * This function can be used to provide external threads instead of ODE's built-in
  161. * thread pools.
  162. *
  163. * The optional callback @readiness_callback is called after the thread has reached
  164. * and has registered within the implementation. The implementation should not
  165. * be used until all dedicated threads register within it as otherwise it will not
  166. * have accurate view of the execution resources available.
  167. *
  168. * @param impl Threading implementation ID
  169. * @param readiness_callback Optional readiness callback to be called after thread enters the implementation
  170. * @param callback_context A value to be passed as parameter to readiness callback
  171. *
  172. * @ingroup threading
  173. * @see dThreadingAllocateMultiThreadedImplementation
  174. * @see dThreadingImplementationShutdownProcessing
  175. */
  176. ODE_API void dExternalThreadingServeMultiThreadedImplementation(dThreadingImplementationID impl,
  177. dThreadReadyToServeCallback *readiness_callback/*=NULL*/, void *callback_context/*=NULL*/);
  178. /**
  179. * @brief Creates an instance of built-in thread pool object that can be used to serve
  180. * multi-threaded threading implementations.
  181. *
  182. * The threads allocated inherit priority of caller thread. Their affinity is not
  183. * explicitly adjusted and gets the value the system assigns by default. Threads
  184. * have their stack memory fully committed immediately on start. On POSIX platforms
  185. * threads are started with all the possible signals blocked. Threads execute
  186. * calls to @c dAllocateODEDataForThread with @p ode_data_allocate_flags
  187. * on initialization.
  188. *
  189. * On POSIX platforms this function must be called with signals masked
  190. * or other measures must be taken to prevent reception of signals by calling thread
  191. * for the duration of the call.
  192. *
  193. * @param thread_count Number of threads to start in pool
  194. * @param stack_size Size of stack to be used for every thread or 0 for system default value
  195. * @param ode_data_allocate_flags Flags to be passed to @c dAllocateODEDataForThread on behalf of each thread
  196. * @returns ID of object allocated or NULL on failure
  197. *
  198. * @ingroup threading
  199. * @see dThreadingAllocateMultiThreadedImplementation
  200. * @see dThreadingImplementationShutdownProcessing
  201. * @see dThreadingFreeThreadPool
  202. */
  203. ODE_API dThreadingThreadPoolID dThreadingAllocateThreadPool(unsigned thread_count,
  204. dsizeint stack_size, unsigned int ode_data_allocate_flags, void *reserved/*=NULL*/);
  205. /**
  206. * @brief Commands an instance of built-in thread pool to serve a built-in multi-threaded
  207. * threading implementation.
  208. *
  209. * A pool can only serve one threading implementation at a time.
  210. * Call @c dThreadingImplementationShutdownProcessing to release pool threads
  211. * from implementation serving and make them idle. Pool threads must be released
  212. * from any implementations before pool is attempted to be deleted.
  213. *
  214. * This function waits for threads to register within implementation before returning.
  215. * So, after the function call exits the implementation can be used immediately.
  216. *
  217. * @param pool Thread pool ID to serve the implementation
  218. * @param impl Implementation ID of implementation to be served
  219. *
  220. * @ingroup threading
  221. * @see dThreadingAllocateThreadPool
  222. * @see dThreadingAllocateMultiThreadedImplementation
  223. * @see dThreadingImplementationShutdownProcessing
  224. */
  225. ODE_API void dThreadingThreadPoolServeMultiThreadedImplementation(dThreadingThreadPoolID pool, dThreadingImplementationID impl);
  226. /**
  227. * @brief Waits until all pool threads are released from threading implementation
  228. * they might be serving.
  229. *
  230. * The function can be used after a call to @c dThreadingImplementationShutdownProcessing
  231. * to make sure all the threads have already been released by threading implementation
  232. * and it can be deleted or it can be cleaned up for restart and served by another pool
  233. * or this pool's threads can be used to serve another threading implementation.
  234. *
  235. * Note that is it not necessary to call this function before pool destruction
  236. * since @c dThreadingFreeThreadPool performs similar wait operation implicitly on its own.
  237. *
  238. * It is OK to call this function even if pool was not serving any threading implementation
  239. * in which case the call exits immediately with minimal delay.
  240. *
  241. * @param pool Thread pool ID to wait for
  242. *
  243. * @ingroup threading
  244. * @see dThreadingAllocateThreadPool
  245. * @see dThreadingImplementationShutdownProcessing
  246. * @see dThreadingFreeThreadPool
  247. */
  248. ODE_API void dThreadingThreadPoolWaitIdleState(dThreadingThreadPoolID pool);
  249. /**
  250. * @brief Deletes a built-in thread pool instance.
  251. *
  252. * The pool threads must be released from any implementations they might be serving
  253. * before this function is called. Otherwise the call is going to block
  254. * and wait until pool's threads return.
  255. *
  256. * @param pool Thread pool ID to delete
  257. *
  258. * @ingroup threading
  259. * @see dThreadingAllocateThreadPool
  260. * @see dThreadingImplementationShutdownProcessing
  261. */
  262. ODE_API void dThreadingFreeThreadPool(dThreadingThreadPoolID pool);
  263. #ifdef __cplusplus
  264. }
  265. #endif
  266. #endif /* #ifndef _ODE_THREADING_IMPL_H_ */