Ruby: coroutine/win64/Context.h Source File

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 {
18 COROUTINE_REGISTERS = 8,
19 COROUTINE_XMM_REGISTERS = 1+10*2,
20};
21
22struct coroutine_context
23{
24 void **stack_pointer;
25};
26
27 typedef void(* coroutine_start)(struct coroutine_context *from, struct coroutine_context *self);
28
29 void coroutine_trampoline();
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
60 context->stack_pointer -= COROUTINE_REGISTERS;
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_start
COROUTINE(* coroutine_start)(struct coroutine_context *from, struct coroutine_context *self)
Definition: Context.h:24
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
coroutine_trampoline
void coroutine_trampoline()
COROUTINE_XMM_REGISTERS
@ COROUTINE_XMM_REGISTERS
Definition: Context.h:19