This commit is contained in:
byuu 2019-10-20 00:28:09 +09:00
parent 3a1855a80f
commit ef1d4b592a
11 changed files with 57 additions and 73 deletions

View File

@ -59,6 +59,10 @@ static void co_init() {
#endif
}
const char* co_method() {
return "aarch64";
}
cothread_t co_active() {
if(!co_active_handle) co_active_handle = &co_active_buffer;
return co_active_handle;
@ -73,7 +77,8 @@ cothread_t co_derive(void* memory, unsigned int size, void (*entrypoint)(void))
if(!co_active_handle) co_active_handle = &co_active_buffer;
if(handle = (unsigned long*)memory) {
unsigned long* p = (unsigned long*)((unsigned char*)handle + size);
unsigned int offset = (size & ~15);
unsigned long* p = (unsigned long*)((unsigned char*)handle + offset);
handle[19] = (unsigned long)p; /* x29 (frame pointer) */
handle[20] = (unsigned long)p; /* x30 (stack pointer) */
handle[21] = (unsigned long)entrypoint; /* x31 (link register) */
@ -83,23 +88,9 @@ cothread_t co_derive(void* memory, unsigned int size, void (*entrypoint)(void))
}
cothread_t co_create(unsigned int size, void (*entrypoint)(void)) {
unsigned long* handle;
if(!co_swap) {
co_init();
co_swap = (void (*)(cothread_t, cothread_t))co_swap_function;
}
if(!co_active_handle) co_active_handle = &co_active_buffer;
size += 256;
size &= ~15;
if(handle = (unsigned long*)malloc(size)) {
unsigned long* p = (unsigned long*)((unsigned char*)handle + size);
handle[19] = (unsigned long)p; /* x29 (frame pointer) */
handle[20] = (unsigned long)p; /* x30 (stack pointer) */
handle[21] = (unsigned long)entrypoint; /* x31 (link register) */
}
return handle;
void* memory = malloc(size);
if(!memory) return (cothread_t)0;
return co_derive(memory, size, entrypoint);
}
void co_delete(cothread_t handle) {

23
amd64.c
View File

@ -115,6 +115,10 @@ static void crash() {
assert(0); /* called only if cothread_t entrypoint returns */
}
const char* co_method() {
return "amd64";
}
cothread_t co_active() {
if(!co_active_handle) co_active_handle = &co_active_buffer;
return co_active_handle;
@ -140,22 +144,9 @@ cothread_t co_derive(void* memory, unsigned int size, void (*entrypoint)(void))
}
cothread_t co_create(unsigned int size, void (*entrypoint)(void)) {
cothread_t handle;
if(!co_swap) {
co_init();
co_swap = (void (*)(cothread_t, cothread_t))co_swap_function;
}
if(!co_active_handle) co_active_handle = &co_active_buffer;
if(handle = (cothread_t)malloc(size)) {
unsigned int offset = (size & ~15) - 32;
long long *p = (long long*)((char*)handle + offset); /* seek to top of stack */
*--p = (long long)crash; /* crash if entrypoint returns */
*--p = (long long)entrypoint; /* start of function */
*(long long*)handle = (long long)p; /* stack pointer */
}
return handle;
void* memory = malloc(size);
if(!memory) return (cothread_t)0;
return co_derive(memory, size, entrypoint);
}
void co_delete(cothread_t handle) {

26
arm.c
View File

@ -35,6 +35,10 @@ static void co_init() {
#endif
}
const char* co_method() {
return "arm";
}
cothread_t co_active() {
if(!co_active_handle) co_active_handle = &co_active_buffer;
return co_active_handle;
@ -49,7 +53,8 @@ cothread_t co_derive(void* memory, unsigned int size, void (*entrypoint)(void))
if(!co_active_handle) co_active_handle = &co_active_buffer;
if(handle = (unsigned long*)memory) {
unsigned long* p = (unsigned long*)((unsigned char*)handle + size);
unsigned int offset = (size & ~15);
unsigned long* p = (unsigned long*)((unsigned char*)handle + offset);
handle[8] = (unsigned long)p;
handle[9] = (unsigned long)entrypoint;
}
@ -58,22 +63,9 @@ cothread_t co_derive(void* memory, unsigned int size, void (*entrypoint)(void))
}
cothread_t co_create(unsigned int size, void (*entrypoint)(void)) {
unsigned long* handle;
if(!co_swap) {
co_init();
co_swap = (void (*)(cothread_t, cothread_t))co_swap_function;
}
if(!co_active_handle) co_active_handle = &co_active_buffer;
size += 256;
size &= ~15;
if(handle = (unsigned long*)malloc(size)) {
unsigned long* p = (unsigned long*)((unsigned char*)handle + size);
handle[8] = (unsigned long)p;
handle[9] = (unsigned long)entrypoint;
}
return handle;
void* memory = malloc(size);
if(!memory) return (cothread_t)0;
return co_derive(memory, size, entrypoint);
}
void co_delete(cothread_t handle) {

View File

@ -16,6 +16,10 @@ static void __stdcall co_thunk(void* coentry) {
((void (*)(void))coentry)();
}
const char* co_method() {
return "fiber";
}
cothread_t co_active() {
if(!co_active_) {
ConvertThreadToFiber(0);

View File

@ -1,7 +1,7 @@
#if defined(__clang__)
#pragma clang diagnostic ignored "-Wparentheses"
//placing code in section(text) does not mark it executable with Clang.
/* placing code in section(text) does not mark it executable with Clang. */
#undef LIBCO_MPROTECT
#define LIBCO_MPROTECT
#endif

View File

@ -1,5 +1,5 @@
/*
libco v20 (2019-10-14)
libco v20 (2019-10-16)
author: byuu
license: ISC
*/
@ -13,6 +13,7 @@ extern "C" {
typedef void* cothread_t;
const char* co_method();
cothread_t co_active();
cothread_t co_derive(void*, unsigned int, void (*)(void));
cothread_t co_create(unsigned int, void (*)(void));

4
ppc.c
View File

@ -413,6 +413,10 @@ static void co_init_(void) {
co_active_handle = co_create_(state_size, (uintptr_t)&co_switch);
}
const char* co_method() {
return "ppc";
}
cothread_t co_active() {
if(!co_active_handle) co_init_();

View File

@ -221,6 +221,10 @@ __asm__(
".size swap_context, .-swap_context\n"
);
const char* co_method() {
return "ppc64v2";
}
cothread_t co_active() {
if(!co_active_handle) {
co_active_handle = (struct ppc64_context*)malloc(MIN_STACK + sizeof(struct ppc64_context));
@ -255,11 +259,9 @@ cothread_t co_derive(void* memory, unsigned int size, void (*coentry)(void)) {
}
cothread_t co_create(unsigned int size, void (*coentry)(void)) {
size_t total = MAX(size, MIN_STACK) + sizeof(struct ppc64_context);
void* memory = malloc(total);
void* memory = malloc(size);
if(!memory) return (cothread_t)0;
return co_derive(memory, total, coentry);
return co_derive(memory, size, coentry);
}
void co_delete(cothread_t handle) {

4
sjlj.c
View File

@ -33,6 +33,10 @@ static void springboard(int ignored) {
}
}
const char* co_method() {
return "sjlj";
}
cothread_t co_active() {
if(!co_running) co_running = &co_primary;
return (cothread_t)co_running;

View File

@ -26,6 +26,10 @@ extern "C" {
static thread_local ucontext_t co_primary;
static thread_local ucontext_t* co_running = 0;
const char* co_module() {
return "ucontext";
}
cothread_t co_active() {
if(!co_running) co_running = &co_primary;
return (cothread_t)co_running;

23
x86.c
View File

@ -69,6 +69,10 @@ static void crash() {
assert(0); /* called only if cothread_t entrypoint returns */
}
const char* co_method() {
return "x86";
}
cothread_t co_active() {
if(!co_active_handle) co_active_handle = &co_active_buffer;
return co_active_handle;
@ -94,22 +98,9 @@ cothread_t co_derive(void* memory, unsigned int size, void (*entrypoint)(void))
}
cothread_t co_create(unsigned int size, void (*entrypoint)(void)) {
cothread_t handle;
if(!co_swap) {
co_init();
co_swap = (void (fastcall*)(cothread_t, cothread_t))co_swap_function;
}
if(!co_active_handle) co_active_handle = &co_active_buffer;
if(handle = (cothread_t)malloc(size)) {
unsigned int offset = (size & ~15) - 32;
long *p = (long*)((char*)handle + offset); /* seek to top of stack */
*--p = (long)crash; /* crash if entrypoint returns */
*--p = (long)entrypoint; /* start of function */
*(long*)handle = (long)p; /* stack pointer */
}
return handle;
void* memory = malloc(size);
if(!memory) return (cothread_t)0;
return co_derive(memory, size, entrypoint);
}
void co_delete(cothread_t handle) {