📄 rse-pmt.ps
字号:
Fx(\(3\))e(calls)k(\(if)208 282 y(a)n(v)n(ailable\))30b(or)h(\(if)g(not)g(a)n(v)n(ailable\))f(emulate)h(the)g(miss-)208382 y(ing)d(functionality)g(manually)f(with)j(additional)eFp(sig-)208 482 y(procmask)p Fx(\(2\))j(calls)i(\(see)hFp(pth)p 1245 482 25 4 v 29 w(mctx.c)f Fx(in)g Fr(GNU)208581 y(Pth)20 b Fx([25)n(]\).)p Black 95 739 50 50 v Black208 739 a(There)37 b(is)i(the)f(function)e Fp(sigaltstack)pFx(\(2\))f(which)208 839 y(conforms)f(to)j(the)g Fr(Single)f(Unix)h(Speci\002cation)p Fx(,)h(V)-9 b(er)n(-)208 938 y(sion)34b(2)g(\()p Fr(SUSv2)f Fx([20)n(],)38 b(aka)c Fr(Unix95/98)pFx(\))e(and)i(its)h(an-)208 1038 y(cestor)25 b(function)fFp(sigstack)p Fx(\(2\))f(from)h Fr(4.2BSD)p Fx(.)g(The)2081137 y(last)d(one)e(e)o(xists)i(only)e(on)h Fr(BSD)pFx(-deri)n(v)o(ed)d(platforms,)i(b)n(ut)208 1237 y(the)g(\002rst)h(function)e(already)h(e)o(xists)h(on)f(all)h(current)e(Unix)2081337 y(platforms.)0 1571 y FA(2.3)99 b(Maximum)25 b(P)n(ortability)f(Solution)0 1726 y Fx(The)44 b(maximum)e(portable)h(solution)g(ob)o(viously)f(is)j(to)f(use)0 1826 y(the)j(standardized)fFp(makecontext)p Fx(\(3\))e(function)i(to)h(cre-)0 1926y(ate)k(threads)f(and)g Fp(switchcontext)p Fx(\(3\))e(or)iFp(getcon-)0 2025 y(text)p Fx(\(3\)/)p Fp(setcontext)pFx(\(3\))18 b(to)23 b(dispatch)e(them.)31 b(And)22 b(actu-)02125 y(ally)h(these)f(are)h(the)f(preferred)f(functions)g(modern)f(user)n(-space)0 2224 y(multithreading)25 b(systems)k(are)e(using.)48b(W)-7 b(e)29 b(could)e(easily)h(im-)0 2324 y(plement)20b(our)g(proposed)f(API)i(as)g(follo)n(wing)e(\(all)i(error)f(checks)02424 y(omitted)f(for)h(better)g(readability\):)p BlackBlack 0 2561 a Fk(/*)63 b Fj(mac)o(hine)19 b(context)g(data)g(structure)44 b Fk(*/)0 2635 y Fi(typedef)g(struct)fFk(mctx_st)h Fo(f)179 2710 y Fk(ucontext_t)f(uc;)0 2785y Fo(g)i Fk(mctx_t;)0 2934 y(/*)63 b Fj(sav)o(e)20 b(mac)o(hine)e(context)45 b Fk(*/)0 3009 y Fi(#define)f Fk(mctx_save\(mctx\))eFo(n)179 3084 y Fk(\()p Fi(void)p Fk(\)getcontext\(&\(mctx\)->uc\))03233 y(/*)63 b Fj(restore)20 b(mac)o(hine)e(context)45b Fk(*/)0 3308 y Fi(#define)f Fk(mctx_restore\(mctx\))dFo(n)179 3383 y Fk(\()p Fi(void)p Fk(\)setcontext\(&\(mctx\)->uc\))03532 y(/*)63 b Fj(switc)o(h)18 b(mac)o(hine)h(context)45b Fk(*/)0 3607 y Fi(#define)f Fk(mctx_switch\(mctx_old,mctx_new\))39b Fo(n)179 3681 y Fk(\()p Fi(void)p Fk(\)swapcontext\(&\(\(mctx_old\)->uc\),)g Fo(n)986 3756y Fk(&\(\(mctx_new\)->uc\)\))0 3906 y(/*)63 b Fj(create)20b(mac)o(hine)e(context)45 b Fk(*/)0 3980 y Fi(void)fFk(mctx_create\()179 4055 y(mctx_t)g(*mctx,)179 4130y Fi(void)g Fk(\(*sf_addr\)\()p Fi(void)f Fk(*\),)h Fi(void)gFk(*sf_arg,)179 4204 y Fi(void)g Fk(*sk_addr,)g(size_t)g(sk_size\))04279 y Fo(f)179 4429 y Fk(/*)64 b Fj(f)o(etc)o(h)18 b(current)g(context)45 b Fk(*/)179 4503 y(getcontext\(&\(mctx->uc\)\);)1794653 y(/*)64 b Fj(adjust)18 b(to)h(new)f(context)45 bFk(*/)179 4728 y(mctx->uc.uc_link)491 b(=)44 b(NULL;)1794802 y(mctx->uc.uc_stack.ss_sp)176 b(=)44 b(sk_addr;)1794877 y(mctx->uc.uc_stack.ss_size)86 b(=)44 b(sk_size;)1794952 y(mctx->uc.uc_stack.ss_flags)d(=)j(0;)179 5101 y(/*)64b Fj(mak)n(e)19 b(new)f(context)45 b Fk(*/)179 5176 y(makecontext\(&\(mctx->uc\),)717 5251 y(sf_addr,)f(1,)g(sf_arg\);)1795325 y Fi(return)p Fk(;)0 5400 y Fo(g)2025 83 y Fx(Unfortunately)14b(there)j(are)g(still)h(lots)g(of)f(Unix)f(platforms)g(where)2025183 y(this)43 b(approach)d(cannot)h(be)h(used,)47 b(because)42b(the)g(standard-)2025 282 y(ized)e Fp(ucontext)p Fx(\(3\))d(API)j(is)g(not)g(pro)o(vided)d(by)i(the)h(v)o(en-)2025 382 y(dor)-5b(.)80 b(Actually)38 b(the)h(platform)e(test)i(results)g(for)fFr(GNU)i(Pth)2025 482 y Fx(\(see)29 b(T)-7 b(able)29b(2)g(belo)n(w\))f(sho)n(wed)g(that)h(only)f(7)h(of)g(21)f(success-)2025 581 y(fully)e(tested)g(Unix)g(\003a)n(v)n(ors)g(pro)o(vided)e(the)i(standardized)e(API)2025 681 y(\()p Fp(makecontext)pFx(\(3\),)19 b Fr(etc.)p Fx(\).)31 b(On)22 b(all)h(other)f(platforms,)fFr(GNU)2025 780 y(Pth)31 b Fx(w)o(as)h(forced)e(to)h(use)h(the)f(f)o(allback)f(approach)f(of)i(imple-)2025 880 y(menting)25b(the)h(machine)f(conte)o(xt)f(as)j(we)f(will)h(describe)e(in)i(the)2025 980 y(follo)n(wing.)44 b(Ob)o(viously)25 b(this)j(f)o(allback)e(approach)f(has)i(to)g(use)2025 1079 y(the)36 b(remaining)f(technical)g(possibilities)h(\()p Fp(sigsetjmp)p Fx(\(3\),)2025 1179y Fr(etc.)p Fx(\).)p 2025 1271 1899 4 v 2025 3174 4 1904v 2103 1351 a Fn(Operating)18 b(System)p 2837 1374 479 v 277 w(Ar)o(chitectur)o(e\(s\))p 3450 1374 V 188w(mcsc)p 3694 1374 V 104 w(sjlj)2103 1430 y Fm(FreeBSD)g(2.x/3.x)p2837 1453 V 339 w(Intel)p 3450 1453 V 494 w(no)p 36941453 V 177 w(yes)2103 1508 y(FreeBSD)g(3.x)p 2837 1532V 440 w(Intel,)g(Alpha)p 3450 1532 V 298 w(no)p 36941532 V 177 w(yes)2103 1587 y(NetBSD)g(1.3/1.4)p 28371611 V 361 w(Intel,)g(PPC,)e(M68K)p 3450 1611 V 135 w(no)p3694 1611 V 177 w(yes)2103 1666 y(OpenBSD)i(2.5/2.6)p2837 1690 V 313 w(Intel,)g(SP)-6 b(ARC)p 3450 1690 V255 w(no)p 3694 1690 V 177 w(yes)2103 1745 y(BSDI)17b(4.0)p 2837 1769 V 536 w(Intel)p 3450 1769 V 494 w(no)p3694 1769 V 177 w(yes)2103 1824 y(Linux)g(2.0.x)g(glibc)h(1.x/2.0)p2837 1848 V 129 w(Intel,)g(SP)-6 b(ARC,)16 b(PPC)p 34501848 V 104 w(no)p 3694 1848 V 177 w(yes)2103 1903 y(Linux)h(2.2.x)g(glibc)h(2.0/2.1)p 2837 1926 V 129 w(Intel,)g(Alpha,)g(ARM)p3450 1926 V 112 w(no)p 3694 1926 V 177 w(yes)2103 1982y(Sun)f(SunOS)g(4.1.x)p 2837 2005 V 329 w(SP)-6 b(ARC)p3450 2005 V 410 w(no)p 3694 2005 V 177 w(yes)2103 2060y(Sun)17 b(Solaris)h(2.5/2.6/2.7)p 2837 2084 V 181 w(SP)-6b(ARC)p 3450 2084 V 410 w(yes)p 3694 2084 V 155 w(yes)21032139 y(SCO)17 b(UnixW)-5 b(are)18 b(2.x/7.x)p 2837 2163V 169 w(Intel)p 3450 2163 V 494 w(yes)p 3694 2163 V 155w(yes)2103 2218 y(SCO)f(OpenServ)o(er)i(5.0.x)p 28372242 V 175 w(Intel)p 3450 2242 V 494 w(no)p 3694 2242V 177 w(yes)2103 2297 y(IBM)e(AIX)g(4.1/4.2/4.3)p 28372321 V 225 w(RS6000,)g(PPC)p 3450 2321 V 249 w(yes)p3694 2321 V 155 w(yes)2103 2376 y(HP)g(HPUX)f(9.10/10.20)p2837 2400 V 205 w(HPP)-6 b(A)p 3450 2400 V 450 w(no)p3694 2400 V 177 w(yes)2103 2455 y(HP)17 b(HPUX)f(11.0)p2837 2479 V 372 w(HPP)-6 b(A)p 3450 2479 V 450 w(yes)p3694 2479 V 155 w(yes)2103 2534 y(SGI)17 b(IRIX)g(5.3)p2837 2557 V 427 w(MIPS)g(32/64)p 3450 2557 V 292 w(no)p3694 2557 V 177 w(yes)2103 2613 y(SGI)g(IRIX)g(6.2/6.5)p2837 2636 V 326 w(MIPS)g(32/64)p 3450 2636 V 292 w(yes)p3694 2636 V 155 w(yes)2103 2691 y(ISC)g(4.0)p 2837 2715V 584 w(Intel)p 3450 2715 V 494 w(no)p 3694 2715 V 177w(yes)2103 2770 y(Apple)h(MacOS)f(X)p 2837 2794 V 341w(PPC)p 3450 2794 V 496 w(no)p 3694 2794 V 177 w(yes)21032849 y(DEC)f(OSF1/T)n(ru64)i(4.0/5.0)p 2837 2873 V 103w(Alpha)p 3450 2873 V 453 w(yes)p 3694 2873 V 155 w(yes)21032928 y(SNI)f(ReliantUNIX)p 2837 2952 V 308 w(MIPS)p 34502952 V 459 w(yes)p 3694 2952 V 155 w(yes)2103 3007 y(AmigaOS)p2837 3031 V 522 w(M68K)p 3450 3031 V 441 w(no)p 36943031 V 177 w(yes)p 3921 3174 4 1904 v 2025 3177 18994 v 2025 3264 a Fn(T)-6 b(able)18 b(2:)67 b Fm(Summary)20b(of)h(operating)j(system)d(support.)33 b(The)20 b(le)n(v)o(el)j(and)f(type)2320 3332 y(of)h(support)h(found)f(on)g(each)i(tested)g(operating)g(system.)39 b Fh(mcsc)p Fm(:)2320 3400 y(functional)168b Fh(makecontext)p Fm(\(3\)/)p Fh(switchcontext)p Fm(\(3\),)23203466 y Fh(sjlj)p Fm(:)83 b(functional)51 b Fh(setjmp)pFm(\(3\)/)p Fh(longjmp)p Fm(\(3\))c(or)i Fh(sig-)23203531 y(setjmp)p Fm(\(3\)/)p Fh(siglongjmp)p Fm(\(3\).)25b(See)19 b(\002le)h Fh(PORTING)d Fm(in)j Fl(GNU)23203597 y(Pth)c Fm([25)q(])h(for)g(more)g(details.)20253834 y FA(2.4)99 b(Remaining)25 b(P)n(ossibilities)20253999 y Fx(Our)f(problem)e(can)i(be)g(di)n(vided)f(into)h(tw)o(o)g(parts,)h(an)f(easy)g(one)2025 4099 y(and)c(a)g(dif)n(\002cult)g(one.)2025 4264 y FA(The)26 b(Easy)f(P)o(art)2025 4399 y Fx(That)kFp(setjmp)p Fx(\(3\))e(and)h Fp(longjmp)p Fx(\(3\))f(can)i(be)g(used)g(to)g(im-)2025 4498 y(plement)20 b(user)n(-space)g(threads)g(is)i(commonly)c(kno)n(wn)h([24)o(,)i(27)o(,)2025 4598 y(28)o(].)57b(Mostly)31 b(all)g(older)g(portable)e(user)n(-space)h(threading)f(li-)2025 4697 y(braries)c(are)h(based)g(on)f(them,)i(although)d(some)h(problems)g(are)2025 4797 y(kno)n(wn)h(with)h(these)g(f)o(acilities)h(\(see)f(belo)n(w\).)45 b(So)28 b(it)g(becomes)2025 4897y(clear)g(that)f(we)h(also)g(ha)n(v)o(e)f(to)h(use)g(these)g(functions)e(and)h(base)2025 4996 y(our)h(machine)f(conte)o(xt)g(\()pFp(mctx)p 2980 4996 25 4 v 29 w(t)p Fx(\))i(on)f(their)gFp(jmp)p 3559 4996 V 29 w(buf)h Fx(data)2025 5096 y(structure.)21505201 y(W)-7 b(e)39 b(immediately)e(recognize)f(that)j(this)f(w)o(ay)h(we)f(ha)n(v)o(e)2025 5300 y(at)j(least)g(solv)o(ed)f(the)g(dispatching)f(problem,)44 b(because)39 b(our)2025 5400y Fp(mctx)p 2230 5400 V 29 w(save)p Fx(,)73 b Fp(mctx)p2753 5400 V 29 w(restore)62 b Fx(and)g Fp(mctx)p 35775400 V 29 w(switch)p Black 1929 5700 a Fx(4)p Black eop%%Page: 5 55 4 bop Black Black 0 83 a Fx(functions)32 b(can)h(be)g(easily)g(implemented)e(with)i Fp(setjmp)p Fx(\(3\))0 183 y(and)20b Fp(longjmp)p Fx(\(3\).)0 358 y FA(The)26 b(Dif\002cult)g(P)o(art)0500 y Fx(Ne)n(v)o(ertheless,)g(the)h(dif)n(\002cult)e(problem)g(of)h(ho)n(w)f(to)i(create)f(the)0 600 y(machine)k(conte)o(xt)f(remains.)56b(Ev)o(en)29 b(kno)n(wing)g(that)i(our)f(ma-)0 699 y(chine)17b(conte)o(xt)f(is)i Fp(jmp)p 687 699 25 4 v 30 w(buf)fFx(based)g(is)i(no)e(adv)n(antage)e(to)j(us.)24 b(A)0799 y Fp(jmp)p 155 799 V 29 w(buf)18 b Fx(has)f(to)g(be)g(treated)g(by)g(us)g(as)h(an)f(opaque)f(data)h(struc-)0 899 y(ture)26b(\227)h(for)e(portability)g(reasons.)42 b(The)26 b(only)g(operations)e(we)0 998 y(can)15 b(perform)f(on)h(it)h(are)f Fp(setjmp)pFx(\(3\))f(and)h Fp(longjmp)p Fx(\(3\))e(calls,)0 1098y(of)23 b(course.)32 b(Additionally)-5 b(,)21 b(we)j(are)f(forced)e(to)i(use)g Fp(sigalt-)0 1198 y(stack)p Fx(\(3\))f(for)h(our)g(stack)h(manipulations,)e(because)h(it)h(is)h(the)0 1297 y(only)19b(portable)g(function)g(which)g(actually)h(deals)g(with)h(stacks.)1251406 y(So)33 b(it)g(is)h(clear)f(that)g(our)f(implementation)f(for)hFp(mctx)p 1801 1406 V 29 w(-)0 1505 y(create)k Fx(has)h(to)g(play)f(a)i(fe)n(w)e(tricks)h(to)g(use)g(a)g Fp(jmp)p 1701 1505V 30 w(buf)0 1605 y Fx(for)44 b(passing)h(e)o(x)o(ecution)e(control)h(to)h(an)g(arbitrary)e(startup)0 1704 y(routine.)82 b(And)39b(our)g(approach)f(has)i(to)g(be)f(careful)g(to)h(en-)01804 y(sure)51 b(that)h(it)g(does)f(not)h(suf)n(fer)e(from)h(une)o(xpected)e(side-)0 1904 y(ef)n(fects.)131 b(It)56 b(should)e(be)i(also)g(ob)o(vious)d(that)j(we)g(can-)0 2003 y(not)e(again)f(e)o(xpect)g(to)i(\002nd)f(an)g(easy)g(solution)g(\(as)g(for)0 2103 yFp(mctx)p 205 2103 V 29 w(save)p Fx(,)g Fp(mctx)p 7092103 V 29 w(restore)47 b Fx(and)h Fp(mctx)p 1504 2103V 29 w(switch)p Fx(\),)0 2203 y(because)29 b Fp(setjmp)pFx(\(3\))f(and)i Fp(sigaltstack)p Fx(\(3\))d(cannot)i(be)02302 y(tri)n(vially)20 b(combined)e(to)i(form)f Fp(mctx)p1102 2302 V 29 w(create)p Fx(.)0 2630 y Fs(3)119 b(Implementation)02833 y Fx(As)24 b(we)g(ha)n(v)o(e)f(already)g(discussed,)g(our)g(implementation)e(con-)0 2932 y(tains)31 b(an)f(easy)h(part)f(\()pFp(mctx)p 869 2932 V 29 w(save)p Fx(,)i Fp(mctx)p 13512932 V 29 w(restore)e Fx(and)0 3032 y Fp(mctx)p 205 3032V 29 w(switch)p Fx(\))i(and)h(a)h(dif)n(\002cult)e(part)h(\()pFp(mctx)p 1504 3032 V 29 w(create)p Fx(\).)0 3131 y(Let)23b(us)h(start)f(with)g(the)g(easy)g(part,)g(whose)g(implementation)e(is)0 3231 y(ob)o(vious)26 b(\(all)i(error)e(checks)i(again)e(omitted)h(for)g(better)g(read-)0 3331 y(ability\):)p Black Black0 3532 a Fk(/*)63 b Fj(mac)o(hine)19 b(context)g(data)g(structure)44b Fk(*/)0 3607 y Fi(typedef)g(struct)f Fk(mctx_st)h Fo(f)1793681 y Fk(jmp_buf)g(jb;)0 3756 y Fo(g)h Fk(mctx_t;)03906 y(/*)63 b Fj(sav)o(e)20 b(mac)o(hine)e(context)45b Fk(*/)0 3980 y Fi(#define)f Fk(mctx_save\(mctx\))eFo(n)179 4055 y Fk(\()p Fi(void)p Fk(\)setjmp\(\(mctx\)->jb\))04204 y(/*)63 b Fj(restore)20 b(mac)o(hine)e(context)45b Fk(*/)0 4279 y Fi(#define)f Fk(mctx_restore\(mctx\))dFo(n)179 4354 y Fk(longjmp\(\(mctx\)->jb,)h(1\))0 4503y(/*)63 b Fj(switc)o(h)18 b(mac)o(hine)h(context)45 bFk(*/)0 4578 y Fi(#define)f Fk(mctx_switch\(mctx_old,mctx_new\))39b Fo(n)179 4653 y Fi(if)45 b Fk(\(setjmp\(\(mctx_old\)->jb\))c(==)j(0\))h Fo(n)359 4728 y Fk(longjmp\(\(mctx_new\)->jb,)c(1\))04877 y(/*)63 b Fj(create)20 b(mac)o(hine)e(context)45b Fk(*/)0 4952 y Fi(void)f Fk(mctx_create\()179 5026y(mctx_t)g(*mctx,)179 5101 y Fi(void)g Fk(\(*sf_addr\)\()pFi(void)f Fk(*\),)h Fi(void)g Fk(*sf_arg,)179 5176 yFi(void)g Fk(*sk_addr,)g(size_t)g(sk_size\))0 5251 yFo(f)198 5325 y Fj(...initialization)16 b(of)j Fg(mctx)fFj(to)h(be)g(\002lled)f(in...)0 5400 y Fo(g)2025 83 yFx(There)23 b(is)i(one)f(subtle)f(b)n(ut)h(important)f(point)g(we)h(should)f(men-)2025 183 y(tion:)45 b(The)30 b(use)h(of)f(the)h(C)g(pre-processor)d Fp(#define)h Fx(direc-)2025 282 y(ti)n(v)o(e)36b(to)g(implement)f Fp(mctx)p 2881 282 V 29 w(save)p Fx(,)40b Fp(mctx)p 3371 282 V 29 w(restore)35 b Fx(and)2025382 y Fp(mctx)p 2230 382 V 29 w(switch)40 b Fx(is)i(intentional.)86b(F)o(or)40 b(technical)g(reasons)2025 482 y(related)35b(to)h Fp(setjmp)p Fx(\(3\))e(semantics)h(and)g Fp(return)gFx(related)2025 581 y(stack)h(beha)n(vior)e(\(which)h(we)i(will)f(e)o(xplain)f(later)h(in)g(detail\))2025 681 y(we)25 b Fr(cannot)gFx(implement)f(these)h(three)g(functions)e(\(at)i(least)h(not)2025780 y Fp(mctx)p 2230 780 V 29 w(switch)p Fx(\))32 b(as)h(C)g(functions)e(if)h(we)h(w)o(ant)f(to)h(achie)n(v)o(e)2025 880 y(maximum)25b(portability)g(across)i(all)g(platforms.)43 b(Instead)26b(the)o(y)2025 980 y(ha)n(v)o(e)20 b(to)g(be)g(implemented)e(as)j(pre-processor)d(macros.)2025 1217 y FA(3.1)99 b(Algorithm)25b(Ov)o(er)o(view)2025 1373 y Fx(The)g(general)e(idea)i(for)fFp(mctx)p 2940 1373 V 29 w(create)h Fx(is)h(to)f(con\002gure)e(the)20251472 y(gi)n(v)o(en)14 b(stack)i(as)g(a)g(signal)f(stack)h(via)fFp(sigaltstack)p Fx(\(2\),)f(send)2025 1572 y(the)33b(current)f(process)g(a)i(signal)e(to)i(transfer)e(e)o(x)o(ecution)e(con-)2025 1672 y(t
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -