-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcilk2c.c
183 lines (146 loc) · 6.14 KB
/
cilk2c.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <stdatomic.h>
#include <stdio.h>
#include <unwind.h>
#include "debug.h"
#include "cilk-internal.h"
#include "cilk2c.h"
#include "fiber.h"
#include "global.h"
#include "readydeque.h"
#include "rts-config.h"
#include "scheduler.h"
CHEETAH_INTERNAL
struct closure_exception exception_reducer = {.exn = NULL};
extern void _Unwind_Resume(struct _Unwind_Exception *);
extern _Unwind_Reason_Code _Unwind_RaiseException(struct _Unwind_Exception *);
CHEETAH_INTERNAL struct cilkrts_callbacks cilkrts_callbacks = {
0, 0, false, {NULL}, {NULL}};
// Test if the Cilk runtime has been initialized. This method is intended to
// help initialization of libraries that depend on the OpenCilk runtime.
int __cilkrts_is_initialized(void) { return NULL != default_cilkrts; }
int __cilkrts_running_on_workers(void) {
return !__cilkrts_need_to_cilkify;
}
// These callback-registration methods can run before the runtime system has
// started.
//
// Init callbacks are called in order of registration. Exit callbacks are
// called in reverse order of registration.
// Register a callback to run at Cilk-runtime initialization. Returns 0 on
// successful registration, nonzero otherwise.
int __cilkrts_atinit(void (*callback)(void)) {
if (cilkrts_callbacks.last_init >= MAX_CALLBACKS ||
cilkrts_callbacks.after_init)
return -1;
cilkrts_callbacks.init[cilkrts_callbacks.last_init++] = callback;
return 0;
}
// Register a callback to run at Cilk-runtime exit. Returns 0 on successful
// registration, nonzero otherwise.
int __cilkrts_atexit(void (*callback)(void)) {
if (cilkrts_callbacks.last_exit >= MAX_CALLBACKS)
return -1;
cilkrts_callbacks.exit[cilkrts_callbacks.last_exit++] = callback;
return 0;
}
// Called after a normal cilk_sync or a cilk_sync performed within the
// personality function. Checks if there is an exception that needs to be
// propagated. This is called from the frame that will handle whatever exception
// was thrown.
void __cilkrts_check_exception_raise(__cilkrts_stack_frame *sf) {
__cilkrts_worker *w = get_worker_from_stack(sf);
CILK_ASSERT_POINTER_EQUAL(w, __cilkrts_get_tls_worker());
struct closure_exception *exn_r = get_exception_reducer(w);
char *exn = exn_r->exn;
// zero exception storage, so we don't unintentionally try to
// handle/propagate this exception again
clear_exception_reducer(w, exn_r);
sf->flags &= ~CILK_FRAME_EXCEPTION_PENDING;
if (exn != NULL) {
_Unwind_RaiseException((struct _Unwind_Exception *)exn); // noreturn
}
return;
}
// Checks if there is an exception that needs to be propagated, and if so,
// resumes unwinding with that exception.
void __cilkrts_check_exception_resume(__cilkrts_stack_frame *sf) {
__cilkrts_worker *w = get_worker_from_stack(sf);
CILK_ASSERT_POINTER_EQUAL(w, __cilkrts_get_tls_worker());
struct closure_exception *exn_r = get_exception_reducer(w);
char *exn = exn_r->exn;
// zero exception storage, so we don't unintentionally try to
// handle/propagate this exception again
clear_exception_reducer(w, exn_r);
sf->flags &= ~CILK_FRAME_EXCEPTION_PENDING;
if (exn != NULL) {
_Unwind_Resume((struct _Unwind_Exception *)exn); // noreturn
}
return;
}
// Called by generated exception-handling code, specifically, at the beginning
// of each landingpad in a spawning function. Ensures that the stack pointer
// points at the fiber and call-stack frame containing sf before any catch
// handlers in that frame execute.
void __cilkrts_cleanup_fiber(__cilkrts_stack_frame *sf, int32_t sel) {
(void)sel; // currently unused
__cilkrts_worker *w = get_worker_from_stack(sf);
CILK_ASSERT_POINTER_EQUAL(w, __cilkrts_get_tls_worker());
CILK_ASSERT(__cilkrts_synced(sf));
struct closure_exception *exn_r = get_exception_reducer_or_null(w);
struct cilk_fiber *throwing_fiber = NULL;
char *parent_rsp = NULL;
if (exn_r != NULL) {
throwing_fiber = exn_r->throwing_fiber;
parent_rsp = exn_r->parent_rsp;
exn_r->throwing_fiber = NULL;
clear_exception_reducer(w, exn_r);
}
// If parent_rsp is non-null, then the Cilk personality function executed
// __cilkrts_sync(sf), which implies that sf is at the top of the deque.
// Because we're executing a non-cleanup landingpad, execution is continuing
// within this function frame, rather than unwinding further to a parent
// frame, which would belong to a distinct closure. Hence, if we reach this
// point, set the stack pointer in sf to parent_rsp if parent_rsp is
// non-null.
if (NULL == parent_rsp) {
// If parent_rsp is null, we might have unwound past the point where the
// Cilk personality function performed a sync. Because we're executing
// a non-cleanup landing pad, execution is continuing within this frame,
// and we can safely free any saved throwing fiber.
if (throwing_fiber) {
cilk_fiber_deallocate_to_pool(w, throwing_fiber);
}
return;
}
SP(sf) = (void *)parent_rsp;
// Since we're longjmping to another fiber, we don't need to save
// throwing_fiber anymore.
if (throwing_fiber) {
cilk_fiber_deallocate_to_pool(w, throwing_fiber);
}
__builtin_longjmp(sf->ctx, 1); // Does not return
return;
}
void __cilkrts_sync(__cilkrts_stack_frame *sf) {
__cilkrts_worker *w = get_worker_from_stack(sf);
CILK_ASSERT_POINTER_EQUAL(w, __cilkrts_get_tls_worker());
CILK_ASSERT(CHECK_CILK_FRAME_MAGIC(w->g, sf));
if (Cilk_sync(w, sf) == SYNC_READY) {
// The Cilk_sync restores the original rsp stored in sf->ctx
// if this frame is ready to sync.
sysdep_longjmp_to_sf(sf);
} else {
longjmp_to_runtime(w);
}
}
///////////////////////////////////////////////////////////////////////////
/// Methods for handling extensions
void __cilkrts_register_extension(void *extension) {
__cilkrts_use_extension = true;
__cilkrts_worker *w = __cilkrts_get_tls_worker();
w->extension = extension;
}
void *__cilkrts_get_extension(void) {
__cilkrts_worker *w = __cilkrts_get_tls_worker();
return w->extension;
}