cooperative.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*************************************************************************
  2. * *
  3. * Open Dynamics Engine, Copyright (C) 2001-2003 Russell L. Smith. *
  4. * All rights reserved. Email: [email protected] Web: www.q12.org *
  5. * *
  6. * This library is free software; you can redistribute it and/or *
  7. * modify it under the terms of EITHER: *
  8. * (1) The GNU Lesser General Public License as published by the Free *
  9. * Software Foundation; either version 2.1 of the License, or (at *
  10. * your option) any later version. The text of the GNU Lesser *
  11. * General Public License is included with this library in the *
  12. * file LICENSE.TXT. *
  13. * (2) The BSD-style license that is included with this library in *
  14. * the file LICENSE-BSD.TXT. *
  15. * *
  16. * This library is distributed in the hope that it will be useful, *
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files *
  19. * LICENSE.TXT and LICENSE-BSD.TXT for more details. *
  20. * *
  21. *************************************************************************/
  22. #ifndef _ODE_COOPERATIVE_H_
  23. #define _ODE_COOPERATIVE_H_
  24. #include <ode/common.h>
  25. #include <ode/threading.h>
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. /**
  30. * @defgroup coop Cooperative Algorithms
  31. *
  32. * Algorithms implemented as multiple threads doing work cooperatively.
  33. */
  34. struct dxCooperative;
  35. struct dxResourceRequirements;
  36. struct dxResourceContainer;
  37. /**
  38. * @brief A container for cooperative algorithms shared context
  39. *
  40. * The Cooperative is a container for cooperative algorithms shared context.
  41. * At present it contains threading object (either a real one or a defaulted
  42. * self-threading).
  43. *
  44. * Cooperative use in functions performing computations must be serialized. That is,
  45. * functions referring to a single instance of Cooperative object must not be called in
  46. * parallel.
  47. */
  48. typedef struct dxCooperative *dCooperativeID;
  49. /**
  50. * @brief A container for resource requirements information
  51. *
  52. * The ResourceRequirements object is a container for descriptive information
  53. * regarding what resources (memory, synchronization objects, etc.) need to be
  54. * allocated for particular computations. The object can be used for accumulating
  55. * resource requirement maxima over multiple functions and then allocating resources
  56. * that would suffice for any of those function calls.
  57. *
  58. * ResourceRequirements objects maintain relations to Cooperative objects since
  59. * amounts of resources that could be required can depend on characteristics of
  60. * shared context, e.g. on maximal number of threads in the threading object.
  61. *
  62. * @ingroup coop
  63. * @see dCooperativeID
  64. * @see dResourceContainerID
  65. */
  66. typedef struct dxResourceRequirements *dResourceRequirementsID;
  67. /**
  68. * @brief A container for algorithm allocated resources
  69. *
  70. * The ResourceContainer object can contain resources allocated according to information
  71. * in a ResourceRequirements. The resources inherit link to the threading object
  72. * from the requirements they are allocated according to.
  73. *
  74. * @ingroup coop
  75. * @see dResourceRequirementsID
  76. * @see dCooperativeID
  77. */
  78. typedef struct dxResourceContainer *dResourceContainerID;
  79. /**
  80. * @brief Creates a Cooperative object related to the specified threading.
  81. *
  82. * NULL's are allowed for the threading. In this case the default (global) self-threading
  83. * object will be used.
  84. *
  85. * Use @c dCooperativeDestroy to destroy the object. The Cooperative object must exist
  86. * until after all the objects referencing it are destroyed.
  87. *
  88. * @param functionInfo The threading functions to use
  89. * @param threadingImpl The threading implementation object to use
  90. * @returns The Cooperative object instance or NULL if allocation fails.
  91. * @ingroup coop
  92. * @see dCooperativeDestroy
  93. */
  94. ODE_API dCooperativeID dCooperativeCreate(const dThreadingFunctionsInfo *functionInfo/*=NULL*/, dThreadingImplementationID threadingImpl/*=NULL*/);
  95. /**
  96. * @brief Destroys Cooperative object.
  97. *
  98. * The Cooperative object can only be destroyed after all the objects referencing it.
  99. *
  100. * @param cooperative A Cooperative object to be deleted (NULL is allowed)
  101. * @ingroup coop
  102. * @see dCooperativeCreate
  103. */
  104. ODE_API void dCooperativeDestroy(dCooperativeID cooperative);
  105. /**
  106. * @brief Creates a ResourceRequirements object related to a Cooperative.
  107. *
  108. * The object is purely descriptive and does not contain any resources by itself.
  109. * The actual resources are allocated by means of ResourceContainer object.
  110. *
  111. * The object is created with empty requirements. It can be then used to accumulate
  112. * requirements for one or more function calls and can be cloned or merged with others.
  113. * The actual requirements information is added to the object by computation related
  114. * functions.
  115. *
  116. * Use @c dResourceRequirementsDestroy to delete the object when it is no longer needed.
  117. *
  118. * @param cooperative A Cooperative object to be used
  119. * @returns The ResourceRequirements object instance or NULL if allocation fails
  120. * @ingroup coop
  121. * @see dResourceRequirementsDestroy
  122. * @see dResourceRequirementsClone
  123. * @see dResourceRequirementsMergeIn
  124. * @see dCooperativeCreate
  125. * @see dResourceContainerAcquire
  126. */
  127. ODE_API dResourceRequirementsID dResourceRequirementsCreate(dCooperativeID cooperative);
  128. /**
  129. * @brief Destroys ResourceRequirements object.
  130. *
  131. * The ResourceRequirements object can be destroyed at any time with no regards
  132. * to other objects' lifetime.
  133. *
  134. * @param requirements A ResourceRequirements object to be deleted (NULL is allowed)
  135. * @ingroup coop
  136. * @see dResourceRequirementsCreate
  137. */
  138. ODE_API void dResourceRequirementsDestroy(dResourceRequirementsID requirements);
  139. /**
  140. * @brief Clones ResourceRequirements object.
  141. *
  142. * The function creates a copy of the ResourceRequirements object with all the
  143. * contents and the relation to Cooperative matching. The object passed in
  144. * the parameter is not changed.
  145. *
  146. * The object created with the function must later be destroyed with @c dResourceRequirementsDestroy.
  147. *
  148. * @param requirements A ResourceRequirements object to be cloned
  149. * @returns A handle to the new object or NULL if allocation fails
  150. * @ingroup coop
  151. * @see dResourceRequirementsCreate
  152. * @see dResourceRequirementsDestroy
  153. * @see dResourceRequirementsMergeIn
  154. */
  155. ODE_API dResourceRequirementsID dResourceRequirementsClone(/*const */dResourceRequirementsID requirements);
  156. /**
  157. * @brief Merges one ResourceRequirements object into another ResourceRequirements object.
  158. *
  159. * The function updates @a summaryRequirements requirements to be also sufficient
  160. * for the purposes @a extraRequirements could be used for. The @a extraRequirements
  161. * object is not changed. The both objects should normally have had been created
  162. * with the same Cooperative object.
  163. *
  164. * @param summaryRequirements A ResourceRequirements object to be changed
  165. * @param extraRequirements A ResourceRequirements the requirements to be taken from
  166. * @ingroup coop
  167. * @see dResourceRequirementsCreate
  168. * @see dResourceRequirementsDestroy
  169. * @see dResourceRequirementsClone
  170. */
  171. ODE_API void dResourceRequirementsMergeIn(dResourceRequirementsID summaryRequirements, /*const */dResourceRequirementsID extraRequirements);
  172. /**
  173. * @brief Allocates resources as specified in ResourceRequirements object.
  174. *
  175. * The ResourceContainer object can be used in cooperative computation algorithms.
  176. *
  177. * The same @a requirements object can be passed to many resource allocations
  178. * (with or without modifications) and can be deleted immediately, without waiting
  179. * for the ResourceContainer object destruction.
  180. *
  181. * Use @c dResourceContainerDestroy to delete the object and release the resources
  182. * when they are no longer needed.
  183. *
  184. * @param requirements The ResourceRequirements object to allocate resources according to
  185. * @returns A ResourceContainer object instance with the resources allocated or NULL if allocation fails
  186. * @ingroup coop
  187. * @see dResourceContainerDestroy
  188. * @see dResourceRequirementsCreate
  189. */
  190. ODE_API dResourceContainerID dResourceContainerAcquire(/*const */dResourceRequirementsID requirements);
  191. /**
  192. * @brief Destroys ResourceContainer object and releases resources allocated in it.
  193. *
  194. * @param resources A ResourceContainer object to be deleted (NULL is allowed)
  195. * @ingroup coop
  196. * @see dResourceContainerAcquire
  197. */
  198. ODE_API void dResourceContainerDestroy(dResourceContainerID resources);
  199. #ifdef __cplusplus
  200. } // extern "C"
  201. #endif
  202. #endif // #ifndef _ODE_COOPERATIVE_H_