Swaps the context of the calling thread.
#include <nks/thread.h> int NXThreadSwapContext ( NXContext_t newctx, NXContext_t *prevctx);
(IN) Specifies the new context to be bound to the calling thread. The context must be of type NX_CTX_NORMAL (see NXContextAlloc). The context must be “idle” and cannot be currently hosted by a thread
(OUT) Points to the previously bound context.
If successful, this function does not return. On failure, returns a nonzero error code:
Only application threads that were created by calling NXThreadCreate without the NX_THR_BIND_CTX flag are allowed to call NXThreadSwapContext. Daemon threads (even if created with NX_THE_DONT_DESTROY) or anonymous threads cannot call NXThreadSwapContext.
NXThreadSwapContext atomically switches the context bound to the calling thread with the new context by doing the following:
Stores the “original” context the thread was hosting at the time of the call and sets prevctx to point to that context.
Binds the calling thread to the context specified by newctx
If the thread subsequently calls NXThreadSwapContext and passes the original context as newctx, program execution continues as if the function returned in the original context.
NXThreadSwapContext can be called to multiplex contexts on top of threads to achieve a co-routine style of programming. An application could then use this functionality to construct a two-level scheduling scheme where threads are scheduled by the virtual machine scheduler and contexts are scheduled on top of threads by the application.
For sample code, see CtxSwap.c.