threading.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. * Threading support header file. *
  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. * ODE threading support interfaces
  28. */
  29. #ifndef _ODE_THREADING_H_
  30. #define _ODE_THREADING_H_
  31. #include <ode/odeconfig.h>
  32. // Include <time.h> since time_t is used and it is not available by default in some OSes
  33. #include <time.h>
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. struct dxThreadingImplementation;
  38. typedef struct dxThreadingImplementation *dThreadingImplementationID;
  39. typedef unsigned dmutexindex_t;
  40. struct dxMutexGroup;
  41. typedef struct dxMutexGroup *dMutexGroupID;
  42. /**
  43. * @brief Allocates a group of muteces.
  44. *
  45. * The Mutex allocated do not need to support recursive locking.
  46. *
  47. * The Mutex names are provided to aid in debugging and thread state tracking.
  48. *
  49. * @param impl Threading implementation ID
  50. * @param Mutex_count Number of Mutex to create
  51. * @Mutex_names_ptr Pointer to optional Mutex names array to be associated with individual Mutex
  52. * @returns MutexGroup ID or NULL if error occurred.
  53. *
  54. * @ingroup threading
  55. * @see dMutexGroupFreeFunction
  56. * @see dMutexGroupMutexLockFunction
  57. * @see dMutexGroupMutexUnlockFunction
  58. */
  59. typedef dMutexGroupID dMutexGroupAllocFunction (dThreadingImplementationID impl, dmutexindex_t Mutex_count, const char *const *Mutex_names_ptr/*=NULL*/);
  60. /**
  61. * @brief Deletes a group of muteces.
  62. *
  63. * @param impl Threading implementation ID
  64. * @param mutex_group Mutex group to deallocate
  65. *
  66. * @ingroup threading
  67. * @see dMutexGroupAllocFunction
  68. * @see dMutexGroupMutexLockFunction
  69. * @see dMutexGroupMutexUnlockFunction
  70. */
  71. typedef void dMutexGroupFreeFunction (dThreadingImplementationID impl, dMutexGroupID mutex_group);
  72. /**
  73. * @brief Locks a mutex in a group of muteces.
  74. *
  75. * The function is to block execution until requested mutex can be locked.
  76. *
  77. * Note: Mutex provided may not support recursive locking. Calling this function
  78. * while mutex is already locked by current thread will result in unpredictable behavior.
  79. *
  80. * @param impl Threading implementation ID
  81. * @param mutex_group Mutex group to use for locking
  82. * @param mutex_index The index of mutex to be locked (0..Mutex_count - 1)
  83. *
  84. * @ingroup threading
  85. * @see dMutexGroupAllocFunction
  86. * @see dMutexGroupFreeFunction
  87. * @see dMutexGroupMutexUnlockFunction
  88. */
  89. typedef void dMutexGroupMutexLockFunction (dThreadingImplementationID impl, dMutexGroupID mutex_group, dmutexindex_t mutex_index);
  90. /**
  91. * @brief Attempts to lock a mutex in a group of muteces.
  92. *
  93. * The function is to lock the requested mutex if it is unoccupied or
  94. * immediately return failure if mutex is already locked by other thread.
  95. *
  96. * Note: Mutex provided may not support recursive locking. Calling this function
  97. * while mutex is already locked by current thread will result in unpredictable behavior.
  98. *
  99. * @param impl Threading implementation ID
  100. * @param mutex_group Mutex group to use for locking
  101. * @param mutex_index The index of mutex to be locked (0..Mutex_count - 1)
  102. * @returns 1 for success (mutex is locked) and 0 for failure (mutex is not locked)
  103. *
  104. * @ingroup threading
  105. * @see dMutexGroupAllocFunction
  106. * @see dMutexGroupFreeFunction
  107. * @see dMutexGroupMutexLockFunction
  108. * @see dMutexGroupMutexUnlockFunction
  109. */
  110. /* typedef int dMutexGroupMutexTryLockFunction (dThreadingImplementationID impl, dMutexGroupID mutex_group, dmutexindex_t mutex_index);*/
  111. /**
  112. * @brief Unlocks a mutex in a group of muteces.
  113. *
  114. * The function is to unlock the given mutex provided it had been locked before.
  115. *
  116. * @param impl Threading implementation ID
  117. * @param mutex_group Mutex group to use for unlocking
  118. * @param mutex_index The index of mutex to be unlocked (0..Mutex_count - 1)
  119. *
  120. * @ingroup threading
  121. * @see dMutexGroupAllocFunction
  122. * @see dMutexGroupFreeFunction
  123. * @see dMutexGroupMutexLockFunction
  124. */
  125. typedef void dMutexGroupMutexUnlockFunction (dThreadingImplementationID impl, dMutexGroupID mutex_group, dmutexindex_t mutex_index);
  126. struct dxCallReleasee;
  127. typedef struct dxCallReleasee *dCallReleaseeID;
  128. struct dxCallWait;
  129. typedef struct dxCallWait *dCallWaitID;
  130. typedef size_t ddependencycount_t;
  131. typedef ptrdiff_t ddependencychange_t;
  132. typedef size_t dcallindex_t;
  133. typedef int dThreadedCallFunction(void *call_context, dcallindex_t instance_index,
  134. dCallReleaseeID this_releasee);
  135. typedef struct dxThreadedWaitTime
  136. {
  137. time_t wait_sec;
  138. unsigned long wait_nsec;
  139. } dThreadedWaitTime;
  140. /**
  141. * @brief Allocates a Wait ID that can be used to wait for a call.
  142. *
  143. * @param impl Threading implementation ID
  144. * @returns Wait ID or NULL if error occurred
  145. *
  146. * @ingroup threading
  147. * @see dThreadedCallWaitResetFunction
  148. * @see dThreadedCallWaitFreeFunction
  149. * @see dThreadedCallPostFunction
  150. * @see dThreadedCallWaitFunction
  151. */
  152. typedef dCallWaitID dThreadedCallWaitAllocFunction(dThreadingImplementationID impl);
  153. /**
  154. * @brief Resets a Wait ID so that it could be used to wait for another call.
  155. *
  156. * @param impl Threading implementation ID
  157. * @param call_wait Wait ID to reset
  158. *
  159. * @ingroup threading
  160. * @see dThreadedCallWaitAllocFunction
  161. * @see dThreadedCallWaitFreeFunction
  162. * @see dThreadedCallPostFunction
  163. * @see dThreadedCallWaitFunction
  164. */
  165. typedef void dThreadedCallWaitResetFunction(dThreadingImplementationID impl, dCallWaitID call_wait);
  166. /**
  167. * @brief Frees a Wait ID.
  168. *
  169. * @param impl Threading implementation ID
  170. * @param call_wait Wait ID to delete
  171. *
  172. * @ingroup threading
  173. * @see dThreadedCallWaitAllocFunction
  174. * @see dThreadedCallPostFunction
  175. * @see dThreadedCallWaitFunction
  176. */
  177. typedef void dThreadedCallWaitFreeFunction(dThreadingImplementationID impl, dCallWaitID call_wait);
  178. /**
  179. * @brief Post a function to be called in another thread.
  180. *
  181. * A call is scheduled to be executed asynchronously.
  182. *
  183. * A @a out_summary_fault variable can be provided for call to accumulate any
  184. * possible faults from its execution and execution of any possible sub-calls.
  185. * This variable gets result that @a call_func returns. Also, if dependent calls
  186. * are executed after the call already exits, the variable is also going to be
  187. * updated with results of all those calls before control is released to master.
  188. *
  189. * @a out_post_releasee parameter receives a value of @c dCallReleaseeID that can
  190. * later be used for @a dependent_releasee while scheduling sub-calls to make
  191. * current call depend on them. The value is only returned if @a dependencies_count
  192. * is not zero (i.e. if any dependencies are expected at all). The call is not going
  193. * to start until all its dependencies complete.
  194. *
  195. * In case if number of dependencies is unknown in advance 1 can be passed on call
  196. * scheduling. Then @c dThreadedCallDependenciesCountAlterFunction can be used to
  197. * add one more extra dependencies before scheduling each subcall. And then, after
  198. * all sub-calls had been scheduled, @c dThreadedCallDependenciesCountAlterFunction
  199. * can be used again to subtract initial extra dependency from total number.
  200. * Adding one dependency in advance is necessary to obtain releasee ID and to make
  201. * sure the call will not start and will not terminate before all sub-calls are scheduled.
  202. *
  203. * Extra dependencies can also be added from the call itself after it has already
  204. * been started (with parameter received in @c dThreadedCallFunction).
  205. * In that case those dependencies will start immediately or after call returns
  206. * but the call's master will not be released/notified until all additional
  207. * dependencies complete. This can be used to schedule sub-calls from a call and
  208. * then pass own job to another sub-call dependent on those initial sub-calls.
  209. *
  210. * By using @ call_wait it is possible to assign a Wait ID that can later
  211. * be passed into @c dThreadedCallWaitFunction to wait for call completion.
  212. *
  213. * If @a call_name is available (and it should!) the string must remain valid until
  214. * after call completion. In most cases this should be a static string literal.
  215. *
  216. * Since the function is an analogue of normal method call it is not supposed to fail.
  217. * Any complications with resource allocation on call scheduling should be
  218. * anticipated, avoided and worked around by implementation.
  219. *
  220. * @param impl Threading implementation ID
  221. * @param out_summary_fault Optional pointer to variable to be set to 1 if function
  222. * call (or any sub-call) fails internally, or 0 if all calls return success
  223. * @param out_post_releasee Optional pointer to variable to receive releasee ID
  224. * associated with the call
  225. * @param dependencies_count Number of dependencies that are going to reference
  226. * this call as dependent releasee
  227. * @param dependent_releasee Optional releasee ID to reference with this call
  228. * @param call_wait Optional Wait ID that can later be used to wait for the call
  229. * @param call_func Pointer to function to be called
  230. * @param call_context Context parameter to be passed into the call
  231. * @param instance_index Index parameter to be passed into the call
  232. * @param call_name Optional name to be associated with the call (for debugging and state tracking)
  233. *
  234. * @ingroup threading
  235. * @see dThreadedCallWaitFunction
  236. * @see dThreadedCallDependenciesCountAlterFunction
  237. * @see dThreadingImplResourcesForCallsPreallocateFunction
  238. */
  239. typedef void dThreadedCallPostFunction(dThreadingImplementationID impl, int *out_summary_fault/*=NULL*/,
  240. dCallReleaseeID *out_post_releasee/*=NULL*/, ddependencycount_t dependencies_count, dCallReleaseeID dependent_releasee/*=NULL*/,
  241. dCallWaitID call_wait/*=NULL*/,
  242. dThreadedCallFunction *call_func, void *call_context, dcallindex_t instance_index,
  243. const char *call_name/*=NULL*/);
  244. /**
  245. * @brief Add or remove extra dependencies from call that has been scheduled
  246. * or is in process of execution.
  247. *
  248. * Extra dependencies can be added to a call if exact number of sub-calls is
  249. * not known in advance at the moment the call is scheduled. Also, some dependencies
  250. * can be removed if sub-calls were planned but then dropped.
  251. *
  252. * In case if total dependency count of a call reaches zero by result of invoking
  253. * this function, the call is free to start executing immediately.
  254. *
  255. * After the call execution had been started, any additional dependencies can only
  256. * be added from the call function itself!
  257. *
  258. * @param impl Threading implementation ID
  259. * @param target_releasee ID of releasee to apply dependencies count change to
  260. * @param dependencies_count_change Number of dependencies to add or remove
  261. *
  262. * @ingroup threading
  263. * @see dThreadedCallPostFunction
  264. */
  265. typedef void dThreadedCallDependenciesCountAlterFunction(dThreadingImplementationID impl, dCallReleaseeID target_releasee,
  266. ddependencychange_t dependencies_count_change);
  267. /**
  268. * @brief Wait for a posted call to complete.
  269. *
  270. * Function blocks until a call identified by @a call_wait completes or
  271. * timeout elapses.
  272. *
  273. * IT IS ILLEGAL TO INVOKE THIS FUNCTION FROM WITHIN A THREADED CALL!
  274. * This is because doing so will block a physical thread and will require
  275. * increasing worker thread count to avoid starvation. Use call dependencies
  276. * if it is necessary make sure sub-calls have been completed instead!
  277. *
  278. * If @a timeout_time_ptr is NULL, the function waits without time limit. If @a timeout_time_ptr
  279. * points to zero value, the function only checks status and does not block.
  280. *
  281. * If @a wait_name is available (and it should!) the string must remain valid for
  282. * the duration of wait. In most cases this should be a static string literal.
  283. *
  284. * Function is not expected to return failures caused by system call faults as
  285. * those are hardly ever possible to be handled in this case anyway. In event of
  286. * system call fault the function is supposed to terminate application.
  287. *
  288. * @param impl Threading implementation ID
  289. * @param out_wait_status Optional pointer to variable to receive 1 if waiting succeeded
  290. * or 0 in case of timeout
  291. * @param call_wait Wait ID that had been passed to scheduling a call that needs to be waited for
  292. * @param timeout_time_ptr Optional pointer to time specification the wait must not
  293. * last longer than (pass NULL for infinite timeout)
  294. * @param wait_name Optional name to be associated with the wait (for debugging and state tracking)
  295. *
  296. * @ingroup threading
  297. * @see dThreadedCallPostFunction
  298. */
  299. typedef void dThreadedCallWaitFunction(dThreadingImplementationID impl, int *out_wait_status/*=NULL*/,
  300. dCallWaitID call_wait, const dThreadedWaitTime *timeout_time_ptr/*=NULL*/,
  301. const char *wait_name/*=NULL*/);
  302. /**
  303. * @brief Retrieve number of active threads that serve the implementation.
  304. *
  305. * @param impl Threading implementation ID
  306. * @returns Number of active threads
  307. *
  308. * @ingroup threading
  309. */
  310. typedef unsigned dThreadingImplThreadCountRetrieveFunction(dThreadingImplementationID impl);
  311. /**
  312. * @brief Preallocate resources to handle posted calls.
  313. *
  314. * The function is intended to make sure enough resources is preallocated for the
  315. * implementation to be able to handle posted calls. Then @c max_simultaneous_calls_estimate
  316. * is an estimate of how many posted calls can potentially be active or scheduled
  317. * at the same time. The value is usually derived from the way the calls are posted
  318. * in library code and dependencies between them.
  319. *
  320. * @warning While working on an implementation be prepared that the estimate provided
  321. * yet rarely but theoretically can be exceeded due to unpredictability of thread execution.
  322. *
  323. * This function is normally going to be invoked by library each time it is entered
  324. * from outside to do the job but before any threaded calls are going to be posted.
  325. *
  326. * @param impl Threading implementation ID
  327. * @param max_simultaneous_calls_estimate An estimated number of calls that can be posted simultaneously
  328. * @returns 1 or 0 to indicate success or failure
  329. *
  330. * @ingroup threading
  331. * @see dThreadedCallPostFunction
  332. */
  333. typedef int dThreadingImplResourcesForCallsPreallocateFunction(dThreadingImplementationID impl,
  334. ddependencycount_t max_simultaneous_calls_estimate);
  335. /**
  336. * @brief An interface structure with function pointers to be provided by threading implementation.
  337. */
  338. typedef struct dxThreadingFunctionsInfo
  339. {
  340. unsigned struct_size;
  341. dMutexGroupAllocFunction *alloc_mutex_group;
  342. dMutexGroupFreeFunction *free_mutex_group;
  343. dMutexGroupMutexLockFunction *lock_group_mutex;
  344. dMutexGroupMutexUnlockFunction *unlock_group_mutex;
  345. dThreadedCallWaitAllocFunction *alloc_call_wait;
  346. dThreadedCallWaitResetFunction *reset_call_wait;
  347. dThreadedCallWaitFreeFunction *free_call_wait;
  348. dThreadedCallPostFunction *post_call;
  349. dThreadedCallDependenciesCountAlterFunction *alter_call_dependencies_count;
  350. dThreadedCallWaitFunction *wait_call;
  351. dThreadingImplThreadCountRetrieveFunction *retrieve_thread_count;
  352. dThreadingImplResourcesForCallsPreallocateFunction *preallocate_resources_for_calls;
  353. /*
  354. * Beware of Jon Watte's anger if you dare to uncomment this!
  355. * May cryptic text below be you a warning!
  356. * Стародавні легенди розказують, що кожного сміливця, хто наважиться порушити табу
  357. * і відкрити заборонений код, спіткає страшне прокляття і він відразу почне робити
  358. * одні лиш помилки.
  359. *
  360. * dMutexGroupMutexTryLockFunction *trylock_group_mutex;
  361. */
  362. } dThreadingFunctionsInfo;
  363. #ifdef __cplusplus
  364. }
  365. #endif
  366. #endif /* #ifndef _ODE_THREADING_H_ */