Debian Codegraph @

rpython/translator/c/src/stacklet/stacklet.c

raw symbols SCIP
1 /********** A really minimal coroutine package for C **********
2 * By Armin Rigo
3 */
4
5 #include "src/stacklet/stacklet.h"
6
7 #include <stddef.h>
8 #include <string.h>
9 #include <stdio.h>
10
11 /************************************************************
12 * platform specific code
13 */
14
15 /* The default stack direction is downwards, 0, but platforms
16 * can redefine it to upwards growing, 1.
17 */
18 #define STACK_DIRECTION 0
19 #define STATIC_NOINLINE __attribute__((noinline)) static
20
21 #include "src/stacklet/slp_platformselect.h"
22
23 #if STACK_DIRECTION != 0
24 # error "review this whole code, which depends on STACK_DIRECTION==0 so far"
25 #endif
26
27 /************************************************************/
28
29 /* #define DEBUG_DUMP */
30
31 #ifdef DEBUG_DUMP
32 #include <stdio.h>
33 #endif
34
35 /************************************************************/
36
37 struct stacklet_s {
38 /* The portion of the real stack claimed by this paused stacklet. */
39 char *stack_start; /* the "near" end of the stack */
40 char *stack_stop; /* the "far" end of the stack */
41
42 /* The amount that has been saved away so far, just after this struct.
43 * There is enough allocated space for 'stack_stop - stack_start'
44 * bytes.
45 */
46 ptrdiff_t stack_saved; /* the amount saved */
47
48 /* Internally, some stacklets are arranged in a list, to handle lazy
49 * saving of stacks: if the stacklet has a partially unsaved stack,
50 * this points to the next stacklet with a partially unsaved stack,
51 * creating a linked list with each stacklet's stack_stop higher
52 * than the previous one. The last entry in the list is always the
53 * main stack.
54 */
55 struct stacklet_s *stack_prev;
56
57 stacklet_thread_handle stack_thrd; /* the thread where the stacklet is */
58 };
59
60 struct stacklet_thread_s {
61 struct stacklet_s *g_stack_chain_head; /* NULL <=> running main */
62 char *g_current_stack_stop;
63 char *g_current_stack_marker;
64 struct stacklet_s *g_source;
65 struct stacklet_s *g_target;
66 };
67
68 #define _check(x) do { if (!(x)) _check_failed(#x); } while (0)
69
70 static void _check_failed(const char *check)
71 {
72 fprintf(stderr, "FATAL: stacklet: %s failed\n", check);
73 abort();
74 }
75
76 static void check_valid(struct stacklet_s *g)
77 {
78 _check(g->stack_saved >= 0);
79 }
80
81 /***************************************************************/
82
83 static void g_save(struct stacklet_s* g, char* stop
84 #ifdef DEBUG_DUMP
85 , int overwrite_stack_for_debug
86 #endif
87 )
88 {
89 /* Save more of g's stack into the heap -- at least up to 'stop'
90
91 In the picture below, the C stack is on the left, growing down,
92 and the C heap on the right. The area marked with xxx is the logical
93 stack of the stacklet 'g'. It can be half in the C stack (its older
94 part), and half in the heap (its newer part).
95
96 g->stack_stop |________|
97 |xxxxxxxx|
98 |xxx __ stop .........
99 |xxxxxxxx| ==> : :
100 |________| :_______:
101 | | |xxxxxxx|
102 | | |xxxxxxx|
103 g->stack_start | | |_______| g+1
104
105 */
106 ptrdiff_t sz1 = g->stack_saved;
107 ptrdiff_t sz2 = stop - g->stack_start;
108 check_valid(g);
109 _check(stop <= g->stack_stop);
110
111 if (sz2 > sz1) {
112 char *c = (char *)(g + 1);
113 #if STACK_DIRECTION == 0
114 memcpy(c+sz1, g->stack_start+sz1, sz2-sz1);
115 # ifdef DEBUG_DUMP
116 if (overwrite_stack_for_debug)
117 memset(g->stack_start+sz1, 0xdb, sz2-sz1);
118 # endif
119 #else
120 xxx;
121 #endif
122 g->stack_saved = sz2;
123 }
124 }
125
126 /* Allocate and store in 'g_source' a new stacklet, which has the C
127 * stack from 'old_stack_pointer' to 'g_current_stack_stop'. It is
128 * initially completely unsaved, so it is attached to the head of the
129 * chained list of 'stack_prev'.
130 */
131 static int g_allocate_source_stacklet(void *old_stack_pointer,
132 struct stacklet_thread_s *thrd)
133 {
134 struct stacklet_s *stacklet;
135 ptrdiff_t stack_size = (thrd->g_current_stack_stop -
136 (char *)old_stack_pointer);
137
138 thrd->g_source = malloc(sizeof(struct stacklet_s) + stack_size);
139 if (thrd->g_source == NULL)
140 return -1;
141
142 stacklet = thrd->g_source;
143 stacklet->stack_start = old_stack_pointer;
144 stacklet->stack_stop = thrd->g_current_stack_stop;
145 stacklet->stack_saved = 0;
146 stacklet->stack_prev = thrd->g_stack_chain_head;
147 stacklet->stack_thrd = thrd;
148 thrd->g_stack_chain_head = stacklet;
149 return 0;
150 }
151
152 /* Save more of the C stack away, up to 'target_stop'.
153 */
154 static void g_clear_stack(struct stacklet_s *g_target,
155 struct stacklet_thread_s *thrd)
156 {
157 struct stacklet_s *current = thrd->g_stack_chain_head;
158 char *target_stop = g_target->stack_stop;
159 check_valid(g_target);
160
161 /* save and unlink stacklets that are completely within
162 the area to clear. */
163 while (current != NULL && current->stack_stop <= target_stop) {
164 struct stacklet_s *prev = current->stack_prev;
165 check_valid(current);
166 current->stack_prev = NULL;
167 if (current != g_target) {
168 /* don't bother saving away g_target, because
169 it would be immediately restored */
170 g_save(current, current->stack_stop
171 #ifdef DEBUG_DUMP
172 , 1
173 #endif
174 );
175 }
176 current = prev;
177 }
178
179 /* save a partial stack */
180 if (current != NULL && current->stack_start < target_stop)
181 g_save(current, target_stop
182 #ifdef DEBUG_DUMP
183 , 1
184 #endif
185 );
186
187 thrd->g_stack_chain_head = current;
188 }
189
190 /* This saves the current state in a new stacklet that gets stored in
191 * 'g_source', and save away enough of the stack to allow a jump to
192 * 'g_target'.
193 */
194 static void *g_save_state(void *old_stack_pointer, void *rawthrd)
195 {
196 struct stacklet_thread_s *thrd = (struct stacklet_thread_s *)rawthrd;
197 if (g_allocate_source_stacklet(old_stack_pointer, thrd) < 0)
198 return NULL;
199 g_clear_stack(thrd->g_target, thrd);
200 return thrd->g_target->stack_start;
201 }
202
203 /* This saves the current state in a new stacklet that gets stored in
204 * 'g_source', but returns NULL, to not do any restoring yet.
205 */
206 static void *g_initial_save_state(void *old_stack_pointer, void *rawthrd)
207 {
208 struct stacklet_thread_s *thrd = (struct stacklet_thread_s *)rawthrd;
209 if (g_allocate_source_stacklet(old_stack_pointer, thrd) == 0)
210 g_save(thrd->g_source, thrd->g_current_stack_marker
211 #ifdef DEBUG_DUMP
212 , 0
213 #endif
214 );
215 return NULL;
216 }
217
218 /* Save away enough of the stack to allow a jump to 'g_target'.
219 */
220 static void *g_destroy_state(void *old_stack_pointer, void *rawthrd)
221 {
222 struct stacklet_thread_s *thrd = (struct stacklet_thread_s *)rawthrd;
223 thrd->g_source = EMPTY_STACKLET_HANDLE;
224 g_clear_stack(thrd->g_target, thrd);
225 return thrd->g_target->stack_start;
226 }
227
228 /* Restore the C stack by copying back from the heap in 'g_target',
229 * and free 'g_target'.
230 */
231 static void *g_restore_state(void *new_stack_pointer, void *rawthrd)
232 {
233 /* Restore the heap copy back into the C stack */
234 struct stacklet_thread_s *thrd = (struct stacklet_thread_s *)rawthrd;
235 struct stacklet_s *g = thrd->g_target;
236 ptrdiff_t stack_saved = g->stack_saved;
237 check_valid(g);
238
239 _check(new_stack_pointer == g->stack_start);
240 #if STACK_DIRECTION == 0
241 memcpy(g->stack_start, g+1, stack_saved);
242 #else
243 memcpy(g->stack_start - stack_saved, g+1, stack_saved);
244 #endif
245 thrd->g_current_stack_stop = g->stack_stop;
246 g->stack_saved = -13; /* debugging */
247 free(g);
248 return EMPTY_STACKLET_HANDLE;
249 }
250
251 STATIC_NOINLINE
252 void *_stacklet_switchstack(void *(*save_state)(void*, void*),
253 void *(*restore_state)(void*, void*),
254 void *extra)
255 {
256 return slp_switch(save_state, restore_state, extra);
257 }
258
259 STATIC_NOINLINE
260 void g_initialstub(struct stacklet_thread_s *thrd,
261 stacklet_run_fn run, void *run_arg)
262 {
263 struct stacklet_s *result;
264
265 /* The following call returns twice! */
266 result = (struct stacklet_s *) _stacklet_switchstack(g_initial_save_state,
267 g_restore_state,
268 thrd);
269 if (result == NULL && thrd->g_source != NULL) {
270 /* First time it returns. Only g_initial_save_state() has run
271 and has created 'g_source'. Call run(). */
272 thrd->g_current_stack_stop = thrd->g_current_stack_marker;
273 result = run(thrd->g_source, run_arg);
274
275 /* Then switch to 'result'. */
276 check_valid(result);
277 thrd->g_target = result;
278 _stacklet_switchstack(g_destroy_state, g_restore_state, thrd);
279
280 _check_failed("we should not return here");
281 abort();
282 }
283 /* The second time it returns. */
284 }
285
286 /************************************************************/
287
288 stacklet_thread_handle stacklet_newthread(void)
289 {
290 struct stacklet_thread_s *thrd;
291
292 thrd = malloc(sizeof(struct stacklet_thread_s));
293 if (thrd != NULL)
294 memset(thrd, 0, sizeof(struct stacklet_thread_s));
295 return thrd;
296 }
297
298 void stacklet_deletethread(stacklet_thread_handle thrd)
299 {
300 free(thrd);
301 }
302
303 stacklet_handle stacklet_new(stacklet_thread_handle thrd,
304 stacklet_run_fn run, void *run_arg)
305 {
306 long stackmarker;
307 _check((char *)NULL < (char *)&stackmarker);
308 if (thrd->g_current_stack_stop <= (char *)&stackmarker)
309 thrd->g_current_stack_stop = ((char *)&stackmarker) + 1;
310
311 thrd->g_current_stack_marker = (char *)&stackmarker;
312 g_initialstub(thrd, run, run_arg);
313 return thrd->g_source;
314 }
315
316 stacklet_handle stacklet_switch(stacklet_handle target)
317 {
318 long stackmarker;
319 stacklet_thread_handle thrd = target->stack_thrd;
320 check_valid(target);
321 if (thrd->g_current_stack_stop <= (char *)&stackmarker)
322 thrd->g_current_stack_stop = ((char *)&stackmarker) + 1;
323
324 thrd->g_target = target;
325 _stacklet_switchstack(g_save_state, g_restore_state, thrd);
326 return thrd->g_source;
327 }
328
329 void stacklet_destroy(stacklet_handle target)
330 {
331 check_valid(target);
332 if (target->stack_prev != NULL) {
333 /* 'target' appears to be in the chained list 'unsaved_stack',
334 so remove it from there. Note that if 'thrd' was already
335 deleted, it means that we left the thread and all stacklets
336 still in the thread should be fully copied away from the
337 stack --- so should have stack_prev == NULL. In this case
338 we don't even read 'stack_thrd', already deallocated. */
339 stacklet_thread_handle thrd = target->stack_thrd;
340 struct stacklet_s **pp = &thrd->g_stack_chain_head;
341 for (; *pp != NULL; pp = &(*pp)->stack_prev) {
342 check_valid(*pp);
343 if (*pp == target) {
344 *pp = target->stack_prev;
345 break;
346 }
347 }
348 }
349 target->stack_saved = -11; /* debugging */
350 free(target);
351 }
352
353 char **_stacklet_translate_pointer(stacklet_handle context, char **ptr)
354 {
355 char *p = (char *)ptr;
356 Signed delta;
357 if (context == NULL)
358 return ptr;
359 check_valid(context);
360 delta = p - context->stack_start;
361 if (((Unsigned)delta) < ((Unsigned)context->stack_saved)) {
362 /* a pointer to a saved away word */
363 char *c = (char *)(context + 1);
364 return (char **)(c + delta);
365 }
366 if (((Unsigned)delta) >=
367 (Unsigned)(context->stack_stop - context->stack_start)) {
368 /* out-of-stack pointer! it's only ok if we are the main stacklet
369 and we are reading past the end, because the main stacklet's
370 stack stop is not exactly known. */
371 _check(delta >= 0);
372 _check(((Signed)context->stack_stop) & 1);
373 }
374 return ptr;
375 }