Ruby: coroutine/ppc64le/Context.h Source File

Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)

Context.h
Go to the documentation of this file.
1#pragma once
2
3#include <assert.h>
4#include <stddef.h>
5#include <stdint.h>
6#include <string.h>
7
8#define COROUTINE __attribute__((noreturn)) void
9
10enum {
11 COROUTINE_REGISTERS =
12 19 /* 18 general purpose registers (r14-r31) and 1 return address */
13 + 4 /* space for fiber_entry() to store the link register */
14};
15
16struct coroutine_context
17{
18 void **stack_pointer;
19};
20
21 typedef COROUTINE(* coroutine_start)(struct coroutine_context *from, struct coroutine_context *self);
22
23static inline void coroutine_initialize_main(struct coroutine_context * context) {
24 context->stack_pointer = NULL;
25}
26
27static inline void coroutine_initialize(
28 struct coroutine_context *context,
29 coroutine_start start,
30 void *stack,
31 size_t size
32) {
33 assert(start && stack && size >= 1024);
34
35 // Stack grows down. Force 16-byte alignment.
36 char * top = (char*)stack + size;
37 context->stack_pointer = (void**)((uintptr_t)top & ~0xF);
38
39 context->stack_pointer -= COROUTINE_REGISTERS;
40 memset(context->stack_pointer, 0, sizeof(void*) * COROUTINE_REGISTERS);
41
42 /* Skip a global prologue that sets the TOC register */
43 context->stack_pointer[18] = ((char*)start) + 8;
44}
45
46struct coroutine_context * coroutine_transfer(struct coroutine_context * current, struct coroutine_context * target);
47
48static inline void coroutine_destroy(struct coroutine_context * context)
49{
50 context->stack_pointer = NULL;
51}
coroutine_start
COROUTINE(* coroutine_start)(struct coroutine_context *from, struct coroutine_context *self)
Definition: Context.h:24
COROUTINE
#define COROUTINE
Definition: Context.h:15
coroutine_transfer
struct coroutine_context * coroutine_transfer(struct coroutine_context *current, struct coroutine_context *target)
Definition: Context.c:136
COROUTINE_REGISTERS
@ COROUTINE_REGISTERS
Definition: Context.h:17
assert.h
assert
#define assert(x)
Definition: dlmalloc.c:1176
size
voidpf void uLong size
Definition: ioapi.h:138
top
unsigned int top
Definition: nkf.c:4323
NULL
#define NULL
Definition: regenc.h:69
coroutine_context
Definition: Context.h:20
coroutine_context::from
struct coroutine_context * from
Definition: Context.h:41
coroutine_context::stack_pointer
void ** stack_pointer
Definition: Context.h:21
coroutine_context::stack
void * stack
Definition: Context.h:33
uintptr_t
unsigned int uintptr_t
Definition: win32.h:106