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 __declspec(noreturn) void
16
17enum {
20};
21
23{
24 void **stack_pointer;
25};
26
27typedef void(* coroutine_start)(struct coroutine_context *from, struct coroutine_context *self);
28
30
31static inline void coroutine_initialize_main(struct coroutine_context * context) {
32 context->stack_pointer = NULL;
33}
34
35static inline void coroutine_initialize(
36 struct coroutine_context *context,
37 coroutine_start start,
38 void *stack,
39 size_t size
40) {
41 assert(start && stack && size >= 1024);
42
43 // Stack grows down. Force 16-byte alignment.
44 char * top = (char*)stack + size;
45 context->stack_pointer = (void**)((uintptr_t)top & ~0xF);
46
47 /* Win64 ABI requires space for arguments */
48 context->stack_pointer -= 4;
49
50 /* Return address */
51 *--context->stack_pointer = 0;
52 *--context->stack_pointer = (void*)start;
53 *--context->stack_pointer = (void*)coroutine_trampoline;
54
55 /* Windows Thread Information Block */
56 /* *--context->stack_pointer = 0; */ /* gs:[0x00] is not used */
57 *--context->stack_pointer = (void*)top; /* gs:[0x08] */
58 *--context->stack_pointer = (void*)stack; /* gs:[0x10] */
59
61 memset(context->stack_pointer, 0, sizeof(void*) * COROUTINE_REGISTERS);
62 memset(context->stack_pointer - COROUTINE_XMM_REGISTERS, 0, sizeof(void*) * COROUTINE_XMM_REGISTERS);
63}
64
65struct coroutine_context * coroutine_transfer(struct coroutine_context * current, struct coroutine_context * target);
66
67static inline void coroutine_destroy(struct coroutine_context * context)
68{
69}
COROUTINE(* coroutine_start)(struct coroutine_context *from, struct coroutine_context *self)
Definition: Context.h:24
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
void coroutine_trampoline()
@ COROUTINE_XMM_REGISTERS
Definition: Context.h:19