2021-01-22 17:21:25 +03:00
|
|
|
#ifndef COROUTINE_X86_CONTEXT_H
|
|
|
|
#define COROUTINE_X86_CONTEXT_H 1
|
|
|
|
|
2018-11-20 13:16:29 +03:00
|
|
|
/*
|
|
|
|
* This file is part of the "Coroutine" project and released under the MIT License.
|
|
|
|
*
|
|
|
|
* Created by Samuel Williams on 3/11/2018.
|
2019-12-28 02:45:37 +03:00
|
|
|
* Copyright, 2018, by Samuel Williams.
|
2018-11-20 13:16:29 +03:00
|
|
|
*/
|
2018-11-20 13:13:59 +03:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <assert.h>
|
2019-12-04 11:16:30 +03:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
2018-11-20 13:13:59 +03:00
|
|
|
#include <string.h>
|
|
|
|
|
2018-11-20 13:16:41 +03:00
|
|
|
#define COROUTINE __attribute__((noreturn, fastcall)) void
|
2019-07-16 07:11:55 +03:00
|
|
|
#define COROUTINE_LIMITED_ADDRESS_SPACE
|
2018-11-20 13:13:59 +03:00
|
|
|
|
2019-05-16 09:51:37 +03:00
|
|
|
enum {COROUTINE_REGISTERS = 4};
|
2018-11-20 13:13:59 +03:00
|
|
|
|
2019-06-24 14:54:19 +03:00
|
|
|
struct coroutine_context
|
2018-11-20 13:13:59 +03:00
|
|
|
{
|
2018-11-20 23:09:38 +03:00
|
|
|
void **stack_pointer;
|
2021-06-26 01:17:26 +03:00
|
|
|
void *argument;
|
2019-06-24 14:54:19 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef COROUTINE(* coroutine_start)(struct coroutine_context *from, struct coroutine_context *self) __attribute__((fastcall));
|
2018-11-20 13:13:59 +03:00
|
|
|
|
2019-06-24 14:54:19 +03:00
|
|
|
static inline void coroutine_initialize_main(struct coroutine_context * context) {
|
|
|
|
context->stack_pointer = NULL;
|
|
|
|
}
|
2018-11-20 13:13:59 +03:00
|
|
|
|
2018-11-20 13:17:17 +03:00
|
|
|
static inline void coroutine_initialize(
|
2019-06-24 14:54:19 +03:00
|
|
|
struct coroutine_context *context,
|
2018-11-20 23:09:38 +03:00
|
|
|
coroutine_start start,
|
2019-06-24 14:54:19 +03:00
|
|
|
void *stack,
|
|
|
|
size_t size
|
2018-11-20 13:13:59 +03:00
|
|
|
) {
|
2019-06-24 14:54:19 +03:00
|
|
|
assert(start && stack && size >= 1024);
|
2018-11-20 13:13:59 +03:00
|
|
|
|
2019-06-24 14:54:19 +03:00
|
|
|
// Stack grows down. Force 16-byte alignment.
|
|
|
|
char * top = (char*)stack + size;
|
|
|
|
context->stack_pointer = (void**)((uintptr_t)top & ~0xF);
|
2018-11-20 13:13:59 +03:00
|
|
|
|
2018-11-20 23:09:38 +03:00
|
|
|
*--context->stack_pointer = NULL;
|
2024-10-08 09:41:28 +03:00
|
|
|
*--context->stack_pointer = (void*)(uintptr_t)start;
|
2018-11-20 13:13:59 +03:00
|
|
|
|
2018-11-20 23:09:38 +03:00
|
|
|
context->stack_pointer -= COROUTINE_REGISTERS;
|
|
|
|
memset(context->stack_pointer, 0, sizeof(void*) * COROUTINE_REGISTERS);
|
2018-11-20 13:13:59 +03:00
|
|
|
}
|
|
|
|
|
2019-06-24 14:54:19 +03:00
|
|
|
struct coroutine_context * coroutine_transfer(struct coroutine_context * current, struct coroutine_context * target) __attribute__((fastcall));
|
2018-11-20 13:13:59 +03:00
|
|
|
|
2019-06-24 14:54:19 +03:00
|
|
|
static inline void coroutine_destroy(struct coroutine_context * context)
|
2018-11-20 13:13:59 +03:00
|
|
|
{
|
2018-11-20 23:09:38 +03:00
|
|
|
context->stack_pointer = NULL;
|
2018-11-20 13:13:59 +03:00
|
|
|
}
|
2021-01-22 17:21:25 +03:00
|
|
|
|
|
|
|
#endif /* COROUTINE_X86_CONTEXT_H */
|