cilk-tbb-interop.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /* cilk-tbb-interop.h -*-C++-*-
  2. *
  3. *************************************************************************
  4. *
  5. * @copyright
  6. * Copyright (C) 2009-2013, Intel Corporation
  7. * All rights reserved.
  8. *
  9. * @copyright
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. * * Neither the name of Intel Corporation nor the names of its
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * @copyright
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  30. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  31. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  32. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  33. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
  35. * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. **************************************************************************/
  38. /**
  39. * @file cilk-tbb-interop.h
  40. *
  41. * @brief Interface between TBB and Cilk to allow TBB to associate it's
  42. * per-thread data with Cilk workers, and maintain the association as work
  43. * moves between worker threads. This handles the case where TBB calls
  44. * into a Cilk function which may later call back to a function making
  45. * TBB calls.
  46. *
  47. * Each thunk structure has two pointers: \"routine\" and \"data\".
  48. * The caller of the thunk invokes *routine, passing \"data\" as the void*
  49. * parameter.
  50. */
  51. #ifndef INCLUDED_CILK_TBB_INTEROP_DOT_H
  52. #define INCLUDED_CILK_TBB_INTEROP_DOT_H
  53. #include <cilk/common.h> // for CILK_EXPORT
  54. __CILKRTS_BEGIN_EXTERN_C
  55. /** A return code. 0 indicates success. */
  56. typedef int __cilk_tbb_retcode;
  57. /**
  58. * Enumeration of reasons that Cilk will call the TBB stack operation
  59. * function.
  60. *
  61. * When a non-empty stack is transfered between threads, the first thread must
  62. * orphan it and the second thread must adopt it.
  63. *
  64. * An empty stack can be transfered similarly, or simply released by the first
  65. * thread.
  66. *
  67. * Here is a summary of the actions as transitions on a state machine.
  68. @verbatim
  69. watch ORPHAN
  70. -->--> -->--
  71. / \ / \
  72. (freed empty stack) (TBB sees stack running on thread) (stack in limbo)
  73. \ / \ /
  74. --<-- --<--
  75. RELEASE or ADOPT
  76. unwatch
  77. @endverbatim
  78. */
  79. typedef enum __cilk_tbb_stack_op {
  80. /**
  81. * Disconnecting stack from a thread.
  82. *
  83. * The thunk must be invoked on the thread disconnecting itself from the
  84. * stack. Must \"happen before\" the stack is adopted elsewhere.
  85. */
  86. CILK_TBB_STACK_ORPHAN,
  87. /**
  88. * Reconnecting orphaned stack to a thread.
  89. *
  90. * The thunk must be invoked on the thread adopting the stack.
  91. */
  92. CILK_TBB_STACK_ADOPT,
  93. /**
  94. * Releasing stack.
  95. *
  96. * The thunk must be invoked on the thread doing the releasing, Must
  97. * \"happen before\" the stack is used elsewhere.
  98. */
  99. CILK_TBB_STACK_RELEASE
  100. } __cilk_tbb_stack_op;
  101. /**
  102. * Function that will be called by the Cilk runtime to inform TBB of a change
  103. * in the stack associated with the current thread.
  104. *
  105. * It does not matter what stack the thunk runs on.
  106. * The thread (not fiber) on which the thunk runs is important.
  107. *
  108. * @param op Enumerated value indicating what type of change is ocurring.
  109. * @param data Context value provided by TBB in the __cilkrts_watch_stack
  110. * call. This data is opaque to Cilk.
  111. *
  112. * @return 0 indicates success.
  113. */
  114. typedef __cilk_tbb_retcode (*__cilk_tbb_pfn_stack_op)(enum __cilk_tbb_stack_op op,
  115. void* data);
  116. /**
  117. * Function that will be called by TBB to inform the Cilk runtime that TBB
  118. * is no longer interested in watching the stack bound to the current thread.
  119. *
  120. * @param data Context value provided to TBB by the __cilkrts_watch_stack
  121. * call. This data is opaque to TBB.
  122. *
  123. * @return 0 indicates success.
  124. */
  125. typedef __cilk_tbb_retcode (*__cilk_tbb_pfn_unwatch_stacks)(void *data);
  126. /**
  127. * Thunk invoked by Cilk to call back to TBB to tell it about a change in
  128. * the stack bound to the current thread.
  129. */
  130. typedef struct __cilk_tbb_stack_op_thunk {
  131. /// Function in TBB the Cilk runtime should call when something
  132. // "interesting" happens involving a stack
  133. __cilk_tbb_pfn_stack_op routine;
  134. /// TBB context data to pass with the call to the stack_op routine
  135. void* data;
  136. } __cilk_tbb_stack_op_thunk;
  137. /**
  138. * Thunk invoked by TBB when it is no longer interested in watching the stack
  139. * bound to the current thread.
  140. */
  141. typedef struct __cilk_tbb_unwatch_thunk {
  142. /// Function in Cilk runtime to call when TBB no longer wants to watch
  143. // stacks
  144. __cilk_tbb_pfn_unwatch_stacks routine;
  145. /// Cilk runtime context data to pass with the call to the unwatch_stacks
  146. /// routine
  147. void* data;
  148. } __cilk_tbb_unwatch_thunk;
  149. /**
  150. * Requests that Cilk invoke __cilk_tbb_orphan_thunk when it orphans a stack.
  151. * Cilk sets *u to a thunk that TBB should call when it is no longer
  152. * interested in watching the stack.
  153. *
  154. * If the thread is not yet bound to the Cilk runtime, the Cilk runtime should
  155. * save this data in thread-local storage until __cilkrts_bind_thread is called.
  156. *
  157. * Called by TBB, defined by Cilk. This function is exported from the Cilk
  158. * runtime DLL/shared object. This declaration also appears in
  159. * cilk/cilk_undocumented.h -- don't change one declaration without also
  160. * changing the other.
  161. *
  162. * @param u __cilk_tbb_unwatch_thunk. This structure will be filled in by
  163. * the Cilk runtime to allow TBB to register that it is no longer interested
  164. * in watching the stack bound to the current thread.
  165. * @param o __cilk_tbb_stack_op_thunk. This structure specifies the routine
  166. * that the Cilk runtime should call when an "interesting" change in the stack
  167. * associate with the current worker occurs.
  168. *
  169. * @return 0 indicates success.
  170. */
  171. CILK_EXPORT
  172. __cilk_tbb_retcode __cilkrts_watch_stack(__cilk_tbb_unwatch_thunk* u,
  173. __cilk_tbb_stack_op_thunk o);
  174. __CILKRTS_END_EXTERN_C
  175. #endif // ! defined(INCLUDED_CILK_TBB_INTEROP_DOT_H)