Skip to content

Commit

Permalink
coroutines: manually create photon vcpu's & add functions to set phot…
Browse files Browse the repository at this point in the history
…on log output (#21012)
  • Loading branch information
joe-conigliaro committed Mar 13, 2024
1 parent 54da256 commit f315676
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 33 deletions.
10 changes: 4 additions & 6 deletions thirdparty/photon/photonwrapper.h
Expand Up @@ -29,12 +29,6 @@ photon::WorkPool* work_pool;
#else
#endif

// using namespace photon;
// typedef WorkPool PhotonWorkPool;
// typedef photon::WorkPool PhotonWorkPool;
// typedef WorkPool PhotonWorkPool;
// typedef PhotonWorkPool1 PhotonWorkPool;
// PhotonWorkPool* new_photon_work_pool();
void* new_photon_work_pool(size_t);
// void delete_photon_work_pool(void*);
void delete_photon_work_pool();
Expand All @@ -44,6 +38,10 @@ void init_photon_work_pool(size_t);
// void photon_thread_migrate(void*);
void photon_thread_create_and_migrate_to_work_pool(void* (* f)(void*), void* arg);
// void photon_thread_create_and_migrate_to_work_pool(void*, void* (* f)(void*), void* arg);
int photon_join_current_thread_into_workpool();
void photon_set_log_output_stdout();
void photon_set_log_output_stderr();
void photon_set_log_output_null();
// direct wrappers to photon functions
int photon_init_default();
void photon_thread_create(void* (* f)(void*), void* arg);
Expand Down
3 changes: 2 additions & 1 deletion vlib/builtin/builtin_d_gcboehm.c.v
Expand Up @@ -185,7 +185,8 @@ fn C.GC_register_my_thread(voidptr) int
fn C.GC_unregister_my_thread() int

// fn C.GC_get_my_stackbottom(voidptr) voidptr
// fn C.GC_set_stackbottom(voidptr, voidptr)
fn C.GC_set_stackbottom(voidptr, voidptr)

// fn C.GC_push_all_stacks()

fn C.GC_add_roots(voidptr, voidptr)
Expand Down
62 changes: 36 additions & 26 deletions vlib/coroutines/coroutines.c.v
Expand Up @@ -25,6 +25,10 @@ fn C.set_photon_thread_stack_allocator(fn (voidptr, int) voidptr, fn (voidptr, v
fn C.new_photon_work_pool(int) voidptr
fn C.delete_photon_work_pool()
fn C.init_photon_work_pool(int)
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() int
fn C.photon_thread_create_and_migrate_to_work_pool(f voidptr, arg voidptr)
fn C.photon_thread_create(f voidptr, arg voidptr)
fn C.photon_thread_migrate()
Expand All @@ -43,25 +47,15 @@ pub fn sleep(duration time.Duration) {

fn alloc(_ voidptr, stack_size int) voidptr {
unsafe {
$if gcboehm ? {
// TODO: do this only once when the worker thread is created.
// I'm currently just doing it here for convenience.
// The second time `C.GC_register_my_thread` gets called from the
// same thread it will just fail (returning non 0), so it't not
// really a problem, it's just not ideal.
mut sb := C.GC_stack_base{}
C.GC_get_stack_base(&sb)
C.GC_register_my_thread(&sb)
}

stack_ptr := malloc(stack_size)

$if gcboehm ? {
// TODO: this wont work if a corouroutine gets moved to a different
// 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)
Expand All @@ -74,32 +68,48 @@ fn alloc(_ voidptr, stack_size int) voidptr {
fn dealloc(_ voidptr, stack_ptr voidptr, stack_size int) {
unsafe {
$if gcboehm ? {
// TODO: only do this once when the worker thread is killed (not in each coroutine)
// come up with a solution for this and `C.GC_register_my_thread` (see alloc above)
// C.GC_unregister_my_thread()

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() {
// 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')
$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()

// NOTE: instead of using photon's WorkPool, we could start our own worker threads,
// then initialize photon in each of them. If we did that we would need to control
// the migration of coroutines across threads etc.
if util.nr_jobs >= 1 {
C.init_photon_work_pool(util.nr_jobs)
// 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 {
Expand Down

0 comments on commit f315676

Please sign in to comment.