Last active
September 26, 2019 08:56
-
-
Save SF-Zhou/15f31e47089e7ec4d645a98bdfc7656a to your computer and use it in GitHub Desktop.
Coroutine Demo (libco)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <stdint.h> | |
#include <vector> | |
extern "C" void swap_context(void *, void *) asm("swap_context"); | |
asm(R"( | |
swap_context: | |
mov 0x00(%rsp), %rdx | |
lea 0x08(%rsp), %rcx | |
mov %r12, 0x00(%rdi) | |
mov %r13, 0x08(%rdi) | |
mov %r14, 0x10(%rdi) | |
mov %r15, 0x18(%rdi) | |
mov %rdx, 0x20(%rdi) | |
mov %rcx, 0x28(%rdi) | |
mov %rbx, 0x30(%rdi) | |
mov %rbp, 0x38(%rdi) | |
mov 0x00(%rsi), %r12 | |
mov 0x08(%rsi), %r13 | |
mov 0x10(%rsi), %r14 | |
mov 0x18(%rsi), %r15 | |
mov 0x20(%rsi), %rax | |
mov 0x28(%rsi), %rcx | |
mov 0x30(%rsi), %rbx | |
mov 0x38(%rsi), %rbp | |
mov %rcx, %rsp | |
jmpq *%rax | |
)"); | |
struct Registers { | |
void *reg[8]; | |
} ma, co; | |
void func() { | |
std::cout << "Hello Coroutine!" << std::endl; | |
swap_context(&co, &ma); | |
} | |
int main() { | |
std::vector<char> mem(4096); | |
void *stack = (char *)((uintptr_t)(&mem.back()) & ~15ull) - sizeof(void *); | |
co.reg[4] = (void *)func; | |
co.reg[5] = stack; | |
swap_context(&ma, &co); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment