Initial project import
This commit is contained in:
commit
23f2d29e32
11957 files changed
+1754577
No files matched your search
@@ -0,0 +1,3 @@
|
||||
## Description
|
||||
|
||||
`coroutines` is a namespace for a wrapper around third party/photon.
|
||||
@@ -0,0 +1,118 @@
|
||||
// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
|
||||
// Use of this source code is governed by an MIT license
|
||||
// that can be found in the LICENSE file.
|
||||
module coroutines
|
||||
|
||||
import v.util
|
||||
import time
|
||||
|
||||
#flag -I @VEXEROOT/thirdparty/photon
|
||||
#flag @VEXEROOT/thirdparty/photon/photonwrapper.so
|
||||
#include "photonwrapper.h"
|
||||
|
||||
$if windows {
|
||||
#include "processthreadsapi.h"
|
||||
} $else {
|
||||
#include <pthread.h>
|
||||
}
|
||||
#flag -I @VEXEROOT/vlib/coroutines
|
||||
#include "sp_corrector.c"
|
||||
|
||||
fn C.set_photon_thread_stack_allocator(fn (voidptr, int) voidptr, fn (voidptr, voidptr, int))
|
||||
|
||||
// fn C.default_photon_thread_stack_alloc(voidptr, int) voidptr
|
||||
// fn C.default_photon_thread_stack_dealloc(voidptr, voidptr, int)
|
||||
fn C.new_photon_work_pool(i32) voidptr
|
||||
fn C.delete_photon_work_pool()
|
||||
fn C.init_photon_work_pool(i32)
|
||||
fn C.photon_set_log_output_stdout()
|
||||
fn C.photon_set_log_output_stderr()
|
||||
fn C.photon_set_log_output_null()
|
||||
fn C.photon_join_current_thread_into_workpool() i32
|
||||
fn C.photon_thread_create_and_migrate_to_work_pool(f voidptr, arg voidptr)
|
||||
fn C.photon_thread_create(f voidptr, arg voidptr, stack_size u64)
|
||||
fn C.photon_thread_migrate()
|
||||
|
||||
// fn C.photon_thread_migrate(work_pool voidptr)
|
||||
fn C.photon_init_default() i32
|
||||
fn C.photon_sleep_s(n i32)
|
||||
fn C.photon_sleep_ms(n i32)
|
||||
|
||||
fn C.sp_corrector(voidptr, voidptr)
|
||||
|
||||
// sleep is coroutine-safe version of time.sleep()
|
||||
pub fn sleep(duration time.Duration) {
|
||||
C.photon_sleep_ms(duration.milliseconds())
|
||||
}
|
||||
|
||||
fn alloc(_ voidptr, stack_size int) voidptr {
|
||||
unsafe {
|
||||
stack_ptr := malloc(stack_size)
|
||||
|
||||
$if gcboehm ? {
|
||||
// TODO: this wont work if a coroutine gets moved to a different
|
||||
// thread, so we are using `C.GC_set_sp_corrector` with our own
|
||||
// corrector function which seems to be the best solution for now.
|
||||
// It would probably be more performant if we could hook into photon's context
|
||||
// switching code (currently not possible) or we write our own implementation.
|
||||
// TODO: update - I'm not sure if what I wrote above is correct. test
|
||||
// C.GC_set_stackbottom(0, stack_ptr)
|
||||
|
||||
C.GC_add_roots(stack_ptr, charptr(stack_ptr) + stack_size)
|
||||
}
|
||||
|
||||
return stack_ptr
|
||||
}
|
||||
}
|
||||
|
||||
fn dealloc(_ voidptr, stack_ptr voidptr, stack_size int) {
|
||||
unsafe {
|
||||
$if gcboehm ? {
|
||||
C.GC_remove_roots(stack_ptr, charptr(stack_ptr) + stack_size)
|
||||
}
|
||||
free(stack_ptr)
|
||||
}
|
||||
}
|
||||
|
||||
fn init_photon_vcpu() {
|
||||
C.photon_init_default()
|
||||
$if gcboehm ? {
|
||||
mut sb := C.GC_stack_base{}
|
||||
C.GC_get_stack_base(&sb)
|
||||
C.GC_register_my_thread(&sb)
|
||||
}
|
||||
C.photon_join_current_thread_into_workpool()
|
||||
$if gcboehm ? {
|
||||
C.GC_unregister_my_thread()
|
||||
}
|
||||
}
|
||||
|
||||
fn init() {
|
||||
$if gcboehm ? {
|
||||
// NOTE `sp_corrector` only works for platforms with the stack growing down
|
||||
// MacOs, Win32 and Linux always have stack growing down.
|
||||
// A proper solution is planned (hopefully) for boehm v8.4.0.
|
||||
C.GC_set_sp_corrector(C.sp_corrector)
|
||||
if C.GC_get_sp_corrector() == unsafe { nil } {
|
||||
panic('stack pointer correction unsupported')
|
||||
}
|
||||
}
|
||||
C.photon_set_log_output_null()
|
||||
C.set_photon_thread_stack_allocator(alloc, dealloc)
|
||||
ret := C.photon_init_default()
|
||||
|
||||
if util.nr_jobs >= 1 {
|
||||
// automatic
|
||||
// C.init_photon_work_pool(util.nr_jobs)
|
||||
// manual - pass 0 because we will start our own
|
||||
C.init_photon_work_pool(0)
|
||||
// start our own vcpu's manually
|
||||
for _ in 1 .. util.nr_jobs {
|
||||
spawn init_photon_vcpu()
|
||||
}
|
||||
}
|
||||
|
||||
if ret < 0 {
|
||||
panic_n('failed to initialize coroutines via photon ret:', ret)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
// TODO: convert this to v code? handle all platforms
|
||||
// currently if it's not macos or win it defaults to the linux impl
|
||||
|
||||
static void sp_corrector(void** sp_ptr, void* tid) {
|
||||
size_t stack_size;
|
||||
char* stack_addr;
|
||||
#ifdef __APPLE__
|
||||
stack_size = pthread_get_stacksize_np((pthread_t)tid);
|
||||
stack_addr = (char*) pthread_get_stackaddr_np((pthread_t)tid);
|
||||
#elif defined(_WIN64)
|
||||
ULONG_PTR stack_low, stack_high;
|
||||
GetCurrentThreadStackLimits(&stack_low, &stack_high);
|
||||
stack_size = stack_high - stack_low;
|
||||
stack_addr = (char*)stack_low;
|
||||
// #elif defined(__linux__)
|
||||
// pthread_attr_t gattr;
|
||||
// pthread_getattr_np((pthread_t)tid, &gattr);
|
||||
// pthread_attr_getstack(&gattr, (void**)&stack_addr, &stack_size);
|
||||
// pthread_attr_destroy(&gattr);
|
||||
// #else
|
||||
// assert("unsupported platform");
|
||||
#else
|
||||
pthread_attr_t gattr;
|
||||
pthread_getattr_np((pthread_t)tid, &gattr);
|
||||
pthread_attr_getstack(&gattr, (void**)&stack_addr, &stack_size);
|
||||
pthread_attr_destroy(&gattr);
|
||||
#endif
|
||||
char *sp = (char*)*sp_ptr;
|
||||
if(sp <= stack_addr || sp >= stack_addr+stack_size) {
|
||||
*sp_ptr = (void*)stack_addr;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user