full_frame.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* full_frame.c -*-C++-*-
  2. *
  3. *************************************************************************
  4. *
  5. * @copyright
  6. * Copyright (C) 2010-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. #include "full_frame.h"
  40. #include "stats.h"
  41. #include "os.h"
  42. #include "bug.h"
  43. #include "jmpbuf.h"
  44. #include "frame_malloc.h"
  45. COMMON_PORTABLE
  46. full_frame *__cilkrts_make_full_frame(__cilkrts_worker *w,
  47. __cilkrts_stack_frame *sf)
  48. {
  49. full_frame *ff;
  50. START_INTERVAL(w, INTERVAL_ALLOC_FULL_FRAME) {
  51. ff = (full_frame *)__cilkrts_frame_malloc(w, sizeof(*ff));
  52. __cilkrts_mutex_init(&ff->lock);
  53. ff->full_frame_magic_0 = FULL_FRAME_MAGIC_0;
  54. ff->join_counter = 0;
  55. ff->parent = 0;
  56. ff->rightmost_child = 0;
  57. ff->left_sibling = ff->right_sibling = 0;
  58. ff->call_stack = sf;
  59. ff->is_call_child = 0;
  60. ff->simulated_stolen = 0;
  61. ff->children_reducer_map = ff->right_reducer_map = 0;
  62. ff->pending_exception =
  63. ff->child_pending_exception =
  64. ff->right_pending_exception = NULL;
  65. ff->sync_sp = 0;
  66. #ifdef _WIN32
  67. ff->exception_sp = 0;
  68. ff->trylevel = (unsigned long)-1;
  69. ff->registration = 0;
  70. #endif
  71. ff->frame_size = 0;
  72. ff->fiber_self = 0;
  73. ff->fiber_child = 0;
  74. ff->sync_master = 0;
  75. /*__cilkrts_init_full_frame_sysdep(w, ff);*/
  76. ff->full_frame_magic_1 = FULL_FRAME_MAGIC_1;
  77. } STOP_INTERVAL(w, INTERVAL_ALLOC_FULL_FRAME);
  78. return ff;
  79. }
  80. COMMON_PORTABLE void __cilkrts_put_stack(full_frame *ff,
  81. __cilkrts_stack_frame *sf)
  82. {
  83. /* When suspending frame ff prior to stealing it, __cilkrts_put_stack is
  84. * used to store the stack pointer for eventual sync. When suspending
  85. * frame ff prior to a sync, __cilkrts_put_stack is called to re-establish
  86. * the sync stack pointer, offsetting it by any change in the stack depth
  87. * that occured between the spawn and the sync.
  88. * Although it is not usually meaningful to add two pointers, the value of
  89. * ff->sync_sp at the time of this call is really an integer, not a
  90. * pointer.
  91. */
  92. ptrdiff_t sync_sp_i = (ptrdiff_t) ff->sync_sp;
  93. char* sp = (char*) __cilkrts_get_sp(sf);
  94. ff->sync_sp = sp + sync_sp_i;
  95. DBGPRINTF("%d- __cilkrts_put_stack - adjust (+) sync "
  96. "stack of full frame %p (+sp: %p) to %p\n",
  97. __cilkrts_get_tls_worker()->self, ff, sp, ff->sync_sp);
  98. }
  99. COMMON_PORTABLE void __cilkrts_take_stack(full_frame *ff, void *sp)
  100. {
  101. /* When resuming the parent after a steal, __cilkrts_take_stack is used to
  102. * subtract the new stack pointer from the current stack pointer, storing
  103. * the offset in ff->sync_sp. When resuming after a sync,
  104. * __cilkrts_take_stack is used to subtract the new stack pointer from
  105. * itself, leaving ff->sync_sp at zero (null). Although the pointers being
  106. * subtracted are not part of the same contiguous chunk of memory, the
  107. * flat memory model allows us to subtract them and get a useable offset.
  108. */
  109. ptrdiff_t sync_sp_i = ff->sync_sp - (char*) sp;
  110. ff->sync_sp = (char *) sync_sp_i;
  111. DBGPRINTF("%d- __cilkrts_take_stack - adjust (-) sync "
  112. "stack of full frame %p to %p (-sp: %p)\n",
  113. __cilkrts_get_tls_worker()->self, ff, ff->sync_sp, sp);
  114. }
  115. COMMON_PORTABLE void __cilkrts_adjust_stack(full_frame *ff, size_t size)
  116. {
  117. /* When resuming the parent after a steal, __cilkrts_take_stack is used to
  118. * subtract the new stack pointer from the current stack pointer, storing
  119. * the offset in ff->sync_sp. When resuming after a sync,
  120. * __cilkrts_take_stack is used to subtract the new stack pointer from
  121. * itself, leaving ff->sync_sp at zero (null). Although the pointers being
  122. * subtracted are not part of the same contiguous chunk of memory, the
  123. * flat memory model allows us to subtract them and get a useable offset.
  124. *
  125. * __cilkrts_adjust_stack() is used to deallocate a Variable Length Array
  126. * by adding it's size to ff->sync_sp.
  127. */
  128. ff->sync_sp = ff->sync_sp + size;
  129. DBGPRINTF("%d- __cilkrts_adjust_stack - adjust (+) sync "
  130. "stack of full frame %p to %p (+ size: 0x%x)\n",
  131. __cilkrts_get_tls_worker()->self, ff, ff->sync_sp, size);
  132. }
  133. COMMON_PORTABLE
  134. void __cilkrts_destroy_full_frame(__cilkrts_worker *w, full_frame *ff)
  135. {
  136. validate_full_frame(ff);
  137. CILK_ASSERT(ff->children_reducer_map == 0);
  138. CILK_ASSERT(ff->right_reducer_map == 0);
  139. CILK_ASSERT(NULL == ff->pending_exception);
  140. CILK_ASSERT(NULL == ff->child_pending_exception);
  141. CILK_ASSERT(NULL == ff->right_pending_exception);
  142. __cilkrts_mutex_destroy(w, &ff->lock);
  143. __cilkrts_frame_free(w, ff, sizeof(*ff));
  144. }
  145. COMMON_PORTABLE void validate_full_frame(full_frame *ff)
  146. {
  147. /* check the magic numbers, for debugging purposes */
  148. if (ff->full_frame_magic_0 != FULL_FRAME_MAGIC_0 ||
  149. ff->full_frame_magic_1 != FULL_FRAME_MAGIC_1)
  150. abort_because_rts_is_corrupted();
  151. }
  152. void __cilkrts_frame_lock(__cilkrts_worker *w, full_frame *ff)
  153. {
  154. validate_full_frame(ff);
  155. __cilkrts_mutex_lock(w, &ff->lock);
  156. }
  157. void __cilkrts_frame_unlock(__cilkrts_worker *w, full_frame *ff)
  158. {
  159. __cilkrts_mutex_unlock(w, &ff->lock);
  160. }
  161. /* End full_frame.c */