threading_impl.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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-2012 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. * The processing is arranged in a way to prevent call stack depth growth
  47. * as more and more nested calls are posted.
  48. *
  49. * Note that it is not necessary to create and assign a self-threaded
  50. * implementation to a world, as there is a global one used by default
  51. * if no implementation is explicitly assigned. You should only assign
  52. * each world an individual threading implementation instance if simulations
  53. * need to be run in parallel in multiple threads for the worlds.
  54. *
  55. * @returns ID of object allocated or NULL on failure
  56. *
  57. * @ingroup threading
  58. * @see dThreadingAllocateMultiThreadedImplementation
  59. * @see dThreadingFreeImplementation
  60. */
  61. ODE_API dThreadingImplementationID dThreadingAllocateSelfThreadedImplementation();
  62. /**
  63. * @brief Allocates built-in multi-threaded threading implementation object.
  64. *
  65. * A multi-threaded implementation is a type of implementation that has to be
  66. * served with a thread pool. The thread pool can be either the built-in ODE object
  67. * or set of external threads that dedicate themselves to this purpose and stay
  68. * in ODE until implementation releases them.
  69. *
  70. * @returns ID of object allocated or NULL on failure
  71. *
  72. * @ingroup threading
  73. * @see dThreadingThreadPoolServeMultiThreadedImplementation
  74. * @see dExternalThreadingServeMultiThreadedImplementation
  75. * @see dThreadingFreeImplementation
  76. */
  77. ODE_API dThreadingImplementationID dThreadingAllocateMultiThreadedImplementation();
  78. /**
  79. * @brief Retrieves the functions record of a built-in threading implementation.
  80. *
  81. * The implementation can be the one allocated by ODE (from @c dThreadingAllocateMultiThreadedImplementation).
  82. * Do not use this function with self-made custom implementations -
  83. * they should be bundled with their own set of functions.
  84. *
  85. * @param impl Threading implementation ID
  86. * @returns Pointer to associated functions structure
  87. *
  88. * @ingroup threading
  89. * @see dThreadingAllocateMultiThreadedImplementation
  90. */
  91. ODE_API const dThreadingFunctionsInfo *dThreadingImplementationGetFunctions(dThreadingImplementationID impl);
  92. /**
  93. * @brief Requests a built-in implementation to release threads serving it.
  94. *
  95. * The function unblocks threads employed in implementation serving and lets them
  96. * return to from where they originate. It's the responsibility of external code
  97. * to make sure all the calls to ODE that might be dependent on given threading
  98. * implementation object had already returned before this call is made. If threading
  99. * implementation is still processing some posted calls while this function is
  100. * invoked the behavior is implementation dependent.
  101. *
  102. * This call is to be used to request the threads to be released before waiting
  103. * for them in host pool or before waiting for them to exit. Implementation object
  104. * must not be destroyed before it is known that all the serving threads have already
  105. * returned from it. If implementation needs to be reused after this function is called
  106. * and all the threads have exited from it a call to @c dThreadingImplementationCleanupForRestart
  107. * must be made to restore internal state of the object.
  108. *
  109. * If this function is called for self-threaded built-in threading implementation
  110. * the call has no effect.
  111. *
  112. * @param impl Threading implementation ID
  113. *
  114. * @ingroup threading
  115. * @see dThreadingAllocateMultiThreadedImplementation
  116. * @see dThreadingImplementationCleanupForRestart
  117. */
  118. ODE_API void dThreadingImplementationShutdownProcessing(dThreadingImplementationID impl);
  119. /**
  120. * @brief Restores built-in implementation's state to let it be reused after shutdown.
  121. *
  122. * If a multi-threaded built-in implementation needs to be reused after a call
  123. * to @c dThreadingImplementationShutdownProcessing this call is to be made to
  124. * restore object's internal state. After that the implementation can be served again.
  125. *
  126. * If this function is called for self-threaded built-in threading implementation
  127. * the call has no effect.
  128. *
  129. * @param impl Threading implementation ID
  130. *
  131. * @ingroup threading
  132. * @see dThreadingAllocateMultiThreadedImplementation
  133. * @see dThreadingImplementationShutdownProcessing
  134. */
  135. ODE_API void dThreadingImplementationCleanupForRestart(dThreadingImplementationID impl);
  136. /**
  137. * @brief Deletes an instance of built-in threading implementation.
  138. *
  139. * @warning A care must be taken to make sure the implementation is unassigned
  140. * from all the objects it was assigned to and that there are no more threads
  141. * serving it before attempting to call this function.
  142. *
  143. * @param impl Threading implementation ID
  144. *
  145. * @ingroup threading
  146. * @see dThreadingAllocateMultiThreadedImplementation
  147. */
  148. ODE_API void dThreadingFreeImplementation(dThreadingImplementationID impl);
  149. typedef void (dThreadReadyToServeCallback)(void *callback_context);
  150. /**
  151. * @brief An entry point for external threads that would like to serve a built-in
  152. * threading implementation object.
  153. *
  154. * A thread that calls this function remains blocked in ODE and serves implementation
  155. * object @p impl until being released with @c dThreadingImplementationShutdownProcessing call.
  156. * This function can be used to provide external threads instead of ODE's built-in
  157. * thread pools.
  158. *
  159. * The optional callback @readiness_callback is called after the thread has reached
  160. * and has registered within the implementation. The implementation should not
  161. * be used until all dedicated threads register within it as otherwise it will not
  162. * have accurate view of the execution resources available.
  163. *
  164. * @param impl Threading implementation ID
  165. * @param readiness_callback Optional readiness callback to be called after thread enters the implementation
  166. * @param callback_context A value to be passed as parameter to readiness callback
  167. *
  168. * @ingroup threading
  169. * @see dThreadingAllocateMultiThreadedImplementation
  170. * @see dThreadingImplementationShutdownProcessing
  171. */
  172. ODE_API void dExternalThreadingServeMultiThreadedImplementation(dThreadingImplementationID impl,
  173. dThreadReadyToServeCallback *readiness_callback/*=NULL*/, void *callback_context/*=NULL*/);
  174. /**
  175. * @brief Creates an instance of built-in thread pool object that can be used to serve
  176. * multi-threaded threading implementations.
  177. *
  178. * The threads allocated inherit priority of caller thread. Their affinity is not
  179. * explicitly adjusted and gets the value the system assigns by default. Threads
  180. * have their stack memory fully committed immediately on start. On POSIX platforms
  181. * threads are started with all the possible signals blocked. Threads execute
  182. * calls to @c dAllocateODEDataForThread with @p ode_data_allocate_flags
  183. * on initialization.
  184. *
  185. * On POSIX platforms this function must be called with signals masked
  186. * or other measures must be taken to prevent reception of signals by calling thread
  187. * for the duration of the call.
  188. *
  189. * @param thread_count Number of threads to start in pool
  190. * @param stack_size Size of stack to be used for every thread or 0 for system default value
  191. * @param ode_data_allocate_flags Flags to be passed to @c dAllocateODEDataForThread on behalf of each thread
  192. * @returns ID of object allocated or NULL on failure
  193. *
  194. * @ingroup threading
  195. * @see dThreadingAllocateMultiThreadedImplementation
  196. * @see dThreadingImplementationShutdownProcessing
  197. * @see dThreadingFreeThreadPool
  198. */
  199. ODE_API dThreadingThreadPoolID dThreadingAllocateThreadPool(unsigned thread_count,
  200. size_t stack_size, unsigned int ode_data_allocate_flags, void *reserved/*=NULL*/);
  201. /**
  202. * @brief Commands an instance of built-in thread pool to serve a built-in multi-threaded
  203. * threading implementation.
  204. *
  205. * A pool can only serve one threading implementation at a time.
  206. * Call @c dThreadingImplementationShutdownProcessing to release pool threads
  207. * from implementation serving and make them idle. Pool threads must be released
  208. * from any implementations before pool is attempted to be deleted.
  209. *
  210. * This function waits for threads to register within implementation before returning.
  211. * So, after the function call exits the implementation can be used immediately.
  212. *
  213. * @param pool Thread pool ID to serve the implementation
  214. * @param impl Implementation ID of implementation to be served
  215. *
  216. * @ingroup threading
  217. * @see dThreadingAllocateThreadPool
  218. * @see dThreadingAllocateMultiThreadedImplementation
  219. * @see dThreadingImplementationShutdownProcessing
  220. */
  221. ODE_API void dThreadingThreadPoolServeMultiThreadedImplementation(dThreadingThreadPoolID pool, dThreadingImplementationID impl);
  222. /**
  223. * @brief Waits until all pool threads are released from threading implementation
  224. * they might be serving.
  225. *
  226. * The function can be used after a call to @c dThreadingImplementationShutdownProcessing
  227. * to make sure all the threads have already been released by threading implementation
  228. * and it can be deleted or it can be cleaned up for restart and served by another pool
  229. * or this pool's threads can be used to serve another threading implementation.
  230. *
  231. * Note that is it not necessary to call this function before pool destruction
  232. * since @c dThreadingFreeThreadPool performs similar wait operation implicitly on its own.
  233. *
  234. * It is OK to call this function even if pool was not serving any threading implementation
  235. * in which case the call exits immediately with minimal delay.
  236. *
  237. * @param pool Thread pool ID to wait for
  238. *
  239. * @ingroup threading
  240. * @see dThreadingAllocateThreadPool
  241. * @see dThreadingImplementationShutdownProcessing
  242. * @see dThreadingFreeThreadPool
  243. */
  244. ODE_API void dThreadingThreadPoolWaitIdleState(dThreadingThreadPoolID pool);
  245. /**
  246. * @brief Deletes a built-in thread pool instance.
  247. *
  248. * The pool threads must be released from any implementations they might be serving
  249. * before this function is called. Otherwise the call is going to block
  250. * and wait until pool's threads return.
  251. *
  252. * @param pool Thread pool ID to delete
  253. *
  254. * @ingroup threading
  255. * @see dThreadingAllocateThreadPool
  256. * @see dThreadingImplementationShutdownProcessing
  257. */
  258. ODE_API void dThreadingFreeThreadPool(dThreadingThreadPoolID pool);
  259. #ifdef __cplusplus
  260. }
  261. #endif
  262. #endif /* #ifndef _ODE_THREADING_IMPL_H_ */