Ruby: coroutine/win32/Context.h Source File

Ruby 4.0.5p0 (2026-05-20 revision 64336ffd0ee9e1f4c05891695a3d7b49cb709721)

Context.h
1#ifndef COROUTINE_WIN32_CONTEXT_H
2#define COROUTINE_WIN32_CONTEXT_H 1
3
4/*
5 * This file is part of the "Coroutine" project and released under the MIT License.
6 *
7 * Created by Samuel Williams on 10/5/2018.
8 * Copyright, 2018, by Samuel Williams.
9*/
10
11#pragma once
12
13#include <assert.h>
14#include <stddef.h>
15#include <stdint.h>
16#include <string.h>
17
18#define COROUTINE __declspec(noreturn) void __fastcall
19#define COROUTINE_DECL void __fastcall
20#define COROUTINE_LIMITED_ADDRESS_SPACE
21
22/* This doesn't include thread information block */
23enum {COROUTINE_REGISTERS = 4};
24
25struct coroutine_context
26{
27 void **stack_pointer;
28 void *argument;
29};
30
31typedef void(__fastcall * coroutine_start)(struct coroutine_context *from, struct coroutine_context *self);
32
33static inline void coroutine_initialize_main(struct coroutine_context * context) {
34 context->stack_pointer = NULL;
35}
36
37static inline void coroutine_initialize(
38 struct coroutine_context *context,
39 coroutine_start start,
40 void *stack,
41 size_t size
42) {
43 assert(start && stack && size >= 1024);
44
45 // Stack grows down. Force 16-byte alignment.
46 char * top = (char*)stack + size;
47 context->stack_pointer = (void**)((uintptr_t)top & ~0xF);
48
49 *--context->stack_pointer = (void*)(uintptr_t)start;
50
51 /* Windows Thread Information Block */
52 *--context->stack_pointer = (void*)0xFFFFFFFF; /* fs:[0] */
53 *--context->stack_pointer = (void*)top; /* fs:[4] */
54 *--context->stack_pointer = (void*)stack; /* fs:[8] */
55
56 context->stack_pointer -= COROUTINE_REGISTERS;
57 memset(context->stack_pointer, 0, sizeof(void*) * COROUTINE_REGISTERS);
58}
59
60struct coroutine_context * __fastcall coroutine_transfer(struct coroutine_context * current, struct coroutine_context * target);
61
62static inline void coroutine_destroy(struct coroutine_context * context)
63{
64}
65
66#endif /* COROUTINE_WIN32_CONTEXT_H */
assert.h
coroutine_context
Definition Context.h:36