Previous: , Up: objects   [Index]


13.27 Notes on calling Scheme code from C

While writing C language code interfacing with Scheme, if we call a C language function which calls back a Scheme function we have to save and restore the current system continuation:

ikpcb_t * pcb = the_pcb;

ik_enter_c_function(pcb);
{
  /* place here C code that calls Scheme code */
}
ik_leave_c_function(pcb);

else, upon returning from the Scheme code, the execution flow will go to the wrong continuation and undefined behaviour ensues.

Function: void ik_enter_c_function (ikpcb_t * pcb)

Save the current Scheme continuation followed by the current system continuation. A call to this function must be matched by a call to ik_leave_c_function().

Function: void ik_leave_c_function (ikpcb_t * pcb)

Restore the previously saved Scheme continuation. A call to this function must be matched by a call to ik_enter_c_function().

Notice that when Scheme calls C then C calls Scheme: garbage collections can happen in the nested Scheme code, so the C code cannot rely on the Scheme values to keep their location. Also, nested C code invocations can happen, so the C code calling out to Scheme cannot use the pcb->root fields to preserve Scheme values.