2

Please explain

  1. What happened with a function getting suspended
  2. where suspended function go during the suspension
  3. How the communication happens to resume the suspended function
  4. Who is responsible to take care of the suspend function mechanism

Thank you

Ashish Dwivedi
  • 8,048
  • 5
  • 58
  • 78

1 Answers1

3

This is rather hard to explain fully in just a few sentences, but it works like this:

  1. Before the code suspends, continuation is created. Continuation is an object that knows how to resume the code from the point where it stopped. It keeps a reference to the current function, to the previous functions on the call stack (technically: to previous continuations), it stores local variables, code offset/location inside the function, etc.
  2. Continuation is passed to the component that controls when to resume - continuation is stored there. For example, when we invoke delay() then delay() gets our continuation and keeps it for later.
  3. Execution returns through the whole call stack, making the thread free to do something else.
  4. When conditions for resuming are met, continuation is scheduled on dispatcher to be executed.
  5. Function where we previously suspended is invoked again, local variables are restored from the continuation and the execution jumps to the offset where it stopped. This effectively resumes a coroutine.
broot
  • 21,588
  • 3
  • 30
  • 35