Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)
Context.h
Go to the documentation of this file.
1/*
2 * This file is part of the "Coroutine" project and released under the MIT License.
3 *
4 * Created by Samuel Williams on 10/5/2018.
5 * Copyright, 2018, by Samuel Williams.
6*/
7
8#pragma once
9
10#include <assert.h>
11#include <stddef.h>
12#include <stdint.h>
13#include <string.h>
14
15#define COROUTINE __attribute__((noreturn)) void
16
17enum {COROUTINE_REGISTERS = 0xb0 / 8};
18
20{
21 void **stack_pointer;
22};
23
25
26static inline void coroutine_initialize_main(struct coroutine_context * context) {
27 context->stack_pointer = NULL;
28}
29
30static inline void coroutine_initialize(
31 struct coroutine_context *context,
32 coroutine_start start,
33 void *stack,
34 size_t size
35) {
36 assert(start && stack && size >= 1024);
37
38 // Stack grows down. Force 16-byte alignment.
39 char * top = (char*)stack + size;
40 context->stack_pointer = (void**)((uintptr_t)top & ~0xF);
41
43 memset(context->stack_pointer, 0, sizeof(void*) * COROUTINE_REGISTERS);
44
45 context->stack_pointer[0xa0 / 8] = (void*)start;
46}
47
48struct coroutine_context * coroutine_transfer(struct coroutine_context * current, struct coroutine_context * target);
49
50static inline void coroutine_destroy(struct coroutine_context * context)
51{
52}
COROUTINE(* coroutine_start)(struct coroutine_context *from, struct coroutine_context *self)
Definition: Context.h:24
#define COROUTINE
Definition: Context.h:15
struct coroutine_context * coroutine_transfer(struct coroutine_context *current, struct coroutine_context *target)
Definition: Context.c:136
@ COROUTINE_REGISTERS
Definition: Context.h:17
#define assert(x)
Definition: dlmalloc.c:1176
voidpf void uLong size
Definition: ioapi.h:138
unsigned int top
Definition: nkf.c:4323
#define NULL
Definition: regenc.h:69
struct coroutine_context * from
Definition: Context.h:41
void ** stack_pointer
Definition: Context.h:21
void * stack
Definition: Context.h:33
unsigned int uintptr_t
Definition: win32.h:106