* config/msp430/msp430-modes.def (PSI): Add.
[official-gcc.git] / libcilkrts / runtime / cilk_api.c
blobbbca984bc03fd2d60084a280f584407ff01b0a25
1 /* cilk_api.c -*-C-*-
3 *************************************************************************
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:
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.
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 **************************************************************************/
40 * Implementation of functions declared in cilk_api.h
44 * Define the COMPILING_CILK_ABI_FUNCTIONS macro, so that
45 * compilation of this file generates non-inlined definitions for the
46 * functions marked as CILK_EXPORT_AND_INLINE in cilk_api.h.
48 * We must deal with these functions differently because we need to
49 * continue to ship nonlined versions of these functions.
51 * CILK_EXPORT_AND_INLINE int __cilkrts_get_worker_rank(uint64_t *rank);
52 * CILK_EXPORT_AND_INLINE int __cilkrts_bump_worker_rank();
53 * CILK_EXPORT_AND_INLINE int __cilkrts_bump_loop_rank();
55 #define COMPILING_CILK_API_FUNCTIONS
57 #include <internal/abi.h>
58 #include <cilk/cilk_api.h>
60 #include "os.h"
61 #include "os_mutex.h"
62 #include "bug.h"
63 #include "global_state.h"
64 #include "local_state.h"
65 #include "scheduler.h"
66 #include "sysdep.h"
68 CILK_API_VOID __cilkrts_init(void)
70 // Initialize, but don't start, the cilk runtime.
71 __cilkrts_init_internal(0);
74 CILK_API_VOID __cilkrts_end_cilk(void)
76 // Take out the global OS mutex while we do this to protect against
77 // another thread attempting to bind while we do this
78 global_os_mutex_lock();
80 if (cilkg_is_published()) {
81 global_state_t *g = cilkg_get_global_state();
82 if (g->Q || __cilkrts_get_tls_worker())
83 __cilkrts_bug("Attempt to shut down Cilk while Cilk is still "
84 "running");
85 __cilkrts_stop_workers(g);
86 __cilkrts_deinit_internal(g);
89 global_os_mutex_unlock();
92 CILK_API_INT
93 __cilkrts_get_nworkers()
95 return cilkg_get_nworkers();
98 CILK_API_INT
99 __cilkrts_get_total_workers()
101 return cilkg_get_total_workers();
104 CILK_API_INT __cilkrts_get_force_reduce(void)
106 return cilkg_get_force_reduce();
109 CILK_API_INT __cilkrts_set_param(const char* param, const char* value)
111 return cilkg_set_param(param, value);
114 #ifdef _WIN32
115 CILK_API_INT __cilkrts_set_param_w(const wchar_t* param, const wchar_t* value)
117 return cilkg_set_param_w(param, value);
119 #endif // _WIN32
121 /* Return a small integer indicating which Cilk worker the function is
122 * currently running on. Each thread started by the Cilk runtime library
123 * (system worker) has a unique worker number in the range 1..P-1, where P is
124 * the valued returned by __cilkrts_get_nworkers(). All threads started by
125 * the user or by other libraries (user workers) share the worker number 0.
126 * Therefore, the worker number is not unique across multiple user threads.
128 * Implementor's note: The value returned from this function is different from
129 * the value, w->self, used in most debug messages.
131 CILK_API_INT
132 __cilkrts_get_worker_number(void)
134 __cilkrts_worker *w = __cilkrts_get_tls_worker();
136 if (0 == w)
137 /* A non-worker always has a worker number of zero. */
138 return 0;
139 else if (WORKER_USER == w->l->type)
140 /* User worker was once a non-worker, so its number should still be
141 * zero. */
142 return 0;
143 else
144 /* w->self for a system worker is in range 0..(P-1); adjust to 1..P
145 * to avoid conflicting with the user thread's worker number. */
146 return w->self + 1;
150 * Internal definition of the pedigree context. The size of the
151 * structure must match __cilkrts_pedigree_context_t defined in abi.i
153 typedef struct pedigree_context_t
155 /** Size of the structure, in bytes */
156 size_t size;
158 /** Next __cilkrts_pedigree to return */
159 const __cilkrts_pedigree *pedigree;
161 /** Unused. Left over from previous implementation */
162 void *unused1;
164 /** Unused. Left over from previous implementation */
165 void *unused2;
167 // // Debugging aid for pedigree-test:
168 // __cilkrts_stack_frame *expected_sf;
169 } pedigree_context_t;
172 * __cilkrts_get_pedigree_info
174 * Fetch the birthrank for a stack frame. To initialize the walk, both sf_in
175 * and frame_in should be NULL. parent_sf_ptr and parent_frame_ptr provide
176 * context for the stackwalk and should be returned as sf_in and frame_in on
177 * the next call.
179 * Returns:
180 * 0 - Success - birthrank, parent_sf_out and parent_frame_out are valid
181 * >1 - Pedigree walk completed
182 * <1 - Failure - -1: No worker bound to thread, -2: Sanity check failed
185 #define PEDIGREE_WALK_COMPLETE (__cilkrts_pedigree *)-1
187 CILK_API_INT
188 __cilkrts_get_pedigree_info(__cilkrts_pedigree_context_t *external_context,
189 uint64_t *sf_birthrank)
191 pedigree_context_t *context = (pedigree_context_t *)external_context;
193 CILK_ASSERT(sizeof(__cilkrts_pedigree_context_t) ==
194 sizeof(pedigree_context_t));
195 if (context->size != sizeof(pedigree_context_t))
196 return -3; // Invalid size
198 // If the pointer to the last __cilkrts_pedigree is -1, we've
199 // finished the walk. We're still done.
200 if (PEDIGREE_WALK_COMPLETE == context->pedigree)
201 return 1;
203 // The passed in context value contains a pointer to the last
204 // __cilkrts_pedigree returned, or NULL if we're starting a
205 // new walk
206 if (NULL == context->pedigree)
208 __cilkrts_worker *w = __cilkrts_get_tls_worker();
209 __cilkrts_pedigree* pedigree_node;
210 if (NULL != w) {
211 pedigree_node = &w->pedigree;
213 else {
214 pedigree_node = __cilkrts_get_tls_pedigree_leaf(1);
216 context->pedigree = pedigree_node->parent;
218 else
219 context->pedigree = context->pedigree->parent;
221 // Note: If we want to omit the user root node,
222 // stop at context->pedigree->parent instead.
223 if (NULL == context->pedigree)
225 context->pedigree = PEDIGREE_WALK_COMPLETE;
226 return 1;
229 *sf_birthrank = context->pedigree->rank;
230 return 0;
233 CILK_API_PEDIGREE
234 __cilkrts_get_pedigree_internal(__cilkrts_worker *w)
236 if (NULL != w) {
237 return w->pedigree;
239 else {
240 const __cilkrts_pedigree *pedigree =
241 __cilkrts_get_tls_pedigree_leaf(1);
242 return *pedigree;
247 CILK_API_INT __cilkrts_bump_worker_rank_internal(__cilkrts_worker *w)
249 __cilkrts_pedigree *pedigree;
250 pedigree = (w ? &w->pedigree : __cilkrts_get_tls_pedigree_leaf(1));
251 pedigree->rank++;
252 return 0;
255 /* End cilk_api.c */