os.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* os.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 os.h
  40. *
  41. * @brief Low-level operating-system dependent facilities, not dependent on
  42. * any Cilk facilities.
  43. */
  44. #ifndef INCLUDED_OS_DOT_H
  45. #define INCLUDED_OS_DOT_H
  46. #include "rts-common.h"
  47. #include "cilk/common.h"
  48. #include "cilk-tbb-interop.h"
  49. #ifdef __cplusplus
  50. # include <cstddef>
  51. #else
  52. # include <stddef.h>
  53. #endif
  54. __CILKRTS_BEGIN_EXTERN_C
  55. // /* Thread-local storage */
  56. // #ifdef _WIN32
  57. // typedef unsigned cilkos_tls_key_t;
  58. // #else
  59. // typedef pthread_key_t cilkos_tls_key_t;
  60. // #endif
  61. // cilkos_tls_key_t cilkos_allocate_tls_key();
  62. // void cilkos_set_tls_pointer(cilkos_tls_key_t key, void* ptr);
  63. // void* cilkos_get_tls_pointer(cilkos_tls_key_t key);
  64. /* The RTS assumes that some thread-local state exists that stores the
  65. worker and reducer map currently associated with a thread. These routines
  66. manipulate this state. */
  67. /** @brief Thread-local state for cilk fibers. */
  68. typedef struct cilk_fiber_sysdep cilk_fiber_sysdep;
  69. /** @brief Initialize all TLS variables for Cilk. */
  70. COMMON_SYSDEP void __cilkrts_init_tls_variables(void);
  71. /** @brief Set worker struct in TLS. */
  72. COMMON_SYSDEP
  73. void __cilkrts_set_tls_worker(__cilkrts_worker *w) cilk_nothrow;
  74. /** @brief Get stack_op for TBB-interop structures from TLS. */
  75. COMMON_SYSDEP
  76. __cilk_tbb_stack_op_thunk *__cilkrts_get_tls_tbb_interop(void);
  77. /** @brief Set stack_op for TBB-interop structures in TLS. */
  78. COMMON_SYSDEP
  79. void __cilkrts_set_tls_tbb_interop(__cilk_tbb_stack_op_thunk *t);
  80. /**
  81. * @brief Get the pointer to the pedigree leaf node from TLS.
  82. *
  83. * Function to get a pointer to the thread's pedigree leaf node. This
  84. * pointer can be NULL.
  85. */
  86. COMMON_SYSDEP
  87. __cilkrts_pedigree * __cilkrts_get_tls_pedigree_leaf(int create_new);
  88. /**
  89. * @brief Sets the pointer to the pedigree leaf node in TLS.
  90. *
  91. * If the previous pointer value was not NULL, it is the caller's
  92. * responsibility to ensure that previous pointer value is saved and
  93. * freed.
  94. *
  95. * @param pedigree_leaf The leaf node to store into TLS.
  96. */
  97. COMMON_SYSDEP
  98. void __cilkrts_set_tls_pedigree_leaf(__cilkrts_pedigree* pedigree_leaf);
  99. #if SUPPORT_GET_CURRENT_FIBER > 0
  100. /**
  101. * @brief Get the cilk_fiber from TLS.
  102. */
  103. COMMON_SYSDEP
  104. cilk_fiber_sysdep* cilkos_get_tls_cilk_fiber(void);
  105. /**
  106. * @brief Set the cilk_fiber in TLS.
  107. *
  108. * @param fiber The fiber to store into TLS.
  109. */
  110. COMMON_SYSDEP
  111. void cilkos_set_tls_cilk_fiber(cilk_fiber_sysdep* fiber);
  112. #endif
  113. /**
  114. * @brief Function for returning the current thread id.
  115. * @warning This function is useful for debugging purposes only.
  116. */
  117. COMMON_SYSDEP
  118. void* cilkos_get_current_thread_id(void);
  119. /** @brief Return number of CPUs supported by this hardware, using whatever definition
  120. of CPU is considered appropriate. */
  121. COMMON_SYSDEP int __cilkrts_hardware_cpu_count(void);
  122. /** @brief Get current value of timer */
  123. COMMON_SYSDEP unsigned long long __cilkrts_getticks(void);
  124. /* Machine instructions */
  125. /// Stall execution for a few cycles.
  126. COMMON_SYSDEP void __cilkrts_short_pause(void);
  127. /// Wrapper for xchg instruction
  128. COMMON_SYSDEP int __cilkrts_xchg(volatile int *ptr, int x);
  129. // Defines __cilkrts_fence - A macro for x86, a function call for other
  130. // architectures
  131. #include "os-fence.h"
  132. COMMON_SYSDEP void __cilkrts_sleep(void); ///< Sleep briefly
  133. COMMON_SYSDEP void __cilkrts_yield(void); ///< Yield quantum
  134. /**
  135. * @brief Gets environment variable 'varname' and copy its value into 'value'.
  136. *
  137. * If the entire value, including the null terminator fits into 'vallen'
  138. * bytes, then returns the length of the value excluding the null. Otherwise,
  139. * leaves the contents of 'value' undefined and returns the number of
  140. * characters needed to store the environment variable's value, *including*
  141. * the null terminator.
  142. *
  143. * @param value Buffer to store value.
  144. * @param vallen Length of value buffer
  145. * @param varname Name of the environment variable.
  146. * @return Length of value buffer (excluding the null).
  147. */
  148. COMMON_SYSDEP __STDNS size_t cilkos_getenv(char* value, __STDNS size_t vallen,
  149. const char* varname);
  150. /**
  151. * @brief Unrecoverable error: Print an error message and abort execution.
  152. */
  153. COMMON_SYSDEP void cilkos_error(const char *fmt, ...);
  154. /**
  155. * @brief Print a warning message and return.
  156. */
  157. COMMON_SYSDEP void cilkos_warning(const char *fmt, ...);
  158. /**
  159. * @brief Convert the user's specified stack size into a "reasonable"
  160. * value for the current OS.
  161. *
  162. * @param specified_stack_size User-specified stack size.
  163. * @return New stack size value, modified for the OS.
  164. */
  165. COMMON_SYSDEP size_t cilkos_validate_stack_size(size_t specified_stack_size);
  166. /**
  167. * @brief Atomic addition: computes *p += x.
  168. *
  169. * @param p Pointer to value to update
  170. * @param x Value of x.
  171. */
  172. COMMON_SYSDEP long cilkos_atomic_add(volatile long* p, long x);
  173. #ifdef _WIN32
  174. /**
  175. * @brief Windows-only low-level functions for processor groups.
  176. */
  177. typedef struct _GROUP_AFFINITY GROUP_AFFINITY;
  178. /**
  179. * @brief Probe the executing OS to see if it supports processor
  180. * groups. These functions are expected to be available in Windows 7
  181. * or later.
  182. */
  183. void win_init_processor_groups(void);
  184. unsigned long win_get_active_processor_count(unsigned short GroupNumber);
  185. unsigned short win_get_active_processor_group_count(void);
  186. int win_set_thread_group_affinity(/*HANDLE*/ void* hThread,
  187. const GROUP_AFFINITY *GroupAffinity,
  188. GROUP_AFFINITY* PreviousGroupAffinity);
  189. /**
  190. * @brief Cleans up any state allocated in TLS.
  191. *
  192. * Only defined for Windows because Linux calls destructors for each
  193. * thread-local variable.
  194. */
  195. void __cilkrts_per_thread_tls_cleanup(void);
  196. #endif // _WIN32
  197. __CILKRTS_END_EXTERN_C
  198. #endif // ! defined(INCLUDED_OS_DOT_H)