aboutsummaryrefslogtreecommitdiffstats
path: root/coroutine
diff options
context:
space:
mode:
authorSamuel Williams <samuel.williams@oriontransfer.co.nz>2019-06-24 23:54:19 +1200
committerSamuel Williams <samuel.williams@oriontransfer.co.nz>2019-06-26 20:19:53 +1200
commit6c6bf9ffcbfeb8be9d9c342e7604b74ec819e88a (patch)
treea611c21b4a3f2a0c4a1eb43a123f2865c7d4e491 /coroutine
parenta84a99ffabf04d90be64ff28cf2e11766f6cce52 (diff)
downloadruby-6c6bf9ffcbfeb8be9d9c342e7604b74ec819e88a.tar.gz
Add `ucontext` coroutine implementation for generic fallback.
Diffstat (limited to 'coroutine')
-rw-r--r--coroutine/amd64/Context.S2
-rw-r--r--coroutine/amd64/Context.h39
-rw-r--r--coroutine/arm32/Context.h38
-rw-r--r--coroutine/arm64/Context.h39
-rw-r--r--coroutine/ppc64le/Context.h39
-rw-r--r--coroutine/ucontext/Context.c16
-rw-r--r--coroutine/ucontext/Context.h66
-rw-r--r--coroutine/win32/Context.h42
-rw-r--r--coroutine/win64/Context.h43
-rw-r--r--coroutine/x86/Context.h39
10 files changed, 199 insertions, 164 deletions
diff --git a/coroutine/amd64/Context.S b/coroutine/amd64/Context.S
index 42fe79c164..ac986b2aa5 100644
--- a/coroutine/amd64/Context.S
+++ b/coroutine/amd64/Context.S
@@ -27,7 +27,7 @@ PREFIXED_SYMBOL(SYMBOL_PREFIX,coroutine_transfer):
# Restore callee stack pointer
movq (%rsi), %rsp
- # Restore callee stack
+ # Restore callee state
popq %r15
popq %r14
popq %r13
diff --git a/coroutine/amd64/Context.h b/coroutine/amd64/Context.h
index 4ae31d157b..8fe323c1a1 100644
--- a/coroutine/amd64/Context.h
+++ b/coroutine/amd64/Context.h
@@ -10,35 +10,32 @@
#include <assert.h>
#include <string.h>
-#if __cplusplus
-extern "C" {
-#endif
-
#define COROUTINE __attribute__((noreturn)) void
enum {COROUTINE_REGISTERS = 6};
-typedef struct
+struct coroutine_context
{
void **stack_pointer;
-} coroutine_context;
+};
+
+typedef COROUTINE(* coroutine_start)(struct coroutine_context *from, struct coroutine_context *self);
-typedef COROUTINE(* coroutine_start)(coroutine_context *from, coroutine_context *self);
+static inline void coroutine_initialize_main(struct coroutine_context * context) {
+ context->stack_pointer = NULL;
+}
static inline void coroutine_initialize(
- coroutine_context *context,
+ struct coroutine_context *context,
coroutine_start start,
- void *stack_pointer,
- size_t stack_size
+ void *stack,
+ size_t size
) {
- /* Force 16-byte alignment */
- context->stack_pointer = (void**)((uintptr_t)stack_pointer & ~0xF);
+ assert(start && stack && size >= 1024);
- if (!start) {
- assert(!context->stack_pointer);
- /* We are main coroutine for this thread */
- return;
- }
+ // Stack grows down. Force 16-byte alignment.
+ char * top = (char*)stack + size;
+ context->stack_pointer = (void**)((uintptr_t)top & ~0xF);
*--context->stack_pointer = NULL;
*--context->stack_pointer = (void*)start;
@@ -47,13 +44,9 @@ static inline void coroutine_initialize(
memset(context->stack_pointer, 0, sizeof(void*) * COROUTINE_REGISTERS);
}
-coroutine_context * coroutine_transfer(coroutine_context * current, coroutine_context * target);
+struct coroutine_context * coroutine_transfer(struct coroutine_context * current, struct coroutine_context * target);
-static inline void coroutine_destroy(coroutine_context * context)
+static inline void coroutine_destroy(struct coroutine_context * context)
{
context->stack_pointer = NULL;
}
-
-#if __cplusplus
-}
-#endif
diff --git a/coroutine/arm32/Context.h b/coroutine/arm32/Context.h
index 8105bd2cff..27f97908bc 100644
--- a/coroutine/arm32/Context.h
+++ b/coroutine/arm32/Context.h
@@ -10,34 +10,32 @@
#include <assert.h>
#include <string.h>
-#if __cplusplus
-extern "C" {
-#endif
-
#define COROUTINE __attribute__((noreturn)) void
enum {COROUTINE_REGISTERS = 8};
-typedef struct
+struct coroutine_context
{
void **stack_pointer;
-} coroutine_context;
+};
+
+typedef COROUTINE(* coroutine_start)(struct coroutine_context *from, struct coroutine_context *self);
-typedef COROUTINE(* coroutine_start)(coroutine_context *from, coroutine_context *self);
+static inline void coroutine_initialize_main(struct coroutine_context * context) {
+ context->stack_pointer = NULL;
+}
static inline void coroutine_initialize(
- coroutine_context *context,
+ struct coroutine_context *context,
coroutine_start start,
- void *stack_pointer,
- size_t stack_size
+ void *stack,
+ size_t size
) {
- context->stack_pointer = (void**)stack_pointer;
+ assert(start && stack && size >= 1024);
- if (!start) {
- assert(!context->stack_pointer);
- /* We are main coroutine for this thread */
- return;
- }
+ // Stack grows down. Force 16-byte alignment.
+ char * top = (char*)stack + size;
+ context->stack_pointer = (void**)((uintptr_t)top & ~0xF);
*--context->stack_pointer = (void*)start;
@@ -45,12 +43,8 @@ static inline void coroutine_initialize(
memset(context->stack_pointer, 0, sizeof(void*) * COROUTINE_REGISTERS);
}
-coroutine_context * coroutine_transfer(coroutine_context * current, coroutine_context * target);
+struct coroutine_context * coroutine_transfer(struct coroutine_context * current, struct coroutine_context * target);
-static inline void coroutine_destroy(coroutine_context * context)
+static inline void coroutine_destroy(struct coroutine_context * context)
{
}
-
-#if __cplusplus
-}
-#endif
diff --git a/coroutine/arm64/Context.h b/coroutine/arm64/Context.h
index ed646e818b..a1ae921144 100644
--- a/coroutine/arm64/Context.h
+++ b/coroutine/arm64/Context.h
@@ -10,35 +10,32 @@
#include <assert.h>
#include <string.h>
-#if __cplusplus
-extern "C" {
-#endif
-
#define COROUTINE __attribute__((noreturn)) void
enum {COROUTINE_REGISTERS = 0xb0 / 8};
-typedef struct
+struct coroutine_context
{
void **stack_pointer;
-} coroutine_context;
+};
+
+typedef COROUTINE(* coroutine_start)(struct coroutine_context *from, struct coroutine_context *self);
-typedef COROUTINE(* coroutine_start)(coroutine_context *from, coroutine_context *self);
+static inline void coroutine_initialize_main(struct coroutine_context * context) {
+ context->stack_pointer = NULL;
+}
static inline void coroutine_initialize(
- coroutine_context *context,
+ struct coroutine_context *context,
coroutine_start start,
- void *stack_pointer,
- size_t stack_size
+ void *stack,
+ size_t size
) {
- /* Force 16-byte alignment */
- context->stack_pointer = (void**)((uintptr_t)stack_pointer & ~0xF);
+ assert(start && stack && size >= 1024);
- if (!start) {
- assert(!context->stack_pointer);
- /* We are main coroutine for this thread */
- return;
- }
+ // Stack grows down. Force 16-byte alignment.
+ char * top = (char*)stack + size;
+ context->stack_pointer = (void**)((uintptr_t)top & ~0xF);
context->stack_pointer -= COROUTINE_REGISTERS;
memset(context->stack_pointer, 0, sizeof(void*) * COROUTINE_REGISTERS);
@@ -46,12 +43,8 @@ static inline void coroutine_initialize(
context->stack_pointer[0xa0 / 8] = (void*)start;
}
-coroutine_context * coroutine_transfer(coroutine_context * current, coroutine_context * target);
+struct coroutine_context * coroutine_transfer(struct coroutine_context * current, struct coroutine_context * target);
-static inline void coroutine_destroy(coroutine_context * context)
+static inline void coroutine_destroy(struct coroutine_context * context)
{
}
-
-#if __cplusplus
-}
-#endif
diff --git a/coroutine/ppc64le/Context.h b/coroutine/ppc64le/Context.h
index 5971cd8a9b..adf21b4fd9 100644
--- a/coroutine/ppc64le/Context.h
+++ b/coroutine/ppc64le/Context.h
@@ -3,10 +3,6 @@
#include <assert.h>
#include <string.h>
-#if __cplusplus
-extern "C" {
-#endif
-
#define COROUTINE __attribute__((noreturn)) void
enum {
@@ -15,27 +11,28 @@ enum {
+ 4 /* space for fiber_entry() to store the link register */
};
-typedef struct
+struct coroutine_context
{
void **stack_pointer;
-} coroutine_context;
+};
-typedef COROUTINE(* coroutine_start)(coroutine_context *from, coroutine_context *self);
+typedef COROUTINE(* coroutine_start)(struct coroutine_context *from, struct coroutine_context *self);
+
+static inline void coroutine_initialize_main(struct coroutine_context * context) {
+ context->stack_pointer = NULL;
+}
static inline void coroutine_initialize(
- coroutine_context *context,
+ struct coroutine_context *context,
coroutine_start start,
- void *stack_pointer,
- size_t stack_size
+ void *stack,
+ size_t size
) {
- /* Force 16-byte alignment */
- context->stack_pointer = (void**)((uintptr_t)stack_pointer & ~0xF);
+ assert(start && stack && size >= 1024);
- if (!start) {
- assert(!context->stack_pointer);
- /* We are main coroutine for this thread */
- return;
- }
+ // Stack grows down. Force 16-byte alignment.
+ char * top = (char*)stack + size;
+ context->stack_pointer = (void**)((uintptr_t)top & ~0xF);
context->stack_pointer -= COROUTINE_REGISTERS;
memset(context->stack_pointer, 0, sizeof(void*) * COROUTINE_REGISTERS);
@@ -44,13 +41,9 @@ static inline void coroutine_initialize(
context->stack_pointer[18] = ((char*)start) + 8;
}
-coroutine_context * coroutine_transfer(coroutine_context * current, coroutine_context * target);
+struct coroutine_context * coroutine_transfer(struct coroutine_context * current, struct coroutine_context * target);
-static inline void coroutine_destroy(coroutine_context * context)
+static inline void coroutine_destroy(struct coroutine_context * context)
{
context->stack_pointer = NULL;
}
-
-#if __cplusplus
-}
-#endif
diff --git a/coroutine/ucontext/Context.c b/coroutine/ucontext/Context.c
new file mode 100644
index 0000000000..ab8fda923e
--- /dev/null
+++ b/coroutine/ucontext/Context.c
@@ -0,0 +1,16 @@
+/*
+ * This file is part of the "Coroutine" project and released under the MIT License.
+ *
+ * Created by Samuel Williams on 24/6/2019.
+ * Copyright, 2019, by Samuel Williams. All rights reserved.
+*/
+
+#include "Context.h"
+
+void coroutine_trampoline(void * _start, void * _context)
+{
+ coroutine_start start = _start;
+ struct coroutine_context * context = _context;
+
+ start(context->from, context);
+}
diff --git a/coroutine/ucontext/Context.h b/coroutine/ucontext/Context.h
new file mode 100644
index 0000000000..bde9be302a
--- /dev/null
+++ b/coroutine/ucontext/Context.h
@@ -0,0 +1,66 @@
+/*
+ * This file is part of the "Coroutine" project and released under the MIT License.
+ *
+ * Created by Samuel Williams on 24/6/2019.
+ * Copyright, 2019, by Samuel Williams. All rights reserved.
+*/
+
+#pragma once
+
+#include <assert.h>
+#include <stddef.h>
+#include <ucontext.h>
+
+#define COROUTINE __attribute__((noreturn)) void
+
+struct coroutine_context
+{
+ ucontext_t state;
+ struct coroutine_context * from;
+};
+
+typedef COROUTINE(* coroutine_start)(struct coroutine_context *from, struct coroutine_context *self);
+
+COROUTINE coroutine_trampoline(void * _start, void * _context);
+
+static inline void coroutine_initialize_main(struct coroutine_context * context) {
+ context->from = NULL;
+ getcontext(&context->state);
+}
+
+static inline void coroutine_initialize(
+ struct coroutine_context *context,
+ coroutine_start start,
+ void *stack,
+ size_t size
+) {
+ assert(start && stack && size >= 1024);
+
+ coroutine_initialize_main(context);
+
+ context->state.uc_stack.ss_size = size;
+ // Despite what it's called, this is not actually a stack pointer. It points to the address of the stack allocation (the lowest address).
+ context->state.uc_stack.ss_sp = (char*)stack;
+ context->state.uc_stack.ss_flags = 0;
+ context->state.uc_link = NULL;
+
+ makecontext(&context->state, (void(*)(void))coroutine_trampoline, 2, (void*)start, (void*)context);
+}
+
+static inline struct coroutine_context * coroutine_transfer(struct coroutine_context * current, struct coroutine_context * target)
+{
+ struct coroutine_context * previous = target->from;
+
+ target->from = current;
+ swapcontext(&current->state, &target->state);
+ target->from = previous;
+
+ return target;
+}
+
+static inline void coroutine_destroy(struct coroutine_context * context)
+{
+ context->state.uc_stack.ss_sp = NULL;
+ context->state.uc_stack.ss_size = 0;
+ context->from = NULL;
+}
diff --git a/coroutine/win32/Context.h b/coroutine/win32/Context.h
index 779a9a8938..fe1a8a3648 100644
--- a/coroutine/win32/Context.h
+++ b/coroutine/win32/Context.h
@@ -10,53 +10,47 @@
#include <assert.h>
#include <string.h>
-#if __cplusplus
-extern "C" {
-#endif
-
#define COROUTINE __declspec(noreturn) void __fastcall
/* This doesn't include thread information block */
enum {COROUTINE_REGISTERS = 4};
-typedef struct
+struct coroutine_context
{
void **stack_pointer;
-} coroutine_context;
+};
+
+typedef void(__fastcall * coroutine_start)(struct coroutine_context *from, struct coroutine_context *self);
-typedef void(__fastcall * coroutine_start)(coroutine_context *from, coroutine_context *self);
+static inline void coroutine_initialize_main(struct coroutine_context * context) {
+ context->stack_pointer = NULL;
+}
static inline void coroutine_initialize(
- coroutine_context *context,
+ struct coroutine_context *context,
coroutine_start start,
- void *stack_pointer,
- size_t stack_size
+ void *stack,
+ size_t size
) {
- context->stack_pointer = (void**)stack_pointer;
+ assert(start && stack && size >= 1024);
- if (!start) {
- assert(!context->stack_pointer);
- /* We are main coroutine for this thread */
- return;
- }
+ // Stack grows down. Force 16-byte alignment.
+ char * top = (char*)stack + size;
+ context->stack_pointer = (void**)((uintptr_t)top & ~0xF);
*--context->stack_pointer = (void*)start;
/* Windows Thread Information Block */
*--context->stack_pointer = 0; /* fs:[0] */
- *--context->stack_pointer = (void*)stack_pointer; /* fs:[4] */
- *--context->stack_pointer = (void*)((char *)stack_pointer - stack_size); /* fs:[8] */
+ *--context->stack_pointer = (void*)top; /* fs:[4] */
+ *--context->stack_pointer = (void*)stack; /* fs:[8] */
context->stack_pointer -= COROUTINE_REGISTERS;
memset(context->stack_pointer, 0, sizeof(void*) * COROUTINE_REGISTERS);
}
-coroutine_context * __fastcall coroutine_transfer(coroutine_context * current, coroutine_context * target);
+struct coroutine_context * __fastcall coroutine_transfer(struct coroutine_context * current, struct coroutine_context * target);
-static inline void coroutine_destroy(coroutine_context * context)
+static inline void coroutine_destroy(struct coroutine_context * context)
{
}
-
-#if __cplusplus
-}
-#endif
diff --git a/coroutine/win64/Context.h b/coroutine/win64/Context.h
index eb4540b779..6bf2dc5b35 100644
--- a/coroutine/win64/Context.h
+++ b/coroutine/win64/Context.h
@@ -10,10 +10,6 @@
#include <assert.h>
#include <string.h>
-#if __cplusplus
-extern "C" {
-#endif
-
#define COROUTINE __declspec(noreturn) void
enum {
@@ -21,29 +17,30 @@ enum {
COROUTINE_XMM_REGISTERS = 1+10*2,
};
-typedef struct
+struct coroutine_context
{
void **stack_pointer;
-} coroutine_context;
+};
-typedef void(* coroutine_start)(coroutine_context *from, coroutine_context *self);
+typedef void(* coroutine_start)(struct coroutine_context *from, struct coroutine_context *self);
void coroutine_trampoline();
+static inline void coroutine_initialize_main(struct coroutine_context * context) {
+ context->stack_pointer = NULL;
+}
+
static inline void coroutine_initialize(
- coroutine_context *context,
+ struct coroutine_context *context,
coroutine_start start,
- void *stack_pointer,
- size_t stack_size
+ void *stack,
+ size_t size
) {
- /* Force 16-byte alignment */
- context->stack_pointer = (void**)((uintptr_t)stack_pointer & ~0xF);
+ assert(start && stack && size >= 1024);
- if (!start) {
- assert(!context->stack_pointer);
- /* We are main coroutine for this thread */
- return;
- }
+ // Stack grows down. Force 16-byte alignment.
+ char * top = (char*)stack + size;
+ context->stack_pointer = (void**)((uintptr_t)top & ~0xF);
/* Win64 ABI requires space for arguments */
context->stack_pointer -= 4;
@@ -55,20 +52,16 @@ static inline void coroutine_initialize(
/* Windows Thread Information Block */
/* *--context->stack_pointer = 0; */ /* gs:[0x00] is not used */
- *--context->stack_pointer = (void*)stack_pointer; /* gs:[0x08] */
- *--context->stack_pointer = (void*)((char *)stack_pointer - stack_size); /* gs:[0x10] */
+ *--context->stack_pointer = (void*)top; /* gs:[0x08] */
+ *--context->stack_pointer = (void*)stack; /* gs:[0x10] */
context->stack_pointer -= COROUTINE_REGISTERS;
memset(context->stack_pointer, 0, sizeof(void*) * COROUTINE_REGISTERS);
memset(context->stack_pointer - COROUTINE_XMM_REGISTERS, 0, sizeof(void*) * COROUTINE_XMM_REGISTERS);
}
-coroutine_context * coroutine_transfer(coroutine_context * current, coroutine_context * target);
+struct coroutine_context * coroutine_transfer(struct coroutine_context * current, struct coroutine_context * target);
-static inline void coroutine_destroy(coroutine_context * context)
+static inline void coroutine_destroy(struct coroutine_context * context)
{
}
-
-#if __cplusplus
-}
-#endif
diff --git a/coroutine/x86/Context.h b/coroutine/x86/Context.h
index b471ecd9fe..a783dd296f 100644
--- a/coroutine/x86/Context.h
+++ b/coroutine/x86/Context.h
@@ -10,35 +10,32 @@
#include <assert.h>
#include <string.h>
-#if __cplusplus
-extern "C" {
-#endif
-
#define COROUTINE __attribute__((noreturn, fastcall)) void
enum {COROUTINE_REGISTERS = 4};
-typedef struct
+struct coroutine_context
{
void **stack_pointer;
-} coroutine_context;
+};
+
+typedef COROUTINE(* coroutine_start)(struct coroutine_context *from, struct coroutine_context *self) __attribute__((fastcall));
-typedef COROUTINE(* coroutine_start)(coroutine_context *from, coroutine_context *self) __attribute__((fastcall));
+static inline void coroutine_initialize_main(struct coroutine_context * context) {
+ context->stack_pointer = NULL;
+}
static inline void coroutine_initialize(
- coroutine_context *context,
+ struct coroutine_context *context,
coroutine_start start,
- void *stack_pointer,
- size_t stack_size
+ void *stack,
+ size_t size
) {
- /* Force 16-byte alignment */
- context->stack_pointer = (void**)((uintptr_t)stack_pointer & ~0xF);
+ assert(start && stack && size >= 1024);
- if (!start) {
- assert(!context->stack_pointer);
- /* We are main coroutine for this thread */
- return;
- }
+ // Stack grows down. Force 16-byte alignment.
+ char * top = (char*)stack + size;
+ context->stack_pointer = (void**)((uintptr_t)top & ~0xF);
*--context->stack_pointer = NULL;
*--context->stack_pointer = (void*)start;
@@ -47,13 +44,9 @@ static inline void coroutine_initialize(
memset(context->stack_pointer, 0, sizeof(void*) * COROUTINE_REGISTERS);
}
-coroutine_context * coroutine_transfer(coroutine_context * current, coroutine_context * target) __attribute__((fastcall));
+struct coroutine_context * coroutine_transfer(struct coroutine_context * current, struct coroutine_context * target) __attribute__((fastcall));
-static inline void coroutine_destroy(coroutine_context * context)
+static inline void coroutine_destroy(struct coroutine_context * context)
{
context->stack_pointer = NULL;
}
-
-#if __cplusplus
-}
-#endif