60 likes | 201 Views
Context and Thread. Chien -Chung Shen CIS, UD cshen@cis.udel.edu. Context. context type ucontext_t is defined in ucontext.h system header file typedef struct ucontext { struct ucontext * uc_link ; // resumed context sigset_t uc_sigmask ; // blocked signals
E N D
Context and Thread Chien-Chung Shen CIS, UD cshen@cis.udel.edu
Context • context type ucontext_tis defined in ucontext.h system header file typedefstructucontext { structucontext *uc_link; // resumed context sigset_tuc_sigmask; // blocked signals stack_tuc_stack; // stack mcontext_tuc_mcontext; // CPU registers } ucontext_t; • uc_mcontext stores execution state (CPU registers and flags, program counter, stack pointer) • uc_link determines the context to be resumed when this context returns • $ man ucontext
Get & Set Context • getcontext(ucontext_t *ucp)saves current context into ucp • setcontext(constucontext_t *ucp)restores context from ucp • successful call to setcontext()shall not return • program execution resumes at the point specified by ucp(transfer control to context in ucp) • $man getcontext • programloop.c
Make Context • makecontext(ucontext_t *ucp, void (*func)(), intargc, ...) • modifiesthe context specified by ucp, which has been initialized by getcontext() • when this context is resumed using swapcontext() or setcontext(),program execution continues by calling func, passing it the arguments that follow argcin the makecontext()call • before calling makecontext(), the context being modified should have a stack allocated • uc_link determines the context resumed when the context being modified by makecontext()returns • $ man makecontextand program ctx.c
Set Context • setcontext(constucontext_t *ucp)restores context pointed at by ucp • ucpshould have been obtained by a call of (1) getcontext()or (2) makecontext() • (1) program execution continues as if this getcontext()just returned • (2) program execution continues by a call to function funcspecified as the 2nd argument of that call to makecontext(); when funcreturns, we continue with the uc_link member of ucp specified as the 1st argument of that call to makecontext(); when this uc_link member is NULL, the thread exits
Swap Context • swapcontext(ucontext_t *restrict oucp, constucontext_t *restrict ucp)saves current context in oucpand set (restore/resume) context in ucp • When successful, swapcontext()does not return