1 #include "threadimpl.h"
2
3 void
makecontext(ucontext_t * uc,void (* fn)(void),int argc,...)4 makecontext(ucontext_t *uc, void (*fn)(void), int argc, ...)
5 {
6 uintptr *sp;
7 va_list arg;
8
9 if(argc != 2)
10 sysfatal("libthread: makecontext misused");
11 va_start(arg, argc);
12 uc->mc.di = va_arg(arg, uint);
13 uc->mc.si = va_arg(arg, uint);
14 va_end(arg);
15
16 sp = USPALIGN(uc, 16);
17 *--sp = 0; // fn's return address
18 *--sp = (uintptr)fn; // return address of setcontext
19 uc->mc.sp = (uintptr)sp;
20 }
21
22 int
swapcontext(ucontext_t * oucp,ucontext_t * ucp)23 swapcontext(ucontext_t *oucp, ucontext_t *ucp)
24 {
25 if(getcontext(oucp) == 0)
26 setcontext(ucp);
27 return 0;
28 }
29