Ruby
3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)
coroutine
win32
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 __fastcall
16
#define COROUTINE_LIMITED_ADDRESS_SPACE
17
18
/* This doesn't include thread information block */
19
enum
{
COROUTINE_REGISTERS
= 4};
20
21
struct
coroutine_context
22
{
23
void
**
stack_pointer
;
24
};
25
26
typedef
void(__fastcall *
coroutine_start
)(
struct
coroutine_context
*
from
,
struct
coroutine_context
*self);
27
28
static
inline
void
coroutine_initialize_main(
struct
coroutine_context
* context) {
29
context->
stack_pointer
=
NULL
;
30
}
31
32
static
inline
void
coroutine_initialize(
33
struct
coroutine_context
*context,
34
coroutine_start
start,
35
void
*
stack
,
36
size_t
size
37
) {
38
assert
(start &&
stack
&&
size
>= 1024);
39
40
// Stack grows down. Force 16-byte alignment.
41
char
*
top
= (
char
*)
stack
+
size
;
42
context->
stack_pointer
= (
void
**)((
uintptr_t
)
top
& ~0xF);
43
44
*--context->
stack_pointer
= (
void
*)start;
45
46
/* Windows Thread Information Block */
47
*--context->
stack_pointer
= (
void
*)0xFFFFFFFF;
/* fs:[0] */
48
*--context->
stack_pointer
= (
void
*)
top
;
/* fs:[4] */
49
*--context->
stack_pointer
= (
void
*)
stack
;
/* fs:[8] */
50
51
context->
stack_pointer
-=
COROUTINE_REGISTERS
;
52
memset(context->
stack_pointer
, 0,
sizeof
(
void
*) *
COROUTINE_REGISTERS
);
53
}
54
55
struct
coroutine_context
* __fastcall
coroutine_transfer
(
struct
coroutine_context
* current,
struct
coroutine_context
* target);
56
57
static
inline
void
coroutine_destroy(
struct
coroutine_context
* context)
58
{
59
}
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
Generated by
1.9.5