3 *************************************************************************
5 * Copyright (C) 2009-2016, Intel Corporation
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * * Neither the name of Intel Corporation nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
32 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
35 * *********************************************************************
37 * PLEASE NOTE: This file is a downstream copy of a file mainitained in
38 * a repository at cilkplus.org. Changes made to this file that are not
39 * submitted through the contribution process detailed at
40 * http://www.cilkplus.org/submit-cilk-contribution will be lost the next
41 * time that a new version is released. Changes only submitted to the
42 * GNU compiler collection or posted to the git repository at
43 * https://bitbucket.org/intelcilkruntime/intel-cilk-runtime.git are
46 * We welcome your contributions to this open source project. Thank you
47 * for your assistance in helping us improve Cilk Plus.
48 **************************************************************************/
53 * @brief Low-level operating-system dependent facilities, not dependent on
54 * any Cilk facilities.
57 #ifndef INCLUDED_OS_DOT_H
58 #define INCLUDED_OS_DOT_H
60 #include "rts-common.h"
61 #include "cilk/common.h"
62 #include "cilk-tbb-interop.h"
63 #include "cilk_str_mem.h"
71 __CILKRTS_BEGIN_EXTERN_C
74 // /* Thread-local storage */
76 // typedef unsigned cilkos_tls_key_t;
78 // typedef pthread_key_t cilkos_tls_key_t;
80 // cilkos_tls_key_t cilkos_allocate_tls_key();
81 // void cilkos_set_tls_pointer(cilkos_tls_key_t key, void* ptr);
82 // void* cilkos_get_tls_pointer(cilkos_tls_key_t key);
84 /* The RTS assumes that some thread-local state exists that stores the
85 worker and reducer map currently associated with a thread. These routines
86 manipulate this state. */
88 /** @brief Thread-local state for cilk fibers. */
89 typedef struct cilk_fiber_sysdep cilk_fiber_sysdep
;
91 /** @brief Initialize all TLS variables for Cilk. */
92 COMMON_SYSDEP
void __cilkrts_init_tls_variables(void);
94 /** @brief Set worker struct in TLS. */
96 void __cilkrts_set_tls_worker(__cilkrts_worker
*w
) cilk_nothrow
;
98 /** @brief Get stack_op for TBB-interop structures from TLS. */
100 __cilk_tbb_stack_op_thunk
*__cilkrts_get_tls_tbb_interop(void);
102 /** @brief Set stack_op for TBB-interop structures in TLS. */
104 void __cilkrts_set_tls_tbb_interop(__cilk_tbb_stack_op_thunk
*t
);
107 * @brief Get the pointer to the pedigree leaf node from TLS.
109 * Function to get a pointer to the thread's pedigree leaf node. This
110 * pointer can be NULL.
113 __cilkrts_pedigree
* __cilkrts_get_tls_pedigree_leaf(int create_new
);
116 * @brief Sets the pointer to the pedigree leaf node in TLS.
118 * If the previous pointer value was not NULL, it is the caller's
119 * responsibility to ensure that previous pointer value is saved and
122 * @param pedigree_leaf The leaf node to store into TLS.
125 void __cilkrts_set_tls_pedigree_leaf(__cilkrts_pedigree
* pedigree_leaf
);
128 #if SUPPORT_GET_CURRENT_FIBER > 0
130 * @brief Get the cilk_fiber from TLS.
133 cilk_fiber_sysdep
* cilkos_get_tls_cilk_fiber(void);
136 * @brief Set the cilk_fiber in TLS.
138 * @param fiber The fiber to store into TLS.
141 void cilkos_set_tls_cilk_fiber(cilk_fiber_sysdep
* fiber
);
145 * @brief Function for returning the current thread id.
146 * @warning This function is useful for debugging purposes only.
149 void* cilkos_get_current_thread_id(void);
151 /** @brief Return number of CPUs supported by this hardware, using whatever definition
152 of CPU is considered appropriate. */
153 COMMON_SYSDEP
int __cilkrts_hardware_cpu_count(void);
155 /** @brief Get current value of timer */
156 COMMON_SYSDEP
unsigned long long __cilkrts_getticks(void);
158 /* Machine instructions */
160 /// Stall execution for a few cycles.
161 COMMON_SYSDEP
void __cilkrts_short_pause(void);
162 /// Wrapper for xchg instruction
163 COMMON_SYSDEP
int __cilkrts_xchg(volatile int *ptr
, int x
);
165 // Defines __cilkrts_fence - A macro for x86, a function call for other
167 #include "os-fence.h"
169 COMMON_SYSDEP
void __cilkrts_sleep(void); ///< Sleep briefly
170 COMMON_SYSDEP
void __cilkrts_yield(void); ///< Yield quantum
171 COMMON_SYSDEP
void __cilkrts_idle(void); ///< Idle
174 * @brief Gets environment variable 'varname' and copy its value into 'value'.
176 * If the entire value, including the null terminator fits into 'vallen'
177 * bytes, then returns the length of the value excluding the null. Otherwise,
178 * leaves the contents of 'value' undefined and returns the number of
179 * characters needed to store the environment variable's value, *including*
180 * the null terminator.
182 * @param value Buffer to store value.
183 * @param vallen Length of value buffer
184 * @param varname Name of the environment variable.
185 * @return Length of value buffer (excluding the null).
187 COMMON_SYSDEP __STDNS
size_t cilkos_getenv(char* value
, __STDNS
size_t vallen
,
188 const char* varname
);
191 * @brief Unrecoverable error: Print an error message and abort execution.
193 COMMON_SYSDEP
void cilkos_error(const char *fmt
, ...);
196 * @brief Print a warning message and return.
198 COMMON_SYSDEP
void cilkos_warning(const char *fmt
, ...);
201 * @brief Convert the user's specified stack size into a "reasonable"
202 * value for the current OS.
204 * @param specified_stack_size User-specified stack size.
205 * @return New stack size value, modified for the OS.
207 COMMON_SYSDEP
size_t cilkos_validate_stack_size(size_t specified_stack_size
);
210 * @brief Atomic addition: computes *p += x.
212 * @param p Pointer to value to update
213 * @param x Value of x.
215 COMMON_SYSDEP
long cilkos_atomic_add(volatile long* p
, long x
);
220 * @brief Windows-only low-level functions for processor groups.
222 typedef struct _GROUP_AFFINITY GROUP_AFFINITY
;
225 * @brief Probe the executing OS to see if it supports processor
226 * groups. These functions are expected to be available in Windows 7
229 void win_init_processor_groups(void);
231 unsigned long win_get_active_processor_count(unsigned short GroupNumber
);
232 unsigned short win_get_active_processor_group_count(void);
233 int win_set_thread_group_affinity(/*HANDLE*/ void* hThread
,
234 const GROUP_AFFINITY
*GroupAffinity
,
235 GROUP_AFFINITY
* PreviousGroupAffinity
);
238 * @brief Cleans up any state allocated in TLS.
240 * Only defined for Windows because Linux calls destructors for each
241 * thread-local variable.
243 void __cilkrts_per_thread_tls_cleanup(void);
247 __CILKRTS_END_EXTERN_C
249 #endif // ! defined(INCLUDED_OS_DOT_H)