Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add an ability to execute a callback function on behalf of target coroutine stack right before return to target coroutine #278

Open
microcai opened this issue Dec 29, 2024 · 3 comments

Comments

@microcai
Copy link

rational

when do "jump_fcontext", we have an opportunity to pass "void*" to target coroutine. how to do with that argument is up to the target coroutine.

some time we do want to do some 'clean up job' right before return to the target coroutine. because there exist some task that must not be executed on current stack. for example , free current stack memory.

and for muti-threaded environment, we do need a way to distinguish where a context-switch is finished.

by changing jump_fcontext to transfer_t jump_fcontext( fcontext_t const to, void * vp, transfer_t (* fn)( fcontext_t from, void*));
we can now:

  • pass a cleanup function to clean stack right after stack pointer got swapped.
  • notify the scheduler that "from coroutine context" is now safe to be used as "to coroutine".
  • record "from" that does not relay on jumpee coroutine to do the job. that makes code cleaner and easy to understand.
  • doing things before return to target is a bit faster. because the registers are not restored and then saved-again by "the code that called when jump_context returns" with is in fact doing things for previous coroutine.

changes to the assembly code

right after loading RSP from "to".
add this lines

    test %rdx, %rdx  // test for 3th argument, aka callback_fn
    je skip_call
    mov %rax, %rsi  // pass from:fctx argument to callback_fn as first argument, %rdi is already the same argument
    call * %rdx // call callback_fn, and use its result as  return , do not touch %rax:%rdx pair.
skip_call:
    // original code that retore  registers
@olk
Copy link
Member

olk commented Jan 1, 2025

executing a function before resuming a coroutine is done via ontop_fcontext() - wouldn't this satisfy your needs?

@microcai
Copy link
Author

microcai commented Jan 2, 2025

executing a function before resuming a coroutine is done via ontop_fcontext() - wouldn't this satisfy your needs?

didn't know ontop_fcontext ;)

it suits my needs !

thanks a lot.

@microcai
Copy link
Author

microcai commented Jan 2, 2025

executing a function before resuming a coroutine is done via ontop_fcontext() - wouldn't this satisfy your needs?

the API satisfy my needs. but when I look into the implementation, no.
the "ontop_fcontext" invoke fn after it "restored" the context. which is then clobbered and callee saved by fn.
that hurt performance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants