Ruby 2.7.6p219 (2022-04-12 revision c9c2245c0a25176072e02db9254f0e0c84c805cd)
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 <string.h>
12
13#define COROUTINE __attribute__((noreturn)) void
14
15enum {COROUTINE_REGISTERS = 0xb0 / 8};
16
18{
19 void **stack_pointer;
20};
21
23
24static inline void coroutine_initialize_main(struct coroutine_context * context) {
25 context->stack_pointer = NULL;
26}
27
28static inline void coroutine_initialize(
29 struct coroutine_context *context,
30 coroutine_start start,
31 void *stack,
32 size_t size
33) {
34 assert(start && stack && size >= 1024);
35
36 // Stack grows down. Force 16-byte alignment.
37 char * top = (char*)stack + size;
38 context->stack_pointer = (void**)((uintptr_t)top & ~0xF);
39
41 memset(context->stack_pointer, 0, sizeof(void*) * COROUTINE_REGISTERS);
42
43 context->stack_pointer[0xa0 / 8] = (void*)start;
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}
COROUTINE(* coroutine_start)(struct coroutine_context *from, struct coroutine_context *self)
Definition: Context.h:22
struct coroutine_context * coroutine_transfer(struct coroutine_context *current, struct coroutine_context *target)
Definition: Context.c:136
@ COROUTINE_REGISTERS
Definition: Context.h:15
#define COROUTINE
Definition: Context.h:13
unsigned int top
Definition: nkf.c:4323
#define NULL
void * memset(void *, int, size_t)
unsigned int size
__uintptr_t uintptr_t
#define assert
struct coroutine_context * from
Definition: Context.h:37
void ** stack_pointer
Definition: Context.h:19
void * stack
Definition: Context.h:29