Why calls to API functions that could cause the co-routine to block can only be made from the co-routine function itself - not from within a function called by the co-routine

Another consequence of sharing a stack is that calls to API functions that could cause the co-routine to block can only be made from the co-routine function itself - not from within a function called by the co-routine.

why is that?

The base reason is that blocking for a co-routine is implemented by having the MACRO for the operation return from the co-routine, giving a value that the co-routine is to be called with to resume it. This is also why you can’t use local (stack) variables in a co-routine across blocking call boundries.

Co-routines use macro tricks like this to allow the routines to be able to switch between them without the cost of a stack for each of them.

Unless that stack cost is really an issue, it is much better to use real tasks instead of co-routines to get around these issues.

1 Like