threading_impl.h 11 KB

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