cilk_fiber-unix.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* cilk_fiber-unix.h -*-C++-*-
  2. *
  3. *************************************************************************
  4. *
  5. * @copyright
  6. * Copyright (C) 2012-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. #ifndef INCLUDED_CILK_FIBER_UNIX_DOT_H
  39. #define INCLUDED_CILK_FIBER_UNIX_DOT_H
  40. #ifndef __cplusplus
  41. # error cilk_fiber-unix.h is a C++-only header
  42. #endif
  43. #include "cilk_fiber.h"
  44. #include "jmpbuf.h"
  45. /**
  46. * @file cilk_fiber-unix.h
  47. *
  48. * @brief Unix-specific implementation for cilk_fiber.
  49. */
  50. /**
  51. * @brief Unix-specific fiber class derived from portable fiber class
  52. */
  53. struct cilk_fiber_sysdep : public cilk_fiber
  54. {
  55. public:
  56. #if SUPPORT_GET_CURRENT_FIBER
  57. /**
  58. * @brief Gets the current fiber from TLS.
  59. */
  60. static cilk_fiber_sysdep* get_current_fiber_sysdep();
  61. #endif
  62. /**
  63. * @brief Construct the system-dependent portion of a fiber.
  64. *
  65. * @param stack_size The size of the stack for this fiber.
  66. */
  67. cilk_fiber_sysdep(std::size_t stack_size);
  68. /**
  69. * @brief Construct the system-dependent of a fiber created from a
  70. * thread.
  71. */
  72. cilk_fiber_sysdep(from_thread_t);
  73. /**
  74. * @brief Destructor
  75. */
  76. ~cilk_fiber_sysdep();
  77. /**
  78. * @brief OS-specific calls to convert this fiber back to thread.
  79. *
  80. * Nothing to do for Linux.
  81. */
  82. void convert_fiber_back_to_thread();
  83. /**
  84. * @brief System-dependent function to suspend self and resume execution of "other".
  85. *
  86. * This fiber is suspended.
  87. *
  88. * @pre @c is_resumable() should be true.
  89. *
  90. * @param other Fiber to resume.
  91. */
  92. void suspend_self_and_resume_other_sysdep(cilk_fiber_sysdep* other);
  93. /**
  94. * @brief System-dependent function called to jump to @p other
  95. * fiber.
  96. *
  97. * @pre @c is_resumable() should be false.
  98. *
  99. * @param other Fiber to resume.
  100. */
  101. NORETURN jump_to_resume_other_sysdep(cilk_fiber_sysdep* other);
  102. /**
  103. * @brief Runs the start_proc.
  104. * @pre is_resumable() should be false.
  105. * @pre is_allocated_from_thread() should be false.
  106. * @pre m_start_proc must be valid.
  107. */
  108. NORETURN run();
  109. /**
  110. * @brief Returns the base of this fiber's stack.
  111. */
  112. inline char* get_stack_base_sysdep() { return m_stack_base; }
  113. private:
  114. char* m_stack_base; ///< The base of this fiber's stack.
  115. char* m_stack; // Stack memory (low address)
  116. __CILK_JUMP_BUFFER m_resume_jmpbuf; // Place to resume fiber
  117. unsigned m_magic; // Magic number for checking
  118. static int s_page_size; // Page size for
  119. // stacks.
  120. // Allocate memory for a stack. This method
  121. // initializes m_stack and m_stack_base.
  122. void make_stack(size_t stack_size);
  123. // Deallocates memory for the stack.
  124. void free_stack();
  125. // Common helper method for implementation of resume_other_sysdep
  126. // variants.
  127. inline void resume_other_sysdep(cilk_fiber_sysdep* other);
  128. };
  129. #endif // ! defined(INCLUDED_CILK_FIBER_UNIX_DOT_H)