head 1.9; access; symbols PTH_2_0_7:1.8 PTH_2_0_6:1.8 PTH_2_0_5:1.8 PTH_2_0_4:1.8 PTH_2_0_3:1.8 PTH_2_0_2:1.8 PTH_2_0_1:1.8 PTH_2_0_0:1.8 PTH_2_0b2:1.8 PTH_2_0b1:1.8 PTH_2_0b0:1.8 PTH_1_4:1.8.0.2 PTH_1_4_1:1.8 PTH_1_4_0:1.8 PTH_1_3_7:1.5.4.1 PTH_1_4a3:1.8 PTH_1_3_6:1.5.4.1 PTH_1_4a2:1.8 PTH_1_3_5:1.5 PTH_1_4a1:1.5 PTH_1_3_4:1.5 PTH_1_3:1.5.0.4 PTH_1_3_3:1.5 PTH_1_3_2:1.5 PTH_1_3_1:1.5 PTH_1_3_0:1.5 PTH_1_3b3:1.5 PTH_1_2_3:1.5 PTH_1_3b2:1.5 PTH_1_3b1:1.5 PTH_1_3a5:1.5 PTH_1_3a4:1.5 PTH_1_3a3:1.5 PTH_1_2_2:1.5 PTH_1_3a2:1.5 PTH_1_2_1:1.5 PTH_1_3a1:1.5 PTH_1_2:1.5.0.2 PTH_1_2_0:1.5 PTH_1_2b8:1.5 PTH_1_2b7:1.5 PTH_1_1_6:1.4 PTH_1_2b6:1.4 PTH_1_2b5:1.4 PTH_1_2b4:1.4 PTH_1_2b3:1.4 PTH_1_2b2:1.4 PTH_1_2b1:1.4 PTH_1_1_5:1.4 PTH_1_1:1.4.0.2 PTH_1_1_4:1.4 PTH_1_1_3:1.4 PTH_1_1_2:1.4 PTH_1_1_1:1.4 PTH_1_1_0:1.4 PTH_1_1b7:1.3 PTH_1_1b6:1.2 PTH_1_1b5:1.1; locks; strict; comment @% @; expand @o@; 1.9 date 2006.06.12.18.02.52; author rse; state Exp; branches; next 1.8; commitid lF669ffNur3U7KAr; 1.8 date 2000.06.18.09.12.35; author rse; state Exp; branches; next 1.7; 1.7 date 2000.06.02.15.40.29; author rse; state Exp; branches; next 1.6; 1.6 date 2000.06.02.15.37.43; author rse; state Exp; branches; next 1.5; 1.5 date 99.10.19.13.21.46; author rse; state Exp; branches 1.5.4.1; next 1.4; 1.4 date 99.08.19.14.24.16; author rse; state Exp; branches; next 1.3; 1.3 date 99.08.18.14.28.31; author rse; state Exp; branches; next 1.2; 1.2 date 99.08.18.11.10.37; author rse; state Exp; branches; next 1.1; 1.1 date 99.08.17.08.15.17; author rse; state Exp; branches; next ; 1.5.4.1 date 2000.07.01.14.07.11; author rse; state Exp; branches; next ; desc @@ 1.9 log @Added LaTeX sources of USENIX paper (rse-pmt.tex, rse-pmt.fig) to source tree and add Makefile targets for re-generating outputs (rse-pmt.ps, rse-pmt.pdf). This way all GNU Pth related material is finally part of the same distribution. @ text @%!PS-Adobe-2.0 %%Creator: dvips(k) 5.95a Copyright 2005 Radical Eye Software %%Title: rse-pmt.dvi %%Pages: 11 %%PageOrder: Ascend %%BoundingBox: 0 0 595 842 %%DocumentFonts: Times-Bold Times-Roman Times-Italic Helvetica-Bold %%+ Courier CMSY9 Times-BoldItalic Courier-Bold Courier-BoldOblique %%+ CMSY8 CMMI10 CMSY10 CMR10 Helvetica %%DocumentPaperSizes: a4 %%EndComments %DVIPSWebPage: (www.radicaleye.com) %DVIPSCommandLine: dvips -o rse-pmt.ps rse-pmt.dvi %DVIPSParameters: dpi=1200 %DVIPSSource: TeX output 2006.06.12:1951 %%BeginProcSet: tex.pro 0 0 %! /TeXDict 300 dict def TeXDict begin/N{def}def/B{bind def}N/S{exch}N/X{S N}B/A{dup}B/TR{translate}N/isls false N/vsize 11 72 mul N/hsize 8.5 72 mul N/landplus90{false}def/@@rigin{isls{[0 landplus90{1 -1}{-1 1}ifelse 0 0 0]concat}if 72 Resolution div 72 VResolution div neg scale isls{ landplus90{VResolution 72 div vsize mul 0 exch}{Resolution -72 div hsize mul 0}ifelse TR}if Resolution VResolution vsize -72 div 1 add mul TR[ matrix currentmatrix{A A round sub abs 0.00001 lt{round}if}forall round exch round exch]setmatrix}N/@@landscape{/isls true N}B/@@manualfeed{ statusdict/manualfeed true put}B/@@copies{/#copies X}B/FMat[1 0 0 -1 0 0] N/FBB[0 0 0 0]N/nn 0 N/IEn 0 N/ctr 0 N/df-tail{/nn 8 dict N nn begin /FontType 3 N/FontMatrix fntrx N/FontBBox FBB N string/base X array /BitMaps X/BuildChar{CharBuilder}N/Encoding IEn N end A{/foo setfont}2 array copy cvx N load 0 nn put/ctr 0 N[}B/sf 0 N/df{/sf 1 N/fntrx FMat N df-tail}B/dfs{div/sf X/fntrx[sf 0 0 sf neg 0 0]N df-tail}B/E{pop nn A definefont setfont}B/Cw{Cd A length 5 sub get}B/Ch{Cd A length 4 sub get }B/Cx{128 Cd A length 3 sub get sub}B/Cy{Cd A length 2 sub get 127 sub} B/Cdx{Cd A length 1 sub get}B/Ci{Cd A type/stringtype ne{ctr get/ctr ctr 1 add N}if}B/CharBuilder{save 3 1 roll S A/base get 2 index get S /BitMaps get S get/Cd X pop/ctr 0 N Cdx 0 Cx Cy Ch sub Cx Cw add Cy setcachedevice Cw Ch true[1 0 0 -1 -.1 Cx sub Cy .1 sub]{Ci}imagemask restore}B/D{/cc X A type/stringtype ne{]}if nn/base get cc ctr put nn /BitMaps get S ctr S sf 1 ne{A A length 1 sub A 2 index S get sf div put }if put/ctr ctr 1 add N}B/I{cc 1 add D}B/bop{userdict/bop-hook known{ bop-hook}if/SI save N @@rigin 0 0 moveto/V matrix currentmatrix A 1 get A mul exch 0 get A mul add .99 lt{/QV}{/RV}ifelse load def pop pop}N/eop{ SI restore userdict/eop-hook known{eop-hook}if showpage}N/@@start{ userdict/start-hook known{start-hook}if pop/VResolution X/Resolution X 1000 div/DVImag X/IEn 256 array N 2 string 0 1 255{IEn S A 360 add 36 4 index cvrs cvn put}for pop 65781.76 div/vsize X 65781.76 div/hsize X}N /p{show}N/RMat[1 0 0 -1 0 0]N/BDot 260 string N/Rx 0 N/Ry 0 N/V{}B/RV/v{ /Ry X/Rx X V}B statusdict begin/product where{pop false[(Display)(NeXT) (LaserWriter 16/600)]{A length product length le{A length product exch 0 exch getinterval eq{pop true exit}if}{pop}ifelse}forall}{false}ifelse end{{gsave TR -.1 .1 TR 1 1 scale Rx Ry false RMat{BDot}imagemask grestore}}{{gsave TR -.1 .1 TR Rx Ry scale 1 1 false RMat{BDot} imagemask grestore}}ifelse B/QV{gsave newpath transform round exch round exch itransform moveto Rx 0 rlineto 0 Ry neg rlineto Rx neg 0 rlineto fill grestore}B/a{moveto}B/delta 0 N/tail{A/delta X 0 rmoveto}B/M{S p delta add tail}B/b{S p tail}B/c{-4 M}B/d{-3 M}B/e{-2 M}B/f{-1 M}B/g{0 M} B/h{1 M}B/i{2 M}B/j{3 M}B/k{4 M}B/w{0 rmoveto}B/l{p -4 w}B/m{p -3 w}B/n{ p -2 w}B/o{p -1 w}B/q{p 1 w}B/r{p 2 w}B/s{p 3 w}B/t{p 4 w}B/x{0 S rmoveto}B/y{3 2 roll p a}B/bos{/SS save N}B/eos{SS restore}B end %%EndProcSet %%BeginProcSet: 8r.enc 0 0 % File 8r.enc TeX Base 1 Encoding Revision 2.0 2002-10-30 % % @@@@psencodingfile@@{ % author = "S. Rahtz, P. MacKay, Alan Jeffrey, B. Horn, K. Berry, % W. Schmidt, P. Lehman", % version = "2.0", % date = "30 October 2002", % filename = "8r.enc", % email = "tex-fonts@@@@tug.org", % docstring = "This is the encoding vector for Type1 and TrueType % fonts to be used with TeX. This file is part of the % PSNFSS bundle, version 9" % @@} % % The idea is to have all the characters normally included in Type 1 fonts % available for typesetting. This is effectively the characters in Adobe % Standard encoding, ISO Latin 1, Windows ANSI including the euro symbol, % MacRoman, and some extra characters from Lucida. % % Character code assignments were made as follows: % % (1) the Windows ANSI characters are almost all in their Windows ANSI % positions, because some Windows users cannot easily reencode the % fonts, and it makes no difference on other systems. The only Windows % ANSI characters not available are those that make no sense for % typesetting -- rubout (127 decimal), nobreakspace (160), softhyphen % (173). quotesingle and grave are moved just because it's such an % irritation not having them in TeX positions. % % (2) Remaining characters are assigned arbitrarily to the lower part % of the range, avoiding 0, 10 and 13 in case we meet dumb software. % % (3) Y&Y Lucida Bright includes some extra text characters; in the % hopes that other PostScript fonts, perhaps created for public % consumption, will include them, they are included starting at 0x12. % These are /dotlessj /ff /ffi /ffl. % % (4) hyphen appears twice for compatibility with both ASCII and Windows. % % (5) /Euro was assigned to 128, as in Windows ANSI % % (6) Missing characters from MacRoman encoding incorporated as follows: % % PostScript MacRoman TeXBase1 % -------------- -------------- -------------- % /notequal 173 0x16 % /infinity 176 0x17 % /lessequal 178 0x18 % /greaterequal 179 0x19 % /partialdiff 182 0x1A % /summation 183 0x1B % /product 184 0x1C % /pi 185 0x1D % /integral 186 0x81 % /Omega 189 0x8D % /radical 195 0x8E % /approxequal 197 0x8F % /Delta 198 0x9D % /lozenge 215 0x9E % /TeXBase1Encoding [ % 0x00 /.notdef /dotaccent /fi /fl /fraction /hungarumlaut /Lslash /lslash /ogonek /ring /.notdef /breve /minus /.notdef /Zcaron /zcaron % 0x10 /caron /dotlessi /dotlessj /ff /ffi /ffl /notequal /infinity /lessequal /greaterequal /partialdiff /summation /product /pi /grave /quotesingle % 0x20 /space /exclam /quotedbl /numbersign /dollar /percent /ampersand /quoteright /parenleft /parenright /asterisk /plus /comma /hyphen /period /slash % 0x30 /zero /one /two /three /four /five /six /seven /eight /nine /colon /semicolon /less /equal /greater /question % 0x40 /at /A /B /C /D /E /F /G /H /I /J /K /L /M /N /O % 0x50 /P /Q /R /S /T /U /V /W /X /Y /Z /bracketleft /backslash /bracketright /asciicircum /underscore % 0x60 /quoteleft /a /b /c /d /e /f /g /h /i /j /k /l /m /n /o % 0x70 /p /q /r /s /t /u /v /w /x /y /z /braceleft /bar /braceright /asciitilde /.notdef % 0x80 /Euro /integral /quotesinglbase /florin /quotedblbase /ellipsis /dagger /daggerdbl /circumflex /perthousand /Scaron /guilsinglleft /OE /Omega /radical /approxequal % 0x90 /.notdef /.notdef /.notdef /quotedblleft /quotedblright /bullet /endash /emdash /tilde /trademark /scaron /guilsinglright /oe /Delta /lozenge /Ydieresis % 0xA0 /.notdef /exclamdown /cent /sterling /currency /yen /brokenbar /section /dieresis /copyright /ordfeminine /guillemotleft /logicalnot /hyphen /registered /macron % 0xD0 /degree /plusminus /twosuperior /threesuperior /acute /mu /paragraph /periodcentered /cedilla /onesuperior /ordmasculine /guillemotright /onequarter /onehalf /threequarters /questiondown % 0xC0 /Agrave /Aacute /Acircumflex /Atilde /Adieresis /Aring /AE /Ccedilla /Egrave /Eacute /Ecircumflex /Edieresis /Igrave /Iacute /Icircumflex /Idieresis % 0xD0 /Eth /Ntilde /Ograve /Oacute /Ocircumflex /Otilde /Odieresis /multiply /Oslash /Ugrave /Uacute /Ucircumflex /Udieresis /Yacute /Thorn /germandbls % 0xE0 /agrave /aacute /acircumflex /atilde /adieresis /aring /ae /ccedilla /egrave /eacute /ecircumflex /edieresis /igrave /iacute /icircumflex /idieresis % 0xF0 /eth /ntilde /ograve /oacute /ocircumflex /otilde /odieresis /divide /oslash /ugrave /uacute /ucircumflex /udieresis /yacute /thorn /ydieresis ] def %%EndProcSet %%BeginProcSet: texps.pro 0 0 %! TeXDict begin/rf{findfont dup length 1 add dict begin{1 index/FID ne 2 index/UniqueID ne and{def}{pop pop}ifelse}forall[1 index 0 6 -1 roll exec 0 exch 5 -1 roll VResolution Resolution div mul neg 0 0]FontType 0 ne{/Metrics exch def dict begin Encoding{exch dup type/integertype ne{ pop pop 1 sub dup 0 le{pop}{[}ifelse}{FontMatrix 0 get div Metrics 0 get div def}ifelse}forall Metrics/Metrics currentdict end def}{{1 index type /nametype eq{exit}if exch pop}loop}ifelse[2 index currentdict end definefont 3 -1 roll makefont/setfont cvx]cvx def}def/ObliqueSlant{dup sin S cos div neg}B/SlantFont{4 index mul add}def/ExtendFont{3 -1 roll mul exch}def/ReEncodeFont{CharStrings rcheck{/Encoding false def dup[ exch{dup CharStrings exch known not{pop/.notdef/Encoding true def}if} forall Encoding{]exch pop}{cleartomark}ifelse}if/Encoding exch def}def end %%EndProcSet %%BeginProcSet: special.pro 0 0 %! TeXDict begin/SDict 200 dict N SDict begin/@@SpecialDefaults{/hs 612 N /vs 792 N/ho 0 N/vo 0 N/hsc 1 N/vsc 1 N/ang 0 N/CLIP 0 N/rwiSeen false N /rhiSeen false N/letter{}N/note{}N/a4{}N/legal{}N}B/@@scaleunit 100 N /@@hscale{@@scaleunit div/hsc X}B/@@vscale{@@scaleunit div/vsc X}B/@@hsize{ /hs X/CLIP 1 N}B/@@vsize{/vs X/CLIP 1 N}B/@@clip{/CLIP 2 N}B/@@hoffset{/ho X}B/@@voffset{/vo X}B/@@angle{/ang X}B/@@rwi{10 div/rwi X/rwiSeen true N}B /@@rhi{10 div/rhi X/rhiSeen true N}B/@@llx{/llx X}B/@@lly{/lly X}B/@@urx{ /urx X}B/@@ury{/ury X}B/magscale true def end/@@MacSetUp{userdict/md known {userdict/md get type/dicttype eq{userdict begin md length 10 add md maxlength ge{/md md dup length 20 add dict copy def}if end md begin /letter{}N/note{}N/legal{}N/od{txpose 1 0 mtx defaultmatrix dtransform S atan/pa X newpath clippath mark{transform{itransform moveto}}{transform{ itransform lineto}}{6 -2 roll transform 6 -2 roll transform 6 -2 roll transform{itransform 6 2 roll itransform 6 2 roll itransform 6 2 roll curveto}}{{closepath}}pathforall newpath counttomark array astore/gc xdf pop ct 39 0 put 10 fz 0 fs 2 F/|______Courier fnt invertflag{PaintBlack} if}N/txpose{pxs pys scale ppr aload pop por{noflips{pop S neg S TR pop 1 -1 scale}if xflip yflip and{pop S neg S TR 180 rotate 1 -1 scale ppr 3 get ppr 1 get neg sub neg ppr 2 get ppr 0 get neg sub neg TR}if xflip yflip not and{pop S neg S TR pop 180 rotate ppr 3 get ppr 1 get neg sub neg 0 TR}if yflip xflip not and{ppr 1 get neg ppr 0 get neg TR}if}{ noflips{TR pop pop 270 rotate 1 -1 scale}if xflip yflip and{TR pop pop 90 rotate 1 -1 scale ppr 3 get ppr 1 get neg sub neg ppr 2 get ppr 0 get neg sub neg TR}if xflip yflip not and{TR pop pop 90 rotate ppr 3 get ppr 1 get neg sub neg 0 TR}if yflip xflip not and{TR pop pop 270 rotate ppr 2 get ppr 0 get neg sub neg 0 S TR}if}ifelse scaleby96{ppr aload pop 4 -1 roll add 2 div 3 1 roll add 2 div 2 copy TR .96 dup scale neg S neg S TR}if}N/cp{pop pop showpage pm restore}N end}if}if}N/normalscale{ Resolution 72 div VResolution 72 div neg scale magscale{DVImag dup scale }if 0 setgray}N/psfts{S 65781.76 div N}N/startTexFig{/psf$SavedState save N userdict maxlength dict begin/magscale true def normalscale currentpoint TR/psf$ury psfts/psf$urx psfts/psf$lly psfts/psf$llx psfts /psf$y psfts/psf$x psfts currentpoint/psf$cy X/psf$cx X/psf$sx psf$x psf$urx psf$llx sub div N/psf$sy psf$y psf$ury psf$lly sub div N psf$sx psf$sy scale psf$cx psf$sx div psf$llx sub psf$cy psf$sy div psf$ury sub TR/showpage{}N/erasepage{}N/setpagedevice{pop}N/copypage{}N/p 3 def @@MacSetUp}N/doclip{psf$llx psf$lly psf$urx psf$ury currentpoint 6 2 roll newpath 4 copy 4 2 roll moveto 6 -1 roll S lineto S lineto S lineto closepath clip newpath moveto}N/endTexFig{end psf$SavedState restore}N /@@beginspecial{SDict begin/SpecialSave save N gsave normalscale currentpoint TR @@SpecialDefaults count/ocount X/dcount countdictstack N} N/@@setspecial{CLIP 1 eq{newpath 0 0 moveto hs 0 rlineto 0 vs rlineto hs neg 0 rlineto closepath clip}if ho vo TR hsc vsc scale ang rotate rwiSeen{rwi urx llx sub div rhiSeen{rhi ury lly sub div}{dup}ifelse scale llx neg lly neg TR}{rhiSeen{rhi ury lly sub div dup scale llx neg lly neg TR}if}ifelse CLIP 2 eq{newpath llx lly moveto urx lly lineto urx ury lineto llx ury lineto closepath clip}if/showpage{}N/erasepage{}N /setpagedevice{pop}N/copypage{}N newpath}N/@@endspecial{count ocount sub{ pop}repeat countdictstack dcount sub{end}repeat grestore SpecialSave restore end}N/@@defspecial{SDict begin}N/@@fedspecial{end}B/li{lineto}B /rl{rlineto}B/rc{rcurveto}B/np{/SaveX currentpoint/SaveY X N 1 setlinecap newpath}N/st{stroke SaveX SaveY moveto}N/fil{fill SaveX SaveY moveto}N/ellipse{/endangle X/startangle X/yrad X/xrad X/savematrix matrix currentmatrix N TR xrad yrad scale 0 0 1 startangle endangle arc savematrix setmatrix}N end %%EndProcSet %%BeginProcSet: color.pro 0 0 %! TeXDict begin/setcmykcolor where{pop}{/setcmykcolor{dup 10 eq{pop setrgbcolor}{1 sub 4 1 roll 3{3 index add neg dup 0 lt{pop 0}if 3 1 roll }repeat setrgbcolor pop}ifelse}B}ifelse/TeXcolorcmyk{setcmykcolor}def /TeXcolorrgb{setrgbcolor}def/TeXcolorgrey{setgray}def/TeXcolorgray{ setgray}def/TeXcolorhsb{sethsbcolor}def/currentcmykcolor where{pop}{ /currentcmykcolor{currentrgbcolor 10}B}ifelse/DC{exch dup userdict exch known{pop pop}{X}ifelse}B/GreenYellow{0.15 0 0.69 0 setcmykcolor}DC /Yellow{0 0 1 0 setcmykcolor}DC/Goldenrod{0 0.10 0.84 0 setcmykcolor}DC /Dandelion{0 0.29 0.84 0 setcmykcolor}DC/Apricot{0 0.32 0.52 0 setcmykcolor}DC/Peach{0 0.50 0.70 0 setcmykcolor}DC/Melon{0 0.46 0.50 0 setcmykcolor}DC/YellowOrange{0 0.42 1 0 setcmykcolor}DC/Orange{0 0.61 0.87 0 setcmykcolor}DC/BurntOrange{0 0.51 1 0 setcmykcolor}DC /Bittersweet{0 0.75 1 0.24 setcmykcolor}DC/RedOrange{0 0.77 0.87 0 setcmykcolor}DC/Mahogany{0 0.85 0.87 0.35 setcmykcolor}DC/Maroon{0 0.87 0.68 0.32 setcmykcolor}DC/BrickRed{0 0.89 0.94 0.28 setcmykcolor}DC/Red{ 0 1 1 0 setcmykcolor}DC/OrangeRed{0 1 0.50 0 setcmykcolor}DC/RubineRed{ 0 1 0.13 0 setcmykcolor}DC/WildStrawberry{0 0.96 0.39 0 setcmykcolor}DC /Salmon{0 0.53 0.38 0 setcmykcolor}DC/CarnationPink{0 0.63 0 0 setcmykcolor}DC/Magenta{0 1 0 0 setcmykcolor}DC/VioletRed{0 0.81 0 0 setcmykcolor}DC/Rhodamine{0 0.82 0 0 setcmykcolor}DC/Mulberry{0.34 0.90 0 0.02 setcmykcolor}DC/RedViolet{0.07 0.90 0 0.34 setcmykcolor}DC /Fuchsia{0.47 0.91 0 0.08 setcmykcolor}DC/Lavender{0 0.48 0 0 setcmykcolor}DC/Thistle{0.12 0.59 0 0 setcmykcolor}DC/Orchid{0.32 0.64 0 0 setcmykcolor}DC/DarkOrchid{0.40 0.80 0.20 0 setcmykcolor}DC/Purple{ 0.45 0.86 0 0 setcmykcolor}DC/Plum{0.50 1 0 0 setcmykcolor}DC/Violet{ 0.79 0.88 0 0 setcmykcolor}DC/RoyalPurple{0.75 0.90 0 0 setcmykcolor}DC /BlueViolet{0.86 0.91 0 0.04 setcmykcolor}DC/Periwinkle{0.57 0.55 0 0 setcmykcolor}DC/CadetBlue{0.62 0.57 0.23 0 setcmykcolor}DC /CornflowerBlue{0.65 0.13 0 0 setcmykcolor}DC/MidnightBlue{0.98 0.13 0 0.43 setcmykcolor}DC/NavyBlue{0.94 0.54 0 0 setcmykcolor}DC/RoyalBlue{1 0.50 0 0 setcmykcolor}DC/Blue{1 1 0 0 setcmykcolor}DC/Cerulean{0.94 0.11 0 0 setcmykcolor}DC/Cyan{1 0 0 0 setcmykcolor}DC/ProcessBlue{0.96 0 0 0 setcmykcolor}DC/SkyBlue{0.62 0 0.12 0 setcmykcolor}DC/Turquoise{0.85 0 0.20 0 setcmykcolor}DC/TealBlue{0.86 0 0.34 0.02 setcmykcolor}DC /Aquamarine{0.82 0 0.30 0 setcmykcolor}DC/BlueGreen{0.85 0 0.33 0 setcmykcolor}DC/Emerald{1 0 0.50 0 setcmykcolor}DC/JungleGreen{0.99 0 0.52 0 setcmykcolor}DC/SeaGreen{0.69 0 0.50 0 setcmykcolor}DC/Green{1 0 1 0 setcmykcolor}DC/ForestGreen{0.91 0 0.88 0.12 setcmykcolor}DC /PineGreen{0.92 0 0.59 0.25 setcmykcolor}DC/LimeGreen{0.50 0 1 0 setcmykcolor}DC/YellowGreen{0.44 0 0.74 0 setcmykcolor}DC/SpringGreen{ 0.26 0 0.76 0 setcmykcolor}DC/OliveGreen{0.64 0 0.95 0.40 setcmykcolor} DC/RawSienna{0 0.72 1 0.45 setcmykcolor}DC/Sepia{0 0.83 1 0.70 setcmykcolor}DC/Brown{0 0.81 1 0.60 setcmykcolor}DC/Tan{0.14 0.42 0.56 0 setcmykcolor}DC/Gray{0 0 0 0.50 setcmykcolor}DC/Black{0 0 0 1 setcmykcolor}DC/White{0 0 0 0 setcmykcolor}DC end %%EndProcSet %%BeginFont: CMR10 %!PS-AdobeFont-1.1: CMR10 1.00B %%CreationDate: 1992 Feb 19 19:54:52 % Copyright (C) 1997 American Mathematical Society. All Rights Reserved. 11 dict begin /FontInfo 7 dict dup begin /version (1.00B) readonly def /Notice (Copyright (C) 1997 American Mathematical Society. All Rights Reserved) readonly def /FullName (CMR10) readonly def /FamilyName (Computer Modern) readonly def /Weight (Medium) readonly def /ItalicAngle 0 def /isFixedPitch false def end readonly def /FontName /CMR10 def /PaintType 0 def /FontType 1 def /FontMatrix [0.001 0 0 0.001 0 0] readonly def /Encoding 256 array 0 1 255 {1 index exch /.notdef put} for dup 43 /plus put dup 50 /two put readonly def /FontBBox{-251 -250 1009 969}readonly def currentdict end currentfile eexec D9D66F633B846A97B686A97E45A3D0AA052A014267B7904EB3C0D3BD0B83D891 016CA6CA4B712ADEB258FAAB9A130EE605E61F77FC1B738ABC7C51CD46EF8171 9098D5FEE67660E69A7AB91B58F29A4D79E57022F783EB0FBBB6D4F4EC35014F D2DECBA99459A4C59DF0C6EBA150284454E707DC2100C15B76B4C19B84363758 469A6C558785B226332152109871A9883487DD7710949204DDCF837E6A8708B8 2BDBF16FBC7512FAA308A093FE5CF7158F1163BC1F3352E22A1452E73FECA8A4 87100FB1FFC4C8AF409B2067537220E605DA0852CA49839E1386AF9D7A1A455F D1F017CE45884D76EF2CB9BC5821FD25365DDEA6E45F332B5F68A44AD8A530F0 92A36FAC8D27F9087AFEEA2096F839A2BC4B937F24E080EF7C0F9374A18D565C 295A05210DB96A23175AC59A9BD0147A310EF49C551A417E0A22703F94FF7B75 409A5D417DA6730A69E310FA6A4229FC7E4F620B0FC4C63C50E99E179EB51E4C 4BC45217722F1E8E40F1E1428E792EAFE05C5A50D38C52114DFCD24D54027CBF 2512DD116F0463DE4052A7AD53B641A27E81E481947884CE35661B49153FA19E 0A2A860C7B61558671303DE6AE06A80E4E450E17067676E6BBB42A9A24ACBC3E B0CA7B7A3BFEA84FED39CCFB6D545BB2BCC49E5E16976407AB9D94556CD4F008 24EF579B6800B6DC3AAF840B3FC6822872368E3B4274DD06CA36AF8F6346C11B 43C772CC242F3B212C4BD7018D71A1A74C9A94ED0093A5FB6557F4E0751047AF D72098ECA301B8AE68110F983796E581F106144951DF5B750432A230FDA3B575 5A38B5E7972AABC12306A01A99FCF8189D71B8DBF49550BAEA9CF1B97CBFC7CC 96498ECC938B1A1710B670657DE923A659DB8757147B140A48067328E7E3F9C3 7D1888B284904301450CE0BC15EEEA00E48CCD6388F3FC3C8578EF9A20A0E06E 4F7ADDAF0E7D1E182D115BF1AD931977325AD391E72E2B13CC108E3726C11099 E2000623188AAAC9F3E233EB253BDD8B0A4759A66A113E066238B0086AC1B634 5ABFF90E4B5ED3FA69C22541981B2BFC9710AEF6B50A8BB53431C7B4D380D721 639E005D6B4688EE16BFF48443E7C9E5FB5BC5883E271CB034289232A0694CCE 12A5A2637485FB47BC281A2129E040DE629890E13430C788F3860C1EC52C8DE4 581DE90A40BF466BD3EFF7D03E7B33CF5DF9BD038D5E27602FC07D387B1115BF 843776E976022FA3DDDB1132F32935428D0B14D9F2835F2E774363ECA0E7BA0B D9B4A0083A8D751A82644167C763874FB86DAB03BDECC3EEC2243CCDB104DD94 24FC1731E5C449F475E53B73D92814059370688B9E8252D0402188058A8214B1 9D409DD944A2F2EA0BF8559353B2F7690C0C23386C54B813FD74D541BD12A6BC 167FC46065D5EC82F71315F8BD967DB8272042F5D9818E7A0F616870A39B7071 29E50828BA48B9D82755F09DA2B6F56C414AF14B7CBD6BE39AFCF681709D2471 652FB14F114FDA3F8141CABABE595D1DD69D1C4A9AE8F491BD3D7DE3B8F7C3ED 9759358B5EBE0C8B3402A9A2025A82639708D9596A8087ED8F601A152A518A37 A6D73278AF9A487F55091DBB148299C853FA5012A3C4522B206B89356C0F6067 0BA3EE71F3B36B7EE3E5AFDD3224E93A8BAD1F64E272417647158942C682700C 3BC5E0995E2EE30DB8BF9469F0995FAF1A82 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 cleartomark %%EndFont %%BeginFont: CMSY10 %!PS-AdobeFont-1.1: CMSY10 1.0 %%CreationDate: 1991 Aug 15 07:20:57 % Copyright (C) 1997 American Mathematical Society. All Rights Reserved. 11 dict begin /FontInfo 7 dict dup begin /version (1.0) readonly def /Notice (Copyright (C) 1997 American Mathematical Society. All Rights Reserved) readonly def /FullName (CMSY10) readonly def /FamilyName (Computer Modern) readonly def /Weight (Medium) readonly def /ItalicAngle -14.035 def /isFixedPitch false def end readonly def /FontName /CMSY10 def /PaintType 0 def /FontType 1 def /FontMatrix [0.001 0 0 0.001 0 0] readonly def /Encoding 256 array 0 1 255 {1 index exch /.notdef put} for dup 2 /multiply put readonly def /FontBBox{-29 -960 1116 775}readonly def currentdict end currentfile eexec D9D66F633B846A97B686A97E45A3D0AA052F09F9C8ADE9D907C058B87E9B6964 7D53359E51216774A4EAA1E2B58EC3176BD1184A633B951372B4198D4E8C5EF4 A213ACB58AA0A658908035BF2ED8531779838A960DFE2B27EA49C37156989C85 E21B3ABF72E39A89232CD9F4237FC80C9E64E8425AA3BEF7DED60B122A52922A 221A37D9A807DD01161779DDE7D31FF2B87F97C73D63EECDDA4C49501773468A 27D1663E0B62F461F6E40A5D6676D1D12B51E641C1D4E8E2771864FC104F8CBF 5B78EC1D88228725F1C453A678F58A7E1B7BD7CA700717D288EB8DA1F57C4F09 0ABF1D42C5DDD0C384C7E22F8F8047BE1D4C1CC8E33368FB1AC82B4E96146730 DE3302B2E6B819CB6AE455B1AF3187FFE8071AA57EF8A6616B9CB7941D44EC7A 71A7BB3DF755178D7D2E4BB69859EFA4BBC30BD6BB1531133FD4D9438FF99F09 4ECC068A324D75B5F696B8688EEB2F17E5ED34CCD6D047A4E3806D000C199D7C 515DB70A8D4F6146FE068DC1E5DE8BC5703711DA090312BA3FC00A08C453C609 C627A8B149D71F3155BFBF5A359E54EFE40EB6AAF7344F336BBDFA5241D39DFD 19DFE4F1B56DCAAEA84D9290E81FFAD8472517CB517C9F8D04EAF8E87A58341D 57386FF17BF29235356C3F85FE85CCB75DFBD407A6B76C198356415768FE0D58 0F0E0665E6B2EFC269256FCE0146C415E35D900293555D8B6CB2AA451586E477 93461FAF0803AE54B9FEE351771B08F9F7D564B5F50CB4E660298DECF4DC0F67 0A4DA5738D89F708019CDFF9443964830B406748B60EF34EA35EDA7DF1C05A1E A385C5DE4B171FCD735AA0C41E54B09D133E48FCAA3A75F1B05C2AD4CD792534 4160FCF0C48076AD3174E1A970FD9A285EB251E016B2739E373B43F17A1D50BC 02FB2383DACFF35A5503EE8D0C8147908BFF5FA0D9D06F243AA8B4FFFCD8FF4E ED02BC 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 cleartomark %%EndFont %%BeginFont: CMMI10 %!PS-AdobeFont-1.1: CMMI10 1.100 %%CreationDate: 1996 Jul 23 07:53:57 % Copyright (C) 1997 American Mathematical Society. All Rights Reserved. 11 dict begin /FontInfo 7 dict dup begin /version (1.100) readonly def /Notice (Copyright (C) 1997 American Mathematical Society. All Rights Reserved) readonly def /FullName (CMMI10) readonly def /FamilyName (Computer Modern) readonly def /Weight (Medium) readonly def /ItalicAngle -14.04 def /isFixedPitch false def end readonly def /FontName /CMMI10 def /PaintType 0 def /FontType 1 def /FontMatrix [0.001 0 0 0.001 0 0] readonly def /Encoding 256 array 0 1 255 {1 index exch /.notdef put} for dup 65 /A put dup 78 /N put readonly def /FontBBox{-32 -250 1048 750}readonly def currentdict end currentfile eexec D9D66F633B846A97B686A97E45A3D0AA0529731C99A784CCBE85B4993B2EEBDE 3B12D472B7CF54651EF21185116A69AB1096ED4BAD2F646635E019B6417CC77B 532F85D811C70D1429A19A5307EF63EB5C5E02C89FC6C20F6D9D89E7D91FE470 B72BEFDA23F5DF76BE05AF4CE93137A219ED8A04A9D7D6FDF37E6B7FCDE0D90B 986423E5960A5D9FBB4C956556E8DF90CBFAEC476FA36FD9A5C8175C9AF513FE D919C2DDD26BDC0D99398B9F4D03D5993DFC0930297866E1CD0A319B6B1FD958 9E394A533A081C36D456A09920001A3D2199583EB9B84B4DEE08E3D12939E321 990CD249827D9648574955F61BAAA11263A91B6C3D47A5190165B0C25ABF6D3E 6EC187E4B05182126BB0D0323D943170B795255260F9FD25F2248D04F45DFBFB DEF7FF8B19BFEF637B210018AE02572B389B3F76282BEB29CC301905D388C721 59616893E774413F48DE0B408BC66DCE3FE17CB9F84D205839D58014D6A88823 D9320AE93AF96D97A02C4D5A2BB2B8C7925C4578003959C46E3CE1A2F0EAC4BF 8B9B325E46435BDE60BC54D72BC8ACB5C0A34413AC87045DC7B84646A324B808 6FD8E34217213E131C3B1510415CE45420688ED9C1D27890EC68BD7C1235FAF9 1DAB3A369DD2FC3BE5CF9655C7B7EDA7361D7E05E5831B6B8E2EEC542A7B38EE 03BE4BAC6079D038ACB3C7C916279764547C2D51976BABA94BA9866D79F13909 95AA39B0F03103A07CBDF441B8C5669F729020AF284B7FF52A29C6255FCAACF1 74109050FBA2602E72593FBCBFC26E726EE4AEF97B7632BC4F5F353B5C67FED2 3EA752A4A57B8F7FEFF1D7341D895F0A3A0BE1D8E3391970457A967EFF84F6D8 47750B1145B8CC5BD96EE7AA99DDC9E06939E383BDA41175233D58AD263EBF19 AFC0E2F840512D321166547B306C592B8A01E1FA2564B9A26DAC14256414E4C8 42616728D918C74D13C349F4186EC7B9708B86467425A6FDB3A396562F7EE4D8 40B43621744CF8A23A6E532649B66C2A0002DD04F8F39618E4F572819DD34837 B5A08E643FDCA1505AF6A1FA3DDFD1FA758013CAED8ACDDBBB334D664DFF5B53 95601766777978D01677B8D19E1B10A078432D2884BB6250F980A231AC216383 1FD449E6C47B12C66F68D19F77E672D207E4454D4FE979040A5CF8D0F31FFB88 13B824C3B5675933EDB05D3EC1687024F38F3B9480096C8CC3EE5E51CF75AF22 92119E844810F5AC882300FFEA881FD9AF30B3641171C15D4B40C088C7A480C4 F0168D1AF719EED16E7F34DFE59E505E5FAAD4DD1E30918D3C06CA3C03CD6A0A AC440A7238E59139C4338BC398EAC55D68593D60981A5FA1DD2222D78164340F C25F5DF2D37EFC9E90F29B44C626410F740734D57C9E463156B7656BB48F8785 18DF94463376E831A865D0A529AB562572F3B37E65CCF7715C1E69DBDE368CE6 5F25C19753DB0D9F25243B703FC683ACC9E35B6643ADC727183649C2EC1927D1 129B93DB94672FA19C9613EAD3B19579B8824BD0D86AD70D89002D44D8328072 BFAE543FCF6BD9A3008DAE7E8E9393EA1672F420FE2E1EF2C49B3532A2642A75 5E1C88B645E99840EBD742881CB6844BA155BD73FDDB923D73895EE2703514C1 5F90371E51BC6A3B252072591921AD07155E1837DFACD9DC3154BBB63DE6FAB4 1867218FE229C1676867B720ED9AFE1C0070DCC3647B01475464C3B8E8414050 488CB48C1A123A49CC016DF43EE649BAE5E00C37DABD7653BC8A18F79F31B926 F0859F043831331AB3DBFB4DC923454C09C29C8049BFBA71BD7FE5EA5B1DFA4C 736D775FDF529F6F99785219D4298E9823771433504E2AA8B0A92EB7D9F433C5 D6406B17D9C03F4DB0277ED4001E1E3D1A537AABB336F651 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 cleartomark %%EndFont %%BeginFont: CMSY8 %!PS-AdobeFont-1.1: CMSY8 1.0 %%CreationDate: 1991 Aug 15 07:22:10 % Copyright (C) 1997 American Mathematical Society. All Rights Reserved. 11 dict begin /FontInfo 7 dict dup begin /version (1.0) readonly def /Notice (Copyright (C) 1997 American Mathematical Society. All Rights Reserved) readonly def /FullName (CMSY8) readonly def /FamilyName (Computer Modern) readonly def /Weight (Medium) readonly def /ItalicAngle -14.035 def /isFixedPitch false def end readonly def /FontName /CMSY8 def /PaintType 0 def /FontType 1 def /FontMatrix [0.001 0 0 0.001 0 0] readonly def /Encoding 256 array 0 1 255 {1 index exch /.notdef put} for dup 2 /multiply put readonly def /FontBBox{-30 -955 1185 779}readonly def currentdict end currentfile eexec D9D66F633B846A97B686A97E45A3D0AA052F09F9C8ADE9D907C058B87E9B6964 7D53359E51216774A4EAA1E2B58EC3176BD1184A633B951372B4198D4E8C5EF4 A213ACB58AA0A658908035BF2ED8531779838A960DFE2B27EA49C37156989C85 E21B3ABF72E39A89232CD9F4237FC80C9E64E8425AA3BEF7DED60B122A52922A 221A37D9A807DD01161779DDE7D5FC1B2109839E5B52DFBB2A7C1B5D8E7E8AA0 5B10EA43D6A8ED61AF5B23D49920D8F79DAB6A59062134D84AC0100187A6CD1F 80F5DDD9D222ACB1C23326A7656A635C4A241CCD32CBFDF8363206B8AA36E107 1477F5496111E055C7491002AFF272E46ECC46422F0380D093284870022523FB DA1716CC4F2E2CCAD5F173FCBE6EDDB874AD255CD5E5C0F86214393FCB5F5C20 9C3C2BB5886E36FC3CCC21483C3AC193485A46E9D22BD7201894E4D45ADD9BF1 CC5CF6A5010B5654AC0BE0DA903DB563B13840BA3015F72E51E3BC80156388BA F83C7D393392BCBC227771CDCB976E93302531886DDA73EBC9178917EFD0C20B 133F1E59A6DB3811BD410DC83AA75BE26D4E754A02DD0075D7A793D8C4B499A2 BC8E8A5316CB716ADB46D95C7B12CA61DD83222233C8DDFA7CDE7D9A6ADE5D37 DCAD7CA36C274C9844CCFFEDA502BBBDB60FAE578D00B89137526796A1EBEAE8 F896D9A1F6DA099B80ABF69047F70A59D086D90DEE48A18606D68FF1B98B671E AED376EEE1EB099004C5343B1D473F845D60C9BCDBB19DA77E0DDDB032052DDB 48629F27A39B5CC68A4A2A285D09356A9BC085EF39D03B96F073FFF0F63AEDDE 0CA7918F4534E6DDC108A93CAEFA2775CB07C30D5D5A108FBBDC5E909F110BB6 89474C11CD76FBC6E9CDA58775C49840E2903429597CBA68253CC92E62FDB847 1D8D9A975D2D7B6E2DD6F3B68F6562C54D091FC926FE5410DE5D9A8C0A73AB17 333450D9 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 cleartomark %%EndFont %%BeginFont: CMSY9 %!PS-AdobeFont-1.1: CMSY9 1.0 %%CreationDate: 1991 Aug 15 07:22:27 % Copyright (C) 1997 American Mathematical Society. All Rights Reserved. 11 dict begin /FontInfo 7 dict dup begin /version (1.0) readonly def /Notice (Copyright (C) 1997 American Mathematical Society. All Rights Reserved) readonly def /FullName (CMSY9) readonly def /FamilyName (Computer Modern) readonly def /Weight (Medium) readonly def /ItalicAngle -14.035 def /isFixedPitch false def end readonly def /FontName /CMSY9 def /PaintType 0 def /FontType 1 def /FontMatrix [0.001 0 0 0.001 0 0] readonly def /Encoding 256 array 0 1 255 {1 index exch /.notdef put} for dup 21 /greaterequal put dup 102 /braceleft put dup 103 /braceright put dup 110 /backslash put readonly def /FontBBox{-30 -958 1146 777}readonly def currentdict end currentfile eexec D9D66F633B846A97B686A97E45A3D0AA052F09F9C8ADE9D907C058B87E9B6964 7D53359E51216774A4EAA1E2B58EC3176BD1184A633B951372B4198D4E8C5EF4 A213ACB58AA0A658908035BF2ED8531779838A960DFE2B27EA49C37156989C85 E21B3ABF72E39A89232CD9F4237FC80C9E64E8425AA3BEF7DED60B122A52922A 221A37D9A807DD01161779DDE7D31FF2B87F97C73D63EECDDA4C49501773468A 27D1663E0B62F461F6E40A5D6676D0037D33F24E2FAC2B0009AD3C8350CDF8CC 65BCA87979C36D14CB552E9A985E48BE4E88ECA16DF418749AF04FDD2B0E1380 D281BB2476BB45FF30946B247DFD7F57305FA87E50CA338121C71CDFDF927A9C 77FF14CB4A1D6D80356FB1171ED38C37702350497B44E42CE31DB2F493807DAA 15B887C671199A54C4C1294BC520F5538C15556BC43C9F62342B121C6DCD6C5F 491DA47FF360201EE21C08A781ED0589A6DF91B99FE118B9B29E4F068672E52F 1A06C514D91C4C937D4E642503392B1CD1B984B04B674C2977A634F63B35677E 9196FFC0F57806BBA111603CDED14B3FD9D537343B83693A57CE39EF70A3F289 CAA584C8ADC703D35922D286242575D4A249B19BBF483DF303D802B5D2FA278F BA6E3C852733048FD342AE39CCC9F4D8809C846471E751597EBD521EFE368858 ADC4CA646D35FF65C797EF775445747B6510960FECCAD9A2EF0B76500EA0C069 B8E8AFA16D099299FE092A97E2FDF483A39FCCACFFF3DF10D4C3CC0D78BB7413 DDB69644F0305FD1158CDB96AC4432BFB81DB1259B8AC16982694149276336C4 659FFC384078BAF79749E72CD873E8B4B99D3D928F624CB6A1FEEE07199E5274 213F60F9902597D145C52DABC5AD44616A8B0894DB47F6F2A07F585BDCE050AE 9F159401B65219C9695CFF122C9CBE743839A29060B2B33A6B0CBDD41516F448 09A8795098237D1805168A35D8E20F1B211A580B2F051572F44E76EC01A7BAE3 66CE505B13C828AF4FAF0EDC9BCD96A014E86993552839DBCFDD88BF84285CE0 BFB2CC6AB9AC3308C2438E1E5E9EB2F7727CDCB4A71BB83F1D70F15436712730 75BD1B40CC9B7FBEFA20B4F3BDA5E68C540D6FC8AF1CA3A30E1FBA7FA7D8E011 2AC2F960786C810642986594F3BB8A33838381FE47D436A485E95DC3C391B251 9C43A85A8D8EAB0E5D5B978ECD8BF0D117D61C3CA57D46BE354513F6DA278B51 FFD5C2E3B25F900BAE47C5D006366E513742409500B32AED625A4B2AC1F4E3F4 8A7D6153C2C6D994804A1E3834E8623F2BC29752B9069BA80C10E9EE8A60E32A 8259CE68944FBB6FB47FACF129FBA781E0D1B7B46481BC05B34482901C8A6770 4D3101B75317FB04E90DFEF394C7A8329D24CC4CA261A339D4B3F4193BD2A5A4 13A1DCDC60A4A685B0F99D8CFC00F944F1D21FC0E0BAC319D36747AA47C156B6 CCD69E3F7AADB801BDFCF16833FCF141FCF33CCACEAF6DA099724DF56FA14CC2 163379669D980643E0BF8B9F2CAA7AB5AE55171A1D6686C28F658B223D039FBB 47A47BFE8FF5907BBBBBE8EC56D2538ADD4ACABA19648592741C5F74F1F1D01F ACB7B023997973E5BC4961990DFA539A16E7764CA2DE4167C2F4F18EAC1D68BD E82B66A38CCA217C10C2A03C4679 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 cleartomark %%EndFont TeXDict begin 39139632 55387786 1000 1200 1200 (rse-pmt.dvi) @@start /Fa 165[73 86 1[113 86 86 73 66 80 86 66 86 86 106 73 86 47 40 86 86 66 73 86 80 80 86 65[{ TeXBase1Encoding ReEncodeFont}25 119.552 /Times-Roman rf /Fb 140[75 50 12[83 101[{TeXBase1Encoding ReEncodeFont}3 149.44 /Helvetica rf /Fc 205[83 6[129 43[{}2 166.044 /CMR10 rf /Fd 253[129 2[{}1 166.044 /CMSY10 rf /Fe 177[133 12[125 65[{}2 166.044 /CMMI10 rf /Ff 253[110 2[{}1 132.835 /CMSY8 rf /Fg 135[90 3[90 6[90 9[90 99[{TeXBase1Encoding ReEncodeFont}4 149.44 /Courier-BoldOblique rf /Fh 135[80 80 2[80 80 2[80 80 80 80 80 80 80 80 80 80 1[80 1[80 1[80 12[80 1[80 1[80 80 80 4[80 1[80 25[80 45[{TeXBase1Encoding ReEncodeFont}25 132.835 /Courier rf /Fi 133[90 90 1[90 90 90 90 90 90 1[90 90 90 1[90 2[90 90 1[90 90 90 90 1[90 1[90 59[90 35[{TeXBase1Encoding ReEncodeFont}21 149.44 /Courier-Bold rf /Fj 133[58 66 75 100 66 83 42 58 58 1[75 75 83 116 42 75 42 42 83 75 50 66 75 66 75 75 50 11[91 83 24[50 75 75 75 75 75 75 75 75 75 75 1[37 6[50 116 35[83 2[{ TeXBase1Encoding ReEncodeFont}43 149.44 /Times-BoldItalic rf /Fk 103[90 29[90 90 90 90 90 90 90 90 90 1[90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 1[90 9[90 90 90 90 2[90 90 90 90 90 1[90 90 90 90 90 90 90 90 90 2[90 90 1[90 9[90 90 90 90 90 90 1[90 90 90 1[90 4[90 33[{ TeXBase1Encoding ReEncodeFont}59 149.44 /Courier rf /Fl 135[59 3[37 1[52 1[66 66 66 96 37 59 1[37 66 2[59 1[59 1[66 11[96 1[66 2[81 1[89 111 5[96 3[89 81 19[33 46[{ TeXBase1Encoding ReEncodeFont}23 132.835 /Times-Italic rf /Fm 134[66 66 96 66 66 37 52 44 1[66 66 66 103 37 66 37 37 66 66 44 59 66 59 66 59 3[44 1[44 2[96 125 96 96 81 74 89 1[74 96 96 118 81 96 1[44 96 96 74 81 96 89 89 96 6[37 66 66 66 66 66 66 66 66 66 66 37 33 44 33 2[44 44 37[74 2[{TeXBase1Encoding ReEncodeFont}66 132.835 /Times-Roman rf /Fn 134[66 66 96 66 74 44 52 59 1[74 66 74 111 37 1[44 37 74 66 44 59 74 59 74 66 12[89 74 2[81 103 8[81 4[96 6[44 66 66 3[66 66 66 66 66 1[33 4[44 44 40[{TeXBase1Encoding ReEncodeFont}40 132.835 /Times-Bold rf /Fo 145[77 6[77 77 80[119 21[{}4 149.44 /CMSY9 rf /Fp 135[100 100 100 100 100 100 100 1[100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 7[100 1[100 100 100 100 100 100 1[100 100 100 2[100 1[100 100 100 1[100 1[100 1[100 12[100 2[100 2[100 100 100 1[100 100 100 4[100 35[{TeXBase1Encoding ReEncodeFont}49 166.044 /Courier rf /Fq 133[74 3[83 1[55 65 3[83 5[46 3[74 92 21[120 157 18[55 8[83 5[95 83 55 55 40[{ TeXBase1Encoding ReEncodeFont}16 166.044 /Times-Bold rf /Fr 133[65 74 74 111 74 83 46 65 65 1[83 83 83 120 46 74 1[46 83 83 46 74 83 74 83 83 8[101 1[101 120 92 83 101 1[101 120 111 138 92 2[55 1[120 101 101 120 111 101 101 3[112 2[55 83 83 1[83 83 83 1[83 83 1[46 42 55 42[83 2[{TeXBase1Encoding ReEncodeFont}56 166.044 /Times-Italic rf /Fs 134[120 3[133 80 93 106 1[133 120 133 199 66 2[66 2[80 106 133 106 133 120 14[173 1[146 6[93 7[173 13[120 120 120 49[{TeXBase1Encoding ReEncodeFont}24 239.103 /Times-Bold rf /Ft 134[83 1[116 1[91 1[83 58 2[91 91 1[42 2[42 91 91 1[83 91 1[91 17[100 4[108 16[50 58[{ TeXBase1Encoding ReEncodeFont}17 149.44 /Helvetica-Bold rf /Fu 134[66 66 100 66 75 42 58 58 75 75 75 75 108 42 66 1[42 75 75 42 66 75 66 75 75 8[91 124 91 108 83 75 91 108 91 108 100 124 83 2[50 1[108 91 91 108 100 91 91 5[50 50 5[75 75 75 75 75 1[37 50 37 4[50 36[75 2[{ TeXBase1Encoding ReEncodeFont}57 149.44 /Times-Italic rf /Fv 133[66 75 75 108 75 75 42 58 50 75 75 75 75 116 42 75 42 42 75 75 50 66 75 66 75 66 3[50 1[50 2[108 141 108 108 91 83 100 108 83 108 108 133 91 108 58 50 108 108 83 91 108 100 100 108 5[42 42 75 75 75 75 75 75 75 75 75 75 42 37 50 37 84 1[50 50 50 116 34[83 83 2[{ TeXBase1Encoding ReEncodeFont}75 149.44 /Times-Roman rf /Fw 134[75 75 1[75 83 50 58 66 1[83 75 83 124 42 83 1[42 83 75 1[66 83 66 83 75 9[149 108 1[100 83 2[91 116 108 141 100 2[58 1[116 1[100 108 108 1[108 18[37 46[{ TeXBase1Encoding ReEncodeFont}37 149.44 /Times-Bold rf /Fx 104[166 83 1[74 74 24[74 83 83 120 83 83 46 65 55 83 83 83 83 129 46 83 46 46 83 83 55 74 83 74 83 74 3[55 1[55 2[120 157 120 120 101 92 111 1[92 120 120 148 101 120 1[55 120 120 92 101 120 111 111 120 153 74 4[46 83 83 83 83 83 83 83 83 83 83 46 42 55 42 2[55 55 55 5[55 29[92 92 2[{TeXBase1Encoding ReEncodeFont}77 166.044 /Times-Roman rf /Fy 87[66 49[88 100 55 78 78 3[100 4[55 100 2[88 1[88 1[100 11[144 111 6[166 35[66 66 40[{ TeXBase1Encoding ReEncodeFont}17 199.253 /Times-Italic rf /Fz 140[78 4[100 1[55 3[100 100 66 88 1[88 1[88 13[111 133 12[122 22[50 46[{TeXBase1Encoding ReEncodeFont}13 199.253 /Times-Roman rf /FA 134[100 100 144 100 111 66 78 88 1[111 100 111 166 55 111 66 55 111 100 66 88 111 88 111 100 9[199 1[144 133 111 144 1[122 155 1[188 1[155 1[78 155 155 122 133 144 144 133 144 8[100 100 100 100 100 100 100 100 2[50 66 3[66 66 1[166 34[111 111 2[{ TeXBase1Encoding ReEncodeFont}57 199.253 /Times-Bold rf /FB 138[191 115 1[153 2[172 191 1[96 2[96 191 172 1[153 191 1[191 172 16[210 2[325 77[{TeXBase1Encoding ReEncodeFont}15 344.375 /Times-Bold rf end %%EndProlog %%BeginSetup %%Feature: *Resolution 1200dpi TeXDict begin %%BeginPaperSize: a4 a4 %%EndPaperSize end %%EndSetup %%Page: 1 1 TeXDict begin 1 0 bop Black Black Black Black 2103 710 a FB(P)-7 b(ortable)87 b(Multithr)-6 b(eading)1507 981 y FA(The)50 b(Signal)f(Stack)h(T)-15 b(rick)51 b(F)-5 b(or)50 b(User)-7 b(-Space)50 b(Thr)l(ead)g(Cr)l(eation)3133 1416 y Fz(Ralf)h(S.)f(Engelschall)2296 1663 y Fy(T)-18 b(ec)m(hnisc)m(he)50 b(Univer)n(sit)3973 1664 y(\250)3957 1663 y(at)g(M)4344 1664 y(\250)4328 1663 y(unc)m(hen)g(\(TUM\))2217 1909 y Fx(rse@@engelschall.com,)39 b(http://www)-11 b(.engelschall.com)p Black Black 3622 2470 a Fw(Abstract)p Black Black 415 2744 a Fv(This)46 b(paper)h(describes)g(a)g(pragmatic)f(b)m(ut)g (portable)f(f)o(allback)g(approach)i(for)f(creating)g(and)g (dispatching)f(between)h(the)h(machine)415 2927 y(conte)n(xts)36 b(of)g(multiple)f(threads)h(of)h(e)n(x)n(ecution)e(on)h(Unix)g(systems) h(that)f(lack)h(a)g(dedicated)f(user)m(-space)i(conte)n(xt)d(switching) g(f)o(acility)-10 b(.)415 3109 y(Such)44 b(a)h(f)o(allback)e(approach)h (for)f(implementing)g(machine)h(conte)n(xts)f(is)h(a)h(vital)e(part)g (of)h(a)g(user)m(-space)h(multithreading)d(en)-6 b(viron-)415 3292 y(ment,)46 b(if)e(it)f(has)h(to)g(achie)l(v)n(e)h(maximum)f (portability)d(across)j(a)h(wide)f(range)g(of)f(Unix)h(\003a)m(v)m (ors.)67 b(The)44 b(approach)g(is)g(entirely)f(based)415 3475 y(on)37 b(standard)h(Unix)f(system)h(f)o(acilities)e(and)h(ANSI-C) h(language)f(features)h(and)g(especially)f(does)h(not)f(require)g(an)n (y)h(assembly)g(code)415 3657 y(or)54 b(platform)f(speci\002c)i(tricks) f(at)g(all.)98 b(The)55 b(most)f(interesting)e(issue)j(is)f(the)g (technique)g(of)g(creating)f(the)i(machine)f(conte)n(xt)g(for)415 3840 y(threads,)35 b(which)f(this)g(paper)h(e)n(xplains)e(in)h(detail.) 45 b(The)35 b(described)g(approach)f(closely)g(follo)l(ws)g(the)g (algorithm)f(as)i(implemented)f(by)415 4023 y(the)40 b(author)e(for)h(the)h(popular)e(user)m(-space)i(multithreading)d (library)h Fu(GNU)i(P)-12 b(ortable)39 b(Thr)-6 b(eads)40 b Fv(\()p Fu(GNU)f(Pth)p Fv(,)h([25)o(]\))g(which)f(this)f(w)o(ay)415 4205 y(quickly)e(g)o(ained)h(the)g(status)g(of)f(one)i(of)e(the)h(most) g(portable)f(user)m(-space)i(multithreading)d(libraries.)415 4487 y Ft(K)n(e)n(yw)m(or)m(ds:)43 b Fv(portability)-10 b(,)29 b(multithreading,)f(Unix,)j(POSIX,)e(SUSv2,)i(ANSI-C,)e(user)m (-space,)j(conte)n(xt)d(creation,)i(conte)n(xt)d(switch-)415 4670 y(ing,)37 b(signal)f(handler)-6 b(,)37 b(stack,)h(mak)o(econte)n (xt,)f(switchconte)n(xt,)e(sig)o(altstack,)i(setjmp,)g(longjmp.)415 4952 y Ft(Pub)o(lishing:)45 b Fv(Early)34 b(drafts)h(of)f(this)h(paper) g(were)g(distrib)m(uted)e(with)g(the)i(GNU)g(Pth)g(distrib)m(ution.)42 b(The)35 b(\002nal)g(release)h(v)n(ersion)e(w)o(as)415 5135 y(published)i(on)g(the)h(USENIX)h(Annual)e(T)-10 b(echnical)37 b(Conference,)h(June)f(18-23,)f(2000,)h(San)g(Die)n(go,)g (California,)f(USA.)0 5895 y Fs(1)239 b(Intr)l(oduction)0 6293 y FA(1.1)198 b(Multithr)l(eading)0 6634 y Fx(The)51 b(paradigm)g(of)g(programming)h(with)f(multiple)g(threads)g(of)0 6833 y(e)n(x)n(ecution)45 b(\(aka)h Fr(multithr)-6 b(eading)p Fx(\))46 b(is)g(already)g(a)g(v)n(ery)g(old)g(one)0 7032 y(and)k(dates)f(back)g(to)h(the)g(decades)e(of)i(programming)h(with)f Fr(co-)0 7232 y(r)-7 b(outines)39 b Fx([2,)h(3].)51 b(P)n(aradoxically) -11 b(,)39 b(the)h(use)g(of)g(threads)g(on)g(Unix)0 7431 y(platforms)i(did)g(not)f(become)g(popular)h(until)g(the)f(early)h (1990s.)0 7776 y FA(Multithr)l(eading)52 b(Adv)n(antages)0 8057 y Fx(Multithreading)68 b(can)f(pro)n(vide)h(man)n(y)f(bene\002ts)h (for)g(applica-)0 8256 y(tions)54 b(\(good)g(runtime)g(concurrenc)n(y) -11 b(,)56 b(parallel)e(programming)0 8455 y(techniques)61 b(can)g(be)g(implemented)h(more)f(easily)-11 b(,)66 b(the)c(popu-)0 8654 y(lar)44 b(procedural)h(programming)g(style)f(can)g(be)g(combined) g(with)0 8854 y(multiple)c(threads)f(of)h(e)n(x)n(ecution,)e Fr(etc.)p Fx(\))50 b(b)m(ut)39 b(the)h(most)f(interest-)0 9053 y(ing)52 b(ones)f(are)g(usually)g(performance)h(g)o(ains)f(and)h (reduced)f(re-)0 9252 y(source)36 b(consumption.)49 b(Because)34 b(in)j(contrast)f(to)g(multiprocess)0 9451 y(applications,)g (multithreaded)f(ones)g(usually)g(require)g(less)g(sys-)0 9651 y(tem)51 b(resources)f(\(mainly)h(memory\))h(and)e(their)i (internal)f(com-)0 9850 y(munication)72 b(part)g(can)f(le)l(v)n(erage)f (from)j(the)e(shared)g(address)0 10049 y(space.)0 10395 y FA(Multithr)l(eading)52 b(and)d(A)-5 b(pplications)0 10675 y Fx(Ne)l(v)n(ertheless)48 b(there)h(still)h(e)n(xist)f(just)g(a) h(fe)l(w)f(real)h(applications)0 10874 y(in)70 b(the)h(free)f(softw)n (are)g(w)n(orld)h(that)g(use)e(multithreading)i(for)0 11073 y(their)43 b(bene\002t,)f(although)g(their)h(application)f (domains)g(are)g(pre-)0 11272 y(destined)65 b(for)i(multithreading.)124 b(F)n(or)66 b(instance,)71 b(the)66 b(popular)p Black Black 4050 6293 a(Apache)79 b(webserv)n(er)f(as)h(of)h(v)n(ersion)f (1.3)g(still)h(uses)e(a)i(pre-)4050 6492 y(forking)70 b(process)e(model)h(for)g(serving)g(HTTP)h(requests,)75 b(al-)4050 6692 y(though)33 b(tw)n(o)h(e)n(xperiments)e(with)h (multithreaded)h(Apache)e(v)l(ari-)4050 6891 y(ants)44 b(in)h(1996)f(\(with)h Fr(r)n(sthr)-6 b(eads)43 b Fx([27]\))j(and)e (1998)g(\(with)h Fr(NSPR)4050 7090 y Fx([31]\))76 b(already)g(sho)l (wed)e(great)i(performance)f(boosts.)153 b(The)4050 7289 y(same)41 b(applies)g(to)g(man)n(y)g(similar)h(applications.)4299 7587 y(The)j(reason)f(for)i(this)e(restraint)h(mainly)g(is)g(that)g (for)g(a)g(long)4050 7787 y(time,)38 b(multithreading)g(f)n(acilities)f (under)h(Unix)f(were)g(rare.)50 b(The)4050 7986 y(situation)d(became)g (better)g(after)h(some)f(v)n(endors)f(lik)n(e)i Fr(Sun)f Fx(and)4050 8185 y Fr(DEC)41 b Fx(incorporated)d(threading)f(f)n (acilities)h(into)f(their)h(Unix)g(\003a-)4050 8384 y(v)m(ors)74 b(and)f Fr(POSIX)83 b Fx(standardized)73 b(a)h(threading)g Fr(Application)4050 8584 y(Pr)-7 b(o)n(gr)n(amming)38 b(Interface)h Fx(\(API\))h(\(aka)f Fr(Pthr)-6 b(eads)39 b Fx([1]\).)51 b(But)39 b(an)4050 8783 y(API)e(and)f(a)g(fe)l(w)f(v)n (endor)h(implementations)g(are)g(not)h(enough)f(to)4050 8982 y(ful\002ll)60 b(the)f(portability)g(requirements)g(of)g(modern)g (free)g(soft-)4050 9181 y(w)n(are)75 b(packages.)151 b(Here)75 b(stand-alone)g(and)g(really)g(portable)4050 9381 y(multithreading)42 b(en)-7 b(vironments)42 b(are)f(needed.)4299 9678 y(The)102 b(author)g(collected)f(and)g(e)l(v)l(aluated)f(o)n(v)n (er)h(twenty)4050 9878 y(\(mostly)88 b(user)m(-space\))e(a)m(v)l (ailable)h(multithreading)h(f)n(acilities)4050 10077 y(for)33 b(Unix)g(systems)e(\(see)h(T)-13 b(able)32 b(1\),)i(b)m(ut)e (only)h(a)f(fe)l(w)g(of)h(them)f(are)4050 10276 y(freely)57 b(a)m(v)l(ailable)e(and)i(sho)l(wed)e(to)i(be)f(really)h(portable.)96 b(And)4050 10475 y(e)l(v)n(en)41 b(the)h(mostly)g(portable)g(ones)f (suf)l(fered)h(from)h(the)f(f)n(act)g(that)4050 10675 y(the)n(y)64 b(partly)h(depend)f(on)g(assembly)f(code)h(or)h(platform)g (spe-)4050 10874 y(ci\002c)56 b(tricks)g(usually)g(related)f(to)i(the)f (creation)f(and)h(dispatch-)4050 11073 y(ing)51 b(of)g(the)f(indi)l (vidual)h(threads.)78 b(This)51 b(means)f(that)h(the)f(num-)4050 11272 y(ber)45 b(of)g(platforms)g(the)n(y)f(support)h(is)g(limited)g (and)g(applications)p Black 3858 11872 a(1)p Black eop end %%Page: 2 2 TeXDict begin 2 1 bop Black Black 0 638 a Fx(which)57 b(are)g(based)f(on)h(these)g(f)n(acilities)g(are)g(only)h(portable)f (to)0 838 y(those)44 b(platforms.)62 b(This)45 b(situation)g(is)g(not)g (satisf)n(actory)-11 b(,)45 b(so)g(ap-)0 1037 y(plication)53 b(authors)h(still)f(a)m(v)m(oid)g(the)g(use)g(of)g(multithreading)i(if) 0 1236 y(the)n(y)j(w)n(ant)g(to)g(\(or)h(ha)m(v)n(e)d(to\))j(achie)l(v) n(e)e(maximum)h(portability)0 1435 y(for)42 b(their)g(application.)249 1644 y(A)50 b(pragmatic)h(and)f(mostly)g(portable)h(f)n(allback)f (technique)0 1844 y(for)45 b(implementing)h(user)m(-space)d(threads)h (can)h(f)n(acilitate)g(wider)0 2043 y(use)c(of)h(multithreading)g(in)g (free)f(softw)n(are)h(applications.)0 2370 y FA(Ingr)l(edients)50 b(of)g(a)g(Thr)l(ead)0 2639 y Fx(A)i(Unix)f(process)g(has)g(man)n(y)g (ingredients,)j(b)m(ut)d(the)h(most)f(im-)0 2838 y(portant)64 b(ones)g(are)f(its)h(memory)h(mapping)f(table,)k(the)c(signal)0 3037 y(dispatching)39 b(table,)g(the)h(signal)f(mask,)g(the)g(set)g(of) h(\002le)f(descrip-)0 3236 y(tors)61 b(and)f(the)g(machine)g(conte)n (xt.)107 b(The)61 b(machine)f(conte)n(xt)g(in)0 3436 y(turn)54 b(consists)e(of)h(at)g(least)f(the)h(CPU)h(re)n(gisters)e (including)h(the)0 3635 y(program)43 b(counter)e(and)h(the)g(stack)f (pointer)-9 b(.)53 b(In)42 b(addition,)g(there)0 3834 y(can)56 b(be)h(light-weight)g(processes)e(\(L)-12 b(WP\))58 b(or)f(threads,)i(which)0 4033 y(usually)g(share)g(all)h(attrib)m(utes) g(with)f(the)h(underlying)g(\(hea)m(vy-)0 4233 y(weight\))42 b(process)e(e)n(xcept)h(for)h(the)f(machine)g(conte)n(xt.)0 4560 y FA(K)-5 b(er)m(nel-Space)52 b(vs.)61 b(User)-7 b(-Space)0 4828 y Fx(Those)55 b(L)-12 b(WPs)55 b(or)h(threads,)i(on)d (a)h(Unix)f(platform)h(classically)0 5028 y(can)69 b(be)g(implemented)g (either)h(in)f(k)n(ernel-space)g(or)h(in)f(user)m(-)0 5227 y(space.)153 b(When)75 b(implemented)h(in)g(k)n(ernel-space,)83 b(one)76 b(usu-)0 5426 y(ally)45 b(calls)f(them)h(L)-12 b(WPs)45 b(or)g(k)n(ernel)g(threads,)g(otherwise)f(\(user)m(-)0 5625 y(space\))38 b(threads.)49 b(If)40 b(threads)e(are)g(implemented)h (by)g(the)f(k)n(ernel,)0 5825 y(the)50 b(thread)f(conte)n(xt)g (switches)g(are)g(performed)h(by)g(the)g(k)n(ernel)0 6024 y(without)36 b(notice)g(by)f(the)h(application,)g(similar)g(to)g (the)f(dispatch-)0 6223 y(ing)68 b(of)g(processes.)128 b(If)68 b(threads)f(are)h(implemented)g(in)f(user)m(-)0 6422 y(space,)40 b(the)h(thread)g(conte)n(xt)g(switches)f(are)h (performed)h(usually)0 6622 y(by)64 b(an)f(application)h(library)g (without)g(notice)g(by)g(the)f(k)n(ernel.)0 6821 y(Additionally)-11 b(,)85 b(there)76 b(e)n(xist)f(h)o(ybrid)i(threading)f(approaches,)0 7020 y(where)58 b(typically)h(a)g(user)m(-space)e(library)j(binds)e (one)h(or)g(more)0 7219 y(user)m(-space)40 b(threads)h(to)h(one)f(or)h (more)f(k)n(ernel-space)g(L)-12 b(WPs.)0 7547 y FA(Thr)l(ead)50 b(Models)0 7815 y Fx(The)57 b(v)n(endor)f(threading)h(f)n(acilities)g (under)g Fr(Sun)g(Solaris)p Fx(,)j Fr(IBM)0 8014 y(AIX)p Fx(,)j Fr(DEC)f(T)-9 b(ru64)62 b Fx(\(formerly)i Fr(DIGIT)-8 b(AL)63 b(UNIX)70 b Fx(or)62 b Fr(OSF/1)p Fx(\))0 8214 y(and)42 b Fr(SGI)h(IRIX)52 b Fx(use)42 b(a)g Fq(M:N)h Fx(mapping)g([21,)f(30],)h Fr(i.e)n(.)p Fx(,)e Fq(M)h Fx(user)m(-)0 8413 y(space)67 b(threads)g(are)h(mapped)g(onto)g Fq(N)g Fx(k)n(ernel-space)g(L)-12 b(WPs.)0 8612 y(On)42 b(the)f(other)h(hand,)g Fr(LinuxThr)-6 b(eads)41 b Fx([29])h(under)g Fr(GNU/Linux)0 8811 y Fx(uses)34 b(a)i Fq(1:1)g Fx(mapping)f(and)h (pure)f(user)m(-space)f(implementations)0 9011 y(lik)n(e)42 b Fr(GNU)g(Pth)p Fx(,)g Fr(FSU)g(pthr)-6 b(eads)41 b Fx(or)h Fr(MIT)h(pthr)-6 b(eads)p Fx(,)40 b Fr(etc.)52 b Fx(use)41 b(a)0 9210 y Fq(M:1)h Fx(mapping)f([25)q(,)g(22,)g(23].)249 9419 y(From)82 b(no)l(w)f(on)g(we)f(focus)h(on)g(such)f Fq(M:1)i Fx(user)e(space)0 9618 y(threading)f(approaches,)87 b(where)79 b(one)g(or)h(more)f(user)g(space)0 9817 y(threads)34 b(are)g(implemented)g(inside)g(a)h(single)f(k)n(ernel)g(space)f(pro-)0 10017 y(cess.)46 b(The)32 b(e)n(x)n(ercise)e(is)h(to)h(implement)g (this)f(by)h(using)f(standard-)0 10216 y(ized)41 b(Unix)h(system)f(and) g(ANSI-C)h(language)f(f)n(acilities)g Fr(only)p Fx(.)0 10744 y FA(1.2)198 b(The)50 b(Exer)l(cise)0 11073 y Fx(As)45 b(we)h(ha)m(v)n(e)e(mentioned,)i(a)g(thread)g(shares)e(its)i(state)f (with)h(the)0 11272 y(underlying)57 b(process)f(e)n(xcept)g(for)i(the)f (machine)f(conte)n(xt.)97 b(So)p Black Black 4050 638 a(the)34 b(major)h(task)e(for)i(a)f(user)m(-space)e(threading)i(system) g(is)g(to)g(cre-)4050 838 y(ate)41 b(and)g(dispatch)g(those)g(machine)g (conte)n(xts.)4299 1037 y(In)35 b(practice,)g(the)f(second)g(major)h (task)f(it)h(has)f(to)g(do)h(is)f(to)h(en-)4050 1236 y(sure)42 b(that)h(no)g(thread)f(by)h(accident)f(blocks)g(the)h(whole)f (process)4050 1436 y(\(and)53 b(thereby)f(all)h(other)g(threads\).)84 b(Instead)52 b(when)h(an)f(opera-)4050 1635 y(tion)59 b(w)n(ould)h(block,)j(the)58 b(threading)i(library)f(should)g(suspend) 4050 1834 y(only)40 b(the)g(e)n(x)n(ecution)e(of)i(the)g(current)g (thread)g(and)f(in)h(the)g(mean-)4050 2033 y(time)h(dispatch)f(the)h (remaining)g(threads.)51 b(But)41 b(this)f(task)h(is)g(out-)4050 2233 y(side)61 b(the)g(scope)g(of)h(this)f(paper)g(\(see)g([11])h(for)g (details)f(about)4050 2432 y(this)47 b(task\).)68 b(W)-13 b(e)47 b(focus)f(only)i(on)f(the)g(aspect)g(of)g(machine)g(con-)4050 2631 y(te)n(xt)41 b(handling.)4050 3107 y FA(1.3)198 b(The)50 b(Curse)g(of)g(P)l(ortability)4050 3418 y Fx(Our)38 b(goal)f(of)h(real)f(portability)i(for)f(a)f(threading)h(system)f (causes)4050 3618 y(some)42 b(non-tri)l(vial)i(problems)f(which)f(ha)m (v)n(e)g(to)h(be)f(solv)n(ed.)54 b(The)4050 3817 y(most)d(ob)n(vious)f (one)h(is)f(that)h(dealing)g(with)g(machine)f(conte)n(xts)4050 4016 y(usually)68 b(suf)l(fers)f(from)i(portability)-11 b(,)75 b(because)67 b(it)h(is)g(a)g(highly)4050 4215 y(CPU)35 b(dependent)f(task)h(for)g(which)g(not)g(e)l(v)n(ery)f(Unix)h (\003a)m(v)m(or)f(pro-)4050 4415 y(vides)51 b(a)f(standardized)h(API.)g (Although)g(such)f(an)h(API)h(w)n(ould)4050 4614 y(be)e(not)g(too)h (hard)f(for)h(v)n(endors)e(to)h(pro)n(vide,)i(because)d(in)h(prin-)4050 4813 y(ciple)45 b(it)h(is)f(just)h(a)f(matter)h(of)g(switching)f(a)g (fe)l(w)g(CPU)h(re)n(gisters)4050 5012 y(\(mainly)c(the)f(program)i (counter)e(and)g(the)h(stack)e(pointer\).)4050 5312 y FA(Assembly)49 b(Code)g(Consider)l(ed)i(Harmful)4050 5561 y Fx(Additionally)-11 b(,)49 b(we)d(disallo)l(w)h(the)g(use)f(of)i (an)n(y)e(assembly)g(solu-)4050 5760 y(tions)39 b(or)h(platform)g (speci\002c)e(tricks,)i(because)d(then)j(the)f(thread-)4050 5959 y(ing)e(system)f(ag)o(ain)h(w)n(ould)g(be)f(only)h(semi-portable,) h Fr(i.e)n(.)p Fx(,)d(it)j(can)4050 6159 y(be)d(ported)h(to)g Fq(N)g Fx(platforms)g(b)m(ut)f(on)h(the)g Fq(\(N+1\))p Fx(th)g(platform)g(one)4050 6358 y(has)56 b(to)h(manually)f(adjust)g (or)h(e)l(v)n(en)e(e)n(xtend)h(it)h(to)g(w)n(ork)g(there,)4050 6557 y(too.)4299 6756 y(This)52 b(is)g(usually)g(not)g(acceptable,)h(e) l(v)n(en)d(if)j(it)f(also)f(mak)n(es)4050 6956 y(solving)43 b(the)g(problems)g(harder)-9 b(.)56 b(At)43 b(least)g(most)g(of)g(the)g (kno)l(wn)4050 7155 y(free)54 b(softw)n(are)h(user)m(-space)d (threading)j(systems)e([22,)h(23,)g(24])4050 7354 y(do)32 b(not)g(restrict)g(themself)g(to)g(this)g(and)f(therefore)i(are)e(just) h(semi-)4050 7553 y(portable.)51 b(But)42 b(real)f(portability)h (should)g(be)f(a)g(major)h(goal.)4050 8114 y Fs(2)239 b(Pr)l(oblem)60 b(Analysis)4050 8519 y FA(2.1)198 b(The)50 b(T)-18 b(ask)49 b(in)h(Detail)4050 8830 y Fx(Our)c(task)f(is)h(simple) f(in)h(principle:)61 b(pro)n(vide)46 b(an)f(API)h(and)g(cor)m(-)4050 9030 y(responding)h(implementation)h(for)f(creating)g(and)g (dispatching)4050 9229 y(machine)33 b(conte)n(xts)g(on)h(which)f(user)m (-space)f(threads)i(can)f(be)g(im-)4050 9428 y(plemented.)4050 9727 y FA(The)50 b(Pr)l(oposed)f(API)4050 9977 y Fx(In)38 b(detail)f(we)g(propose)g(the)g(follo)l(wing)h Fr(Application)g(Pr)-7 b(o)n(gr)n(am-)4050 10176 y(mer)n(s)41 b(Interface)g Fx(\(API\))h(for)g(the)g(machine)f(conte)n(xt)g(handling:)p Black 4241 10541 100 100 v Black 4465 10541 a(A)51 b(data)f(structure)h (of)g(type)g Fp(mctx)p 6531 10541 50 7 v 58 w(t)g Fx(which)f(holds)h (the)4465 10741 y(machine)41 b(conte)n(xt.)p Black 4241 11073 100 100 v Black 4465 11073 a(A)190 b(function)h(\223)p Fq(v)n(oid)g Fp(mctx)p 6496 11073 50 7 v 59 w(create\(mctx)p 7655 11073 V 55 w(t)4465 11301 y(*)4565 11272 y Fr(mctx)p Fx(,)54 b Fq(v)n(oid)f Fp(\()5430 11301 y(*)5530 11272 y Fr(sf)p 5651 11272 V 59 w(addr)t Fp(\)\()p Fq(v)n(oid)e(*)p Fp(\),)k Fq(v)n(oid)7264 11301 y Fp(*)7364 11272 y Fr(sf)p 7485 11272 V 59 w(ar)-6 b(g)p Fx(,)p Black 3858 11872 a(2)p Black eop end %%Page: 3 3 TeXDict begin 3 2 bop Black Black Black 0 479 7727 7 v 0 5355 7 4877 v 135 1757 a Fw(P)o(ackage)1106 1757 y gsave currentpoint currentpoint translate 45 neg rotate neg exch neg exch translate 1106 1757 a Fw(Genesis)1106 1757 y currentpoint grestore moveto 1106 1757 a 1488 1757 a gsave currentpoint currentpoint translate 45 neg rotate neg exch neg exch translate 1488 1757 a Fw(Latest)37 b(V)-15 b(ersion)1488 1757 y currentpoint grestore moveto 1488 1757 a 2020 1757 a gsave currentpoint currentpoint translate 45 neg rotate neg exch neg exch translate 2020 1757 a Fw(Implementation)37 b(Space)2020 1757 y currentpoint grestore moveto 2020 1757 a 2808 1757 a gsave currentpoint currentpoint translate 45 neg rotate neg exch neg exch translate 2808 1757 a Fw(Thr)m(ead)h(Mapping)2808 1757 y currentpoint grestore moveto 2808 1757 a 3123 1757 a gsave currentpoint currentpoint translate 45 neg rotate neg exch neg exch translate 3123 1757 a Fw(Acti)o(v)o(e)e(De)n(v)o(elopment)3123 1757 y currentpoint grestore moveto 3123 1757 a 3406 1757 a gsave currentpoint currentpoint translate 45 neg rotate neg exch neg exch translate 3406 1757 a Fw(Experimental)h(State)3406 1757 y currentpoint grestore moveto 3406 1757 a 3688 1757 a gsave currentpoint currentpoint translate 45 neg rotate neg exch neg exch translate 3688 1757 a Fw(Open)h(Sour)m(ce)3688 1757 y currentpoint grestore moveto 3688 1757 a 3970 1757 a gsave currentpoint currentpoint translate 45 neg rotate neg exch neg exch translate 3970 1757 a Fw(Pthr)m(ead)g(API)3970 1757 y currentpoint grestore moveto 3970 1757 a 4252 1757 a gsave currentpoint currentpoint translate 45 neg rotate neg exch neg exch translate 4252 1757 a Fw(Pthr)m(ead)h(Shar)m(ed)f (Memory)4252 1757 y currentpoint grestore moveto 4252 1757 a 4535 1757 a gsave currentpoint currentpoint translate 45 neg rotate neg exch neg exch translate 4535 1757 a Fw(Nati)o(v)o(e)d(API)4535 1757 y currentpoint grestore moveto 4535 1757 a 4817 1757 a gsave currentpoint currentpoint translate 45 neg rotate neg exch neg exch translate 4817 1757 a Fw(Nati)o(v)o(e)g(API)j Fo(\025)g Fw(Pthr)m(ead)g(API)4817 1757 y currentpoint grestore moveto 4817 1757 a 5099 1757 a gsave currentpoint currentpoint translate 45 neg rotate neg exch neg exch translate 5099 1757 a Fw(Nati)o(v)o(e)d(API)j(is)f(Pthr)m(ead)h(API)5099 1757 y currentpoint grestore moveto 5099 1757 a 5531 1757 a gsave currentpoint currentpoint translate 45 neg rotate neg exch neg exch translate 5531 1757 a Fw(Pr)m(eempti)o(v)o(e)g (Scheduling)5531 1757 y currentpoint grestore moveto 5531 1757 a 5813 1757 a gsave currentpoint currentpoint translate 45 neg rotate neg exch neg exch translate 5813 1757 a Fw(P)m(ortability)5813 1757 y currentpoint grestore moveto 5813 1757 a 6719 1757 a gsave currentpoint currentpoint translate 45 neg rotate neg exch neg exch translate 6719 1757 a Fw(Assembly)g(Code)6719 1757 y currentpoint grestore moveto 6719 1757 a 7018 1757 a gsave currentpoint currentpoint translate 45 neg rotate neg exch neg exch translate 7018 1757 a Fw(SysCall)e(Wrap.)7018 1757 y currentpoint grestore moveto 7018 1757 a 94 1819 7315 7 v 135 1947 a Fv(gnu-pth)504 b(1999)82 b(1.3.5)233 b(user)539 b(n:1)123 b(yes)84 b(no)132 b(yes)83 b(yes)g(no)133 b(yes)83 b(yes)g(no)282 b(no)132 b(full/mcsc+sjlj)81 b(no)149 b(partly)135 2113 y(cmu-l)o(wp)440 b(1984)82 b(1.4)345 b(user)539 b(n:1)123 b(yes)84 b(no)132 b(yes)83 b(no)132 b(-)233 b(yes)83 b(yes)g(partly)f(no)132 b(semi/\002x)n(ed:8)168 b(yes)100 b(no)135 2279 y(fsu-pthread)289 b(1992)82 b(3.13)270 b(user)539 b(n:1)123 b(no)133 b(no)f(yes)83 b(yes)g(no)133 b(no)f(-)232 b(-)382 b(yes)83 b(semi/\002x)n(ed:6)168 b(yes)100 b(yes)135 2445 y(mit-pthread)272 b(1993)82 b(1.8.9)233 b(user)539 b(n:1)123 b(no)133 b(no)f(yes)83 b(yes)g(no)133 b(no)f(-)232 b(-)382 b(yes)83 b(semi/\002x)n(ed:17)93 b(yes)100 b(yes)135 2611 y(ptl)812 b(1997)82 b(990622)g(user)539 b(n:1)123 b(no)133 b(no)f(yes)83 b(yes)g(no)133 b(no)f(-)232 b(-)382 b(yes)83 b(semi/\002x)n(ed:10)93 b(yes)100 b(yes)135 2777 y(linuxthreads)230 b(1997)82 b(2.1.2)233 b(user+k)o(ernel)82 b(1:1)123 b(yes)84 b(no)132 b(yes)83 b(yes)g(no)133 b(no)f(-)232 b(-)382 b(yes)83 b(semi/\002x)n(ed:5)168 b(yes)100 b(yes)135 2943 y(uthread)522 b(1998)82 b(3.4)345 b(user)539 b(n:1)123 b(yes)84 b(no)132 b(yes)83 b(yes)g(no)133 b(no)f(-)232 b(-)382 b(yes)83 b(semi/\002x)n(ed:2)168 b(yes)100 b(yes)135 3109 y(cthread)531 b(1991)82 b(991115)g(user)539 b(n:1)123 b(no)133 b(no)f(yes)83 b(no)132 b(-)233 b(yes)83 b(yes)g(no)282 b(no)132 b(semi/\002x)n(ed:8)168 b(yes)100 b(yes)135 3275 y(openthreads/qt)89 b(1996)82 b(2.0)345 b(user)539 b(n:1)123 b(no)133 b(no)f(yes)83 b(no)132 b(-)233 b(yes)83 b(no)132 b(no)282 b(no)132 b(semi/\002x)n(ed:9)168 b(yes)100 b(no)135 3441 y(rt++/qt)552 b(1996)82 b(1.0)345 b(user)539 b(n:1)123 b(no)133 b(no)f(yes)83 b(no)132 b(-)233 b(yes)83 b(yes)g(no)282 b(no)132 b(semi/\002x)n(ed:9)168 b(yes)100 b(no)135 3607 y(rsthreads)431 b(1996)82 b(980331)g(user)539 b(n:1)123 b(no)133 b(yes)83 b(yes)g(no)132 b(-)233 b(yes)83 b(no)132 b(no)282 b(no)132 b(semi/\002x)n(ed:9)168 b(yes)100 b(no)135 3773 y(pcthread)456 b(1996)82 b(1.0)345 b(user)539 b(n:1)123 b(no)133 b(yes)83 b(yes)g(yes)g(no)133 b(no)f(-)232 b(-)382 b(yes)83 b(semi/\002x)n(ed:1)168 b(yes)100 b(no)135 3939 y(bbthreads)389 b(1996)82 b(0.3)345 b(k)o(ernel)415 b(1:1)123 b(no)133 b(yes)83 b(yes)g(no)132 b(-)233 b(yes)83 b(no)132 b(-)382 b(yes)83 b(semi/\002x)n(ed:1)168 b(yes)100 b(no)135 4105 y(jkthreads)422 b(1998)82 b(1.2)345 b(k)o(ernel)415 b(1:1)123 b(no)133 b(yes)83 b(yes)g(no)132 b(-)233 b(yes)83 b(no)132 b(-)382 b(yes)83 b(semi/\002x)n(ed:1)168 b(yes)100 b(no)135 4271 y(nthreads)464 b(1997)82 b(970604)g(user)539 b(n:1)123 b(no)133 b(yes)83 b(yes)g(no)132 b(-)233 b(yes)83 b(no)132 b(-)382 b(no)132 b(semi/\002x)n(ed:9)168 b(yes)100 b(partly)135 4437 y(re)n(xthreads)350 b(1993)82 b(930614)g(user)539 b(n:1)123 b(no)133 b(yes)83 b(yes)g(no)132 b(-)233 b(yes)83 b(no)132 b(-)382 b(no)132 b(semi/\002x)n(ed:4)168 b(yes)100 b(no)135 4603 y(coro)705 b(1999)82 b(1.0.3)233 b(user)539 b(n:1)123 b(no)133 b(yes)83 b(yes)g(no)132 b(-)233 b(yes)83 b(no)132 b(-)382 b(no)132 b(semi/\002x)n(ed:1)168 b(yes)100 b(no)135 4769 y(greenthreads)207 b(1995)82 b(1.2)345 b(user)539 b(n:1)123 b(no)133 b(no)f(no)g(no)g(-)233 b(yes)83 b(yes)g(-)382 b(yes)83 b(full/mcsc)349 b(no)149 b(no)135 4935 y(solaris-pthread)81 b(NN)166 b(2.7)345 b(user+k)o(ernel)82 b(n:m)g(yes)i(no)132 b(no)g(yes)83 b(yes)h(yes)f(yes)g(no)282 b(yes)83 b(NN)690 b(NN)83 b(yes)135 5101 y(tru64-pthread)155 b(NN)166 b(5.0)345 b(user+k)o(ernel)82 b(n:m)g(yes)i(no)132 b(no)g(yes)83 b(yes)h(no)132 b(no)g(no)282 b(yes)83 b(NN)690 b(NN)83 b(yes)135 5267 y(aix-pthread)289 b(NN)166 b(4.3)345 b(user+k)o(ernel)82 b(1:1)123 b(yes)84 b(no)132 b(no)g(yes)83 b(yes)h(no)132 b(no)g(no)282 b(yes)83 b(NN)690 b(NN)83 b(yes)p 7719 5355 7 4877 v 0 5362 7727 7 v 0 5537 a Fn(T)-12 b(able)33 b(1:)133 b Fm(Summary)42 b(of)f(e)m(v)m(aluated)g(multithreading)i (packages)e(and)g(some)g(of)g(their)g(determined)h(characteristics.)63 b(Notice)41 b(that)f(mostly)h(all)g(packages)g(contain)589 5673 y(assembly)34 b(code)g(and)g(are)f(just)g(semi-portable,)i Fl(i.e)n(.)p Fm(,)f(the)n(y)g(support)h(only)f(a)f(\002x)n(ed)h(set)f (of)h(platforms)g(and)g(do)g(not)g(automatically)h(adjust)e(for)h(ne)m (w)f(ones.)p Black 415 6199 a Fq(v)n(oid)764 6228 y Fp(*)864 6199 y Fr(sk)p 1013 6199 50 7 v 59 w(addr)p Fx(,)47 b Fq(size)p 1734 6199 V 59 w(t)f Fr(sk)p 2033 6199 V 59 w(size)p Fp(\))p Fx(\224)f(which)h(creates)f(and)415 6399 y(initializes)35 b(a)g(machine)g(conte)n(xt)g(structure)g(in)g Fr(mctx)h Fx(with)415 6598 y(a)51 b(start)g(function)g Fr(sf)p 1602 6598 V 60 w(addr)p Fx(,)h(a)f(start)f(function)i(ar)m (gument)415 6797 y Fr(sf)p 536 6797 V 60 w(ar)-6 b(g)p Fx(,)73 b(and)68 b(a)f(stack)g(starting)h(at)g Fr(sk)p 2687 6797 V 59 w(addr)p Fx(,)74 b(which)67 b(is)415 6996 y Fr(sk)p 564 6996 V 59 w(size)41 b Fx(bytes)g(in)g(size.)p Black 191 7480 100 100 v Black 415 7480 a(A)46 b(function)g(\223)p Fq(v)n(oid)h Fp(mctx)p 2013 7480 50 7 v 58 w(save\(mctx)p 2971 7480 V 56 w(t)3163 7509 y(*)3263 7480 y Fr(mctx)p Fp(\))p Fx(\224)415 7679 y(which)56 b(sa)m(v)n(es)e(the)i(current)g (machine)f(conte)n(xt)h(into)g(the)415 7878 y(machine)41 b(conte)n(xt)g(structure)h Fr(mctx)p Fx(.)p Black 191 8362 100 100 v Black 415 8362 a(A)157 b(function)h(\223)p Fq(v)n(oid)g Fp(mctx)p 2347 8362 50 7 v 58 w(restore\(mctx)p 3605 8362 V 55 w(t)415 8590 y(*)515 8561 y Fr(mctx)p Fp(\))p Fx(\224)81 b(which)h(restores)g(the)g(ne)l(w)f(machine)h(con-) 415 8760 y(te)n(xt)38 b(from)i(the)e(machine)h(conte)n(xt)f(structure)g Fr(mctx)p Fx(.)50 b(This)415 8960 y(function)64 b(does)f(not)h(return)g (to)g(the)g(caller)-9 b(.)117 b(Instead)63 b(it)415 9159 y(does)39 b(return)h(at)f(the)h(location)f(stored)h(in)f Fr(mctx)h Fx(\(which)f(is)415 9358 y(either)c Fr(sf)p 949 9358 V 60 w(addr)j Fx(from)d(a)g(pre)l(vious)f Fp(mctx)p 2829 9358 V 59 w(create)e Fx(call)415 9557 y(or)42 b(the)f(location)h (of)g(a)f(pre)l(vious)g Fp(mctx)p 2730 9557 V 58 w(save)f Fx(call\).)p Black 191 10041 100 100 v Black 415 10041 a(A)190 b(function)h(\223)p Fq(v)n(oid)g Fp(mctx)p 2446 10041 50 7 v 59 w(switch\(mctx)p 3605 10041 V 55 w(t)415 10269 y(*)515 10240 y Fr(mctx)p 839 10240 V 59 w(old)p Fx(,)40 b Fp(mctx)p 1592 10240 V 59 w(t)1780 10269 y(*)1880 10240 y Fr(mctx)p 2204 10240 V 59 w(ne)n(w)p Fp(\))p Fx(\224)f(which)h(switches)415 10439 y(from)123 b(the)f(current)h (machine)e(conte)n(xt)h(\(sa)m(v)n(ed)f(to)415 10639 y Fr(mctx)p 739 10639 V 59 w(old)59 b Fx(for)54 b(later)f(use\))g(to)h (a)f(ne)l(w)g(conte)n(xt)g(\(restored)415 10838 y(from)47 b Fr(mctx)p 1108 10838 V 59 w(ne)n(w)p Fx(\).)63 b(This)47 b(function)f(returns)g(only)g(to)g(the)415 11037 y(caller)81 b(if)g Fp(mctx)p 1457 11037 V 58 w(restore)d Fx(or)j Fp(mctx)p 2912 11037 V 59 w(switch)d Fx(is)415 11236 y(ag)o(ain)41 b(used)g(on)h Fr(mctx)p 1693 11236 V 59 w(old)p Fx(.)p Black Black 4050 6199 a FA(2.2)198 b(T)-18 b(echnical)51 b(P)l(ossibilities)4050 6513 y Fx(Poking)39 b(around)g(in)g(the)g(references)f(of)h(the)f(ANSI-C)i(language)4050 6712 y(reference)77 b(and)f(the)h(Unix)h(standards)e(sho)l(w)g(the)h (follo)l(wing)4050 6911 y(functions)42 b(on)f(which)g(an)h (implementation)g(can)f(be)g(based:)p Black 4241 7281 100 100 v Black 4465 7281 a(There)113 b(is)g(the)f Fp(ucontext)p Fx(\(3\))f(f)n(acility)i(with)g(the)4465 7480 y(functions)73 b Fp(getcontext)p Fx(\(3\),)78 b Fp(makecontext)p Fx(\(3\),)4465 7679 y Fp(swapcontext)p Fx(\(3\))67 b(and)i Fp(setcontext)p Fx(\(3\))e(which)4465 7879 y(conform)50 b(to)f(the)g Fr(Single)g(Unix)g(Speci\002cation)p Fx(,)h(V)-18 b(ersion)4465 8078 y(2)77 b(\()p Fr(SUSv2)g Fx([20)q(],)86 b(aka)76 b Fr(Unix95/98)p Fx(\).)159 b(Unfortunately)4465 8277 y(these)32 b(are)h(a)m(v)l(ailable)f(on)h(modern)g(Unix)h(platforms)f (only)-11 b(.)p Black 4241 8614 V Black 4465 8614 a(There)178 b(are)f(the)h Fp(jmp)p 6101 8614 50 7 v 59 w(buf)e Fx(based)h (functions)4465 8813 y Fp(setjmp)p Fx(\(3\))62 b(and)g Fp(longjmp)p Fx(\(3\))f(which)i(conform)h(to)4465 9012 y(ISO)83 b(9899:1990)g(\(ISO-C\))h(and)f(the)g Fp(sigjmp)p 7454 9012 V 57 w(buf)4465 9211 y Fx(based)145 b Fp(sigsetjmp)p Fx(\(3\))e(and)i Fp(siglongjmp)p Fx(\(3\))4465 9411 y(functions)39 b(which)g(conform)g(to)g(IEEE)h(Std1003.1-1988)4465 9610 y(\()p Fr(POSIX)7 b Fx(\),)43 b(and)e Fr(Single)g(Unix)g (Speci\002cation)p Fx(,)g(V)-18 b(ersion)40 b(2)4465 9809 y(\()p Fr(SUSv2)50 b Fx([20],)i(aka)d Fr(Unix95/98)p Fx(\).)76 b(The)50 b(\002rst)g(tw)n(o)g(func-)4465 10008 y(tions)37 b(are)f(a)m(v)l(ailable)g(really)g(on)h(all)g(Unix)g (platforms,)g(the)4465 10208 y(last)k(tw)n(o)h(are)f(a)m(v)l(ailable)g (only)h(on)f(some)g(of)h(them.)4465 10475 y(On)j(some)f(platforms)h Fp(setjmp)p Fx(\(3\))e(and)h Fp(longjmp)p Fx(\(3\))4465 10675 y(sa)m(v)n(e)91 b(and)h(restore)g(also)g(the)h(signal)f(mask)g (\(if)h(one)4465 10874 y(does)c(not)h(w)n(ant)g(this)g(semantics,)100 b(one)90 b(has)f(to)h(call)p 4465 11073 V 4515 11073 a Fp(setjmp)p Fx(\(3\))74 b(and)p 5708 11073 V 136 w Fp(longjmp)p Fx(\(3\))g(there\))j(while)f(on)4465 11272 y(others)106 b(one)h(has)f(to)g(e)n(xplicitly)h(use)e(the)i(superset)p Black 3858 11872 a(3)p Black eop end %%Page: 4 4 TeXDict begin 4 3 bop Black Black 415 638 a Fx(functions)66 b Fp(sigsetjmp)p Fx(\(3\))d(and)i Fp(siglongjmp)p Fx(\(3\))415 838 y(for)108 b(this.)250 b(In)107 b(our)h(discussion)f(we)g(can)g (assume)415 1037 y(that)75 b Fp(setjmp)p Fx(\(3\))f(and)g Fp(longjmp)p Fx(\(3\))g(sa)m(v)n(e)f(and)i(re-)415 1236 y(store)69 b(the)g(signal)f(mask,)75 b(because)68 b(if)h(this)g(is)g (not)g(the)415 1435 y(case)95 b(in)h(practice,)108 b(one)95 b(easily)h(can)f(replace)g(them)415 1635 y(with)54 b Fp(sigsetjmp)p Fx(\(3\))d(and)j Fp(siglongjmp)p Fx(\(3\))d(calls)415 1834 y(\(if)93 b(a)m(v)l(ailable\))f(or)h(\(if)g(not)g(a)m(v)l (ailable\))f(emulate)g(the)415 2033 y(missing)98 b(functionality)h (manually)f(with)g(additional)415 2233 y Fp(sigprocmask)p Fx(\(2\))c(calls)i(\(see)g Fp(pth)p 2877 2233 50 7 v 59 w(mctx.c)f Fx(in)415 2432 y Fr(GNU)41 b(Pth)h Fx([25)q(]\).)p Black 191 2772 100 100 v Black 415 2772 a(There)77 b(is)g(the)g (function)h Fp(sigaltstack)p Fx(\(2\))73 b(which)415 2971 y(conforms)i(to)g(the)f Fr(Single)h(Unix)f(Speci\002cation)p Fx(,)83 b(V)-18 b(er)m(-)415 3171 y(sion)69 b(2)h(\()p Fr(SUSv2)g Fx([20],)76 b(aka)69 b Fr(Unix95/98)p Fx(\))h(and)g(its)f (an-)415 3370 y(cestor)52 b(function)g Fp(sigstack)p Fx(\(2\))e(from)i Fr(4.2BSD)p Fx(.)g(The)415 3569 y(last)41 b(one)g(e)n(xists)f(only)i(on)f Fr(BSD)p Fx(-deri)l(v)n(ed)h (platforms,)f(b)m(ut)415 3768 y(the)f(\002rst)h(function)f(already)g(e) n(xists)f(on)h(all)h(current)f(Unix)415 3968 y(platforms.)0 4454 y FA(2.3)198 b(Maximum)50 b(P)l(ortability)g(Solution)0 4770 y Fx(The)141 b(maximum)h(portable)f(solution)g(ob)n(viously)g(is)g (to)0 4969 y(use)160 b(the)h(standardized)f Fp(makecontext)p Fx(\(3\))e(function)0 5168 y(to)176 b(create)f(threads)g(and)g Fp(switchcontext)p Fx(\(3\))d(or)0 5367 y Fp(getcontext)p Fx(\(3\)/)p Fp(setcontext)p Fx(\(3\))83 b(to)90 b(dispatch)e(them.)0 5567 y(And)65 b(actually)g(these)g(are)g(the)h(preferred)f(functions)h (modern)0 5766 y(user)m(-space)52 b(multithreading)j(systems)e(are)h (using.)88 b(W)-13 b(e)53 b(could)0 5965 y(easily)78 b(implement)i(our)f(proposed)g(API)h(as)e(follo)l(wing)i(\(all)0 6164 y(error)42 b(checks)e(omitted)i(for)g(better)g(readability\):)p Black Black 0 6490 a Fk(/)90 6516 y(*)306 6490 y Fj(mac)o(hine)c (context)e(data)g(structure)2308 6516 y Fk(*)2398 6490 y(/)0 6640 y Fi(typedef)87 b(struct)h Fk(mctx_st)f Fo(f)359 6789 y Fk(ucontext_t)f(uc;)0 6939 y Fo(g)j Fk(mctx_t;)0 7238 y(/)90 7264 y(*)306 7238 y Fj(sav)n(e)38 b(mac)o(hine)f(context) 1713 7264 y Fk(*)1803 7238 y(/)0 7387 y Fi(#define)87 b Fk(mctx_save\(mctx\))e Fo(n)359 7536 y Fk(\()p Fi(void)p Fk(\)getcontext\(&\(mctx\)->uc)o(\))0 7835 y(/)90 7861 y(*)306 7835 y Fj(restore)38 b(mac)o(hine)f(context)1873 7861 y Fk(*)1963 7835 y(/)0 7985 y Fi(#define)87 b Fk (mctx_restore\(mctx\))d Fo(n)359 8134 y Fk(\()p Fi(void)p Fk(\)setcontext\(&\(mctx\)->uc)o(\))0 8433 y(/)90 8459 y(*)306 8433 y Fj(switc)o(h)37 b(mac)o(hine)g(context)1838 8459 y Fk(*)1928 8433 y(/)0 8583 y Fi(#define)87 b Fk (mctx_switch\(mctx_old,mctx_new\))80 b Fo(n)359 8732 y Fk(\()p Fi(void)p Fk(\)swapcontext\(&\(\(mctx_ol)o(d\)-)o(>uc)o(\),)g Fo(n)1973 8881 y Fk(&\(\(mctx_new\)->uc\)\))0 9180 y(/)90 9206 y(*)306 9180 y Fj(create)38 b(mac)o(hine)f(context)1823 9206 y Fk(*)1913 9180 y(/)0 9330 y Fi(void)88 b Fk(mctx_create\()359 9479 y(mctx_t)986 9505 y(*)1076 9479 y(mctx,)359 9629 y Fi(void)g Fk(\()897 9655 y(*)987 9629 y(sf_addr\)\()p Fi(void)2242 9655 y Fk(*)2332 9629 y(\),)g Fi(void)3049 9655 y Fk(*)3139 9629 y(sf_arg,)359 9778 y Fi(void)807 9804 y Fk(*)897 9778 y(sk_addr,)f(size_t)g(sk_size\))0 9927 y Fo(f)359 10226 y Fk(/)449 10252 y(*)665 10226 y Fj(f)o(etc)o(h)36 b(current)j(context)2039 10252 y Fk(*)2129 10226 y(/)359 10376 y(getcontext\(&\(mctx->uc\)\);)359 10675 y(/)449 10701 y(*)665 10675 y Fj(adjust)d(to)h(new)g(context)2055 10701 y Fk(*)2145 10675 y(/)359 10824 y(mctx->uc.uc_link)981 b(=)89 b(NULL;)359 10974 y(mctx->uc.uc_stack.ss_sp)351 b(=)89 b(sk_addr;)359 11123 y(mctx->uc.uc_stack.ss_size)171 b(=)89 b(sk_size;)359 11272 y(mctx->uc.uc_stack.ss_flags)81 b(=)89 b(0;)p Black Black 4409 788 a(/)4499 814 y(*)4715 788 y Fj(mak)l(e)37 b(new)g(context)5905 814 y Fk(*)5995 788 y(/)4409 937 y(makecontext\(&\(mctx->uc\),)5485 1087 y(sf_addr,)87 b(1,)i(sf_arg\);)4409 1236 y Fi(return)p Fk(;)4050 1386 y Fo(g)4050 1762 y Fx(Unfortunately)36 b(there)f(are)g(still)g(lots)h(of)f(Unix)h(platforms)f(where)4050 1961 y(this)85 b(approach)h(cannot)f(be)g(used,)96 b(because)84 b(the)h(standard-)4050 2160 y(ized)80 b Fp(ucontext)p Fx(\(3\))e(API)j(is)f(not)h(pro)n(vided)f(by)g(the)g(v)n(en-)4050 2360 y(dor)-9 b(.)162 b(Actually)78 b(the)g(platform)i(test)e(results)g (for)h Fr(GNU)e(Pth)4050 2559 y Fx(\(see)58 b(T)-13 b(able)59 b(2)g(belo)l(w\))g(sho)l(wed)f(that)h(only)g(7)g(of)h(21)f(success-) 4050 2758 y(fully)54 b(tested)f(Unix)g(\003a)m(v)m(ors)g(pro)n(vided)h (the)f(standardized)g(API)4050 2957 y(\()p Fp(makecontext)p Fx(\(3\),)43 b Fr(etc.)p Fx(\).)62 b(On)46 b(all)f(other)h(platforms,)g Fr(GNU)4050 3157 y(Pth)64 b Fx(w)n(as)f(forced)h(to)g(use)e(the)i(f)n (allback)g(approach)f(of)g(imple-)4050 3356 y(menting)53 b(the)g(machine)g(conte)n(xt)g(as)f(we)h(will)g(describe)g(in)g(the) 4050 3555 y(follo)l(wing.)93 b(Ob)n(viously)54 b(this)i(f)n(allback)f (approach)g(has)f(to)i(use)4050 3754 y(the)73 b(remaining)h(technical)f (possibilities)g(\()p Fp(sigsetjmp)p Fx(\(3\),)4050 3954 y Fr(etc.)p Fx(\).)p 4050 4114 3798 7 v 4050 7921 7 3807 v 4206 4274 a Fn(Operating)34 b(System)p 5674 4322 7 158 v 552 w(Ar)n(chitectur)n(e\(s\))p 6901 4322 V 368 w(mcsc)p 7387 4322 V 205 w(sjlj)4206 4432 y Fm(FreeBSD)f(2.x/3.x)p 5674 4479 V 679 w(Intel)p 6901 4479 V 984 w(no)p 7387 4479 V 354 w(yes)4206 4590 y(FreeBSD)g(3.x)p 5674 4637 V 881 w(Intel,)h(Alpha)p 6901 4637 V 593 w(no)p 7387 4637 V 354 w(yes)4206 4748 y(NetBSD)e(1.3/1.4)p 5674 4795 V 724 w(Intel,)i(PPC,)f(M68K)p 6901 4795 V 268 w(no)p 7387 4795 V 354 w(yes)4206 4905 y(OpenBSD)g(2.5/2.6)p 5674 4953 V 628 w(Intel,)h(SP)-12 b(ARC)p 6901 4953 V 507 w(no)p 7387 4953 V 354 w(yes)4206 5063 y(BSDI)33 b(4.0)p 5674 5110 V 1073 w(Intel)p 6901 5110 V 984 w(no)p 7387 5110 V 354 w(yes)4206 5221 y(Linux)i(2.0.x)g(glibc)e(1.x/2.0)p 5674 5268 V 259 w(Intel,)h(SP)-12 b(ARC,)32 b(PPC)p 6901 5268 V 205 w(no)p 7387 5268 V 354 w(yes)4206 5379 y(Linux)j(2.2.x)g (glibc)e(2.0/2.1)p 5674 5426 V 259 w(Intel,)h(Alpha,)g(ARM)p 6901 5426 V 223 w(no)p 7387 5426 V 354 w(yes)4206 5536 y(Sun)g(SunOS)g(4.1.x)p 5674 5584 V 660 w(SP)-12 b(ARC)p 6901 5584 V 817 w(no)p 7387 5584 V 354 w(yes)4206 5694 y(Sun)34 b(Solaris)f(2.5/2.6/2.7)p 5674 5741 V 363 w(SP)-12 b(ARC)p 6901 5741 V 817 w(yes)p 7387 5741 V 309 w(yes)4206 5852 y(SCO)33 b(UnixW)-11 b(are)34 b(2.x/7.x)p 5674 5899 V 340 w(Intel)p 6901 5899 V 984 w(yes)p 7387 5899 V 309 w(yes)4206 6010 y(SCO)f(OpenServ)n(er)h(5.0.x)p 5674 6057 V 353 w(Intel)p 6901 6057 V 984 w(no)p 7387 6057 V 354 w(yes)4206 6167 y(IBM)f(AIX)g(4.1/4.2/4.3)p 5674 6215 V 452 w(RS6000,)i(PPC)p 6901 6215 V 495 w(yes)p 7387 6215 V 309 w(yes)4206 6325 y(HP)e(HPUX)g(9.10/10.20)p 5674 6372 V 411 w(HPP)-12 b(A)p 6901 6372 V 899 w(no)p 7387 6372 V 354 w(yes)4206 6483 y(HP)33 b(HPUX)g(11.0)p 5674 6530 V 745 w(HPP)-12 b(A)p 6901 6530 V 899 w(yes)p 7387 6530 V 309 w(yes)4206 6641 y(SGI)33 b(IRIX)g(5.3)p 5674 6688 V 856 w(MIPS)g(32/64)p 6901 6688 V 583 w(no)p 7387 6688 V 354 w(yes)4206 6798 y(SGI)g(IRIX)g(6.2/6.5)p 5674 6846 V 654 w(MIPS)g(32/64)p 6901 6846 V 583 w(yes)p 7387 6846 V 309 w(yes)4206 6956 y(ISC)g(4.0)p 5674 7003 V 1169 w(Intel)p 6901 7003 V 984 w(no)p 7387 7003 V 354 w(yes)4206 7114 y(Apple)h(MacOS)f(X)p 5674 7161 V 681 w(PPC)p 6901 7161 V 990 w(no)p 7387 7161 V 354 w(yes)4206 7271 y(DEC)g(OSF1/T)-5 b(ru64)35 b(4.0/5.0)p 5674 7319 V 208 w(Alpha)p 6901 7319 V 903 w(yes)p 7387 7319 V 309 w(yes)4206 7429 y(SNI)e(ReliantUNIX)p 5674 7477 V 611 w(MIPS)p 6901 7477 V 917 w(yes)p 7387 7477 V 309 w(yes)4206 7587 y(Amig)o(aOS)p 5674 7634 V 1044 w(M68K)p 6901 7634 V 881 w(no)p 7387 7634 V 354 w(yes)p 7841 7921 7 3807 v 4050 7928 3798 7 v 4050 8102 a Fn(T)-12 b(able)33 b(2:)133 b Fm(Summary)43 b(of)e(operating)i(system)e(support.)66 b(The)42 b(le)m(v)n(el)e(and)i(type)4639 8238 y(of)k(support)h(found)h (on)e(each)g(tested)f(operating)i(system.)78 b Fh(mcsc)p Fm(:)4639 8374 y(functional)331 b Fh(makecontext)p Fm(\(3\)/)p Fh(switchcontext)p Fm(\(3\),)4639 8505 y Fh(sjlj)p Fm(:)165 b(functional)98 b Fh(setjmp)p Fm(\(3\)/)p Fh(longjmp)p Fm(\(3\))c(or)i Fh(sig-)4639 8635 y(setjmp)p Fm(\(3\)/)p Fh(siglongjmp)p Fm(\(3\).)51 b(See)37 b(\002le)g Fh(PORTING)e Fm(in)j Fl(GNU)4639 8766 y(Pth)c Fm([25)q(])f(for)i(more)f(details.) 4050 9199 y FA(2.4)198 b(Remaining)50 b(P)l(ossibilities)4050 9515 y Fx(Our)g(problem)f(can)g(be)g(di)l(vided)h(into)f(tw)n(o)h (parts,)h(an)e(easy)f(one)4050 9715 y(and)41 b(a)h(dif)l(\002cult)g (one.)4050 10022 y FA(The)50 b(Easy)e(P)n(art)4050 10276 y Fx(That)59 b Fp(setjmp)p Fx(\(3\))f(and)h Fp(longjmp)p Fx(\(3\))e(can)h(be)h(used)f(to)h(im-)4050 10475 y(plement)43 b(user)m(-space)e(threads)h(is)g(commonly)h(kno)l(wn)g([24,)f(27,)4050 10675 y(28].)116 b(Mostly)62 b(all)h(older)g(portable)g(user)m(-space)e (threading)i(li-)4050 10874 y(braries)53 b(are)f(based)g(on)h(them,)i (although)e(some)f(problems)h(are)4050 11073 y(kno)l(wn)i(with)h(these) f(f)n(acilities)g(\(see)g(belo)l(w\).)93 b(So)55 b(it)h(becomes)4050 11272 y(clear)g(that)h(we)f(also)g(ha)m(v)n(e)f(to)i(use)e(these)h (functions)h(and)f(base)p Black 3858 11872 a(4)p Black eop end %%Page: 5 5 TeXDict begin 5 4 bop Black Black 0 638 a Fx(our)58 b(machine)g(conte)n (xt)g(\()p Fp(mctx)p 1910 638 50 7 v 58 w(t)p Fx(\))g(on)g(their)h Fp(jmp)p 3068 638 V 58 w(buf)e Fx(data)0 838 y(structure.)249 1038 y(W)-13 b(e)77 b(immediately)h(recognize)f(that)g(this)h(w)n(ay)f (we)g(ha)m(v)n(e)0 1237 y(at)82 b(least)f(solv)n(ed)g(the)h (dispatching)g(problem,)92 b(because)81 b(our)0 1437 y Fp(mctx)p 410 1437 V 58 w(save)p Fx(,)146 b Fp(mctx)p 1456 1437 V 58 w(restore)124 b Fx(and)i Fp(mctx)p 3104 1437 V 58 w(switch)0 1636 y Fx(functions)67 b(can)g(be)g(easily)g (implemented)g(with)h Fp(setjmp)p Fx(\(3\))0 1835 y(and)41 b Fp(longjmp)p Fx(\(3\).)0 2137 y FA(The)50 b(Dif\002cult)h(P)n(art)0 2389 y Fx(Ne)l(v)n(ertheless,)j(the)f(dif)l(\002cult)i(problem)e(of)h (ho)l(w)g(to)f(create)g(the)0 2588 y(machine)62 b(conte)n(xt)g (remains.)114 b(Ev)n(en)62 b(kno)l(wing)g(that)h(our)g(ma-)0 2787 y(chine)54 b(conte)n(xt)h(is)f Fp(jmp)p 1431 2787 V 59 w(buf)g Fx(based)g(is)g(no)h(adv)l(antage)f(to)h(us.)0 2987 y(A)k Fp(jmp)p 489 2987 V 58 w(buf)f Fx(has)h(to)g(be)g(treated)f (by)h(us)g(as)f(an)h(opaque)f(data)0 3186 y(structure)45 b(\227)g(for)h(portability)g(reasons.)61 b(The)45 b(only)g(operations)0 3385 y(we)51 b(can)f(perform)i(on)f(it)g(are)g Fp(setjmp)p Fx(\(3\))e(and)i Fp(longjmp)p Fx(\(3\))0 3584 y(calls,)89 b(of)81 b(course.)166 b(Additionally)-11 b(,)91 b(we)80 b(are)g(forced)g(to)h(use)0 3784 y Fp(sigaltstack)p Fx(\(3\))36 b(for)j(our)h(stack)e(manipulations,)h(because)0 3983 y(it)k(is)f(the)h(only)g(portable)f(function)h(which)g(actually)f (deals)g(with)0 4182 y(stacks.)249 4382 y(So)67 b(it)h(is)e(clear)h (that)g(our)g(implementation)h(for)f Fp(mctx)p 3602 4382 V 58 w(-)0 4582 y(create)72 b Fx(has)i(to)h(play)g(a)f(fe)l(w)h(tricks) f(to)h(use)f(a)g Fp(jmp)p 3402 4582 V 59 w(buf)0 4781 y Fx(for)92 b(passing)e(e)n(x)n(ecution)g(control)h(to)g(an)g (arbitrary)h(startup)0 4980 y(routine.)168 b(And)81 b(our)g(approach)f (has)g(to)g(be)h(careful)f(to)h(en-)0 5179 y(sure)104 b(that)g(it)g(does)g(not)g(suf)l(fer)g(from)h(une)n(xpected)e(side-)0 5379 y(ef)l(fects.)263 b(It)112 b(should)h(be)f(also)f(ob)n(vious)h (that)g(we)g(can-)0 5578 y(not)e(ag)o(ain)f(e)n(xpect)g(to)g(\002nd)h (an)g(easy)e(solution)i(\(as)f(for)0 5777 y Fp(mctx)p 410 5777 V 58 w(save)p Fx(,)g Fp(mctx)p 1419 5777 V 58 w(restore)94 b Fx(and)i Fp(mctx)p 3007 5777 V 58 w(switch)p Fx(\),)0 5976 y(because)59 b Fp(setjmp)p Fx(\(3\))h(and)g Fp(sigaltstack)p Fx(\(3\))e(cannot)i(be)0 6176 y(tri)l(vially)42 b(combined)g(to)f(form)i Fp(mctx)p 2205 6176 V 58 w(create)p Fx(.)0 6742 y Fs(3)239 b(Implementation)0 7116 y Fx(As)48 b(we)f(ha)m(v)n(e)g(already)h(discussed,)g(our)g(implementation)h(con-) 0 7315 y(tains)62 b(an)f(easy)h(part)g(\()p Fp(mctx)p 1737 7315 V 58 w(save)p Fx(,)k Fp(mctx)p 2703 7315 V 58 w(restore)59 b Fx(and)0 7514 y Fp(mctx)p 410 7514 V 58 w(switch)p Fx(\))66 b(and)h(a)g(dif)l(\002cult)h(part)g(\()p Fp(mctx)p 3007 7514 V 58 w(create)p Fx(\).)0 7713 y(Let)47 b(us)g(start)g(with)g(the)g(easy)f(part,)j(whose)d(implementation)h(is) 0 7913 y(ob)n(vious)56 b(\(all)h(error)g(checks)e(ag)o(ain)h(omitted)h (for)g(better)g(read-)0 8112 y(ability\):)p Black Black 0 8433 a Fk(/)90 8459 y(*)306 8433 y Fj(mac)o(hine)38 b(context)e(data)g(structure)2308 8459 y Fk(*)2398 8433 y(/)0 8583 y Fi(typedef)87 b(struct)h Fk(mctx_st)f Fo(f)359 8732 y Fk(jmp_buf)g(jb;)0 8881 y Fo(g)i Fk(mctx_t;)0 9180 y(/)90 9206 y(*)306 9180 y Fj(sav)n(e)38 b(mac)o(hine)f(context) 1713 9206 y Fk(*)1803 9180 y(/)0 9330 y Fi(#define)87 b Fk(mctx_save\(mctx\))e Fo(n)359 9479 y Fk(\()p Fi(void)p Fk(\)setjmp\(\(mctx\)->jb\))0 9778 y(/)90 9804 y(*)306 9778 y Fj(restore)38 b(mac)o(hine)f(context)1873 9804 y Fk(*)1963 9778 y(/)0 9927 y Fi(#define)87 b Fk(mctx_restore\(mctx\))d Fo(n)359 10077 y Fk(longjmp\(\(mctx\)->jb,)f(1\))0 10376 y(/)90 10402 y(*)306 10376 y Fj(switc)o(h)37 b(mac)o(hine)g(context) 1838 10402 y Fk(*)1928 10376 y(/)0 10525 y Fi(#define)87 b Fk(mctx_switch\(mctx_old,mctx_new\))80 b Fo(n)359 10675 y Fi(if)89 b Fk(\(setjmp\(\(mctx_old\)->jb\))82 b(==)89 b(0\))g Fo(n)717 10824 y Fk(longjmp\(\(mctx_new\)->jb,)82 b(1\))0 11123 y(/)90 11149 y(*)306 11123 y Fj(create)38 b(mac)o(hine)f(context)1823 11149 y Fk(*)1913 11123 y(/)0 11272 y Fi(void)88 b Fk(mctx_create\()p Black Black 4409 638 a(mctx_t)5036 664 y(*)5126 638 y(mctx,)4409 788 y Fi(void)g Fk(\()4947 814 y(*)5037 788 y(sf_addr\)\()p Fi(void)6292 814 y Fk(*)6382 788 y(\),)g Fi(void)7099 814 y Fk(*)7189 788 y(sf_arg,)4409 937 y Fi(void)4857 963 y Fk(*)4947 937 y(sk_addr,)f(size_t)g(sk_size\))4050 1087 y Fo(f)4446 1236 y Fj(...initialization)34 b(of)j Fg(mctx)f Fj(to)h(be)g(\002lled)g(in...)4050 1386 y Fo(g)4050 1753 y Fx(There)49 b(is)g(one)g(subtle)g(b)m(ut)g(important)h(point)f (we)g(should)g(men-)4050 1952 y(tion:)93 b(The)62 b(use)f(of)h(the)g(C) g(pre-processor)f Fp(#define)f Fx(direc-)4050 2151 y(ti)l(v)n(e)73 b(to)g(implement)h Fp(mctx)p 5762 2151 V 58 w(save)p Fx(,)79 b Fp(mctx)p 6741 2151 V 59 w(restore)70 b Fx(and)4050 2351 y Fp(mctx)p 4460 2351 V 58 w(switch)81 b Fx(is)h(intentional.)175 b(F)n(or)83 b(technical)f(reasons)4050 2550 y(related)72 b(to)g Fp(setjmp)p Fx(\(3\))f(semantics)g(and)h Fp(return)e Fx(related)4050 2749 y(stack)i(beha)m(vior)h(\(which)g(we)f(will)h(e)n (xplain)g(later)g(in)g(detail\))4050 2948 y(we)51 b Fr(cannot)j Fx(implement)d(these)f(three)h(functions)g(\(at)g(least)g(not)4050 3148 y Fp(mctx)p 4460 3148 V 58 w(switch)p Fx(\))64 b(as)h(C)h (functions)f(if)h(we)g(w)n(ant)g(to)g(achie)l(v)n(e)4050 3347 y(maximum)55 b(portability)g(across)f(all)g(platforms.)91 b(Instead)54 b(the)n(y)4050 3546 y(ha)m(v)n(e)40 b(to)i(be)f (implemented)h(as)f(pre-processor)g(macros.)4050 4024 y FA(3.1)198 b(Algorithm)51 b(Ov)n(er)n(view)4050 4336 y Fx(The)g(general)g(idea)f(for)i Fp(mctx)p 5881 4336 V 58 w(create)c Fx(is)j(to)g(con\002gure)g(the)4050 4535 y(gi)l(v)n(en)31 b(stack)h(as)f(a)h(signal)f(stack)h(via)g Fp(sigaltstack)p Fx(\(2\),)e(send)4050 4735 y(the)67 b(current)g(process)f(a)h(signal)g(to)g(transfer)g(e)n(x)n(ecution)f (con-)4050 4934 y(trol)59 b(onto)f(this)g(stack,)i(sa)m(v)n(e)d(the)g (machine)h(conte)n(xt)f(there)h(via)4050 5133 y Fp(setjmp)p Fx(\(3\),)35 b(get)h(rid)g(of)h(the)f(signal)g(handler)g(scope)f(and)g (boot-)4050 5332 y(strap)41 b(into)h(the)g(startup)f(routine.)4299 5532 y(The)64 b(real)g(problem)h(in)f(this)g(approach)g(comes)f(from)i (the)4050 5731 y(signal)60 b(handler)g(scope)f(which)g(implies)h(v)l (arious)g(restrictions)4050 5931 y(on)44 b(Unix)f(platforms)h(\(the)g (signal)g(handler)f(scope)g(often)h(is)f(just)4050 6130 y(a)49 b(\003ag)g(in)h(the)f(process)f(control)i(block)f(\(PCB\))g (which)g(v)l(arious)4050 6329 y(system)61 b(calls,)k(lik)n(e)d Fp(sigaltstack)p Fx(\(2\),)g(check)e(before)i(al-)4050 6528 y(lo)l(wing)52 b(the)f(operation)h(\226)f(b)m(ut)g(because)f(it)i (is)g(part)f(of)h(the)f(pro-)4050 6728 y(cess)38 b(state)h(the)g(k)n (ernel)h(manages,)f(the)g(process)g(cannot)g(change)4050 6927 y(it)h(itself\).)51 b(As)39 b(we)g(will)h(see,)e(we)h(ha)m(v)n(e)f (to)i(perform)h(a)e(fe)l(w)g(tricks)4050 7126 y(to)44 b(get)g(rid)h(of)g(it.)59 b(The)44 b(second)g(main)g(problem)g(is:)57 b(ho)l(w)44 b(do)g(we)4050 7325 y(prepare)54 b(the)f(calling)h(of)g (the)f(start)h(routine)g(without)g(immedi-)4050 7525 y(ately)41 b(entering)h(it?)4050 8002 y FA(3.2)198 b(Algorithm)4050 8315 y Fx(The)42 b(input)g(to)g(the)f Fp(mctx)p 5558 8315 V 58 w(create)f Fx(function)i(is)f(the)g(machine)4050 8514 y(conte)n(xt)63 b(structure)i Fr(mctx)f Fx(which)g(should)g(be)g (initialized,)69 b(the)4050 8713 y(thread)33 b(startup)h(function)g (address)e Fr(sf)p 6223 8713 V 60 w(addr)p Fx(,)i(the)g(thread)f (startup)4050 8912 y(function)54 b(ar)m(gument)f Fr(sf)p 5455 8912 V 59 w(ar)-6 b(g)53 b Fx(and)g(a)g(chunk)g(of)g(memory)h (start-)4050 9112 y(ing)65 b(at)f Fr(sk)p 4660 9112 V 59 w(addr)69 b Fx(and)64 b Fr(sk)p 5545 9112 V 59 w(size)g Fx(bytes)g(in)h(size,)k(which)64 b(should)4050 9311 y(become)41 b(the)g(threads)g(stack.)4299 9511 y(The)69 b(follo)l(wing)g(algorithm) g(for)g Fp(mctx)p 6722 9511 V 58 w(create)d Fx(is)j(di-)4050 9710 y(rectly)44 b(modeled)g(after)h(the)f(implemented)g(algorithm)h (one)g(can)4050 9909 y(\002nd)59 b(in)g Fr(GNU)f(Pth)h Fx([25],)k(which)58 b(in)h(turn)g(w)n(as)f(deri)l(v)n(ed)g(from)4050 10108 y(techniques)41 b(originally)h(found)g(in)g Fr(r)n(sthr)-6 b(eads)40 b Fx([27]:)p Black 4258 10475 a(1.)p Black 82 w(Preserv)n(e)49 b(the)h(current)g(signal)g(mask)f(and)h(block)f(an) h(ar)m(-)4465 10675 y(bitrary)k(w)n(ork)n(er)g(signal)e(\(we)h(use)f Fp(SIGUSR1)p Fx(,)h(b)m(ut)g(an)n(y)4465 10874 y(signal)47 b(can)g(be)g(used)g(for)h(this)f(\226)g(e)l(v)n(en)g(an)g(already)g (used)4465 11073 y(one\).)109 b(This)61 b(w)n(ork)n(er)h(signal)f(is)f (later)h(temporarily)h(re-)4465 11272 y(quired)42 b(for)g(the)f (trampoline)h(step.)p Black 3858 11872 a(5)p Black eop end %%Page: 6 6 TeXDict begin 6 5 bop Black Black Black 208 638 a Fx(2.)p Black 82 w(Preserv)n(e)59 b(a)g(possibly)g(e)n(xisting)g(signal)g (action)g(for)h(the)415 838 y(w)n(ork)n(er)44 b(signal)f(and)f (con\002gure)h(a)g(trampoline)h(function)415 1037 y(as)39 b(the)g(ne)l(w)h(temporary)g(signal)f(action.)50 b(The)40 b(signal)f(de-)415 1236 y(li)l(v)n(ery)k(is)g(con\002gured)g(to)g (occur)g(on)g(an)g(alternate)f(signal)415 1435 y(stack)f(\(see)g(ne)n (xt)g(step\).)p Black 208 1802 a(3.)p Black 82 w(Preserv)n(e)72 b(a)h(possibly)g(acti)l(v)n(e)f(alternate)h(signal)g(stack)415 2001 y(and)115 b(con\002gure)f(the)h(memory)g(chunk)f(starting)h(at)415 2200 y Fr(sk)p 564 2200 50 7 v 59 w(addr)96 b Fx(as)c(the)g(ne)l(w)g (temporary)h(alternate)f(signal)415 2399 y(stack)41 b(of)h(length)f Fr(sk)p 1583 2399 V 59 w(size)p Fx(.)p Black 208 2765 a(4.)p Black 82 w(Sa)m(v)n(e)82 b(parameters)g(for)h(the)g(trampoline)g (step)f(\()p Fr(mctx)p Fx(,)415 2965 y Fr(sf)p 536 2965 V 60 w(addr)p Fx(,)52 b Fr(sf)p 1115 2965 V 60 w(ar)-6 b(g)p Fx(,)52 b Fr(etc.)p Fx(\))80 b(in)51 b(global)g(v)l(ariables,)i (send)d(the)415 3164 y(current)44 b(process)f(the)g(w)n(ork)n(er)i (signal,)e(temporarily)i(un-)415 3363 y(block)36 b(it)g(and)g(this)g(w) n(ay)g(allo)l(w)g(it)g(to)g(be)g(deli)l(v)n(ered)f(on)h(the)415 3562 y(signal)53 b(stack)f(in)h(order)g(to)g(transfer)g(e)n(x)n (ecution)f(control)415 3762 y(to)42 b(the)f(trampoline)h(function.)p Black 208 4128 a(5.)p Black 82 w(After)51 b(the)g(trampoline)h (function)f(asynchronously)f(en-)415 4327 y(tered,)58 b(sa)m(v)n(e)53 b(its)i(machine)f(conte)n(xt)g(in)h(the)g Fr(mctx)g Fx(struc-)415 4526 y(ture)63 b(and)f(immediately)h(return)h (from)f(it)g(to)g(terminate)415 4725 y(the)42 b(signal)f(handler)g (scope.)p Black 208 5091 a(6.)p Black 82 w(Restore)59 b(the)h(preserv)n(ed)f(alternate)g(signal)h(stack,)j(pre-)415 5291 y(serv)n(ed)39 b(signal)g(action)h(and)f(preserv)n(ed)g(signal)g (mask)g(for)415 5490 y(w)n(ork)n(er)65 b(signal.)119 b(This)65 b(w)n(ay)f(an)g(e)n(xisting)g(application)415 5689 y(con\002guration)42 b(for)g(the)g(w)n(ork)n(er)g(signal)f(is)h (restored.)p Black 208 6055 a(7.)p Black 82 w(Sa)m(v)n(e)231 b(the)g(current)h(machine)f(conte)n(xt)g(of)415 6254 y Fp(mctx)p 825 6254 V 58 w(create)p Fx(.)106 b(This)60 b(allo)l(ws)g(us)g(to)h(return)g(to)f(this)415 6454 y(point)42 b(after)g(the)f(ne)n(xt)g(trampoline)h(step.)p Black 208 6820 a(8.)p Black 82 w(Restore)61 b(the)g(pre)l(viously)g(sa)m(v)n (ed)f(machine)h(conte)n(xt)g(of)415 7019 y(the)33 b(trampoline)g (function)g(\()p Fr(mctx)p Fx(\))h(to)f(ag)o(ain)g(transfer)f(e)n(x-) 415 7218 y(ecution)67 b(control)g(onto)g(the)g(alternate)f(stack,)72 b(b)m(ut)67 b(this)415 7417 y(time)42 b(without\(!\))53 b(signal)41 b(handler)g(scope.)p Black 208 7783 a(9.)p Black 82 w(After)103 b(reaching)f(the)h(trampoline)g(function)f(\()p Fr(mctx)p Fx(\))415 7983 y(ag)o(ain,)84 b(immediately)76 b(bootstrap)f(into)h(a)g(clean)f(stack)415 8182 y(frame)42 b(by)f(just)h(calling)f(a)h(second)e(function.)p Black 125 8548 a(10.)p Black 82 w(Set)104 b(the)f(ne)l(w)g(signal)g(mask)h (to)f(be)h(the)f(same)g(as)415 8747 y(the)65 b(original)h(signal)f (mask)g(which)g(w)n(as)f(acti)l(v)n(e)g(when)415 8946 y Fp(mctx)p 825 8946 V 58 w(create)59 b Fx(w)n(as)h(called.)108 b(This)61 b(is)g(required)g(be-)415 9146 y(cause)38 b(in)i(the)g (\002rst)f(trampoline)h(step)f(we)h(usually)f(had)g(at)415 9345 y(least)i(the)g(w)n(ork)n(er)i(signal)e(block)n(ed.)p Black 125 9711 a(11.)p Black 82 w(Load)107 b(the)f(passed)f(startup)h (information)h(\()p Fr(sf)p 3345 9711 V 60 w(addr)p Fx(,)415 9910 y Fr(sf)p 536 9910 V 60 w(ar)-6 b(g)p Fx(\))94 b(from)h Fp(mctx)p 1787 9910 V 59 w(create)d Fx(into)j(local)f(\(stack-)415 10109 y(based\))57 b(v)l(ariables.)96 b(This)58 b(is)e(important)i (because)e(their)415 10309 y(v)l(alues)35 b(ha)m(v)n(e)g(to)h(be)g (preserv)n(ed)f(in)h(machine)g(conte)n(xt)f(de-)415 10508 y(pendent)h(memory)i(until)f(the)f(created)g(machine)h(conte)n(xt)415 10707 y(is)k(the)h(\002rst)g(time)f(restored)h(by)f(the)h(application.) p Black 125 11073 a(12.)p Black 82 w(Sa)m(v)n(e)53 b(the)h(current)g (machine)f(conte)n(xt)h(for)g(later)g(restor)m(-)415 11272 y(ing)42 b(by)f(the)h(calling)f(application.)p Black Black Black 4175 638 a(13.)p Black 82 w(Restore)61 b(the)g(pre)l(viously)g(sa)m(v)n(ed)f(machine)h(conte)n(xt)g(of)4465 838 y Fp(mctx)p 4875 838 V 58 w(create)42 b Fx(to)i(transfer)f(e)n(x)n (ecution)f(control)j(back)4465 1037 y(to)d(it.)p Black 4175 1350 a(14.)p Black 82 w(Return)g(to)f(the)h(calling)f (application.)4050 1666 y(When)35 b(the)h(calling)g(application)g(no)l (w)f(ag)o(ain)h(switches)f(into)h(the)4050 1866 y(established)50 b(machine)i(conte)n(xt)e Fr(mctx)p Fx(,)j(the)f(thread)f(starts)g(run-) 4050 2065 y(ning)58 b(at)g(routine)g Fr(sf)p 5230 2065 V 59 w(addr)j Fx(with)d(ar)m(gument)g Fr(sf)p 6810 2065 V 60 w(ar)-6 b(g)p Fx(.)99 b(Figure)58 b(1)4050 2264 y(illustrates)d(the)f(algorithm)i(\(the)f(numbers)g(refer)g(to)g(the)g (algo-)4050 2463 y(rithm)42 b(steps)f(listed)g(abo)n(v)n(e\).)4050 10360 y @@beginspecial 0 @@llx 0 @@lly 224 @@urx 461 @@ury 2240 @@rwi @@setspecial %%BeginDocument: rse-pmt.eps %!PS-Adobe-2.0 EPSF-2.0 %%Title: rse-pmt.fig %%Creator: fig2dev Version 3.2 Patchlevel 5-alpha7 %%CreationDate: Mon Jun 12 19:51:15 2006 %%For: rse@@en1.engelschall.com (Ralf S. Engelschall) %%BoundingBox: 0 0 224 461 %Magnification: 1.0000 %%EndComments /$F2psDict 200 dict def $F2psDict begin $F2psDict /mtrx matrix put /col-1 {0 setgray} bind def /col0 {0.000 0.000 0.000 srgb} bind def /col1 {0.000 0.000 1.000 srgb} bind def /col2 {0.000 1.000 0.000 srgb} bind def /col3 {0.000 1.000 1.000 srgb} bind def /col4 {1.000 0.000 0.000 srgb} bind def /col5 {1.000 0.000 1.000 srgb} bind def /col6 {1.000 1.000 0.000 srgb} bind def /col7 {1.000 1.000 1.000 srgb} bind def /col8 {0.000 0.000 0.560 srgb} bind def /col9 {0.000 0.000 0.690 srgb} bind def /col10 {0.000 0.000 0.820 srgb} bind def /col11 {0.530 0.810 1.000 srgb} bind def /col12 {0.000 0.560 0.000 srgb} bind def /col13 {0.000 0.690 0.000 srgb} bind def /col14 {0.000 0.820 0.000 srgb} bind def /col15 {0.000 0.560 0.560 srgb} bind def /col16 {0.000 0.690 0.690 srgb} bind def /col17 {0.000 0.820 0.820 srgb} bind def /col18 {0.560 0.000 0.000 srgb} bind def /col19 {0.690 0.000 0.000 srgb} bind def /col20 {0.820 0.000 0.000 srgb} bind def /col21 {0.560 0.000 0.560 srgb} bind def /col22 {0.690 0.000 0.690 srgb} bind def /col23 {0.820 0.000 0.820 srgb} bind def /col24 {0.500 0.190 0.000 srgb} bind def /col25 {0.630 0.250 0.000 srgb} bind def /col26 {0.750 0.380 0.000 srgb} bind def /col27 {1.000 0.500 0.500 srgb} bind def /col28 {1.000 0.630 0.630 srgb} bind def /col29 {1.000 0.750 0.750 srgb} bind def /col30 {1.000 0.880 0.880 srgb} bind def /col31 {1.000 0.840 0.000 srgb} bind def end save newpath 0 461 moveto 0 0 lineto 224 0 lineto 224 461 lineto closepath clip newpath -21.3 477.0 translate 1 -1 scale /cp {closepath} bind def /ef {eofill} bind def /gr {grestore} bind def /gs {gsave} bind def /sa {save} bind def /rs {restore} bind def /l {lineto} bind def /m {moveto} bind def /rm {rmoveto} bind def /n {newpath} bind def /s {stroke} bind def /sh {show} bind def /slc {setlinecap} bind def /slj {setlinejoin} bind def /slw {setlinewidth} bind def /srgb {setrgbcolor} bind def /rot {rotate} bind def /sc {scale} bind def /sd {setdash} bind def /ff {findfont} bind def /sf {setfont} bind def /scf {scalefont} bind def /sw {stringwidth} bind def /tr {translate} bind def /tnt {dup dup currentrgbcolor 4 -2 roll dup 1 exch sub 3 -1 roll mul add 4 -2 roll dup 1 exch sub 3 -1 roll mul add 4 -2 roll dup 1 exch sub 3 -1 roll mul add srgb} bind def /shd {dup dup currentrgbcolor 4 -2 roll mul 4 -2 roll mul 4 -2 roll mul srgb} bind def /$F2psBegin {$F2psDict begin /$F2psEnteredState save def} def /$F2psEnd {$F2psEnteredState restore end} def $F2psBegin 10 setmiterlimit 0 slj 0 slc 0.06299 0.06299 sc % % Fig objects follow % % % here starts figure with depth 100 % Polyline 0 slj 0 slc 15.000 slw n 780 705 m 750 705 750 855 30 arcto 4 {pop} repeat 750 885 900 885 30 arcto 4 {pop} repeat 930 885 930 735 30 arcto 4 {pop} repeat 930 705 780 705 30 arcto 4 {pop} repeat cp gs 0.00 setgray ef gr gs col0 s gr % Polyline n 1035 1185 m 1005 1185 1005 1335 30 arcto 4 {pop} repeat 1005 1365 1155 1365 30 arcto 4 {pop} repeat 1185 1365 1185 1215 30 arcto 4 {pop} repeat 1185 1185 1035 1185 30 arcto 4 {pop} repeat cp gs 0.00 setgray ef gr gs col0 s gr % Polyline n 3075 2760 m 3045 2760 3045 2910 30 arcto 4 {pop} repeat 3045 2940 3195 2940 30 arcto 4 {pop} repeat 3225 2940 3225 2790 30 arcto 4 {pop} repeat 3225 2760 3075 2760 30 arcto 4 {pop} repeat cp gs 0.00 setgray ef gr gs col0 s gr % Polyline n 3105 885 m 3075 885 3075 1035 30 arcto 4 {pop} repeat 3075 1065 3225 1065 30 arcto 4 {pop} repeat 3255 1065 3255 915 30 arcto 4 {pop} repeat 3255 885 3105 885 30 arcto 4 {pop} repeat cp gs 0.00 setgray ef gr gs col0 s gr % Polyline n 2040 1500 m 2010 1500 2010 1650 30 arcto 4 {pop} repeat 2010 1680 2160 1680 30 arcto 4 {pop} repeat 2190 1680 2190 1530 30 arcto 4 {pop} repeat 2190 1500 2040 1500 30 arcto 4 {pop} repeat cp gs 0.00 setgray ef gr gs col0 s gr % Polyline n 2040 2100 m 2010 2100 2010 2250 30 arcto 4 {pop} repeat 2010 2280 2160 2280 30 arcto 4 {pop} repeat 2190 2280 2190 2130 30 arcto 4 {pop} repeat 2190 2100 2040 2100 30 arcto 4 {pop} repeat cp gs 0.00 setgray ef gr gs col0 s gr % Polyline n 1035 705 m 1005 705 1005 855 30 arcto 4 {pop} repeat 1005 885 1155 885 30 arcto 4 {pop} repeat 1185 885 1185 735 30 arcto 4 {pop} repeat 1185 705 1035 705 30 arcto 4 {pop} repeat cp gs 0.00 setgray ef gr gs col0 s gr % Polyline n 2040 2640 m 2010 2640 2010 2790 30 arcto 4 {pop} repeat 2010 2820 2160 2820 30 arcto 4 {pop} repeat 2190 2820 2190 2670 30 arcto 4 {pop} repeat 2190 2640 2040 2640 30 arcto 4 {pop} repeat cp gs 0.00 setgray ef gr gs col0 s gr % Polyline n 2040 525 m 2010 525 2010 675 30 arcto 4 {pop} repeat 2010 705 2160 705 30 arcto 4 {pop} repeat 2190 705 2190 555 30 arcto 4 {pop} repeat 2190 525 2040 525 30 arcto 4 {pop} repeat cp gs 0.00 setgray ef gr gs col0 s gr % Polyline n 3075 2505 m 3045 2505 3045 2655 30 arcto 4 {pop} repeat 3045 2685 3195 2685 30 arcto 4 {pop} repeat 3225 2685 3225 2535 30 arcto 4 {pop} repeat 3225 2505 3075 2505 30 arcto 4 {pop} repeat cp gs 0.00 setgray ef gr gs col0 s gr % Polyline n 1305 1500 m 1275 1500 1275 1650 30 arcto 4 {pop} repeat 1275 1680 1425 1680 30 arcto 4 {pop} repeat 1455 1680 1455 1530 30 arcto 4 {pop} repeat 1455 1500 1305 1500 30 arcto 4 {pop} repeat cp gs 0.00 setgray ef gr gs col0 s gr % Polyline n 1545 3225 m 1515 3225 1515 3375 30 arcto 4 {pop} repeat 1515 3405 1665 3405 30 arcto 4 {pop} repeat 1695 3405 1695 3255 30 arcto 4 {pop} repeat 1695 3225 1545 3225 30 arcto 4 {pop} repeat cp gs 0.00 setgray ef gr gs col0 s gr % Polyline n 1350 570 m 1320 570 1320 720 30 arcto 4 {pop} repeat 1320 750 1470 750 30 arcto 4 {pop} repeat 1500 750 1500 600 30 arcto 4 {pop} repeat 1500 570 1350 570 30 arcto 4 {pop} repeat cp gs 0.00 setgray ef gr gs col0 s gr % Polyline n 3345 2505 m 3315 2505 3315 2655 30 arcto 4 {pop} repeat 3315 2685 3465 2685 30 arcto 4 {pop} repeat 3495 2685 3495 2535 30 arcto 4 {pop} repeat 3495 2505 3345 2505 30 arcto 4 {pop} repeat cp gs 0.00 setgray ef gr gs col0 s gr % here ends figure; % % here starts figure with depth 50 /Times-Bold ff 158.75 scf sf 3068 2895 m gs 1 -1 sc (12) col7 sh gr % Polyline 0 slj 0 slc 7.500 slw n 630 270 m 1890 270 l 1890 3600 l 630 3600 l cp gs col0 s gr % Polyline n 2340 270 m 3600 270 l 3600 1800 l 2340 1800 l cp gs col0 s gr % Polyline n 2340 2070 m 3600 2070 l 3600 3600 l 2340 3600 l cp gs col0 s gr % Polyline [15 15] 15 sd n 1980 810 m 2025 720 l 2070 900 l 2115 720 l 2160 900 l 2205 810 l gs col0 s gr [] 0 sd % Polyline [15 15] 15 sd n 1980 1170 m 2014 1081 l 2059 1261 l 2104 1081 l 2149 1261 l 2205 1170 l gs col0 s gr [] 0 sd % Polyline 15.000 slw gs clippath 1108 660 m 1275 660 l 1275 600 l 1108 600 l 1108 600 l 1228 630 l 1108 660 l cp eoclip n 1260 4586 m 1980 4593 l 1980 3690 l 450 3690 l 450 630 l 990 630 l 1260 630 l gs col0 s gr gr % arrowhead 7.500 slw n 1108 660 m 1228 630 l 1108 600 l 1108 660 l cp gs 0.00 setgray ef gr col0 s % Polyline 15.000 slw gs clippath 1828 840 m 1995 840 l 1995 780 l 1828 780 l 1828 780 l 1948 810 l 1828 840 l cp eoclip n 1260 630 m 1260 810 l 1980 810 l gs col0 s gr gr % arrowhead 7.500 slw n 1828 840 m 1948 810 l 1828 780 l 1828 840 l cp gs 0.00 setgray ef gr col0 s % Polyline 15.000 slw gs clippath 2818 840 m 2985 840 l 2985 780 l 2818 780 l 2818 780 l 2938 810 l 2818 840 l cp eoclip n 2205 810 m 2970 810 l gs col0 s gr gr % arrowhead 7.500 slw n 2818 840 m 2938 810 l 2818 780 l 2818 840 l cp gs 0.00 setgray ef gr col0 s % Polyline 15.000 slw gs clippath 2940 1018 m 2940 1185 l 3000 1185 l 3000 1018 l 3000 1018 l 2970 1138 l 2940 1018 l cp eoclip n 2970 810 m 2970 1170 l gs col0 s gr gr % arrowhead 7.500 slw n 2940 1018 m 2970 1138 l 3000 1018 l 2940 1018 l cp gs 0.00 setgray ef gr col0 s % Polyline 15.000 slw gs clippath 2357 1140 m 2190 1140 l 2190 1200 l 2357 1200 l 2357 1200 l 2237 1170 l 2357 1140 l cp eoclip n 2205 1170 m 2970 1170 l gs col0 s gr gr % arrowhead 7.500 slw n 2357 1140 m 2237 1170 l 2357 1200 l 2357 1140 l cp gs 0.00 setgray ef gr col0 s % Polyline 15.000 slw gs clippath 1412 1140 m 1245 1140 l 1245 1200 l 1412 1200 l 1412 1200 l 1292 1170 l 1412 1140 l cp eoclip n 1260 1170 m 1980 1170 l gs col0 s gr gr % arrowhead 7.500 slw n 1412 1140 m 1292 1170 l 1412 1200 l 1412 1140 l cp gs 0.00 setgray ef gr col0 s % Polyline 15.000 slw gs clippath 2818 1470 m 2985 1470 l 2985 1410 l 2818 1410 l 2818 1410 l 2938 1440 l 2818 1470 l cp eoclip n 1260 1440 m 2970 1440 l gs col0 s gr gr % arrowhead 7.500 slw n 2818 1470 m 2938 1440 l 2818 1410 l 2818 1470 l cp gs 0.00 setgray ef gr col0 s % Polyline 15.000 slw n 1260 1170 m 1260 1440 l gs col0 s gr % Polyline gs clippath 2818 2550 m 2985 2550 l 2985 2490 l 2818 2490 l 2818 2490 l 2938 2520 l 2818 2550 l cp eoclip n 2970 1440 m 2970 1890 l 2250 1890 l 2250 2520 l 2970 2520 l gs col0 s gr gr % arrowhead 7.500 slw n 2818 2550 m 2938 2520 l 2818 2490 l 2818 2550 l cp gs 0.00 setgray ef gr col0 s % Polyline 15.000 slw n 2970 2520 m 2970 2883 l gs col0 s gr % Polyline gs clippath 1397 2853 m 1230 2852 l 1229 2912 l 1396 2913 l 1396 2913 l 1277 2883 l 1397 2853 l cp eoclip n 2970 2891 m 1245 2883 l gs col0 s gr gr % arrowhead 7.500 slw n 1397 2853 m 1277 2883 l 1396 2913 l 1397 2853 l cp gs 0.00 setgray ef gr col0 s % Polyline 15.000 slw gs clippath 1396 4749 m 1229 4751 l 1230 4811 l 1397 4809 l 1397 4809 l 1277 4781 l 1396 4749 l cp eoclip n 1260 2880 m 1260 3150 l 2070 3150 l 2062 4773 l 1245 4781 l gs col0 s gr gr % arrowhead 7.500 slw n 1396 4749 m 1277 4781 l 1397 4809 l 1396 4749 l cp gs 0.00 setgray ef gr col0 s % Polyline 15.000 slw gs clippath 1230 4434 m 1230 4601 l 1290 4601 l 1290 4434 l 1290 4434 l 1260 4554 l 1230 4434 l cp eoclip n 450 4050 m 1260 4050 l 1260 4586 l gs col0 s gr gr % arrowhead 7.500 slw n 1230 4434 m 1260 4554 l 1290 4434 l 1230 4434 l cp gs 0.00 setgray ef gr col0 s % Polyline 15.000 slw gs clippath 1226 5068 m 1230 5235 l 1290 5234 l 1286 5067 l 1286 5067 l 1259 5188 l 1226 5068 l cp eoclip n 1252 4773 m 1260 5220 l gs col0 s gr gr % arrowhead 7.500 slw n 1226 5068 m 1259 5188 l 1286 5067 l 1226 5068 l cp gs 0.00 setgray ef gr col0 s % Polyline 1 slc 15.000 slw [15 45] 45 sd gs clippath 2818 4260 m 2985 4260 l 2985 4200 l 2818 4200 l 2818 4200 l 2938 4230 l 2818 4260 l cp eoclip n 2970 3150 m 2970 3690 l 2250 3690 l 2250 4230 l 2970 4230 l gs col0 s gr gr [] 0 sd % arrowhead 0 slc 7.500 slw n 2818 4260 m 2938 4230 l 2818 4200 l 2818 4260 l cp gs 0.00 setgray ef gr col0 s % Polyline n 630 6300 m 1890 6300 l 1890 7290 l gs col0 s gr % Polyline gs clippath 3660 7408 m 3660 7575 l 3720 7575 l 3720 7408 l 3720 7408 l 3690 7528 l 3660 7408 l cp 3720 6452 m 3720 6285 l 3660 6285 l 3660 6452 l 3660 6452 l 3690 6332 l 3720 6452 l cp eoclip n 3690 6300 m 3690 7560 l gs col0 s gr gr % arrowhead n 3720 6452 m 3690 6332 l 3660 6452 l 3720 6452 l cp gs 0.00 setgray ef gr col0 s % arrowhead n 3660 7408 m 3690 7528 l 3720 7408 l 3660 7408 l cp gs 0.00 setgray ef gr col0 s % Polyline n 2340 6300 m 3600 6300 l 3600 7560 l 2340 7560 l cp gs col0 s gr % Polyline n 630 3780 m 1890 3780 l 1890 6120 l 630 6120 l cp gs col0 s gr % Polyline n 2340 3780 m 3600 3780 l 3600 6120 l 2340 6120 l cp gs col0 s gr % Polyline 1 slc 15.000 slw [15 45] 45 sd gs clippath 2818 3180 m 2985 3180 l 2985 3120 l 2818 3120 l 2818 3120 l 2938 3150 l 2818 3180 l cp eoclip n 1260 5220 m 2160 5220 l 2160 3150 l 2970 3150 l gs col0 s gr gr [] 0 sd % arrowhead 0 slc 7.500 slw n 2818 3180 m 2938 3150 l 2818 3120 l 2818 3180 l cp gs 0.00 setgray ef gr col0 s % Polyline n 630 6300 m 630 7380 l gs col0 s gr % Polyline 1 slc n 630 6570 m 900 6300 l gs col0 s gr % Polyline n 630 6840 m 1170 6300 l gs col0 s gr % Polyline n 630 7110 m 1440 6300 l gs col0 s gr % Polyline n 630 7380 m 1710 6300 l gs col0 s gr % Polyline n 810 7470 m 1890 6435 l gs col0 s gr % Polyline n 1080 7515 m 1890 6750 l gs col0 s gr % Polyline n 1665 7245 m 1890 7020 l gs col0 s gr % Polyline n 2340 6570 m 2610 6300 l gs col0 s gr % Polyline n 2340 6840 m 2925 6300 l gs col0 s gr % Polyline n 2340 7110 m 3240 6300 l gs col0 s gr % Polyline n 2340 7380 m 3510 6300 l gs col0 s gr % Polyline n 2520 7560 m 3600 6570 l gs col0 s gr % Polyline n 2880 7560 m 3600 6930 l gs col0 s gr % Polyline n 3240 7560 m 3600 7245 l gs col0 s gr % Polyline 15.000 slw [15 45] 45 sd gs clippath 2940 5158 m 2940 5325 l 3000 5325 l 3000 5158 l 3000 5158 l 2970 5278 l 2940 5158 l cp eoclip n 2970 4230 m 2970 5310 l gs col0 s gr gr [] 0 sd % arrowhead 0 slc 7.500 slw n 2940 5158 m 2970 5278 l 3000 5158 l 2940 5158 l cp gs 0.00 setgray ef gr col0 s % Polyline 1 slc 15.000 slw [15 45] 45 sd gs clippath 1412 5280 m 1245 5280 l 1245 5340 l 1412 5340 l 1412 5340 l 1292 5310 l 1412 5280 l cp eoclip n 2970 5310 m 1260 5310 l gs col0 s gr gr [] 0 sd % arrowhead 0 slc 7.500 slw n 1412 5280 m 1292 5310 l 1412 5340 l 1412 5280 l cp gs 0.00 setgray ef gr col0 s % Polyline 1 slc 15.000 slw [15 45] 45 sd gs clippath 1412 5640 m 1245 5640 l 1245 5700 l 1412 5700 l 1412 5700 l 1292 5670 l 1412 5640 l cp eoclip n 2970 5580 m 2970 5670 l 1260 5670 l gs col0 s gr gr [] 0 sd % arrowhead 0 slc 7.500 slw n 1412 5640 m 1292 5670 l 1412 5700 l 1412 5640 l cp gs 0.00 setgray ef gr col0 s % Polyline 1 slc 15.000 slw [15 45] 45 sd gs clippath 512 5910 m 345 5910 l 345 5970 l 512 5970 l 512 5970 l 392 5940 l 512 5910 l cp eoclip n 1260 5670 m 1260 5940 l 360 5940 l gs col0 s gr gr [] 0 sd % arrowhead 0 slc 7.500 slw n 512 5910 m 392 5940 l 512 5970 l 512 5910 l cp gs 0.00 setgray ef gr col0 s % Polyline 1 slc 15.000 slw [15 45] 45 sd gs clippath 2818 5610 m 2985 5610 l 2985 5550 l 2818 5550 l 2818 5550 l 2938 5580 l 2818 5610 l cp eoclip n 1260 5355 m 1260 5580 l 2970 5580 l gs col0 s gr gr [] 0 sd % arrowhead 0 slc 7.500 slw n 2818 5610 m 2938 5580 l 2818 5550 l 2818 5610 l cp gs 0.00 setgray ef gr col0 s % Polyline 2 slj n 630 7380 m 631 7381 l 634 7383 l 639 7388 l 647 7394 l 657 7402 l 670 7412 l 686 7423 l 703 7435 l 723 7448 l 744 7460 l 766 7472 l 791 7483 l 817 7493 l 845 7502 l 877 7509 l 911 7514 l 949 7517 l 991 7518 l 1035 7515 l 1076 7510 l 1115 7502 l 1150 7493 l 1181 7483 l 1207 7473 l 1228 7463 l 1245 7453 l 1259 7443 l 1269 7433 l 1278 7423 l 1286 7414 l 1294 7404 l 1303 7394 l 1314 7384 l 1327 7373 l 1344 7362 l 1364 7350 l 1390 7338 l 1419 7326 l 1454 7313 l 1491 7301 l 1530 7290 l 1576 7279 l 1619 7271 l 1657 7265 l 1690 7262 l 1720 7261 l 1747 7261 l 1771 7263 l 1793 7266 l 1813 7269 l 1831 7273 l 1847 7277 l 1861 7280 l 1872 7284 l 1880 7286 l 1885 7288 l 1889 7289 l 1890 7290 l gs col0 s gr /Times-Bold ff 158.75 scf sf 2430 6480 m gs 1 -1 sc (sk_addr) col0 sh gr /Times-Bold ff 158.75 scf sf 720 6480 m gs 1 -1 sc (main stack) col0 sh gr /Times-Bold ff 158.75 scf sf 720 3960 m gs 1 -1 sc (main) col0 sh gr /Times-Bold ff 158.75 scf sf 2430 3960 m gs 1 -1 sc (sf_addr\(sf_arg\)) col0 sh gr /Times-Bold ff 158.75 scf sf 2430 2250 m gs 1 -1 sc (mctx_create) col0 sh gr /Times-Bold ff 158.75 scf sf 2430 450 m gs 1 -1 sc (mctx_create) col0 sh gr /Times-Bold ff 158.75 scf sf 720 450 m gs 1 -1 sc (mctx_create) col0 sh gr /Times-Bold ff 158.75 scf sf 3150 2250 m gs 1 -1 sc (_boot) col0 sh gr /Times-Bold ff 158.75 scf sf 2790 585 m gs 1 -1 sc (_trampoline) col0 sh gr /Times-Bold ff 158.75 scf sf 3870 7110 m gs 1 -1 sc 90.0 rot (sk_size) col0 sh gr /Times-Bold ff 158.75 scf sf 1350 5175 m gs 1 -1 sc (mctx_switch) col0 sh gr /Times-Bold ff 158.75 scf sf 2385 5445 m gs 1 -1 sc (mctx_switch) col0 sh gr /Times-Bold ff 158.75 scf sf 1350 5535 m gs 1 -1 sc (mctx_switch) col0 sh gr /Times-Bold ff 158.75 scf sf 2385 5805 m gs 1 -1 sc (mctx_switch) col0 sh gr /Times-Bold ff 158.75 scf sf 1305 4725 m gs 1 -1 sc (mctx_create) col0 sh gr /Times-Bold ff 158.75 scf sf 810 840 m gs 1 -1 sc (1) col7 sh gr /Times-Bold ff 158.75 scf sf 1065 840 m gs 1 -1 sc (2) col7 sh gr /Times-Bold ff 158.75 scf sf 1065 1320 m gs 1 -1 sc (6) col7 sh gr /Times-Bold ff 158.75 scf sf 1335 1635 m gs 1 -1 sc (7) col7 sh gr /Times-Bold ff 158.75 scf sf 2070 1635 m gs 1 -1 sc (8) col7 sh gr /Times-Bold ff 158.75 scf sf 2070 660 m gs 1 -1 sc (4) col7 sh gr /Times-Bold ff 158.75 scf sf 1380 705 m gs 1 -1 sc (3) col7 sh gr /Times-Bold ff 158.75 scf sf 3135 1020 m gs 1 -1 sc (5) col7 sh gr /Times-Bold ff 158.75 scf sf 2070 2235 m gs 1 -1 sc (9) col7 sh gr /Times-Bold ff 158.75 scf sf 2040 2775 m gs 1 -1 sc (13) col7 sh gr /Times-Bold ff 158.75 scf sf 3068 2640 m gs 1 -1 sc (10) col7 sh gr /Times-Bold ff 158.75 scf sf 1545 3360 m gs 1 -1 sc (14) col7 sh gr /Times-Bold ff 158.75 scf sf 3337 2640 m gs 1 -1 sc (11) col7 sh gr % here ends figure; $F2psEnd rs showpage %%Trailer %EOF %%EndDocument @@endspecial 174 x Fn(Figur)n(e)33 b(1:)134 b Fm(Illustration)28 b(of)h(the)f(machine)g(conte)n(xt)h(creation)g(procedure.)41 b(The)4701 10670 y(thick)h(solid)g(lines)g(and)h(numeric)g(marks)f (correspond)i(to)f(the)f(al-)4701 10806 y(gorithm)35 b(steps)e(as)g(described)i(in)f(section)f(3.2.)44 b(The)34 b(thick)g(dotted)4701 10942 y(lines)i(sho)m(w)h(a)g(possible)h(further) g(processing)g(where)f(a)g(fe)m(w)g(con-)4701 11078 y(te)n(xt)i (switches)f(are)i(performed)h(to)e(dispatch)h(between)g(the)f(main)4701 11214 y(thread)34 b(and)g(the)f(ne)m(w)h(created)f(thread.)p Black 3858 11872 a Fx(6)p Black eop end %%Page: 7 7 TeXDict begin 7 6 bop Black Black 0 638 a FA(3.3)198 b(Sour)l(ce)51 b(Code)0 950 y Fx(The)88 b(corresponding)h(ANSI-C)f (code,)99 b(which)88 b(implements)0 1149 y Fp(mctx)p 410 1149 50 7 v 58 w(create)p Fx(,)73 b(is)c(a)g(little)g(bit)g(more)g (complicated.)133 b(But)0 1348 y(with)47 b(the)f(presented)g(algorithm) h(in)g(mind,)g(it)g(is)f(no)l(w)h(straight-)0 1548 y(forw)n(ard.)p Black Black 0 1858 a Fi(static)88 b Fk(mctx_t)625 b(mctx_caller;)0 2007 y Fi(static)88 b Fk(sig_atomic_t)d(mctx_called;)0 2306 y Fi(static)j Fk(mctx_t)1704 2332 y(*)1794 2306 y(mctx_creat;)0 2456 y Fi(static)g(void)626 b Fk(\()1704 2482 y(*)1794 2456 y(mctx_creat_func\)\()p Fi(void)3766 2482 y Fk(*)3856 2456 y(\);)0 2605 y Fi(static)88 b(void)1704 2631 y Fk(*)1794 2605 y(mctx_creat_arg;)0 2754 y Fi(static)g Fk(sigset_t)445 b(mctx_creat_sigs;)0 3053 y Fi(void)88 b Fk(mctx_create\()359 3203 y(mctx_t)986 3229 y(*)1076 3203 y(mctx,)359 3352 y Fi(void)g Fk(\()897 3378 y(*)987 3352 y(sf_addr\)\()p Fi(void)2242 3378 y Fk(*)2332 3352 y(\),)g Fi(void)3049 3378 y Fk(*)3139 3352 y(sf_arg,)359 3502 y Fi(void)807 3528 y Fk(*)897 3502 y(sk_addr,)f Fi(size_t)g Fk(sk_size\))0 3651 y Fo(f)359 3800 y Fi(struct)g Fk(sigaction)g(sa;)359 3950 y Fi(struct)g Fk(sigaction)g(osa;)359 4099 y Fi(struct)g Fk(sigaltstack)f(ss;)359 4249 y Fi(struct)h Fk(sigaltstack)f(oss;)359 4398 y(sigset_t)h(osigs;)359 4548 y(sigset_t)g(sigs;)359 4847 y(/)449 4873 y(*)665 4847 y Fj(Step)37 b(1:)1182 4873 y Fk(*)1272 4847 y(/)359 4996 y(sigemptyset\(&sigs\);)359 5145 y(sigaddset\(&sigs,)84 b(SIGUSR1\);)359 5295 y(sigprocmask\(SIG_BLOCK,)e(&sigs,)88 b(&osigs\);)359 5594 y(/)449 5620 y(*)665 5594 y Fj(Step)37 b(2:)1182 5620 y Fk(*)1272 5594 y(/)359 5743 y(memset\(\()p Fi(void)1524 5769 y Fk(*)1614 5743 y(\)&sa,)88 b(0,)986 5893 y Fi(sizeof)p Fk(\()p Fi(struct)e Fk(sigaction\)\);)359 6042 y(sa.sa_handler)f(=)k(mctx_create_trampoline;)359 6191 y(sa.sa_flags)d(=)j(SA_ONSTACK;)359 6341 y (sigemptyset\(&sa.sa_mask\);)359 6490 y(sigaction\(SIGUSR1,)83 b(&sa,)89 b(&osa\);)359 6789 y(/)449 6815 y(*)665 6789 y Fj(Step)37 b(3:)1182 6815 y Fk(*)1272 6789 y(/)359 6939 y(ss.ss_sp)356 b(=)89 b(sk_addr;)359 7088 y(ss.ss_size)176 b(=)89 b(sk_size;)359 7238 y(ss.ss_flags)d(=)j(0;)359 7387 y(sigaltstack\(&ss,)84 b(&oss\);)359 7686 y(/)449 7712 y(*)665 7686 y Fj(Step)37 b(4:)1182 7712 y Fk(*)1272 7686 y(/)359 7835 y(mctx_creat)534 b(=)90 b(mctx;)359 7985 y(mctx_creat_func)84 b(=)90 b(sf_addr;)359 8134 y(mctx_creat_arg)174 b(=)90 b(sf_arg;)359 8284 y(mctx_creat_sigs)84 b(=)90 b(osigs;)359 8433 y(mctx_called)444 b(=)90 b(FALSE;)359 8583 y(kill\(getpid\(\),)85 b(SIGUSR1\);)359 8732 y (sigfillset\(&sigs\);)359 8881 y(sigdelset\(&sigs,)f(SIGUSR1\);)359 9031 y Fi(while)k Fk(\(!mctx_called\))717 9180 y(sigsuspend\(&sigs\);) 359 9479 y(/)449 9505 y(*)665 9479 y Fj(Step)37 b(6:)1182 9505 y Fk(*)1272 9479 y(/)359 9629 y(sigaltstack\(NULL,)84 b(&ss\);)359 9778 y(ss.ss_flags)i(=)j(SS_DISABLE;)359 9927 y(sigaltstack\(&ss,)84 b(NULL\);)359 10077 y(if)89 b(\(!\(oss.ss_flags)84 b(&)90 b(SS_DISABLE\)\))717 10226 y(sigaltstack\(&oss,)84 b(NULL\);)359 10376 y(sigaction\(SIGUSR1,)f (&osa,)88 b(NULL\);)359 10525 y(sigprocmask\(SIG_SETMASK,)1435 10675 y(&osigs,)f(NULL\);)359 10974 y(/)449 11000 y(*)665 10974 y Fj(Step)37 b(7)g(&)h(Step)f(8:)1751 11000 y Fk(*)1841 10974 y(/)359 11123 y(mctx_switch\(&mctx_caller,)81 b(mctx\);)p Black Black 4409 638 a(/)4499 664 y(*)4715 638 y Fj(Step)37 b(14:)5307 664 y Fk(*)5397 638 y(/)4409 788 y Fi(return)p Fk(;)4050 937 y Fo(g)4050 1236 y Fi(void)88 b Fk (mctx_create_trampoline\()p Fi(int)81 b Fk(sig\))4050 1386 y Fo(f)4409 1535 y Fk(/)4499 1561 y(*)4715 1535 y Fj(Step)37 b(5:)5232 1561 y Fk(*)5322 1535 y(/)4409 1685 y Fi(if)89 b Fk(\(mctx_save\(mctx_creat\))82 b(==)89 b(0\))g Fo(f)4767 1834 y Fk(mctx_called)d(=)k(TRUE;)4767 1983 y Fi(return)p Fk(;)4409 2133 y Fo(g)4409 2432 y Fk(/)4499 2458 y(*)4715 2432 y Fj(Step)37 b(9:)5232 2458 y Fk(*)5322 2432 y(/)4409 2581 y(mctx_create_boot\(\);)4050 2731 y Fo(g)4050 3030 y Fi(void)88 b Fk(mctx_create_boot\()p Fi(void)p Fk(\))4050 3179 y Fo(f)4409 3328 y Fi(void)g Fk(\()4947 3354 y(*)5037 3328 y(mctx_start_func\)\()p Fi(void)7009 3354 y Fk(*)7099 3328 y(\);)4409 3478 y Fi(void)4857 3504 y Fk(*)4947 3478 y(mctx_start_arg;)4409 3777 y(/)4499 3803 y(*)4715 3777 y Fj(Step)37 b(10:)5307 3803 y Fk(*)5397 3777 y(/)4409 3926 y(sigprocmask\(SIG_SETMASK,)5485 4076 y(&mctx_creat_sigs,)84 b(NULL\);)4409 4374 y(/)4499 4400 y(*)4715 4374 y Fj(Step)37 b(11:)5307 4400 y Fk(*)5397 4374 y(/)4409 4524 y(mctx_start_func)84 b(=)90 b(mctx_creat_func;)4409 4673 y(mctx_start_arg)174 b(=)90 b(mctx_creat_arg;)4409 4972 y(/)4499 4998 y(*)4715 4972 y Fj(Step)37 b(12)g(&)g(Step)h(13:) 5950 4998 y Fk(*)6040 4972 y(/)4409 5122 y(mctx_switch\(mctx_creat,)82 b(&mctx_caller\);)4409 5421 y(/)4499 5447 y(*)4715 5421 y Fj(The)38 b(thread)f(``magically'')e(starts...)6834 5447 y Fk(*)6924 5421 y(/)4409 5570 y(mctx_start_func\(mctx_start_a)o (rg\))o(;)4409 5869 y(/)4499 5895 y(*)4678 5869 y(NOTREACHED)5664 5895 y(*)5754 5869 y(/)4409 6018 y(abort\(\);)4050 6168 y Fo(g)4050 6721 y FA(3.4)198 b(Run-time)51 b(P)l(enalty)4050 7060 y Fx(After)64 b(this)g(discussion)f(of)h(the)g(implementation)g (details,)k(an)4050 7259 y(ob)n(viously)c(occuring)g(question)f(no)l(w) h(is)g(what)f(the)h(e)n(xpected)4050 7458 y(run-time)j(penalty)f(is.) 125 b(That)67 b(is,)k(what)c(does)e(our)i(presented)4050 7657 y(machine)88 b(conte)n(xt)h(implementation)g(cost)g(compared)f(to) h(a)4050 7857 y Fp(ucontext)p Fx(\(3\))70 b(based)h(solution.)143 b(From)73 b(the)f(already)g(dis-)4050 8056 y(cussed)43 b(details)g(we)h(can)f(easily)g(guess)g(that)h(our)h(comple)n(x)e(ma-) 4050 8255 y(chine)56 b(conte)n(xt)f(creation)h(procedure)g(\()p Fp(mctx)p 6784 8255 V 59 w(create)p Fx(\))e(will)4050 8454 y(be)41 b(certainly)g(noticeably)g(slo)l(wer)f(than)h(a)g (solution)g(based)f(on)h(a)4050 8654 y Fp(ucontext)p Fx(\(3\))e(f)n(acility)-11 b(.)4299 8867 y(But)96 b(a)g(wild)h(guess)e (is)h(not)h(suf)l(\002cing)f(for)h(a)f(reason-)4050 9066 y(able)c(statement.)201 b(So)92 b(we)g(ha)m(v)n(e)f(written)h(a)f Fr(Simple)h(Ma-)4050 9266 y(c)n(hine)43 b(Conte)m(xt)g(Benc)n(hmark)j Fx(\(SMCB)d([32)q(]\))h(which)g(w)n(as)g(used)4050 9465 y(to)73 b(compare)f(run-time)i(costs)d(of)i(the)g Fp(mctx)p 6842 9465 V 58 w(create)d Fx(and)4050 9664 y Fp(mctx)p 4460 9664 V 58 w(switch)54 b Fx(functions)h(if)h(once)f(implemented)g (through)4050 9863 y(the)34 b Fr(POSIX)43 b Fp(makecontext)p Fx(\(3\)/)p Fp(swapcontext)p Fx(\(3\))28 b(func-)4050 10063 y(tions)112 b(\(as)f(sho)l(wn)h(in)g(section)f(2.3\),)129 b(and)112 b(once)f(imple-)4050 10262 y(mented)90 b(with)h(our)f(based)g (f)n(allback)g(implementation)h(\(for)4050 10461 y(con)-7 b(v)n(enience)84 b(reasons)h(we)g(directly)h(used)f Fp(sigjmp)p 7412 10461 V 58 w(buf)p Fx(,)4050 10660 y Fp(sigsetjmp)p Fx(\(3\))h(and)k Fp(siglongjmp)p Fx(\(3\))c(in)j(the)g(bench-)4050 10860 y(mark,)64 b(because)58 b(all)i(tested)f(platforms)h(pro)n(vided) g(this\).)106 b(The)4050 11059 y(results)41 b(are)g(sho)l(wn)g(T)-13 b(able)41 b(3)h(belo)l(w)-11 b(.)4299 11272 y(As)106 b(one)h(can)f(deri)l(v)n(e)g(from)h(these)f(e)l(v)l(aluations,)122 b(our)p Black 3858 11872 a(7)p Black eop end %%Page: 8 8 TeXDict begin 8 7 bop Black Black 0 638 a Fx(signal)114 b(stack)f(trick)h(to)g(implement)g Fp(mctx)p 2861 638 50 7 v 58 w(create)e Fx(in)0 838 y(practice)100 b(is)g(approximately)g (15)g(times)g(slo)l(wer)g(than)g(the)0 1037 y Fp(makecontext)p Fx(\(3\))51 b(based)j(v)l(ariant.)92 b(This)55 b(cost)f(should)h(not)0 1236 y(be)k(ne)n(glected.)104 b(On)60 b(the)f(other)h(hand,)j(the)d Fp(sigsetjmp)p Fx(\(3\)/)0 1435 y Fp(siglongjmp)p Fx(\(3\))36 b(based)j Fp(mctx)p 2057 1435 V 58 w(switch)e Fx(performs)j(about)0 1635 y(as)d(good)g(as)f(the)h Fp(swapcontext)p Fx(\(3\))d(based)i(v)l (ariant)i(\(the)f(rea-)0 1834 y(son)c(wh)o(y)h(on)g(most)g(of)g(the)g (tested)f(platforms)h(it)g(is)g(e)l(v)n(en)f(slightly)0 2033 y(f)n(aster)75 b(is)g(not)g(kno)l(wn)g(\226)g(b)m(ut)g(we)f(guess) g(it)i(is)e(related)h(to)g(a)0 2233 y(greater)55 b(management)g(o)n(v)n (erhead)f(in)h(the)g Fp(ucontext)p Fx(\(3\))e(f)n(a-)0 2432 y(cility)-11 b(,)34 b(which)e(is)h(a)f(superset)f(of)i(the)f (functionality)h(we)f(require\).)0 2631 y(Or)h(in)f(short:)48 b(our)33 b(presented)e(f)n(allback)i(approach)f(costs)g(notice-)0 2830 y(able)49 b(e)n(xtra)g(CPU)h(c)n(ycles)e(on)i(thread)f(creation)g (time,)j(b)m(ut)d(is)h(as)0 3030 y(f)n(ast)34 b(as)f(the)h (standardized)f(solution)h(under)g(thread)g(dispatching)0 3229 y(time.)p 0 3456 3798 7 v 0 6335 7 2880 v 156 3597 a Fn(10000)h Ff(\002)e Fn(mctx)p 952 3597 40 7 v 48 w(cr)n(eate)h(\(in) f(seconds\):)156 3893 y(Platf)m(orm)p 1654 3941 7 158 v 1118 w(mcsc)p 2159 3941 V 327 w(sjlj)p 2664 3941 V 206 w(o)o(v)o(erhead)156 4051 y Fm(Sun)h(Solaris)f(2.6)h(\(SP)-12 b(ARC\))p 1654 4098 V 266 w(0.076)p 2159 4098 V 207 w(1.268)p 2664 4098 V 497 w(16.7)156 4209 y(DEC)33 b(T)-5 b(ru64)35 b(5.0)f(\(Alpha\))p 1654 4256 V 342 w(0.019)p 2159 4256 V 207 w(0.235)p 2664 4256 V 497 w(12.4)156 4367 y(SGI)f(IRIX)g(6.5)i (\(MIPS\))p 1654 4414 V 454 w(0.105)p 2159 4414 V 207 w(1.523)p 2664 4414 V 497 w(14.5)156 4524 y(SCO)e(UnixW)-11 b(are)34 b(7.0)h(\(Intel\))p 1654 4572 V 207 w(0.204)p 2159 4572 V 207 w(3.827)p 2664 4572 V 497 w(18.8)156 4682 y(HP)e(HP/UX)g(11.0)h(\(HPP)-12 b(A\))p 1654 4729 V 289 w(0.057)p 2159 4729 V 207 w(0.667)p 2664 4729 V 497 w(11.8)2383 4840 y Fn(A)f(v)o(erage:)175 b(14.8)156 5095 y(10000)35 b Ff(\002)e Fn(mctx)p 952 5095 40 7 v 48 w(switch)g(\(in)g(seconds\):)156 5291 y(Platf)m(orm)p 1654 5339 7 158 v 1118 w(mcsc)p 2159 5339 V 327 w(sjlj)p 2664 5339 V 206 w(o)o(v)o(erhead)156 5449 y Fm(Sun)h(Solaris)f(2.6)h (\(SP)-12 b(ARC\))p 1654 5497 V 266 w(0.137)p 2159 5497 V 207 w(0.210)p 2664 5497 V 563 w(1.5)156 5607 y(DEC)33 b(T)-5 b(ru64)35 b(5.0)f(\(Alpha\))p 1654 5654 V 342 w(0.034)p 2159 5654 V 207 w(0.022)p 2664 5654 V 563 w(0.6)156 5765 y(SGI)f(IRIX)g(6.5)i(\(MIPS\))p 1654 5812 V 454 w(0.235)p 2159 5812 V 207 w(0.190)p 2664 5812 V 563 w(0.8)156 5922 y(SCO)e(UnixW)-11 b(are)34 b(7.0)h(\(Intel\))p 1654 5970 V 207 w(0.440)p 2159 5970 V 207 w(0.398)p 2664 5970 V 563 w(0.9)156 6080 y(HP)e(HP/UX)g(11.0)h(\(HPP)-12 b(A\))p 1654 6128 V 289 w(0.106)p 2159 6128 V 207 w(0.065)p 2664 6128 V 563 w(0.6)2450 6238 y Fn(A)f(v)o(erage:)174 b(0.9)p 3791 6335 7 2880 v 0 6342 3798 7 v 0 6516 a(T)-12 b(able)33 b(3:)133 b Fm(Summary)43 b(of)f Fl(Simple)f(Mac)n(hine)h (Conte)m(xt)g(Benc)n(hmark)i Fm(\(SMCB,)589 6652 y([32)q(]\).)e(The)33 b(speed)f(of)h(machine)h(conte)n(xt)f(creation)g(and)g(switching)589 6788 y(found)51 b(on)f(each)f(tested)g(operating)h(system.)89 b Fn(mcsc)p Fm(:)71 b(functional)589 6925 y Fh(makecontext)p Fm(\(3\))23 b(/)j Fh(switchcontext)p Fm(\(3\),)e Fn(sjlj)p Fm(:)37 b(functional)589 7059 y Fh(sigsetjmp)p Fm(\(3\)/)p Fh(siglongjmp)p Fm(\(3\).)116 b Fn(o)o(v)o(erhead)p Fm(:)94 b(the)59 b(o)n(v)n(er)m(-)589 7191 y(head)34 b(of)g(using)g Fn(sjlj)f Fm(instead)h(of)g Fn(mcsc)p Fm(.)0 7619 y FA(3.5)198 b(Remaining)50 b(Issues)0 7932 y Fx(The)41 b(presented)g(algorithm)g (and)g(source)g(code)f(can)h(be)g(directly)0 8131 y(used)59 b(in)i(practice)e(for)i(implementing)f(a)g(minimal)h(threading)0 8330 y(system)52 b(or)i(the)f(concept)f(of)h(co-routines.)86 b(Its)53 b(big)g(adv)l(antage)0 8530 y(is)39 b(that)f(if)i(the)e (operating)h(system)f(pro)n(vides)g(the)h(required)g(stan-)0 8729 y(dardized)k(primiti)l(v)n(es,)h(we)f(do)g(not)h(need)f(to)h(kno)l (w)f(an)n(ything)g(at)0 8928 y(all)56 b(about)f(the)h(machine)f(we)g (are)h(running)g(on)g(\227)f(e)l(v)n(erything)0 9127 y(just)50 b(w)n(orks.)78 b(Ne)l(v)n(ertheless,)50 b(there)h(remain)f(a) g(fe)l(w)g(special)g(is-)0 9327 y(sues)40 b(we)i(still)f(ha)m(v)n(e)f (to)i(discuss.)0 9627 y FA(The)50 b(W)-13 b(aggly)49 b(longjmp\(3\))h(after)h(Retur)m(n)0 9877 y Fx(On)e(some)f(platforms,)i Fp(longjmp)p Fx(\(3\))d(may)i(not)g(be)f(called)h(af-)0 10076 y(ter)61 b(the)g(function)h(which)e(called)h(the)g Fp(setjmp)p Fx(\(3\))e(returned.)0 10276 y(When)46 b(this)g(is)g(done,) h(the)g(stack)e(frame)i(situation)f(is)g(not)h(guar)m(-)0 10475 y(anteed)54 b(to)h(be)f(in)h(a)f(clean)g(and)h(consistent)e (state.)90 b(But)55 b(this)f(is)0 10674 y(e)n(xactly)c(the)h(mechanism) f(we)h(use)f(in)i(order)f(to)g(get)g(rid)h(of)f(the)0 10873 y(signal)41 b(handler)h(scope)e(in)i(step)f(5.)249 11073 y(The)35 b(only)g(alternati)l(v)n(e)g(w)n(ould)g(be)g(to)g(lea)m (v)n(e)e(the)i(signal)g(han-)0 11272 y(dler)56 b(via)f Fp(longjmp)p Fx(\(3\),)i(b)m(ut)f(then)f(we)h(w)n(ould)g(ha)m(v)n(e)e (another)p Black Black 4050 638 a(problem,)i(as)d(e)n(xperience)f(sho)l (wed.)86 b(F)n(or)53 b(instance,)60 b(R)r Fm(O)8 b(B)g(E)g(RT)4054 838 y Fx(S)g(.)65 b(T)8 b Fm(H)g(A)q(U)t Fx(')-9 b(s)57 b Fr(Really)g(Simple)g(Thr)-6 b(eads)55 b Fx(\()p Fr(r)n(sthr)-6 b(eads)p Fx(\))56 b([27)q(])g(w)n(as)4050 1037 y(ported)37 b(to)g(se)l(v)n(eral)f(platforms)i(and)e(w)n(as)h(used)f(to)i(run)f(an) g(e)n(xper)m(-)4050 1236 y(imental)47 b(multithreaded)h(v)n(ersion)e (of)i(the)f(Apache)f(webserv)n(er)-9 b(.)4054 1435 y(T)8 b Fm(H)g(A)q(U)t Fx(')-9 b(s)35 b(approach)f(w)n(as)g(similar)h(to)g (ours,)g(b)m(ut)g(dif)l(fered)f(signif-)4050 1635 y(icantly)40 b(in)g(the)g(w)n(ay)g(the)g(signal)f(handler)h(is)g(left.)51 b(In)40 b(particular)-7 b(,)4050 1834 y(in)54 b(an)g(attempt)g(to)g(a)m (v)m(oid)g(the)g(unsafe)f(stack)h(frame,)i(it)f(used)e(a)4050 2033 y Fp(longjmp)p Fx(\(3\))40 b(call)j(to)f(lea)m(v)n(e)f(the)i (signal)f(handler)-7 b(,)42 b(rather)h(than)4050 2233 y(returning)60 b(from)f(it.)103 b(But)59 b(this)g(approach)f(does)h (not)g(w)n(ork)g(on)4050 2432 y(some)41 b Fr(SysV)13 b Fx(-deri)l(v)n(ed)41 b(k)n(ernels,)g(as)f(we)i(already)f(mentioned.) 4299 2631 y(The)64 b(problem)h(is)e(that)h(these)g(k)n(ernels)f(do)h (not)h(\223belie)l(v)n(e\224)4050 2830 y(that)59 b(the)h(code)e(is)h (out)h(of)f(the)h(signal-handling)f(conte)n(xt,)k(un-)4050 3030 y(til)i(the)g(signal)g(handler)g(has)f(returned)h(\227)g(and)f (accordingly)-11 b(,)4050 3229 y(refuse)57 b(to)h(allo)l(w)f (readjustment)g(of)h(the)f(signal)g(stack)g(until)h(it)4050 3428 y(has.)121 b(But)66 b(with)f(the)g Fr(r)n(sthr)-6 b(eads)64 b Fx(approach,)70 b(the)65 b(signal)g(han-)4050 3627 y(dler)47 b(that)g(created)f(the)h(\002rst)g(thread)g(ne)l(v)n(er) f(returns,)i(and)e(when)4050 3827 y Fr(r)n(sthr)-6 b(eads)33 b Fx(w)n(ants)i(to)g(create)f(the)h(second)f(thread,)h(these)f(k)n (ernels)4050 4026 y(refuse)48 b(to)g(readjust)f(the)h(signal)g(stack,)g (and)g(we)f(are)h(stuck.)70 b(So)4050 4225 y(with)35 b(portability)g(in)f(mind,)i(we)e(decided)f(that)i(it)f(is)g(better)h (to)f(get)4050 4424 y(rid)47 b(of)h(the)f(signal)f(handler)h(scope)g (with)g(the)g(straight-forw)n(ard)4050 4624 y(\223)p Fp(return)p Fx(\224)77 b(and)i(instead)g(\002ght)h(the)g(mentioned)f (\(simpler\))4050 4823 y(problem)42 b(of)g(an)f(unsafe)g(stack)g (frame.)4299 5022 y(F)n(ortunately)-11 b(,)55 b(in)d(practice)g(this)g (is)g(not)g(as)f(problematic)i(as)4050 5221 y(it)h(seems,)i(because)c (e)l(v)l(aluations)h(\(for)i Fr(GNU)f(Pth)p Fx(\))g(on)h(a)e(wide)4050 5421 y(range)72 b(of)i(current)e(Unix)h(platforms)g(sho)l(wed)f(that)h (one)g(can)4050 5620 y(reach)43 b(a)g(safe)g(stack)g(frame)h(ag)o(ain)f (by)h(just)f(calling)h(a)f(function.)4050 5819 y(That)54 b(is)g(the)g(reason)f(wh)o(y)h(our)h(algorithm)g(enters)e(the)h(second) 4050 6018 y(trampoline)42 b(function)g(in)g(step)f(9.)4050 6309 y FA(The)50 b(Uncooperati)n(v)n(e)g(longjmp\(3\))4050 6553 y Fx(Ev)n(en)i(on)g(operating)h(systems)e(which)h(ha)m(v)n(e)f(w)n (orking)i Fr(POSIX)4050 6752 y Fx(functions,)43 b(our)g(approach)g(may) g(theoretically)g(still)h(not)f(w)n(ork,)4050 6952 y(because)97 b Fp(longjmp)p Fx(\(3\))g(does)h(not)h(cooperate.)223 b(F)n(or)99 b(in-)4050 7151 y(stance,)33 b(on)h(some)f(platforms)g(the) h(standard)f Fr(libc)g Fp(longjmp)p Fx(\(3\))4050 7350 y(branches)e(to)h(error)m(-handling)g(code)g(if)g(it)g(detects)f(that)h (the)f(caller)4050 7549 y(tries)39 b(to)g(jump)g Fr(up)h Fx(the)e(stack,)h Fr(i.e)n(.)p Fx(,)e(into)i(a)g(stack)f(frame)i(that)f (has)4050 7749 y(already)i(returned.)4299 7948 y(This)58 b(is)g(usually)f(implemented)h(by)g(comparing)g(the)g(cur)m(-)4050 8147 y(rent)d(stack)g(pointer)g(to)h(the)f(one)f(in)i(the)f Fp(jmp)p 6775 8147 50 7 v 58 w(buf)g Fx(structure.)4050 8346 y(That)62 b(is)f(wh)o(y)h(it)f(is)h(important)g(for)g(our)g (algorithm)g(to)f(return)4050 8546 y(from)41 b(the)f(signal)g(handler)g (and)g(this)g(w)n(ay)h(enter)f(the)g(\(dif)l(ferent\))4050 8745 y(stack)63 b(of)h(the)f(parent)h(thread.)117 b(In)64 b(practice,)k(the)c(implemen-)4050 8944 y(tation)54 b(in)f Fr(GNU)g(Pth)i Fx(sho)l(wed)d(that)i(then)f(one)h(no)f(longer)h(suf-) 4050 9144 y(fers)33 b(from)g(those)f(uncooperati)l(v)n(e)g Fp(longjmp)p Fx(\(3\))f(implementa-)4050 9343 y(tions,)36 b(b)m(ut)f(one)h(should)f(k)n(eep)g(this)h(point)g(in)f(mind)h(when)f (reach-)4050 9542 y(ing)60 b(e)l(v)n(en)f(more)h(uncooperati)l(v)n(e)f (v)l(ariants)h(on)g(esoteric)f(Unix)4050 9741 y(platforms.)49 b(If)34 b(it)f(still)h(occurs,)g(one)f(can)f(only)i(try)g(to)f(resume)g (the)4050 9941 y(operation)52 b(by)g(using)g(a)g(possibly)f(e)n (xisting)h(platform-speci\002c)4050 10140 y(error)42 b(handling)g(hook.)4050 10430 y FA(Garbage)49 b(at)h(Bottom)g(of)g (Stacks)4050 10675 y Fx(There)37 b(is)f(a)g(subtle)h(side-ef)l(fect)f (of)h(our)g(implementation:)49 b(there)4050 10874 y(remains)35 b(some)f(g)o(arbage)h(at)g(the)g(bottom)h(of)f(each)f(thread)h(stack.) 4050 11073 y(The)69 b(reason)e(is)h(that)h(if)g(a)f(signal)g(is)g(deli) l(v)n(ered,)74 b(the)68 b(operat-)4050 11272 y(ing)63 b(system)f(pushes)g(some)h(state)f(onto)h(the)g(stack,)k(which)c(is)p Black 3858 11872 a(8)p Black eop end %%Page: 9 9 TeXDict begin 9 8 bop Black Black 0 638 a Fx(restored)58 b(later)-7 b(,)62 b(when)57 b(the)h(signal)g(handler)g(returns.)100 b(But)58 b(al-)0 838 y(though)64 b(we)f(return)i(from)f(the)g(signal)f (handler)-7 b(,)69 b(we)64 b(jump)g(in)0 1037 y(ag)o(ain,)56 b(and)d(this)g(time)h(we)f(enter)g(not)g(directly)h(at)f(the)g(bottom)0 1236 y(of)35 b(the)g(stack,)g(because)f(of)h(the)g Fp(setjmp)p Fx(\(3\))e(in)j(the)f(trampoline)0 1435 y(function.)249 1647 y(Since)64 b(the)g(operating)g(system)g(has)f(to)h(capture)g(all)g (CPU)0 1846 y(re)n(gisters)44 b(\(including)i(those)e(that)h(are)g (ordinarily)h(scratch)e(re)n(g-)0 2046 y(isters)d(or)h(caller)m(-sa)m (v)n(e)e(re)n(gisters\),)g(there)i(can)f(be)g(a)g(f)n(air)i(amount)0 2245 y(of)g(memory)h(at)f(the)g(bottom)h(of)f(the)g(established)f (thread)h(stack.)0 2444 y(F)n(or)37 b(some)f(systems)f(this)i(can)f(be) g(e)l(v)n(en)g(up)h(to)f(1)h(KB)f(of)h(g)o(arbage)0 2643 y([27].)51 b(But)40 b(e)n(xcept)e(for)i(the)g(additional)g(memory)g (consumption)0 2843 y(it)i(does)f(not)g(hurt.)249 3054 y(W)-13 b(e)64 b(just)g(ha)m(v)n(e)f(to)h(k)n(eep)h(in)f(mind)h(this)f (additional)h(stack)0 3253 y(consumption)57 b(when)f(deciding)h(the)g (stack)f(size)g(\()p Fr(sk)p 3137 3253 50 7 v 60 w(size)p Fx(\).)96 b(A)0 3453 y(reasonable)49 b(stack)h(size)f(usually)h(is)g (between)g(16)g(and)g(32)h(KB.)0 3652 y(Less)62 b(is)g(neither)h (reasonable)e(nor)i(al)n(w)n(ays)f(allo)l(wed)g(\(current)0 3851 y(Unix)54 b(platforms)h(usually)e(require)i(a)e(stack)h(to)g(be)g (at)g(least)f(16)0 4050 y(KB)41 b(in)h(size\).)0 4386 y FA(Stack)50 b(Ov)n(er\003o)n(ws)0 4660 y Fx(There)98 b(is)f(a)h(noticeable)f(dif)l(ference)g(between)g(the)g(initial)0 4859 y Fp(main)p Fx(\(\))59 b(thread)i(and)f(the)g(e)n(xplicitly)g(spa) n(wned)f(threads:)89 b(the)0 5059 y(initial)63 b(thread)g(runs)g(on)f (the)h(standard)f(process)g(stack.)114 b(This)0 5258 y(stack)85 b(automatically)h(can)g(gro)l(w)g(under)g(Unix,)97 b(while)86 b(the)0 5457 y(stacks)44 b(of)h(the)g(spa)n(wned)f(threads)g (are)h(\002x)n(ed)g(in)g(size.)60 b(So)46 b(stack)0 5656 y(o)n(v)n(er\003o)l(ws)54 b(can)h(occur)h(for)g(the)g(spa)n(wned)e (threads.)93 b(This)55 b(im-)0 5856 y(plies)i(that)g(the)g(parent)g (has)f(to)h(mak)n(e)g(a)g(reasonable)f(guess)g(of)0 6055 y(the)45 b(threads)f(stack)g(space)f(requirement)i(already)f(at)h(spa)n (wning)0 6254 y(time.)249 6466 y(And)i(there)f(is)h(no)f(really)h (portable)g(solution)g(to)g(this)f(prob-)0 6665 y(lem,)65 b(because)59 b(e)l(v)n(en)g(if)i(the)f(thread)g(library')-9 b(s)61 b(scheduler)f(can)0 6864 y(detect)33 b(the)g(stack)g(o)n(v)n (er\003o)l(w)-11 b(,)34 b(it)f(cannot)h(easily)e(resize)h(the)g(stack.) 0 7063 y(The)70 b(reason)f(is)h(simply)g(that)g(the)f(stack)g (initialization)i(goes)0 7263 y(hand)55 b(in)h(hand)f(with)h(the)f (initialization)h(of)f(the)h(start)f(routine,)0 7462 y(as)49 b(we)h(discussed)e(before.)77 b(And)50 b(this)g(start)f (routine)i(has)e(to)h(be)0 7661 y(a)d(real)f(C)h(function)g(in)g(order) g(to)g Fr(call)p Fx(.)67 b(But)47 b(once)f(the)h(thread)f(is)0 7860 y(running,)54 b(there)d(no)g(longer)g(e)n(xists)f(such)h(an)g (entry)g(point.)80 b(So,)0 8060 y(e)l(v)n(en)62 b(if)h(the)g(scheduler) f(w)n(ould)i(be)e(able)h(to)g(gi)l(v)n(e)f(the)h(thread)0 8259 y(a)g(ne)l(w)f(enlar)m(ged)h(stack,)k(there)c(is)g(no)g(chance)f (to)h(restart)g(the)0 8458 y(thread)41 b(on)h(this)f(ne)l(w)g(stack.) 249 8670 y(Or)k(more)h(correct,)f(there)g(is)g(no)g Fr(portable)f Fx(w)n(ay)h(to)h(achie)l(v)n(e)0 8869 y(it.)k(As)35 b(with)i(the)f(pre) l(vious)g(problems,)h(there)f(is)g(a)h(non-portable)0 9068 y(solution.)101 b(That)59 b(is)e(wh)o(y)i(our)f(implementation)h (did)f(not)g(deal)0 9268 y(with)f(this)g(issue.)97 b(Instead)56 b(in)h(practice)g(one)g(usually)f(lets)h(the)0 9467 y(scheduler)35 b(just)g(detect)g(the)g(stack)f(o)n(v)n(er\003o)l(w)h(and)g(terminate)g (the)0 9666 y(thread.)54 b(This)42 b(is)h(done)f(by)g(using)g(a)h(red)f (zone)g(at)g(the)h(top)f(of)h(the)0 9865 y(stack)k(which)g(is)g(mark)n (ed)h(with)f(a)g(magic)h(v)l(alue)e(the)i(scheduler)0 10065 y(checks)40 b(between)h(thread)h(dispatching)f(operations.)249 10276 y(Resizing)d(solutions)h(are)g(only)h(possible)e(in)i (semi-portable)0 10475 y(w)n(ays.)79 b(One)50 b(approach)h(is)f(to)h (place)g(the)f(thread)h(stacks)f(into)h(a)0 10675 y(memory)j(mapped)f (area)g(\(see)g Fp(mmap)p Fx(\(2\)\))g(of)h(the)f(process)g(ad-)0 10874 y(dress)61 b(space)h(and)g(let)g(the)g(scheduler)g(catch)f Fp(SIGSEGV)f Fx(sig-)0 11073 y(nals.)139 b(When)71 b(such)f(a)h(signal) g(occurs,)77 b(because)70 b(of)h(a)g(stack)0 11272 y(o)n(v)n(er\003o)l (w)79 b(in)g(this)h(area,)88 b(the)79 b(scheduler)g(e)n(xplicitly)g (resizes)p Black Black 4050 638 a(the)69 b(memory)h(mapped)f(area.)133 b(This)70 b(resizing)f(can)f(be)h(done)4050 838 y(either)c(by)g(cop)n (ying)h(the)f(stack)f(contents)h(into)g(a)g(ne)l(w)g(lar)m(ger)4050 1037 y(area)57 b(which)h(is)f(then)h(re-mapped)g(to)g(the)f(old)h (address)f(or)h(via)4050 1236 y(an)65 b(e)l(v)n(en)e(more)i(ele)n(g)o (ant)f(w)n(ay)-11 b(,)70 b(as)64 b(the)h(v)n(endor)f(threading)h(li-) 4050 1435 y(braries)57 b(of)h Fr(Sun)g(Solaris)p Fx(,)j Fr(F)-9 b(r)j(eeBSD)57 b Fx(and)h Fr(DEC)f(T)-9 b(ru64)58 b Fx(do)g(it:)4050 1635 y(the)64 b(thread)g(stacks)f(are)h(allocated)f (inside)h(memory)g(mapped)4050 1834 y(areas)c(which)g(are)g(already)g (initially)i(a)e(fe)l(w)g(MB)g(in)h(\(virtual\))4050 2033 y(size)50 b(and)g(then)g(one)g(just)g(relies)g(on)h(the)f(virtual) h(memory)g(sys-)4050 2233 y(tem')-9 b(s)61 b(feature)h(that)f(only)h (the)f(actually)g(consumed)g(memory)4050 2432 y(space)40 b(is)i(mapped.)4050 2722 y FA(Startup)50 b(Routine)g(T)-18 b(ermination)4050 2967 y Fx(There)99 b(is)g(a)g(cruel)h Fp(abort)p Fx(\(3\))e(call)h(at)g(the)g(end)g(of)h(our)4050 3166 y Fp(mctx)p 4460 3166 V 58 w(create)p 5118 3166 V 58 w(boot)89 b Fx(function.)200 b(This)91 b(means,)102 b(if)91 b(the)4050 3365 y(startup)38 b(routine)h(w)n(ould)g(return,)h (the)e(process)f(is)i(aborted.)50 b(That)4050 3564 y(is)58 b(ob)n(viously)f(not)i(reasonable,)h(so)e(wh)o(y)g(ha)m(v)n(e)f(we)g (written)i(it)4050 3764 y(this)41 b(w)n(ay?)4299 3963 y(If)32 b(the)g(thread)f(returns)h(from)h(the)e(startup)h(routine,)h (it)f(should)4050 4162 y(be)45 b(cleanly)g(terminated.)62 b(But)45 b(it)h(cannot)e(terminate)i(itself)f(\(for)4050 4361 y(instance,)h(because)g(it)g(cannot)h(free)f(its)h(o)l(wn)f(stack) g(while)g(run-)4050 4561 y(ning)64 b(on)g(it,)70 b Fr(etc.)p Fx(\).)117 b(So)65 b(the)f(termination)g(handling)g(actually)4050 4760 y(is)57 b(the)f(task)h(of)g(the)g(thread)g(library)g(scheduler)-9 b(.)97 b(As)56 b(a)h(conse-)4050 4959 y(quence,)45 b(the)g(thread)g (spa)n(wning)g(function)g(of)h(a)f(thread)g(library)4050 5158 y(should)c(be)h(not)f(directly)h Fp(mctx)p 5945 5158 V 58 w(create)p Fx(.)4299 5358 y(Instead)56 b(the)g(thread)h(spa)n (wning)e(function)i(should)f(use)g(an)4050 5557 y(additional)38 b(trampoline)g(function)h(as)e(the)h(higher)m(-le)l(v)n(el)f(startup) 4050 5756 y(routine.)50 b(And)38 b(this)h(trampoline)f(function)h (performs)g(a)f(conte)n(xt)4050 5955 y(switch)52 b(back)g(into)h(the)g (thread)f(library)i(scheduler)d(before)i(the)4050 6155 y(lo)l(wer)m(-le)l(v)n(el)e(startup)g(routine)h(w)n(ould)g(return.)82 b(The)52 b(scheduler)4050 6354 y(then)g(can)g(safely)g(remo)n(v)n(e)g (the)g(thread)g(and)h(its)f(machine)g(con-)4050 6553 y(te)n(xt.)72 b(That)48 b(is)h(wh)o(y)f(the)h Fp(abort)p Fx(\(3\))e(call)h(is)g(ne)l(v)n(er)g(reached)g(in)4050 6752 y(practice)56 b(\(more)g(details)g(can)f(be)h(found)g(in)h(the)f (implementa-)4050 6952 y(tions)44 b(of)h Fp(pth)p 4910 6952 V 59 w(spawn)d Fx(and)i Fp(pth)p 6095 6952 V 59 w(exit)f Fx(in)i Fp(pth)p 7071 6952 V 58 w(lib.c)e Fx(of)4050 7151 y Fr(GNU)e(Pth)h Fx([25)q(]\))4050 7441 y FA(The)50 b(sigstack\(2\))f(F)-5 b(allback)50 b(Situation)4050 7686 y Fx(Not)230 b(all)h(platforms)g(pro)n(vide)f(the)g(standardized) 4050 7885 y Fp(sigaltstack)p Fx(\(2\).)266 b(Instead)114 b(the)n(y)f(at)i(least)e(pro)n(vide)4050 8084 y(the)104 b Fr(4.2BSD)g Fx(ancestor)g(function)g Fp(sigstack)p Fx(\(2\).)237 b(But)4050 8284 y(one)122 b(cannot)g(tri)l(vially)h (replace)e Fp(sigaltstack)p Fx(\(2\))e(by)4050 8483 y Fp(sigstack)p Fx(\(2\))63 b(in)k(this)e(situation,)72 b(because)64 b(in)i(contrast)g(to)4050 8682 y Fp(sigaltstack)p Fx(\(2\),)45 b(the)i(old)g Fp(sigstack)p Fx(\(2\))e(does)i(not)g(au-) 4050 8881 y(tomatically)64 b(handle)g(the)g(machine)f(dependent)g (direction)i(of)4050 9081 y(stack)41 b(gro)l(wth.)4299 9280 y(Instead,)90 b(the)81 b(caller)g(has)g(to)g(kno)l(w)h(the)f (direction)g(and)4050 9479 y(al)n(w)n(ays)103 b(call)h Fp(sigstack)p Fx(\(2\))d(with)i(the)h(address)e(of)i(the)4050 9678 y(bottom)j(of)h(the)e(stack.)247 b(So,)123 b(in)107 b(a)g(real-w)n(orld)g(imple-)4050 9878 y(mentation)80 b(one)g(\002rst)h(has)e(to)i(determine)f(the)g(direction)h(of)4050 10077 y(stack)f(gro)l(wth)h(in)f(order)h(to)g(use)f Fp(sigstack)p Fx(\(2\))e(as)i(a)h(re-)4050 10276 y(placement)102 b(for)g Fp(sigaltstack)p Fx(\(2\).)230 b(F)n(ortunately)102 b(this)4050 10475 y(is)108 b(easier)g(than)g(it)h(seems)e(on)i(the)f(\002rst)h (look)g(\(for)g(de-)4050 10675 y(tails)85 b(see)f(the)h(macros)g Fp(AC)p 5772 10675 V 59 w(CHECK)p 6331 10675 V 58 w(STACKGROWTH)c Fx(and)4050 10874 y Fp(AC)p 4260 10874 V 59 w(CHECK)p 4819 10874 V 58 w(STACKSETUP)65 b Fx(in)70 b(\002le)f Fp(aclocal.m4)c Fx(from)4050 11073 y Fr(GNU)47 b(Pth)h Fx([25]\).)69 b(Alternati)l(v)n(ely)47 b(if)h(one)f(can)f(af)l(ford)i (to)g(w)n(aste)4050 11272 y(memory)-11 b(,)38 b(one)f(can)f(use)h(an)g (ele)n(g)o(ant)f(trick:)50 b(to)37 b(set)g(up)g(a)f(stack)h(of)p Black 3858 11872 a(9)p Black eop end %%Page: 10 10 TeXDict begin 10 9 bop Black Black 0 638 a Fx(size)41 b Fe(N)18 b Fx(,)43 b(one)f(allocates)f(a)g(chunk)h(of)h(memory)f (\(starting)h(at)f(ad-)0 838 y(dress)35 b Fe(A)p Fx(\))h(of)g(size)f Fe(N)f Fd(\002)16 b Fc(2)35 b Fx(and)h(then)f(calls)g Fp(sigstack)p Fx(\(2\))f(with)0 1037 y(the)41 b(parameters)f Fr(sk)p 1162 1037 50 7 v 59 w(addr=)p Fe(A)35 b Fc(+)g Fe(N)59 b Fx(and)41 b Fr(sk)p 2583 1037 V 59 w(size=)p Fe(N)18 b Fx(,)40 b Fr(i.e)n(.)p Fx(,)f(one)0 1236 y(speci\002es)58 b(the)h(middle)g(of)g(the)g(memory)h(chunk)f(as)f(the)h(stack)0 1435 y(base.)0 1726 y FA(The)50 b(Blind)g(Alley)h(of)e(Brain-Dead)h (Platf)-5 b(orms)0 1970 y Fx(The)34 b(w)n(orld)g(w)n(ould)h(not)e(be)h (as)f(funn)n(y)h(as)f(it)h(is,)g(if)g(really)g(all)g(Unix)0 2170 y(platforms)48 b(w)n(ould)g(be)f(f)n(air)i(to)e(us.)69 b(Instead,)48 b(currently)g(at)g(least)0 2369 y(one)67 b(platform)h(e)n(xists)e(which)i(plays)e(unf)n(air:)105 b(unfortunately)-11 b(,)0 2568 y(ancient)62 b(v)n(ersions)g(of)g(the)h (popular)g Fr(GNU/Linux)p Fx(.)114 b(Although)0 2767 y(we)35 b(will)g(disco)n(v)n(er)f(that)h(it)g(both)g(pro)n(vides)g Fp(sigaltstack)p Fx(\(2\))0 2967 y(and)54 b Fp(sigstack)p Fx(\(2\),)g(our)h(approach)e(w)n(on')m(t)i(w)n(ork)g(on)f Fr(Linux)0 3166 y Fx(k)n(ernels)37 b(prior)g(to)g(v)n(ersion)f(2.2)g (and)h Fr(glibc)g Fx(prior)g(to)g(v)n(ersion)f(2.1.)249 3365 y(Wh)o(y?)87 b(Because)51 b(its)j Fr(libc)f Fx(pro)n(vides)g(only) g(stubs)g(of)h(these)0 3564 y(functions)j(which)g(al)n(w)n(ays)f (return)i(just)e Fp(-1)g Fx(with)i Fp(errno)d Fx(set)0 3764 y(to)i Fp(ENOSYS)p Fx(.)d(So,)60 b(this)d(de\002nitely)g(means)f (that)g(our)h(nifty)h(al-)0 3963 y(gorithm)85 b(is)f(useless)e(there,) 94 b(because)83 b(its)h(central)g(point)g Fr(is)0 4162 y Fp(sigaltstack)p Fx(\(2\)/)p Fp(sigstack)p Fx(\(2\).)68 b(Ne)l(v)n(ertheless)46 b(we)j(do)0 4361 y(not)36 b(need)g(to)h(gi)l(v) n(e)e(up.)50 b(At)36 b(least)f(not,)j(if)e(we,)h(for)g(a)f(single)f (brain-)0 4561 y(dead)61 b(platform,)66 b(accept)60 b(to)h(break)g(our) h(general)e(goal)h(of)h(not)0 4760 y(using)41 b(an)n(y)g(platform)h (dependent)f(code.)249 4959 y(So,)e(what)f(can)g(we)g(actually)g(do)h (here?)50 b(All)39 b(we)f(ha)m(v)n(e)f(to)h(do,)0 5158 y(is)d(to)h(\002ddle)f(around)h(a)f(little)h(bit)g(with)f(the)h (machine-dependent)0 5358 y Fp(jmp)p 310 5358 V 59 w(buf)46 b Fx(ingredients)h(\(by)h(poking)g(around)f(in)h Fp(setjmp.h)0 5557 y Fx(or)33 b(by)h(disassembling)e Fp(longjmp)p Fx(\(3\))f(in)i (the)g(deb)m(ugger\).)48 b(Usu-)0 5756 y(ally)38 b(one)g(just)g(has)g (to)g(do)h(a)f Fp(setjmp)p Fx(\(3\))e(to)j(get)f(an)g(initial)h(state)0 5955 y(in)49 b(the)g Fp(jmp)p 740 5955 V 59 w(buf)e Fx(structure)i(and) g(then)g(manually)g(adjust)g(tw)n(o)0 6155 y(of)70 b(its)g(\002elds:) 107 b(the)70 b(program)g(counter)g(\(usually)g(a)f(structure)0 6354 y(member)63 b(with)f(\223)p Fp(pc)p Fx(\224)g(in)g(the)h(name\))f (and)g(the)h(stack)f(pointer)0 6553 y(\(usually)41 b(a)h(structure)f (member)h(with)g(\223)p Fp(sp)p Fx(\224)e(in)h(the)h(name\).)249 6752 y(That)f(is)f(all)g(and)h(can)f(be)g(acceptable)f(for)i(a)f (real-w)n(orld)h(im-)0 6952 y(plementation)36 b(which)g(really)g(w)n (ants)g(to)g(co)n(v)n(er)f(mostly)h Fr(all)h Fx(plat-)0 7151 y(forms)h(\226)f(at)g(least)g(as)g(long)h(as)e(the)i(special)e (treatment)i(is)f(needed)0 7350 y(just)63 b(for)g(one)f(or)h(tw)n(o)g (platforms.)115 b(But)63 b(one)f(has)g(to)h(k)n(eep)g(in)0 7549 y(mind)d(that)f(it)g(at)g(least)g(breaks)f(one)h(of)h(the)f (initial)h(goals)e(and)0 7749 y(has)41 b(to)h(be)f(treated)g(as)g(a)g (last)g(chance)g(solution.)0 8039 y FA(Functions)49 b(sigsetjmp\(3\))h (and)f(siglongjmp\(3\))249 8284 y Fx(One)86 b(certainly)g(w)n(ants)g (the)g Fr(POSIX)94 b Fx(thread)86 b(semantics)0 8483 y(where)101 b(a)h(thread)g(has)f(its)h(o)l(wn)f(signal)h(mask.)232 b(As)101 b(al-)0 8682 y(ready)74 b(mentioned,)82 b(on)75 b(some)f(platforms)g Fp(setjmp)p Fx(\(3\))f(and)0 8881 y Fp(longjmp)p Fx(\(3\))c(do)h(not)h(pro)n(vide)f(this)h(and)f(instead) g(one)h(has)0 9081 y(to)47 b(e)n(xplicitly)h(call)f Fp(sigsetjmp)p Fx(\(3\))d(and)j Fp(siglongjmp)p Fx(\(3\))0 9280 y(instead.)224 b(There)99 b(is)g(only)h(one)f(snare:)166 b(on)100 b(some)f(plat-)0 9479 y(forms)74 b Fp(sigsetjmp)p Fx(\(3\)/)p Fp(siglongjmp)p Fx(\(3\))68 b(sa)m(v)n(e)k(also)i(in-)0 9678 y(formation)81 b(about)f(the)g(alternate)f(signals)g(stack.)166 b(So)80 b(here)0 9878 y(one)92 b(has)f(to)h(mak)n(e)g(sure)g(that)g(although)g (the)g(thread)g(dis-)0 10077 y(patching)76 b(later)h(uses)e Fp(sigsetjmp)p Fx(\(3\)/)p Fp(siglongjmp)p Fx(\(3\),)0 10276 y(the)87 b(thread)g(creation)g(step)f(in)i Fp(mctx)p 2395 10276 V 58 w(create)d Fx(still)i(uses)0 10475 y(plain)93 b Fp(setjmp)p Fx(\(3\)/)p Fp(longjmp)p Fx(\(3\))c(calls)j(for)h(the)f (trampo-)0 10675 y(line)g(trick.)204 b(One)92 b(just)g(has)g(to)g(be)g (careful)g(because)f(the)0 10874 y Fp(jmp)p 310 10874 V 59 w(buf)38 b Fx(and)i Fp(sigjmp)p 1587 10874 V 57 w(buf)f Fx(structures)g(cannot)h(be)f(mix)n(ed)0 11073 y(between)58 b(calls)g(to)g(the)h Fp(sigsetjmp)p Fx(\(3\)/)p Fp(siglongjmp)p Fx(\(3\))0 11272 y(and)41 b Fp(setjmp)p Fx(\(3\)/)p Fp(longjmp)p Fx(\(3\).)p Black Black 4050 638 a FA(Mor)l(e)51 b(Machine)f(Context)g(Ingr)l(edients)4050 908 y Fx(Finally)-11 b(,)68 b(for)c(a)e(real-w)n(orld)i(threading)f (implementation)g(one)4050 1107 y(usually)h(w)n(ants)g(to)h(put)f(more) h(state)e(into)i(the)f(machine)g(con-)4050 1306 y(te)n(xt)87 b(structure)g Fp(mctx)p 5462 1306 V 58 w(t)p Fx(.)188 b(F)n(or)87 b(instance)f(to)i(ful\002ll)g(more)4050 1505 y Fr(POSIX)53 b Fx(threading)45 b(semantics,)f(it)h(is)g(reasonable)f (to)g(also)h(sa)m(v)n(e)4050 1705 y(and)76 b(restore)g(the)g(global)g Fp(errno)e Fx(v)l(ariable.)154 b(All)77 b(this)f(can)4050 1904 y(be)66 b(easily)f(achie)l(v)n(ed)g(by)h(e)n(xtending)f(the)h Fp(mctx)p 7005 1904 V 58 w(t)g Fx(structure)4050 2103 y(with)61 b(additional)g(\002elds)g(and)f(by)h(making)g(the)g Fp(mctx)p 7312 2103 V 58 w(save)p Fx(,)4050 2302 y Fp(mctx)p 4460 2302 V 58 w(restore)77 b Fx(and)h Fp(mctx)p 6013 2302 V 59 w(switch)e Fx(functions)j(to)g(be)4050 2502 y(a)n(w)n(are)41 b(of)h(them.)4050 3033 y FA(3.6)198 b(Related)51 b(W)-15 b(ork)4050 3363 y Fx(Beside)97 b Fr(GNU)h(Pth)h Fx([25],)112 b(there)99 b(are)f(other)g(multithread-) 4050 3562 y(ing)82 b(libraries)g(which)g(use)f(v)l(ariants)h(of)g(the)g (presented)f(ap-)4050 3762 y(proach)33 b(for)h(implementing)g(machine)f (conte)n(xts)g(in)g(user)m(-space.)4050 3961 y(Most)99 b(notably)g(there)g(are)k(R)r Fm(O)8 b(B)g(E)g(RT)107 b Fx(S)8 b(.)108 b(T)8 b Fm(H)g(A)q(U)t Fx(')-9 b(s)100 b Fr(Really)4050 4160 y(Simple)78 b(Thr)-6 b(eads)77 b Fx(\()p Fr(r)n(sthr)-6 b(eads)p Fx(,)85 b([27]\))79 b(package)d(which)i(uses)4050 4359 y Fp(sigaltstack)p Fx(\(2\))42 b(in)j(a)g(v)n(ery)g(similar)h(w)n(ay)f(for)h(thread)f (cre-)4050 4559 y(ation,)56 b(and)i(K)t Fm(O)s(T)l(A)j Fx(A)8 b Fm(B)g(E)t Fx(')-9 b(s)54 b Fr(P)-13 b(ortable)53 b(Thr)-6 b(ead)54 b(Libr)n(ary)e Fx(\()p Fr(PTL)p Fx(,)4050 4758 y([24]\))34 b(which)f(uses)f(a)g Fp(sigstack)p Fx(\(2\))f (approach.)48 b(But)32 b(because)4050 4957 y(their)52 b(approaches)f(handle)h(the)g(signal)f(handler)h(scope)f(dif)l(fer)m(-) 4050 5156 y(ently)-11 b(,)64 b(the)n(y)59 b(are)h(not)g(able)f(to)h (achie)l(v)n(e)e(the)i(same)f(amount)h(of)4050 5356 y(portability)34 b(and)f(this)g(w)n(ay)g(do)g(not)h(w)n(ork)f(for)h(instance)e(on)h (some)4050 5555 y(System-V)-17 b(-deri)l(v)n(ed)42 b(platforms.)4050 6086 y FA(3.7)198 b(Summary)49 b(&)h(A)-20 b(v)n(ailability)4050 6416 y Fx(W)-13 b(e)42 b(ha)m(v)n(e)f(presented)h(a)g(pragmatic)g(and)h (mostly)f(portable)h(f)n(all-)4050 6615 y(back)38 b(approach)f(for)i (implementing)f(the)g(machine)g(conte)n(xt)f(for)4050 6815 y(user)m(-space)61 b(threads,)68 b(based)62 b(entirely)i(on)f (Unix)g(system)f(and)4050 7014 y(ANSI-C)48 b(language)f(f)n(acilities.) 70 b(The)48 b(approach)f(w)n(as)h(success-)4050 7213 y(fully)55 b(tested)f(in)h(practice)g(on)f(a)h(wide)f(range)h(of)g (Unix)g(\003a)m(v)m(ors)4050 7412 y(by)36 b Fr(GNU)f(Pth)h Fx(and)f(should)h(also)f(adapt)g(to)h(the)f(remaining)h(Unix)4050 7612 y(platforms)c(as)g(long)g(as)g(the)n(y)f(adhere)h(to)g(the)g(rele) l(v)l(ant)f(standards.)4299 7821 y(The)103 b Fr(GNU)f(Pth)h Fx(package)f(is)h(distrib)m(uted)f(under)h(the)4050 8020 y(GNU)64 b(Library)i(General)e(Public)h(License)g(\(LGPL)h(2.1\))e(and) 4050 8219 y(freely)83 b(a)m(v)l(ailable)f(from)i Fr(http://www)-12 b(.gnu.or)-6 b(g/softwar)g(e/pth/)4050 8419 y Fx(and)41 b Fr(ftp://ftp.gnu.or)-6 b(g/gnu/pth/)p Fx(.)4050 8949 y FA(3.8)198 b(Ackno)n(wledgements)4050 9280 y Fx(I)35 b(w)n(ould)f(lik)n(e)h(to)g(thank)j(R)r Fm(O)8 b(B)g(E)g(RT)43 b Fx(S)8 b(.)43 b(T)8 b Fm(H)g(A)q(U)t Fx(,)40 b(D)r Fm(A)-10 b(V)8 b(I)g(D)43 b Fx(B)7 b Fm(U)h(T)g(E)g(N)g Fx(-)4054 9479 y Fm(H)g(O)g(F)t Fx(,)52 b(M)8 b Fm(A)g(RT)g(I)g(N)56 b Fx(K)8 b Fm(R)g(A)g(E)g(M)g(E)g(R)t Fx(,)53 b(E)8 b Fm(R)g(I)g(C)57 b Fx(N)8 b Fm(E)g(W)g(T)e(O)i(N)52 b Fx(and)f(B)8 b Fm(R)s(U)g(N)g(O)4054 9678 y Fx(H)g Fm(A)g(I)g(B)g(L)g (E)66 b Fx(for)c(their)f(comments)g(which)f(helped)h(to)g(write)g(the) 4050 9878 y(initial)c(v)n(ersion)f(of)h(this)g(paper)-9 b(.)96 b(Additionally)-11 b(,)61 b(credit)c(has)f(to)4050 10077 y(be)42 b(gi)l(v)n(en)g(to)k(C)8 b Fm(H)g(R)g(I)g(S)g(T)e(O)i(P)g (H)g(E)g(R)51 b Fx(S)8 b Fm(M)g(A)g(L)g(L)48 b Fx(and)42 b(the)h(USENIX)g(re-)4050 10276 y(vie)l(wers)e(for)h(their)g(in)-7 b(v)l(aluable)42 b(feedback)f(which)h(allo)l(wed)f(this)4050 10475 y(paper)58 b(to)h(be)g(e)n(xtended,)i(cleaned)d(up)h(and)f (\002nally)h(published)4050 10675 y(at)47 b(the)h(USENIX)g(Annual)f(T) -12 b(echnical)48 b(Conference)e(2000.)69 b(Fi-)4050 10874 y(nally)-11 b(,)56 b(thanks)e(go)f(to)h(all)g(users)e(of)i Fr(GNU)g(Pth)g Fx(for)g(their)g(feed-)4050 11073 y(back)39 b(on)h(the)f(implementation,)h(which)f(helped)h(in)f(\002ne-tuning)4050 11272 y(the)i(presented)g(approach.)1859 b([)p Fb(rse)p Fx(])p Black 3817 11872 a(10)p Black eop end %%Page: 11 11 TeXDict begin 11 10 bop Black Black 0 638 a Fs(Refer)l(ences)p Black 141 976 a Fv([1])p Black 82 w Fu(POSIX)38 b(1003.1c)e(Thr)-6 b(eading)p Fv(,)37 b(IEEE)h(POSIX)f(1003.1c-1995,)398 1158 y(ISO/IEC)g(9945-1:1996)p Black 141 1397 a([2])p Black 86 w(M)8 b(.)g(E)g(.)93 b(C)8 b Fa(O)g(N)g(W)-6 b(A)h(Y)t Fv(:)141 b Fu(Design)86 b(of)g(a)g(separ)n(able)f(tr)n (ansition-)398 1580 y(dia)o(gr)n(am)35 b(compiler)-17 b(.)p Fv(,)38 b(Comm.)g(A)-6 b(CM)37 b(6:7,)f(1963,)h(p.396-408)p Black 141 1819 a([3])p Black 86 w(E)8 b(.)g(W)-6 b(.)45 b(D)8 b Fa(I)g(J)g(K)g(S)g(T)g(R)g(A)t Fv(:)41 b Fu(Co-oper)n(ating)35 b(sequential)h(pr)-7 b(ocesses)p Fv(,)39 b(in)398 2001 y(F)-12 b(.)51 b(Genuys)e(\(Ed.\),)k Fu(Pr)-7 b(o)o(gr)n(amming)48 b(Langua)o(g)o(es)p Fv(,)j(N)-5 b(A)-17 b(T)m(O)51 b(Ad-)398 2184 y(v)l(anced)43 b(Study)f(Institute,)g(Academic)h(Press,)i(London,) e(1965,)398 2367 y(p.42-112.)p Black 141 2605 a([4])p Black 86 w(B)8 b(.)70 b(N)8 b Fa(I)g(C)g(H)g(O)g(L)g(S)g Fv(,)73 b(D)8 b(.)70 b(B)7 b Fa(U)h(T)g(T)g(L)g(A)g(R)g Fv(,)72 b(J)8 b(.)g(P)-9 b(.)70 b(F)n Fa(A)8 b(R)g(R)g(E)g(L)t Fv(:)93 b Fu(Pthr)-6 b(eads)398 2788 y(Pr)f(o)o(gr)n(amming)43 b(-)h(A)h(POSIX)f(Standar)-6 b(d)43 b(for)h(Better)h(Multipr)-7 b(o-)398 2971 y(cessing)p Fv(,)38 b(O'Reilly)-10 b(,)36 b(1996;)g(ISBN)h(1-56592-115-1)p Black 141 3209 a([5])p Black 86 w(B)8 b(.)55 b(L)8 b Fa(E)g(W)g(I)g(S)g Fv(,)55 b(D)8 b(.)55 b(J)8 b(.)55 b(B)8 b Fa(E)g(R)g(G)t Fv(:)63 b Fu(Thr)-6 b(eads)48 b(Primer)f(-)h(A)f(Guide)g(T)-14 b(o)398 3392 y(Multithr)-6 b(eaded)33 b(Pr)-7 b(o)o(gr)n(amming)p Fv(,)33 b(Prentice)g(Hall,)h(1996;)g(ISBN)398 3575 y(0-13-443698-9)p Black 141 3813 a([6])p Black 86 w(S)8 b(.)40 b(J)8 b(.)39 b(N)8 b Fa(O)g(R)q(T)e(O)i(N)g Fv(,)37 b(M)8 b(.)39 b(D)8 b(.)40 b(D)8 b Fa(I)g(P)m(A)g(S)g(Q)f(U)s(A)h(L)g(E)t Fv(:)39 b Fu(Thr)-6 b(ead)32 b(T)-8 b(ime)31 b(-)h(The)398 3996 y(Multithr)-6 b(eaded)28 b(Pr)-7 b(o)o(gr)n(amming)26 b(Guide)p Fv(,)k(Prentice)f(Hall,)h(1997;)398 4179 y(ISBN)37 b(0-13-190067-6)p Black 141 4417 a([7])p Black 86 w(D)8 b(.)49 b(R)8 b(.)48 b(B)7 b Fa(U)h(T)g(E)g(N)g(H)g(O)g(F)t Fv(:)50 b Fu(Pr)-7 b(o)o(gr)n(amming)39 b(with)h(POSIX)h(Thr)-6 b(eads)p Fv(,)398 4600 y(Addison)36 b(W)-12 b(esle)n(y)i(,)38 b(1997;)e(ISBN)h(0-201-63392-2)p Black 141 4839 a([8])p Black 86 w(S)8 b(.)56 b(P)8 b Fa(R)g(A)g(S)g(A)g(D)t Fv(:)65 b Fu(Multithr)-6 b(eading)46 b(Pr)-7 b(o)o(gr)n(amming)46 b(T)-14 b(ec)n(hniques)p Fv(,)398 5022 y(McGra)n(w-Hill,)36 b(1996;)g(ISBN)h(0-079-12250-7)p Black 141 5260 a([9])p Black 86 w(S)8 b(.)66 b(K)8 b Fa(L)g(E)g(I)g(N)g(M)g(A)g(N)g Fv(,)68 b(B)8 b(.)65 b(S)8 b Fa(M)g(A)g(L)g(D)g(E)g(R)g(S)g Fv(,)68 b(D)8 b(.)66 b(S)8 b Fa(H)g(A)g(H)t Fv(:)86 b Fu(Pr)-7 b(o)o(gr)n(am-)398 5443 y(ming)55 b(with)g(Thr)-6 b(eads)p Fv(,)61 b(Prentice)55 b(Hall,)61 b(1995;)i(ISBN)56 b(0-131-)398 5626 y(72389-8)p Black 66 5864 a([10])p Black 86 w(C)8 b(.)g(J)g(.)82 b(N)8 b Fa(O)g(R)q(T)g(H)g(R)s(U)g(P)t Fv(:)116 b Fu(Pr)-7 b(o)o(gr)n(amming)73 b(W)-8 b(ith)74 b(Unix)g(Thr)-6 b(eads)p Fv(,)398 6047 y(John)37 b(W)-6 b(ile)n(y)36 b(&)i(Sons,)f(1996;)f(ISBN)h(0-471-13751-0)p Black 66 6286 a([11])p Black 86 w(P)-9 b(.)61 b(B)s Fa(A)8 b(R)q(T)e(O)i(N)g Fv(-)g(D)s Fa(A)-8 b(V)8 b(I)g(S)g Fv(,)58 b(D)8 b(.)60 b(M)8 b Fa(C)g Fv(N)s Fa(A)g(M)g(E)g(E)g Fv(,)61 b(R)8 b(.)60 b(V)-10 b Fa(A)8 b(S)g(W)-6 b(A)8 b(N)g(I)g Fv(,)61 b(E)8 b(.)402 6468 y(L)g Fa(A)g(Z)g(O)t(W)g(S)g(K)g (A)t Fv(:)39 b Fu(Adding)28 b(Sc)n(heduler)h(Activations)e(to)i(Mac)n (h)g(3.0)p Fv(,)398 6651 y(Uni)l(v)n(ersity)51 b(of)i(W)-12 b(ashington,)55 b(1992;)k(T)-10 b(echnical)53 b(Report)f(92-)398 6834 y(08-03)p Black 66 7072 a([12])p Black 86 w(D)8 b(.)37 b(S)8 b Fa(T)g(E)g(I)g(N)g Fv(,)36 b(D)8 b(.)36 b(S)8 b Fa(H)g(A)g(H)t Fv(:)40 b Fu(Implementing)27 b(Lightwight)f(Thr) -6 b(eads)p Fv(,)398 7255 y(SunSoft)36 b(Inc.,)i(1992)e(\(published)g (at)h(USENIX'92\).)p Black 66 7494 a([13])p Black 86 w(W)-6 b(.)8 b(R)g(.)g(S)g Fa(T)g(E)g(V)g(E)g(N)g(S)t Fv(:)103 b Fu(Advanced)68 b(Pr)-7 b(o)o(gr)n(amming)66 b(in)h(the)g(Unix)398 7676 y(En)-6 b(vir)f(onment)p Fv(,)114 b(Addison-W)-12 b(esle)n(y)i(,)114 b(1992;)128 b(ISBN)99 b(0-201-)398 7859 y(56317-7)p Black 66 8098 a([14])p Black 86 w(D)8 b(.)84 b(L)8 b Fa(E)g(W)g(I)g(N)g(E)t Fv(:)121 b Fu(POSIX)77 b(Pr)-7 b(o)o(gr)n(ammer')h(s)76 b(Guide:)123 b(Writing)398 8281 y(P)-12 b(ortable)67 b(Unix)g(Pr)-7 b(o)o(gr)n(ams)p Fv(,)74 b(O'Reilly)65 b(&)j(Associates,Inc.,)398 8463 y(1994;)36 b(ISBN)h(0-937175-73-0)p Black 66 8702 a([15])p Black 86 w(B)8 b Fa(RY)-6 b(A)8 b(N)274 b Fv(O)8 b(')g(S)g Fa(U)g(L)g(L)g(I)g(V)-8 b(A)8 b(N)t Fv(:)504 b Fu(F)-8 b(r)i(equently)268 b(ask)o(ed)398 8885 y(questions)369 b(for)h(comp.os.r)-6 b(esear)g(c)n(h)p Fv(,)456 b(1995;)398 9067 y(http://www)-10 b(.serpentine.com/)p Fk(\230)p Fv(bos/os-f)o(aq/,)398 9250 y (ftp://rtfm.mit.edu/pub/usenet/comp.os.research/)p Black 66 9489 a([16])p Black 86 w(S)8 b Fa(U)g(N)50 b Fv(M)8 b Fa(I)g(C)g(R)s(O)g(S)g(Y)g(S)g(T)g(E)g(M)g(S)g Fv(,)46 b(I)8 b Fa(N)g(C)t Fv(:)54 b Fu(Thr)-6 b(eads)42 b(F)-8 b(r)i(equently)43 b(Ask)o(ed)398 9671 y(Questions)p Fv(,)219 b(1995,)g(http://www.sun.com/w)o(orkshop/-)398 9854 y(threads/f)o (aq.html)p Black Black Black 4116 638 a([17])p Black 86 w(B)8 b Fa(RY)-6 b(A)8 b(N)156 b Fv(O)8 b(')g(S)g Fa(U)g(L)g(L)g(I)g(V)-8 b(A)8 b(N)t Fv(:)266 b Fu(F)-8 b(r)i(equently)150 b(ask)o(ed)g(ques-)4448 821 y(tions)266 b(for)g(comp.pr)-7 b(o)o(gr)n(amming)n(.thr)h(eads)p Fv(,)323 b(1997;)4448 1004 y(http://www)-10 b(.serpentine.com/)p Fk(\230)p Fv(bos/threads-f)o(aq/.)p Black 4116 1253 a([18])p Black 86 w(B)8 b Fa(I)g(L)167 b Fv(L)8 b Fa(E)g(W)g(I)g(S)t Fv(:)290 b Fu(F)-8 b(r)i(equently)160 b(ask)o(ed)g(questions)g(for)4448 1435 y(comp.pr)-7 b(o)o(gr)n(amming)n(.thr)h(eads)p Fv(,)418 b(1999;)495 b(http://-)4448 1618 y(www)-10 b(.lambdacs.com/ne)l (wsgroup/F)f(A)j(Q.html)p Black 4116 1867 a([19])p Black 86 w(N)8 b Fa(U)g(M)g(E)g(R)g(I)g(C)116 b Fv(Q)7 b Fa(U)h(E)g(S)g(T)116 b Fv(I)8 b Fa(N)g(C)t Fv(:)193 b Fu(Multithr)-6 b(eading)109 b(-)i(De\002ni-)4448 2050 y(tions)118 b(and)h(Guidelines)p Fv(;)158 b(1998;)h(http://www)-10 b(.numeric-)4448 2233 y(quest.com/lang/multi-frame.html)p Black 4116 2482 a([20])p Black 86 w(T)8 b Fa(H)g(E)85 b Fv(O)8 b Fa(P)g(E)g(N)84 b Fv(G)8 b Fa(R)s(O)g(U)g(P)t Fv(:)125 b Fu(The)79 b(Single)d(Unix)i (Speci\002cation,)4448 2664 y(V)-17 b(er)o(sion)91 b(2)f(-)h(Thr)-6 b(eads)p Fv(;)117 b(1997;)f(http://www.opengroup-)4448 2847 y(.or)m(g/onlinepubs/007908799/xsh/threads.html)p Black 4116 3096 a([21])p Black 86 w(S)8 b Fa(U)g(N)83 b Fv(M)8 b Fa(I)g(C)g(R)s(O)g(S)g(Y)g(S)g(T)g(E)g(M)g(S)79 b Fv(I)8 b Fa(N)g(C)t Fv(:)121 b Fu(SMI)76 b(Thr)-6 b(ead)77 b(Resour)-6 b(ces)p Fv(;)4448 3279 y(http://www)c(.sun.com/w)o (orkshop/threads)p Black 4116 3528 a([22])p Black 86 w(F)8 b Fa(R)g(A)g(N)g(K)59 b Fv(M)8 b Fa(U)g(E)g(L)g(L)g(E)g(R)t Fv(:)73 b Fu(FSU)53 b(pthr)-6 b(eads)p Fv(;)59 b(1997;)g(http://www-) 4448 3710 y(.cs.fsu.edu/)p Fk(\230)p Fv(mueller/pthreads/)p Black 4116 3959 a([23])p Black 86 w(C)8 b Fa(H)g(R)g(I)g(S)169 b Fv(P)8 b Fa(R)s(O)r(V)g(E)g(N)g(Z)g(A)g(N)g(O)t Fv(:)295 b Fu(MIT)164 b(pthr)-6 b(eads)p Fv(;)227 b(1993;)4448 4142 y(http://www)-10 b(.mit.edu/people/prov)n(en/pthreads.html)4448 4325 y(\(old\),)418 b(http://www.humanf)o(actor.com/pthreads/mit-)4448 4507 y(pthreads.html)36 b(\(updated\))p Black 4116 4756 a([24])p Black 86 w(K)t Fa(O)s(T)m(A)58 b Fv(A)8 b Fa(B)g(E)t Fv(:)73 b Fu(P)-12 b(ortable)51 b(Thr)-6 b(eading)51 b(Libr)n(ary)g Fv(\(PTL\);)h(1999;)4448 4939 y (http://www.media.osaka-cu.ac.jp/)p Fk(\230)p Fv(k-abe/PTL/)p Black 4116 5188 a([25])p Black 86 w(R)8 b Fa(A)g(L)g(F)40 b Fv(S)8 b(.)41 b(E)8 b Fa(N)g(G)g(E)g(L)g(S)g(C)g(H)g(A)g(L)g(L)t Fv(:)39 b Fu(GNU)33 b(P)-12 b(ortable)33 b(Thr)-6 b(eads)34 b Fv(\(Pth\);)4448 5371 y(1999;)105 b(http://www.gnu.or)m(g/softw)o (are/pth/,)88 b(ftp://ftp.gnu-)4448 5553 y(.or)m(g/gnu/pth/)p Black 4116 5802 a([26])p Black 86 w(M)8 b Fa(I)g(C)g(H)g(A)g(E)g(L)73 b Fv(T)m(.)j(P)8 b Fa(E)g(T)g(E)g(R)g(S)g(O)g(N)t Fv(:)105 b Fu(POSIX)68 b(and)g(DCE)h(Thr)-6 b(eads)4448 5985 y(F)-16 b(or)115 b(Linux)e Fv(\(PCThreads\);)152 b(1995;)e(http://members.aa-) 4448 6168 y(.net/)p Fk(\230)p Fv(mtp/PCthreads.html)p Black 4116 6417 a([27])p Black 86 w(R)s Fa(O)8 b(B)g(E)g(R)q(T)54 b Fv(S)8 b(.)58 b(T)8 b Fa(H)g(A)q(U)t Fv(:)71 b Fu(Really)50 b(Simple)g(Thr)-6 b(eads)50 b Fv(\(rsthreads\);)4448 6599 y(1996;)36 b(ftp://ftp.ai.mit.edu/pub/rst/)p Black 4116 6849 a([28])p Black 86 w(J)8 b Fa(O)g(H)g(N)189 b Fv(B)8 b Fa(I)g(R)g(R)g(E)g(L)g(L)t Fv(:)329 b Fu(F)-8 b(r)i(eeBSD)183 b(uthr)-6 b(eads)p Fv(;)253 b(1998;)4448 7031 y(ftp://ftp.freebsd.or)m(g/pub/FreeBSD/FreeBSD-)4448 7214 y(current/src/lib/libc)p 5565 7214 45 7 v 50 w(r/uthread/)p Black 4116 7463 a([29])p Black 86 w(X)8 b Fa(A)-8 b(V)8 b(I)g(E)g(R)92 b Fv(L)8 b Fa(E)g(R)s(O)r(Y)t Fv(:)144 b Fu(The)88 b(LinuxThr)-6 b(eads)87 b(libr)n(ary)p Fv(;)112 b(1999;)4448 7646 y(http://pauillac.inria.fr/)p Fk(\230)p Fv(xlero)o(y/linuxth)o(reads/)p Black 4116 7895 a([30])p Black 86 w(I)8 b(B)g(M)t(:)34 b Fu(AIX)j(V)-17 b(er)o(sion)36 b(4.3)g(Gener)n(al)g(Pr)-7 b(o)o(gr)n(amming)34 b(Concepts:)4448 8077 y(Writing)110 b(and)f(Deb)m(ug)o(ging)g(Pr)-7 b(o)o(gr)n(ams;)146 b(Under)o(standing)4448 8260 y(Thr)-6 b(eads)p Fv(;)81 b(1998;)d(http://www.rs6000.ibm.com/doc)p 7435 8260 V 50 w(link/-)4448 8443 y(en)p 4598 8443 V 54 w(US/a)p 4951 8443 V 54 w(doc)p 5221 8443 V 53 w(lib/aixpr)m (ggd/genprogc/understanding-)p 4448 8625 V 4493 8625 a(threads.htm)p Black 4116 8874 a([31])p Black 82 w Fu(Netscape)218 b(P)-12 b(ortable)218 b(Runtime)f Fv(\(NSPR\);)g(http://-)4448 9057 y(www)-10 b(.mozilla.or)m(g/docs/refList/refNSPR/,)493 b(http://-)4448 9240 y(lxr)-8 b(.mozilla.or)m(g/seamonk)o(e)n (y/source/nsprpub/)p Black 4116 9489 a([32])p Black 86 w(R)8 b Fa(A)g(L)g(F)90 b Fv(S)8 b(.)91 b(E)8 b Fa(N)g(G)g(E)g(L)g(S)g (C)g(H)g(A)g(L)g(L)t Fv(:)134 b Fu(Simple)83 b(Mac)n(hine)g(Conte)m(xt) 4448 9671 y(Benc)n(hmark)s Fv(;)142 b(2000;)g(http://www)-10 b(.gnu.or)m(g/softw)o(are/pth-)4448 9854 y(/smcb)k(.tar)e(.gz)p Black 3817 11872 a Fx(11)p Black eop end %%Trailer userdict /end-hook known{end-hook}if %%EOF @ 1.8 log @*** empty log message *** @ text @d2 2 a3 2 %%Creator: dvips(k) 5.86 Copyright 1999 Radical Eye Software %%Title: pmt.dvi d6 1 a6 1 %%BoundingBox: 0 0 612 792 d8 3 a10 1 %%+ Courier Times-BoldItalic Courier-Bold Courier-BoldOblique Helvetica d13 4 a16 4 %DVIPSCommandLine: dvips -t letter pmt.dvi %DVIPSParameters: dpi=600, compressed %DVIPSSource: TeX output 2000.06.18:1110 %%BeginProcSet: texc.pro d35 4 a38 16 1 add N}if}B/id 0 N/rw 0 N/rc 0 N/gp 0 N/cp 0 N/G 0 N/CharBuilder{save 3 1 roll S A/base get 2 index get S/BitMaps get S get/Cd X pop/ctr 0 N Cdx 0 Cx Cy Ch sub Cx Cw add Cy setcachedevice Cw Ch true[1 0 0 -1 -.1 Cx sub Cy .1 sub]/id Ci N/rw Cw 7 add 8 idiv string N/rc 0 N/gp 0 N/cp 0 N{ rc 0 ne{rc 1 sub/rc X rw}{G}ifelse}imagemask restore}B/G{{id gp get/gp gp 1 add N A 18 mod S 18 idiv pl S get exec}loop}B/adv{cp add/cp X}B /chg{rw cp id gp 4 index getinterval putinterval A gp add/gp X adv}B/nd{ /cp 0 N rw exit}B/lsh{rw cp 2 copy get A 0 eq{pop 1}{A 255 eq{pop 254}{ A A add 255 and S 1 and or}ifelse}ifelse put 1 adv}B/rsh{rw cp 2 copy get A 0 eq{pop 128}{A 255 eq{pop 127}{A 2 idiv S 128 and or}ifelse} ifelse put 1 adv}B/clr{rw cp 2 index string putinterval adv}B/set{rw cp fillstr 0 4 index getinterval putinterval adv}B/fillstr 18 string 0 1 17 {2 copy 255 put pop}for N/pl[{adv 1 chg}{adv 1 chg nd}{1 add chg}{1 add chg nd}{adv lsh}{adv lsh nd}{adv rsh}{adv rsh nd}{1 add adv}{/rc X nd}{ 1 add set}{1 add clr}{adv 2 chg}{adv 2 chg nd}{pop nd}]A{bind pop} forall N/D{/cc X A type/stringtype ne{]}if nn/base get cc ctr put nn d62 3 a64 1 %%BeginProcSet: 8r.enc d66 9 a74 7 % author = "S. Rahtz, P. MacKay, Alan Jeffrey, B. Horn, K. Berry", % version = "0.6", % date = "1 July 1998", % filename = "8r.enc", % email = "tex-fonts@@@@tug.org", % docstring = "Encoding for TrueType or Type 1 fonts % to be used with TeX." d77 1 a77 1 % Idea is to have all the characters normally included in Type 1 fonts d79 2 a80 1 % Standard Encoding + ISO Latin 1 + extra characters from Lucida. d98 1 d100 23 a122 7 % (4) Remaining positions left undefined are for use in (hopefully) % upward-compatible revisions, if someday more characters are generally % available. % % (5) hyphen appears twice for compatibility with both % ASCII and Windows. % d124 5 a128 8 % 0x00 (encoded characters from Adobe Standard not in Windows 3.1) /.notdef /dotaccent /fi /fl /fraction /hungarumlaut /Lslash /lslash /ogonek /ring /.notdef /breve /minus /.notdef % These are the only two remaining unencoded characters, so may as % well include them. /Zcaron /zcaron d130 5 a134 9 /caron /dotlessi % (unusual TeX characters available in, e.g., Lucida Bright) /dotlessj /ff /ffi /ffl /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef % very contentious; it's so painful not having quoteleft and quoteright % at 96 and 145 that we move the things normally found there to here. /grave /quotesingle % 0x20 (ASCII begins) d137 2 a138 1 /parenleft /parenright /asterisk /plus /comma /hyphen /period /slash d140 4 a143 2 /zero /one /two /three /four /five /six /seven /eight /nine /colon /semicolon /less /equal /greater /question d145 4 a148 1 /at /A /B /C /D /E /F /G /H /I /J /K /L /M /N /O d150 4 a153 2 /P /Q /R /S /T /U /V /W /X /Y /Z /bracketleft /backslash /bracketright /asciicircum /underscore d155 4 a158 1 /quoteleft /a /b /c /d /e /f /g /h /i /j /k /l /m /n /o d160 4 a163 3 /p /q /r /s /t /u /v /w /x /y /z /braceleft /bar /braceright /asciitilde /.notdef % rubout; ASCII ends d165 1 a165 1 /.notdef /.notdef /quotesinglbase /florin d168 1 a168 1 /OE /.notdef /.notdef /.notdef d173 1 a173 1 /oe /.notdef /.notdef /Ydieresis d175 1 a175 2 /.notdef % nobreakspace /exclamdown /cent /sterling d178 1 a178 4 /logicalnot /hyphen % Y&Y (also at 45); Windows' softhyphen /registered /macron d185 2 a186 1 /Agrave /Aacute /Acircumflex /Atilde /Adieresis /Aring /AE /Ccedilla d206 1 d208 1 a208 1 %%BeginProcSet: texps.pro d212 11 a222 10 exec 0 exch 5 -1 roll VResolution Resolution div mul neg 0 0]/Metrics exch def dict begin Encoding{exch dup type/integertype ne{pop pop 1 sub dup 0 le{pop}{[}ifelse}{FontMatrix 0 get div Metrics 0 get div def} ifelse}forall Metrics/Metrics currentdict end def[2 index currentdict end definefont 3 -1 roll makefont/setfont cvx]cvx def}def/ObliqueSlant{ dup sin S cos div neg}B/SlantFont{4 index mul add}def/ExtendFont{3 -1 roll mul exch}def/ReEncodeFont{CharStrings rcheck{/Encoding false def dup[exch{dup CharStrings exch known not{pop/.notdef/Encoding true def} if}forall Encoding{]exch pop}{cleartomark}ifelse}if/Encoding exch def} def end d225 1 a225 1 %%BeginProcSet: special.pro d262 20 a281 19 TR/showpage{}N/erasepage{}N/copypage{}N/p 3 def @@MacSetUp}N/doclip{ psf$llx psf$lly psf$urx psf$ury currentpoint 6 2 roll newpath 4 copy 4 2 roll moveto 6 -1 roll S lineto S lineto S lineto closepath clip newpath moveto}N/endTexFig{end psf$SavedState restore}N/@@beginspecial{SDict begin/SpecialSave save N gsave normalscale currentpoint TR @@SpecialDefaults count/ocount X/dcount countdictstack N}N/@@setspecial{ CLIP 1 eq{newpath 0 0 moveto hs 0 rlineto 0 vs rlineto hs neg 0 rlineto closepath clip}if ho vo TR hsc vsc scale ang rotate rwiSeen{rwi urx llx sub div rhiSeen{rhi ury lly sub div}{dup}ifelse scale llx neg lly neg TR }{rhiSeen{rhi ury lly sub div dup scale llx neg lly neg TR}if}ifelse CLIP 2 eq{newpath llx lly moveto urx lly lineto urx ury lineto llx ury lineto closepath clip}if/showpage{}N/erasepage{}N/copypage{}N newpath}N /@@endspecial{count ocount sub{pop}repeat countdictstack dcount sub{end} repeat grestore SpecialSave restore end}N/@@defspecial{SDict begin}N /@@fedspecial{end}B/li{lineto}B/rl{rlineto}B/rc{rcurveto}B/np{/SaveX currentpoint/SaveY X N 1 setlinecap newpath}N/st{stroke SaveX SaveY moveto}N/fil{fill SaveX SaveY moveto}N/ellipse{/endangle X/startangle X /yrad X/xrad X/savematrix matrix currentmatrix N TR xrad yrad scale 0 0 1 startangle endangle arc savematrix setmatrix}N end d284 1 a284 1 %%BeginProcSet: color.pro d333 432 a764 129 TeXDict begin 40258431 52099146 1000 600 600 (pmt.dvi) @@start /Fa 165[37 43 1[56 43 43 37 33 40 43 33 43 43 53 37 43 23 20 43 43 33 37 43 40 40 43 65[{ TeXBase1Encoding ReEncodeFont}25 59.7758 /Times-Roman rf /Fb 140[37 25 12[42 101[{TeXBase1Encoding ReEncodeFont}3 74.7198 /Helvetica rf %DVIPSBitmapFont: Fc cmr10 10 2 /Fc 2 51 df<15301578B3A6007FB812F8B912FCA26C17F8C80078C8FCB3A6153036367B AF41>43 D50 D E %EndDVIPSBitmapFont %DVIPSBitmapFont: Fd cmsy10 10 1 /Fd 1 3 df<0060150600F0150F6C151F007C153E6C157C6C15F86C6CEB01F06C6CEB03 E06C6CEB07C06C6CEB0F806C6CEB1F00017C133E6D5B6D5B90380F81F0903807C3E09038 03E7C06DB45A6D90C7FC147EA214FF497F903803E7C0903807C3E090380F81F049C67E01 3E137C497F497F4848EB0F804848EB07C04848EB03E04848EB01F048C812F8003E157C48 153E48151F48150F00601506282874A841>2 D E %EndDVIPSBitmapFont %DVIPSBitmapFont: Fe cmmi10 10 2 /Fe 2 79 df<1760177017F01601A21603A21607160FA24C7EA216331673166316C3A2ED 0183A2ED0303150683150C160115181530A21560A215C014011580DA03007FA202061300 140E140C5C021FB5FC5CA20260C7FC5C83495A8349C8FC1306A25BA25B13381330017016 80EA01F0D807F815FF267FFF80013F13FEB516FF18FE383C7DBB3E>65 D<902603FFF891381FFFF8495E6D6D80D90007030113006FEC007802061670814A6C1560 A26F7EA202186D5C151FA26F7E02304B5A6F7EA282DA60034AC7FCA26F7EA24A6C6C1306 A2828249485DEE3F80A217C049C7001F5B17E0160FA201066E6C5AA217F81603495EEE01 FCA2EE00FE495E17FF177FA2496F5AA2171F137095C8FC01F081EA07FC267FFFC080B56C 14064A140245397DB843>78 D E %EndDVIPSBitmapFont %DVIPSBitmapFont: Ff cmsy8 8 1 /Ff 1 3 df<006015C000E01401007014030078EC07806CEC0F006C141E6C5C6C6C5B6C 6C5B6C6C485A6C6C485A90387807806D48C7FCEB1E1E6D5AEB07F86D5A6D5A497E497EEB 0F3CEB1E1E497E496C7E496C7E48486C7E48486C7E4848137848C77E001E80488048EC07 800070EC03C048140100601400222376A137>2 D E %EndDVIPSBitmapFont /Fg 135[45 3[45 6[45 9[45 99[{TeXBase1Encoding ReEncodeFont}4 74.7198 /Courier-BoldOblique rf /Fh 135[40 40 2[40 40 2[40 40 40 40 40 40 40 40 40 40 1[40 1[40 1[40 12[40 1[40 1[40 40 40 4[40 1[40 25[40 45[{TeXBase1Encoding ReEncodeFont}25 66.4176 /Courier rf /Fi 133[45 45 1[45 45 45 45 45 45 1[45 45 45 1[45 2[45 45 1[45 45 45 45 1[45 1[45 59[45 35[{TeXBase1Encoding ReEncodeFont}21 74.7198 /Courier-Bold rf /Fj 133[29 33 37 50 33 42 21 29 29 1[37 37 42 58 21 37 21 21 42 37 25 33 37 33 37 37 25 11[46 42 24[25 37 37 37 37 37 37 37 37 37 37 1[19 6[25 58 35[42 2[{ TeXBase1Encoding ReEncodeFont}43 74.7198 /Times-BoldItalic rf /Fk 103[45 29[45 45 45 45 45 45 45 45 45 1[45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 1[45 9[45 45 45 45 2[45 45 45 45 45 1[45 45 45 45 45 45 45 45 45 2[45 45 1[45 9[45 45 45 45 45 45 1[45 45 45 1[45 4[45 33[{ TeXBase1Encoding ReEncodeFont}59 74.7198 /Courier rf /Fl 135[29 3[18 1[26 1[33 33 33 48 18 29 1[18 33 2[29 1[29 1[33 11[48 1[33 2[41 1[44 55 5[48 3[44 41 19[17 46[{TeXBase1Encoding ReEncodeFont}23 66.4176 /Times-Italic rf /Fm 134[33 33 48 33 33 18 26 22 1[33 33 33 52 18 33 18 18 33 33 22 29 33 29 33 29 3[22 1[22 2[48 63 48 48 41 37 44 1[37 48 48 59 41 48 1[22 48 48 37 41 48 44 44 48 6[18 33 33 33 33 33 33 33 33 33 33 18 17 22 17 2[22 22 37[37 2[{TeXBase1Encoding ReEncodeFont}66 66.4176 /Times-Roman rf /Fn 134[33 33 48 33 37 22 26 29 1[37 33 37 55 18 1[22 18 37 33 22 29 37 29 37 33 12[44 37 2[41 52 8[41 4[48 6[22 33 33 3[33 33 33 33 33 1[17 4[22 22 40[{TeXBase1Encoding ReEncodeFont}40 66.4176 /Times-Bold rf %DVIPSBitmapFont: Fo cmsy9 9 4 /Fo 4 111 df<126012F812FEEA7F80EA1FE0EA07F8EA01FE38007F80EB1FE0EB07F8EB 01FE9038007FC0EC1FF0EC07FCEC01FF9138003FC0ED0FF0ED03FCED00FFEE3FC0EE0FF0 EE03FCEE00FEA2EE03FCEE0FF0EE3FC0EEFF00ED03FCED0FF0ED3FC003FFC7FCEC07FCEC 1FF0EC7FC04948C8FCEB07F8EB1FE0EB7F80D801FEC9FCEA07F8EA1FE0EA7F8000FECAFC 12F81260CBFCAC007FB712FCB812FEA26C16FC2F3E7AB03C>21 D102 D<127CEAFFC0EA07F0EA01F8EA007C7F133F6D7EB3A66D7EA26D7E6D7E6D7E9038007FC0 EC1FE0EC7FC0903801F800495A495A495AA2495AB3A649C7FC133E5B485AEA07F0EAFFC0 007CC8FC1B4B7BB726>I<126012F0A21278A37EA37EA37EA36C7EA36C7EA26C7EA36C7E A31378A37FA37FA37FA36D7EA26D7EA36D7EA36D7EA31478A380A380A280A3EC0780A3EC 03C0A3EC01E0A3EC00F0A31578A215301D4B7CB726>110 D E %EndDVIPSBitmapFont /Fp 135[50 50 50 50 50 50 50 1[50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 7[50 1[50 50 50 50 50 50 1[50 50 50 2[50 1[50 50 50 1[50 1[50 1[50 12[50 2[50 2[50 50 50 1[50 50 50 4[50 35[{TeXBase1Encoding ReEncodeFont}49 83.022 /Courier rf /Fq 133[37 3[42 1[28 32 3[42 5[23 3[37 46 21[60 78 18[28 8[42 5[47 42 28 28 40[{ TeXBase1Encoding ReEncodeFont}16 83.022 /Times-Bold rf /Fr 133[32 37 37 55 37 42 23 32 32 1[42 42 42 60 23 37 1[23 42 42 23 37 42 37 42 42 8[51 1[51 60 46 42 51 1[51 60 55 69 46 2[28 1[60 51 51 60 55 51 51 3[56 2[28 42 42 1[42 42 42 1[42 42 1[23 21 28 42[42 2[{TeXBase1Encoding ReEncodeFont} 56 83.022 /Times-Italic rf /Fs 134[60 3[66 40 47 53 1[66 60 66 100 33 2[33 2[40 53 66 53 66 60 14[86 1[73 6[47 7[86 13[60 60 60 49[{TeXBase1Encoding ReEncodeFont}24 119.552 /Times-Bold rf /Ft 134[42 1[58 1[46 1[42 29 2[46 46 1[21 2[21 46 46 1[42 46 1[46 17[50 4[54 16[25 58[{ TeXBase1Encoding ReEncodeFont}17 74.7198 /Helvetica-Bold rf /Fu 134[33 33 50 33 37 21 29 29 37 37 37 37 54 21 33 1[21 37 37 21 33 37 33 37 37 8[46 62 46 54 42 37 46 54 46 54 50 62 42 2[25 1[54 46 46 54 50 46 46 5[25 25 5[37 37 37 37 37 1[19 25 19 4[25 36[37 2[{TeXBase1Encoding ReEncodeFont} 57 74.7198 /Times-Italic rf /Fv 133[33 37 37 54 37 37 21 29 25 37 37 37 37 58 21 37 21 21 37 37 25 33 37 33 37 33 3[25 1[25 2[54 71 54 54 46 42 50 54 42 54 54 66 46 54 29 25 54 54 42 46 54 50 50 54 5[21 21 37 37 37 37 37 37 37 37 37 37 21 19 25 19 42 1[25 25 25 58 34[42 42 2[{TeXBase1Encoding ReEncodeFont}75 74.7198 /Times-Roman rf /Fw 134[37 37 1[37 42 25 29 33 1[42 37 42 62 21 42 1[21 42 37 1[33 42 33 42 37 9[75 54 1[50 42 2[46 58 54 71 50 2[29 1[58 1[50 54 54 1[54 18[19 46[{TeXBase1Encoding ReEncodeFont} 37 74.7198 /Times-Bold rf /Fx 104[83 42 1[37 37 24[37 42 42 60 42 42 23 32 28 42 42 42 42 65 23 42 23 23 42 42 28 37 42 37 42 37 3[28 1[28 2[60 78 60 60 51 46 55 1[46 60 60 74 51 60 1[28 60 60 46 51 60 55 55 60 76 37 4[23 42 42 42 42 42 42 42 42 42 42 23 21 28 21 2[28 28 28 5[28 29[46 46 2[{TeXBase1Encoding ReEncodeFont}77 83.022 /Times-Roman rf /Fy 87[33 49[44 50 28 39 39 3[50 4[28 50 2[44 1[44 1[50 11[72 55 6[83 35[33 33 40[{ TeXBase1Encoding ReEncodeFont}17 99.6264 /Times-Italic rf /Fz 140[39 4[50 1[28 3[50 50 33 44 1[44 1[44 13[55 66 12[61 22[25 46[{TeXBase1Encoding ReEncodeFont}13 99.6264 /Times-Roman rf /FA 134[50 50 72 50 55 33 39 44 1[55 50 55 83 28 55 33 28 55 50 33 44 55 44 55 50 9[100 1[72 66 55 72 1[61 78 1[94 1[78 1[39 78 78 61 66 72 72 66 72 8[50 50 50 50 50 50 50 50 2[25 33 3[33 33 1[83 34[55 55 2[{TeXBase1Encoding ReEncodeFont}57 99.6264 /Times-Bold rf /FB 138[96 57 1[76 2[86 96 1[48 2[48 96 86 1[76 96 1[96 86 16[105 2[163 77[{TeXBase1Encoding ReEncodeFont}15 172.188 /Times-Bold rf end d767 1 a767 1 %%Feature: *Resolution 600dpi d769 2 a770 2 %%BeginPaperSize: Letter letter d772 1 a772 1 d775 113 a887 109 1 0 bop Black Black Black Black 1052 119 a FB(P)m(ortable)44 b(Multithr)m(eading)753 254 y FA(The)26 b(Signal)f(Stack)h(T)-7 b(rick)26 b(F)n(or)e(User)l(-Space)i(Thr)n(ead)h(Cr)n(eation)1567 472 y Fz(Ralf)e(S.)g(Engelschall)1148 595 y Fy(T)-9 b(ec)o(hnisc)o(he) 24 b(Univer)o(sit)1988 596 y(\250)1980 595 y(at)f(M)2172 596 y(\250)2164 595 y(unc)o(hen)h(\(TUM\))1109 718 y Fx(rse@@engelschall.com,)18 b(http://www)-5 b(.engelschall.com)p Black Black 1811 999 a Fw(Abstract)p Black Black 208 1136 a Fv(This)23 b(paper)h(describes)g(a)g(pragmatic)g(b)o(ut)f (portable)h(f)o(allback)h(approach)g(for)e(creating)h(and)h (dispatching)f(between)h(the)e(machine)208 1227 y(conte)o(xts)c(of)g (multiple)f(threads)h(of)g(e)o(x)o(ecution)h(on)f(Unix)g(systems)g (that)f(lack)h(a)f(dedicated)i(user)o(-space)f(conte)o(xt)h(switching)f (f)o(acility)-5 b(.)208 1318 y(Such)22 b(a)g(f)o(allback)h(approach)h (for)f(implementing)g(machine)g(conte)o(xts)g(is)f(a)g(vital)g(part)h (of)f(a)g(user)o(-space)h(multithreading)g(en)m(viron-)208 1410 y(ment,)g(if)e(it)h(has)h(to)f(achie)n(v)o(e)h(maximum)g (portability)g(across)g(a)f(wide)g(range)h(of)g(Unix)f(\003a)o(v)o (ors.)33 b(The)22 b(approach)i(is)e(entirely)g(based)208 1501 y(on)d(standard)h(Unix)g(system)f(f)o(acilities)g(and)h(ANSI-C)e (language)i(features)g(and)g(especially)g(does)g(not)f(require)h(an)o (y)f(assembly)h(code)208 1592 y(or)27 b(platform)h(speci\002c)f(tricks) g(at)h(all.)48 b(The)27 b(most)h(interesting)g(issue)f(is)g(the)h (technique)h(of)e(creating)h(the)g(machine)g(conte)o(xt)g(for)208 1684 y(threads,)18 b(which)g(this)f(paper)h(e)o(xplains)h(in)e(detail.) 23 b(The)17 b(described)i(approach)g(closely)f(follo)n(ws)g(the)g (algorithm)g(as)f(implemented)i(by)208 1775 y(the)h(author)h(for)f(the) g(popular)h(user)o(-space)g(multithreading)g(library)f Fu(GNU)f(P)-6 b(ortable)21 b(Thr)m(eads)g Fv(\()p Fu(GNU)e(Pth)p Fv(,)h([25)q(]\))f(which)i(this)f(w)o(ay)208 1866 y(quickly)f(gained)h (the)f(status)g(of)g(one)g(of)g(the)g(most)g(portable)h(user)o(-space)f (multithreading)h(libraries.)208 2007 y Ft(K)o(e)o(yw)o(or)o(ds:)e Fv(portability)-5 b(,)15 b(multithreading,)i(Unix,)e(POSIX,)f(SUSv2,)h (ANSI-C,)e(user)o(-space,)j(conte)o(xt)g(creation,)g(conte)o(xt)f (switch-)208 2099 y(ing,)j(signal)i(handler)m(,)f(stack,)g(mak)o (econte)o(xt,)h(switchconte)o(xt,)g(sigaltstack,)e(setjmp,)h(longjmp.) 208 2240 y Ft(Pub)o(lishing:)g Fv(Early)f(drafts)f(of)h(this)g(paper)g (were)g(distrib)o(uted)g(with)f(the)h(GNU)g(Pth)f(distrib)o(ution.)22 b(The)c(\002nal)g(release)g(v)o(ersion)g(w)o(as)208 2331 y(published)i(on)f(the)g(USENIX)f(Annual)h(T)-5 b(echnical)20 b(Conference,)f(June)h(18-23,)g(2000,)f(San)g(Die)o(go,)g(California,)f (USA.)0 2711 y Fs(1)119 b(Intr)n(oduction)0 2895 y FA(1.1)99 b(Multithr)n(eading)0 3052 y Fx(The)25 b(paradigm)d(of)j(programming)c (with)k(multiple)g(threads)f(of)0 3152 y(e)o(x)o(ecution)c(\(aka)i Fr(multithr)m(eading)p Fx(\))f(is)i(already)f(a)h(v)o(ery)e(old)h(one)0 3252 y(and)i(dates)g(back)g(to)g(the)h(decades)f(of)g(programming)c (with)25 b Fr(co-)0 3351 y(r)l(outines)19 b Fx([2,)g(3].)25 b(P)o(aradoxically)-5 b(,)17 b(the)i(use)h(of)f(threads)g(on)g(Unix)0 3451 y(platforms)g(did)h(not)g(become)f(popular)f(until)i(the)g(early)g (1990s.)0 3602 y FA(Multithr)n(eading)26 b(Adv)o(antages)0 3728 y Fx(Multithreading)31 b(can)i(pro)o(vide)f(man)o(y)g(bene\002ts)h (for)g(applica-)0 3828 y(tions)26 b(\(good)f(runtime)g(concurrenc)o(y) -5 b(,)24 b(parallel)i(programming)0 3927 y(techniques)j(can)h(be)g (implemented)e(more)i(easily)-5 b(,)32 b(the)e(popu-)0 4027 y(lar)22 b(procedural)d(programming)f(style)k(can)f(be)h(combined) d(with)0 4127 y(multiple)g(threads)f(of)h(e)o(x)o(ecution,)e Fr(etc.)p Fx(\))24 b(b)n(ut)c(the)f(most)g(interest-)0 4226 y(ing)25 b(ones)g(are)g(usually)g(performance)d(gains)j(and)f (reduced)g(re-)0 4326 y(source)17 b(consumption.)k(Because)d(in)g (contrast)f(to)h(multiprocess)0 4425 y(applications,)e(multithreaded)f (ones)i(usually)f(require)g(less)i(sys-)0 4525 y(tem)25 b(resources)f(\(mainly)g(memory\))e(and)j(their)f(internal)g(com-)0 4625 y(munication)33 b(part)i(can)g(le)n(v)o(erage)f(from)g(the)i (shared)e(address)0 4724 y(space.)0 4876 y FA(Multithr)n(eading)26 b(and)g(A)n(pplications)0 5001 y Fx(Ne)n(v)o(ertheless)d(there)h(still) h(e)o(xist)g(just)g(a)f(fe)n(w)h(real)f(applications)0 5101 y(in)35 b(the)f(free)g(softw)o(are)h(w)o(orld)f(that)g(use)h (multithreading)d(for)0 5201 y(their)20 b(bene\002t,)g(although)f (their)h(application)f(domains)h(are)g(pre-)0 5300 y(destined)32 b(for)f(multithreading.)59 b(F)o(or)32 b(instance,)j(the)d(popular)0 5400 y(Apache)38 b(webserv)o(er)g(as)i(of)f(v)o(ersion)f(1.3)h(still)h (uses)g(a)f(pre-)2025 2895 y(forking)32 b(process)h(model)g(for)h (serving)f(HTTP)h(requests,)i(al-)2025 2995 y(though)14 b(tw)o(o)i(e)o(xperiments)e(with)j(multithreaded)c(Apache)i(v)n(ari-) 2025 3095 y(ants)22 b(in)g(1996)e(\(with)i Fr(r)o(sthr)m(eads)g Fx([27)n(]\))g(and)f(1998)f(\(with)i Fr(NSPR)2025 3194 y Fx([31)o(]\))37 b(already)f(sho)n(wed)g(great)h(performance)d (boosts.)76 b(The)2025 3294 y(same)20 b(applies)g(to)h(man)o(y)e (similar)h(applications.)2150 3401 y(The)h(reason)g(for)g(this)i (restraint)e(mainly)g(is)i(that)f(for)f(a)h(long)2025 3500 y(time,)c(multithreading)e(f)o(acilities)j(under)d(Unix)i(were)g (rare.)24 b(The)2025 3600 y(situation)f(became)f(better)h(after)f(some) h(v)o(endors)f(lik)o(e)h Fr(Sun)f Fx(and)2025 3699 y Fr(DEC)f Fx(incorporated)15 b(threading)h(f)o(acilities)j(into)f(their) g(Unix)f(\003a-)2025 3799 y(v)n(ors)36 b(and)g Fr(POSIX)j Fx(standardized)c(a)i(threading)e Fr(Application)2025 3899 y(Pr)l(o)o(gr)o(amming)18 b(Interface)g Fx(\(API\))h(\(aka)f Fr(Pthr)m(eads)g Fx([1]\).)24 b(But)c(an)2025 3998 y(API)e(and)f(a)h (fe)n(w)f(v)o(endor)f(implementations)f(are)j(not)f(enough)e(to)2025 4098 y(ful\002ll)29 b(the)g(portability)e(requirements)g(of)h(modern)f (free)i(soft-)2025 4198 y(w)o(are)37 b(packages.)74 b(Here)37 b(stand-alone)f(and)g(really)h(portable)2025 4297 y(multithreading)18 b(en)m(vironments)f(are)j(needed.)2150 4404 y(The)49 b(author)g(collected)h(and)f(e)n(v)n(aluated)g(o)o(v)o(er)g(twenty)2025 4503 y(\(mostly)42 b(user)n(-space\))h(a)n(v)n(ailable)f (multithreading)f(f)o(acilities)2025 4603 y(for)15 b(Unix)g(systems)h (\(see)g(T)-7 b(able)16 b(1\),)g(b)n(ut)g(only)e(a)i(fe)n(w)g(of)f (them)h(are)2025 4703 y(freely)27 b(a)n(v)n(ailable)g(and)g(sho)n(wed)g (to)h(be)g(really)f(portable.)46 b(And)2025 4802 y(e)n(v)o(en)19 b(the)i(mostly)f(portable)f(ones)h(suf)n(fered)f(from)g(the)i(f)o(act)f (that)2025 4902 y(the)o(y)31 b(partly)g(depend)f(on)h(assembly)g(code)g (or)h(platform)e(spe-)2025 5001 y(ci\002c)e(tricks)f(usually)g(related) g(to)h(the)f(creation)g(and)g(dispatch-)2025 5101 y(ing)d(of)h(the)g (indi)n(vidual)e(threads.)38 b(This)25 b(means)f(that)h(the)g(num-)2025 5201 y(ber)c(of)h(platforms)f(the)o(y)g(support)f(is)j(limited)e(and)h (applications)2025 5300 y(which)27 b(are)h(based)g(on)g(these)g(f)o (acilities)h(are)f(only)f(portable)f(to)2025 5400 y(those)c(platforms.) 29 b(This)22 b(situation)f(is)i(not)f(satisf)o(actory)-5 b(,)21 b(so)h(ap-)p Black 1929 5700 a(1)p Black eop d889 136 a1024 133 2 1 bop Black Black 0 83 a Fx(plication)25 b(authors)g(still)j(a)n(v)n (oid)d(the)i(use)f(of)g(multithreading)d(if)0 183 y(the)o(y)28 b(w)o(ant)g(to)h(\(or)f(ha)n(v)o(e)g(to\))g(achie)n(v)o(e)f(maximum)g (portability)0 282 y(for)20 b(their)f(application.)125 387 y(A)25 b(pragmatic)e(and)h(mostly)g(portable)f(f)o(allback)h (technique)0 486 y(for)d(implementing)f(user)n(-space)h(threads)g(can)h (f)o(acilitate)g(wider)0 586 y(use)e(of)g(multithreading)e(in)i(free)g (softw)o(are)g(applications.)0 750 y FA(Ingr)n(edients)26 b(of)f(a)g(Thr)n(ead)0 884 y Fx(A)h(Unix)f(process)f(has)i(man)o(y)e (ingredients,)h(b)n(ut)g(the)g(most)h(im-)0 983 y(portant)k(ones)h(are) h(its)g(memory)e(mapping)f(table,)34 b(the)e(signal)0 1083 y(dispatching)18 b(table,)h(the)g(signal)g(mask,)g(the)g(set)h(of) f(\002le)h(descrip-)0 1183 y(tors)30 b(and)f(the)h(machine)e(conte)o (xt.)52 b(The)29 b(machine)g(conte)o(xt)f(in)0 1282 y(turn)d(consists)i (of)e(at)i(least)g(the)f(CPU)h(re)o(gisters)e(including)g(the)0 1382 y(program)18 b(counter)h(and)h(the)h(stack)f(pointer)-5 b(.)25 b(In)20 b(addition,)f(there)0 1482 y(can)28 b(be)f(light-weight) g(processes)g(\(L)-6 b(WP\))28 b(or)f(threads,)i(which)0 1581 y(usually)g(share)g(all)h(attrib)n(utes)f(with)g(the)h(underlying) c(\(hea)n(vy-)0 1681 y(weight\))19 b(process)h(e)o(xcept)f(for)h(the)g (machine)f(conte)o(xt.)0 1845 y FA(K)n(er)o(nel-Space)25 b(vs.)31 b(User)l(-Space)0 1979 y Fx(Those)c(L)-6 b(WPs)28 b(or)f(threads,)h(on)e(a)i(Unix)f(platform)e(classically)0 2078 y(can)34 b(be)g(implemented)e(either)i(in)g(k)o(ernel-space)f(or)g (in)i(user)n(-)0 2178 y(space.)76 b(When)38 b(implemented)d(in)j(k)o (ernel-space,)h(one)e(usu-)0 2278 y(ally)22 b(calls)h(them)e(L)-6 b(WPs)23 b(or)e(k)o(ernel)g(threads,)h(otherwise)f(\(user)n(-)0 2377 y(space\))d(threads.)24 b(If)19 b(threads)f(are)h(implemented)d (by)j(the)g(k)o(ernel,)0 2477 y(the)24 b(thread)g(conte)o(xt)f (switches)h(are)h(performed)c(by)j(the)g(k)o(ernel)0 2576 y(without)17 b(notice)f(by)h(the)g(application,)f(similar)i(to)f (the)h(dispatch-)0 2676 y(ing)33 b(of)g(processes.)64 b(If)33 b(threads)g(are)g(implemented)e(in)j(user)n(-)0 2776 y(space,)20 b(the)g(thread)f(conte)o(xt)g(switches)i(are)f (performed)d(usually)0 2875 y(by)31 b(an)g(application)f(library)g (without)h(notice)f(by)h(the)h(k)o(ernel.)0 2975 y(Additionally)-5 b(,)39 b(there)e(e)o(xist)h(hybrid)d(threading)h(approaches,)0 3075 y(where)28 b(typically)g(a)h(user)n(-space)g(library)e(binds)i (one)f(or)g(more)0 3174 y(user)n(-space)19 b(threads)h(to)g(one)g(or)g (more)f(k)o(ernel-space)g(L)-6 b(WPs.)0 3338 y FA(Thr)n(ead)27 b(Models)0 3472 y Fx(The)h(v)o(endor)d(threading)h(f)o(acilities)j (under)d Fr(Sun)h(Solaris)p Fx(,)i Fr(IBM)0 3572 y(AIX)p Fx(,)h Fr(DEC)h(T)-5 b(ru64)31 b Fx(\(formerly)d Fr(DIGIT)l(AL)i(UNIX)k Fx(or)d Fr(OSF/1)p Fx(\))0 3671 y(and)20 b Fr(SGI)h(IRIX)i Fx(use)e(a)h Fq(M:N)f Fx(mapping)e([21)o(,)i(30)o(],)g Fr(i.e)o(.)p Fx(,)f Fq(M)i Fx(user)n(-)0 3771 y(space)33 b(threads)g(are)h(mapped)e(onto)g Fq(N)i Fx(k)o(ernel-space)e(L)-6 b(WPs.)0 3871 y(On)20 b(the)h(other)e(hand,)g Fr(LinuxThr)m(eads)g Fx([29)o(])i(under)e Fr(GNU/Linux)0 3970 y Fx(uses)f(a)f Fq(1:1)g Fx(mapping)e(and)i(pure)f(user)n(-space)h(implementations)0 4070 y(lik)o(e)k Fr(GNU)g(Pth)p Fx(,)f Fr(FSU)g(pthr)m(eads)f Fx(or)i Fr(MIT)f(pthr)m(eads)p Fx(,)g Fr(etc.)26 b Fx(use)20 b(a)0 4170 y Fq(M:1)g Fx(mapping)f([25)n(,)i(22)o(,)f(23)o(].)125 4274 y(From)39 b(no)n(w)g(on)h(we)g(focus)g(on)f(such)h Fq(M:1)g Fx(user)g(space)0 4374 y(threading)d(approaches,)42 b(where)c(one)h(or)f(more)h(user)g(space)0 4473 y(threads)16 b(are)g(implemented)f(inside)i(a)g(single)f(k)o(ernel)g(space)h(pro-)0 4573 y(cess.)24 b(The)15 b(e)o(x)o(ercise)f(is)i(to)g(implement)e(this) i(by)e(using)h(standard-)0 4672 y(ized)20 b(Unix)g(system)g(and)g (ANSI-C)g(language)f(f)o(acilities)i Fr(only)p Fx(.)0 4936 y FA(1.2)99 b(The)26 b(Exer)n(cise)0 5101 y Fx(As)d(we)g(ha)n(v)o (e)f(mentioned,)e(a)j(thread)e(shares)i(its)g(state)g(with)g(the)0 5201 y(underlying)i(process)j(e)o(xcept)f(for)g(the)h(machine)f(conte)o (xt.)46 b(So)0 5300 y(the)17 b(major)e(task)i(for)f(a)h(user)n(-space)f (threading)e(system)j(is)g(to)g(cre-)0 5400 y(ate)k(and)e(dispatch)h (those)g(machine)f(conte)o(xts.)2150 83 y(In)d(practice,)g(the)h (second)f(major)g(task)h(it)g(has)g(to)g(do)f(is)i(to)f(en-)2025 183 y(sure)k(that)f(no)h(thread)f(by)g(accident)g(blocks)g(the)h(whole) f(process)2025 282 y(\(and)25 b(thereby)f(all)i(other)f(threads\).)41 b(Instead)25 b(when)g(an)h(opera-)2025 382 y(tion)j(w)o(ould)f(block,)i (the)f(threading)e(library)h(should)g(suspend)2025 482 y(only)19 b(the)g(e)o(x)o(ecution)e(of)i(the)g(current)g(thread)f(and)h (in)g(the)h(mean-)2025 581 y(time)g(dispatch)f(the)h(remaining)e (threads.)24 b(But)d(this)f(task)g(is)h(out-)2025 681 y(side)30 b(the)h(scope)e(of)h(this)h(paper)e(\(see)h([11)o(])h(for)e (details)i(about)2025 780 y(this)24 b(task\).)33 b(W)-7 b(e)24 b(focus)f(only)f(on)h(the)g(aspect)g(of)g(machine)e(con-)2025 880 y(te)o(xt)f(handling.)2025 1112 y FA(1.3)99 b(The)26 b(Curse)g(of)e(P)n(ortability)2025 1267 y Fx(Our)18 b(goal)g(of)f(real) i(portability)d(for)i(a)h(threading)d(system)i(causes)2025 1367 y(some)j(non-tri)n(vial)d(problems)i(which)g(ha)n(v)o(e)g(to)h(be) g(solv)o(ed.)26 b(The)2025 1467 y(most)f(ob)o(vious)e(one)h(is)i(that)f (dealing)f(with)h(machine)f(conte)o(xts)2025 1566 y(usually)33 b(suf)n(fers)g(from)f(portability)-5 b(,)35 b(because)d(it)j(is)f(a)g (highly)2025 1666 y(CPU)18 b(dependent)d(task)i(for)f(which)g(not)h(e)n (v)o(ery)e(Unix)i(\003a)n(v)n(or)g(pro-)2025 1766 y(vides)25 b(a)g(standardized)e(API.)i(Although)e(such)i(an)g(API)g(w)o(ould)2025 1865 y(be)f(not)h(too)f(hard)f(for)h(v)o(endors)f(to)i(pro)o(vide,)e (because)h(in)h(prin-)2025 1965 y(ciple)d(it)h(is)g(just)g(a)g(matter)e (of)h(switching)g(a)h(fe)n(w)f(CPU)h(re)o(gisters)2025 2064 y(\(mainly)c(the)h(program)e(counter)h(and)h(the)g(stack)g (pointer\).)2025 2201 y FA(Assembly)k(Code)i(Consider)n(ed)g(Harmful) 2025 2319 y Fx(Additionally)-5 b(,)21 b(we)j(disallo)n(w)e(the)h(use)h (of)e(an)o(y)g(assembly)h(solu-)2025 2418 y(tions)c(or)g(platform)e (speci\002c)j(tricks,)f(because)f(then)h(the)g(thread-)2025 2518 y(ing)e(system)i(again)d(w)o(ould)h(be)h(only)f(semi-portable,)f Fr(i.e)o(.)p Fx(,)i(it)h(can)2025 2617 y(be)e(ported)f(to)i Fq(N)f Fx(platforms)f(b)n(ut)i(on)f(the)g Fq(\(N+1\))p Fx(th)f(platform)g(one)2025 2717 y(has)28 b(to)g(manually)e(adjust)i (or)f(e)n(v)o(en)g(e)o(xtend)f(it)i(to)g(w)o(ork)f(there,)2025 2817 y(too.)2150 2916 y(This)e(is)h(usually)f(not)g(acceptable,)h(e)n (v)o(en)e(if)i(it)g(also)g(mak)o(es)2025 3016 y(solving)20 b(the)h(problems)f(harder)-5 b(.)26 b(At)c(least)f(most)g(of)g(the)g (kno)n(wn)2025 3116 y(free)26 b(softw)o(are)g(user)n(-space)g (threading)f(systems)i([22)o(,)f(23)o(,)h(24)o(])2025 3215 y(do)15 b(not)g(restrict)h(themself)e(to)i(this)g(and)f(therefore) e(are)j(just)g(semi-)2025 3315 y(portable.)24 b(But)c(real)g (portability)f(should)g(be)h(a)h(major)f(goal.)2025 3589 y Fs(2)119 b(Pr)n(oblem)30 b(Analysis)2025 3791 y FA(2.1)99 b(The)26 b(T)-9 b(ask)25 b(in)g(Detail)2025 3947 y Fx(Our)d(task)h(is)g (simple)f(in)h(principle:)28 b(pro)o(vide)20 b(an)j(API)f(and)g(cor)n (-)2025 4046 y(responding)f(implementation)f(for)j(creating)f(and)g (dispatching)2025 4146 y(machine)15 b(conte)o(xts)g(on)h(which)g(user)n (-space)g(threads)f(can)h(be)h(im-)2025 4246 y(plemented.)2025 4382 y FA(The)26 b(Pr)n(oposed)g(API)2025 4500 y Fx(In)18 b(detail)g(we)g(propose)f(the)h(follo)n(wing)e Fr(Application)g(Pr)l(o) o(gr)o(am-)2025 4599 y(mer)o(s)21 b(Interface)f Fx(\(API\))g(for)f(the) h(machine)f(conte)o(xt)g(handling:)p Black 2120 4749 50 50 v Black 2233 4749 a(A)25 b(data)g(structure)f(of)g(type)h Fp(mctx)p 3266 4749 25 4 v 29 w(t)g Fx(which)f(holds)h(the)2233 4849 y(machine)19 b(conte)o(xt.)p Black 2120 5001 50 50 v Black 2233 5001 a(A)95 b(function)e(\223)p Fq(v)o(oid)h Fp(mctx)p 3248 5001 25 4 v 29 w(create\(mctx)p 3827 5001 V 28 w(t)2233 5101 y(*)p Fr(mctx)p Fx(,)26 b Fq(v)o(oid)g Fp(\(*)p Fr(sf)p 2825 5101 V 30 w(addr)r Fp(\)\()p Fq(v)o(oid)e(*)p Fp(\),)i Fq(v)o(oid)g Fp(*)p Fr(sf)p 3742 5101 V 30 w(ar)m(g)p Fx(,)2233 5201 y Fq(v)o(oid)c Fp(*)p Fr(sk)p 2531 5201 V 30 w(addr)p Fx(,)g Fq(size)p 2891 5201 V 30 w(t)h Fr(sk)p 3041 5201 V 30 w(size)p Fp(\))p Fx(\224)g(which)g(creates)f(and)2233 5300 y(initializes)17 b(a)h(machine)e(conte)o(xt)f(structure)h(in)i Fr(mctx)f Fx(with)2233 5400 y(a)25 b(start)g(function)e Fr(sf)p 2825 5400 V 31 w(addr)p Fx(,)i(a)g(start)h(function)d(ar)o (gument)p Black 1929 5700 a(2)p Black eop d1026 3 a1028 2 3 2 bop Black Black Black 0 3 3864 4 v 0 2441 4 2439 v 68 642 a Fw(P)o(ackage)553 642 y d1031 1 a1031 2 553 642 a Fw(Genesis)553 642 y d1033 2 a1034 1 553 642 a 744 642 a d1037 2 a1038 2 744 642 a Fw(Latest)19 b(V)-7 b(ersion)744 642 y d1040 1 a1040 1 744 642 a 1010 642 a d1043 2 a1044 2 1010 642 a Fw(Implementation)18 b(Space)1010 642 y d1046 1 a1046 1 1010 642 a 1404 642 a d1049 2 a1050 2 1404 642 a Fw(Thr)o(ead)g (Mapping)1404 642 y d1052 2 a1053 1 1404 642 a 1562 642 a d1056 2 a1057 2 1562 642 a Fw(Acti)o(v)o(e)h(De)o(v)o(elopment)1562 642 y d1059 1 a1059 2 1562 642 a 1703 642 a d1062 2 a1063 2 1703 642 a Fw(Experimental)f(State)1703 642 y d1065 1 a1065 1 1703 642 a 1844 642 a d1068 2 a1069 2 1844 642 a Fw(Open)g(Sour)o(ce)1844 642 y d1071 1 a1071 1 1844 642 a 1985 642 a d1074 2 a1075 2 1985 642 a Fw(Pthr)o(ead)g(API)1985 642 y d1077 1 a1077 1 1985 642 a 2126 642 a d1080 2 a1081 2 2126 642 a Fw(Pthr)o(ead)g(Shar)o(ed)g (Memory)2126 642 y d1083 1 a1083 1 2126 642 a 2267 642 a d1086 2 a1087 2 2267 642 a Fw(Nati)o(v)o(e)i (API)2267 642 y d1089 2 a1090 1 2267 642 a 2408 642 a d1093 2 a1094 2 2408 642 a Fw(Nati)o(v)o(e)g(API) e Fo(\025)h Fw(Pthr)o(ead)f(API)2408 642 y d1096 2 a1097 2 2408 642 a 2550 642 a d1100 2 a1101 2 2550 642 a Fw(Nati)o(v)o(e)h(API)g(is)f(Pthr)o(ead)g(API) 2550 642 y d1103 1 a1103 1 2550 642 a 2765 642 a d1106 2 a1107 2 2765 642 a Fw(Pr)o(eempti)o(v)o(e)h (Scheduling)2765 642 y d1109 1 a1109 1 2765 642 a 2906 642 a d1112 2 a1113 2 2906 642 a Fw(P)o(ortability)2906 642 y d1115 2 a1116 1 2906 642 a 3360 642 a d1119 1 a1119 2 3360 642 a Fw(Assembly)f(Code)3360 642 y d1121 2 a1122 2 3360 642 a 3509 642 a d1125 2 a1126 1 3509 642 a Fw(SysCall)g(Wrap.)3509 642 y d1128 152 a1279 147 3509 642 a 47 673 3658 4 v 68 737 a Fv(gnu-pth)254 b(1999)43 b(1.3.5)117 b(user)270 b(n:1)63 b(yes)42 b(no)67 b(yes)42 b(yes)g(no)67 b(yes)42 b(yes)h(no)141 b(no)67 b(full/mcsc+sjlj)42 b(no)75 b(partly)68 820 y(cmu-l)o(wp)221 b(1984)43 b(1.4)173 b(user)270 b(n:1)63 b(yes)42 b(no)67 b(yes)42 b(no)67 b(-)116 b(yes)42 b(yes)h(partly)e(no)67 b(semi/\002x)o(ed:8)86 b(yes)50 b(no)68 903 y(fsu-pthread)146 b(1992)43 b(3.13)136 b(user)270 b(n:1)63 b(no)k(no)g(yes)42 b(yes)g(no)67 b(no)g(-)117 b(-)190 b(yes)42 b(semi/\002x)o(ed:6)86 b(yes)50 b(yes)68 986 y(mit-pthread)137 b(1993)43 b(1.8.9)117 b(user)270 b(n:1)63 b(no)k(no)g(yes)42 b(yes)g(no)67 b(no)g(-)117 b(-)190 b(yes)42 b(semi/\002x)o(ed:17)49 b(yes)h(yes)68 1069 y(ptl)406 b(1997)43 b(990622)h(user)270 b(n:1)63 b(no)k(no)g(yes)42 b(yes)g(no)67 b(no)g(-)117 b(-)190 b(yes)42 b(semi/\002x)o(ed:10)49 b(yes)h(yes)68 1152 y(linuxthreads)117 b(1997)43 b(2.1.2)117 b(user+k)o(ernel)43 b(1:1)63 b(yes)42 b(no)67 b(yes)42 b(yes)g(no)67 b(no)g(-)117 b(-)190 b(yes)42 b(semi/\002x)o(ed:5)86 b(yes)50 b(yes)68 1235 y(uthread)262 b(1998)43 b(3.4)173 b(user)270 b(n:1)63 b(yes)42 b(no)67 b(yes)42 b(yes)g(no)67 b(no)g(-)117 b(-)190 b(yes)42 b(semi/\002x)o(ed:2)86 b(yes)50 b(yes)68 1318 y(cthread)266 b(1991)43 b(991115)h(user)270 b(n:1)63 b(no)k(no)g(yes)42 b(no)67 b(-)116 b(yes)42 b(yes)h(no)141 b(no)67 b(semi/\002x)o(ed:8)86 b(yes)50 b(yes)68 1401 y(openthreads/qt)d(1996)c(2.0)173 b(user)270 b(n:1)63 b(no)k(no)g(yes)42 b(no)67 b(-)116 b(yes)42 b(no)68 b(no)141 b(no)67 b(semi/\002x)o(ed:9)86 b(yes)50 b(no)68 1484 y(rt++/qt)276 b(1996)43 b(1.0)173 b(user)270 b(n:1)63 b(no)k(no)g(yes)42 b(no)67 b(-)116 b(yes)42 b(yes)h(no)141 b(no)67 b(semi/\002x)o(ed:9)86 b(yes)50 b(no)68 1567 y(rsthreads)216 b(1996)43 b(980331)h(user)270 b(n:1)63 b(no)k(yes)42 b(yes)g(no)67 b(-)116 b(yes)42 b(no)68 b(no)141 b(no)67 b(semi/\002x)o(ed:9)86 b(yes)50 b(no)68 1650 y(pcthread)229 b(1996)43 b(1.0)173 b(user)270 b(n:1)63 b(no)k(yes)42 b(yes)g(yes)g(no)67 b(no)g(-)117 b(-)190 b(yes)42 b(semi/\002x)o(ed:1)86 b(yes)50 b(no)68 1733 y(bbthreads)196 b(1996)43 b(0.3)173 b(k)o(ernel)209 b(1:1)63 b(no)k(yes)42 b(yes)g(no)67 b(-)116 b(yes)42 b(no)68 b(-)190 b(yes)42 b(semi/\002x)o(ed:1)86 b(yes)50 b(no)68 1816 y(jkthreads)212 b(1998)43 b(1.2)173 b(k)o(ernel)209 b(1:1)63 b(no)k(yes)42 b(yes)g(no)67 b(-)116 b(yes)42 b(no)68 b(-)190 b(yes)42 b(semi/\002x)o(ed:1)86 b(yes)50 b(no)68 1899 y(nthreads)233 b(1997)43 b(970604)h(user)270 b(n:1)63 b(no)k(yes)42 b(yes)g(no)67 b(-)116 b(yes)42 b(no)68 b(-)190 b(no)67 b(semi/\002x)o(ed:9)86 b(yes)50 b(partly)68 1982 y(re)o(xthreads)176 b(1993)43 b(930614)h(user)270 b(n:1)63 b(no)k(yes)42 b(yes)g(no)67 b(-)116 b(yes)42 b(no)68 b(-)190 b(no)67 b(semi/\002x)o(ed:4)86 b(yes)50 b(no)68 2065 y(coro)353 b(1999)43 b(1.0.3)117 b(user)270 b(n:1)63 b(no)k(yes)42 b(yes)g(no)67 b(-)116 b(yes)42 b(no)68 b(-)190 b(no)67 b(semi/\002x)o(ed:1)86 b(yes)50 b(no)68 2148 y(greenthreads)105 b(1995)43 b(1.2)173 b(user)270 b(n:1)63 b(no)k(no)g(no)g(no)g(-)116 b(yes)42 b(yes)h(-)190 b(yes)42 b(full/mcsc)176 b(no)75 b(no)68 2231 y(solaris-pthread)42 b(NN)83 b(2.7)173 b(user+k)o(ernel)43 b(n:m)f(yes)g(no)67 b(no)g(yes)42 b(yes)g(yes)g(yes)h(no)141 b(yes)42 b(NN)346 b(NN)41 b(yes)68 2314 y(tru64-pthread)80 b(NN)j(5.0)173 b(user+k)o(ernel)43 b(n:m)f(yes)g(no)67 b(no)g(yes)42 b(yes)g(no)67 b(no)h(no)141 b(yes)42 b(NN)346 b(NN)41 b(yes)68 2397 y(aix-pthread)146 b(NN)83 b(4.3)173 b(user+k)o(ernel)43 b(1:1)63 b(yes)42 b(no)67 b(no)g(yes)42 b(yes)g(no)67 b(no)h(no)141 b(yes)42 b(NN)346 b(NN)41 b(yes)p 3860 2441 4 2439 v 0 2444 3864 4 v 0 2532 a Fn(T)-6 b(able)18 b(1:)67 b Fm(Summary)20 b(of)h(e)n(v)n(aluated)j(multithreading)g (packages)f(and)e(some)g(of)f(their)i(determined)h(characteristics.)36 b(Notice)22 b(that)g(mostly)f(all)h(packages)g(contain)295 2600 y(assembly)17 b(code)h(and)g(are)g(just)f(semi-portable,)j Fl(i.e)o(.)p Fm(,)c(the)o(y)i(support)g(only)g(a)f(\002x)o(ed)g(set)g (of)g(platforms)i(and)e(do)g(not)h(automatically)j(adjust)d(for)f(ne)n (w)h(ones.)p Black 208 2864 a Fr(sf)p 268 2864 25 4 v 30 w(ar)m(g)p Fx(,)36 b(and)d(a)h(stack)f(starting)g(at)h Fr(sk)p 1343 2864 V 30 w(addr)p Fx(,)i(which)d(is)208 2963 y Fr(sk)p 282 2963 V 30 w(size)21 b Fx(bytes)f(in)g(size.)p Black 95 3115 50 50 v Black 208 3115 a(A)i(function)f(\223)p Fq(v)o(oid)h Fp(mctx)p 1006 3115 25 4 v 30 w(save\(mctx)p 1486 3115 V 28 w(t)h(*)p Fr(mctx)p Fp(\))p Fx(\224)208 3215 y(which)j(sa)n(v)o(es)i(the)g(current)e(machine)g(conte)o(xt)g (into)i(the)208 3314 y(machine)19 b(conte)o(xt)g(structure)g Fr(mctx)p Fx(.)p Black 95 3466 50 50 v Black 208 3466 a(A)78 b(function)e(\223)p Fq(v)o(oid)i Fp(mctx)p 1173 3466 25 4 v 30 w(restore\(mctx)p 1803 3466 V 27 w(t)208 3566 y(*)p Fr(mctx)p Fp(\))p Fx(\224)40 b(which)g(restores)g(the)h(ne)n (w)f(machine)f(con-)208 3666 y(te)o(xt)18 b(from)g(the)h(machine)e (conte)o(xt)h(structure)g Fr(mctx)p Fx(.)24 b(This)208 3765 y(function)29 b(does)i(not)g(return)f(to)h(the)h(caller)-5 b(.)58 b(Instead)31 b(it)208 3865 y(does)18 b(return)g(at)i(the)f (location)g(stored)f(in)i Fr(mctx)f Fx(\(which)g(is)208 3964 y(either)d Fr(sf)p 474 3964 V 30 w(addr)i Fx(from)e(a)h(pre)n (vious)f Fp(mctx)p 1415 3964 V 29 w(create)g Fx(call)208 4064 y(or)j(the)i(location)e(of)h(a)g(pre)n(vious)f Fp(mctx)p 1365 4064 V 29 w(save)h Fx(call\).)p Black 95 4216 50 50 v Black 208 4216 a(A)95 b(function)e(\223)p Fq(v)o(oid)h Fp(mctx)p 1223 4216 25 4 v 29 w(switch\(mctx)p 1802 4216 V 28 w(t)208 4316 y(*)p Fr(mctx)p 420 4316 V 29 w(old)p Fx(,)19 b Fp(mctx)p 796 4316 V 29 w(t)h(*)p Fr(mctx)p 1102 4316 V 30 w(ne)o(w)p Fp(\))p Fx(\224)f(which)g(switches)208 4415 y(from)59 b(the)i(current)e(machine)g(conte)o(xt)h(\(sa)n(v)o(ed)f (to)208 4515 y Fr(mctx)p 370 4515 V 29 w(old)28 b Fx(for)e(later)g (use\))g(to)g(a)h(ne)n(w)f(conte)o(xt)f(\(restored)208 4615 y(from)c Fr(mctx)p 554 4615 V 30 w(ne)o(w)p Fx(\).)31 b(This)22 b(function)f(returns)g(only)h(to)h(the)208 4714 y(caller)39 b(if)h Fp(mctx)p 728 4714 V 30 w(restore)f Fx(or)g Fp(mctx)p 1456 4714 V 29 w(switch)h Fx(is)208 4814 y(again)19 b(used)h(on)f Fr(mctx)p 846 4814 V 30 w(old)p Fx(.)0 5045 y FA(2.2)99 b(T)-9 b(echnical)26 b(P)n(ossibilities)0 5201 y Fx(Poking)18 b(around)f(in)i(the)f (references)g(of)g(the)h(ANSI-C)g(language)0 5300 y(reference)36 b(and)i(the)g(Unix)f(standards)g(sho)n(w)h(the)g(follo)n(wing)0 5400 y(functions)19 b(on)h(which)f(an)h(implementation)e(can)i(be)g (based:)p Black 2120 2864 50 50 v Black 2233 2864 a(There)55 b(is)h(the)g Fp(ucontext)p Fx(\(3\))e(f)o(acility)i(with)g(the)2233 2963 y(functions)34 b Fp(getcontext)p Fx(\(3\),)j Fp(makecontext)p Fx(\(3\),)2233 3063 y Fp(swapcontext)p Fx(\(3\))31 b(and)j Fp(setcontext)p Fx(\(3\))e(which)2233 3162 y(conform)21 b(to)j(the)g Fr(Single)f(Unix)h(Speci\002cation)p Fx(,)e(V)-9 b(ersion)2233 3262 y(2)38 b(\()p Fr(SUSv2)e Fx([20)o(],)42 b(aka)c Fr(Unix95/98)p Fx(\).)76 b(Unfortunately)2233 3362 y(these)16 b(are)g(a)n(v)n(ailable)f(on)h(modern)e(Unix)h (platforms)g(only)-5 b(.)p Black 2120 3625 V Black 2233 3625 a(There)87 b(are)h(the)h Fp(jmp)p 3051 3625 25 4 v 29 w(buf)f Fx(based)g(functions)2233 3724 y Fp(setjmp)p Fx(\(3\))29 b(and)h Fp(longjmp)p Fx(\(3\))f(which)i(conform)d(to)2233 3824 y(ISO)41 b(9899:1990)c(\(ISO-C\))k(and)f(the)h Fp(sigjmp)p 3727 3824 V 29 w(buf)2233 3924 y Fx(based)71 b Fp(sigsetjmp)p Fx(\(3\))f(and)i Fp(siglongjmp)p Fx(\(3\))2233 4023 y(functions)17 b(which)h(conform)f(to)i(IEEE)f(Std1003.1-1988)2233 4123 y(\()p Fr(POSIX)t Fx(\),)f(and)j Fr(Single)f(Unix)h(Speci\002cation)p Fx(,)d(V)-9 b(ersion)19 b(2)2233 4222 y(\()p Fr(SUSv2)j Fx([20)o(],)j(aka)f Fr(Unix95/98)p Fx(\).)35 b(The)24 b(\002rst)h(tw)o(o)g(func-)2233 4322 y(tions)17 b(are)h(a)n(v)n (ailable)f(really)h(on)f(all)h(Unix)g(platforms,)f(the)2233 4422 y(last)k(tw)o(o)f(are)g(a)n(v)n(ailable)g(only)f(on)h(some)g(of)g (them.)2233 4603 y(On)h(some)h(platforms)e Fp(setjmp)p Fx(\(3\))g(and)i Fp(longjmp)p Fx(\(3\))2233 4703 y(sa)n(v)o(e)45 b(and)h(restore)f(also)h(the)f(signal)h(mask)f(\(if)h(one)2233 4802 y(does)e(not)g(w)o(ant)g(this)h(semantics,)50 b(one)44 b(has)h(to)f(call)p 2233 4902 V 2257 4902 a Fp(setjmp)p Fx(\(3\))36 b(and)p 2854 4902 V 67 w Fp(longjmp)p Fx(\(3\))g(there\))g (while)i(on)2233 5001 y(others)23 b(one)h(has)g(to)g(e)o(xplicitly)g (use)g(the)g(superset)g(func-)2233 5101 y(tions)36 b Fp(sigsetjmp)p Fx(\(3\))f(and)h Fp(siglongjmp)p Fx(\(3\))e(for)2233 5201 y(this.)115 b(In)50 b(our)f(discussion)h(we)g(can)g(assume)g(that) 2233 5300 y Fp(setjmp)p Fx(\(3\))40 b(and)j Fp(longjmp)p Fx(\(3\))d(sa)n(v)o(e)j(and)f(restore)2233 5400 y(the)36 b(signal)f(mask,)40 b(because)35 b(if)h(this)h(is)g(not)e(the)h(case)p Black 1929 5700 a(3)p Black eop d1281 158 a1438 151 4 3 bop Black Black 208 83 a Fx(in)46 b(practice,)53 b(one)46 b(easily)h(can)f(replace)g(them)g(with)208 183 y Fp(sigsetjmp)p Fx(\(3\))41 b(and)i Fp(siglongjmp)p Fx(\(3\))e(calls)k(\(if)208 282 y(a)n(v)n(ailable\))30 b(or)h(\(if)g(not)g(a)n(v)n(ailable\))f(emulate)h(the)g(miss-)208 382 y(ing)d(functionality)g(manually)f(with)j(additional)e Fp(sig-)208 482 y(procmask)p Fx(\(2\))j(calls)i(\(see)h Fp(pth)p 1245 482 25 4 v 29 w(mctx.c)f Fx(in)g Fr(GNU)208 581 y(Pth)20 b Fx([25)n(]\).)p Black 95 739 50 50 v Black 208 739 a(There)37 b(is)i(the)f(function)e Fp(sigaltstack)p Fx(\(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)34 b(2)g(\()p Fr(SUSv2)f Fx([20)n(],)38 b(aka)c Fr(Unix95/98)p Fx(\))e(and)i(its)h(an-)208 1038 y(cestor)25 b(function)f Fp(sigstack)p Fx(\(2\))f(from)h Fr(4.2BSD)p Fx(.)g(The)208 1137 y(last)d(one)e(e)o(xists)i(only)e(on)h Fr(BSD)p Fx(-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)208 1337 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)f Fp(makecontext)p Fx(\(3\))e(function)i(to)h(cre-)0 1926 y(ate)k(threads)f(and)g Fp(switchcontext)p Fx(\(3\))e(or)i Fp(getcon-)0 2025 y(text)p Fx(\(3\)/)p Fp(setcontext)p Fx(\(3\))18 b(to)23 b(dispatch)e(them.)31 b(And)22 b(actu-)0 2125 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.)48 b(W)-7 b(e)29 b(could)e(easily)h(im-)0 2324 y(plement)20 b(our)g(proposed)f(API)i(as)g(follo)n(wing)e(\(all)i(error)f(checks)0 2424 y(omitted)f(for)h(better)g(readability\):)p Black Black 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)f Fk(mctx_st)h Fo(f)179 2710 y Fk(ucontext_t)f(uc;)0 2785 y 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\))e Fo(n)179 3084 y Fk(\()p Fi(void)p Fk(\)getcontext\(&\(mctx\)->uc\))0 3233 y(/*)63 b Fj(restore)20 b(mac)o(hine)e(context)45 b Fk(*/)0 3308 y Fi(#define)f Fk(mctx_restore\(mctx\))d Fo(n)179 3383 y Fk(\()p Fi(void)p Fk(\)setcontext\(&\(mctx\)->uc\))0 3532 y(/*)63 b Fj(switc)o(h)18 b(mac)o(hine)h(context)45 b Fk(*/)0 3607 y Fi(#define)f Fk(mctx_switch\(mctx_old,mctx_new\))39 b Fo(n)179 3681 y Fk(\()p Fi(void)p Fk (\)swapcontext\(&\(\(mctx_old\)->uc\),)g Fo(n)986 3756 y Fk(&\(\(mctx_new\)->uc\)\))0 3906 y(/*)63 b Fj(create)20 b(mac)o(hine)e(context)45 b Fk(*/)0 3980 y Fi(void)f Fk(mctx_create\()179 4055 y(mctx_t)g(*mctx,)179 4130 y Fi(void)g Fk(\(*sf_addr\)\()p Fi(void)f Fk(*\),)h Fi(void)g Fk(*sf_arg,)179 4204 y Fi(void)g Fk(*sk_addr,)g(size_t)g(sk_size\))0 4279 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\)\);)179 4653 y(/*)64 b Fj(adjust)18 b(to)h(new)f(context)45 b Fk(*/)179 4728 y(mctx->uc.uc_link)491 b(=)44 b(NULL;)179 4802 y(mctx->uc.uc_stack.ss_sp)176 b(=)44 b(sk_addr;)179 4877 y(mctx->uc.uc_stack.ss_size)86 b(=)44 b(sk_size;)179 4952 y(mctx->uc.uc_stack.ss_flags)d(=)j(0;)179 5101 y(/*)64 b 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\);)179 5325 y Fi(return)p Fk(;)0 5400 y Fo(g)2025 83 y Fx(Unfortunately)14 b(there)j(are)g(still)h(lots)g(of)f(Unix)f(platforms)g(where)2025 183 y(this)43 b(approach)d(cannot)h(be)h(used,)47 b(because)42 b(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)-5 b(.)80 b(Actually)38 b(the)h(platform)e(test)i(results)g(for)f Fr(GNU)i(Pth)2025 482 y Fx(\(see)29 b(T)-7 b(able)29 b(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)p Fx(\(3\),)19 b Fr(etc.)p Fx(\).)31 b(On)22 b(all)h(other)f(platforms,)f Fr(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)25 b(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 1179 y Fr(etc.)p Fx(\).)p 2025 1271 1899 4 v 2025 3174 4 1904 v 2103 1351 a Fn(Operating)18 b(System)p 2837 1374 4 79 v 277 w(Ar)o(chitectur)o(e\(s\))p 3450 1374 V 188 w(mcsc)p 3694 1374 V 104 w(sjlj)2103 1430 y Fm(FreeBSD)g(2.x/3.x)p 2837 1453 V 339 w(Intel)p 3450 1453 V 494 w(no)p 3694 1453 V 177 w(yes)2103 1508 y(FreeBSD)g(3.x)p 2837 1532 V 440 w(Intel,)g(Alpha)p 3450 1532 V 298 w(no)p 3694 1532 V 177 w(yes)2103 1587 y(NetBSD)g(1.3/1.4)p 2837 1611 V 361 w(Intel,)g(PPC,)e(M68K)p 3450 1611 V 135 w(no)p 3694 1611 V 177 w(yes)2103 1666 y(OpenBSD)i(2.5/2.6)p 2837 1690 V 313 w(Intel,)g(SP)-6 b(ARC)p 3450 1690 V 255 w(no)p 3694 1690 V 177 w(yes)2103 1745 y(BSDI)17 b(4.0)p 2837 1769 V 536 w(Intel)p 3450 1769 V 494 w(no)p 3694 1769 V 177 w(yes)2103 1824 y(Linux)g(2.0.x)g(glibc)h(1.x/2.0)p 2837 1848 V 129 w(Intel,)g(SP)-6 b(ARC,)16 b(PPC)p 3450 1848 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)p 3450 1926 V 112 w(no)p 3694 1926 V 177 w(yes)2103 1982 y(Sun)f(SunOS)g(4.1.x)p 2837 2005 V 329 w(SP)-6 b(ARC)p 3450 2005 V 410 w(no)p 3694 2005 V 177 w(yes)2103 2060 y(Sun)17 b(Solaris)h(2.5/2.6/2.7)p 2837 2084 V 181 w(SP)-6 b(ARC)p 3450 2084 V 410 w(yes)p 3694 2084 V 155 w(yes)2103 2139 y(SCO)17 b(UnixW)-5 b(are)18 b(2.x/7.x)p 2837 2163 V 169 w(Intel)p 3450 2163 V 494 w(yes)p 3694 2163 V 155 w(yes)2103 2218 y(SCO)f(OpenServ)o(er)i(5.0.x)p 2837 2242 V 175 w(Intel)p 3450 2242 V 494 w(no)p 3694 2242 V 177 w(yes)2103 2297 y(IBM)e(AIX)g(4.1/4.2/4.3)p 2837 2321 V 225 w(RS6000,)g(PPC)p 3450 2321 V 249 w(yes)p 3694 2321 V 155 w(yes)2103 2376 y(HP)g(HPUX)f(9.10/10.20)p 2837 2400 V 205 w(HPP)-6 b(A)p 3450 2400 V 450 w(no)p 3694 2400 V 177 w(yes)2103 2455 y(HP)17 b(HPUX)f(11.0)p 2837 2479 V 372 w(HPP)-6 b(A)p 3450 2479 V 450 w(yes)p 3694 2479 V 155 w(yes)2103 2534 y(SGI)17 b(IRIX)g(5.3)p 2837 2557 V 427 w(MIPS)g(32/64)p 3450 2557 V 292 w(no)p 3694 2557 V 177 w(yes)2103 2613 y(SGI)g(IRIX)g(6.2/6.5)p 2837 2636 V 326 w(MIPS)g(32/64)p 3450 2636 V 292 w(yes)p 3694 2636 V 155 w(yes)2103 2691 y(ISC)g(4.0)p 2837 2715 V 584 w(Intel)p 3450 2715 V 494 w(no)p 3694 2715 V 177 w(yes)2103 2770 y(Apple)h(MacOS)f(X)p 2837 2794 V 341 w(PPC)p 3450 2794 V 496 w(no)p 3694 2794 V 177 w(yes)2103 2849 y(DEC)f(OSF1/T)n(ru64)i(4.0/5.0)p 2837 2873 V 103 w(Alpha)p 3450 2873 V 453 w(yes)p 3694 2873 V 155 w(yes)2103 2928 y(SNI)f(ReliantUNIX)p 2837 2952 V 308 w(MIPS)p 3450 2952 V 459 w(yes)p 3694 2952 V 155 w(yes)2103 3007 y(AmigaOS)p 2837 3031 V 522 w(M68K)p 3450 3031 V 441 w(no)p 3694 3031 V 177 w(yes)p 3921 3174 4 1904 v 2025 3177 1899 4 v 2025 3264 a Fn(T)-6 b(able)18 b(2:)67 b Fm(Summary)20 b(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)168 b Fh(makecontext)p Fm(\(3\)/)p Fh(switchcontext)p Fm(\(3\),)2320 3466 y Fh(sjlj)p Fm(:)83 b(functional)51 b Fh(setjmp)p Fm(\(3\)/)p Fh(longjmp)p Fm(\(3\))c(or)i Fh(sig-)2320 3531 y(setjmp)p Fm(\(3\)/)p Fh(siglongjmp)p Fm(\(3\).)25 b(See)19 b(\002le)h Fh(PORTING)d Fm(in)j Fl(GNU)2320 3597 y(Pth)c Fm([25)q(])h(for)g(more)g(details.)2025 3834 y FA(2.4)99 b(Remaining)25 b(P)n(ossibilities)2025 3999 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)k Fp(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(].)57 b(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 4897 y(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(\()p Fp(mctx)p 2980 4996 25 4 v 29 w(t)p Fx(\))i(on)f(their)g Fp(jmp)p 3559 4996 V 29 w(buf)h Fx(data)2025 5096 y(structure.)2150 5201 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 5400 y Fp(mctx)p 2230 5400 V 29 w(save)p Fx(,)73 b Fp(mctx)p 2753 5400 V 29 w(restore)62 b Fx(and)g Fp(mctx)p 3577 5400 V 29 w(switch)p Black 1929 5700 a Fx(4)p Black eop d1440 144 a1583 132 5 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)20 b Fp(longjmp)p Fx(\(3\).)0 358 y FA(The)26 b(Dif\002cult)g(P)o(art)0 500 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.)56 b(Ev)o(en)29 b(kno)n(wing)g(that)i(our)f(ma-)0 699 y(chine)17 b(conte)o(xt)f(is)i Fp(jmp)p 687 699 25 4 v 30 w(buf)f Fx(based)g(is)i(no)e(adv)n(antage)e(to)j(us.)24 b(A)0 799 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)26 b(\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)p Fx(\(3\))f(and)h Fp(longjmp)p Fx(\(3\))e(calls,)0 1098 y(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)19 b(portable)g(function)g(which)g(actually)h(deals)g(with)h(stacks.)125 1406 y(So)33 b(it)g(is)h(clear)f(that)g(our)f(implementation)f(for)h Fp(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 1505 V 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)39 b(our)g(approach)f(has)i(to)g(be)f(careful)g(to)h(en-)0 1804 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 y Fp(mctx)p 205 2103 V 29 w(save)p Fx(,)g Fp(mctx)p 709 2103 V 29 w(restore)47 b Fx(and)h Fp(mctx)p 1504 2103 V 29 w(switch)p Fx(\),)0 2203 y(because)29 b Fp(setjmp)p Fx(\(3\))f(and)i Fp(sigaltstack)p Fx(\(3\))d(cannot)i(be)0 2302 y(tri)n(vially)20 b(combined)e(to)i(form)f Fp(mctx)p 1102 2302 V 29 w(create)p Fx(.)0 2630 y Fs(3)119 b(Implementation)0 2833 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(\()p Fp(mctx)p 869 2932 V 29 w(save)p Fx(,)i Fp(mctx)p 1351 2932 V 29 w(restore)e Fx(and)0 3032 y Fp(mctx)p 205 3032 V 29 w(switch)p Fx(\))i(and)h(a)h(dif)n(\002cult)e(part)h(\()p Fp(mctx)p 1504 3032 V 29 w(create)p Fx(\).)0 3131 y(Let)23 b(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 Black 0 3532 a Fk(/*)63 b Fj(mac)o(hine)19 b(context)g(data)g(structure)44 b Fk(*/)0 3607 y Fi(typedef)g(struct)f Fk(mctx_st)h Fo(f)179 3681 y Fk(jmp_buf)g(jb;)0 3756 y Fo(g)h Fk(mctx_t;)0 3906 y(/*)63 b Fj(sav)o(e)20 b(mac)o(hine)e(context)45 b Fk(*/)0 3980 y Fi(#define)f Fk(mctx_save\(mctx\))e Fo(n)179 4055 y Fk(\()p Fi(void)p Fk(\)setjmp\(\(mctx\)->jb\))0 4204 y(/*)63 b Fj(restore)20 b(mac)o(hine)e(context)45 b Fk(*/)0 4279 y Fi(#define)f Fk(mctx_restore\(mctx\))d Fo(n)179 4354 y Fk(longjmp\(\(mctx\)->jb,)h(1\))0 4503 y(/*)63 b Fj(switc)o(h)18 b(mac)o(hine)h(context)45 b Fk(*/)0 4578 y Fi(#define)f Fk(mctx_switch\(mctx_old,mctx_new\))39 b 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\))0 4877 y(/*)63 b Fj(create)20 b(mac)o(hine)e(context)45 b Fk(*/)0 4952 y Fi(void)f Fk(mctx_create\()179 5026 y(mctx_t)g(*mctx,)179 5101 y Fi(void)g Fk(\(*sf_addr\)\()p Fi(void)f Fk(*\),)h Fi(void)g Fk(*sf_arg,)179 5176 y Fi(void)g Fk(*sk_addr,)g(size_t)g(sk_size\))0 5251 y Fo(f)198 5325 y Fj(...initialization)16 b(of)j Fg(mctx)f Fj(to)h(be)g(\002lled)f(in...)0 5400 y Fo(g)2025 83 y Fx(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)36 b(to)g(implement)f Fp(mctx)p 2881 282 V 29 w(save)p Fx(,)40 b Fp(mctx)p 3371 282 V 29 w(restore)35 b Fx(and)2025 382 y Fp(mctx)p 2230 382 V 29 w(switch)40 b Fx(is)i(intentional.)86 b(F)o(or)40 b(technical)g(reasons)2025 482 y(related)35 b(to)h Fp(setjmp)p Fx(\(3\))e(semantics)h(and)g Fp(return)g Fx(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)g Fx(implement)f(these)h(three)g(functions)e(\(at)i(least)h(not)2025 780 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)25 b(portability)g(across)i(all)g(platforms.)43 b(Instead)26 b(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)25 b(Ov)o(er)o(view)2025 1373 y Fx(The)g(general)e(idea)i(for)f Fp(mctx)p 2940 1373 V 29 w(create)h Fx(is)h(to)f(con\002gure)e(the)2025 1472 y(gi)n(v)o(en)14 b(stack)i(as)g(a)g(signal)f(stack)h(via)f Fp(sigaltstack)p Fx(\(2\),)f(send)2025 1572 y(the)33 b(current)f(process)g(a)i(signal)e(to)i(transfer)e(e)o(x)o(ecution)e (con-)2025 1672 y(trol)e(onto)g(this)h(stack,)h(sa)n(v)o(e)f(the)f (machine)f(conte)o(xt)h(there)g(via)2025 1771 y Fp(setjmp)p Fx(\(3\),)16 b(get)h(rid)h(of)f(the)g(signal)h(handler)e(scope)h(and)g (boot-)2025 1871 y(strap)j(into)g(the)g(startup)g(routine.)2150 1970 y(The)31 b(real)g(problem)f(in)i(this)g(approach)d(comes)i(from)g (the)2025 2070 y(signal)e(handler)f(scope)h(which)g(implies)h(v)n (arious)e(restrictions)2025 2170 y(on)21 b(Unix)g(platforms)f(\(the)h (signal)g(handler)f(scope)g(often)h(is)h(just)2025 2269 y(a)i(\003ag)h(in)f(the)g(process)f(control)g(block)g(\(PCB\))i(which)f (v)n(arious)2025 2369 y(system)30 b(calls,)j(lik)o(e)e Fp(sigaltstack)p Fx(\(2\),)e(check)h(before)e(al-)2025 2469 y(lo)n(wing)c(the)h(operation)f(\226)h(b)n(ut)g(because)g(it)h(is) g(part)f(of)f(the)i(pro-)2025 2568 y(cess)20 b(state)g(the)f(k)o(ernel) g(manages,)f(the)h(process)g(cannot)f(change)2025 2668 y(it)i(itself\).)25 b(As)20 b(we)f(will)h(see,)g(we)g(ha)n(v)o(e)e(to)i (perform)d(a)i(fe)n(w)h(tricks)2025 2767 y(to)i(get)f(rid)h(of)f(it.)29 b(The)22 b(second)e(main)h(problem)f(is:)29 b(ho)n(w)21 b(do)g(we)2025 2867 y(prepare)k(the)h(calling)g(of)g(the)g(start)h (routine)e(without)h(immedi-)2025 2967 y(ately)20 b(entering)f(it?)2025 3204 y FA(3.2)99 b(Algorithm)2025 3360 y Fx(The)20 b(input)f(to)i(the)f Fp(mctx)p 2779 3360 V 29 w(create)g Fx(function)e(is)j(the)g(machine) 2025 3459 y(conte)o(xt)30 b(structure)h Fr(mctx)h Fx(which)f(should)f (be)i(initialized,)i(the)2025 3559 y(thread)15 b(startup)h(function)e (address)i Fr(sf)p 3111 3559 V 30 w(addr)p Fx(,)h(the)f(thread)f (startup)2025 3659 y(function)24 b(ar)o(gument)g Fr(sf)p 2727 3659 V 30 w(ar)m(g)i Fx(and)f(a)i(chunk)d(of)i(memory)e(start-) 2025 3758 y(ing)31 b(at)i Fr(sk)p 2330 3758 V 30 w(addr)g Fx(and)e Fr(sk)p 2772 3758 V 30 w(size)i Fx(bytes)e(in)h(size,)j(which) d(should)2025 3858 y(become)19 b(the)h(threads)g(stack.)2150 3958 y(The)33 b(follo)n(wing)f(algorithm)g(for)h Fp(mctx)p 3361 3958 V 29 w(create)g Fx(is)i(di-)2025 4057 y(rectly)21 b(modeled)f(after)h(the)h(implemented)e(algorithm)g(one)h(can)2025 4157 y(\002nd)28 b(in)h Fr(GNU)h(Pth)e Fx([25)o(],)j(which)d(in)h(turn) f(w)o(as)i(deri)n(v)o(ed)d(from)2025 4256 y(techniques)19 b(originally)f(found)h(in)h Fr(r)o(sthr)m(eads)h Fx([27)n(]:)p Black 2129 4438 a(1.)p Black 41 w(Preserv)o(e)i(the)h(current)g(signal) g(mask)g(and)g(block)f(an)i(ar)n(-)2233 4537 y(bitrary)f(w)o(ork)o(er)h (signal)h(\(we)g(use)g Fp(SIGUSR1)p Fx(,)g(b)n(ut)g(an)o(y)2233 4637 y(signal)d(can)f(be)i(used)e(for)h(this)g(\226)h(e)n(v)o(en)e(an)h (already)f(used)2233 4736 y(one\).)53 b(This)30 b(w)o(ork)o(er)f (signal)g(is)i(later)f(temporarily)e(re-)2233 4836 y(quired)18 b(for)i(the)g(trampoline)f(step.)p Black 2129 5001 a(2.)p Black 41 w(Preserv)o(e)28 b(a)h(possibly)g(e)o(xisting)f(signal)h (action)g(for)g(the)2233 5101 y(w)o(ork)o(er)19 b(signal)i(and)g (con\002gure)e(a)i(trampoline)e(function)2233 5201 y(as)g(the)h(ne)n(w) f(temporary)e(signal)i(action.)24 b(The)19 b(signal)g(de-)2233 5300 y(li)n(v)o(ery)g(is)j(con\002gured)d(to)i(occur)f(on)g(an)h (alternate)g(signal)2233 5400 y(stack)f(\(see)g(ne)o(xt)g(step\).)p Black 1929 5700 a(5)p Black eop d1585 83 a1667 75 6 5 bop Black Black Black 104 83 a Fx(3.)p Black 41 w(Preserv)o(e)35 b(a)h(possibly)g(acti)n(v)o(e)f(alternate)g(signal)h(stack)208 183 y(and)56 b(con\002gure)f(the)h(memory)f(chunk)h(starting)g(at)208 282 y Fr(sk)p 282 282 25 4 v 30 w(addr)47 b Fx(as)f(the)g(ne)n(w)f (temporary)f(alternate)h(signal)208 382 y(stack)20 b(of)g(length)f Fr(sk)p 791 382 V 30 w(size)p Fx(.)p Black 104 566 a(4.)p Black 41 w(Sa)n(v)o(e)40 b(parameters)g(for)g(the)h(trampoline)e(step)i (\()p Fr(mctx)p Fx(,)208 666 y Fr(sf)p 268 666 V 30 w(addr)p Fx(,)25 b Fr(sf)p 557 666 V 30 w(ar)m(g)p Fx(,)h Fr(etc.)p Fx(\))39 b(in)26 b(global)e(v)n(ariables,)h(send)g(the)208 766 y(current)20 b(process)g(the)i(w)o(ork)o(er)e(signal,)h (temporarily)e(un-)208 865 y(block)d(it)i(and)f(this)h(w)o(ay)f(allo)n (w)h(it)g(to)g(be)f(deli)n(v)o(ered)f(on)h(the)208 965 y(signal)25 b(stack)h(in)g(order)f(to)h(transfer)f(e)o(x)o(ecution)f (control)208 1065 y(to)c(the)g(trampoline)f(function.)p Black 104 1249 a(5.)p Black 41 w(After)24 b(the)h(trampoline)e (function)g(asynchronously)f(en-)208 1349 y(tered,)27 b(sa)n(v)o(e)g(its)h(machine)e(conte)o(xt)g(in)h(the)g Fr(mctx)g Fx(struc-)208 1448 y(ture)j(and)g(immediately)g(return)f (from)h(it)h(to)g(terminate)208 1548 y(the)20 b(signal)g(handler)f (scope.)p Black 104 1732 a(6.)p Black 41 w(Restore)29 b(the)g(preserv)o(ed)f(alternate)h(signal)g(stack,)i(pre-)208 1832 y(serv)o(ed)18 b(signal)h(action)g(and)g(preserv)o(ed)e(signal)i (mask)g(for)208 1932 y(w)o(ork)o(er)30 b(signal.)59 b(This)32 b(w)o(ay)g(an)f(e)o(xisting)g(application)208 2031 y(con\002guration)17 b(for)j(the)g(w)o(ork)o(er)f(signal)h(is)h(restored.)p Black 104 2216 a(7.)p Black 41 w(Sa)n(v)o(e)115 b(the)g(current)f (machine)g(conte)o(xt)g(of)208 2315 y Fp(mctx)p 413 2315 V 29 w(create)p Fx(.)52 b(This)30 b(allo)n(ws)g(us)g(to)g(return)e(to)i (this)208 2415 y(point)19 b(after)h(the)g(ne)o(xt)g(trampoline)e(step.) p Black 104 2599 a(8.)p Black 41 w(Restore)30 b(the)g(pre)n(viously)e (sa)n(v)o(ed)i(machine)f(conte)o(xt)g(of)208 2699 y(the)15 b(trampoline)g(function)f(\()p Fr(mctx)p Fx(\))h(to)h(again)f(transfer) g(e)o(x-)208 2799 y(ecution)31 b(control)h(onto)g(the)h(alternate)f (stack,)k(b)n(ut)d(this)208 2898 y(time)20 b(without\(!\))j(signal)d (handler)f(scope.)p Black 104 3083 a(9.)p Black 41 w(After)50 b(reaching)f(the)i(trampoline)e(function)g(\()p Fr(mctx)p Fx(\))208 3182 y(again,)40 b(immediately)c(bootstrap)g(into)h(a)g (clean)g(stack)208 3282 y(frame)19 b(by)h(just)g(calling)g(a)h(second)e (function.)p Black 62 3467 a(10.)p Black 41 w(Set)51 b(the)h(ne)n(w)f(signal)g(mask)g(to)g(be)g(the)g(same)h(as)208 3566 y(the)32 b(original)e(signal)i(mask)g(which)g(w)o(as)h(acti)n(v)o (e)e(when)208 3666 y Fp(mctx)p 413 3666 V 29 w(create)e Fx(w)o(as)i(called.)53 b(This)30 b(is)h(required)d(be-)208 3765 y(cause)19 b(in)g(the)g(\002rst)h(trampoline)e(step)h(we)h (usually)e(had)h(at)208 3865 y(least)h(the)h(w)o(ork)o(er)e(signal)h (block)o(ed.)p Black 62 4050 a(11.)p Black 41 w(Load)51 b(the)i(passed)f(startup)g(information)f(\()p Fr(sf)p 1673 4050 V 29 w(addr)p Fx(,)208 4149 y Fr(sf)p 268 4149 V 30 w(ar)m(g)p Fx(\))46 b(from)g Fp(mctx)p 894 4149 V 29 w(create)g Fx(into)g(local)h(\(stack-)208 4249 y(based\))27 b(v)n(ariables.)47 b(This)28 b(is)h(important)e(because)g(their)208 4348 y(v)n(alues)17 b(ha)n(v)o(e)f(to)i(be)f(preserv)o(ed)f(in)i (machine)e(conte)o(xt)g(de-)208 4448 y(pendent)g(memory)g(until)h(the)h (created)f(machine)g(conte)o(xt)208 4548 y(is)k(the)f(\002rst)h(time)f (restored)f(by)h(the)g(application.)p Black 62 4732 a(12.)p Black 41 w(Sa)n(v)o(e)26 b(the)g(current)f(machine)h(conte)o(xt)f(for)h (later)g(restor)n(-)208 4832 y(ing)19 b(by)h(the)g(calling)g (application.)p Black 62 5016 a(13.)p Black 41 w(Restore)30 b(the)g(pre)n(viously)e(sa)n(v)o(ed)i(machine)f(conte)o(xt)g(of)208 5116 y Fp(mctx)p 413 5116 V 29 w(create)20 b Fx(to)i(transfer)e(e)o(x)o (ecution)f(control)h(back)208 5216 y(to)g(it.)p Black 62 5400 a(14.)p Black 41 w(Return)f(to)i(the)f(calling)g(application.) 2025 83 y(When)d(the)h(calling)f(application)e(no)n(w)i(again)g (switches)g(into)h(the)2025 183 y(established)25 b(machine)f(conte)o (xt)g Fr(mctx)p Fx(,)i(the)f(thread)g(starts)h(run-)2025 282 y(ning)h(at)i(routine)e Fr(sf)p 2614 282 V 31 w(addr)i Fx(with)g(ar)o(gument)d Fr(sf)p 3405 282 V 30 w(ar)m(g)p Fx(.)49 b(Figure)27 b(1)2025 382 y(illustrates)g(the)g(algorithm)e (\(the)i(numbers)e(refer)h(to)i(the)e(algo-)2025 482 y(rithm)20 b(steps)g(listed)h(abo)o(v)o(e\).)2025 1015 y 14669332 30325391 0 0 14669332 30325391 startTexFig 2025 1015 a %%BeginDocument: pmt-fig.eps d1669 3 a1671 3 %%Title: pmt-fig.eps %%Creator: fig2dev Version 3.2.3 Patchlevel %%CreationDate: Sat Apr 22 23:14:14 2000 d1673 2 a1674 2 %%BoundingBox: 0 0 223 461 %%Magnification: 1.0000 d1715 2 a1716 2 newpath 0 461 moveto 0 0 lineto 223 0 lineto 223 461 lineto closepath clip newpath -21.0 477.0 translate a1753 1 %%Page: 1 1 d1755 1 d1757 5 d1763 2 d1849 4 a1852 1 /Times-Bold ff 150.00 scf sf d1856 2 d1878 1 a1878 1 1275 660 m 1275 600 l 1124 600 l 1244 630 l 1124 660 l cp d1886 1 a1886 1 n 1124 660 m 1244 630 l 1124 600 l 1124 660 l cp gs 0.00 setgray ef gr col0 s d1890 1 a1890 1 1995 840 m 1995 780 l 1844 780 l 1964 810 l 1844 840 l cp d1897 1 a1897 1 n 1844 840 m 1964 810 l 1844 780 l 1844 840 l cp gs 0.00 setgray ef gr col0 s d1901 1 a1901 1 2985 840 m 2985 780 l 2834 780 l 2954 810 l 2834 840 l cp d1908 1 a1908 1 n 2834 840 m 2954 810 l 2834 780 l 2834 840 l cp gs 0.00 setgray ef gr col0 s d1912 1 a1912 1 2940 1185 m 3000 1185 l 3000 1034 l 2970 1154 l 2940 1034 l cp d1919 1 a1919 1 n 2940 1034 m 2970 1154 l 3000 1034 l 2940 1034 l cp gs 0.00 setgray ef gr col0 s d1923 1 a1923 1 2190 1140 m 2190 1200 l 2341 1200 l 2221 1170 l 2341 1140 l cp d1930 1 a1930 1 n 2341 1140 m 2221 1170 l 2341 1200 l 2341 1140 l cp gs 0.00 setgray ef gr col0 s d1934 1 a1934 1 1245 1140 m 1245 1200 l 1396 1200 l 1276 1170 l 1396 1140 l cp d1941 1 a1941 1 n 1396 1140 m 1276 1170 l 1396 1200 l 1396 1140 l cp gs 0.00 setgray ef gr col0 s d1945 1 a1945 1 2985 1470 m 2985 1410 l 2834 1410 l 2954 1440 l 2834 1470 l cp d1952 1 a1952 1 n 2834 1470 m 2954 1440 l 2834 1410 l 2834 1470 l cp gs 0.00 setgray ef gr col0 s d1959 1 a1959 1 2985 2550 m 2985 2490 l 2834 2490 l 2954 2520 l 2834 2550 l cp d1966 1 a1966 1 n 2834 2550 m 2954 2520 l 2834 2490 l 2834 2550 l cp gs 0.00 setgray ef gr col0 s d1973 1 a1973 1 1230 2852 m 1229 2912 l 1380 2913 l 1261 2883 l 1381 2853 l cp d1980 1 a1980 1 n 1381 2853 m 1261 2883 l 1380 2913 l 1381 2853 l cp gs 0.00 setgray ef gr col0 s d1984 1 a1984 1 1229 4751 m 1230 4811 l 1381 4809 l 1261 4781 l 1380 4749 l cp d1991 1 a1991 1 n 1380 4749 m 1261 4781 l 1381 4809 l 1380 4749 l cp gs 0.00 setgray ef gr col0 s d1995 1 a1995 1 1230 4601 m 1290 4601 l 1290 4450 l 1260 4570 l 1230 4450 l cp d2002 1 a2002 1 n 1230 4450 m 1260 4570 l 1290 4450 l 1230 4450 l cp gs 0.00 setgray ef gr col0 s d2006 1 a2006 1 1230 5235 m 1290 5234 l 1287 5083 l 1260 5204 l 1227 5084 l cp d2013 1 a2013 1 n 1227 5084 m 1260 5204 l 1287 5083 l 1227 5084 l cp gs 0.00 setgray ef gr col0 s d2019 1 a2019 1 2985 4260 m 2985 4200 l 2834 4200 l 2954 4230 l 2834 4260 l cp d2027 1 a2027 1 n 2834 4260 m 2954 4230 l 2834 4200 l 2834 4260 l cp gs 0.00 setgray ef gr col0 s d2033 2 a2034 2 3660 7575 m 3720 7575 l 3720 7424 l 3690 7544 l 3660 7424 l cp 3720 6285 m 3660 6285 l 3660 6436 l 3690 6316 l 3720 6436 l cp d2040 1 a2040 1 n 3720 6436 m 3690 6316 l 3660 6436 l 3720 6436 l cp gs 0.00 setgray ef gr col0 s d2042 1 a2042 1 n 3660 7424 m 3690 7544 l 3720 7424 l 3660 7424 l cp gs 0.00 setgray ef gr col0 s d2057 1 a2057 1 2985 3180 m 2985 3120 l 2834 3120 l 2954 3150 l 2834 3180 l cp d2065 1 a2065 1 n 2834 3180 m 2954 3150 l 2834 3120 l 2834 3180 l cp gs 0.00 setgray ef gr col0 s d2116 1 a2116 1 2940 5325 m 3000 5325 l 3000 5174 l 2970 5294 l 2940 5174 l cp d2124 1 a2124 1 n 2940 5174 m 2970 5294 l 3000 5174 l 2940 5174 l cp gs 0.00 setgray ef gr col0 s d2130 1 a2130 1 1245 5280 m 1245 5340 l 1396 5340 l 1276 5310 l 1396 5280 l cp d2138 1 a2138 1 n 1396 5280 m 1276 5310 l 1396 5340 l 1396 5280 l cp gs 0.00 setgray ef gr col0 s d2144 1 a2144 1 1245 5640 m 1245 5700 l 1396 5700 l 1276 5670 l 1396 5640 l cp d2152 1 a2152 1 n 1396 5640 m 1276 5670 l 1396 5700 l 1396 5640 l cp gs 0.00 setgray ef gr col0 s d2158 1 a2158 1 345 5910 m 345 5970 l 496 5970 l 376 5940 l 496 5910 l cp d2166 1 a2166 1 n 496 5910 m 376 5940 l 496 5970 l 496 5910 l cp gs 0.00 setgray ef gr col0 s d2172 1 a2172 1 2985 5610 m 2985 5550 l 2834 5550 l 2954 5580 l 2834 5610 l cp d2180 1 a2180 1 n 2834 5610 m 2954 5580 l 2834 5550 l 2834 5610 l cp gs 0.00 setgray ef gr col0 s d2182 1 d2196 1 a2196 1 /Times-Bold ff 150.00 scf sf d2199 1 a2199 1 /Times-Bold ff 150.00 scf sf d2202 1 a2202 1 /Times-Bold ff 150.00 scf sf d2205 1 a2205 1 /Times-Bold ff 150.00 scf sf d2208 1 a2208 1 /Times-Bold ff 150.00 scf sf d2211 1 a2211 1 /Times-Bold ff 150.00 scf sf d2214 1 a2214 1 /Times-Bold ff 150.00 scf sf d2217 1 a2217 1 /Times-Bold ff 150.00 scf sf d2220 1 a2220 1 /Times-Bold ff 150.00 scf sf d2223 1 a2223 1 /Times-Bold ff 150.00 scf sf d2226 1 a2226 1 /Times-Bold ff 150.00 scf sf d2229 1 a2229 1 /Times-Bold ff 150.00 scf sf d2232 1 a2232 1 /Times-Bold ff 150.00 scf sf d2235 1 a2235 1 /Times-Bold ff 150.00 scf sf d2238 1 a2238 1 /Times-Bold ff 150.00 scf sf d2241 1 a2241 1 /Times-Bold ff 150.00 scf sf d2244 1 a2244 1 /Times-Bold ff 150.00 scf sf d2247 1 a2247 1 /Times-Bold ff 150.00 scf sf d2250 1 a2250 1 /Times-Bold ff 150.00 scf sf d2253 1 a2253 1 /Times-Bold ff 150.00 scf sf d2256 1 a2256 1 /Times-Bold ff 150.00 scf sf d2259 1 a2259 1 /Times-Bold ff 150.00 scf sf d2262 1 a2262 1 /Times-Bold ff 150.00 scf sf d2265 1 a2265 1 /Times-Bold ff 150.00 scf sf d2268 1 a2268 1 /Times-Bold ff 150.00 scf sf d2271 1 a2271 1 /Times-Bold ff 150.00 scf sf d2274 1 a2274 1 /Times-Bold ff 150.00 scf sf d2277 1 a2277 1 /Times-Bold ff 150.00 scf sf d2280 1 d2283 3 d2288 10 a2297 12 endTexFig 2025 4944 a Fn(Figur)o(e)d(1:)66 b Fm(Illustration)18 b(of)c(the)h(machine)h(conte)o(xt)g(creation)h(procedure.)22 b(The)2350 5012 y(thick)h(solid)f(lines)h(and)e(numeric)i(marks)e (correspond)i(to)f(the)g(al-)2350 5080 y(gorithm)c(steps)g(as)f (described)i(in)f(section)h(3.2.)h(The)d(thick)i(dotted)2350 5148 y(lines)h(sho)n(w)f(a)g(possible)h(further)g(processing)h(where)f (a)f(fe)n(w)g(con-)2350 5216 y(te)o(xt)i(switches)h(are)e(performed)i (to)e(dispatch)i(between)f(the)g(main)2350 5284 y(thread)e(and)e(the)h (ne)n(w)g(created)h(thread.)p Black 1929 5700 a Fx(6)p Black eop d2299 120 a2418 102 7 6 bop Black Black 0 83 a FA(3.3)99 b(Sour)n(ce)27 b(Code)0 239 y Fx(The)43 b(corresponding)d(ANSI-C)k(code,)49 b(which)43 b(implements)0 338 y Fp(mctx)p 205 338 25 4 v 29 w(create)p Fx(,)37 b(is)d(a)h(little)f(bit)h(more)e(complicated.)64 b(But)0 438 y(with)23 b(the)g(presented)e(algorithm)g(in)i(mind,)f(it)i (is)f(no)n(w)f(straight-)0 538 y(forw)o(ard.)p Black Black 0 693 a Fi(static)44 b Fk(mctx_t)313 b(mctx_caller;)0 767 y Fi(static)44 b Fk(sig_atomic_t)f(mctx_called;)0 917 y Fi(static)h Fk(mctx_t)268 b(*mctx_creat;)0 992 y Fi(static)44 b(void)313 b Fk(\(*mctx_creat_func\)\()p Fi(void)41 b Fk(*\);)0 1066 y Fi(static)j(void)358 b Fk(*mctx_creat_arg;)0 1141 y Fi(static)44 b Fk(sigset_t)223 b(mctx_creat_sigs;)0 1290 y Fi(void)44 b Fk(mctx_create\()179 1365 y(mctx_t)g(*mctx,)179 1440 y Fi(void)g Fk(\(*sf_addr\)\()p Fi(void)f Fk(*\),)h Fi(void)g Fk(*sf_arg,)179 1515 y Fi(void)g Fk(*sk_addr,)g Fi(size_t)g Fk(sk_size\))0 1589 y Fo(f)179 1664 y Fi(struct)g Fk(sigaction)f(sa;)179 1739 y Fi(struct)h Fk(sigaction)f(osa;)179 1813 y Fi(struct)h Fk(sigaltstack)f(ss;)179 1888 y Fi(struct)h Fk(sigaltstack)f(oss;)179 1963 y(sigset_t)h(osigs;)179 2038 y(sigset_t)g(sigs;)179 2187 y(/*)64 b Fj(Step)18 b(1:)45 b Fk(*/)179 2262 y (sigemptyset\(&sigs\);)179 2336 y(sigaddset\(&sigs,)d(SIGUSR1\);)179 2411 y(sigprocmask\(SIG_BLOCK,)f(&sigs,)j(&osigs\);)179 2561 y(/*)64 b Fj(Step)18 b(2:)45 b Fk(*/)179 2635 y(memset\(\()p Fi(void)e Fk(*\)&sa,)h(0,)493 2710 y Fi(sizeof)p Fk(\()p Fi(struct)f Fk(sigaction\)\);)179 2785 y(sa.sa_handler)g(=)i (mctx_create_trampoline;)179 2860 y(sa.sa_flags)e(=)i(SA_ONSTACK;)179 2934 y(sigemptyset\(&sa.sa_mask\);)179 3009 y(sigaction\(SIGUSR1,)d (&sa,)i(&osa\);)179 3158 y(/*)64 b Fj(Step)18 b(3:)45 b Fk(*/)179 3233 y(ss.ss_sp)178 b(=)45 b(sk_addr;)179 3308 y(ss.ss_size)88 b(=)45 b(sk_size;)179 3383 y(ss.ss_flags)e(=)i(0;) 179 3457 y(sigaltstack\(&ss,)d(&oss\);)179 3607 y(/*)64 b Fj(Step)18 b(4:)45 b Fk(*/)179 3681 y(mctx_creat)268 b(=)44 b(mctx;)179 3756 y(mctx_creat_func)f(=)h(sf_addr;)179 3831 y(mctx_creat_arg)88 b(=)44 b(sf_arg;)179 3906 y(mctx_creat_sigs)f (=)h(osigs;)179 3980 y(mctx_called)223 b(=)44 b(FALSE;)179 4055 y(kill\(getpid\(\),)f(SIGUSR1\);)179 4130 y(sigfillset\(&sigs\);) 179 4204 y(sigdelset\(&sigs,)f(SIGUSR1\);)179 4279 y Fi(while)i Fk(\(!mctx_called\))359 4354 y(sigsuspend\(&sigs\);)179 4503 y(/*)64 b Fj(Step)18 b(6:)45 b Fk(*/)179 4578 y (sigaltstack\(NULL,)d(&ss\);)179 4653 y(ss.ss_flags)h(=)i(SS_DISABLE;) 179 4728 y(sigaltstack\(&ss,)d(NULL\);)179 4802 y(if)j (\(!\(oss.ss_flags)d(&)j(SS_DISABLE\)\))359 4877 y(sigaltstack\(&oss,)d (NULL\);)179 4952 y(sigaction\(SIGUSR1,)g(&osa,)i(NULL\);)179 5026 y(sigprocmask\(SIG_SETMASK,)717 5101 y(&osigs,)g(NULL\);)179 5251 y(/*)64 b Fj(Step)18 b(7)h(&)g(Step)f(8:)45 b Fk(*/)179 5325 y(mctx_switch\(&mctx_caller,)c(mctx\);)2204 83 y(/*)64 b Fj(Step)18 b(14:)45 b Fk(*/)2204 158 y Fi(return)p Fk(;)2025 232 y Fo(g)2025 382 y Fi(void)f Fk(mctx_create_trampoline\()p Fi(int)d Fk(sig\))2025 457 y Fo(f)2204 531 y Fk(/*)64 b Fj(Step)18 b(5:)45 b Fk(*/)2204 606 y Fi(if)g Fk (\(mctx_save\(mctx_creat\))c(==)j(0\))h Fo(f)2384 681 y Fk(mctx_called)e(=)h(TRUE;)2384 756 y Fi(return)p Fk(;)2204 830 y Fo(g)2204 980 y Fk(/*)64 b Fj(Step)18 b(9:)45 b Fk(*/)2204 1054 y(mctx_create_boot\(\);)2025 1129 y Fo(g)2025 1279 y Fi(void)f Fk(mctx_create_boot\()p Fi(void)p Fk(\))2025 1353 y Fo(f)2204 1428 y Fi(void)g Fk(\(*mctx_start_func\)\()p Fi(void)d Fk(*\);)2204 1503 y Fi(void)j Fk(*mctx_start_arg;)2204 1652 y(/*)64 b Fj(Step)18 b(10:)45 b Fk(*/)2204 1727 y(sigprocmask\(SIG_SETMASK,)2742 1802 y(&mctx_creat_sigs,)d(NULL\);) 2204 1951 y(/*)64 b Fj(Step)18 b(11:)45 b Fk(*/)2204 2026 y(mctx_start_func)e(=)h(mctx_creat_func;)2204 2100 y(mctx_start_arg)88 b(=)44 b(mctx_creat_arg;)2204 2250 y(/*)64 b Fj(Step)18 b(12)h(&)g(Step)g(13:)45 b Fk(*/)2204 2325 y(mctx_switch\(mctx_creat,)c(&mctx_caller\);)2204 2474 y(/*)64 b Fj(The)18 b(thread)h(``magically'')g(starts...)62 b Fk(*/)2204 2549 y(mctx_start_func\(mctx_start_arg\);)2204 2698 y(/*)45 b(NOTREACHED)e(*/)2204 2773 y(abort\(\);)2025 2848 y Fo(g)2025 3124 y FA(3.4)99 b(Run-time)26 b(P)n(enalty)2025 3294 y Fx(After)31 b(this)h(discussion)f(of)g(the)g(implementation)e (details,)35 b(an)2025 3393 y(ob)o(viously)29 b(occuring)h(question)g (no)n(w)h(is)h(what)g(the)f(e)o(xpected)2025 3493 y(run-time)g(penalty) h(is.)63 b(That)32 b(is,)k(what)d(does)f(our)g(presented)2025 3592 y(machine)43 b(conte)o(xt)f(implementation)g(cost)i(compared)e(to) i(a)2025 3692 y Fp(ucontext)p Fx(\(3\))33 b(based)j(solution.)70 b(From)35 b(the)g(already)f(dis-)2025 3792 y(cussed)21 b(details)h(we)g(can)f(easily)h(guess)f(that)h(our)e(comple)o(x)g(ma-) 2025 3891 y(chine)27 b(conte)o(xt)f(creation)h(procedure)d(\()p Fp(mctx)p 3392 3891 V 29 w(create)p Fx(\))j(will)2025 3991 y(be)20 b(certainly)f(noticeably)f(slo)n(wer)i(than)g(a)g (solution)f(based)h(on)f(a)2025 4091 y Fp(ucontext)p Fx(\(3\))f(f)o(acility)-5 b(.)2150 4197 y(But)48 b(a)g(wild)g(guess)f (is)i(not)e(suf)n(\002cing)g(for)g(a)h(reason-)2025 4297 y(able)d(statement.)101 b(So)45 b(we)h(ha)n(v)o(e)f(written)g(a)h Fr(Simple)f(Ma-)2025 4397 y(c)o(hine)21 b(Conte)n(xt)g(Benc)o(hmark)g Fx(\(SMCB)i([32)n(]\))f(which)f(w)o(as)h(used)2025 4496 y(to)36 b(compare)e(run-time)g(costs)j(of)e(the)h Fp(mctx)p 3421 4496 V 29 w(create)f Fx(and)2025 4596 y Fp(mctx)p 2230 4596 V 29 w(switch)27 b Fx(functions)f(if)h(once)f(implemented)g (through)2025 4696 y(the)17 b Fr(POSIX)i Fp(makecontext)p Fx(\(3\)/)p Fp(swapcontext)p Fx(\(3\))11 b(func-)2025 4795 y(tions)55 b(\(as)h(sho)n(wn)f(in)g(section)h(2.3\),)63 b(and)55 b(once)f(imple-)2025 4895 y(mented)44 b(with)g(our)g(based)g (f)o(allback)g(implementation)f(\(for)2025 4994 y(con)m(v)o(enience)c (reasons)j(we)h(directly)e(used)h Fp(sigjmp)p 3706 4994 V 29 w(buf)p Fx(,)2025 5094 y Fp(sigsetjmp)p Fx(\(3\))g(and)h Fp(siglongjmp)p Fx(\(3\))f(in)i(the)g(bench-)2025 5194 y(mark,)31 b(because)d(all)i(tested)g(platforms)e(pro)o(vided)e (this\).)53 b(The)2025 5293 y(results)21 b(are)f(sho)n(wn)f(T)-7 b(able)20 b(3)h(belo)n(w)-5 b(.)2150 5400 y(As)32 b(one)e(can)h(deri)n (v)o(e)f(from)g(these)i(e)n(v)n(aluations,)g(our)e(sig-)p Black 1929 5700 a(7)p Black eop d2420 166 a2585 162 8 7 bop Black Black 0 83 a Fx(nal)25 b(stack)f(trick)h(to)f(implement)f Fp(mctx)p 1174 83 25 4 v 30 w(create)h Fx(in)g(practice)0 183 y(is)31 b(approximately)c(15)j(times)h(slo)n(wer)f(than)f(the)h Fp(makecon-)0 282 y(text)p Fx(\(3\))36 b(based)i(v)n(ariant.)77 b(This)38 b(cost)g(should)f(not)g(be)h(ne-)0 382 y(glected.)f(On)25 b(the)f(other)g(hand,)g(the)h Fp(sigsetjmp)p Fx(\(3\)/)d Fp(sig-)0 482 y(longjmp)p Fx(\(3\))29 b(based)i Fp(mctx)p 903 482 V 29 w(switch)g Fx(performs)f(about)g(as)0 581 y(good)17 b(as)h(the)g Fp(swapcontext)p Fx(\(3\))e(based)i(v)n(ariant)f (\(the)g(reason)0 681 y(why)29 b(on)g(most)g(of)h(the)f(tested)h (platforms)e(it)j(is)f(e)n(v)o(en)f(slightly)0 780 y(f)o(aster)37 b(is)h(not)f(kno)n(wn)e(\226)i(b)n(ut)g(we)g(guess)g(it)h(is)g(related) e(to)h(a)0 880 y(greater)26 b(management)f(o)o(v)o(erhead)f(in)k(the)f Fp(ucontext)p Fx(\(3\))e(f)o(a-)0 980 y(cility)-5 b(,)16 b(which)f(is)i(a)f(superset)f(of)g(the)h(functionality)e(we)h (require\).)0 1079 y(Or)h(in)g(short:)22 b(our)15 b(presented)g(f)o (allback)g(approach)e(costs)j(notice-)0 1179 y(able)24 b(e)o(xtra)g(CPU)h(c)o(ycles)f(on)g(thread)f(creation)g(time,)i(b)n(ut) g(is)g(as)0 1279 y(f)o(ast)17 b(as)g(the)g(standardized)d(solution)i (under)f(thread)g(dispatching)0 1378 y(time.)p 0 1492 1899 4 v 0 2931 4 1440 v 78 1562 a Fn(10000)j Ff(\002)e Fn(mctx)p 475 1562 20 4 v 25 w(cr)o(eate)j(\(in)e(seconds\):)78 1710 y(Platf)n(orm)p 827 1734 4 79 v 561 w(mcsc)p 1080 1734 V 165 w(sjlj)p 1332 1734 V 103 w(o)o(v)o(erhead)78 1789 y Fm(Sun)g(Solaris)h(2.6)f(\(SP)-6 b(ARC\))p 827 1813 V 133 w(0.076)p 1080 1813 V 104 w(1.268)p 1332 1813 V 247 w(16.7)78 1868 y(DEC)16 b(T)n(ru64)h(5.0)g(\(Alpha\))p 827 1892 V 171 w(0.019)p 1080 1892 V 104 w(0.235)p 1332 1892 V 247 w(12.4)78 1947 y(SGI)g(IRIX)g(6.5)f(\(MIPS\))p 827 1971 V 227 w(0.105)p 1080 1971 V 104 w(1.523)p 1332 1971 V 247 w(14.5)78 2026 y(SCO)h(UnixW)-5 b(are)18 b(7.0)e(\(Intel\))p 827 2050 V 105 w(0.204)p 1080 2050 V 104 w(3.827)p 1332 2050 V 247 w(18.8)78 2105 y(HP)h(HP/UX)f(11.0)h(\(HPP)-6 b(A\))p 827 2128 V 144 w(0.057)p 1080 2128 V 104 w(0.667)p 1332 2128 V 247 w(11.8)1192 2184 y Fn(A)f(v)o(erage:)89 b(14.8)78 2311 y(10000)18 b Ff(\002)e Fn(mctx)p 475 2311 20 4 v 25 w(switch)i(\(in)f(seconds\):)78 2410 y(Platf)n(orm)p 827 2433 4 79 v 561 w(mcsc)p 1080 2433 V 165 w(sjlj)p 1332 2433 V 103 w(o)o(v)o(erhead)78 2488 y Fm(Sun)g(Solaris)h(2.6)f (\(SP)-6 b(ARC\))p 827 2512 V 133 w(0.137)p 1080 2512 V 104 w(0.210)p 1332 2512 V 281 w(1.5)78 2567 y(DEC)16 b(T)n(ru64)h(5.0)g(\(Alpha\))p 827 2591 V 171 w(0.034)p 1080 2591 V 104 w(0.022)p 1332 2591 V 281 w(0.6)78 2646 y(SGI)g(IRIX)g(6.5)f(\(MIPS\))p 827 2670 V 227 w(0.235)p 1080 2670 V 104 w(0.190)p 1332 2670 V 281 w(0.8)78 2725 y(SCO)h(UnixW)-5 b(are)18 b(7.0)e(\(Intel\))p 827 2749 V 105 w(0.440)p 1080 2749 V 104 w(0.398)p 1332 2749 V 281 w(0.9)78 2804 y(HP)h(HP/UX)f(11.0)h(\(HPP)-6 b(A\))p 827 2828 V 144 w(0.106)p 1080 2828 V 104 w(0.065)p 1332 2828 V 281 w(0.6)1225 2883 y Fn(A)f(v)o(erage:)90 b(0.9)p 1896 2931 4 1440 v 0 2934 1899 4 v 0 3022 a(T)-6 b(able)18 b(3:)67 b Fm(Summary)20 b(of)h Fl(Simple)i(Mac)o(hine)g(Conte)o(xt)g (Benc)o(hmark)g Fm(\(SMCB,)295 3090 y([32]\).)d(The)d(speed)g(of)g (machine)h(conte)o(xt)h(creation)g(and)e(switching)295 3158 y(found)25 b(on)g(each)h(tested)g(operating)i(system.)44 b Fn(mcsc)p Fm(:)38 b(functional)295 3226 y Fh(makecontext)p Fm(\(3\))11 b(/)i Fh(switchcontext)p Fm(\(3\),)f Fn(sjlj)p Fm(:)20 b(functional)295 3293 y Fh(sigsetjmp)p Fm(\(3\)/)p Fh(siglongjmp)p Fm(\(3\).)58 b Fn(o)o(v)o(erhead)p Fm(:)49 b(the)31 b(o)o(v)o(er)o(-)295 3359 y(head)18 b(of)f(using)g Fn(sjlj)g Fm(instead)i(of)e Fn(mcsc)p Fm(.)0 3573 y FA(3.5)99 b(Remaining)25 b(Issues)0 3730 y Fx(The)20 b(presented)e(algorithm)h (and)g(source)g(code)h(can)f(be)h(directly)0 3829 y(used)29 b(in)h(practice)f(for)f(implementing)g(a)i(minimal)e(threading)0 3929 y(system)e(or)g(the)g(concept)e(of)i(co-routines.)40 b(Its)27 b(big)f(adv)n(antage)0 4029 y(is)20 b(that)f(if)g(the)g (operating)e(system)i(pro)o(vides)e(the)i(required)e(stan-)0 4128 y(dardized)j(primiti)n(v)o(es,)g(we)h(do)g(not)g(need)f(to)i(kno)n (w)e(an)o(ything)f(at)0 4228 y(all)28 b(about)e(the)h(machine)g(we)g (are)g(running)f(on)g(\227)i(e)n(v)o(erything)0 4327 y(just)d(w)o(orks.)38 b(Ne)n(v)o(ertheless,)25 b(there)f(remain)g(a)h (fe)n(w)f(special)h(is-)0 4427 y(sues)c(we)f(still)i(ha)n(v)o(e)d(to)i (discuss.)0 4577 y FA(The)26 b(W)-6 b(aggly)23 b(longjmp\(3\))i(after)h (Retur)o(n)0 4702 y Fx(On)e(some)f(platforms,)h Fp(longjmp)p Fx(\(3\))e(may)h(not)g(be)h(called)g(af-)0 4802 y(ter)30 b(the)g(function)e(which)i(called)f(the)h Fp(setjmp)p Fx(\(3\))f(returned.)0 4902 y(When)22 b(this)i(is)f(done,)f(the)h (stack)g(frame)e(situation)i(is)g(not)f(guar)n(-)0 5001 y(anteed)k(to)h(be)g(in)f(a)i(clean)e(and)g(consistent)h(state.)45 b(But)27 b(this)h(is)0 5101 y(e)o(xactly)c(the)h(mechanism)f(we)h(use)g (in)g(order)f(to)h(get)g(rid)g(of)g(the)0 5200 y(signal)20 b(handler)f(scope)h(in)g(step)g(5.)125 5300 y(The)c(only)g(alternati)n (v)o(e)g(w)o(ould)g(be)h(to)g(lea)n(v)o(e)g(the)g(signal)f(han-)0 5400 y(dler)27 b(via)g Fp(longjmp)p Fx(\(3\),)g(b)n(ut)g(then)g(we)h(w) o(ould)e(ha)n(v)o(e)h(another)2025 83 y(problem,)f(as)h(e)o(xperience)d (sho)n(wed.)41 b(F)o(or)26 b(instance,)j(R)q Fm(O)t(B)t(E)t(RT)2027 183 y Fx(S)t(.)k(T)t Fm(H)t(AU)r Fx(')-5 b(s)29 b Fr(Really)e(Simple)g (Thr)m(eads)h Fx(\()p Fr(r)o(sthr)m(eads)p Fx(\))f([27)o(])h(w)o(as) 2025 282 y(ported)17 b(to)h(se)n(v)o(eral)f(platforms)g(and)g(w)o(as)i (used)f(to)g(run)f(an)h(e)o(xper)n(-)2025 382 y(imental)23 b(multithreaded)d(v)o(ersion)i(of)h(the)g(Apache)f(webserv)o(er)-5 b(.)2027 482 y(T)t Fm(H)t(AU)r Fx(')g(s)18 b(approach)d(w)o(as)i (similar)g(to)g(ours,)g(b)n(ut)f(dif)n(fered)f(signif-)2025 581 y(icantly)k(in)h(the)f(w)o(ay)g(the)h(signal)f(handler)f(is)i (left.)25 b(In)19 b(particular)m(,)2025 681 y(in)27 b(an)f(attempt)g (to)h(a)n(v)n(oid)f(the)g(unsafe)g(stack)h(frame,)g(it)g(used)f(a)2025 780 y Fp(longjmp)p Fx(\(3\))19 b(call)i(to)g(lea)n(v)o(e)f(the)h (signal)f(handler)m(,)f(rather)h(than)2025 880 y(returning)27 b(from)h(it.)51 b(But)30 b(this)f(approach)e(does)h(not)h(w)o(ork)f(on) 2025 980 y(some)20 b Fr(SysV)6 b Fx(-deri)n(v)o(ed)18 b(k)o(ernels,)h(as)i(we)g(already)e(mentioned.)2150 1079 y(The)31 b(problem)e(is)k(that)e(these)h(k)o(ernels)f(do)g(not)g (\223belie)n(v)o(e\224)2025 1179 y(that)e(the)g(code)g(is)h(out)e(of)h (the)g(signal-handling)e(conte)o(xt,)i(un-)2025 1279 y(til)k(the)f(signal)f(handler)g(has)h(returned)e(\227)i(and)g (accordingly)-5 b(,)2025 1378 y(refuse)28 b(to)g(allo)n(w)g (readjustment)f(of)g(the)i(signal)f(stack)g(until)g(it)2025 1478 y(has.)61 b(But)32 b(with)g(the)h Fr(r)o(sthr)m(eads)f Fx(approach,)g(the)h(signal)e(han-)2025 1577 y(dler)23 b(that)f(created)h(the)f(\002rst)i(thread)e(ne)n(v)o(er)f(returns,)i (and)f(when)2025 1677 y Fr(r)o(sthr)m(eads)17 b Fx(w)o(ants)g(to)g (create)g(the)g(second)f(thread,)g(these)h(k)o(ernels)2025 1777 y(refuse)23 b(to)g(readjust)g(the)h(signal)f(stack,)h(and)f(we)h (are)f(stuck.)34 b(So)2025 1876 y(with)17 b(portability)e(in)i(mind,)f (we)h(decided)e(that)i(it)g(is)h(better)e(to)h(get)2025 1976 y(rid)23 b(of)f(the)h(signal)g(handler)f(scope)g(with)h(the)g (straight-forw)o(ard)2025 2076 y(\223)p Fp(return)p Fx(\224)38 b(and)h(instead)g(\002ght)g(the)g(mentioned)f(\(simpler\))2025 2175 y(problem)18 b(of)i(an)h(unsafe)e(stack)h(frame.)2150 2275 y(F)o(ortunately)-5 b(,)24 b(in)i(practice)e(this)i(is)h(not)e(as) h(problematic)e(as)2025 2374 y(it)j(seems,)h(because)e(e)n(v)n (aluations)f(\(for)h Fr(GNU)h(Pth)p Fx(\))f(on)g(a)h(wide)2025 2474 y(range)35 b(of)g(current)g(Unix)g(platforms)g(sho)n(wed)g(that)h (one)f(can)2025 2574 y(reach)21 b(a)g(safe)h(stack)f(frame)f(again)h (by)f(just)i(calling)f(a)h(function.)2025 2673 y(That)k(is)i(the)e (reason)g(why)f(our)h(algorithm)f(enters)h(the)g(second)2025 2773 y(trampoline)18 b(function)h(in)h(step)h(9.)2025 2918 y FA(The)26 b(Uncooperati)o(v)o(e)g(longjmp\(3\))2025 3040 y Fx(Ev)o(en)e(on)i(operating)d(systems)j(which)f(ha)n(v)o(e)g(w)o (orking)f Fr(POSIX)2025 3140 y Fx(functions,)c(our)g(approach)f(may)h (theoretically)g(still)i(not)e(w)o(ork,)2025 3240 y(because)48 b Fp(longjmp)p Fx(\(3\))f(does)i(not)f(cooperate.)109 b(F)o(or)49 b(in-)2025 3339 y(stance,)17 b(on)e(some)h(platforms)f(the) h(standard)f Fr(libc)i Fp(longjmp)p Fx(\(3\))2025 3439 y(branches)d(to)h(error)n(-handling)d(code)j(if)g(it)h(detects)g(that)f (the)h(caller)2025 3539 y(tries)j(to)h(jump)e Fr(up)g Fx(the)h(stack,)g Fr(i.e)o(.)p Fx(,)g(into)g(a)g(stack)g(frame)f(that)h (has)2025 3638 y(already)g(returned.)2150 3738 y(This)28 b(is)h(usually)f(implemented)f(by)h(comparing)e(the)i(cur)n(-)2025 3837 y(rent)f(stack)g(pointer)f(to)h(the)g(one)f(in)h(the)g Fp(jmp)p 3387 3837 25 4 v 30 w(buf)g Fx(structure.)2025 3937 y(That)j(is)h(why)e(it)i(is)g(important)e(for)g(our)h(algorithm)e (to)j(return)2025 4037 y(from)19 b(the)g(signal)h(handler)e(and)h(this) h(w)o(ay)f(enter)g(the)h(\(dif)n(ferent\))2025 4136 y(stack)31 b(of)g(the)h(parent)e(thread.)57 b(In)31 b(practice,)i(the)e(implemen-) 2025 4236 y(tation)26 b(in)g Fr(GNU)h(Pth)f Fx(sho)n(wed)g(that)g(then) g(one)f(no)h(longer)f(suf-)2025 4336 y(fers)16 b(from)f(those)g (uncooperati)n(v)o(e)d Fp(longjmp)p Fx(\(3\))i(implementa-)2025 4435 y(tions,)k(b)n(ut)f(one)f(should)h(k)o(eep)f(this)i(point)e(in)i (mind)e(when)h(reach-)2025 4535 y(ing)29 b(e)n(v)o(en)f(more)h (uncooperati)n(v)o(e)d(v)n(ariants)j(on)g(esoteric)g(Unix)2025 4634 y(platforms.)22 b(If)16 b(it)h(still)g(occurs,)f(one)g(can)g(only) f(try)h(to)g(resume)g(the)2025 4734 y(operation)24 b(by)h(using)f(a)i (possibly)f(e)o(xisting)g(platform-speci\002c)2025 4834 y(error)19 b(handling)f(hook.)2025 4979 y FA(Garbage)25 b(at)g(Bottom)g(of)g(Stacks)2025 5101 y Fx(There)17 b(is)i(a)f(subtle)f (side-ef)n(fect)g(of)g(our)g(implementation:)22 b(there)2025 5201 y(remains)16 b(some)h(garbage)e(at)j(the)f(bottom)e(of)i(each)g (thread)f(stack.)2025 5300 y(The)33 b(reason)g(is)i(that)e(if)h(a)g (signal)g(is)g(deli)n(v)o(ered,)h(the)f(operat-)2025 5400 y(ing)d(system)g(pushes)f(some)h(state)h(onto)e(the)h(stack,)i (which)e(is)p Black 1929 5700 a(8)p Black eop d2587 151 a2737 147 9 8 bop Black Black 0 83 a Fx(restored)27 b(later)m(,)j(when)e(the)h (signal)f(handler)f(returns.)48 b(But)29 b(al-)0 183 y(though)h(we)h(return)f(from)h(the)g(signal)g(handler)m(,)h(we)g(jump) e(in)0 282 y(again,)c(and)g(this)h(time)f(we)g(enter)g(not)g(directly)f (at)i(the)f(bottom)0 382 y(of)17 b(the)g(stack,)g(because)f(of)h(the)g Fp(setjmp)p Fx(\(3\))e(in)i(the)g(trampoline)0 482 y(function.)125 587 y(Since)31 b(the)h(operating)d(system)j(has)f(to)h(capture)e(all)j (CPU)0 687 y(re)o(gisters)22 b(\(including)d(those)j(that)g(are)g (ordinarily)e(scratch)i(re)o(g-)0 787 y(isters)f(or)f(caller)n(-sa)n(v) o(e)g(re)o(gisters\),)f(there)h(can)g(be)g(a)h(f)o(air)f(amount)0 886 y(of)h(memory)e(at)i(the)h(bottom)d(of)i(the)g(established)g (thread)f(stack.)0 986 y(F)o(or)d(some)h(systems)g(this)g(can)g(be)g(e) n(v)o(en)e(up)i(to)g(1)g(KB)g(of)g(garbage)0 1085 y([27)o(].)25 b(But)19 b(e)o(xcept)g(for)f(the)i(additional)d(memory)h(consumption)0 1185 y(it)j(does)f(not)g(hurt.)125 1291 y(W)-7 b(e)32 b(just)g(ha)n(v)o(e)f(to)h(k)o(eep)f(in)h(mind)f(this)h(additional)e (stack)0 1391 y(consumption)25 b(when)j(deciding)e(the)i(stack)g(size)h (\()p Fr(sk)p 1569 1391 25 4 v 29 w(size)p Fx(\).)49 b(A)0 1490 y(reasonable)23 b(stack)i(size)g(usually)f(is)i(between)e (16)g(and)g(32)g(KB.)0 1590 y(Less)31 b(is)h(neither)e(reasonable)f (nor)h(al)o(w)o(ays)h(allo)n(wed)f(\(current)0 1689 y(Unix)c(platforms) f(usually)h(require)f(a)i(stack)g(to)f(be)h(at)g(least)g(16)0 1789 y(KB)21 b(in)f(size\).)0 1957 y FA(Stack)26 b(Ov)o(er\003o)o(ws)0 2094 y Fx(There)47 b(is)j(a)e(noticeable)f(dif)n(ference)g(between)g (the)h(initial)0 2193 y Fp(main)p Fx(\(\))29 b(thread)f(and)h(the)h(e)o (xplicitly)f(spa)o(wned)f(threads:)44 b(the)0 2293 y(initial)31 b(thread)f(runs)g(on)h(the)f(standard)g(process)g(stack.)57 b(This)0 2393 y(stack)43 b(automatically)e(can)h(gro)n(w)g(under)f (Unix,)47 b(while)c(the)0 2492 y(stacks)22 b(of)g(the)g(spa)o(wned)f (threads)g(are)h(\002x)o(ed)f(in)h(size.)31 b(So)22 b(stack)0 2592 y(o)o(v)o(er\003o)n(ws)k(can)h(occur)f(for)h(the)g(spa)o(wned)f (threads.)45 b(This)28 b(im-)0 2692 y(plies)g(that)g(the)g(parent)f (has)h(to)h(mak)o(e)e(a)h(reasonable)f(guess)h(of)0 2791 y(the)22 b(threads)f(stack)h(space)g(requirement)d(already)i(at)h(spa)o (wning)0 2891 y(time.)125 2997 y(And)g(there)g(is)i(no)e(really)h (portable)e(solution)h(to)h(this)g(prob-)0 3096 y(lem,)32 b(because)d(e)n(v)o(en)f(if)i(the)g(thread)f(library')-5 b(s)28 b(scheduler)h(can)0 3196 y(detect)16 b(the)g(stack)g(o)o(v)o (er\003o)n(w)-5 b(,)15 b(it)i(cannot)e(easily)h(resize)g(the)h(stack.)0 3296 y(The)34 b(reason)g(is)h(simply)f(that)g(the)h(stack)f (initialization)g(goes)0 3395 y(hand)26 b(in)h(hand)f(with)i(the)f (initialization)f(of)h(the)g(start)h(routine,)0 3495 y(as)d(we)g(discussed)f(before.)36 b(And)24 b(this)h(start)g(routine)e (has)i(to)g(be)0 3594 y(a)e(real)g(C)h(function)d(in)i(order)f(to)g Fr(call)p Fx(.)33 b(But)24 b(once)e(the)h(thread)f(is)0 3694 y(running,)i(there)g(no)h(longer)f(e)o(xists)h(such)g(an)g(entry)f (point.)39 b(So,)0 3794 y(e)n(v)o(en)30 b(if)h(the)g(scheduler)f(w)o (ould)g(be)h(able)f(to)h(gi)n(v)o(e)g(the)g(thread)0 3893 y(a)g(ne)n(w)g(enlar)o(ged)e(stack,)34 b(there)c(is)i(no)e(chance) g(to)i(restart)f(the)0 3993 y(thread)19 b(on)h(this)h(ne)n(w)f(stack.) 125 4099 y(Or)i(more)f(correct,)g(there)g(is)i(no)f Fr(portable)f Fx(w)o(ay)h(to)g(achie)n(v)o(e)0 4198 y(it.)j(As)18 b(with)g(the)g(pre) n(vious)e(problems,)g(there)h(is)i(a)f(non-portable)0 4298 y(solution.)49 b(That)28 b(is)i(why)d(our)h(implementation)e(did)j (not)f(deal)0 4398 y(with)g(this)h(issue.)48 b(Instead)28 b(in)g(practice)f(one)g(usually)h(lets)h(the)0 4497 y(scheduler)16 b(just)h(detect)g(the)g(stack)h(o)o(v)o(er\003o)n(w)d(and)h(terminate)h (the)0 4597 y(thread.)25 b(This)c(is)h(done)d(by)i(using)f(a)h(red)f (zone)g(at)h(the)g(top)f(of)h(the)0 4696 y(stack)i(which)g(is)h(mark)o (ed)e(with)h(a)h(magic)e(v)n(alue)h(the)g(scheduler)0 4796 y(checks)d(between)f(thread)g(dispatching)g(operations.)125 4902 y(Resizing)g(solutions)f(are)h(only)f(possible)h(in)g (semi-portable)0 5001 y(w)o(ays.)39 b(One)25 b(approach)e(is)j(to)f (place)f(the)h(thread)f(stacks)i(into)e(a)0 5101 y(memory)g(mapped)h (area)h(\(see)h Fp(mmap)p Fx(\(2\)\))d(of)i(the)g(process)g(ad-)0 5201 y(dress)31 b(space)f(and)g(let)h(the)g(scheduler)e(catch)i Fp(SIGSEGV)e Fx(sig-)0 5300 y(nals.)70 b(When)34 b(such)h(a)g(signal)g (occurs,)j(because)c(of)h(a)g(stack)0 5400 y(o)o(v)o(er\003o)n(w)i(in)j (this)f(area,)44 b(the)39 b(scheduler)f(e)o(xplicitly)g(resizes)2025 83 y(the)c(memory)e(mapped)h(area.)66 b(This)34 b(resizing)g(can)g(be)g (done)2025 183 y(either)e(by)f(cop)o(ying)f(the)i(stack)h(contents)e (into)h(a)g(ne)n(w)g(lar)o(ger)2025 282 y(area)c(which)g(is)h(then)f (re-mapped)e(to)i(the)h(old)f(address)g(or)g(via)2025 382 y(an)k(e)n(v)o(en)f(more)g(ele)o(gant)f(w)o(ay)-5 b(,)34 b(as)f(the)f(v)o(endor)d(threading)h(li-)2025 482 y(braries)e(of)g Fr(Sun)f(Solaris)p Fx(,)j Fr(F)-5 b(r)m(eeBSD)28 b Fx(and)g Fr(DEC)h(T)-5 b(ru64)28 b Fx(do)g(it:)2025 581 y(the)j(thread)g(stacks)h(are)f(allocated)g(inside)g(memory)f (mapped)2025 681 y(areas)g(which)f(are)h(already)e(initially)i(a)g(fe)n (w)g(MB)g(in)g(\(virtual\))2025 780 y(size)25 b(and)f(then)g(one)h (just)g(relies)g(on)f(the)g(virtual)g(memory)f(sys-)2025 880 y(tem')-5 b(s)31 b(feature)e(that)h(only)f(the)h(actually)g (consumed)e(memory)2025 980 y(space)20 b(is)h(mapped.)2025 1149 y FA(Startup)26 b(Routine)g(T)-9 b(ermination)2025 1287 y Fx(There)48 b(is)i(a)g(cruel)e Fp(abort)p Fx(\(3\))g(call)h(at)h (the)f(end)g(of)f(our)2025 1387 y Fp(mctx)p 2230 1387 V 29 w(create)p 2559 1387 V 29 w(boot)d Fx(function.)97 b(This)45 b(means,)50 b(if)c(the)2025 1486 y(startup)18 b(routine)g(w)o(ould)g(return,)f(the)i(process)f(is)i(aborted.)j(That) 2025 1586 y(is)29 b(ob)o(viously)e(not)h(reasonable,)h(so)f(why)g(ha)n (v)o(e)g(we)h(written)f(it)2025 1685 y(this)21 b(w)o(ay?)2150 1792 y(If)15 b(the)g(thread)f(returns)h(from)f(the)h(startup)g (routine,)g(it)h(should)2025 1891 y(be)22 b(cleanly)f(terminated.)29 b(But)23 b(it)g(cannot)e(terminate)g(itself)i(\(for)2025 1991 y(instance,)g(because)f(it)h(cannot)f(free)g(its)i(o)n(wn)e(stack) h(while)g(run-)2025 2091 y(ning)31 b(on)g(it,)j Fr(etc.)p Fx(\).)59 b(So)31 b(the)h(termination)e(handling)f(actually)2025 2190 y(is)g(the)f(task)g(of)g(the)f(thread)g(library)g(scheduler)-5 b(.)47 b(As)29 b(a)f(conse-)2025 2290 y(quence,)21 b(the)h(thread)f (spa)o(wning)g(function)f(of)i(a)h(thread)e(library)2025 2389 y(should)e(be)h(not)g(directly)g Fp(mctx)p 2973 2389 V 29 w(create)p Fx(.)2150 2496 y(Instead)27 b(the)g(thread)g(spa)o (wning)f(function)g(should)h(use)h(an)2025 2595 y(additional)17 b(trampoline)g(function)f(as)j(the)g(higher)n(-le)n(v)o(el)d(startup) 2025 2695 y(routine.)23 b(And)18 b(this)h(trampoline)e(function)g (performs)g(a)i(conte)o(xt)2025 2795 y(switch)26 b(back)f(into)h(the)f (thread)g(library)g(scheduler)f(before)h(the)2025 2894 y(lo)n(wer)n(-le)n(v)o(el)f(startup)h(routine)f(w)o(ould)g(return.)39 b(The)25 b(scheduler)2025 2994 y(then)g(can)h(safely)f(remo)o(v)o(e)f (the)i(thread)e(and)h(its)i(machine)d(con-)2025 3094 y(te)o(xt.)35 b(That)24 b(is)g(why)f(the)h Fp(abort)p Fx(\(3\))e(call)i(is)h(ne)n(v)o(er)d(reached)g(in)2025 3193 y(practice)27 b(\(more)f(details)i(can)f(be)h(found)d(in)j(the)f (implementa-)2025 3293 y(tions)22 b(of)f Fp(pth)p 2455 3293 V 29 w(spawn)h Fx(and)f Fp(pth)p 3048 3293 V 29 w(exit)g Fx(in)h Fp(pth)p 3535 3293 V 30 w(lib.c)f Fx(of)2025 3392 y Fr(GNU)g(Pth)f Fx([25)o(]\))2025 3562 y FA(The)26 b(sigstack\(2\))f(F)n(allback)f(Situation)2025 3700 y Fx(Not)37 b(all)h(platforms)e(pro)o(vide)g(the)h(standardized)e Fp(sigalt-)2025 3799 y(stack)p Fx(\(2\).)d(Instead)22 b(the)o(y)g(at)i(least)g(pro)o(vide)c(the)j Fr(4.2BSD)f Fx(an-)2025 3899 y(cestor)i(function)f Fp(sigstack)p Fx(\(2\).)35 b(But)24 b(one)g(cannot)f(tri)n(vially)2025 3999 y(replace)c Fp(sigaltstack)p Fx(\(2\))e(by)i Fp(sigstack)p Fx(\(2\))f(in)i(this)g(sit-)2025 4098 y(uation,)c(because)f(in)i (contrast)e(to)i Fp(sigaltstack)p Fx(\(2\),)d(the)i(old)2025 4198 y Fp(sigstack)p Fx(\(2\))34 b(does)h(not)g(automatically)g(handle) f(the)i(ma-)2025 4297 y(chine)20 b(dependent)e(direction)g(of)i(stack)h (gro)n(wth.)2150 4404 y(Instead,)44 b(the)c(caller)g(has)g(to)g(kno)n (w)f(the)h(direction)f(and)2025 4503 y(al)o(w)o(ays)52 b(call)f Fp(sigstack)p Fx(\(2\))e(with)j(the)f(address)g(of)g(the)2025 4603 y(bottom)h(of)g(the)h(stack.)123 b(So,)61 b(in)54 b(a)f(real-w)o(orld)e(imple-)2025 4703 y(mentation)38 b(one)h(\002rst)h(has)g(to)g(determine)e(the)i(direction)e(of)2025 4802 y(stack)i(gro)n(wth)e(in)i(order)f(to)h(use)g Fp(sigstack)p Fx(\(2\))d(as)k(a)f(re-)2025 4902 y(placement)49 b(for)h Fp(sigaltstack)p Fx(\(2\).)113 b(F)o(ortunately)49 b(this)2025 5001 y(is)55 b(easier)e(than)h(it)g(seems)g(on)f(the)h(\002rst)g(look)f (\(for)g(de-)2025 5101 y(tails)43 b(see)g(the)f(macros)f Fp(AC)p 2886 5101 V 30 w(CHECK)p 3166 5101 V 29 w(STACKGROWTH)f Fx(and)2025 5201 y Fp(AC)p 2130 5201 V 30 w(CHECK)p 2410 5201 V 28 w(STACKSETUP)33 b Fx(in)h(\002le)h Fp(aclocal.m4)e Fx(from)2025 5300 y Fr(GNU)24 b(Pth)f Fx([25)o(]\).)33 b(Alternati)n(v)o(ely)21 b(if)j(one)e(can)h(af)n(ford)f(to)h(w)o(aste) 2025 5400 y(memory)-5 b(,)16 b(one)h(can)h(use)g(an)g(ele)o(gant)f (trick:)24 b(to)18 b(set)h(up)e(a)i(stack)f(of)p Black 1929 5700 a(9)p Black eop d2739 70 a2808 69 10 9 bop Black Black 0 83 a Fx(size)21 b Fe(N)9 b Fx(,)21 b(one)f(allocates)g(a)h(chunk)e(of)h(memory)f(\(starting)h(at)h(ad-)0 183 y(dress)c Fe(A)p Fx(\))h(of)f(size)h Fe(N)f Fd(\002)8 b Fc(2)17 b Fx(and)f(then)h(calls)h Fp(sigstack)p Fx(\(2\))d(with)0 282 y(the)20 b(parameters)f Fr(sk)p 581 282 25 4 v 30 w(addr=)p Fe(A)d Fc(+)h Fe(N)30 b Fx(and)19 b Fr(sk)p 1291 282 V 30 w(size=)p Fe(N)9 b Fx(,)21 b Fr(i.e)o(.)p Fx(,)e(one)0 382 y(speci\002es)29 b(the)g(middle)g(of)f(the)h(memory)e (chunk)h(as)h(the)g(stack)0 482 y(base.)0 651 y FA(The)d(Blind)g(Alley) e(of)h(Brain-Dead)h(Platf)n(orms)0 789 y Fx(The)31 b(w)o(orld)g(w)o (ould)f(not)i(be)f(as)h(funn)o(y)d(as)j(it)g(is,)j(if)d(really)f(all)0 888 y(Unix)f(platforms)e(w)o(ould)i(be)g(f)o(air)g(to)g(us.)55 b(Instead,)31 b(currently)0 988 y(at)24 b(least)g(one)e(platform)g(e)o (xists)i(which)f(plays)g(unf)o(air:)30 b(unfortu-)0 1088 y(nately)-5 b(,)26 b(ancient)e(v)o(ersions)h(of)g(the)g(popular)f Fr(GNU/Linux)p Fx(.)40 b(Al-)0 1187 y(though)21 b(we)i(will)h(disco)o (v)o(er)d(that)i(it)h(both)e(pro)o(vides)f Fp(sigalt-)0 1287 y(stack)p Fx(\(2\))c(and)i Fp(sigstack)p Fx(\(2\),)d(our)j (approach)d(w)o(on')o(t)i(w)o(ork)0 1387 y(on)27 b Fr(Linux)g Fx(k)o(ernels)g(prior)f(to)i(v)o(ersion)e(2.2)g(and)h Fr(glibc)g Fx(prior)f(to)0 1486 y(v)o(ersion)19 b(2.1.)125 1592 y(Why?)42 b(Because)26 b(its)h Fr(libc)g Fx(pro)o(vides)d(only)h (stubs)i(of)e(these)0 1692 y(functions)h(which)i(al)o(w)o(ays)g(return) f(just)h Fp(-1)g Fx(with)g Fp(errno)g Fx(set)0 1792 y(to)g Fp(ENOSYS)p Fx(.)f(So,)j(this)e(de\002nitely)f(means)g(that)h(our)f (nifty)g(al-)0 1891 y(gorithm)40 b(is)i(useless)h(there,)j(because)40 b(its)j(central)e(point)g Fr(is)0 1991 y Fp(sigaltstack)p Fx(\(2\)/)p Fp(sigstack)p Fx(\(2\).)31 b(Ne)n(v)o(ertheless)23 b(we)h(do)0 2091 y(not)17 b(need)g(to)h(gi)n(v)o(e)f(up.)23 b(At)18 b(least)h(not,)e(if)h(we,)g(for)f(a)h(single)g(brain-)0 2190 y(dead)29 b(platform,)i(accept)f(to)g(break)f(our)g(general)g (goal)g(of)h(not)0 2290 y(using)20 b(an)o(y)f(platform)g(dependent)f (code.)125 2396 y(So,)g(what)h(can)g(we)g(actually)f(do)g(here?)24 b(All)19 b(we)g(ha)n(v)o(e)f(to)h(do,)0 2496 y(is)f(to)f(\002ddle)g (around)e(a)j(little)g(bit)f(with)h(the)f(machine-dependent)0 2595 y Fp(jmp)p 155 2595 V 29 w(buf)23 b Fx(ingredients)f(\(by)h (poking)e(around)g(in)i Fp(setjmp.h)0 2695 y Fx(or)16 b(by)f(disassembling)g Fp(longjmp)p Fx(\(3\))f(in)j(the)f(deb)n (ugger\).)21 b(Usu-)0 2795 y(ally)e(one)f(just)h(has)g(to)f(do)g(a)h Fp(setjmp)p Fx(\(3\))e(to)i(get)f(an)h(initial)g(state)0 2894 y(in)24 b(the)g Fp(jmp)p 370 2894 V 29 w(buf)g Fx(structure)f(and) h(then)f(manually)f(adjust)i(tw)o(o)0 2994 y(of)34 b(its)h(\002elds:)54 b(the)35 b(program)d(counter)g(\(usually)i(a)h(structure)0 3094 y(member)29 b(with)i(\223)p Fp(pc)p Fx(\224)g(in)g(the)f(name\))g (and)g(the)h(stack)g(pointer)0 3193 y(\(usually)19 b(a)i(structure)e (member)g(with)h(\223)p Fp(sp)p Fx(\224)g(in)h(the)f(name\).)125 3299 y(That)f(is)i(all)f(and)f(can)g(be)h(acceptable)e(for)h(a)i (real-w)o(orld)d(im-)0 3399 y(plementation)e(which)g(really)i(w)o(ants) f(to)h(co)o(v)o(er)e(mostly)h Fr(all)h Fx(plat-)0 3499 y(forms)f(\226)i(at)f(least)h(as)g(long)e(as)i(the)f(special)h (treatment)e(is)i(needed)0 3598 y(just)31 b(for)f(one)h(or)f(tw)o(o)h (platforms.)56 b(But)31 b(one)f(has)h(to)g(k)o(eep)f(in)0 3698 y(mind)e(that)h(it)h(at)g(least)f(breaks)g(one)f(of)h(the)g (initial)g(goals)g(and)0 3798 y(has)20 b(to)h(be)f(treated)g(as)g(a)h (last)g(chance)e(solution.)0 3967 y FA(Functions)26 b(sigsetjmp\(3\))f (and)g(siglongjmp\(3\))125 4105 y Fx(One)42 b(certainly)f(w)o(ants)i (the)g Fr(POSIX)i Fx(thread)d(semantics)0 4204 y(where)50 b(a)h(thread)e(has)i(its)g(o)n(wn)f(signal)g(mask.)115 b(As)51 b(al-)0 4304 y(ready)36 b(mentioned,)i(on)e(some)h(platforms)e Fp(setjmp)p Fx(\(3\))g(and)0 4404 y Fp(longjmp)p Fx(\(3\))e(do)h(not)h (pro)o(vide)d(this)j(and)g(instead)f(one)g(has)0 4503 y(to)23 b(e)o(xplicitly)f(call)i Fp(sigsetjmp)p Fx(\(3\))d(and)h Fp(siglongjmp)p Fx(\(3\))0 4603 y(instead.)111 b(There)49 b(is)h(only)e(one)g(snare:)83 b(on)49 b(some)g(plat-)0 4703 y(forms)36 b Fp(sigsetjmp)p Fx(\(3\)/)p Fp(siglongjmp)p Fx(\(3\))31 b(sa)n(v)o(e)36 b(also)h(in-)0 4802 y(formation)g(about)i (the)g(alternate)g(signals)h(stack.)82 b(So)40 b(here)0 4902 y(one)45 b(has)h(to)f(mak)o(e)g(sure)g(that)h(although)e(the)h (thread)f(dis-)0 5001 y(patching)36 b(later)i(uses)g d2810 87 a2896 81 5101 y(the)43 b(thread)f(creation)g(step)h(in)g Fp(mctx)p 1197 5101 V 29 w(create)g Fx(still)h(uses)0 5201 y(plain)h Fp(setjmp)p Fx(\(3\)/)p Fp(longjmp)p Fx(\(3\))d(calls)k(for)f(the)h (trampo-)0 5300 y(line)g(trick.)100 b(One)46 b(just)g(has)g(to)g(be)f (careful)g(because)g(the)0 5400 y Fp(jmp)p 155 5400 V 29 w(buf)20 b Fx(and)e Fp(sigjmp)p 793 5400 V 29 w(buf)i Fx(structures)e(cannot)g(be)i(mix)o(ed)2025 83 y(between)28 b(calls)h(to)g(the)g Fp(sigsetjmp)p Fx(\(3\)/)p Fp(siglongjmp)p Fx(\(3\))2025 183 y(and)20 b Fp(setjmp)p Fx(\(3\)/)p Fp(longjmp)p Fx(\(3\).)2025 340 y FA(Mor)n(e)25 b(Machine)h(Context)g (Ingr)n(edients)2025 470 y Fx(Finally)-5 b(,)33 b(for)d(a)h(real-w)o (orld)f(threading)f(implementation)f(one)2025 570 y(usually)j(w)o(ants) h(to)g(put)f(more)g(state)h(into)f(the)h(machine)e(con-)2025 670 y(te)o(xt)43 b(structure)f Fp(mctx)p 2731 670 V 29 w(t)p Fx(.)94 b(F)o(or)42 b(instance)h(to)g(ful\002ll)g(more)2025 769 y Fr(POSIX)25 b Fx(threading)20 b(semantics,)i(it)g(is)h (reasonable)d(to)i(also)g(sa)n(v)o(e)2025 869 y(and)37 b(restore)g(the)g(global)g Fp(errno)g Fx(v)n(ariable.)76 b(All)38 b(this)g(can)2025 968 y(be)32 b(easily)h(achie)n(v)o(ed)e(by)h (e)o(xtending)e(the)j Fp(mctx)p 3503 968 V 29 w(t)g Fx(structure)2025 1068 y(with)d(additional)f(\002elds)h(and)f(by)h(making)e(the)i Fp(mctx)p 3656 1068 V 29 w(save)p Fx(,)2025 1168 y Fp(mctx)p 2230 1168 V 29 w(restore)38 b Fx(and)h Fp(mctx)p 3007 1168 V 29 w(switch)f Fx(functions)g(to)h(be)2025 1267 y(a)o(w)o(are)20 b(of)g(them.)2025 1520 y FA(3.6)99 b(Related)26 b(W)-7 b(ork)2025 1681 y Fx(Beside)38 b Fr(GNU)h(Pth)e Fx([25)o(],)42 b(there)37 b(are)h(other)f(multithreading)2025 1781 y(libraries)c(which)f(use)i(v)n(ariants)e(of)h(the)g(presented)f (approach)2025 1880 y(for)23 b(implementing)e(machine)h(conte)o(xts)h (in)g(user)n(-space.)34 b(Most)2025 1980 y(notably)40 b(there)g(are)j(R)q Fm(O)t(B)t(E)t(RT)k Fx(S)t(.)f(T)t Fm(H)t(AU)r Fx(')-5 b(s)42 b Fr(Really)f(Simple)2025 2079 y(Thr)m(eads)18 b Fx(\()p Fr(r)o(sthr)m(eads)p Fx(,)h([27)o(]\))f (package)f(which)h(uses)h Fp(sigalt-)2025 2179 y(stack)p Fx(\(2\))h(in)i(a)g(v)o(ery)f(similar)h(w)o(ay)f(for)h(thread)e (creation,)h(and)2027 2279 y(K)r Fm(O)q(T)n(A)h Fx(A)t Fm(B)t(E)r Fx(')-5 b(s)19 b Fr(P)-7 b(ortable)17 b(Thr)m(ead)g(Libr)o (ary)g Fx(\()p Fr(PTL)p Fx(,)h([24)o(]\))f(which)2025 2378 y(uses)30 b(a)g Fp(sigstack)p Fx(\(2\))d(approach.)51 b(But)30 b(because)f(their)g(ap-)2025 2478 y(proaches)14 b(handle)h(the)h(signal)g(handler)e(scope)h(dif)n(ferently)-5 b(,)14 b(the)o(y)2025 2578 y(are)30 b(not)g(able)h(to)f(achie)n(v)o(e)f (the)i(same)f(amount)f(of)h(portability)2025 2677 y(and)23 b(this)h(w)o(ay)f(do)g(not)g(w)o(ork)g(for)g(instance)g(on)g(some)g (System-)2025 2777 y(V)-8 b(-deri)n(v)o(ed)18 b(platforms.)2025 3030 y FA(3.7)99 b(Summary)26 b(&)f(A)-10 b(v)o(ailability)2025 3190 y Fx(W)j(e)22 b(ha)n(v)o(e)e(presented)f(a)i(pragmatic)f(and)g (mostly)g(portable)g(f)o(all-)2025 3290 y(back)e(approach)e(for)i (implementing)e(the)j(machine)e(conte)o(xt)g(for)2025 3390 y(user)n(-space)30 b(threads,)j(based)e(entirely)f(on)g(Unix)h (system)g(and)2025 3489 y(ANSI-C)24 b(language)d(f)o(acilities.)35 b(The)23 b(approach)e(w)o(as)j(success-)2025 3589 y(fully)i(tested)h (in)g(practice)f(on)h(a)g(wide)g(range)e(of)i(Unix)f(\003a)n(v)n(ors) 2025 3689 y(by)17 b Fr(GNU)h(Pth)f Fx(and)g(should)f(also)h(adapt)g(to) g(the)h(remaining)d(Unix)2025 3788 y(platforms)f(as)i(long)f(as)h(the)o (y)f(adhere)f(to)i(the)f(rele)n(v)n(ant)g(standards.)2150 3891 y(The)50 b Fr(GNU)i(Pth)e Fx(package)g(is)h(distrib)n(uted)f (under)g(the)2025 3990 y(GNU)32 b(Library)f(General)g(Public)g(License) h(\(LGPL)f(2.1\))g(and)2025 4090 y(freely)40 b(a)n(v)n(ailable)h(from)e Fr(http://www)-6 b(.gnu.or)m(g/softwar)m(e/pth/)2025 4189 y Fx(and)20 b Fr(ftp://ftp.gnu.or)m(g/gnu/pth/)p Fx(.)2025 4442 y FA(3.8)99 b(Ackno)o(wledgements)2025 4603 y Fx(I)17 b(w)o(ould)f(lik)o(e)h(to)f(thank)i(R)q Fm(O)t(B)t(E)t(RT)k Fx(S)t(.)f(T)t Fm(H)t(AU)r Fx(,)f(D)q Fm(A)-5 b(V)t(I)t(D)22 b Fx(B)s Fm(U)t(T)t(E)t(N)t Fx(-)2027 4703 y Fm(H)t(O)t(F)r Fx(,)k(M)t Fm(A)t(RT)t(I)t(N)i Fx(K)t Fm(R)t(A)t(E)t(M)t(E)t(R)r Fx(,)f(E)t Fm(R)t(I)t(C)h Fx(N)t Fm(E)t(W)t(T)s(O)t(N)d Fx(and)g(B)t Fm(R)q(U)t(N)t(O)2027 4802 y Fx(H)t Fm(A)t(I)t(B)t(L)t(E)33 b Fx(for)c(their)g(comments)g (which)h(helped)e(to)i(write)h(the)2025 4902 y(initial)d(v)o(ersion)f (of)g(this)h(paper)-5 b(.)48 b(Additionally)-5 b(,)27 b(credit)g(has)h(to)2025 5001 y(be)21 b(gi)n(v)o(en)e(to)k(C)t Fm(H)t(R)t(I)t(S)t(T)s(O)t(P)t(H)t(E)t(R)j Fx(S)t Fm(M)t(A)t(L)t(L)d Fx(and)d(the)g(USENIX)h(re-)2025 5101 y(vie)n(wers)f(for)g(their)g(in)m (v)n(aluable)e(feedback)h(which)g(allo)n(wed)h(this)2025 5201 y(paper)28 b(to)h(be)f(e)o(xtended,)h(cleaned)f(up)g(and)h (\002nally)f(published)2025 5300 y(at)c(the)f(USENIX)g(Annual)f(T)-6 b(echnical)22 b(Conference)g(2000.)32 b(Fi-)2025 5400 y(nally)-5 b(,)27 b(thanks)e(go)h(to)h(all)f(users)h(of)f Fr(GNU)h(Pth)f Fx(for)f(their)h(feed-)p Black 1908 5700 a(10)p Black eop d2898 81 a2978 73 11 10 bop Black Black 0 83 a Fx(back)19 b(on)f(the)i(implementation,)c (which)j(helped)f(in)i(\002ne-tuning)0 183 y(the)g(presented)f (approach.)927 b([)p Fb(rse)p Fx(])0 446 y Fs(Refer)n(ences)p Black 71 615 a Fv([1])p Black 41 w Fu(POSIX)18 b(1003.1c)j(Thr)m (eading)p Fv(,)e(IEEE)f(POSIX)f(1003.1c-1995,)199 706 y(ISO/IEC)g(9945-1:1996)p Black 71 821 a([2])p Black 43 w(M)t(.)t(E)t(.)46 b(C)t Fa(O)t(N)t(W)m(A)n(Y)r Fv(:)70 b Fu(Design)44 b(of)f(a)h(separ)o(able)h(tr)o(ansition-)199 913 y(dia)o(gr)o(am)20 b(compiler)-8 b(.)p Fv(,)19 b(Comm.)f(A)m(CM)h (6:7,)g(1963,)h(p.396-408)p Black 71 1028 a([3])p Black 43 w(E)t(.)t(W)m(.)h(D)t Fa(I)t(J)t(K)t(S)t(T)t(R)t(A)r Fv(:)f Fu(Co-oper)o(ating)g(sequential)g(pr)m(ocesses)p Fv(,)g(in)199 1119 y(F)-6 b(.)24 b(Genuys)j(\(Ed.\),)e Fu(Pr)m(o)o(gr)o(amming)h(Langua)o(g)o(es)p Fv(,)j(N)m(A)-8 b(T)o(O)24 b(Ad-)199 1211 y(v)n(anced)f(Study)f(Institute,)f(Academic)i (Press,)e(London,)i(1965,)199 1302 y(p.42-112.)p Black 71 1417 a([4])p Black 43 w(B)t(.)34 b(N)t Fa(I)t(C)t(H)t(O)t(L)t(S)t Fv(,)i(D)t(.)e(B)s Fa(U)t(T)t(T)t(L)t(A)t(R)t Fv(,)h(J)t(.)t(P)l(.)e(F) o Fa(A)t(R)t(R)t(E)t(L)r Fv(:)45 b Fu(Pthr)m(eads)199 1509 y(Pr)m(o)o(gr)o(amming)23 b(-)g(A)e(POSIX)h(Standar)m(d)j(for)d (Better)g(Multipr)m(o-)199 1600 y(cessing)p Fv(,)e(O'Reilly)-5 b(,)18 b(1996;)h(ISBN)f(1-56592-115-1)p Black 71 1715 a([5])p Black 43 w(B)t(.)27 b(L)t Fa(E)t(W)t(I)t(S)t Fv(,)g(D)t(.)f(J)t(.)h(B)t Fa(E)t(R)t(G)r Fv(:)k Fu(Thr)m(eads)25 b(Primer)f(-)f(A)h(Guide)g(T)-7 b(o)199 1807 y(Multithr)m(eaded)19 b(Pr)m(o)o(gr)o(amming)p Fv(,)f(Prentice)f(Hall,)f(1996;)j(ISBN)199 1898 y(0-13-443698-9)p Black 71 2013 a([6])p Black 43 w(S)t(.)g(J)t(.)g(N)t Fa(O)t(RT)s(O)t(N)t Fv(,)f(M)t(.)i(D)t(.)f(D)t Fa(I)t(P)o(A)t(S)t(Q)s(U)r(A)t(L)t(E)r Fv(:)f Fu(Thr)m(ead)f(T)l(ime)e (-)h(The)199 2105 y(Multithr)m(eaded)g(Pr)m(o)o(gr)o(amming)g(Guide)p Fv(,)f(Prentice)f(Hall,)h(1997;)199 2196 y(ISBN)j(0-13-190067-6)p Black 71 2312 a([7])p Black 43 w(D)t(.)24 b(R)t(.)g(B)s Fa(U)t(T)t(E)t(N)t(H)t(O)t(F)r Fv(:)g Fu(Pr)m(o)o(gr)o(amming)e(with)e (POSIX)g(Thr)m(eads)p Fv(,)199 2403 y(Addison)g(W)-6 b(esle)o(y)h(,)19 b(1997;)g(ISBN)f(0-201-63392-2)p Black 71 2518 a([8])p Black 43 w(S)t(.)27 b(P)t Fa(R)t(A)t(S)t(A)t(D)r Fv(:)32 b Fu(Multithr)m(eading)26 b(Pr)m(o)o(gr)o(amming)f(T)-7 b(ec)o(hniques)p Fv(,)199 2610 y(McGra)o(w-Hill,)18 b(1996;)i(ISBN)e (0-079-12250-7)p Black 71 2725 a([9])p Black 43 w(S)t(.)32 b(K)t Fa(L)t(E)t(I)t(N)t(M)t(A)t(N)t Fv(,)g(B)t(.)h(S)t Fa(M)t(A)t(L)t(D)t(E)t(R)t(S)t Fv(,)e(D)t(.)i(S)t Fa(H)t(A)t(H)r Fv(:)43 b Fu(Pr)m(o)o(gr)o(am-)199 2816 y(ming)29 b(with)e(Thr)m(eads)p Fv(,)k(Prentice)d(Hall,)i(1995;)k(ISBN)27 b(0-131-)199 2908 y(72389-8)p Black 33 3023 a([10])p Black 44 w(C)t(.)t(J)t(.)40 b(N)t Fa(O)t(RT)t(H)t(R)r(U)t(P)r Fv(:)58 b Fu(Pr)m(o)o(gr)o(amming)38 b(W)l(ith)g(Unix)f(Thr)m(eads)p Fv(,)199 3114 y(John)20 b(W)m(ile)o(y)e(&)h(Sons,)g(1996;)h(ISBN)d(0-471-13751-0)p Black 33 3230 a([11])p Black 44 w(P)l(.)29 b(B)r Fa(A)t(RT)s(O)t(N)t Fv(-)t(D)q Fa(A)l(V)t(I)t(S)t Fv(,)g(D)t(.)g(M)t Fa(C)t Fv(N)r Fa(A)t(M)t(E)t(E)t Fv(,)g(R)t(.)h(V)-5 b Fa(A)t(S)t(W)m(A)t(N)t (I)t Fv(,)30 b(E)t(.)201 3321 y(L)t Fa(A)t(Z)t(O)r(W)t(S)t(K)t(A)r Fv(:)19 b Fu(Adding)d(Sc)o(heduler)g(Activations)f(to)f(Mac)o(h)i(3.0)p Fv(,)199 3412 y(Uni)n(v)o(ersity)27 b(of)g(W)-6 b(ashington,)29 b(1992;)j(T)-5 b(echnical)27 b(Report)f(92-)199 3504 y(08-03)p Black 33 3619 a([12])p Black 44 w(D)t(.)18 b(S)t Fa(T)t(E)t(I)t(N)t Fv(,)e(D)t(.)h(S)t Fa(H)t(A)t(H)r Fv(:)j Fu(Implementing)c(Lightwight)e(Thr)m(eads)p Fv(,)199 3710 y(SunSoft)19 b(Inc.,)f(1992)i(\(published)g(at)f(USENIX'92\).)p Black 33 3826 a([13])p Black 44 w(W)m(.)t(R)t(.)t(S)t Fa(T)t(E)t(V)t(E)t(N)t(S)r Fv(:)48 b Fu(Advanced)35 b(Pr)m(o)o(gr)o (amming)g(in)f(the)h(Unix)199 3917 y(En)m(vir)m(onment)p Fv(,)58 b(Addison-W)-6 b(esle)o(y)h(,)58 b(1992;)66 b(ISBN)49 b(0-201-)199 4008 y(56317-7)p Black 33 4124 a([14])p Black 44 w(D)t(.)42 b(L)t Fa(E)t(W)t(I)t(N)t(E)r Fv(:)59 b Fu(POSIX)38 b(Pr)m(o)o(gr)o(ammer')m(s)h(Guide:)63 b(Writing)199 4215 y(P)-6 b(ortable)34 b(Unix)g(Pr)m(o)o(gr)o(ams)p Fv(,)k(O'Reilly)33 b(&)h(Associates,Inc.,)199 4306 y(1994;)20 b(ISBN)e(0-937175-73-0)p Black 33 4422 a([15])p Black 44 w(B)t Fa(RY)m(A)t(N)137 b Fv(O)t(')t(S)t Fa(U)t(L)t(L)t(I)t(V)l(A)t (N)r Fv(:)250 b Fu(F)l(r)m(equently)136 b(ask)o(ed)199 4513 y(questions)187 b(for)e(comp.os.r)m(esear)m(c)o(h)p Fv(,)229 b(1995;)199 4604 y(http://www)-5 b(.serpentine.com/)p Fk(\230)p Fv(bos/os-f)o(aq/,)199 4696 y d2980 87 a3066 82 33 4811 a([16])p Black 44 w(S)t Fa(U)t(N)24 b Fv(M)t Fa(I)t(C)t(R)r(O)t(S)t(Y)t(S)t(T)t(E)t(M)t(S)t Fv(,)e(I)t Fa(N)t(C)r Fv(:)27 b Fu(Thr)m(eads)22 b(F)l(r)m(equently)g(Ask)o(ed)199 4902 y(Questions)p Fv(,)111 b(1995,)f(http://www.sun.com/w)o(orkshop/-) 199 4994 y(threads/f)o(aq.html)p Black 2058 83 a([17])p Black 44 w(B)t Fa(RY)m(A)t(N)78 b Fv(O)t(')t(S)t Fa(U)t(L)t(L)t(I)t(V)l (A)t(N)r Fv(:)132 b Fu(F)l(r)m(equently)76 b(ask)o(ed)g(ques-)2224 174 y(tions)134 b(for)g(comp.pr)m(o)o(gr)o(amming)o(.thr)m(eads)p Fv(,)165 b(1997;)2224 266 y(http://www)-5 b(.serpentine.com/)p Fk(\230)p Fv(bos/threads-f)o(aq/.)p Black 2058 410 a([18])p Black 44 w(B)t Fa(I)t(L)83 b Fv(L)t Fa(E)t(W)t(I)t(S)r Fv(:)144 b Fu(F)l(r)m(equently)82 b(ask)o(ed)f(questions)h(for)2224 502 y(comp.pr)m(o)o(gr)o(amming)o(.thr)m(eads)p Fv(,)213 b(1999;)250 b(http://-)2224 593 y(www)-5 b(.lambdacs.com/ne)n (wsgroup/F)f(A)l(Q.html)p Black 2058 738 a([19])p Black 44 w(N)t Fa(U)t(M)t(E)t(R)t(I)t(C)57 b Fv(Q)s Fa(U)t(E)t(S)t(T)h Fv(I)t Fa(N)t(C)r Fv(:)96 b Fu(Multithr)m(eading)58 b(-)e(De\002ni-) 2224 829 y(tions)k(and)h(Guidelines)p Fv(;)81 b(1998;)g(http://www)-5 b(.numeric-)2224 920 y(quest.com/lang/multi-frame.html)p Black 2058 1065 a([20])p Black 44 w(T)t Fa(H)t(E)42 b Fv(O)t Fa(P)t(E)t(N)f Fv(G)t Fa(R)r(O)t(U)t(P)r Fv(:)62 b Fu(The)39 b(Single)h(Unix)f(Speci\002cation,)2224 1156 y(V)-8 b(er)o(sion)46 b(2)g(-)f(Thr)m(eads)p Fv(;)60 b(1997;)f(http://www.opengroup-)2224 1248 y(.or)o (g/onlinepubs/007908799/xsh/threads.html)p Black 2058 1393 a([21])p Black 44 w(S)t Fa(U)t(N)41 b Fv(M)t Fa(I)t(C)t(R)r(O)t(S) t(Y)t(S)t(T)t(E)t(M)t(S)d Fv(I)t Fa(N)t(C)r Fv(:)61 b Fu(SMI)39 b(Thr)m(ead)g(Resour)m(ces)p Fv(;)2224 1484 y(http://www)-5 b(.sun.com/w)o(orkshop/threads)p Black 2058 1629 a([22])p Black 44 w(F)t Fa(R)t(A)t(N)t(K)29 b Fv(M)t Fa(U)t(E)t(L)t(L)t(E)t(R)r Fv(:)35 b Fu(FSU)26 b(pthr)m(eads)p Fv(;)32 b(1997;)g(http://www-)2224 1720 y(.cs.fsu.edu/)p Fk(\230)p Fv(mueller/pthreads/)p Black 2058 1865 a([23])p Black 44 w(C)t Fa(H)t(R)t(I)t(S)85 b Fv(P)t Fa(R)r(O)q(V)t(E)t(N)t(Z)t(A)t(N)t(O)r Fv(:)145 b Fu(MIT)82 b(pthr)m(eads)p Fv(;)115 b(1993;)2224 1956 y(http://www)-5 b(.mit.edu/people/prov)o(en/pthreads.html)2224 2047 y(\(old\),)209 b(http://www.humanf)o(actor.com/pthreads/mit-)2224 2139 y(pthreads.html)20 b(\(updated\))p Black 2058 2283 a([24])p Black 44 w(K)r Fa(O)r(T)n(A)29 b Fv(A)t Fa(B)t(E)r Fv(:)35 b Fu(P)-6 b(ortable)27 b(Thr)m(eading)g(Libr)o(ary)g Fv(\(PTL\);)d(1999;)2224 2375 y(http://www.media.osaka-cu.ac.jp/)p Fk(\230)p Fv(k-abe/PTL/)p Black 2058 2519 a([25])p Black 44 w(R)t Fa(A)t(L)t(F)19 b Fv(S)t(.)h(E)t Fa(N)t(G)t(E)t(L)t(S)t(C)t(H) t(A)t(L)t(L)r Fv(:)d Fu(GNU)g(P)-6 b(ortable)17 b(Thr)m(eads)h Fv(\(Pth\);)2224 2611 y(1999;)55 b(http://www.gnu.or)o(g/softw)o (are/pth/,)47 b(ftp://ftp.gnu-)2224 2702 y(.or)o(g/gnu/pth/)p Black 2058 2847 a([26])p Black 44 w(M)t Fa(I)t(C)t(H)t(A)t(E)t(L)36 b Fv(T)n(.)i(P)t Fa(E)t(T)t(E)t(R)t(S)t(O)t(N)r Fv(:)50 b Fu(POSIX)34 b(and)h(DCE)f(Thr)m(eads)2224 2938 y(F)-8 b(or)57 b(Linux)h Fv(\(PCThreads\);)75 b(1995;)j(http://members.aa-) 2224 3029 y(.net/)p Fk(\230)p Fv(mtp/PCthreads.html)p Black 2058 3174 a([27])p Black 44 w(R)q Fa(O)t(B)t(E)t(RT)27 b Fv(S)t(.)h(T)t Fa(H)t(A)q(U)r Fv(:)35 b Fu(Really)25 b(Simple)g(Thr)m(eads)i Fv(\(rsthreads\);)2224 3265 y(1996;)20 b(ftp://ftp.ai.mit.edu/pub/rst/)p Black 2058 3410 a([28])p Black 44 w(J)t Fa(O)t(H)t(N)94 b Fv(B)t Fa(I)t(R)t(R)t(E)t(L)t(L)r Fv(:)163 b Fu(F)l(r)m(eeBSD)91 b(uthr)m(eads)p Fv(;)129 b(1998;)2224 3502 y(ftp://ftp.freebsd.or)o(g/pub/FreeBSD/FreeBSD-)2224 3593 y(current/src/lib/libc)p 2780 3593 23 4 v 27 w(r/uthread/)p Black 2058 3738 a([29])p Black 44 w(X)t Fa(A)l(V)t(I)t(E)t(R)46 b Fv(L)t Fa(E)t(R)r(O)q(Y)r Fv(:)70 b Fu(The)44 b(LinuxThr)m(eads)h (libr)o(ary)p Fv(;)57 b(1999;)2224 3829 y(http://pauillac.inria.fr/)p Fk(\230)p Fv(xlero)o(y/linuxthreads/)p Black 2058 3974 a([30])p Black 44 w(I)t(B)t(M)r(:)17 b Fu(AIX)h(V)-8 b(er)o(sion)18 b(4.3)h(Gener)o(al)g(Pr)m(o)o(gr)o(amming)g(Concepts:) 2224 4065 y(Writing)56 b(and)g(Deb)o(ug)o(ging)h(Pr)m(o)o(gr)o(ams;)74 b(Under)o(standing)2224 4156 y(Thr)m(eads)p Fv(;)41 b(1998;)h (http://www.rs6000.ibm.com/doc)p 3714 4156 V 28 w(link/-)2224 4248 y(en)p 2298 4248 V 28 w(US/a)p 2476 4248 V 26 w(doc)p 2609 4248 V 28 w(lib/aixpr)o(ggd/genprogc/understanding-)p 2224 4339 V 2247 4339 a(threads.htm)p Black 2058 4484 a([31])p Black 42 w Fu(Netscape)110 b(P)-6 b(ortable)110 b(Runtime)f Fv(\(NSPR\);)f(http://-)2224 4575 y(www)-5 b(.mozilla.or)o(g/docs/refList/refNSPR/,)246 b(http://-)2224 4666 y(lxr)l(.mozilla.or)o(g/seamonk)o(e)o(y/source/nsprpub/)p Black 2058 4811 a([32])p Black 44 w(R)t Fa(A)t(L)t(F)45 b Fv(S)t(.)f(E)t Fa(N)t(G)t(E)t(L)t(S)t(C)t(H)t(A)t(L)t(L)r Fv(:)65 b Fu(Simple)42 b(Mac)o(hine)h(Conte)o(xt)2224 4902 y(Benc)o(hmark)q Fv(;)73 b(2000;)f(http://www)-5 b(.gnu.or)o(g/softw)o(are/pth-)2224 4994 y(/smcb)m(.tar)l(.gz)p Black 1908 5700 a Fx(11)p Black eop d3068 1 a3068 1 end @ 1.7 log @*** empty log message *** @ text @d13 1 a13 1 %DVIPSSource: TeX output 2000.06.02:1740 d2417 2 a2418 2 (orld)f(threading)f(implementation)f(one)2025 570 y(usually)35 b(w)o(ant)g(to)g(put)g(more)f(state)i(into)f(the)g(machine)f(con-)2025 @ 1.6 log @*** empty log message *** @ text @d13 1 a13 1 %DVIPSSource: TeX output 2000.06.02:1735 d322 6 a327 12 %DVIPSBitmapFont: Fc cmr10 10 4 /Fc 4 51 df<146014E0EB01C0EB0380EB0700130E5B133C13385B13F05B1201485AA248 5AA348C7FCA25A121EA2123EA2123CA2127CA5127812F8B01278127CA5123CA2123EA212 1EA2121F7EA26C7EA36C7EA26C7E12007F13707F133C131C7F7FEB0380EB01C0EB00E014 60135278BD20>40 D<7E7E7E12707E7E7E120F7E6C7E7F12017F6C7EA21378A37FA2133E 131EA2131FA27FA21480A5130714C0B01480130FA51400A25BA2131EA2133E133CA25BA3 5BA2485A5B12035B48C7FC5A120E5A5A5A5A5A5A12527BBD20>I<15301578B3A6007FB8 12F8B912FCA26C17F8C80078C8FCB3A6153036367BAF41>43 D50 D E d2271 5 a2275 5 1125 y FA(Startup)26 b(Routine)g(T)-9 b(ermination)2025 1247 y Fx(There)48 b(is)i(a)g(cruel)e Fp(abort)p Fx(\(3\))g(call)h(at)h (the)f(end)g(of)f(our)2025 1347 y Fp(mctx)p 2230 1347 V 29 w(create)p 2559 1347 V 29 w(boot)d Fx(function.)97 b(This)45 b(means,)50 b(if)c(the)2025 1446 y(startup)18 d2277 4 a2280 4 2025 1546 y(is)29 b(ob)o(viously)e(not)h(reasonable,)h(so)f(why)g(ha)n (v)o(e)g(we)h(written)f(it)2025 1646 y(this)21 b(w)o(ay?)2150 1745 y(If)15 b(the)g(thread)f(returns)h(from)f(the)h(startup)g (routine,)g(it)h(should)2025 1845 y(be)22 b(cleanly)f(terminated.)29 d2282 2 a2283 2 1944 y(instance,)g(because)f(it)h(cannot)f(free)g(its)i(o)n(wn)e(stack) h(while)g(run-)2025 2044 y(ning)31 b(on)g(it,)j Fr(etc.)p d2285 2 a2286 2 2144 y(is)g(the)f(task)g(of)g(the)f(thread)g(library)g(scheduler)-5 b(.)47 b(As)29 b(a)f(conse-)2025 2243 y(quence,)21 b(the)h(thread)f d2288 3 a2290 3 2343 y(should)e(be)h(not)g(directly)g Fp(mctx)p 2973 2343 V 29 w(create)p Fx(.)2150 2443 y(Instead)27 b(the)g(thread)g(spa)o (wning)f(function)g(should)h(use)h(an)2025 2542 y(additional)17 d2292 3 a2294 3 2025 2642 y(routine.)23 b(And)18 b(this)h(trampoline)e(function)g (performs)g(a)i(conte)o(xt)2025 2742 y(switch)26 b(back)f(into)h(the)f (thread)g(library)g(scheduler)f(before)h(the)2025 2841 d2296 2 a2297 2 b(The)25 b(scheduler)2025 2941 y(then)g(can)h(safely)f(remo)o(v)o(e)f (the)i(thread)e(and)h(its)i(machine)d(con-)2025 3040 d2300 6 a2305 6 3140 y(practice)27 b(\(more)f(details)i(can)f(be)h(found)d(in)j(the)f (implementa-)2025 3240 y(tions)22 b(of)f Fp(pth)p 2455 3240 V 29 w(spawn)h Fx(and)f Fp(pth)p 3048 3240 V 29 w(exit)g Fx(in)h Fp(pth)p 3535 3240 V 30 w(lib.c)f Fx(of)2025 3339 y Fr(GNU)g(Pth)f Fx([25)o(]\))2025 3484 y FA(The)26 b(sigstack\(2\))f(F)n(allback)f(Situation)2025 3607 y d2307 1 a2307 1 Fp(sigalt-)2025 3706 y(stack)p Fx(\(2\).)d(Instead)22 d2309 1 a2309 1 Fx(an-)2025 3806 y(cestor)i(function)f Fp(sigstack)p d2311 2 a2312 2 3906 y(replace)c Fp(sigaltstack)p Fx(\(2\))e(by)i Fp(sigstack)p Fx(\(2\))f(in)i(this)g(sit-)2025 4005 y(uation,)c(because)f(in)i d2314 4 a2317 4 4105 y Fp(sigstack)p Fx(\(2\))34 b(does)h(not)g(automatically)g(handle) f(the)i(ma-)2025 4204 y(chine)20 b(dependent)e(direction)g(of)i(stack)h (gro)n(wth.)2150 4304 y(Instead,)44 b(the)c(caller)g(has)g(to)g(kno)n (w)f(the)h(direction)f(and)2025 4404 y(al)o(w)o(ays)52 d2319 2 a2320 2 4503 y(bottom)h(of)g(the)h(stack.)123 b(So,)61 b(in)54 b(a)f(real-w)o(orld)e(imple-)2025 4603 y(mentation)38 d2322 2 a2323 2 4703 y(stack)i(gro)n(wth)e(in)i(order)f(to)h(use)g Fp(sigstack)p Fx(\(2\))d(as)k(a)f(re-)2025 4802 y(placement)49 b(for)h d2325 6 a2330 6 4902 y(is)55 b(easier)e(than)h(it)g(seems)g(on)f(the)h(\002rst)g(look)f (\(for)g(de-)2025 5001 y(tails)43 b(see)g(the)f(macros)f Fp(AC)p 2886 5001 V 30 w(CHECK)p 3166 5001 V 29 w(STACKGROWTH)f Fx(and)2025 5101 y Fp(AC)p 2130 5101 V 30 w(CHECK)p 2410 5101 V 28 w(STACKSETUP)33 b Fx(in)h(\002le)h Fp(aclocal.m4)e Fx(from)2025 5201 y Fr(GNU)24 b(Pth)f Fx([25)o(]\).)33 d2332 3 a2334 4 2025 5300 y(memory)-5 b(,)24 b(one)h(can)h(use)f(an)h(ele)o(gant)e (trick:)36 b(to)25 b(set)h(up)f(a)h(stack)2025 5400 y(of)e(size)h Fe(N)9 b Fx(,)25 b(one)e(allocates)h(a)h(chunk)e(of)g(memory)g (\(starting)g(at)p Black 1929 5700 a(9)p Black eop d2336 16 a2351 15 10 9 bop Black Black 0 83 a Fx(address)24 b Fe(A)p Fx(\))h(of)g(size)g Fe(N)31 b Fd(\002)21 b Fc(2)k Fx(and)f(then)g(calls)i Fp(sigstack)p Fx(\(2\))0 183 y(with)h(the)h(parameters)e Fr(sk)p 771 183 25 4 v 30 w(addr=)p Fe(A)d Fc(+)g(\()p Fe(N)9 b Fc(\))28 b Fx(and)f Fr(sk)p 1573 183 V 30 w(size=)p Fe(N)9 b Fx(,)0 282 y Fr(i.e)o(.)p Fx(,)19 b(one)f(speci\002es)i(the)e (middle)h(of)f(the)h(memory)e(chunk)h(as)i(the)0 382 y(stack)g(base.)0 527 y FA(The)26 b(Blind)g(Alley)e(of)h(Brain-Dead)h (Platf)n(orms)0 649 y Fx(The)31 b(w)o(orld)g(w)o(ould)f(not)i(be)f(as)h (funn)o(y)d(as)j(it)g(is,)j(if)d(really)f(all)0 749 y(Unix)f(platforms) e(w)o(ould)i(be)g(f)o(air)g(to)g(us.)55 b(Instead,)31 b(currently)0 849 y(at)24 b(least)g(one)e(platform)g(e)o(xists)i(which) f(plays)g(unf)o(air:)30 b(unfortu-)0 948 y(nately)-5 b(,)26 b(ancient)e(v)o(ersions)h(of)g(the)g(popular)f Fr(GNU/Linux)p Fx(.)40 b(Al-)0 1048 y(though)21 b(we)i(will)h(disco)o d2353 2 a2354 2 1147 y(stack)p Fx(\(2\))c(and)i Fp(sigstack)p Fx(\(2\),)d(our)j (approach)d(w)o(on')o(t)i(w)o(ork)0 1247 y(on)27 b Fr(Linux)g d2356 4 a2359 4 Fr(glibc)g Fx(prior)f(to)0 1347 y(v)o(ersion)19 b(2.1.)125 1446 y(Why?)42 b(Because)26 b(its)h Fr(libc)g Fx(pro)o(vides)d(only)h (stubs)i(of)e(these)0 1546 y(functions)h(which)i(al)o(w)o(ays)g(return) f(just)h Fp(-1)g Fx(with)g Fp(errno)g Fx(set)0 1646 y(to)g d2361 2 a2362 2 (nifty)g(al-)0 1745 y(gorithm)40 b(is)i(useless)h(there,)j(because)40 b(its)j(central)e(point)g Fr(is)0 1845 y Fp(sigaltstack)p d2364 1 a2364 1 b(we)h(do)0 1944 y(not)17 b(need)g(to)h(gi)n(v)o(e)f(up.)23 d2366 4 a2369 4 2044 y(dead)29 b(platform,)i(accept)f(to)g(break)f(our)g(general)g (goal)g(of)h(not)0 2144 y(using)20 b(an)o(y)f(platform)g(dependent)f (code.)125 2243 y(So,)g(what)h(can)g(we)g(actually)f(do)g(here?)24 b(All)19 b(we)g(ha)n(v)o(e)f(to)h(do,)0 2343 y(is)f(to)f(\002ddle)g d2371 2 a2372 2 2443 y Fp(jmp)p 155 2443 V 29 w(buf)23 b Fx(ingredients)f(\(by)h (poking)e(around)g(in)i Fp(setjmp.h)0 2542 y Fx(or)16 d2374 1 a2374 1 (ugger\).)21 b(Usu-)0 2642 y(ally)e(one)f(just)h(has)g(to)f(do)g(a)h d2376 2 a2377 2 2742 y(in)24 b(the)g Fp(jmp)p 370 2742 V 29 w(buf)g Fx(structure)f(and) h(then)f(manually)f(adjust)i(tw)o(o)0 2841 y(of)34 b(its)h(\002elds:)54 d2379 2 a2380 2 2941 y(member)29 b(with)i(\223)p Fp(pc)p Fx(\224)g(in)g(the)f(name\))g (and)g(the)h(stack)g(pointer)0 3040 y(\(usually)19 b(a)i(structure)e d2382 3 a2384 3 3140 y(That)f(is)i(all)f(and)f(can)g(be)h(acceptable)e(for)h(a)i (real-w)o(orld)d(im-)0 3240 y(plementation)e(which)g(really)i(w)o(ants) f(to)h(co)o(v)o(er)e(mostly)h Fr(all)h Fx(plat-)0 3339 d2386 1 a2386 1 (treatment)e(is)i(needed)0 3439 y(just)31 b(for)f(one)h(or)f(tw)o(o)h d2388 5 a2392 5 3539 y(mind)e(that)h(it)h(at)g(least)f(breaks)g(one)f(of)h(the)g (initial)g(goals)g(and)0 3638 y(has)20 b(to)h(be)f(treated)g(as)g(a)h (last)g(chance)e(solution.)0 3783 y FA(Functions)26 b(sigsetjmp\(3\))f (and)g(siglongjmp\(3\))125 3906 y Fx(One)42 b(certainly)f(w)o(ants)i (the)g Fr(POSIX)i Fx(thread)d(semantics)0 4005 y(where)50 d2394 3 a2396 3 b(As)51 b(al-)0 4105 y(ready)36 b(mentioned,)i(on)e(some)h(platforms)e Fp(setjmp)p Fx(\(3\))g(and)0 4204 y Fp(longjmp)p Fx(\(3\))e(do)h(not)h (pro)o(vide)d(this)j(and)g(instead)f(one)g(has)0 4304 d2398 1 a2398 1 Fp(siglongjmp)p Fx(\(3\))0 4404 y(instead.)111 b(There)49 d2400 2 a2401 2 4503 y(forms)36 b Fp(sigsetjmp)p Fx(\(3\)/)p Fp(siglongjmp)p Fx(\(3\))31 b(sa)n(v)o(e)36 b(also)h(in-)0 4603 y(formation)g(about)i d2403 2 a2404 2 4703 y(one)45 b(has)h(to)f(mak)o(e)g(sure)g(that)h(although)e(the)h (thread)f(dis-)0 4802 y(patching)36 b(later)i(uses)g d2406 2 a2407 2 4902 y(the)43 b(thread)f(creation)g(step)h(in)g Fp(mctx)p 1197 4902 V 29 w(create)g Fx(still)h(uses)0 5001 y(plain)h d2409 4 a2412 4 (trampo-)0 5101 y(line)g(trick.)100 b(One)46 b(just)g(has)g(to)g(be)f (careful)g(because)g(the)0 5201 y Fp(jmp)p 155 5201 V 29 w(buf)20 b Fx(and)e Fp(sigjmp)p 793 5201 V 29 w(buf)i Fx(structures)e(cannot)g(be)i(mix)o(ed)0 5300 y(between)28 d2414 9 a2422 9 Fx(\(3\))0 5400 y(and)20 b Fp(setjmp)p Fx(\(3\)/)p Fp(longjmp)p Fx(\(3\).)2025 83 y FA(Mor)n(e)25 b(Machine)h(Context)g(Ingr)n(edients) 2025 218 y Fx(Finally)-5 b(,)33 b(for)d(a)h(real-w)o(orld)f(threading)f (implementation)f(one)2025 317 y(usually)35 b(w)o(ant)g(to)g(put)g (more)f(state)i(into)f(the)g(machine)f(con-)2025 417 y(te)o(xt)43 b(structure)f Fp(mctx)p 2731 417 V 29 w(t)p Fx(.)94 b(F)o(or)42 b(instance)h(to)g(ful\002ll)g(more)2025 517 y Fr(POSIX)25 b Fx(threading)20 b(semantics,)i(it)g(is)h (reasonable)d(to)i(also)g(sa)n(v)o(e)2025 616 y(and)37 d2424 8 a2431 8 b(All)38 b(this)g(can)2025 716 y(be)32 b(easily)h(achie)n(v)o(ed)e(by)h (e)o(xtending)e(the)j Fp(mctx)p 3503 716 V 29 w(t)g Fx(structure)2025 815 y(with)d(additional)f(\002elds)h(and)f(by)h(making)e(the)i Fp(mctx)p 3656 815 V 29 w(save)p Fx(,)2025 915 y Fp(mctx)p 2230 915 V 29 w(restore)38 b Fx(and)h Fp(mctx)p 3007 915 V 29 w(switch)f Fx(functions)g(to)h(be)2025 1015 y(a)o(w)o(are)20 b(of)g(them.)2025 1280 y FA(3.6)99 b(Related)26 b(W)-7 b(ork)2025 1445 y Fx(Beside)38 b Fr(GNU)h(Pth)e d2433 3 a2435 3 1545 y(libraries)c(which)f(use)i(v)n(ariants)e(of)h(the)g(presented)f (approach)2025 1645 y(for)23 b(implementing)e(machine)h(conte)o(xts)h (in)g(user)n(-space.)34 b(Most)2025 1744 y(notably)40 d2438 2 a2439 2 1844 y(Thr)m(eads)18 b Fx(\()p Fr(r)o(sthr)m(eads)p Fx(,)h([27)o(]\))f (package)f(which)h(uses)h Fp(sigalt-)2025 1943 y(stack)p d2441 1 a2441 1 (creation,)h(and)2027 2043 y(K)r Fm(O)q(T)n(A)h Fx(A)t d2444 2 a2445 2 2143 y(uses)30 b(a)g Fp(sigstack)p Fx(\(2\))d(approach.)51 b(But)30 b(because)f(their)g(ap-)2025 2242 y(proaches)14 d2447 2 a2448 2 b(,)14 b(the)o(y)2025 2342 y(are)30 b(not)g(able)h(to)f(achie)n(v)o(e)f (the)i(same)f(amount)f(of)h(portability)2025 2442 y(and)23 d2450 4 a2453 4 (System-)2025 2541 y(V)-8 b(-deri)n(v)o(ed)18 b(platforms.)2025 2807 y FA(3.7)99 b(Summary)26 b(&)f(A)-10 b(v)o(ailability)2025 2972 y Fx(W)j(e)22 b(ha)n(v)o(e)e(presented)f(a)i(pragmatic)f(and)g (mostly)g(portable)g(f)o(all-)2025 3071 y(back)e(approach)e(for)i d2455 3 a2457 3 3171 y(user)n(-space)30 b(threads,)j(based)e(entirely)f(on)g(Unix)h (system)g(and)2025 3271 y(ANSI-C)24 b(language)d(f)o(acilities.)35 b(The)23 b(approach)e(w)o(as)j(success-)2025 3370 y(fully)i(tested)h d2459 2 a2460 2 2025 3470 y(by)17 b Fr(GNU)h(Pth)f Fx(and)g(should)f(also)h(adapt)g(to) g(the)h(remaining)d(Unix)2025 3570 y(platforms)f(as)i(long)f(as)h(the)o d2462 3 a2464 3 3674 y(The)50 b Fr(GNU)i(Pth)e Fx(package)g(is)h(distrib)n(uted)f (under)g(the)2025 3774 y(GNU)32 b(Library)f(General)g(Public)g(License) h(\(LGPL)f(2.1\))g(and)2025 3873 y(freely)40 b(a)n(v)n(ailable)h(from)e d2466 3 a2468 3 3973 y Fx(and)20 b Fr(ftp://ftp.gnu.or)m(g/gnu/pth/)p Fx(.)2025 4238 y FA(3.8)99 b(Ackno)o(wledgements)2025 4404 y Fx(I)17 b(w)o(ould)f(lik)o(e)h(to)f(thank)i(R)q d2471 1 a2471 1 4503 y Fm(H)t(O)t(F)r Fx(,)k(M)t Fm(A)t(RT)t(I)t(N)i d2474 2 a2475 2 4603 y Fx(H)t Fm(A)t(I)t(B)t(L)t(E)33 b Fx(for)c(their)g(comments)g (which)h(helped)e(to)i(write)h(the)2025 4703 y(initial)d(v)o(ersion)f d2477 1 a2477 1 b(credit)g(has)h(to)2025 4802 y(be)21 b(gi)n(v)o(en)e(to)k(C)t d2479 1 a2479 1 Fx(and)d(the)g(USENIX)h(re-)2025 4902 y(vie)n(wers)f(for)g(their)g(in)m d2481 3 a2483 3 5001 y(paper)28 b(to)h(be)f(e)o(xtended,)h(cleaned)f(up)g(and)h (\002nally)f(published)2025 5101 y(at)c(the)f(USENIX)g(Annual)f(T)-6 b(echnical)22 b(Conference)g(2000.)32 b(Fi-)2025 5201 d2485 2 a2486 4 Fr(GNU)h(Pth)f Fx(for)f(their)h(feed-)2025 5300 y(back)19 b(on)f(the)i(implementation,)c(which)j(helped)f(in)i(\002ne-tuning)2025 5400 y(the)g(presented)f(approach.)927 b([)p Fb(rse)p Fx(])p Black 1908 5700 a(10)p Black eop d2488 13 a2500 10 11 10 bop Black Black 0 83 a Fs(Refer)n(ences)p Black 71 252 a Fv([1])p Black 41 w Fu(POSIX)18 b(1003.1c)j(Thr)m(eading)p Fv(,)e(IEEE)f(POSIX)f(1003.1c-1995,)199 343 y(ISO/IEC)g(9945-1:1996)p Black 71 462 a([2])p Black 43 w(M)t(.)t(E)t(.)46 b(C)t Fa(O)t(N)t(W)m(A)n(Y)r Fv(:)70 b Fu(Design)44 b(of)f(a)h(separ)o(able)h (tr)o(ansition-)199 554 y(dia)o(gr)o(am)20 b(compiler)-8 b(.)p Fv(,)19 b(Comm.)f(A)m(CM)h(6:7,)g(1963,)h(p.396-408)p Black 71 673 a([3])p Black 43 w(E)t(.)t(W)m(.)h(D)t Fa(I)t(J)t(K)t(S)t (T)t(R)t(A)r Fv(:)f Fu(Co-oper)o(ating)g(sequential)g(pr)m(ocesses)p Fv(,)g(in)199 764 y(F)-6 b(.)24 b(Genuys)j(\(Ed.\),)e d2502 3 a2504 3 b(T)o(O)24 b(Ad-)199 856 y(v)n(anced)f(Study)f(Institute,)f(Academic)i (Press,)e(London,)i(1965,)199 947 y(p.42-112.)p Black 71 1066 a([4])p Black 43 w(B)t(.)34 b(N)t Fa(I)t(C)t(H)t(O)t(L)t(S)t d2507 3 a2509 3 1158 y(Pr)m(o)o(gr)o(amming)23 b(-)g(A)e(POSIX)h(Standar)m(d)j(for)d (Better)g(Multipr)m(o-)199 1249 y(cessing)p Fv(,)e(O'Reilly)-5 b(,)18 b(1996;)h(ISBN)f(1-56592-115-1)p Black 71 1368 d2512 1 a2512 1 b(Primer)f(-)f(A)h(Guide)g(T)-7 b(o)199 1460 y(Multithr)m(eaded)19 d2514 1 a2514 1 1551 y(0-13-443698-9)p Black 71 1670 a([6])p Black 43 d2517 3 a2519 3 (-)h(The)199 1762 y(Multithr)m(eaded)g(Pr)m(o)o(gr)o(amming)g(Guide)p Fv(,)f(Prentice)f(Hall,)h(1997;)199 1853 y(ISBN)j(0-13-190067-6)p Black 71 1973 a([7])p Black 43 w(D)t(.)24 b(R)t(.)g(B)s d2521 1 a2521 1 (POSIX)g(Thr)m(eads)p Fv(,)199 2064 y(Addison)g(W)-6 d2523 1 a2523 1 71 2183 a([8])p Black 43 w(S)t(.)27 b(P)t Fa(R)t(A)t(S)t(A)t(D)r d2525 2 a2526 2 b(ec)o(hniques)p Fv(,)199 2275 y(McGra)o(w-Hill,)18 b(1996;)i(ISBN)e (0-079-12250-7)p Black 71 2394 a([9])p Black 43 w(S)t(.)32 d2529 1 a2529 1 Fv(:)43 b Fu(Pr)m(o)o(gr)o(am-)199 2485 y(ming)29 b(with)e(Thr)m(eads)p d2531 1 a2531 1 2577 y(72389-8)p Black 33 2696 a([10])p Black 44 w(C)t(.)t(J)t(.)40 d2533 1 a2533 1 b(W)l(ith)g(Unix)f(Thr)m(eads)p Fv(,)199 2787 y(John)20 d2535 1 a2535 1 Black 33 2907 a([11])p Black 44 w(P)l(.)29 b(B)r Fa(A)t(RT)s(O)t(N)t d2538 1 a2538 1 (I)t Fv(,)30 b(E)t(.)201 2998 y(L)t Fa(A)t(Z)t(O)r(W)t(S)t(K)t(A)r d2540 3 a2542 3 Fv(,)199 3089 y(Uni)n(v)o(ersity)27 b(of)g(W)-6 b(ashington,)29 b(1992;)j(T)-5 b(echnical)27 b(Report)f(92-)199 3181 y(08-03)p Black 33 3300 a([12])p Black 44 w(D)t(.)18 d2545 2 a2546 2 3391 y(SunSoft)19 b(Inc.,)f(1992)i(\(published)g(at)f(USENIX'92\).)p Black 33 3511 a([13])p Black 44 w(W)m(.)t(R)t(.)t(S)t d2548 1 a2548 1 (amming)g(in)f(the)h(Unix)199 3602 y(En)m(vir)m(onment)p d2550 1 a2550 1 b(0-201-)199 3693 y(56317-7)p Black 33 3813 a([14])p d2553 3 a2555 3 b(Writing)199 3904 y(P)-6 b(ortable)34 b(Unix)g(Pr)m(o)o(gr)o(ams)p Fv(,)k(O'Reilly)33 b(&)h(Associates,Inc.,)199 3995 y(1994;)20 b(ISBN)e(0-937175-73-0)p Black 33 4115 a([15])p Black d2558 3 a2560 3 4206 y(questions)187 b(for)e(comp.os.r)m(esear)m(c)o(h)p Fv(,)229 b(1995;)199 4297 y(http://www)-5 b(.serpentine.com/)p Fk(\230)p Fv(bos/os-f)o(aq/,)199 4389 y d2562 1 a2562 1 33 4508 a([16])p Black 44 w(S)t Fa(U)t(N)24 b Fv(M)t d2565 2 a2566 2 4599 y(Questions)p Fv(,)111 b(1995,)f(http://www.sun.com/w)o(orkshop/-) 199 4691 y(threads/f)o(aq.html)p Black 2058 83 a([17])p d2571 1 a2571 1 Fk(\230)p Fv(bos/threads-f)o(aq/.)p Black 2058 390 a([18])p d2574 3 a2576 3 482 y(comp.pr)m(o)o(gr)o(amming)o(.thr)m(eads)p Fv(,)213 b(1999;)250 b(http://-)2224 573 y(www)-5 b(.lambdacs.com/ne)n (wsgroup/F)f(A)l(Q.html)p Black 2058 697 a([19])p Black d2579 3 a2581 3 2224 789 y(tions)k(and)h(Guidelines)p Fv(;)81 b(1998;)g(http://www)-5 b(.numeric-)2224 880 y(quest.com/lang/multi-frame.html)p Black 2058 1005 a([20])p Black 44 w(T)t Fa(H)t(E)42 b d2583 1 a2583 1 b Fu(The)39 b(Single)h(Unix)f(Speci\002cation,)2224 1096 d2585 1 a2585 1 b(1997;)f(http://www.opengroup-)2224 1187 y(.or)o d2587 1 a2587 1 1312 a([21])p Black 44 w(S)t Fa(U)t(N)41 b Fv(M)t Fa(I)t(C)t(R)r(O)t(S) d2589 1 a2589 1 Fu(SMI)39 b(Thr)m(ead)g(Resour)m(ces)p Fv(;)2224 1403 d2591 1 a2591 1 2058 1528 a([22])p Black 44 w(F)t Fa(R)t(A)t(N)t(K)29 d2593 1 a2593 1 b(pthr)m(eads)p Fv(;)32 b(1997;)g(http://www-)2224 1619 d2595 1 a2595 1 2058 1743 a([23])p Black 44 w(C)t Fa(H)t(R)t(I)t(S)85 d2597 1 a2597 1 b Fu(MIT)82 b(pthr)m(eads)p Fv(;)115 b(1993;)2224 1835 d2599 2 a2600 2 1926 y(\(old\),)209 b(http://www.humanf)o(actor.com/pthreads/mit-)2224 2017 y(pthreads.html)20 b(\(updated\))p Black 2058 2142 d2603 2 a2604 2 Fv(\(PTL\);)d(1999;)2224 2233 y(http://www.media.osaka-cu.ac.jp/)p Fk(\230)p Fv(k-abe/PTL/)p Black 2058 2358 a([25])p Black d2607 3 a2609 3 Fv(\(Pth\);)2224 2449 y(1999;)55 b(http://www.gnu.or)o(g/softw)o (are/pth/,)47 b(ftp://ftp.gnu-)2224 2540 y(.or)o(g/gnu/pth/)p Black 2058 2665 a([26])p Black 44 w(M)t Fa(I)t(C)t(H)t(A)t(E)t(L)36 d2611 1 a2611 1 b Fu(POSIX)34 b(and)h(DCE)f(Thr)m(eads)2224 2756 y(F)-8 d2613 2 a2614 2 2224 2848 y(.net/)p Fk(\230)p Fv(mtp/PCthreads.html)p Black 2058 2972 a([27])p Black 44 w(R)q Fa(O)t(B)t(E)t(RT)27 d2616 2 a2617 2 b(Simple)g(Thr)m(eads)i Fv(\(rsthreads\);)2224 3064 y(1996;)20 b(ftp://ftp.ai.mit.edu/pub/rst/)p Black 2058 3188 a([28])p d2620 3 a2622 3 b(1998;)2224 3279 y(ftp://ftp.freebsd.or)o(g/pub/FreeBSD/FreeBSD-)2224 3371 y(current/src/lib/libc)p 2780 3371 23 4 v 27 w(r/uthread/)p Black 2058 3495 a([29])p Black 44 w(X)t Fa(A)l(V)t(I)t(E)t(R)46 d2624 2 a2625 2 (libr)o(ary)p Fv(;)57 b(1999;)2224 3587 y(http://pauillac.inria.fr/)p Fk(\230)p Fv(xlero)o(y/linuxthreads/)p Black 2058 3711 d2628 6 a2633 6 2224 3802 y(Writing)56 b(and)g(Deb)o(ug)o(ging)h(Pr)m(o)o(gr)o(ams;)74 b(Under)o(standing)2224 3894 y(Thr)m(eads)p Fv(;)41 b(1998;)h (http://www.rs6000.ibm.com/doc)p 3714 3894 V 28 w(link/-)2224 3985 y(en)p 2298 3985 V 28 w(US/a)p 2476 3985 V 26 w(doc)p 2609 3985 V 28 w(lib/aixpr)o(ggd/genprogc/understanding-)p 2224 4076 V 2247 4076 a(threads.htm)p Black 2058 4201 d2635 1 a2635 1 b(Runtime)f Fv(\(NSPR\);)f(http://-)2224 4292 y(www)-5 d2637 2 a2638 2 4384 y(lxr)l(.mozilla.or)o(g/seamonk)o(e)o(y/source/nsprpub/)p Black 2058 4508 a([32])p Black 44 w(R)t Fa(A)t(L)t(F)45 d2641 2 a2642 2 4599 y(Benc)o(hmark)q Fv(;)73 b(2000;)f(http://www)-5 b(.gnu.or)o(g/softw)o(are/pth-)2224 4691 y(/smcb)m(.tar)l(.gz)p @ 1.5 log @*** empty log message *** @ text @d3 2 a4 2 %%Title: article.dvi %%Pages: 9 d7 2 a8 2 %%DocumentFonts: Helvetica-Bold Helvetica Times-Roman Times-Italic %%+ Times-Bold Courier Courier-Bold Times-BoldItalic d11 1 a11 1 %DVIPSCommandLine: dvips article.dvi d13 1 a13 1 %DVIPSSource: TeX output 1999.10.19:1520 d316 42 a357 31 TeXDict begin @@defspecial userdict begin /draftcopy-CurrentPage 0 def /draftcopy-old-BOPhook { } def userdict /bop-hook\ known{ /draftcopy-old-BOPhook /bop-hookload def } if /bop-hook{ draftcopy-old-BOPhook /draftcopy-CurrentPage draftcopy-CurrentPage 1 add def draftcopy-CurrentPage 0 ge draftcopy-CurrentPage 99999 le and { gsave 200 70 translate 65 rotate /Times-Roman findfont 215 scalefont setfont 0 0 moveto 0.90 setgray (DRAFT) show grestore } if } def end @@fedspecial end TeXDict begin 40258431 52099146 1000 600 600 (article.dvi) @@start /Fa 165[37 43 1[56 43 43 37 33 40 43 33 43 43 53 37 43 23 20 43 43 33 37 1[40 40 43 65[{TeXBase1Encoding ReEncodeFont}24 59.7758 /Times-Roman rf /Fb 140[37 25 12[42 101[{ TeXBase1Encoding ReEncodeFont}3 74.7198 /Helvetica rf /Fc 139[18 11[33 18[48 4[41 1[44 6[48 71[{TeXBase1Encoding ReEncodeFont} 6 66.4176 /Times-Italic rf /Fd 144[40 1[40 40 8[40 1[40 44[40 5[40 46[{TeXBase1Encoding ReEncodeFont}7 66.4176 /Courier rf /Fe 134[33 4[21 29 29 1[37 37 42 58 21 2[21 42 37 1[33 37 33 37 37 25 11[46 42 24[25 37 37 37 37 37 37 37 37 37 37 1[19 1[19 4[25 36[42 2[{TeXBase1Encoding ReEncodeFont} 35 74.7198 /Times-BoldItalic rf /Ff 133[45 45 1[45 45 45 45 45 45 1[45 45 45 1[45 2[45 45 1[45 45 45 45 1[45 1[45 59[45 35[{TeXBase1Encoding ReEncodeFont}21 74.7198 /Courier-Bold rf %DVIPSBitmapFont: Fg cmsy9 9 2 /Fg 2 104 df102 D<127CEAFFC0EA07F0EA01F8EA007C7F133F6D7EB3A6 6D7EA26D7E6D7E6D7E9038007FC0EC1FE0EC7FC0903801F800495A495A495AA2495AB3A6 49C7FC133E5B485AEA07F0EAFFC0007CC8FC1B4B7BB726>I E d359 28 a386 11 /Fh 103[45 29[45 45 45 45 45 45 45 45 45 1[45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 1[45 9[45 45 45 45 2[45 45 45 45 45 1[45 1[45 45 45 45 45 45 45 2[45 45 1[45 9[45 45 45 45 45 45 1[45 45 45 1[45 4[45 33[{ TeXBase1Encoding ReEncodeFont}58 74.7198 /Courier rf /Fi 135[37 1[37 42 25 1[33 2[37 42 62 3[21 2[25 33 42 33 99[{TeXBase1Encoding ReEncodeFont}13 74.7198 /Times-Bold rf /Fj 135[50 50 50 50 50 50 50 1[50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 7[50 2[50 50 50 50 50 2[50 50 1[50 2[50 1[50 50 50 3[50 15[50 2[50 50 50 1[50 50 50 4[50 35[{TeXBase1Encoding ReEncodeFont}45 83.022 /Courier d388 13 a400 6 %DVIPSBitmapFont: Fk cmsy5 5 1 /Fk 1 14 df13 D E d402 49 a450 70 /Fl 134[21 2[21 1[12 21 14 1[23 23 23 1[9 21 1[9 23 23 12 23 23 21 1[23 11[30 25 28 30 11[25 28 1[30 1[28 7[23 7[23 2[12 14 45[{TeXBase1Encoding ReEncodeFont}30 41.511 /Helvetica rf /Fm 138[25 14 1[16 2[25 25 1[12 2[12 25 25 1[23 25 1[25 23 16[28 2[35 77[{TeXBase1Encoding ReEncodeFont}15 41.511 /Helvetica-Bold rf /Fn 133[37 1[42 1[42 1[28 32 3[42 46 69 3[23 3[37 46 37 20[60 78 18[28 8[42 5[47 1[28 28 40[{TeXBase1Encoding ReEncodeFont}19 83.022 /Times-Bold rf /Fo 134[33 1[48 1[33 18 26 22 1[33 33 33 52 18 33 1[18 33 33 22 29 33 29 33 29 8[48 63 48 48 41 37 44 1[37 48 48 59 41 2[22 48 48 37 41 48 44 44 48 6[18 33 2[33 33 33 33 1[33 33 18 17 22 42[37 2[{TeXBase1Encoding ReEncodeFont}54 66.4176 /Times-Roman rf /Fp 201[29 29 29 29 29 50[{ TeXBase1Encoding ReEncodeFont}5 58.1154 /Times-Roman rf /Fq 134[42 42 60 42 46 23 42 28 1[46 46 46 69 18 42 18 18 46 46 23 46 46 42 46 46 9[78 1[60 51 55 60 1[55 65 1[69 1[55 1[23 60 65 51 55 60 60 55 55 13[46 46 3[23 28 3[28 28 36[42 42 2[{TeXBase1Encoding ReEncodeFont}50 83.022 /Helvetica rf /Fr 133[32 37 37 2[42 23 32 32 1[42 42 42 60 23 37 1[23 42 42 23 37 42 37 42 42 8[51 2[60 46 42 51 1[51 1[55 69 46 2[28 1[60 51 1[60 1[51 51 10[42 1[42 4[23 1[28 45[{TeXBase1Encoding ReEncodeFont}41 83.022 /Times-Italic rf /Fs 104[83 42 1[37 37 24[37 42 42 60 42 42 23 32 28 42 42 42 42 65 23 42 23 23 42 42 28 37 42 37 42 37 3[28 1[28 2[60 78 60 60 51 46 55 1[46 60 60 74 51 60 1[28 60 1[46 51 60 55 55 60 1[37 4[23 42 42 42 42 42 42 42 42 42 42 23 21 28 21 2[28 28 28 5[28 29[46 46 2[{TeXBase1Encoding ReEncodeFont}75 83.022 /Times-Roman rf /Ft 134[60 60 86 60 66 33 60 40 2[66 66 100 27 60 1[27 66 66 33 66 66 60 66 66 12[73 80 86 1[80 93 1[100 3[33 3[80 86 86 1[80 10[66 66 66 66 66 66 2[33 46[{ TeXBase1Encoding ReEncodeFont}40 119.552 /Helvetica rf /Fu 134[80 3[88 48 80 56 1[88 88 88 128 40 2[40 2[48 80 88 80 88 80 14[104 1[96 6[40 7[104 13[80 80 80 49[{ TeXBase1Encoding ReEncodeFont}24 143.462 /Helvetica-Bold rf /Fv 201[25 25 25 25 25 25 49[{TeXBase1Encoding ReEncodeFont}6 49.8132 /Times-Roman rf /Fw 134[33 33 50 33 37 21 29 29 37 37 37 37 54 21 33 1[21 37 37 21 33 37 33 37 37 8[46 62 46 54 42 37 46 54 46 54 50 62 42 2[25 1[54 46 46 54 50 46 46 5[25 25 5[37 37 37 3[19 25 19 4[25 36[37 2[{TeXBase1Encoding ReEncodeFont}55 74.7198 /Times-Italic rf /Fx 134[42 1[58 2[25 42 29 2[46 9[42 46 42 46 42 21[54 9[54 6[25 58[{TeXBase1Encoding ReEncodeFont}14 74.7198 /Helvetica-Bold rf %DVIPSBitmapFont: Fy cmsy8 8 1 /Fy 1 14 df13 D E %EndDVIPSBitmapFont /Fz 134[33 2[33 37 18 33 22 1[37 37 37 55 15 2[15 37 37 18 37 37 33 1[37 10[44 2[44 48 12[44 48 48 1[44 7[37 1[37 5[37 37 1[18 1[18 44[{TeXBase1Encoding ReEncodeFont}32 66.4176 /Helvetica rf /FA 134[37 37 54 37 37 21 29 25 37 37 37 37 58 21 37 21 21 37 37 25 33 37 33 37 33 3[25 1[25 2[54 71 1[54 46 42 50 54 42 54 54 66 46 54 29 25 54 54 42 46 54 50 50 54 5[21 21 37 37 37 37 37 37 37 37 37 37 21 19 25 19 2[25 25 25 58 35[42 2[{ TeXBase1Encoding ReEncodeFont}71 74.7198 /Times-Roman rf /FB 139[28 39 33 2[50 50 78 28 3[50 50 33 44 1[44 50 44 13[55 66 2[72 9[61 4[92 6[50 7[50 2[25 46[{ TeXBase1Encoding ReEncodeFont}22 99.6264 /Times-Roman rf /FC 139[28 50 33 1[55 55 55 1[22 50 1[22 55 55 1[55 55 50 1[55 11[72 61 66 12[61 2[72 21[33 45[{ TeXBase1Encoding ReEncodeFont}21 99.6264 /Helvetica rf /FD 138[126 69 1[80 2[126 126 1[57 2[57 126 126 1[115 126 1[126 115 16[138 2[172 77[{TeXBase1Encoding ReEncodeFont}15 206.559 /Helvetica-Bold rf end d455 3 d461 109 a569 81 1 0 bop Black Black Black Black 800 312 a FD(P)-8 b(or)t(tab)n(le)57 b(Multithreading)721 494 y FC(The)29 b(Signal)g(Stac)n(k)f(T)-12 b(r)q(ic)n(k)28 b(F)m(or)g(User-Space)i(Thread)f(Creation)1567 735 y FB(Ralf)c(S.)g(Engelschall)1524 851 y(rse@@engelschall.com)1273 968 y FA(W)-6 b(eblinger)19 b(W)-6 b(e)o(g)18 b(28,)i(85221)g(Dachau,)g (German)o(y)-5 b(.)1677 1162 y FB(October)25 b(1999)1157 1278 y Fz(Cop)n(yr)q(ight)1478 1276 y(c)1459 1278 y Fy(\015)18 b Fz(Ralf)g(S)o(.)h(Engelschall,)g(All)f(Rights)h(Reser)r(v)n(ed.)1578 1371 y(Document)g(V)-5 b(ersion)18 b(0.9.17)p Black Black 1798 1669 a Fx(Abstract)p Black Black 208 1806 a FA(This)e(paper)h (describes)g(a)f(pragmatic)h(b)o(ut)f(mostly)h(portable)g(f)o(allback)g (approach)h(for)e(creating)h(and)g(dispatching)g(between)g(multiple)208 1898 y(threads)25 b(of)f(e)o(x)o(ecution)i(on)f(Unix)g(systems)g(which) g(lack)f(a)h(dedicated)h(user)o(-space)f(conte)o(xt)g(switching)g(f)o (acility)-5 b(.)40 b(The)24 b(described)208 1989 y(approach)d(closely)e (follo)n(w)h(the)f(techniques)i(implemented)f(in)f(the)h(author')l(s)g (user)o(-space)g(multithreading)g(library)f Fw(GNU)g(P)-6 b(ortable)208 2080 y(Thr)m(eads)27 b FA(\(Pth\))626 2049 y Fv(1)680 2080 y FA(and)g(usually)f(can)h(be)f(easily)g(adapted)i(by)e (other)h(user)o(-space)f(implementations)h(as)f(their)g(f)o(allback)h (approach.)208 2172 y(It)g(is)h(entirely)h(based)g(on)g(Unix)g(system)f (f)o(acilities)g(and)h(ANSI)f(C)g(language)h(features)g(and)g (especially)g(does)g(not)g(require)g(an)o(y)208 2263 y(assembly)19 b(code)f(or)h(platform)f(speci\002c)g(tricks)g(at)f(all.) 23 b(The)18 b(most)g(interesting)g(issue)h(is)e(the)h(technique)i(of)e (creating)g(threads,)h(which)208 2354 y(is)f(e)o(xplained)i(in)f (detail.)208 2496 y Fx(K)o(e)o(yw)o(or)o(ds:)39 b FA(portability)-5 b(,)31 b(multithreading,)h(Unix,)f(POSIX,)c(ANSI)h(C,)g(user)o(-space,) j(conte)o(xt)f(creation,)h(conte)o(xt)e(switching,)208 2587 y(signal)19 b(handler)m(,)g(stack,)g(mak)o(econte)o(xt,)h (switchconte)o(xt,)g(sigaltstack,)e(setjmp,)h(longjmp.)0 2900 y Fu(1)143 b(Intr)m(oduction)0 3088 y Ft(1.1)121 b(Multithreading)0 3253 y Fs(The)29 b(paradigm)f(of)h(programming)e (with)i(multiple)g(threads)g(of)0 3352 y(e)o(x)o(ecution)c(\(aka)h Fr(multithr)m(eading)p Fs(\))e(is)k(already)d(a)j(v)o(ery)d(old)i(one)0 3452 y(and)h(dates)g(back)f(to)h(the)g(decades)g(of)g(programming)c (with)k Fr(co-)0 3551 y(r)l(outines)c Fs([1)o(,)h(2)o(].)37 b(P)o(aradoxically)22 b(the)i(use)g(of)g(threads)g(on)f(Unix)0 3651 y(platforms)15 b(became)h(popular)f(the)h(\002rst)h(time)g(in)g (the)f(early)g(1990s.)0 3874 y Fq(Multithreading)25 b(Adv)n(antages)0 4009 y Fs(Multithreading)14 b(can)j(pro)o(vide)d(man)o(y)i(bene\002ts)g (for)g(applications)0 4108 y(\(good)30 b(runtime)h(concurrenc)o(y)-5 b(,)31 b(parallel)g(programming)e(tech-)0 4208 y(niques)19 b(can)g(be)g(easier)h(implemented,)d(the)j(popular)d(procedural)0 4308 y(programming)12 b(style)17 b(can)e(be)h(combined)e(with)i (multiple)f(threads)0 4407 y(of)24 b(e)o(x)o(ecution,)f(etc.\))38 b(b)n(ut)24 b(the)g(most)h(interesting)e(ones)h(are)h(usu-)0 4507 y(ally)33 b(performance)c(gains)j(and)g(reduced)f(resource)g (consump-)0 4606 y(tion.)67 b(Because)35 b(in)g(contrast)f(to)g (multiprocess)f(applications,)0 4706 y(multithreaded)22 b(ones)j(usually)f(require)f(less)j(system)f(resources)0 4806 y(\(mainly)i(memory\))g(and)h(their)g(internal)g(communication)d (part)1992 3084 y(can)20 b(le)n(v)o(erage)e(from)h(the)h(shared)g (address)g(space.)1992 3285 y Fq(Multithreading)25 b(and)f(Application) 1992 3411 y Fs(Ne)n(v)o(ertheless)j(there)h(still)i(e)o(xist)e(just)h (a)g(fe)n(w)f(real)h(applications)1992 3510 y(in)g(the)g(free)f(softw)o (are)h(w)o(orld)f(which)h(use)g(multithreading)d(for)1992 3610 y(their)18 b(bene\002t,)g(although)f(their)i(application)e(areas)i (are)g(predesti-)1992 3709 y(nated)c(for)h(multithreading.)22 b(F)o(or)16 b(instance)g(the)h(popular)d(Apache)1992 3809 y(webserv)o(er)2354 3779 y Fp(2)2410 3809 y Fs(as)24 b(of)e(v)o(ersion)f(1.3)g(still)j(uses)f(a)g(pre-forking)c(pro-)1992 3909 y(cess)28 b(model)e(for)h(serving)f(HTTP)h(requests,)i(although)c (e)o(xperi-)1992 4008 y(ments)15 b(with)g(multithreaded)f(Apache)g(v)n (ariants)h(already)f(in)i(1996)1992 4108 y(sho)n(wed)29 b(great)g(performance)e(boosts.)55 b(The)29 b(same)i(applies)e(to)1992 4208 y(other)19 b(applications.)2116 4308 y(The)f(reason)f(mainly)g (is,)i(that)f(for)g(a)g(long)f(time)i(multithread-)1992 4407 y(ing)i(f)o(acilities)h(under)d(Unix)i(were)h(rare.)28 b(The)21 b(situation)f(became)1992 4507 y(better)g(after)h(some)f(v)o (endors)g(lik)o(e)h(Sun)f(and)h(DEC)g(incorporated)1992 4606 y(threading)37 b(f)o(acilities)j(into)f(their)h(Unix)f(\003a)n(v)n (ors)g(and)g(POSIX)1992 4706 y(standardized)34 b(a)j(threading)d(API)j (\(kno)n(wn)d(as)j(Pthreads\).)3846 4676 y Fp(3)3879 4706 y Fs(.)1992 4806 y(But)g(an)g(API)h(and)e(a)i(fe)n(w)f(v)o(endor)e (implementation)g(are)i(not)p Black 0 4859 764 4 v 90 4914 a Fv(1)120 4938 y Fo(see)17 b(http://www)l(.gnu.or)o(g/softw)o (are/pth/)90 4995 y Fv(2)120 5019 y Fo(see)g(http://www)l(.apache.or)o (g/)90 5077 y Fv(3)120 5100 y Fo(IEEE)e(POSIX)h(1003.1c-1995)j(aka)f (ISO/IEC)f(9945-1:1996)p Black Black 1929 5400 a Fs(1)p Black eop d571 133 a703 131 2 1 bop Black Black 0 -37 a Fs(enough)20 b(to)j(ful\002ll)f(the)h (portability)e(requirements)f(of)i(free)g(soft-)0 63 y(w)o(are)e(packages.)j(Here)d(portable)e(multithreading)g(libraries)h (are)0 162 y(needed)g(\002rst.)125 264 y(The)52 b(author)g(currently)g (kno)n(ws)g(about)g(o)o(v)o(er)g(fourteen)0 364 y(\(mostly)25 b(user)n(-space\))h(freely)f(a)n(v)n(ailable)h(multithreading)d(solu-)0 463 y(tions)k(for)f(Unix)g(systems)796 433 y Fp(4)829 463 y Fs(,)j(b)n(ut)d(only)g(a)h(fe)n(w)f(of)h(them)f(are)g(re-)0 563 y(ally)i(portable.)48 b(And)28 b(e)n(v)o(en)f(the)h(mostly)g (portable)f(ones)h(suf)n(fer)0 663 y(from)h(the)h(f)o(act)g(that)g(the) o(y)f(partly)g(depend)f(on)h(assembly)h(code)0 762 y(or)c(platform)f (speci\002c)i(tricks)f(usually)g(related)g(to)h(the)f(creation)0 862 y(and)18 b(dispatching)e(of)i(the)g(indi)n(vidual)e(threads.)24 b(This)18 b(means)g(that)0 961 y(the)24 b(number)f(of)h(platforms)e (the)o(y)i(are)g(w)o(orking)f(on)h(are)g(usually)0 1061 y(a)h(\002x)o(ed)f(set)h(and)f(applications)f(which)h(are)g(based)g(on) g(them)g(are)0 1161 y(only)c(portable)f(to)h(those)h(platforms.)j(This) d(situation)f(is)h(not)g(sat-)0 1260 y(isf)o(actory)-5 b(,)23 b(so)h(application)f(authors)f(still)j(a)n(v)n(oid)f(the)g(use)g (multi-)0 1360 y(threading)j(when)g(the)o(y)h(w)o(ant)h(to)g(\(or)e(ha) n(v)o(e)h(to\))h(achie)n(v)o(e)e(maxi-)0 1460 y(mum)19 b(portability)g(for)h(their)g(application.)125 1561 y(So)f(a)g (pragmatic)e(b)n(ut)i(mostly)f(portable)g(f)o(allback)g(technique)0 1661 y(for)i(implementing)e(user)n(-space)i(threads)g(w)o(ould)f(be)i (useful,)e(be-)0 1761 y(cause)29 b(this)h(w)o(ay)g(the)f(possibilities) h(to)f(use)h(multithreading)d(in)0 1860 y(free)20 b(softw)o(are)g (applications)f(could)g(be)h(greatly)f(impro)o(v)o(ed.)0 2070 y Fq(Ing)o(redients)24 b(Of)f(A)g(Thread)0 2199 y Fs(A)29 b(Unix)g(process)f(has)h(man)o(y)f(ingredients,)h(b)n(ut)g (the)g(most)g(im-)0 2298 y(portant)17 b(ones)h(are)f(its)i(memory)e (mapping)f(table,)i(the)g(signal)g(dis-)0 2398 y(patch)h(table,)g(the)g (signal)g(mask,)g(the)g(set)h(of)f(\002le)h(descriptors)e(and)0 2498 y(the)j(machine)f(conte)o(xt,)f(which)i(in)g(turn)f(consists)h(of) g(at)g(least)h(the)0 2597 y(CPU)17 b(re)o(gisters)e(including)f(the)h (program)f(counter)g(and)h(the)h(stack)0 2697 y(pointer)-5 b(.)26 b(On)20 b(the)h(other)f(hand,)g(there)g(can)g(be)h(light-weight) e(pro-)0 2796 y(cesses)24 b(\(L)-6 b(WP\))23 b(or)g(threads)g(which)f (share)h(all)h(ingredients)e(with)0 2896 y(the)f(underlying)d(\(hea)n (vy-weight\))g(process)i(e)o(xcept)g(for)h(the)f(ma-)0 2996 y(chine)g(conte)o(xt.)0 3205 y Fq(K)m(er)r(nel-Space)k(vs)o(.)j (User-Space)0 3334 y Fs(Those)k(L)-6 b(WPs)32 b(or)f(threads,)i(on)e(a) g(Unix)g(platform)f(classically)0 3434 y(can)k(be)g(implemented)e (either)h(inside)h(k)o(ernel-space)e(or)i(user)n(-)0 3533 y(space.)53 b(When)30 b(implemented)e(in)h(k)o(ernel-space,)i(one) e(usually)0 3633 y(calls)22 b(them)e(L)-6 b(WPs,)22 b(else)f(\(user)n (-space\))f(threads.)26 b(If)21 b(threads)f(are)0 3733 y(implemented)29 b(by)i(the)g(k)o(ernel,)i(the)e(thread)g(conte)o(xt)f (switches)0 3832 y(are)17 b(performed)e(by)i(the)h(k)o(ernel)e(without) h(notice)g(by)g(the)g(applica-)0 3932 y(tion,)h(similar)g(to)h(the)f (dispatching)e(of)i(processes.)24 b(If)18 b(threads)f(are)0 4032 y(implemented)31 b(in)i(user)n(-space,)i(the)d(thread)g(conte)o (xt)g(switches)0 4131 y(are)e(performed)e(usually)i(by)g(an)g (application)f(library)g(without)0 4231 y(notice)38 b(by)g(the)h(k)o (ernel.)79 b(Additionally)-5 b(,)41 b(there)d(e)o(xist)h(hybrid)0 4330 y(threading)14 b(approaches,)h(where)g(typically)g(a)i(user)n (-space)e(library)0 4430 y(binds)38 b(one)h(or)g(more)f(user)n(-space)g (threads)h(to)g(one)f(or)h(more)0 4530 y(k)o(ernel-space)18 b(L)-6 b(WPs.)0 4739 y Fq(Thread)23 b(Models)0 4868 y Fs(F)o(or)41 b(instance)g(the)g(v)o(endor)e(threading)h(f)o(acilities)i (under)e Fr(So-)0 4968 y(laris)p Fs(,)25 b Fr(AIX)p Fs(,)e Fr(T)-5 b(ru64)23 b Fs(and)g Fr(IRIX)j Fs(use)e(a)f Fn(M:N)h Fs(mapping)e([16)n(,)i(25)o(],)1992 -37 y(i.e.,)42 b Fn(M)d Fs(user)n(-space)e(threads)g(are)h(mapped)f(onto)g Fn(N)i Fs(k)o(ernel-)1992 63 y(space)29 b(L)-6 b(WPs.)52 b(On)29 b(the)g(other)g(hand)f Fr(LinuxThr)m(eads)f Fs([24)o(])i(un-) 1992 162 y(der)d Fr(GNU/Linux)h Fs(uses)g(a)g Fn(1:1)f Fs(mapping)f(and)h(pure)f(user)n(-space)1992 262 y(implementations)35 b(lik)o(e)i Fr(GNU)h(Pth)p Fs(,)j Fr(FSU)c(pthr)m(eads)f Fs(or)h Fr(MIT)1992 362 y(pthr)m(eads)p Fs(,)19 b(etc.)25 b(use)20 b(a)h Fn(M:1)f Fs(mapping)e([20)o(,)j(17)o(,)f(18)o(].)2116 467 y(From)38 b(no)n(w)g(on)g(we)h(talk)g(about)e(such)i Fn(M:1)f Fs(user)h(space)1992 567 y(threading)i(approaches,)48 b(where)43 b(one)g(or)g(more)g(user)h(space)1992 666 y(threads)20 b(are)g(implemented)f(inside)i(a)g(single)g(k)o(ernel)f (space)h(pro-)1992 766 y(cess.)k(The)19 b(e)o(x)o(ercise)f(is)i(to)g (implement)e(this)i(by)e(using)h(standard-)1992 865 y(ized)h(Unix)f (system)i(and)e(ANSI)i(C)g(language)d(f)o(acilities)j Fr(only)p Fs(.)1992 1168 y Ft(1.2)120 b(The)34 b(Ex)l(ercise)1992 1334 y Fs(As)20 b(we)h(ha)n(v)o(e)f(mentioned,)e(a)i(thread)f(shares)i (all)f(things)g(with)h(the)1992 1434 y(underlying)14 b(process)j(e)o(xcept)g(for)g(the)h(machine)e(conte)o(xt.)23 b(So)18 b(the)1992 1533 y(major)25 b(task)j(for)e(a)h(user)n(-space)f (threading)f(library)g(is)j(to)f(create)1992 1633 y(and)19 b(dispatch)h(those)g(machine)f(conte)o(xts)g(for)g(the)i(application.) 2116 1738 y(In)31 b(practice,)h(the)f(second)f(major)g(task)h(it)g(has) g(to)g(do)g(is)g(to)1992 1838 y(mak)o(e)g(sure)h(that)h(no)f(thread)f (by)h(accident)f(blocks)h(the)g(whole)1992 1938 y(process)24 b(\(and)h(thereby)f(all)i(other)f(threads\).)39 b(Instead)25 b(when)g(an)1992 2037 y(operation)i(w)o(ould)i(block,)h(the)g(task)g (of)f(the)g(threading)f(library)1992 2137 y(is)22 b(to)f(suspend)g (only)f(the)h(e)o(x)o(ecution)e(of)j(the)f(current)f(thread)g(and)1992 2237 y(in)j(the)g(meantime)f(dispatch)g(the)h(remaining)f(threads.)32 b(But)24 b(this)1992 2336 y(task)g(is)g(outside)g(the)f(scope)h(of)f (this)i(paper)-5 b(.)35 b(W)-7 b(e)25 b(focus)e(only)g(on)1992 2436 y(the)d(aspect)g(of)g(machine)f(conte)o(xt)g(handling.)1992 2738 y Ft(1.3)120 b(The)34 b(Curse)h(of)e(P)-6 b(or)5 b(tability)1992 2905 y Fs(Our)22 b(goal)h(of)f(real)h(portability)f (for)g(a)h(threading)e(library)h(causes)1992 3004 y(some)h(non-tri)n (vial)g(problems)f(which)i(ha)n(v)o(e)g(to)g(be)g(solv)o(ed.)36 b(The)1992 3104 y(most)29 b(ob)o(vious)e(one)h(is)i(that)f(dealing)f (with)i(machine)e(conte)o(xts)1992 3204 y(usually)34 b(suf)n(fers)h(greatly)f(from)g(portability)-5 b(,)37 b(because)e(it)h(is)g(a)1992 3303 y(highly)16 b(CPU)j(dependent)d (task,)j(although)d(in)i(principle)e(it)j(is)g(just)1992 3403 y(a)d(matter)g(of)g(switching)g(a)h(fe)n(w)f(CPU)h(re)o(gisters)f (\(mainly)f(the)i(pro-)1992 3502 y(gram)i(counter)g(and)g(the)h(stack)h (pointer\).)1992 3730 y Fq(Assemb)n(ly)i(Code)g(Considered)h(Har)r (mful)1992 3866 y Fs(Additionally)j(we)i(disallo)n(w)g(the)h(use)f(of)g (an)o(y)f(assembly)h(solu-)1992 3966 y(tions)23 b(or)g(platform)e (speci\002c)j(tricks,)f(because)g(then)g(the)g(thread-)1992 4066 y(ing)f(library)f(again)g(w)o(ould)h(be)g(only)g(semi-portable,)f (i.e.,)i(it)g(can)1992 4165 y(be)d(ported)f(to)i Fn(N)g Fs(platforms)f(b)n(ut)g(on)h(the)f Fn(\(N+1\))p Fs(th)g(platform)f(one) 1992 4265 y(has)c(to)h(manually)f(adjust)g(or)h(e)n(v)o(en)f(e)o(xtend) f(the)i(threading)e(library)1992 4364 y(to)20 b(w)o(ork)f(there,)h (too.)2116 4470 y(That)g(is)g(not)g(acceptable)f(for)g(us,)h(e)n(v)o (en)f(when)g(it)i(also)f(mak)o(es)1992 4569 y(solving)41 b(the)i(problems)e(harder)m(,)46 b(of)c(course.)91 b(At)43 b(least)g(all)1992 4669 y(other)26 b(kno)n(wn)f(free)i(softw)o(are)f (user)n(-space)h(threading)e(libraries)1992 4769 y([17)n(,)g(18)o(,)g (19)o(])g(do)g(not)f(restrict)h(themself)f(to)h(this)g(and)f(therefore) 1992 4868 y(are)29 b(just)g(semi-portable.)50 b(But)29 b(real)g(portability)f(is)i(our)e(major)1992 4968 y(goal.)p Black 0 5021 764 4 v 90 5077 a Fv(4)120 5100 y Fo(see)17 b(the)h(GNU)e(Pth)h(webpage)i(for)e(references)j(to)d(them)p Black Black 0 5400 a Fm(P)n(or)q(table)11 b(Multithreading)0 5458 y Fl(The)h(Signal)g(Stac)o(k)e(T)-5 b(r)q(ic)o(k)12 b(F)o(or)f(User-Space)g(Thread)h(Creation)1929 5400 y Fs(2)1221 b Fl(Cop)o(yr)q(ight)3398 5399 y(c)3381 5400 y Fk(\015)11 b Fl(1999)g(Ralf)h(S)o(.)f(Engelschall)3524 5458 y(All)g(Rights)h(Reser)q(v)o(ed.)p Black eop d705 607 a1311 59 3 2 bop Black Black 0 -15 a Fu(2)143 b(Pr)m(ob)o(lem)38 b(Anal)n(ysis)0 220 y Ft(2.1)121 b(The)34 b(T)-14 b(ask)33 b(in)g(Detail)0 376 y Fs(Our)26 b(task)g(sounds)g(simple:)37 b(Create)27 b(and)e(initialize)i(a)f(machine)0 475 y(conte)o(xt)d(\()p Fq(mctx)p Fs(\))h(for)g(a)i(thread,)e(which)h(when)f(switched)g(to)h (the)0 575 y(\002rst)h(time,)h(lets)g(the)e(thread)g(start)h(e)o(x)o (ecuting)d(at)j(a)g(startup)f(rou-)0 675 y(tine)h(\()p Fq(func)p Fs(\))f(with)h(a)g(startup)f(ar)o(gument)f(\()p Fq(arg)p Fs(\))h(on)g(a)h(particular)0 774 y(run-time)17 b(stack)h(\()p Fq(stac)n(k)p Fs(\).)23 b(When)18 b(the)g(thread)f(then) h(again)f(yields)0 874 y(e)o(x)o(ecution)30 b(its)j(indi)n(vidual)e (thread)g(ingredients)g(are)h(stored)g(in)0 973 y Fq(mctx)23 b Fs(and)g(on)g(the)g(ne)o(xt)f(dispatching)g(the)o(y)h(are)g(again)f (restored)0 1073 y(from)d(there.)0 1253 y Fq(The)k(Proposed)h(API)0 1373 y Fs(In)f(detail)f(we)i(propose)d(the)i(follo)n(wing)e Fr(Application)g(Pr)l(o)o(gr)o(am-)0 1472 y(mer)o(s)g(Interface)f Fs(\(API\))g(for)f(the)h(machine)f(conte)o(xt)g(handling:)p Black 0.7 0.7 0.7 TeXcolorrgb 95 1630 50 50 v Black Black 208 1630 a(A)30 b(data)g(structure)f Fn(mctx)p 969 1630 25 4 v 29 w(t)h Fs(which)g(holds)f(the)h(machine)208 1730 y(conte)o(xt.)p Black 0.7 0.7 0.7 TeXcolorrgb 95 1886 50 50 v Black Black 208 1886 a(A)78 b(function)f(\223)p Fn(mctx)p 920 1886 25 4 v 29 w(t)h Fj(*mctx)p 1305 1886 V 29 w(create\()g Fn(v)o(oid)208 1986 y Fj(\(*)p Fr(func)p Fj(\)\()p Fn(v)o(oid)p Fj(\),)25 b Fn(v)o(oid)h Fj(*)p Fr(ar)m(g)p Fs(,)i Fn(v)o(oid)e Fj(*)p Fr(skaddr)p Fs(,)h Fn(size)p 1856 1986 V 30 w(t)208 2086 y Fr(sksize)p Fj(\))p Fs(\224)22 b(which)g(creates)g(a)g(machine)f(conte)o(xt)g(structure)208 2185 y(out)g(of)g(a)h(start)h(routine)d Fr(func)p Fs(,)h(a)i(start)f (ar)o(gument)d Fr(ar)m(g)j Fs(and)208 2285 y(a)27 b(stack)f(starting)h (at)g Fr(skaddr)p Fs(,)g(which)f(is)i Fr(sksize)f Fs(bytes)g(in)208 2384 y(size.)e(It)c(returns)e(the)h(created)g Fn(mctx)p 1270 2384 V 29 w(t)g Fs(data)g(structure.)p Black 0.7 0.7 0.7 TeXcolorrgb 95 2541 50 50 v Black Black 208 2541 a(A)56 b(function)e(\223)p Fn(int)j Fj(mctx)p 1053 2541 25 4 v 29 w(save\()p Fn(mctx)p 1508 2541 V 28 w(t)f Fj(*)p Fr(mctx)p Fj(\))p Fs(\224)208 2640 y(which)31 b(sa)n(v)o(es)i(the)f (current)f(machine)g(conte)o(xt)g(into)h(the)208 2740 y(machine)25 b(conte)o(xt)g(structure)g Fr(mctx)p Fs(.)43 b(It)27 b(returns)e Fj(FALSE)208 2840 y Fs(when)c(the)i(machine)e (conte)o(xt)g(w)o(as)j(sa)n(v)o(ed)e(and)g(it)h(returns)208 2939 y(the)e(\002rst)h(time,)g(and)f Fj(TRUE)g Fs(when)g(the)h(machine) e(conte)o(xt)208 3039 y(w)o(as)h(restored)e(by)h Fj(mctx)p 954 3039 V 29 w(restore)g Fs(and)g(it)h(returns)e(the)208 3139 y(second)g(time)h(\(see)h(belo)n(w\).)p Black 0.7 0.7 0.7 TeXcolorrgb 95 3295 50 50 v Black Black 208 3295 a(A)78 b(function)f(\223)p Fn(v)o(oid)h Fj(mctx)p 1174 3295 25 4 v 29 w(restore\()g Fn(mctx)p 1857 3295 V 29 w(t)208 3394 y Fj(*)p Fr(mctx)p Fj(\))p Fs(\224)26 b(which)f(restores)h(the)g (machine)e(conte)o(xt)h(from)208 3494 y(the)h(machine)g(conte)o(xt)g (structure)g Fr(mctx)p Fs(.)45 b(This)27 b(function)208 3594 y(does)22 b(not)f(return)g(at)i(all.)32 b(Instead)21 b(it)i(returns)f(at)g(the)h(loca-)208 3693 y(tion)i(of)g(the)h Fj(mctx)p 791 3693 V 29 w(save)f Fs(function)f(b)n(ut)i(with)f(a)h (return)208 3793 y(of)19 b Fj(TRUE)p Fs(.)246 3942 y 11182899 9143664 0 0 11182899 9143664 startTexFig 246 3942 a %%BeginDocument: fig1.eps d1313 3 a1315 3 %%Title: fig1.eps %%Creator: fig2dev Version 3.2 Patchlevel 1 %%CreationDate: Tue Jul 13 13:37:15 1999 d1317 1 a1317 5 %%Orientation: Portrait %%BoundingBox: 0 0 170 139 %%Pages: 0 %%BeginSetup %%EndSetup a1355 1 /col32 {0.941 0.941 0.941 srgb} bind def d1359 2 a1360 1 -197.0 156.0 translate a1395 1 %%EndProlog d1398 1 d1400 89 a1488 2 n -1000 3595 m -1000 -1000 l 7111 -1000 l 7111 3595 l cp clip 0.06000 0.06000 sc d1491 2 a1492 1 n 3450 1350 m 4050 1350 l 4050 1500 l 3450 1500 l cp gs col32 1.00 shd ef gr gs col0 s gr d1494 2 a1495 1 n 3450 1650 m 4050 1650 l 4050 1800 l 3450 1800 l cp gs col32 1.00 shd ef gr gs col0 s gr d1497 2 a1498 1 n 4800 675 m 5850 675 l 5850 900 l 4800 900 l cp gs col32 1.00 shd ef gr gs col0 s gr d1500 7 a1506 1 n 4800 1500 m 5850 1500 l 5850 1725 l 4800 1725 l cp gs col32 1.00 shd ef gr gs col0 s gr d1508 1 d1510 5 a1514 58 4678 842 m 4789 787 l 4717 887 l 4820 800 l 4781 754 l cp clip n 4053 1411 m 4789 787 l gs col0 s gr gr % arrowhead n 4678 842 m 4789 787 l 4717 887 l 4697 865 l 4678 842 l cp gs 0.00 setgray ef gr col0 s % Polyline n 3450 450 m 4050 450 l 4050 600 l 3450 600 l cp gs col0 s gr % Polyline n 3450 750 m 4050 750 l 4050 900 l 3450 900 l cp gs col0 s gr % Polyline n 4800 300 m 5850 300 l 5850 1200 l 4800 1200 l cp gs col0 s gr % Polyline n 4800 1350 m 5850 1350 l 5850 2100 l 4800 2100 l cp gs col0 s gr % Polyline n 3300 300 m 4200 300 l 4200 2100 l 3300 2100 l cp gs col0 s gr /Helvetica-Bold ff 150.00 scf sf 3577 870 m gs 1 -1 sc (errno) col0 sh gr /Helvetica-Bold ff 150.00 scf sf 3465 570 m gs 1 -1 sc (sigmask) col0 sh gr /Helvetica-Bold ff 150.00 scf sf 3585 2055 m gs 1 -1 sc (mctx) col0 sh gr /Helvetica ff 150.00 scf sf 4875 1650 m gs 1 -1 sc (func\(void *arg\)) col0 sh gr /Helvetica-Bold ff 150.00 scf sf 5175 2062 m gs 1 -1 sc (code) col0 sh gr /Helvetica-Bold ff 150.00 scf sf 5160 1162 m gs 1 -1 sc (stack) col0 sh gr /Helvetica-Bold ff 150.00 scf sf 3645 1478 m gs 1 -1 sc (SP) col0 sh gr /Helvetica-Bold ff 150.00 scf sf 3645 1778 m gs 1 -1 sc (PC) col0 sh gr /Helvetica ff 150.00 scf sf 3450 2550 m gs 1 -1 sc (mctx = mctx_create\(func, arg, stack\);) col0 sh gr % Polyline n 3450 1050 m 4050 1050 l 4050 1200 l 3450 1200 l cp gs col0 s gr /Helvetica ff 150.00 scf sf 5250 825 m gs 1 -1 sc (arg) col0 sh gr /Helvetica-Bold ff 150.00 scf sf 3652 1177 m gs 1 -1 sc (Rx) col0 sh gr % Arc gs clippath 3826 2198 m 3750 2100 l 3863 2151 l 3757 2067 l 3720 2114 l cp clip n 4537.5 1218.8 1181.8 48.2 131.8 arc gs col0 s gr gr d1517 2 a1518 1 n 3826 2198 m 3750 2100 l 3863 2151 l 3844 2174 l 3826 2198 l cp gs 0.00 setgray ef gr col0 s d1520 1 d1522 4 a1525 3 4666 1600 m 4789 1612 l 4675 1660 l 4808 1639 l 4799 1580 l cp clip n 4050 1725 m 4789 1612 l gs col0 s gr gr a1527 378 n 4666 1600 m 4789 1612 l 4675 1660 l 4670 1630 l 4666 1600 l cp gs 0.00 setgray ef gr col0 s $F2psEnd rs %%EndDocument endTexFig 1992 -37 a Fq(Conte)n(xt)j(Switching)1992 159 y Fs(There)38 b(are)h(tw)o(o)h(good)e(reasons)g(for)h(the)g(particular)f(seman-)1992 259 y(tics)18 b(of)g Fj(mctx)p 2418 259 25 4 v 29 w(save)p Fs(:)24 b(First,)19 b(the)f(underlying)d(implementation)1992 359 y(we)31 b(will)i(choose)d(later)m(,)k(will)f(support)d(e)o(xactly)h (these)g(seman-)1992 458 y(tics.)25 b(Second,)19 b(we)h(w)o(ant)g(to)g (use)g(an)g(easy)g(dispatching)e(function)1992 558 y(which)h(looks)h (lik)o(e)g(this:)p Black Black 1992 912 a Fi(v)o(oid)1992 1003 y Fh(mctx_switch\()p Fi(mctx)p 2693 1003 23 4 v 25 w(t)45 b Fh(*)p Fw(fr)m(om)p Fh(,)2529 1094 y Fi(mctx)p 2690 1094 V 28 w(t)g Fh(*)p Fw(to)p Fh(\))1992 1186 y Fg(f)2081 1277 y Fi(if)g Fh(\(mctx_save\()p Fw(fr)m(om)p Fh(\))d(==)j(FALSE\))2216 1368 y(mctx_restore\()p Fw(to)p Fh(\);)2081 1460 y Fi(r)o(etur)o(n)p Fh(;)1992 1551 y Fg(g)1992 2019 y Ft(2.2)120 b(P)-6 b(ossibilities)1992 2243 y Fs(When)28 b(we)i(pok)o(e)e(around)f(in)i(the)g(references)f(of) g(the)h(ANSI)h(C)1992 2343 y(language)g(and)h(the)h(Unix)f(standards)g (we)i(\002nd)e(the)h(follo)n(wing)1992 2443 y(functions)18 b(on)i(which)g(we)g(can)g(base)h(our)e(implementation:)p Black 0.7 0.7 0.7 TeXcolorrgb 2087 2769 50 50 v Black Black 2199 2769 a(There)25 b(are)g(the)g(functions)f Fj(getcontext)p Fs(\(3\),)g Fj(make-)2199 2869 y(context)p Fs(\(3\),)41 b Fj(swapcontext)p Fs(\(3\))35 b(and)j Fj(setcon-)2199 2968 y(text)p Fs(\(3\))25 b(which)h(conform)e(to)i(the)g(Single)g(Unix) g(Spec-)2199 3068 y(i\002cation,)j(V)-9 b(ersion)27 b(2)g(\(SUSv2)g ([15)o(],)i(aka)e(Unix95/98\).)2199 3167 y(Unfortunately)d(these)j(are) g(a)n(v)n(ailable)f(on)h(modern)e(Unix)2199 3267 y(platforms)19 b(only)-5 b(.)p Black 0.7 0.7 0.7 TeXcolorrgb 2087 3577 V Black Black 2199 3577 a(There)37 b(are)h(the)g(functions)e Fj(setjmp)p Fs(\(3\))h(and)g Fj(long-)2199 3676 y(jmp)p Fs(\(3\))23 b(which)h(conform)d(to)k(ISO)f(9899:1990)d(\(ISO)j(C\))2199 3776 y(and)29 b Fj(sigsetjmp)p Fs(\(3\))d(and)j Fj(siglongjmp)p Fs(\(3\))d(which)2199 3876 y(conform)39 b(to)i(IEEE)f(Std1003.1-1988)c (\(POSIX\),)k(and)2199 3975 y(Single)24 b(Unix)g(Speci\002cation,)h(V) -9 b(ersion)23 b(2)i(\(SUSv2)f([15)o(],)2199 4075 y(aka)i(Unix95/98\).) 40 b(The)26 b(\002rst)h(tw)o(o)g(functions)e(are)h(a)n(v)n(ail-)2199 4175 y(able)i(really)g(on)g(all)g(Unix)g(platforms,)h(the)f(last)h(tw)o (o)g(are)2199 4274 y(a)n(v)n(ailable)20 b(only)f(on)h(some)g(of)g (them.)p Black 0.7 0.7 0.7 TeXcolorrgb 2087 4584 V Black Black 2199 4584 a(There)44 b(is)i(the)e(function)f Fj(sigaltstack)p Fs(\(2\))f(which)2199 4684 y(conforms)c(to)i(the)g(Single)g(Unix)f (Speci\002cation,)44 b(V)-9 b(er)n(-)2199 4783 y(sion)48 b(2)g(\(SUSv2)g([15)n(]\))g(and)g(its)h(ancestor)e(function)2199 4883 y Fj(sigstack)p Fs(\(2\))32 b(from)g(4.2BSD.)i(The)f(last)i(one)e (e)o(xists)2199 4982 y(only)25 b(on)h(BSD-deri)n(v)o(ed)e(platforms,)i (b)n(ut)f(the)h(\002rst)h(func-)2199 5082 y(tion)20 b(already)f(e)o (xists)i(on)e(all)i(current)e(Unix)h(platforms.)p Black 0 5400 a Fm(P)n(or)q(table)11 b(Multithreading)0 5458 y Fl(The)h(Signal)g(Stac)o(k)e(T)-5 b(r)q(ic)o(k)12 b(F)o(or)f (User-Space)g(Thread)h(Creation)1929 5400 y Fs(3)1221 b Fl(Cop)o(yr)q(ight)3398 5399 y(c)3381 5400 y Fk(\015)11 b Fl(1999)g(Ralf)h(S)o(.)f(Engelschall)3524 5458 y(All)g(Rights)h (Reser)q(v)o(ed.)p Black eop %%Page: 4 4 4 3 bop Black Black 0 -32 a Ft(2.3)121 b(Maxim)o(um)32 b(P)-6 b(or)5 b(tability)33 b(Solution)0 138 y Fs(The)49 b(maximum)e(portable)g(solution)h(ob)o(viously)f(is)j(to)f(use)0 237 y(the)54 b(standardized)e Fj(makecontext)p Fs(\(3\))f(function)h (to)i(cre-)0 337 y(ate)k(threads)e(and)h Fj(switchcontext)p Fs(\(2\))e(or)i Fj(getcon-)0 436 y(text)p Fs(\(2\)/)p Fj(setcontext)p Fs(\(3\))23 b(to)k(dispatch)f(them.)46 b(And)27 b(actu-)0 536 y(ally)g(these)h(are)f(the)g(preferred)e (functions)h(modern)g(user)n(-space)0 636 y(multithreading)14 b(libraries)j(are)g(using.)23 b(Unfortunately)15 b(there)h(are)0 735 y(still)i(a)f(lot)g(of)f(platforms)g(where)g(this)h(approach)d (cannot)i(be)h(used.)0 835 y(So)24 b(our)f(f)o(allback)f(approach)g (has)h(to)h(use)g(the)f(remaining)f(possi-)0 935 y(bilities.)0 1245 y Ft(2.4)121 b(Remaining)34 b(P)-6 b(ossibilities)0 1415 y Fs(Our)27 b(problem)f(can)h(be)g(di)n(vided)f(into)h(tw)o(o)h (parts,)h(an)e(easy)h(one)0 1514 y(and)20 b(a)g(dif)n(\002cult)g(one.)0 1750 y Fq(The)j(Easy)g(P)m(ar)s(t)0 1889 y Fs(That)d Fj(setjmp)p Fs(\(3\))f(and)h Fj(longjmp)p Fs(\(3\))e(can)j(be)f(used)g (to)h(imple-)0 1989 y(ment)26 b(user)n(-space)g(threads)g(is)i (commonly)c(kno)n(wn,)j(of)f(course.)0 2088 y(Mostly)15 b(all)h(older)f(portable)f(user)n(-space)h(threading)e(libraries)i(are) 0 2188 y(based)24 b(on)g(them,)h(although)e(some)h(problems)f(are)i (kno)n(wn)e(with)0 2287 y(these)i(f)o(acilities)h(\(see)f(belo)n(w\).) 38 b(So)25 b(it)h(becomes)e(clear)m(,)i(that)f(we)0 2387 y(ha)n(v)o(e)18 b(to)h(use)g(the)f(these)h(functions)e(and)h(base)h (our)f(machine)f(con-)0 2487 y(te)o(xt)j(\()p Fn(mctx)p 353 2487 25 4 v 29 w(t)p Fs(\))g(on)g(their)g Fj(jmp)p 885 2487 V 29 w(buf)g Fs(data)g(structure.)125 2594 y(W)-7 b(e)44 b(immediately)d(recognize)h(that)h(this)g(w)o(ay)g(we)h(ha)n(v)o (e)0 2693 y(at)i(least)f(solv)o(ed)g(the)g(dispatching)e(problem,)49 b(because)c(our)0 2793 y Fj(mctx)p 205 2793 V 29 w(save)27 b Fs(and)f Fj(mctx)p 808 2793 V 29 w(restore)g Fs(functions)g(were)g (mod-)0 2892 y(eled)20 b(wisely)h(after)e Fj(setjmp)p Fs(\(3\))g(and)h Fj(longjmp)p Fs(\(3\).)0 3128 y Fq(The)j(Dif\002cult)g (P)m(ar)s(t)0 3267 y Fs(Ne)n(v)o(ertheless)32 b(the)h(dif)n(\002cult)g (problem)e(of)i(ho)n(w)g(to)g(create)g(the)0 3367 y(machine)g(conte)o (xt)g(remains.)67 b(Ev)o(en)34 b(kno)n(wing)e(that)j(our)e(ma-)0 3466 y(chine)20 b(conte)o(xt)g(is)i Fj(jmp)p 698 3466 V 29 w(buf)f Fs(based)f(is)i(no)e(adv)n(antage)f(to)i(us.)28 b(A)0 3566 y Fj(jmp)p 155 3566 V 29 w(buf)21 b Fs(has)f(to)g(be)h (treated)e(by)h(us)h(as)g(an)f(opaque)e(data)i(struc-)0 3665 y(ture)29 b(\227)h(for)f(portability)f(reasons.)53 b(The)29 b(only)g(operations)f(we)0 3765 y(can)17 b(perform)e(on)h(it,) i(are)f Fj(setjmp)p Fs(\(3\))e(and)i Fj(longjmp)p Fs(\(3\))e(calls,)0 3865 y(of)26 b(course.)42 b(Additionally)-5 b(,)26 b(we)g(are)g(forced) f(to)h(use)h Fj(sigalt-)0 3964 y(stack)p Fs(\(3\))f(for)h(our)g(stack)h (manipulations,)f(because)g(it)h(is)h(the)0 4064 y(only)19 b(portable)g(function)g(which)g(actually)h(deals)g(with)h(stacks.)125 4171 y(It)32 b(should)g(be)g(noted,)j(that)d(other)g(thread)f(packages) h(ha)n(v)o(e)0 4270 y(tried)48 b(similar)g(approaches.)107 b(In)48 b(particular)m(,)56 b(R)q Fo(O)t(B)t(E)t(RT)d Fs(S)t(.)2 4370 y(T)t Fo(H)t(AU)r Fs(')-5 b(s)40 b Fr(Really)e(Simple)h (Thr)m(eads)f Fs(\(rsthreads,)k([22)o(]\))c(pack-)0 4470 y(age)e(uses)g Fj(sigaltstack)p Fs(\(2\))e(in)i(a)g(v)o(ery)f(similar)i (w)o(ay)f(for)0 4569 y(thread)29 b(creation.)53 b(And)32 b(K)r Fo(O)q(T)n(A)i Fs(A)t Fo(B)t(E)r Fs(')-5 b(s)31 b Fr(P)-7 b(ortable)29 b(Thr)m(ead)h(Li-)0 4669 y(br)o(ary)21 b Fs(\(PTL,)g([19)o(]\))g(uses)h(a)f(similar)h Fj(sigstack)p Fs(\(2\))d(approach)0 4769 y(as)g(its)g(f)o(allback)f(approach)e(for)h (thread)g(creation.)24 b(But)18 b(both)g(v)n(ari-)0 4868 y(ants)31 b(do)g(not)g(w)o(ork)f(on)h(some)g(System-V)-8 b(-deri)n(v)o(ed)28 b(platforms,)0 4968 y(for)20 b(reasons)f(we)i(will) g(discuss)f(later)-5 b(.)2116 -37 y(It)21 b(is)h(clear)e(our)g (implementation)f(for)h Fj(mctx)p 3445 -37 V 29 w(create)g Fs(has)1992 63 y(to)i(play)g(a)h(fe)n(w)f(tricks)g(to)h(use)f(a)h Fj(jmp)p 3093 63 V 29 w(buf)g Fs(for)e(passing)h(e)o(x)o(ecu-)1992 162 y(tion)15 b(control)g(to)h(an)g(arbitrary)f(startup)g(routine.)23 b(And)15 b(it)i(has)f(to)h(be)1992 262 y(carefully)f(to)j(mak)o(e)f (sure)g(to)g(not)g(suf)n(fer)g(from)f(une)o(xpected)f(side-)1992 362 y(ef)n(fects.)54 b(It)30 b(should)f(be)h(also)h(ob)o(vious)d(that)i (we)h(cannot)e(again)1992 461 y(e)o(xpect)e(to)i(\002nd)g(an)f(easy)h (solution)f(\(as)h(for)f Fj(mctx)p 3527 461 V 30 w(save)g Fs(and)1992 561 y Fj(mctx)p 2197 561 V 29 w(restore)p Fs(\),)40 b(because)c Fj(setjmp)p Fs(\(3\))g(and)g Fj(sigalt-)1992 660 y(stack)p Fs(\(3\))15 b(cannot)i(be)g(tri)n(vially)g(combined)f(to) h(achie)n(v)o(e)g(our)f(e)o(x-)1992 760 y(pected)j(semantics)h(for)g Fj(mctx)p 2900 760 V 29 w(create)p Fs(.)1992 1115 y Fu(3)143 b(Implementation)1992 1315 y Fs(As)23 b(we)g(ha)n(v)o(e)f(already)g (disco)o(v)o(ered,)f(our)h(implementation)e(con-)1992 1414 y(tains)35 b(an)g(easy)h(part)f(\()p Fj(mctx)p 2880 1414 V 29 w(save)g Fs(and)g Fj(mctx)p 3500 1414 V 29 w(restore)p Fs(\))1992 1514 y(and)30 b(a)i(dif)n(\002cult)f(part)g(\()p Fj(mctx)p 2899 1514 V 29 w(create)p Fs(\).)57 b(Let)32 b(us)g(start)f(with)1992 1613 y(the)20 b(easy)g(part,)g(whose)f (implementation)f(is)k(ob)o(vious:)3614 1583 y Fp(5)p Black Black 1992 1825 a Ff(typedef)43 b(struct)h Fh(mctx_st)g Fg(f)2171 1917 y Fh(jmp_buf)f(jb;)1992 2008 y Fg(g)i Fh(mctx_t;)1992 2191 y Ff(#define)e Fh(mctx_save\(mctx\))2171 2282 y(\(setjmp\(mctx->jb\)\))1992 2465 y Ff(#define)g Fh(mctx_restore\(mctx\))2171 2556 y(\(longjmp\(mctx->jb,)f(TRUE\)\)) 1992 2739 y(mctx)p 2176 2739 23 4 v 26 w(t)i(*)1992 2830 y(mctx_create\()p Ff(void)p Fh(\(*func\)\()p Ff(void)p Fh(\),)2529 2921 y Ff(void)h Fh(*arg,)2529 3013 y Ff(void)g Fh(*skaddr,)2529 3104 y Ff(size_t)f Fh(sksize)223 b(\))1992 3195 y Fg(f)2171 3286 y Fh(mctx)p 2355 3286 V 26 w(t)45 b(*mctx;)2190 3469 y Fe(...to)17 b(be)j(\002lled)e(in...)2171 3560 y Ff(return)44 b Fh(mctx;)1992 3652 y Fg(g)1992 3864 y Fs(There)29 b(is)i(only)f(one)f(subtle)h(b)n(ut)h(important)d (point)i(we)h(should)1992 3964 y(mention:)42 b(The)29 b(use)h(of)f(the)h(C)g(pre-processor)d Fj(#define)i Fs(di-)1992 4064 y(recti)n(v)o(e)d(to)i(implement)e Fj(mctx)p 2923 4064 25 4 v 29 w(save)h Fs(and)g Fj(mctx)p 3527 4064 V 29 w(restore)1992 4163 y Fs(is)j(not)g(indiscriminately)-5 b(.)52 b(F)o(or)30 b(technical)f(reasons)h(related)f(to)1992 4263 y Fj(setjmp)p Fs(\(3\))j(and)j Fj(return)f Fs(\(which)g(we)h(will) g(e)o(xplain)e(later)1992 4363 y(in)h(detail\))g(we)h Fr(cannot)f Fs(implement)g(these)g(tw)o(o)h(functions)e(as)1992 4462 y(C)k(functions)f(if)h(we)g(w)o(ant)g(to)f(achie)n(v)o(e)g (maximum)f(portabil-)1992 4562 y(ity)-5 b(.)25 b(Instead)19 b(the)o(y)h(ha)n(v)o(e)g(to)g(be)h(implemented)d(as)j(pre-processor) 1992 4661 y(macros.)2116 4769 y(Before)30 b(we)h(no)n(w)e(can)i (proceed)d(with)j(\002lling)f(in)h(the)f(gaps)1992 4868 y(of)18 b(the)i(implementation)d(sk)o(eleton)h(for)h Fj(mctx)p 3360 4868 V 29 w(create)p Fs(,)f(let)i(us)1992 4968 y(\002rst)g(discuss)h(the)f(proposed)e(algorithm.)p Black 0 5021 764 4 v 90 5077 a Fv(5)120 5100 y Fo(All)f(error)h(checks) g(are)g(omitted)g(for)f(better)i(readability)l(.)p Black Black 0 5400 a Fm(P)n(or)q(table)11 b(Multithreading)0 5458 y Fl(The)h(Signal)g(Stac)o(k)e(T)-5 b(r)q(ic)o(k)12 b(F)o(or)f(User-Space)g(Thread)h(Creation)1929 5400 y Fs(4)1221 b Fl(Cop)o(yr)q(ight)3398 5399 y(c)3381 5400 y Fk(\015)11 b Fl(1999)g(Ralf)h(S)o(.)f(Engelschall)3524 5458 y(All)g(Rights)h(Reser)q(v)o(ed.)p Black eop %%Page: 5 5 5 4 bop Black Black 0 -32 a Ft(3.1)121 b(Ov)m(er)t(vie)n(w)0 124 y Fs(The)26 b(general)f(idea)h(for)g Fj(mctx)p 921 124 25 4 v 29 w(create)p Fs(,)h(is)g(to)g(con\002gure)d(the)0 223 y(gi)n(v)o(en)18 b(stack)i(as)h(a)f(signal)g(stack)g(via)f Fj(sigaltstack)p Fs(\(2\),)e(send)0 323 y(the)37 b(current)f(process)h (a)g(signal)g(to)h(transfer)e(e)o(x)o(ecution)f(con-)0 423 y(trol)d(onto)f(this)i(stack,)i(sa)n(v)o(e)d(the)g(machine)f(conte) o(xt)f(there)i(via)0 522 y Fj(setjmp)p Fs(\(3\),)19 b(get)j(rid)f(of)g (the)g(signal)g(handler)f(scope)h(and)f(boot-)0 622 y(strap)g(into)g (the)g(startup)g(routine.)125 722 y(As)46 b(mentioned,)k(the)45 b(real)h(problem)d(in)j(this)g(approach)0 821 y(comes)27 b(from)e(the)i(signal)g(handler)f(scope)g(which)h(implies)g(v)n(ar)n(-) 0 921 y(ious)g(restrictions)g(on)g(Unix)g(platforms.)45 b(As)28 b(we)f(will)h(see,)i(we)0 1020 y(ha)n(v)o(e)25 b(to)g(perform)e(a)j(fe)n(w)f(tricks)g(to)h(get)f(rid)g(of)g(it.)41 b(The)25 b(second)0 1120 y(main)30 b(problem)e(is:)45 b(ho)n(w)30 b(do)f(we)i(prepare)d(the)i(calling)f(of)h(the)0 1220 y(start)21 b(routine)e(without)g(actually)h(entering)f(it.)0 1484 y Ft(3.2)121 b(Algor)r(ithm)0 1640 y Fs(The)33 b(input)g(to)h(the) g Fj(mctx)p 808 1640 V 29 w(create)f Fs(function)f(is)i(the)g(thread)0 1740 y(startup)24 b(routine)g Fr(func)p Fs(,)h(the)g(thread)f(startup)g (ar)o(gument)f Fr(ar)m(g)i Fs(and)0 1839 y(a)g(chunk)e(of)i(memory)e (starting)h(at)i Fr(skaddr)g Fs(and)e(of)h(size)g Fr(sksize)p Fs(,)0 1939 y(which)20 b(should)f(become)g(the)h(threads)g(stack.)26 b(The)20 b(algorithm)e(of)0 2039 y Fj(mctx)p 205 2039 V 29 w(create)d Fs(is)i(directly)e(modeled)g(after)g(the)h(implemented) 0 2138 y(algorithm)j(one)g(can)h(\002nd)g(in)h Fr(GNU)g(Pth)p Fs(:)p Black 104 2287 a(1.)p Black 41 w(Allocate)f(a)g(ne)n(w)g (machine)f(conte)o(xt)g(structure)g Fr(mctx)p Fs(.)p Black 104 2439 a(2.)p Black 41 w(Preserv)o(e)g(the)h(current)f(signal)h (mask)h(and)e(block)h(an)g(arbi-)208 2539 y(trary)f(w)o(ork)o(er)g (signal.)p Black 104 2691 a(3.)p Black 41 w(Preserv)o(e)33 b(a)h(possibly)f(e)o(xisting)g(signal)h(action)g(for)f(the)208 2791 y(w)o(ork)o(er)14 b(signal)h(con\002gure)f(a)i(trampoline)d (function)h(as)i(the)208 2890 y(ne)n(w)k(temporary)e(signal)i(action.)p Black 104 3043 a(4.)p Black 41 w(Preserv)o(e)d(a)i(possibly)f(acti)n(v) o(e)g(alternate)g(signal)g(stack)h(and)208 3142 y(con\002gure)35 b Fr(skaddr)j Fs(as)g(the)f(ne)n(w)g(temporary)e(alternate)208 3242 y(signal)20 b(stack)g(of)g(length)f Fr(sksize)p Fs(.)p Black 104 3394 a(5.)p Black 41 w(Send)33 b(the)h(current)e (process)h(the)h(w)o(ork)o(er)f(signal,)j(tem-)208 3494 y(porarily)16 b(unblock)g(it)i(and)g(this)g(w)o(ay)g(allo)n(w)g(it)g (to)g(be)g(deli)n(v-)208 3594 y(ered)24 b(on)g(the)h(signal)g(stack)g (in)f(order)g(to)h(transfer)f(e)o(x)o(ecu-)208 3693 y(tion)19 b(control)g(to)i(the)f(trampoline)e(function.)p Black 104 3846 a(6.)p Black 41 w(After)31 b(the)g(trampoline)f(function)g (asynchronously)e(en-)208 3945 y(tered,)18 b(sa)n(v)o(e)g(its)i (machine)d(conte)o(xt)h(in)g(the)h Fr(mctx)g Fs(structure)208 4045 y(and)f(immediately)g(return)f(from)h(it)i(to)f(terminate)f(the)h (sig-)208 4145 y(nal)h(handler)e(scope.)p Black 104 4297 a(7.)p Black 41 w(Restore)i(the)h(preserv)o(ed)e(alternate)h(signal)g (stack)h(and)f(the)208 4397 y(preserv)o(ed)c(signal)j(action)f(and)g (mask)g(for)g(w)o(ork)o(er)f(signal.)p Black 104 4549 a(8.)p Black 41 w(Sa)n(v)o(e)121 b(the)h(current)f(machine)g(conte)o (xt)f(of)208 4649 y Fj(mctx)p 413 4649 V 29 w(create)p Fs(.)p Black 104 4801 a(9.)p Black 41 w(Restore)16 b(the)g(pre)n (viously)e(sa)n(v)o(ed)i(machine)f(conte)o(xt)f(of)i(the)208 4901 y(trampoline)k(function)g(\()p Fr(mctx)p Fs(\))i(to)g(again)g (transfer)f(e)o(x)o(ecu-)208 5000 y(tion)28 b(control)g(onto)g(the)h (alternate)f(stack,)j(b)n(ut)e(this)h(time)208 5100 y(without\(!\))23 b(signal)d(handler)f(scope.)p Black 2054 -37 a(10.)p Black 40 w(After)57 b(reaching)f(the)h(trampoline)f(function)g(\()p Fr(mctx)p Fs(\))2199 63 y(again,)47 b(immediately)41 b(bootstrap)h(into)g(a)h(clean)f(stack)2199 162 y(frame)k(situation)h (by)f(calling)h(a)g(second)f(trampoline)2199 262 y(function.)p Black 2054 483 a(11.)p Black 40 w(Set)56 b(the)f(ne)n(w)f(signal)h (mask)g(to)g(be)g(the)g(same)g(as)2199 582 y(the)37 b(original)f (signal)g(mask)h(which)f(w)o(as)i(acti)n(v)o(e)e(when)2199 682 y Fj(mctx)p 2404 682 V 29 w(create)20 b Fs(w)o(as)h(called.)p Black 2054 903 a(12.)p Black 40 w(Load)50 b(the)i(startup)e (information)f(\()p Fr(func)p Fs(,)58 b Fr(ar)m(g)p Fs(\))51 b(from)2199 1003 y Fj(mctx)p 2404 1003 V 29 w(create)20 b Fs(into)g(local)g(\(stack-based\))e(v)n(ariables.)p Black 2054 1224 a(13.)p Black 40 w(Restore)26 b(the)g(machine)f(conte)o (xt)g(of)h Fj(mctx)p 3486 1224 V 29 w(create)f Fs(to)2199 1323 y(transfer)19 b(e)o(x)o(ecution)f(control)h(back)h(to)g(it.)p Black 2054 1544 a(14.)p Black 40 w(Return)28 b(the)h(generated)d (machine)i(conte)o(xt)f Fr(mctx)i Fs(to)g(the)2199 1644 y(caller)-5 b(.)1992 1881 y(When)23 b(we)h(no)n(w)g(again)f(switch)h (into)f(the)h(established)g(machine)1992 1981 y(conte)o(xt)15 b Fr(mctx)p Fs(,)j(the)f(thread)f(starts)i(running)d(at)j(routine)e Fr(func)g Fs(with)1992 2081 y(ar)o(gument)22 b Fr(ar)m(g)p Fs(.)40 b(The)25 b(follo)n(wing)e(\002gure)h(tries)i(to)f(illustrate)h (the)1992 2180 y(algorithm:)2001 2516 y 14917438 13873209 0 0 18484674 17300602 startTexFig 2001 2516 a %%BeginDocument: fig2.eps %!PS-Adobe-2.0 EPSF-2.0 %%Title: fig2.eps %%Creator: fig2dev Version 3.2 Patchlevel 1 %%CreationDate: Mon Jul 19 18:21:01 1999 %%For: rse@@en1.engelschall.com (Ralf S. Engelschall) %%Orientation: Portrait %%BoundingBox: 0 0 281 263 %%Pages: 0 %%BeginSetup %%EndSetup %%Magnification: 1.0000 %%EndComments /$F2psDict 200 dict def $F2psDict begin $F2psDict /mtrx matrix put /col-1 {0 setgray} bind def /col0 {0.000 0.000 0.000 srgb} bind def /col1 {0.000 0.000 1.000 srgb} bind def /col2 {0.000 1.000 0.000 srgb} bind def /col3 {0.000 1.000 1.000 srgb} bind def /col4 {1.000 0.000 0.000 srgb} bind def /col5 {1.000 0.000 1.000 srgb} bind def /col6 {1.000 1.000 0.000 srgb} bind def /col7 {1.000 1.000 1.000 srgb} bind def /col8 {0.000 0.000 0.560 srgb} bind def /col9 {0.000 0.000 0.690 srgb} bind def /col10 {0.000 0.000 0.820 srgb} bind def /col11 {0.530 0.810 1.000 srgb} bind def /col12 {0.000 0.560 0.000 srgb} bind def /col13 {0.000 0.690 0.000 srgb} bind def /col14 {0.000 0.820 0.000 srgb} bind def /col15 {0.000 0.560 0.560 srgb} bind def /col16 {0.000 0.690 0.690 srgb} bind def /col17 {0.000 0.820 0.820 srgb} bind def /col18 {0.560 0.000 0.000 srgb} bind def /col19 {0.690 0.000 0.000 srgb} bind def /col20 {0.820 0.000 0.000 srgb} bind def /col21 {0.560 0.000 0.560 srgb} bind def /col22 {0.690 0.000 0.690 srgb} bind def /col23 {0.820 0.000 0.820 srgb} bind def /col24 {0.500 0.190 0.000 srgb} bind def /col25 {0.630 0.250 0.000 srgb} bind def /col26 {0.750 0.380 0.000 srgb} bind def /col27 {1.000 0.500 0.500 srgb} bind def /col28 {1.000 0.630 0.630 srgb} bind def /col29 {1.000 0.750 0.750 srgb} bind def /col30 {1.000 0.880 0.880 srgb} bind def /col31 {1.000 0.840 0.000 srgb} bind def end save -62.0 271.0 translate 1 -1 scale /cp {closepath} bind def /ef {eofill} bind def /gr {grestore} bind def /gs {gsave} bind def /sa {save} bind def /rs {restore} bind def /l {lineto} bind def /m {moveto} bind def /rm {rmoveto} bind def /n {newpath} bind def /s {stroke} bind def /sh {show} bind def /slc {setlinecap} bind def /slj {setlinejoin} bind def /slw {setlinewidth} bind def /srgb {setrgbcolor} bind def /rot {rotate} bind def /sc {scale} bind def /sd {setdash} bind def /ff {findfont} bind def /sf {setfont} bind def /scf {scalefont} bind def /sw {stringwidth} bind def /tr {translate} bind def /tnt {dup dup currentrgbcolor 4 -2 roll dup 1 exch sub 3 -1 roll mul add 4 -2 roll dup 1 exch sub 3 -1 roll mul add 4 -2 roll dup 1 exch sub 3 -1 roll mul add srgb} bind def /shd {dup dup currentrgbcolor 4 -2 roll mul 4 -2 roll mul 4 -2 roll mul srgb} bind def /DrawEllipse { /endangle exch def /startangle exch def /yrad exch def /xrad exch def /y exch def /x exch def /savematrix mtrx currentmatrix def x y tr xrad yrad sc 0 0 1 startangle endangle arc closepath savematrix setmatrix } def /$F2psBegin {$F2psDict begin /$F2psEnteredState save def} def /$F2psEnd {$F2psEnteredState restore end} def %%EndProlog $F2psBegin 10 setmiterlimit n -1000 5512 m -1000 -1000 l 6712 -1000 l 6712 5512 l cp clip 0.06000 0.06000 sc d1529 1 a1529 39 % Ellipse n 4440 2460 90 90 0 360 DrawEllipse gs 0.00 setgray ef gr gs col0 s gr % Ellipse n 3335 690 90 90 0 360 DrawEllipse gs 0.00 setgray ef gr gs col0 s gr % Ellipse n 2565 2085 90 90 0 360 DrawEllipse gs 0.00 setgray ef gr gs col0 s gr % Ellipse n 1935 465 90 90 0 360 DrawEllipse gs 0.00 setgray ef gr gs col0 s gr % Ellipse n 3585 690 90 90 0 360 DrawEllipse gs 0.00 setgray ef gr gs col0 s gr % Ellipse n 1365 1515 90 90 0 360 DrawEllipse gs 0.00 setgray ef gr gs col0 s gr % Ellipse n 1665 2640 90 90 0 360 DrawEllipse gs 0.00 setgray ef gr gs col0 s gr % Ellipse n 1935 2985 90 90 0 360 DrawEllipse gs 0.00 setgray ef gr gs col0 s gr % Ellipse n 3060 1365 90 90 0 360 DrawEllipse gs 0.00 setgray ef gr gs col0 s gr % Ellipse n 3510 3060 90 90 0 360 DrawEllipse gs 0.00 setgray ef gr gs col0 s gr % Ellipse n 3435 3735 90 90 0 360 DrawEllipse gs 0.00 setgray ef gr gs col0 s gr % Ellipse n 1710 3810 90 90 0 360 DrawEllipse gs 0.00 setgray ef gr gs col0 s gr % Ellipse n 4665 2835 90 90 0 360 DrawEllipse gs 0.00 setgray ef gr gs col0 s gr d1531 1 d1533 4 a1536 3 4173 842 m 4200 900 l 4142 873 l 4195 927 l 4227 895 l cp clip n 3675 375 m 4200 900 l gs col0 s gr gr d1539 2 a1540 1 n 4173 842 m 4200 900 l 4142 873 l 4158 858 l 4173 842 l cp gs 0.00 setgray ef gr col0 s d1542 1 d1544 4 a1547 3 4176 1890 m 4200 1950 l 4143 1921 l 4193 1976 l 4227 1946 l cp clip n 2775 375 m 4200 1950 l gs col0 s gr gr d1550 2 a1551 1 n 4176 1890 m 4200 1950 l 4143 1921 l 4160 1906 l 4176 1890 l cp gs 0.00 setgray ef gr col0 s d1553 1 d1555 4 a1558 3 2373 1543 m 2325 1500 l 2389 1501 l 2319 1474 l 2303 1515 l cp clip n 4425 2325 m 2325 1500 l gs col0 s gr gr d1561 2 a1562 1 n 2373 1543 m 2325 1500 l 2389 1501 l 2381 1522 l 2373 1543 l cp gs 0.00 setgray ef gr col0 s d1564 1 a1564 1 45.000 slw d1566 4 a1569 3 4134 2280 m 4253 2325 l 4134 2370 l 4335 2370 l 4335 2280 l cp clip n 3675 2325 m 4275 2325 l gs col0 s gr gr d1572 3 a1575 3 n 4134 2280 m 4253 2325 l 4134 2370 l 4134 2325 l 4134 2280 l cp gs 0.00 setgray ef gr col0 s % Polyline 45.000 slw d1577 4 a1580 3 1920 2709 m 1875 2828 l 1830 2709 l 1830 2910 l 1920 2910 l cp clip n 4275 2550 m 1875 2550 l 1875 2850 l gs col0 s gr gr d1583 3 d1587 2 a1588 1 n 1920 2709 m 1875 2828 l 1830 2709 l 1875 2709 l 1920 2709 l cp gs 0.00 setgray ef gr col0 s a1589 1 45.000 slw d1591 4 a1594 3 3159 2280 m 3278 2325 l 3159 2370 l 3360 2370 l 3360 2280 l cp clip n 1875 2175 m 1875 2325 l 3300 2325 l gs col0 s gr gr d1597 3 d1601 2 a1602 1 n 3159 2280 m 3278 2325 l 3159 2370 l 3159 2325 l 3159 2280 l cp gs 0.00 setgray ef gr col0 s a1603 1 45.000 slw d1605 4 a1608 3 1659 2130 m 1778 2175 l 1659 2220 l 1860 2220 l 1860 2130 l cp clip n 1200 2175 m 1800 2175 l gs col0 s gr gr d1611 3 a1614 3 n 1659 2130 m 1778 2175 l 1659 2220 l 1659 2175 l 1659 2130 l cp gs 0.00 setgray ef gr col0 s % Polyline 7.500 slw d1616 4 a1619 3 1365 1253 m 1425 1275 l 1365 1298 l 1440 1298 l 1440 1253 l cp clip n 1800 3000 m 1050 3000 l 1050 1275 l 1425 1275 l gs col0 s gr gr d1622 2 a1623 1 n 1365 1253 m 1425 1275 l 1365 1298 l 1365 1275 l 1365 1253 l cp gs 0.00 setgray ef gr col0 s d1625 1 a1625 3 n 4200 1950 m 5700 1950 l 5700 3000 l 4200 3000 l cp gs col0 s gr % Polyline 45.000 slw d1627 4 a1630 3 4545 3459 m 4500 3578 l 4455 3459 l 4455 3660 l 4545 3660 l cp clip n 4500 2925 m 4500 3600 l gs col0 s gr gr d1633 3 a1636 3 n 4545 3459 m 4500 3578 l 4455 3459 l 4500 3459 l 4545 3459 l cp gs 0.00 setgray ef gr col0 s % Polyline 45.000 slw d1638 4 a1641 3 4359 2805 m 4478 2850 l 4359 2895 l 4560 2895 l 4560 2805 l cp clip n 1875 3150 m 1875 3300 l 3300 3300 l 3300 2850 l 4500 2850 l gs col0 s gr gr d1644 4 d1649 14 a1662 1 n 4359 2805 m 4478 2850 l 4359 2895 l 4359 2850 l 4359 2805 l cp gs 0.00 setgray ef gr col0 s a1663 1 7.500 slw d1665 5 a1669 3 4072 1464 m 4125 1500 l 4061 1507 l 4134 1525 l 4145 1482 l cp clip n 2325 1050 m 4125 1500 l gs col0 s gr gr d1672 12 a1683 1 n 4072 1464 m 4125 1500 l 4061 1507 l 4067 1485 l 4072 1464 l cp gs 0.00 setgray ef gr col0 s d1685 3 a1687 1 45.000 slw d1689 5 a1693 4 1920 3759 m 1875 3878 l 1830 3759 l 1830 3960 l 1920 3960 l cp clip n 4425 3600 m 1875 3600 l 1875 3900 l gs col0 s gr gr d1695 50 d1746 11 a1756 1 n 1920 3759 m 1875 3878 l 1830 3759 l 1875 3759 l 1920 3759 l cp gs 0.00 setgray ef gr col0 s d1758 3 a1760 1 45.000 slw d1762 5 a1766 4 1341 4020 m 1221 3975 l 1341 3930 l 1140 3930 l 1140 4020 l cp clip n 1875 3975 m 1200 3975 l gs col0 s gr gr d1768 5 d1774 11 a1784 1 n 1341 4020 m 1221 3975 l 1341 3930 l 1341 3975 l 1341 4020 l cp gs 0.00 setgray ef gr col0 s d1786 11 d1798 1 a1798 1 n 1500 1950 m 3000 1950 l 3000 4200 l 1500 4200 l cp gs col0 s gr d1800 1 d1802 1 a1802 1 [38] 0 sd d1804 4 a1807 3 4545 4059 m 4500 4178 l 4455 4059 l 4455 4230 l 4545 4230 l cp clip n 4500 3675 m 4500 4200 l gs col0 s gr gr d1810 3 a1812 16 n 4545 4059 m 4500 4178 l 4455 4059 l 4500 4059 l 4545 4059 l cp gs 0.00 setgray ef gr col0 s /Helvetica-Narrow-Bold ff 150.00 scf sf 3450 300 m gs 1 -1 sc (sigstack) col0 sh gr /Helvetica-Narrow-Bold ff 150.00 scf sf 4275 1050 m gs 1 -1 sc (stack) col0 sh gr /Helvetica-Narrow-Bold ff 150.00 scf sf 1725 1350 m gs 1 -1 sc (pmctx) col0 sh gr /Helvetica-Narrow-Bold ff 150.00 scf sf 4875 3450 m gs 1 -1 sc (trampoline II) col0 sh gr /Helvetica-Narrow-Bold ff 150.00 scf sf 5025 2100 m gs 1 -1 sc (trampoline) col0 sh gr d1814 72 a1885 29 7.500 slw n 1500 1200 m 2250 1200 l 2250 1425 l 1500 1425 l cp gs col0 s gr /Helvetica-Narrow-Bold ff 150.00 scf sf 4425 1650 m gs 1 -1 sc (val) col0 sh gr /Helvetica-Narrow-Bold ff 150.00 scf sf 1725 1125 m gs 1 -1 sc (val) col0 sh gr /Helvetica-Narrow-Bold ff 150.00 scf sf 1725 900 m gs 1 -1 sc (func) col0 sh gr /Helvetica-Narrow-Bold ff 150.00 scf sf 4425 1425 m gs 1 -1 sc (func) col0 sh gr /Helvetica-Narrow-Bold ff 150.00 scf sf 5325 4050 m gs 1 -1 sc (func\(\)) col0 sh gr % Polyline n 1500 600 m 2250 600 l 2250 1800 l 1500 1800 l cp gs col0 s gr % Polyline n 1500 975 m 2250 975 l 2250 1200 l 1500 1200 l cp gs col0 s gr % Polyline n 1500 750 m 2250 750 l 2250 975 l 1500 975 l cp gs col0 s gr % Polyline n 4200 1275 m 4950 1275 l 4950 1500 l 4200 1500 l cp gs col0 s gr % Polyline n 4200 1500 m 4950 1500 l 4950 1725 l 4200 1725 l cp gs col0 s gr /Helvetica-Narrow-Bold ff 150.00 scf sf 3482 3117 m d1887 2 a1888 2 /Helvetica-Narrow-Bold ff 150.00 scf sf 2537 2135 m d1890 2 a1891 2 /Helvetica-Narrow-Bold ff 150.00 scf sf 3557 747 m d1893 2 a1894 2 /Helvetica-Narrow-Bold ff 150.00 scf sf 4403 2517 m d1896 2 a1897 2 /Helvetica-Narrow-Bold ff 150.00 scf sf 4636 2885 m d1899 5 a1903 18 /Helvetica-Narrow-Bold ff 150.00 scf sf 1330 1588 m gs 1 -1 sc (1) col7 sh gr /Helvetica-Narrow-Bold ff 150.00 scf sf 3365 3790 m gs 1 -1 sc (12) col7 sh gr /Helvetica-Narrow-Bold ff 150.00 scf sf 1637 2698 m gs 1 -1 sc (6) col7 sh gr % Polyline n 4200 3300 m 5700 3300 l 5700 3750 l 4200 3750 l cp gs col0 s gr /Helvetica-Narrow-Bold ff 150.00 scf sf 1907 3042 m gs 1 -1 sc (7) col7 sh gr % Polyline n 4200 3900 m 5700 3900 l 5700 4500 l 4200 4500 l cp gs col0 s gr /Helvetica-Narrow-Bold ff 150.00 scf sf 2990 1420 m d1905 5 a1909 5 /Helvetica-Narrow-Bold ff 150.00 scf sf 3308 733 m gs 1 -1 sc (2) col7 sh gr /Helvetica-Narrow-Bold ff 150.00 scf sf 1865 520 m a1910 38 /Helvetica-Narrow-Bold ff 150.00 scf sf 1643 3859 m gs 1 -1 sc (13) col7 sh gr % Polyline n 3300 2325 m 3300 2324 l 3302 2321 l 3307 2313 l 3314 2300 l 3323 2286 l 3333 2273 l 3342 2261 l 3350 2252 l 3357 2247 l 3363 2245 l 3369 2246 l 3375 2250 l 3382 2259 l 3389 2271 l 3397 2286 l 3404 2303 l 3413 2320 l 3421 2336 l 3428 2349 l 3436 2358 l 3443 2363 l 3450 2363 l 3456 2358 l 3462 2349 l 3468 2337 l 3474 2323 l 3481 2306 l 3488 2289 l 3494 2272 l 3501 2257 l 3507 2244 l 3513 2234 l 3519 2227 l 3525 2225 l 3531 2227 l 3537 2234 l 3543 2244 l 3549 2258 l 3556 2274 l 3563 2293 l 3569 2312 l 3576 2331 l 3582 2348 l 3588 2364 l 3594 2377 l 3600 2387 l 3600 2388 l 3606 2394 l 3612 2398 l 3618 2397 l 3625 2393 l 3633 2386 l 3642 2375 l 3652 2362 l 3661 2349 l 3668 2337 l 3673 2329 l 3675 2326 l 3675 2325 l gs col0 s gr /Helvetica-Narrow-Bold ff 150.00 scf sf 1575 2100 m gs 1 -1 sc (create\(\)) col0 sh gr /Helvetica-Narrow-Bold ff 150.00 scf sf 1650 300 m gs 1 -1 sc (sigmask) col0 sh gr % Polyline n 4200 900 m 4950 900 l 4950 1800 l 4200 1800 l cp gs col0 s gr % Polyline n 1500 150 m 2250 150 l 2250 375 l 1500 375 l cp gs col0 s gr % Polyline n 2400 150 m 3150 150 l 3150 375 l 2400 375 l cp gs col0 s gr % Polyline n 3300 150 m 4050 150 l 4050 375 l 3300 375 l cp gs col0 s gr % Polyline n 1500 1425 m 2250 1425 l 2250 1650 l 1500 1650 l cp gs col0 s gr /Helvetica-Narrow-Bold ff 150.00 scf sf 2550 300 m gs 1 -1 sc (sigaction) col0 sh gr /Helvetica-Narrow-Bold ff 150.00 scf sf 1725 1575 m gs 1 -1 sc (mctx) col0 sh gr d1917 10 a1926 108 1992 4619 a Ft(3.3)120 b(Source)34 b(Code)1992 4801 y Fs(The)45 b(corresponding)e(ANSI)k(C)g(code,)52 b(which)46 b(implements)1992 4901 y Fj(mctx)p 2197 4901 25 4 v 29 w(create)p Fs(,)55 b(is)50 b(a)f(little)h(bit)f(more)g(complicated,)54 b(of)1992 5000 y(course.)f(But)31 b(with)f(the)h(presented)d(algorithm) h(in)h(mind,)i(it)f(is)1992 5100 y(no)n(w)19 b(straight-forw)o(ard.)p Black 0 5400 a Fm(P)n(or)q(table)11 b(Multithreading)0 5458 y Fl(The)h(Signal)g(Stac)o(k)e(T)-5 b(r)q(ic)o(k)12 b(F)o(or)f(User-Space)g(Thread)h(Creation)1929 5400 y Fs(5)1221 b Fl(Cop)o(yr)q(ight)3398 5399 y(c)3381 5400 y Fk(\015)11 b Fl(1999)g(Ralf)h(S)o(.)f(Engelschall)3524 5458 y(All)g(Rights)h(Reser)q(v)o(ed.)p Black eop %%Page: 6 6 6 5 bop Black Black Black Black 0 -37 a Ff(static)44 b Fh(mctx_t)716 b(mctx_caller;)0 38 y Ff(static)44 b(volatile)f Fh(sig_atomic_t)g(mctx_called;)0 187 y Ff(static)h Fh(mctx_t)88 b(*mctx_creat;)0 262 y Ff(static)44 b(void)134 b Fh (\(*mctx_creat_func\)\()p Ff(void)41 b Fh(*\);)0 337 y Ff(static)j(void)178 b Fh(*mctx_creat_arg;)0 411 y Ff(static)44 b Fh(sigset_t)f(mctx_creat_sigs;)0 561 y(mctx_t)h (*mctx_create\()179 636 y Ff(void)g Fh(\(*func\)\()p Ff(void)f Fh(*\),)i Ff(void)f Fh(*arg,)179 710 y Ff(void)g Fh(*skaddr,)g Ff(size_t)g Fh(sksize\))0 785 y Fg(f)179 860 y Fh(mctx_t)g(*mctx;)179 934 y Ff(struct)g Fh(sigaction)f(sa;)179 1009 y Ff(struct)h Fh(sigaction)f(osa;)179 1084 y Ff(struct)h Fh(sigaltstack)f(ss;)179 1159 y Ff(struct)h Fh(sigaltstack)f(oss;)179 1233 y(sigset_t)h(osigs;)179 1308 y(sigset_t)g(sigs;)179 1457 y(/*)64 b Fe(Step)18 b(1:)45 b Fh(*/)179 1532 y(mctx)f(=)h (malloc\()p Ff(sizeof)p Fh(\(mctx_t\)\);)179 1682 y(/*)64 b Fe(Step)18 b(2:)45 b Fh(*/)179 1756 y(sigemptyset\(&sigs\);)179 1831 y(sigaddset\(&sigs,)d(SIGUSR1\);)179 1906 y (sigprocmask\(SIG_BLOCK,)717 1980 y(&sigs,)i(&osigs\);)179 2130 y(/*)64 b Fe(Step)18 b(3:)45 b Fh(*/)179 2205 y(memset\()p Ff(void)e Fh(*\)&sa,)h(0,)493 2279 y Ff(sizeof)p Fh(\()p Ff(struct)807 2354 y Fh(sigaction\)\);)179 2429 y(sa.sa_handler)f(=)359 2503 y(mctx_create_trampoline;)179 2578 y(sa.sa_flags)g(=)i (SA_ONSTACK;)179 2653 y(sigemptyset\(&sa.sa_mask\);)179 2728 y(sigaction\(SIGUSR1,)d(&sa,)i(&osa\);)179 2877 y(/*)64 b Fe(Step)18 b(4:)45 b Fh(*/)179 2952 y(ss.ss_sp)178 b(=)45 b(skaddr;)179 3027 y(ss.ss_size)88 b(=)45 b(sksize;)179 3101 y(ss.ss_flags)e(=)i(0;)179 3176 y(sigaltstack\(&ss,)d(&oss\);)179 3325 y(/*)64 b Fe(Step)18 b(5:)45 b Fh(*/)179 3400 y(mctx_creat)268 b(=)44 b(mctx;)179 3475 y(mctx_creat_func)f(=)h(func;)179 3550 y(mctx_creat_arg)88 b(=)44 b(arg;)179 3624 y(mctx_creat_sigs)f(=)h (osigs;)179 3699 y(mctx_called)223 b(=)44 b(FALSE;)179 3774 y(kill\(getpid\(\),)f(SIGUSR1\);)179 3848 y(sigfillset\(&sigs\);) 179 3923 y(sigdelset\(&sigs,)f(SIGUSR1\);)179 3998 y Ff(while)i Fh(\(!mctx_called\))359 4073 y(sigsuspend\(&sigs\);)179 4222 y(/*)64 b Fe(Step)18 b(7:)45 b Fh(*/)179 4297 y (sigaltstack\(NULL,)d(&ss\);)179 4371 y(ss.ss_flags)h(=)i(SS_DISABLE;) 179 4446 y(sigaltstack\(&ss,)d(NULL\);)179 4521 y(if)j (\(!\(oss.ss_flags)d(&)j(SS_DISABLE\)\))359 4596 y(sigaltstack\(&oss,)d (NULL\);)179 4670 y(sigaction\(SIGUSR1,)g(&osa,)i(NULL\);)179 4745 y(sigprocmask\(SIG_SETMASK,)717 4820 y(&osigs,)g(NULL\);)179 4969 y(/*)64 b Fe(Step)18 b(8,9:)45 b Fh(*/)179 5044 y(mctx_switch\(&mctx_caller,)c(mctx\);)2171 38 y(/*)63 b Fe(Step)19 b(14:)45 b Fh(*/)2171 112 y Ff(return)f Fh(mctx;)1992 187 y Fg(g)1992 337 y Ff(void)g Fh (mctx_create_trampoline\()p Ff(int)c Fh(sig\))1992 411 y Fg(f)2171 486 y Fh(/*)63 b Fe(Step)19 b(6:)45 b Fh(*/)2171 561 y Ff(if)f Fh(\(mctx_save\(mctx_creat\))d(==)k(0\))f Fg(f)2350 636 y Fh(mctx_called)f(=)i(TRUE;)2350 710 y Ff(return)p Fh(;)2171 785 y Fg(g)2171 934 y Fh(/*)63 b Fe(Step)19 b(10:)45 b Fh(*/)2171 1009 y(mctx_create_boot\(\);)1992 1084 y Fg(g)1992 1233 y Ff(void)f Fh(mctx_create_boot\()p Ff(void)p Fh(\))1992 1308 y Fg(f)2171 1383 y Fh(mctx_t)g(*mctx_start;) 2171 1457 y Ff(void)g Fh(\(*mctx_start_func\)\()p Ff(void)d Fh(*\);)2171 1532 y Ff(void)j Fh(*mctx_start_arg;)2171 1682 y(/*)63 b Fe(Step)19 b(11:)45 b Fh(*/)2171 1756 y(sigprocmask\(SIG_SETMASK,)2709 1831 y(&mctx_creat_sigs,)2709 1906 y(NULL\);)2171 2055 y(/*)63 b Fe(Step)19 b(12:)45 b Fh(*/)2171 2130 y(mctx_start)267 b(=)45 b(mctx_creat;)2171 2205 y(mctx_start_func)d(=)j(mctx_creat_func;)2171 2279 y(mctx_start_arg)87 b(=)45 b(mctx_creat_arg;)2171 2429 y(/*)63 b Fe(Step)19 b(13:)45 b Fh(*/)2171 2503 y (mctx_switch\(mctx_start,)2709 2578 y(&mctx_caller\);)2171 2728 y(/*)63 b Fe(The)18 b(thread)h(``magically'')g(starts...)63 b Fh(*/)2171 2802 y(mctx_start_func\(mctx_start_arg\);)2171 2877 y(abort\(\);)1992 2952 y Fg(g)1992 3224 y Ft(3.4)120 b(Remaining)35 b(Issues)1992 3380 y Fs(The)23 b(presented)g(algorithm)g (and)g(source)h(code)f(can)h(be)g(directly)1992 3479 y(used)33 b(in)i(practice)e(for)h(implementing)e(a)j(minimal)e (threading)1992 3579 y(system)19 b(or)g(the)h(concept)e(of)h (co-routines.)k(Its)d(big)f(adv)n(antage)f(is,)1992 3679 y(that)k(if)h(the)g(operating)d(system)j(pro)o(vides)e(the)h(required)f (POSIX)1992 3778 y(primiti)n(v)o(es,)i(we)i(do)f(not)f(need)h(to)g(kno) n(w)f(an)o(ything)f(at)j(all)g(about)1992 3878 y(the)d(machine)g(we)h (are)f(running)f(on)h(\227)h(e)n(v)o(erything)d(just)j(w)o(orks.)1992 3978 y(Ne)n(v)o(ertheless,)32 b(there)f(remain)f(a)i(fe)n(w)f(special)g (issues)h(we)f(still)1992 4077 y(ha)n(v)o(e)19 b(to)h(discuss.)1992 4277 y Fq(The)j(W)m(aggly)g(longjmp\(3\))h(after)g(Retur)r(n)1992 4402 y Fs(On)c(some)h(platforms,)e Fj(longjmp)p Fs(\(3\))g(may)h(not)g (be)g(called)h(after)1992 4502 y(the)d(function)e(which)h(called)h(the) g Fj(setjmp)p Fs(\(3\))e(returned.)23 b(When)1992 4602 y(this)30 b(is)g(done,)h(the)e(stack)h(frame)f(situation)g(is)i(not)e (guaranteed)1992 4701 y(to)e(be)h(in)g(a)g(clean)f(and)g(consistent)h (state.)48 b(But)28 b(this)g(is)g(e)o(xactly)1992 4801 y(on)22 b(what)h(our)f(trick)g(is)i(based)e(to)h(get)g(rid)g(of)f(the)h (signal)g(handler)1992 4901 y(scope,)c(when)h(you)f(remember)-5 b(.)2116 5000 y(The)20 b(only)g(alternati)n(v)o(e)f(w)o(ould)h(be)h(to) g(lea)n(v)o(e)f(the)h(signal)f(han-)1992 5100 y(dler)31 b(via)g Fj(longjmp)p Fs(\(3\),)h(b)n(ut)f(then)g(we)h(w)o(ould)e(ha)n (v)o(e)h(another)p Black 0 5400 a Fm(P)n(or)q(table)11 b(Multithreading)0 5458 y Fl(The)h(Signal)g(Stac)o(k)e(T)-5 b(r)q(ic)o(k)12 b(F)o(or)f(User-Space)g(Thread)h(Creation)1929 5400 y Fs(6)1221 b Fl(Cop)o(yr)q(ight)3398 5399 y(c)3381 5400 y Fk(\015)11 b Fl(1999)g(Ralf)h(S)o(.)f(Engelschall)3524 5458 y(All)g(Rights)h(Reser)q(v)o(ed.)p Black eop d1928 102 a2029 148 7 6 bop Black Black 0 -37 a Fs(problem)31 b(as)j(e)o(xperience)d(sho)n (wed.)62 b(F)o(or)32 b(instance,)38 b(R)q Fo(O)t(B)t(E)t(RT)2 63 y Fs(S)t(.)f(T)t Fo(H)t(AU)r Fs(')-5 b(s)34 b Fr(Really)e(Simple)g (Thr)m(eads)h Fs(\()p Fr(r)o(sthr)m(eads)p Fs(\))f([22)n(])h(w)o(as)0 162 y(ported)20 b(to)h(se)n(v)o(eral)g(platforms)f(and)h(w)o(as)h(used) f(to)h(run)e(an)h(e)o(xper)n(-)0 262 y(imental)j(multithreaded)e(v)o (ersion)h(of)h(the)h(Apache)e(web)i(serv)o(er)-5 b(.)2 362 y(T)t Fo(H)t(AU)r Fs(')g(s)32 b(approach)c(dif)n(fered)g(mainly)i (in)g(the)h(w)o(ay)f(the)g(signal)0 461 y(handler)17 b(is)j(left.)25 b(In)18 b(particular)m(,)f(in)i(an)g(attempt)f(to)h(a)n (v)n(oid)f(the)h(un-)0 561 y(safe)27 b(stack)g(frame)f(situation,)i(it) f(uses)h(a)f Fj(longjmp)p Fs(\(3\))e(call)i(to)0 660 y(lea)n(v)o(e)19 b(the)g(signal)f(handler)m(,)f(rather)h(than)h (returning)e(from)g(it.)26 b(But)0 760 y(this)j(approach)d(does)h(not)h (w)o(ork)g(on)f(some)h(System-V)-8 b(-deri)n(v)o(ed)0 860 y(k)o(ernels,)19 b(as)i(we)g(already)e(mentioned.)125 960 y(The)e(problem)f(is)j(that)f(these)f(k)o(ernels)h(do)f(not)g (\223belie)n(v)o(e\224)g(that)0 1059 y(the)g(code)g(is)i(out)e(of)g (the)g(signal-handling)e(conte)o(xt)h(until)h(the)h(sig-)0 1159 y(nal)k(handler)f(has)h(returned)f(\227)i(and)e(accordingly)-5 b(,)20 b(refuse)i(to)g(al-)0 1259 y(lo)n(w)f(readjustment)e(of)i(the)g (signal)f(stack)h(until)g(it)g(has.)28 b(But)21 b(with)0 1358 y(the)e Fr(r)o(sthr)m(eads)h Fs(approach,)d(the)i(signal)g (handler)e(for)i(the)g(creation)0 1458 y(of)f(the)g(\002rst)g(thread)f (ne)n(v)o(er)g(returns,)g(and)g(when)g Fr(r)o(sthr)m(eads)i Fs(w)o(ants)0 1557 y(to)d(create)f(the)g(second)g(thread,)g(these)h(k)o (ernels)f(refuse)g(to)h(readjust)0 1657 y(the)24 b(signal)g(stack,)g (and)g(we)g(are)g(stuck.)35 b(So)24 b(with)h(portability)d(in)0 1757 y(mind,)i(we)h(decided,)e(that)i(it)g(is)g(better)f(to)g(get)g (rid)g(of)g(the)g(signal)0 1856 y(handler)30 b(scope)g(with)i(the)f (straight-forw)o(ard)d(\223)p Fj(return)p Fs(\224)j(and)0 1956 y(instead)g(\002ght)g(the)f(mentioned)g(\(simpler\))f(stack)j (frame)e(prob-)0 2056 y(lem.)125 2156 y(F)o(ortunately)f(in)j(practice) f(this)i(is)f(not)g(as)g(problematic)e(as)0 2255 y(it)c(seems,)h (because)e(e)n(v)n(aluations)f(on)g(mostly)h(all)h(current)e(Unix)0 2355 y(platforms)c(for)h Fr(GNU)h(Pth)g Fs(sho)n(wed,)e(that)i(one)f (can)g(reach)g(a)h(safe)0 2454 y(stack)17 b(frame)f(situation)g(again)g (by)g(just)i(calling)e(a)h(function.)22 b(That)0 2554 y(is)d(the)f(reason)f(why)g(our)g(algorithm)f(enters)i(the)g(second)f (trampo-)0 2654 y(line)j(function.)0 2855 y Fq(The)j(Uncooper)o(ativ)n (e)h(longjmp\(3\))0 2980 y Fs(Ev)o(en)k(on)h(operating)e(systems)i (which)g(ha)n(v)o(e)f(w)o(orking)g(POSIX)0 3080 y(functions,)d(our)g (approach)f(may)h(theoretically)f(still)j(not)e(w)o(ork,)0 3179 y(because)52 b Fj(longjmp)p Fs(\(3\))f(does)i(not)g(cooperate.)122 b(F)o(or)52 b(in-)0 3279 y(stance,)21 b(on)g(some)f(platforms)g(the)h (standard)f Fr(libc)h Fj(longjmp)p Fs(\(3\))0 3379 y(branches)d(to)h (error)n(-handling)c(code)k(if)g(it)h(detects)f(that)g(the)g(caller)0 3478 y(tries)j(to)g(jump)f Fr(up)g Fs(the)h(stack,)f(i.e.,)h(into)g(a)g (stack)f(frame)g(that)h(has)0 3578 y(already)d(returned.)125 3678 y(This)f(is)h(usually)f(implemented)f(by)h(comparing)e(the)i (current)0 3777 y(stack)26 b(pointer)e(to)i(the)g(one)f(in)h(the)g Fj(jmp)p 1197 3777 25 4 v 29 w(buf)p Fs(.)41 b(That)26 b(is)h(why)d(it)0 3877 y(is)31 b(important)e(for)g(our)g(algorithm)g (to)h(return)f(from)g(the)h(signal)0 3977 y(handler)18 b(and)h(this)i(w)o(ay)e(enter)h(the)f(\(dif)n(ferent\))f(stack)h(of)h (the)f(par)n(-)0 4076 y(ent.)29 b(In)21 b(practice,)g(the)g (implementation)e(in)j Fr(GNU)g(Pth)f Fs(sho)n(wed,)0 4176 y(that)j(then)g(one)g(no)g(longer)f(suf)n(fers)g(from)g(those)h (uncooperati)n(v)o(e)0 4276 y Fj(longjmp)p Fs(\(3\))h(implementations,) i(b)n(ut)g(one)f(should)g(k)o(eep)h(this)0 4375 y(point)34 b(in)h(mind)e(when)h(reaching)f(e)n(v)o(en)h(more)f(uncooperati)n(v)o (e)0 4475 y(v)n(ariants)22 b(on)g(esoteric)g(Unix)g(platforms.)31 b(If)23 b(it)g(still)g(occurs,)g(one)0 4574 y(can)i(only)f(try)h(to)h (resume)e(the)h(operation)f(by)g(using)h(a)h(possibly)0 4674 y(e)o(xisting)19 b(platform-speci\002c)f(error)h(handling)g(hook.) 0 4875 y Fq(Garbage)k(at)g(Bottom)i(of)e(Stac)n(ks)0 5000 y Fs(When)d(you)f(carefully)g(follo)n(wed)g(the)h(algorithms)f (control)g(\003o)n(w)-5 b(,)0 5100 y(you)26 b(certainly)g(ha)n(v)o(e)g (recognized)e(a)k(subtle)e(side-ef)n(fect:)38 b(there)1992 -37 y(remains)20 b(some)g(garbage)f(at)i(the)g(bottom)e(of)i(each)f (thread)g(stack.)1992 63 y(The)h(reason)h(is,)h(that)g(when)e(a)i (signal)f(is)h(deli)n(v)o(ered,)e(the)h(operat-)1992 162 y(ing)f(system)i(pushes)e(some)h(state)h(onto)e(the)i(stack,)f (which)g(is)h(re-)1992 262 y(stored)16 b(later)m(,)i(when)e(the)i (signal)f(handler)f(returns.)23 b(But)17 b(although)1992 362 y(we)23 b(return)e(from)h(the)g(signal)h(handler)m(,)e(we)i(jump)f (in)h(again,)f(and)1992 461 y(this)h(time)g(we)h(enter)e(not)h (directly)f(at)i(the)f(bottom)f(of)h(the)g(stack,)1992 561 y(because)c(of)h(the)g Fj(setjmp)p Fs(\(3\))f(in)h(the)g (trampoline)f(function.)2116 663 y(Since)e(the)g(operating)e(system)i (has)g(to)g(capture)f(all)i(CPU)g(re)o(g-)1992 763 y(isters)34 b(\(including)e(those)h(that)h(are)g(ordinarily)e(scratch)h(re)o(gis-) 1992 862 y(ters)d(or)f(caller)n(-sa)n(v)o(e)h(re)o(gisters\),)h(there)e (can)h(be)g(a)g(f)o(air)g(amount)1992 962 y(of)24 b(memory)f(at)i(the)g (bottom)e(of)i(the)g(established)f(thread)g(stack.)1992 1062 y(F)o(or)c(some)g(systems)h(this)g(can)g(be)f(e)n(v)o(en)g(up)g (to)g(1)h(KB)g(of)g(garbage)1992 1161 y([22)n(].)35 b(But)23 b(e)o(xcept)g(for)f(the)i(additional)d(memory)h(consumption)1992 1261 y(it)e(does)g(not)g(hurt,)f(of)h(course.)2116 1363 y(W)-7 b(e)29 b(just)g(ha)n(v)o(e)f(to)g(remember)e(this)j(additional)e (stack)h(con-)1992 1463 y(sumption)17 b(when)h(deciding)e(the)j(stack)g (size)g(\()p Fr(sksize)p Fs(\).)25 b(A)19 b(reason-)1992 1562 y(able)k(stack)h(size)h(usually)e(is)i(between)e(16)h(and)f(32)g (KB.)i(Less)g(is)1992 1662 y(neither)18 b(reasonable)h(nor)f(al)o(w)o (ays)j(allo)n(wed)e(\(current)f(Unix)h(plat-)1992 1761 y(forms)f(usually)h(require)e(a)j(stack)f(to)h(be)f(at)g(least)h(16)f (KB)h(in)g(size\).)1992 1974 y Fq(Stac)n(k)j(Ov)n(er\003o)o(ws)1992 2103 y Fs(There)52 b(is)i(a)f(noticeable)f(dif)n(ference)f(between)h (the)h(initial)1992 2203 y(\223thread\224)30 b(and)i(the)g(e)o (xplicitly)f(spa)o(wned)g(threads:)49 b(the)32 b(initial)1992 2303 y(thread)22 b(runs)h(on)g(the)h(standard)e(process)h(stack.)35 b(This)23 b(stack)h(au-)1992 2402 y(tomatically)e(can)h(gro)n(w)g (under)e(Unix,)j(while)f(the)h(stacks)f(of)h(the)1992 2502 y(spa)o(wned)19 b(threads)i(are)g(\002x)o(ed)g(in)g(size.)28 b(So)22 b(stack)f(o)o(v)o(er\003o)n(ws)f(can)1992 2602 y(occur)14 b(for)h(the)g(spa)o(wned)f(threads,)i(of)f(course.)22 b(This)16 b(implies)f(that)1992 2701 y(the)25 b(parent)f(has)i(to)f (mak)o(e)g(a)h(reasonable)e(guess)h(of)g(the)g(threads)1992 2801 y(stack)20 b(space)g(requirement)e(already)h(at)i(spa)o(wning)d (time.)2116 2903 y(And)26 b(there)g(is)i(no)e(really)g(portable)f (solution)g(to)i(this)g(prob-)1992 3003 y(lem,)18 b(because)f(e)n(v)o (en)g(when)g(the)h(thread)f(library')-5 b(s)17 b(scheduler)g(can)1992 3102 y(detect)i(the)h(stack)g(o)o(v)o(er\003o)n(w)-5 b(,)18 b(it)j(cannot)d(easily)j(resize)f(the)g(stack.)1992 3202 y(The)15 b(reason)h(is)h(simply)f(that)g(the)h(stack)f (initialization)g(goes)g(hand)1992 3302 y(in)28 b(hand)g(with)h(the)f (initialization)g(of)g(the)h(start)g(routine,)g(when)1992 3401 y(you)c(remember)-5 b(.)44 b(And)26 b(this)i(start)f(routine)f (has)h(to)g(be)f(a)i(real)f(C)1992 3501 y(function)22 b(in)j(order)e(to)h Fr(call)p Fs(.)38 b(But)24 b(once)g(the)g(thread)g (is)h(running,)1992 3600 y(there)20 b(no)h(longer)f(e)o(xists)i(such)f (an)g(entry)f(point.)27 b(So,)22 b(e)n(v)o(en)e(when)1992 3700 y(the)k(scheduler)f(w)o(ould)g(be)i(able)f(to)g(gi)n(v)o(e)g(the)g (thread)f(a)i(ne)n(w)f(en-)1992 3800 y(lar)o(ged)15 b(stack,)i(there)g (is)h(no)e(chance)g(to)h(restart)g(the)g(thread)f(on)g(this)1992 3899 y(ne)n(w)k(stack.)2116 4002 y(Or)h(more)f(correct,)f(there)i(is)g (no)g Fr(portable)e Fs(chance.)26 b(As)21 b(with)1992 4101 y(the)h(problems)f(before,)g(when)h(one)g(accept)f(platform)g (speci\002cs,)1992 4201 y(there)g(is)i(a)f(chance,)g(of)f(course.)30 b(That')-5 b(s)22 b(why)f(our)g(implementa-)1992 4300 y(tion)26 b(did)g(not)g(deal)g(with)h(this)g(issue.)44 b(Instead)26 b(in)g(practice)g(one)1992 4400 y(usually)k(lets)i(the)f (scheduler)f(just)h(detect)g(the)g(stack)g(o)o(v)o(er\003o)n(w)1992 4500 y(and)18 b(terminate)g(the)g(thread.)24 b(This)19 b(is)g(done)f(by)h(using)f(a)h(red)f(zone)1992 4599 y(at)g(the)h(top)f (of)g(the)h(stack)f(which)g(is)i(mark)o(ed)d(with)h(a)h(magic)f(v)n (alue)1992 4699 y(the)30 b(scheduler)e(checks)i(between)f(thread)g (dispatching)g(opera-)1992 4799 y(tions.)2116 4901 y(More)i(adv)n (anced)e(solutions)i(are)g(only)f(possible)h(in)h(semi-)1992 5000 y(portable)19 b(w)o(ays.)26 b(One)21 b(approach)d(is)k(to)e(place) h(the)f(thread)g(stacks)1992 5100 y(into)32 b(a)h(memory)e(mapped)g (area)h(\(see)h Fj(mmap)p Fs(\(2\)\))e(of)h(the)h(pro-)p Black 0 5400 a Fm(P)n(or)q(table)11 b(Multithreading)0 5458 y Fl(The)h(Signal)g(Stac)o(k)e(T)-5 b(r)q(ic)o(k)12 b(F)o(or)f(User-Space)g(Thread)h(Creation)1929 5400 y Fs(7)1221 b Fl(Cop)o(yr)q(ight)3398 5399 y(c)3381 5400 y Fk(\015)11 b Fl(1999)g(Ralf)h(S)o(.)f(Engelschall)3524 5458 y(All)g(Rights)h(Reser)q(v)o(ed.)p Black eop d2031 162 a2192 148 8 7 bop Black Black 0 -37 a Fs(cess)24 b(address)f(space)g(and)g(let)h (the)f(scheduler)f(catch)h Fj(SIGSEGV)0 63 y Fs(signals.)53 b(When)29 b(such)g(a)h(signal)f(occurs,)h(because)f(of)g(a)h(stack)0 162 y(o)o(v)o(er\003o)n(w)25 b(in)h(this)h(area,)h(the)f(scheduler)e (manually)g(resizes)i(the)0 262 y(memory)16 b(mapped)g(area.)24 b(This)18 b(resizing)g(can)f(be)h(done)f(either)g(by)0 362 y(cop)o(ying)26 b(the)i(stack)h(contents)e(into)h(a)g(ne)n(w)g(lar) o(ger)f(area)h(which)0 461 y(is)c(then)f(re-mapped)e(to)i(the)g(old)g (address)g(or)f(via)i(an)f(e)n(v)o(en)f(more)0 561 y(ele)o(gant)i(w)o (ay)-5 b(,)26 b(as)f(the)h(v)o(endor)d(threading)g(libraries)i(of)f Fr(Solaris)p Fs(,)0 660 y Fr(F)-5 b(r)m(eeBSD)23 b Fs(and)g Fr(T)-5 b(ru64)23 b Fs(do)g(it:)33 b(the)23 b(thread)g(stacks)g(are)h (allocated)0 760 y(inside)19 b(memory)e(mapped)g(areas)j(which)e(are)h (already)f(initially)h(a)0 860 y(fe)n(w)e(MB)g(in)g(\(virtual\))e(size) i(and)f(then)g(one)g(just)h(relies)g(on)g(the)f(vir)n(-)0 959 y(tual)25 b(memory)f(system')-5 b(s)26 b(feature)e(that)h(only)g (the)g(actually)g(con-)0 1059 y(sumed)20 b(memory)e(space)i(is)h (mapped.)0 1280 y Fq(Star)s(tup)j(Routine)g(T)-10 b(er)r(mination)0 1413 y Fs(When)27 b(you)g(ha)n(v)o(e)g(read)g(the)g(proposed)e (implementation)h(care-)0 1513 y(fully)-5 b(,)23 b(you)f(should)g(ha)n (v)o(e)g(also)i(recognized)d(a)i(cruel)g Fj(abort)p Fs(\(3\))0 1612 y(call)31 b(at)g(the)g(end)f(of)h Fj(mctx)p 832 1612 25 4 v 29 w(trampoline)p 1361 1612 V 28 w(jumpin)p Fs(.)56 b(This)0 1712 y(means,)36 b(when)c(the)h(startup)g(routine)f (returns,)j(the)e(process)g(is)0 1811 y(aborted.)47 b(That)28 b(is)h(ob)o(viously)d(not)i(reasonable,)h(so)f(why)g(ha)n(v)o(e)0 1911 y(we)21 b(written)f(it)g(this)h(w)o(ay?)125 2015 y(When)30 b(the)h(thread)f(returns)h(from)f(the)h(startup)f(routine,)i (it)0 2115 y(should)18 b(be)i(cleanly)e(terminated,)g(of)i(course.)k (But)19 b(it)i(cannot)d(ter)n(-)0 2214 y(minate)23 b(itself)i(\(for)e (instance,)h(because)f(it)i(cannot)d(free)i(its)h(o)n(wn)0 2314 y(stack)e(while)g(running)d(on)j(it,)h(etc\).)32 b(The)23 b(termination)e(handling)0 2413 y(actually)g(is)i(the)f(task)g (of)f(the)h(thread)f(libraries)g(scheduler)-5 b(.)29 b(Usu-)0 2513 y(ally)c(the)f(thread)g(spa)o(wning)f(function)g(of)h (the)h(thread)f(library)f(is)0 2613 y(not)d(directly)f Fj(mctx)p 606 2613 V 29 w(create)p Fs(.)125 2717 y(Instead)34 b(the)h(thread)f(spa)o(wning)g(function)f(uses)j(an)f(addi-)0 2816 y(tional)19 b(trampoline)f(function)g(as)j(the)f(startup)f (routine.)k(And)d(this)0 2916 y(trampoline)25 b(function)g(performs)f (a)j(conte)o(xt)f(switch)g(back)g(into)0 3016 y(the)h(thread)f(library) g(scheduler)g(when)g(the)h(real)g(thread)g(startup)0 3115 y(routine)g(returned.)49 b(The)28 b(scheduler)f(then)h(can)h (safely)f(remo)o(v)o(e)0 3215 y(the)40 b(thread)e(and)h(its)i(machine)d (conte)o(xt.)82 b(That)39 b(is)h(why)f(the)0 3314 y Fj(abort)p Fs(\(3\))19 b(call)h(is)h(ne)n(v)o(er)e(reached)g(in)h(practice.)0 3535 y Fq(The)j(sigstac)n(k\(2\))f(F)l(allbac)n(k)h(Situation)0 3668 y Fs(Not)45 b(all)g(platforms)e(pro)o(vide)f(POSIX)j Fj(sigaltstack)p Fs(\(2\).)0 3768 y(Instead)59 b(the)o(y)f(at)i(least)g (pro)o(vide)d(the)j(4.2BSD)f(ancestor)0 3868 y(function)40 b Fj(sigstack)p Fs(\(2\).)87 b(But)42 b(one)g(cannot)e(tri)n(vially)h (re-)0 3967 y(place)29 b Fj(sigaltstack)p Fs(\(2\))d(by)j Fj(sigstack)p Fs(\(2\))e(in)i(this)h(situ-)0 4067 y(ation,)i(because)d (in)g(contrast)h(to)g Fj(sigaltstack)p Fs(\(2\))d(the)i(old)0 4167 y Fj(sigstack)p Fs(\(2\))14 b(does)h(not)h(automatically)e(handle) h(the)h(machine)0 4266 y(dependent)i(direction)h(of)h(stack)g(gro)n (wth.)125 4370 y(Instead,)31 b(the)f(caller)f(has)i(to)e(kno)n(w)g(the) h(direction)f(and)g(al-)0 4470 y(w)o(ays)e(call)h Fj(sigstack)p Fs(\(2\))c(with)j(the)g(address)g(of)f(the)h(bottom)0 4569 y(of)34 b(the)h(stack.)68 b(So,)39 b(in)c(a)g(real-w)o(orld)e (implementation)f(\(lik)o(e)0 4669 y Fr(GNU)20 b(Pth)p Fs(\))f(one)f(\002rst)i(has)g(to)f(determine)f(the)h(direction)f(of)h (stack)0 4769 y(gro)n(wth)30 b(in)h(order)f(to)h(use)g Fj(sigstack)p Fs(\(2\))e(as)j(a)g(replacement)0 4868 y(for)26 b Fj(sigaltstack)p Fs(\(2\).)41 b(F)o(ortunately)24 b(this)j(is)h(easier)e(than)g(it)0 4968 y(seems)21 b(on)e(the)i (\002rst)g(look.)794 4938 y Fp(6)1992 -37 y Fq(The)i(Blind)h(Alle)n(y)h (of)e(Br)o(ain-Dead)h(Platf)n(or)r(ms)1992 99 y Fs(Finally)31 b(the)h(w)o(orld)f(w)o(ould)g(not)h(be)g(as)g(funn)o(y)e(as)j(it)f(is,) k(if)c(re-)1992 198 y(ally)38 b(all)i(Unix)e(platforms)g(w)o(ould)g(be) h(f)o(air)g(to)g(us.)81 b(Instead,)1992 298 y(currently)36 b(at)k(least)f(one)f(platform)f(e)o(xists)j(which)e(plays)g(un-)1992 398 y(f)o(air:)28 b(unfortunately)19 b(the)j(popular)e Fr(GNU/Linux)p Fs(.)30 b(Although)20 b(we)1992 497 y(will)f(disco)o(v)o (er)d(that)j(it)g(both)f(pro)o(vides)e Fj(sigaltstack)p Fs(\(2\))g(and)1992 597 y Fj(sigstack)p Fs(\(2\),)29 b(our)g(approach)e(w)o(on')o(t)h(w)o(ork)h(on)g Fr(Linux)h Fs(k)o(er)n(-)1992 697 y(nels)20 b(prior)f(to)i(v)o(ersion)d(2.2)i(and) g Fr(glibc)f Fs(prior)h(to)g(v)o(ersion)f(2.1.)2116 802 y(Why?)75 b(Because)37 b(its)h Fr(libc)f Fs(pro)o(vides)e(only)h (dummies)g(of)1992 901 y(these)e(functions)g(which)g(al)o(w)o(ays)h (return)f(just)h Fj(-1)g Fs(with)g Fj(er-)1992 1001 y(rno)c Fs(set)i(to)f Fj(ENOSYS)p Fs(.)e(So,)35 b(this)d(de\002nitely)f(means)h (that)f(our)1992 1101 y(nifty)16 b(algorithm)f(is)j(useless)g(there,)f (because)f(its)i(central)e(point)h Fr(is)1992 1200 y Fj(sigaltstack)p Fs(\(2\)/)p Fj(sigstack)p Fs(\(2\).)50 b(Ne)n(v)o(ertheless)30 b(we)h(do)1992 1300 y(not)f(need)f(to)i(gi)n(v) o(e)e(up,)j(of)f(course.)54 b(At)31 b(least)g(not,)i(if)d(we,)j(for) 1992 1400 y(a)24 b(single)f(brain-dead)f(platform,)h(accept)g(to)h (break)f(our)g(general)1992 1499 y(goal)c(of)h(not)g(using)g(an)o(y)f (platform)g(dependent)f(things.)2116 1605 y(So,)k(what)f(can)h(we)g (actually)f(do)g(here?)28 b(All)23 b(we)f(ha)n(v)o(e)f(to)g(do,)1992 1704 y(is)g(to)g(\002ddle)g(around)e(a)i(little)h(bit)f(with)g(the)g (machine-dependent)1992 1804 y Fj(jmp)p 2147 1804 V 29 w(buf)29 b Fs(ingredients)e(\(by)h(poking)f(around)g(in)h Fj(setjmp.h)1992 1903 y Fs(or)20 b(by)h(disassembling)f Fj(longjmp)p Fs(\(3\))f(in)i(the)g(deb)n(ugger\).)k(Usu-)1992 2003 y(ally)k(one)f(just)i(has)g(do)e(a)i Fj(setjmp)p Fs(\(3\))e(to)h(get)g(an)g(initial)h(state)1992 2103 y(in)e(the)g Fj(jmp)p 2370 2103 V 29 w(buf)g Fs(structure)f(and)h(then) g(manually)e(adjust)i(tw)o(o)1992 2202 y(of)g(its)h(ingredients:)40 b(the)28 b(program)e(counter)h(\(usually)g(a)i(struc-)1992 2302 y(ture)17 b(member)g(with)h(\223)p Fj(pc)p Fs(\224)g(in)g(the)g (name\))f(and)h(the)g(stack)g(pointer)1992 2402 y(\(usually)h(a)h (structure)g(member)e(with)j(\223)p Fj(sp)p Fs(\224)f(in)g(the)g (name\).)2116 2507 y(That')-5 b(s)26 b(all)g(and)f(can)g(be)h (acceptable)f(for)g(a)h(real-w)o(orld)e(im-)1992 2606 y(plementation)e(which)i(really)g(w)o(ants)h(to)g(co)o(v)o(er)e(all)i (platforms)e(\226)1992 2706 y(at)g(least)g(as)h(long)e(as)i(the)f (special)f(treatment)g(is)i(needed)e(just)h(for)1992 2806 y(one)e(or)h(tw)o(o)g(platforms.)29 b(But)23 b(one)e(has)i(to)f(k) o(eep)f(in)h(mind)g(that)g(it)1992 2905 y(at)c(least)g(breaks)g(one)f (of)g(the)h(initial)g(goals)g(and)f(has)h(to)g(be)g(treated)1992 3005 y(as)i(a)h(last)g(chance)e(solution,)g(of)h(course.)1992 3232 y Fq(More)j(Machine)h(Conte)n(xt)e(Ing)o(redients)1992 3368 y Fs(Finally)39 b(for)g(a)g(real-w)o(orld)f(threading)g (implementation)f(one)1992 3468 y(usually)d(w)o(ant)h(to)h(put)f(more)f (ingredients)g(into)h(the)g(machine)1992 3567 y(state)47 b Fn(mctx)p 2372 3567 V 29 w(t)h Fs(structure,)53 b(of)46 b(course.)106 b(F)o(or)46 b(instance)h(one)1992 3667 y(usually)33 b(w)o(ant)g(per)n(-thread)f(signal)i(masks)f(in)h(order)f (to)g(ful\002ll)1992 3767 y(the)d(POSIX.1c)h(signal)f(handling)f (requirements)g(for)h(threads.)1992 3866 y(When)d Fj(sigsetjmp)p Fs(\(3\))f(and)h Fj(siglongjmp)p Fs(\(3\))f(e)o(xists)i(one)1992 3966 y(can)15 b(just)i(use)f(them)f(instead)h(of)f Fj(setjmp)p Fs(\(3\))f(and)i Fj(longjmp)p Fs(\(2\))1992 4066 y(to)28 b(achie)n(v)o(e)g(this.)51 b(There)28 b(is)h(only)f(one)g(snare:)42 b(on)28 b(some)h(plat-)1992 4165 y(forms)18 b Fj(sigsetjmp)p Fs(/)p Fj(siglongjmp)p Fs(\(3\))d(sa)n(v)o(e)20 b(also)g(informa-)1992 4265 y(tion)j(about)h(the)g(alternate)f(signals)i(stack.)37 b(So)24 b(here)f(one)h(has)g(to)1992 4364 y(mak)o(e)i(sure)h(that)g(e)n (v)o(en)f(the)h(thread)f(dispatching)f(later)j(can)e(use)1992 4464 y Fj(sigsetjmp)p Fs(/)p Fj(siglongjmp)p Fs(\(3\),)e(the)j(thread)e (creation)h(step)1992 4564 y(has)18 b(to)g(use)h(plain)e Fj(setjmp)p Fs(/)p Fj(longjmp)p Fs(\(3\))f(for)h(the)h(trampoline)1992 4663 y(trick.)2116 4769 y(And)31 b(if)g Fj(sigsetjmp)p Fs(/)p Fj(siglongjmp)p Fs(\(3\))c(do)k(not)g(e)o(xist,)1992 4868 y(one)25 b(can)g(easily)h(emulate)f(them)g(by)h(sa)n(ving)f(and)g (restoring)g(the)1992 4968 y(thread)i(signal)i(masks)g(manually)e(via)h Fj(sigprocmask)p Fs(\(2\))e(in)p Black 0 5021 764 4 v 90 5077 a Fv(6)120 5100 y Fo(See)17 b(the)h(\002le)f Fd(aclocal.m4)e Fo(in)j(the)f Fc(GNU)g(Pth)g Fo(distrib)o(ution)j(for)d (details)p Black Black 0 5400 a Fm(P)n(or)q(table)11 b(Multithreading)0 5458 y Fl(The)h(Signal)g(Stac)o(k)e(T)-5 b(r)q(ic)o(k)12 b(F)o(or)f(User-Space)g(Thread)h(Creation)1929 5400 y Fs(8)1221 b Fl(Cop)o(yr)q(ight)3398 5399 y(c)3381 5400 y Fk(\015)11 b Fl(1999)g(Ralf)h(S)o(.)f(Engelschall)3524 5458 y(All)g(Rights)h(Reser)q(v)o(ed.)p Black eop d2194 372 a2565 81 9 8 bop Black Black 0 -37 a Fj(mctx)p 205 -37 25 4 v 29 w(switch)p Fs(.)24 b(One)c(just)h(has)f(to)h(be)f(a)g(little)h(bit)f (carefully)f(to)0 63 y(pre)n(v)o(ent)f(race)i(conditions)f(at)i (dispatching.)125 162 y(Additionally)k(it)i(is)h(reasonable)d(to)i (also)g(sa)n(v)o(e)g(and)g(restore)0 262 y(the)15 b(global)g Fj(errno)g Fs(v)n(ariable.)22 b(F)o(or)15 b(a)h(sophisticated)f (implemen-)0 362 y(tation)24 b(which)g(does)g(also)h(all)g(those)f (additional)f(things,)i(ha)n(v)o(e)f(a)0 461 y(look)c(at)i(the)f (source)g(\002le)g Fj(pth)p 895 461 V 30 w(mctx.c)f Fs(in)i(the)f Fr(GNU)h(Pth)f Fs(dis-)0 561 y(trib)n(ution)e([20)o(].)0 828 y Ft(3.5)121 b(Summar)t(y)0 984 y Fs(W)-7 b(e)26 b(ha)n(v)o(e)e(presented)g(a)h(pragmatic)f(and)g(mostly)h(portable)e(f) o(all-)0 1083 y(back)i(approach)e(for)i(implementing)e(user)n(-space)i (multithread-)0 1183 y(ing,)20 b(based)g(entirely)g(on)g(Unix)h(system) f(and)g(ANSI)h(C)h(language)0 1283 y(f)o(acilities.)44 b(The)26 b(approach)f(w)o(as)i(successfully)f(tested)g(in)h(prac-)0 1382 y(tice)34 b(on)e(a)i(wide)f(range)f(of)h(Unix)g(\003a)n(v)n(ors)h (by)e Fr(GNU)j(Pth)e Fs(and)0 1482 y(should)17 b(also)i(adapt)f(to)g (the)g(remaining)f(Unix)h(platforms)f(as)i(long)0 1581 y(as)j(the)o(y)e(beha)n(v)o(e)f(mostly)i(identical)f(to)h(the)g (current)e(major)h(Unix)0 1681 y(platforms.)0 1948 y Ft(3.6)121 b(Ac)n(kno)n(wledgements)0 2104 y Fs(I)21 b(w)o(ould)e(lik)o(e)i(to)g(thank)g(R)q Fo(O)t(B)t(E)t(RT)26 b Fs(S)t(.)f(T)t Fo(H)t(AU)r Fs(,)e(D)q Fo(A)-5 b(V)t(I)t(D)25 b Fs(B)s Fo(U)t(T)t(E)t(N)t Fs(-)2 2204 y Fo(H)t(O)t(F)r Fs(,)33 b(M)t Fo(A)t(RT)t(I)t(N)g Fs(K)t Fo(R)t(A)t(E)t(M)t(E)t(R)r Fs(,)g(E)t Fo(R)t(I)t(C)g Fs(N)t Fo(E)t(W)t(T)s(O)t(N)d Fs(and)g(B)t Fo(R)q(U)t(N)t(O)2 2303 y Fs(H)t Fo(A)t(I)t(B)t(L)t(E)21 b Fs(for)e(their)f(comments)g(which)g(helped)g(to)h(impro)o(v)o(e)e (this)0 2403 y(paper)-5 b(.)36 b(Additional)23 b(thanks)g(go)h(to)g (the)h(users)f(of)g Fr(GNU)h(Pth)f Fs(for)0 2503 y(their)g(feedback)f (on)i(the)f(implementation)f(which)h(helped)g(a)h(lot)0 2602 y(in)20 b(\002ne-tuning)e(the)j(presented)e(approach.)489 b([)p Fb(rse)p Fs(])0 2899 y Fu(Ref)o(erences)p Black 71 3068 a FA([1])p Black 43 w(M)t(.)t(E)t(.)50 b(C)t Fa(O)t(N)t(W)m(A)n(Y)r FA(:)81 b Fw(Design)48 b(of)g(a)h(separ)o(able)g (tr)o(ansition-)199 3159 y(dia)o(gr)o(am)20 b(compiler)-8 b(.)p FA(,)19 b(Comm.)f(A)m(CM)h(6:7,)g(1963,)h(p.396-408)p Black 71 3276 a([2])p Black 43 w(E)t(.)t(W)m(.)26 b(D)t Fa(I)t(J)t(K)t(S)t(T)t(R)t(A)r FA(:)31 b Fw(Co-oper)o(ating)26 b(sequential)f(pr)m(ocesses)p FA(,)h(in)199 3367 y(F)-6 b(.)29 b(Genuys)j(\(Ed.\),)g Fw(Pr)m(o)o(gr)o(amming)f(Langua)o(g)o(es) p FA(,)k(N)m(A)-8 b(T)o(O)29 b(Ad-)199 3458 y(v)n(anced)f(Study)f (Institute,)g(Academic)h(Press,)f(London,)i(1965,)199 3550 y(p.42-112.)p Black 71 3666 a([3])p Black 43 w(B)t(.)18 b(N)t Fa(I)t(C)t(H)t(O)t(L)t(S)t FA(,)f(D)t(.)g(B)s Fa(U)t(T)t(T)t(L)t (A)t(R)t FA(,)f(J)t(.)t(P)l(.)g(F)o Fa(A)t(R)t(R)t(E)t(L)r FA(:)i Fw(Pthr)m(eads)e(Pr)m(o-)199 3758 y(gr)o(amming)26 b(-)e(A)h(POSIX)f(Standar)m(d)j(for)e(Better)f(Multipr)m(ocess-)199 3849 y(ing)p FA(,)19 b(O'Reilly)f(1996;)i(ISBN)e(1-56592-115-1)p Black 71 3965 a([4])p Black 43 w(B)t(.)30 b(L)t Fa(E)t(W)t(I)t(S)t FA(,)g(D)t(.)g(J)t(.)g(B)t Fa(E)t(R)t(G)r FA(:)37 b Fw(Thr)m(eads)28 b(Primer)e(-)h(A)f(Guide)h(T)-7 b(o)199 4057 y(Multithr)m(eaded)28 b(Pr)m(o)o(gr)o(amming)p FA(,)h(Prentice)e(Hall)f(1996;)31 b(ISBN)199 4148 y(0-13-443698-9)p Black 71 4265 a([5])p Black 43 w(S)t(.)23 b(J)t(.)g(N)t Fa(O)t(RT)s(O)t(N)t FA(,)e(M)t(.)j(D)t(.)e(D)t Fa(I)t(P)o(A)t(S)t(Q)s(U)r(A)t(L)t(E)r FA(:)g Fw(Thr)m(ead)f(T)l(ime)e(-)g(The)199 4356 y(Multithr)m(eaded)27 b(Pr)m(o)o(gr)o(amming)f(Guide)p FA(,)g(Prentice)f(Hall)f(1997;)199 4447 y(ISBN)18 b(0-13-190067-6)p Black 71 4564 a([6])p Black 43 w(D)t(.)29 b(R)t(.)f(B)s Fa(U)t(T)t(E)t(N)t(H)t(O)t(F)r FA(:)34 b Fw(Pr)m(o)o(gr)o(amming)27 b(with)e(POSIX)g(Thr)m(eads)p FA(,)199 4655 y(Addison)20 b(W)-6 b(esle)o(y)19 b(1997;)h(ISBN)e (0-201-63392-2)p Black 71 4772 a([7])p Black 43 w(D)t(.)t(S)t Fa(T)t(E)t(I)t(N)t FA(,)30 b(D)t(.)t(S)t Fa(H)t(A)t(H)r FA(:)40 b Fw(Implementing)29 b(Lightwight)f(Thr)m(eads)p FA(,)199 4863 y(SunSoft)19 b(Inc.,)f(1992)i(\(published)g(at)f (USENIX'92\).)p Black 71 4980 a([8])p Black 43 w(W)m(.)t(R)t(.)t(S)t Fa(T)t(E)t(V)t(E)t(N)t(S)r FA(:)e Fw(Advanced)j(Pr)m(o)o(gr)o(amming)g (in)e(the)h(Unix)g(En-)199 5071 y(vir)m(onment)p FA(,)g(Addison-W)-6 b(esle)o(y)h(,)20 b(1992;)g(ISBN)e(0-201-56317-7)p Black 2062 -37 a([9])p Black 44 w(D)t(.)46 b(L)t Fa(E)t(W)t(I)t(N)t(E)r FA(:)69 b Fw(POSIX)42 b(Pr)m(o)o(gr)o(ammer')m(s)i(Guide:)73 b(Writing)2191 54 y(P)-6 b(ortable)40 b(Unix)h(Pr)m(o)o(gr)o(ams)p FA(,)k(O'Reilly)40 b(&)g(Associates,Inc.,)2191 146 y(1994;)20 b(ISBN)d(0-937175-73-0)p Black 2025 277 a([10])p Black 44 w(B)t Fa(RY)m(A)t(N)145 b FA(O)t(')t(S)t Fa(U)t(L)t(L)t(I)t(V)l(A)t (N)r FA(:)267 b Fw(F)l(r)m(equently)144 b(ask)o(ed)2191 368 y(questions)196 b(for)g(comp.os.r)m(esear)m(c)o(h)p FA(,)242 b(1995;)2191 459 y(http://www)-5 b(.serpentine.com/)p Fh(\230)p FA(bos/os-f)o(aq/,)2191 551 y d2567 28 a2594 28 2025 681 a([11])p Black 44 w(S)t Fa(U)t(N)68 b FA(M)t Fa(I)t(C)t(R)r(O)t(S)t(Y)t(S)t(T)t(E)t(M)t(S)t FA(,)77 b(I)t Fa(N)t(C)r FA(:)115 b Fw(Thr)m(eads)67 b(F)l(r)m(equently)2191 773 y(Ask)o(ed)25 b(Questions)p FA(,)h(1995,)g(http://www.sun.com/w)o (orkshop/-)2191 864 y(threads/f)o(aq.html)p Black 2025 995 a([12])p Black 44 w(B)t Fa(RY)m(A)t(N)84 b FA(O)t(')t(S)t Fa(U)t(L)t(L)t(I)t(V)l(A)t(N)r FA(:)145 b Fw(F)l(r)m(equently)83 b(ask)o(ed)g(ques-)2191 1086 y(tions)144 b(for)g(comp.pr)m(o)o(gr)o (amming)o(.thr)m(eads)p FA(,)178 b(1997;)2191 1178 y(http://www)-5 b(.serpentine.com/)p Fh(\230)p FA(bos/threads-f)o(aq/.)p Black 2025 1308 a([13])p Black 44 w(B)t Fa(I)t(L)147 b FA(L)t Fa(E)t(W)t(I)t(S)r FA(:)273 b Fw(F)l(r)m(equently)145 b(ask)o(ed)h(ques-)2191 1400 y(tions)e(for)g(comp.pr)m(o)o(gr)o(amming) o(.thr)m(eads)p FA(,)178 b(1999;)2191 1491 y(http://www)-5 b(.lambdacs.com/ne)n(wsgroup/F)f(A)l(Q.html)p Black 2025 1622 a([14])p Black 44 w(N)t Fa(U)t(M)t(E)t(R)t(I)t(C)62 b FA(Q)s Fa(U)t(E)t(S)t(T)i FA(I)t Fa(N)t(C)r FA(:)107 b Fw(Multithr)m(eading)63 b(-)f(De\002ni-)2191 1713 y(tions)k(and)i (Guidelines)p FA(;)91 b(1998;)g(http://www)-5 b(.numeric-)2191 1805 y(quest.com/lang/multi-frame.html)p Black 2025 1936 a([15])p Black 44 w(T)t Fa(H)t(E)46 b FA(O)t Fa(P)t(E)t(N)g FA(G)t Fa(R)r(O)t(U)t(P)r FA(:)72 b Fw(The)44 b(Single)g(Unix)g (Speci\002cation,)2191 2027 y(V)-8 b(er)o(sion)51 b(2)g(-)g(Thr)m(eads) p FA(;)68 b(1997;)g(http://www.opengroup-)2191 2118 y(.or)o (g/onlinepubs/007908799/xsh/threads.html)p Black 2025 2249 a([16])p Black 44 w(S)t Fa(U)t(N)46 b FA(M)t Fa(I)t(C)t(R)r(O)t(S) t(Y)t(S)t(T)t(E)t(M)t(S)e FA(I)t Fa(N)t(C)r FA(:)72 b Fw(SMI)44 b(Thr)m(ead)h(Resour)m(ces)p FA(;)2191 2340 d2596 53 a2648 48 2025 2471 a([17])p Black 44 w(F)t Fa(R)t(A)t(N)t(K)33 b FA(M)t Fa(U)t(E)t(L)t(L)t(E)t(R)r FA(:)45 b Fw(FSU)31 b(pthr)m(eads)p FA(;)39 b(1997;)g(http://www-)2191 2563 y(.cs.fsu.edu/)p Fh(\230)p FA(mueller/pthreads/)p Black 2025 2693 a([18])p Black 44 w(C)t Fa(H)t(R)t(I)t(S)90 b FA(P)t Fa(R)r(O)q(V)t(E)t(N)t(Z)t(A)t(N)t(O)r FA(:)158 b Fw(MIT)88 b(pthr)m(eads)p FA(;)124 b(1993;)2191 2785 y(http://www)-5 b(.mit.edu/people/prov)o(en/pthreads.html)2191 2876 y(\(old\),)242 b(http://www.humanf)o(actor.com/pthreads/mit-)2191 2967 y(pthreads.html)19 b(\(updated\))p Black 2025 3098 a([19])p Black 44 w(K)r Fa(O)r(T)n(A)33 b FA(A)t Fa(B)t(E)r FA(:)45 b Fw(P)-6 b(ortable)32 b(Thr)m(eading)g(Libr)o(ary)f FA(\(PTL\);)e(1999;)2191 3190 y(http://www.media.osaka-cu.ac.jp/)p Fh(\230)p FA(k-abe/PTL/)p Black 2025 3321 a([20])p Black 44 w(R)t Fa(A)t(L)t(F)24 b FA(S)t(.)h(E)t Fa(N)t(G)t(E)t(L)t(S)t(C)t(H) t(A)t(L)t(L)r FA(:)f Fw(GNU)e(P)-6 b(ortable)23 b(Thr)m(eads)g FA(\(Pth\);)2191 3412 y(1999;)72 b(http://www.gnu.or)o(g/softw)o (are/pth/,)63 b(ftp://ftp.gnu-)2191 3503 y(.or)o(g/gnu/pth/)p Black 2025 3634 a([21])p Black 44 w(M)t Fa(I)t(C)t(H)t(A)t(E)t(L)40 b FA(T)n(.)j(P)t Fa(E)t(T)t(E)t(R)t(S)t(O)t(N)r FA(:)59 b Fw(POSIX)39 b(and)h(DCE)f(Thr)m(eads)2191 3725 y(F)-8 b(or)64 b(Linux)g FA(\(PCThreads\);)85 b(1995;)j(http://members.aa-) 2191 3817 y(.net/)p Fh(\230)p FA(mtp/PCthreads.html)p Black 2025 3948 a([22])p Black 44 w(R)q Fa(O)t(B)t(E)t(RT)31 b FA(S)t(.)i(T)t Fa(H)t(A)q(U)r FA(:)44 b Fw(Really)30 b(Simple)h(Thr)m(eads)g FA(\(rsthreads\);)2191 4039 y(1996;)20 b(ftp://ftp.ai.mit.edu/pub/rst/)p Black 2025 4170 a([23])p Black 44 w(J)t Fa(O)t(H)t(N)100 b FA(B)t Fa(I)t(R)t(R)t(E)t(L)t(L)r FA(:)175 b Fw(F)l(r)m(eeBSD)97 b(uthr)m(eads)p FA(;)138 b(1998;)2191 4261 y(ftp://ftp.freebsd.or)o(g/pub/FreeBSD/FreeBSD-)2191 4352 y(current/src/lib/libc)p 2747 4352 23 4 v 27 w(r/uthread/)p Black 2025 4483 a([24])p Black 44 w(X)t Fa(A)l(V)t(I)t(E)t(R)50 b FA(L)t Fa(E)t(R)r(O)q(Y)r FA(:)81 b Fw(The)49 b(LinuxThr)m(eads)i (libr)o(ary)p FA(;)64 b(1999;)2191 4575 y(http://pauillac.inria.fr/)p Fh(\230)p FA(xlero)o(y/linuxthreads/)p Black 2025 4706 a([25])p Black 44 w(I)t(B)t(M)r(:)23 b Fw(AIX)f(V)-8 b(er)o(sion)25 b(4.3)f(Gener)o(al)g(Pr)m(o)o(gr)o(amming)h(Concepts:) 2191 4797 y(Writing)63 b(and)h(Deb)o(ug)o(ging)g(Pr)m(o)o(gr)o(ams;)85 b(Under)o(standing)2191 4888 y(Thr)m(eads)p FA(;)58 b(1998;)g (http://www.rs6000.ibm.com/doc)p 3714 4888 V 28 w(link/-)2191 4980 y(en)p 2265 4980 V 27 w(US/a)p 2442 4980 V 26 w(doc)p 2575 4980 V 28 w(lib/aixpr)o(ggd/genprogc/understanding-)p 2191 5071 V 2213 5071 a(threads.htm)p Black 0 5400 a Fm(P)n(or)q(table)11 b(Multithreading)0 5458 y Fl(The)h(Signal)g(Stac)o (k)e(T)-5 b(r)q(ic)o(k)12 b(F)o(or)f(User-Space)g(Thread)h(Creation) 1929 5400 y Fs(9)1221 b Fl(Cop)o(yr)q(ight)3398 5399 y(c)3381 5400 y Fk(\015)11 b Fl(1999)g(Ralf)h(S)o(.)f(Engelschall)3524 5458 y(All)g(Rights)h(Reser)q(v)o(ed.)p Black eop @ 1.5.4.1 log @*** empty log message *** @ text @d3 2 a4 2 %%Title: pmt.dvi %%Pages: 11 d7 2 a8 2 %%DocumentFonts: Times-Bold Times-Roman Times-Italic Helvetica-Bold %%+ Courier Times-BoldItalic Courier-Bold Courier-BoldOblique Helvetica d11 1 a11 1 %DVIPSCommandLine: dvips -t letter pmt.dvi d13 1 a13 1 %DVIPSSource: TeX output 2000.06.18:1110 d316 31 a346 12 TeXDict begin 40258431 52099146 1000 600 600 (pmt.dvi) @@start /Fa 165[37 43 1[56 43 43 37 33 40 43 33 43 43 53 37 43 23 20 43 43 33 37 43 40 40 43 65[{ TeXBase1Encoding ReEncodeFont}25 59.7758 /Times-Roman rf /Fb 140[37 25 12[42 101[{TeXBase1Encoding ReEncodeFont}3 74.7198 /Helvetica rf %DVIPSBitmapFont: Fc cmr10 10 2 /Fc 2 51 df<15301578B3A6007FB812F8B912FCA26C17F8C80078C8FCB3A6153036367B AF41>43 D50 D E d348 18 a365 6 %DVIPSBitmapFont: Fd cmsy10 10 1 /Fd 1 3 df<0060150600F0150F6C151F007C153E6C157C6C15F86C6CEB01F06C6CEB03 E06C6CEB07C06C6CEB0F806C6CEB1F00017C133E6D5B6D5B90380F81F0903807C3E09038 03E7C06DB45A6D90C7FC147EA214FF497F903803E7C0903807C3E090380F81F049C67E01 3E137C497F497F4848EB0F804848EB07C04848EB03E04848EB01F048C812F8003E157C48 153E48151F48150F00601506282874A841>2 D E d367 51 a417 10 %DVIPSBitmapFont: Fe cmmi10 10 2 /Fe 2 79 df<1760177017F01601A21603A21607160FA24C7EA216331673166316C3A2ED 0183A2ED0303150683150C160115181530A21560A215C014011580DA03007FA202061300 140E140C5C021FB5FC5CA20260C7FC5C83495A8349C8FC1306A25BA25B13381330017016 80EA01F0D807F815FF267FFF80013F13FEB516FF18FE383C7DBB3E>65 D<902603FFF891381FFFF8495E6D6D80D90007030113006FEC007802061670814A6C1560 A26F7EA202186D5C151FA26F7E02304B5A6F7EA282DA60034AC7FCA26F7EA24A6C6C1306 A2828249485DEE3F80A217C049C7001F5B17E0160FA201066E6C5AA217F81603495EEE01 FCA2EE00FE495E17FF177FA2496F5AA2171F137095C8FC01F081EA07FC267FFFC080B56C 14064A140245397DB843>78 D E d419 18 a436 98 %DVIPSBitmapFont: Ff cmsy8 8 1 /Ff 1 3 df<006015C000E01401007014030078EC07806CEC0F006C141E6C5C6C6C5B6C 6C5B6C6C485A6C6C485A90387807806D48C7FCEB1E1E6D5AEB07F86D5A6D5A497E497EEB 0F3CEB1E1E497E496C7E496C7E48486C7E48486C7E4848137848C77E001E80488048EC07 800070EC03C048140100601400222376A137>2 D E %EndDVIPSBitmapFont /Fg 135[45 3[45 6[45 9[45 99[{TeXBase1Encoding ReEncodeFont}4 74.7198 /Courier-BoldOblique rf /Fh 135[40 40 2[40 40 2[40 40 40 40 40 40 40 40 40 40 1[40 1[40 1[40 12[40 1[40 1[40 40 40 4[40 1[40 25[40 45[{TeXBase1Encoding ReEncodeFont}25 66.4176 /Courier rf /Fi 133[45 45 1[45 45 45 45 45 45 1[45 45 45 1[45 2[45 45 1[45 45 45 45 1[45 1[45 59[45 35[{TeXBase1Encoding ReEncodeFont}21 74.7198 /Courier-Bold rf /Fj 133[29 33 37 50 33 42 21 29 29 1[37 37 42 58 21 37 21 21 42 37 25 33 37 33 37 37 25 11[46 42 24[25 37 37 37 37 37 37 37 37 37 37 1[19 6[25 58 35[42 2[{ TeXBase1Encoding ReEncodeFont}43 74.7198 /Times-BoldItalic rf /Fk 103[45 29[45 45 45 45 45 45 45 45 45 1[45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 1[45 9[45 45 45 45 2[45 45 45 45 45 1[45 45 45 45 45 45 45 45 45 2[45 45 1[45 9[45 45 45 45 45 45 1[45 45 45 1[45 4[45 33[{ TeXBase1Encoding ReEncodeFont}59 74.7198 /Courier rf /Fl 135[29 3[18 1[26 1[33 33 33 48 18 29 1[18 33 2[29 1[29 1[33 11[48 1[33 2[41 1[44 55 5[48 3[44 41 19[17 46[{TeXBase1Encoding ReEncodeFont}23 66.4176 /Times-Italic rf /Fm 134[33 33 48 33 33 18 26 22 1[33 33 33 52 18 33 18 18 33 33 22 29 33 29 33 29 3[22 1[22 2[48 63 48 48 41 37 44 1[37 48 48 59 41 48 1[22 48 48 37 41 48 44 44 48 6[18 33 33 33 33 33 33 33 33 33 33 18 17 22 17 2[22 22 37[37 2[{TeXBase1Encoding ReEncodeFont}66 66.4176 /Times-Roman rf /Fn 134[33 33 48 33 37 22 26 29 1[37 33 37 55 18 1[22 18 37 33 22 29 37 29 37 33 12[44 37 2[41 52 8[41 4[48 6[22 33 33 3[33 33 33 33 33 1[17 4[22 22 40[{TeXBase1Encoding ReEncodeFont}40 66.4176 /Times-Bold rf %DVIPSBitmapFont: Fo cmsy9 9 4 /Fo 4 111 df<126012F812FEEA7F80EA1FE0EA07F8EA01FE38007F80EB1FE0EB07F8EB 01FE9038007FC0EC1FF0EC07FCEC01FF9138003FC0ED0FF0ED03FCED00FFEE3FC0EE0FF0 EE03FCEE00FEA2EE03FCEE0FF0EE3FC0EEFF00ED03FCED0FF0ED3FC003FFC7FCEC07FCEC 1FF0EC7FC04948C8FCEB07F8EB1FE0EB7F80D801FEC9FCEA07F8EA1FE0EA7F8000FECAFC 12F81260CBFCAC007FB712FCB812FEA26C16FC2F3E7AB03C>21 D102 D<127CEAFFC0EA07F0EA01F8EA007C7F133F6D7EB3A66D7EA26D7E6D7E6D7E9038007FC0 EC1FE0EC7FC0903801F800495A495A495AA2495AB3A649C7FC133E5B485AEA07F0EAFFC0 007CC8FC1B4B7BB726>I<126012F0A21278A37EA37EA37EA36C7EA36C7EA26C7EA36C7E A31378A37FA37FA37FA36D7EA26D7EA36D7EA36D7EA31478A380A380A280A3EC0780A3EC 03C0A3EC01E0A3EC00F0A31578A215301D4B7CB726>110 D E %EndDVIPSBitmapFont /Fp 135[50 50 50 50 50 50 50 1[50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 7[50 1[50 50 50 50 50 50 1[50 50 50 2[50 1[50 50 50 1[50 1[50 1[50 12[50 2[50 2[50 50 50 1[50 50 50 4[50 35[{TeXBase1Encoding ReEncodeFont}49 83.022 /Courier rf /Fq 133[37 3[42 1[28 32 3[42 5[23 3[37 46 21[60 78 18[28 8[42 5[47 42 28 28 40[{ TeXBase1Encoding ReEncodeFont}16 83.022 /Times-Bold rf /Fr 133[32 37 37 55 37 42 23 32 32 1[42 42 42 60 23 37 1[23 42 42 23 37 42 37 42 42 8[51 1[51 60 46 42 51 1[51 60 55 69 46 2[28 1[60 51 51 60 55 51 51 3[56 2[28 42 42 1[42 42 42 1[42 42 1[23 21 28 42[42 2[{TeXBase1Encoding ReEncodeFont} 56 83.022 /Times-Italic rf /Fs 134[60 3[66 40 47 53 1[66 60 66 100 33 2[33 2[40 53 66 53 66 60 14[86 1[73 6[47 7[86 13[60 60 60 49[{TeXBase1Encoding ReEncodeFont}24 119.552 /Times-Bold rf /Ft 134[42 1[58 1[46 1[42 29 2[46 46 1[21 2[21 46 46 1[42 46 1[46 17[50 4[54 16[25 58[{ TeXBase1Encoding ReEncodeFont}17 74.7198 /Helvetica-Bold rf /Fu 134[33 33 50 33 37 21 29 29 37 37 37 37 54 21 33 1[21 37 37 21 33 37 33 37 37 8[46 62 46 54 42 37 46 54 46 54 50 62 42 2[25 1[54 46 46 54 50 46 46 5[25 25 5[37 37 37 37 37 1[19 25 19 4[25 36[37 2[{TeXBase1Encoding ReEncodeFont} 57 74.7198 /Times-Italic rf /Fv 133[33 37 37 54 37 37 21 29 25 37 37 37 37 58 21 37 21 21 37 37 25 33 37 33 37 33 3[25 1[25 2[54 71 54 54 46 42 50 54 42 54 54 66 46 54 29 25 54 54 42 46 54 50 50 54 5[21 21 37 37 37 37 37 37 37 37 37 37 21 19 25 19 42 1[25 25 25 58 34[42 42 2[{TeXBase1Encoding ReEncodeFont}75 74.7198 /Times-Roman rf /Fw 134[37 37 1[37 42 25 29 33 1[42 37 42 62 21 42 1[21 42 37 1[33 42 33 42 37 9[75 54 1[50 42 2[46 58 54 71 50 2[29 1[58 1[50 54 54 1[54 18[19 46[{TeXBase1Encoding ReEncodeFont} 37 74.7198 /Times-Bold rf /Fx 104[83 42 1[37 37 24[37 42 42 60 42 42 23 32 28 42 42 42 42 65 23 42 23 23 42 42 28 37 42 37 42 37 3[28 1[28 2[60 78 60 60 51 46 55 1[46 60 60 74 51 60 1[28 60 60 46 51 60 55 55 60 76 37 4[23 42 42 42 42 42 42 42 42 42 42 23 21 28 21 2[28 28 28 5[28 29[46 46 2[{TeXBase1Encoding ReEncodeFont}77 83.022 /Times-Roman rf /Fy 87[33 49[44 50 28 39 39 3[50 4[28 50 2[44 1[44 1[50 11[72 55 6[83 35[33 33 40[{ TeXBase1Encoding ReEncodeFont}17 99.6264 /Times-Italic rf /Fz 140[39 4[50 1[28 3[50 50 33 44 1[44 1[44 13[55 66 12[61 22[25 46[{TeXBase1Encoding ReEncodeFont}13 99.6264 /Times-Roman rf /FA 134[50 50 72 50 55 33 39 44 1[55 50 55 83 28 55 33 28 55 50 33 44 55 44 55 50 9[100 1[72 66 55 72 1[61 78 1[94 1[78 1[39 78 78 61 66 72 72 66 72 8[50 50 50 50 50 50 50 50 2[25 33 3[33 33 1[83 34[55 55 2[{TeXBase1Encoding ReEncodeFont}57 99.6264 /Times-Bold rf /FB 138[96 57 1[76 2[86 96 1[48 2[48 96 86 1[76 96 1[96 86 16[105 2[163 77[{TeXBase1Encoding ReEncodeFont}15 172.188 /Times-Bold rf end a440 3 %%BeginPaperSize: Letter letter %%EndPaperSize d444 81 a524 109 1 0 bop Black Black Black Black 1052 119 a FB(P)m(ortable)44 b(Multithr)m(eading)753 254 y FA(The)26 b(Signal)f(Stack)h(T)-7 b(rick)26 b(F)n(or)e(User)l(-Space)i(Thr)n(ead)h(Cr)n(eation)1567 472 y Fz(Ralf)e(S.)g(Engelschall)1148 595 y Fy(T)-9 b(ec)o(hnisc)o(he) 24 b(Univer)o(sit)1988 596 y(\250)1980 595 y(at)f(M)2172 596 y(\250)2164 595 y(unc)o(hen)h(\(TUM\))1109 718 y Fx(rse@@engelschall.com,)18 b(http://www)-5 b(.engelschall.com)p Black Black 1811 999 a Fw(Abstract)p Black Black 208 1136 a Fv(This)23 b(paper)h(describes)g(a)g(pragmatic)g(b)o(ut)f (portable)h(f)o(allback)h(approach)g(for)e(creating)h(and)h (dispatching)f(between)h(the)e(machine)208 1227 y(conte)o(xts)c(of)g (multiple)f(threads)h(of)g(e)o(x)o(ecution)h(on)f(Unix)g(systems)g (that)f(lack)h(a)f(dedicated)i(user)o(-space)f(conte)o(xt)h(switching)f (f)o(acility)-5 b(.)208 1318 y(Such)22 b(a)g(f)o(allback)h(approach)h (for)f(implementing)g(machine)g(conte)o(xts)g(is)f(a)g(vital)g(part)h (of)f(a)g(user)o(-space)h(multithreading)g(en)m(viron-)208 1410 y(ment,)g(if)e(it)h(has)h(to)f(achie)n(v)o(e)h(maximum)g (portability)g(across)g(a)f(wide)g(range)h(of)g(Unix)f(\003a)o(v)o (ors.)33 b(The)22 b(approach)i(is)e(entirely)g(based)208 1501 y(on)d(standard)h(Unix)g(system)f(f)o(acilities)g(and)h(ANSI-C)e (language)i(features)g(and)g(especially)g(does)g(not)f(require)h(an)o (y)f(assembly)h(code)208 1592 y(or)27 b(platform)h(speci\002c)f(tricks) g(at)h(all.)48 b(The)27 b(most)h(interesting)g(issue)f(is)g(the)h (technique)h(of)e(creating)h(the)g(machine)g(conte)o(xt)g(for)208 1684 y(threads,)18 b(which)g(this)f(paper)h(e)o(xplains)h(in)e(detail.) 23 b(The)17 b(described)i(approach)g(closely)f(follo)n(ws)g(the)g (algorithm)g(as)f(implemented)i(by)208 1775 y(the)h(author)h(for)f(the) g(popular)h(user)o(-space)g(multithreading)g(library)f Fu(GNU)f(P)-6 b(ortable)21 b(Thr)m(eads)g Fv(\()p Fu(GNU)e(Pth)p Fv(,)h([25)q(]\))f(which)i(this)f(w)o(ay)208 1866 y(quickly)f(gained)h (the)f(status)g(of)g(one)g(of)g(the)g(most)g(portable)h(user)o(-space)f (multithreading)h(libraries.)208 2007 y Ft(K)o(e)o(yw)o(or)o(ds:)e Fv(portability)-5 b(,)15 b(multithreading,)i(Unix,)e(POSIX,)f(SUSv2,)h (ANSI-C,)e(user)o(-space,)j(conte)o(xt)g(creation,)g(conte)o(xt)f (switch-)208 2099 y(ing,)j(signal)i(handler)m(,)f(stack,)g(mak)o (econte)o(xt,)h(switchconte)o(xt,)g(sigaltstack,)e(setjmp,)h(longjmp.) 208 2240 y Ft(Pub)o(lishing:)g Fv(Early)f(drafts)f(of)h(this)g(paper)g (were)g(distrib)o(uted)g(with)f(the)h(GNU)g(Pth)f(distrib)o(ution.)22 b(The)c(\002nal)g(release)g(v)o(ersion)g(w)o(as)208 2331 y(published)i(on)f(the)g(USENIX)f(Annual)h(T)-5 b(echnical)20 b(Conference,)f(June)h(18-23,)g(2000,)f(San)g(Die)o(go,)g(California,)f (USA.)0 2711 y Fs(1)119 b(Intr)n(oduction)0 2895 y FA(1.1)99 b(Multithr)n(eading)0 3052 y Fx(The)25 b(paradigm)d(of)j(programming)c (with)k(multiple)g(threads)f(of)0 3152 y(e)o(x)o(ecution)c(\(aka)i Fr(multithr)m(eading)p Fx(\))f(is)i(already)f(a)h(v)o(ery)e(old)h(one)0 3252 y(and)i(dates)g(back)g(to)g(the)h(decades)f(of)g(programming)c (with)25 b Fr(co-)0 3351 y(r)l(outines)19 b Fx([2,)g(3].)25 b(P)o(aradoxically)-5 b(,)17 b(the)i(use)h(of)f(threads)g(on)g(Unix)0 3451 y(platforms)g(did)h(not)g(become)f(popular)f(until)i(the)g(early)g (1990s.)0 3602 y FA(Multithr)n(eading)26 b(Adv)o(antages)0 3728 y Fx(Multithreading)31 b(can)i(pro)o(vide)f(man)o(y)g(bene\002ts)h (for)g(applica-)0 3828 y(tions)26 b(\(good)f(runtime)g(concurrenc)o(y) -5 b(,)24 b(parallel)i(programming)0 3927 y(techniques)j(can)h(be)g (implemented)e(more)i(easily)-5 b(,)32 b(the)e(popu-)0 4027 y(lar)22 b(procedural)d(programming)f(style)k(can)f(be)h(combined) d(with)0 4127 y(multiple)g(threads)f(of)h(e)o(x)o(ecution,)e Fr(etc.)p Fx(\))24 b(b)n(ut)c(the)f(most)g(interest-)0 4226 y(ing)25 b(ones)g(are)g(usually)g(performance)d(gains)j(and)f (reduced)g(re-)0 4326 y(source)17 b(consumption.)k(Because)d(in)g (contrast)f(to)h(multiprocess)0 4425 y(applications,)e(multithreaded)f (ones)i(usually)f(require)g(less)i(sys-)0 4525 y(tem)25 b(resources)f(\(mainly)g(memory\))e(and)j(their)f(internal)g(com-)0 4625 y(munication)33 b(part)i(can)g(le)n(v)o(erage)f(from)g(the)i (shared)e(address)0 4724 y(space.)0 4876 y FA(Multithr)n(eading)26 b(and)g(A)n(pplications)0 5001 y Fx(Ne)n(v)o(ertheless)d(there)h(still) h(e)o(xist)g(just)g(a)f(fe)n(w)h(real)f(applications)0 5101 y(in)35 b(the)f(free)g(softw)o(are)h(w)o(orld)f(that)g(use)h (multithreading)d(for)0 5201 y(their)20 b(bene\002t,)g(although)f (their)h(application)f(domains)h(are)g(pre-)0 5300 y(destined)32 b(for)f(multithreading.)59 b(F)o(or)32 b(instance,)j(the)d(popular)0 5400 y(Apache)38 b(webserv)o(er)g(as)i(of)f(v)o(ersion)f(1.3)h(still)h (uses)g(a)f(pre-)2025 2895 y(forking)32 b(process)h(model)g(for)h (serving)f(HTTP)h(requests,)i(al-)2025 2995 y(though)14 b(tw)o(o)i(e)o(xperiments)e(with)j(multithreaded)c(Apache)i(v)n(ari-) 2025 3095 y(ants)22 b(in)g(1996)e(\(with)i Fr(r)o(sthr)m(eads)g Fx([27)n(]\))g(and)f(1998)f(\(with)i Fr(NSPR)2025 3194 y Fx([31)o(]\))37 b(already)f(sho)n(wed)g(great)h(performance)d (boosts.)76 b(The)2025 3294 y(same)20 b(applies)g(to)h(man)o(y)e (similar)h(applications.)2150 3401 y(The)h(reason)g(for)g(this)i (restraint)e(mainly)g(is)i(that)f(for)f(a)h(long)2025 3500 y(time,)c(multithreading)e(f)o(acilities)j(under)d(Unix)i(were)g (rare.)24 b(The)2025 3600 y(situation)f(became)f(better)h(after)f(some) h(v)o(endors)f(lik)o(e)h Fr(Sun)f Fx(and)2025 3699 y Fr(DEC)f Fx(incorporated)15 b(threading)h(f)o(acilities)j(into)f(their) g(Unix)f(\003a-)2025 3799 y(v)n(ors)36 b(and)g Fr(POSIX)j Fx(standardized)c(a)i(threading)e Fr(Application)2025 3899 y(Pr)l(o)o(gr)o(amming)18 b(Interface)g Fx(\(API\))h(\(aka)f Fr(Pthr)m(eads)g Fx([1]\).)24 b(But)c(an)2025 3998 y(API)e(and)f(a)h (fe)n(w)f(v)o(endor)f(implementations)f(are)j(not)f(enough)e(to)2025 4098 y(ful\002ll)29 b(the)g(portability)e(requirements)g(of)h(modern)f (free)i(soft-)2025 4198 y(w)o(are)37 b(packages.)74 b(Here)37 b(stand-alone)f(and)g(really)h(portable)2025 4297 y(multithreading)18 b(en)m(vironments)f(are)j(needed.)2150 4404 y(The)49 b(author)g(collected)h(and)f(e)n(v)n(aluated)g(o)o(v)o(er)g(twenty)2025 4503 y(\(mostly)42 b(user)n(-space\))h(a)n(v)n(ailable)f (multithreading)f(f)o(acilities)2025 4603 y(for)15 b(Unix)g(systems)h (\(see)g(T)-7 b(able)16 b(1\),)g(b)n(ut)g(only)e(a)i(fe)n(w)g(of)f (them)h(are)2025 4703 y(freely)27 b(a)n(v)n(ailable)g(and)g(sho)n(wed)g (to)h(be)g(really)f(portable.)46 b(And)2025 4802 y(e)n(v)o(en)19 b(the)i(mostly)f(portable)f(ones)h(suf)n(fered)f(from)g(the)i(f)o(act)f (that)2025 4902 y(the)o(y)31 b(partly)g(depend)f(on)h(assembly)g(code)g (or)h(platform)e(spe-)2025 5001 y(ci\002c)e(tricks)f(usually)g(related) g(to)h(the)f(creation)g(and)g(dispatch-)2025 5101 y(ing)d(of)h(the)g (indi)n(vidual)e(threads.)38 b(This)25 b(means)f(that)h(the)g(num-)2025 5201 y(ber)c(of)h(platforms)f(the)o(y)g(support)f(is)j(limited)e(and)h (applications)2025 5300 y(which)27 b(are)h(based)g(on)g(these)g(f)o (acilities)h(are)f(only)f(portable)f(to)2025 5400 y(those)c(platforms.) 29 b(This)22 b(situation)f(is)i(not)f(satisf)o(actory)-5 b(,)21 b(so)h(ap-)p Black 1929 5700 a(1)p Black eop d526 131 a656 133 2 1 bop Black Black 0 83 a Fx(plication)25 b(authors)g(still)j(a)n(v)n (oid)d(the)i(use)f(of)g(multithreading)d(if)0 183 y(the)o(y)28 b(w)o(ant)g(to)h(\(or)f(ha)n(v)o(e)g(to\))g(achie)n(v)o(e)f(maximum)g (portability)0 282 y(for)20 b(their)f(application.)125 387 y(A)25 b(pragmatic)e(and)h(mostly)g(portable)f(f)o(allback)h (technique)0 486 y(for)d(implementing)f(user)n(-space)h(threads)g(can)h (f)o(acilitate)g(wider)0 586 y(use)e(of)g(multithreading)e(in)i(free)g (softw)o(are)g(applications.)0 750 y FA(Ingr)n(edients)26 b(of)f(a)g(Thr)n(ead)0 884 y Fx(A)h(Unix)f(process)f(has)i(man)o(y)e (ingredients,)h(b)n(ut)g(the)g(most)h(im-)0 983 y(portant)k(ones)h(are) h(its)g(memory)e(mapping)f(table,)34 b(the)e(signal)0 1083 y(dispatching)18 b(table,)h(the)g(signal)g(mask,)g(the)g(set)h(of) f(\002le)h(descrip-)0 1183 y(tors)30 b(and)f(the)h(machine)e(conte)o (xt.)52 b(The)29 b(machine)g(conte)o(xt)f(in)0 1282 y(turn)d(consists)i (of)e(at)i(least)g(the)f(CPU)h(re)o(gisters)e(including)g(the)0 1382 y(program)18 b(counter)h(and)h(the)h(stack)f(pointer)-5 b(.)25 b(In)20 b(addition,)f(there)0 1482 y(can)28 b(be)f(light-weight) g(processes)g(\(L)-6 b(WP\))28 b(or)f(threads,)i(which)0 1581 y(usually)g(share)g(all)h(attrib)n(utes)f(with)g(the)h(underlying) c(\(hea)n(vy-)0 1681 y(weight\))19 b(process)h(e)o(xcept)f(for)h(the)g (machine)f(conte)o(xt.)0 1845 y FA(K)n(er)o(nel-Space)25 b(vs.)31 b(User)l(-Space)0 1979 y Fx(Those)c(L)-6 b(WPs)28 b(or)f(threads,)h(on)e(a)i(Unix)f(platform)e(classically)0 2078 y(can)34 b(be)g(implemented)e(either)i(in)g(k)o(ernel-space)f(or)g (in)i(user)n(-)0 2178 y(space.)76 b(When)38 b(implemented)d(in)j(k)o (ernel-space,)h(one)e(usu-)0 2278 y(ally)22 b(calls)h(them)e(L)-6 b(WPs)23 b(or)e(k)o(ernel)g(threads,)h(otherwise)f(\(user)n(-)0 2377 y(space\))d(threads.)24 b(If)19 b(threads)f(are)h(implemented)d (by)j(the)g(k)o(ernel,)0 2477 y(the)24 b(thread)g(conte)o(xt)f (switches)h(are)h(performed)c(by)j(the)g(k)o(ernel)0 2576 y(without)17 b(notice)f(by)h(the)g(application,)f(similar)i(to)f (the)h(dispatch-)0 2676 y(ing)33 b(of)g(processes.)64 b(If)33 b(threads)g(are)g(implemented)e(in)j(user)n(-)0 2776 y(space,)20 b(the)g(thread)f(conte)o(xt)g(switches)i(are)f (performed)d(usually)0 2875 y(by)31 b(an)g(application)f(library)g (without)h(notice)f(by)h(the)h(k)o(ernel.)0 2975 y(Additionally)-5 b(,)39 b(there)e(e)o(xist)h(hybrid)d(threading)h(approaches,)0 3075 y(where)28 b(typically)g(a)h(user)n(-space)g(library)e(binds)i (one)f(or)g(more)0 3174 y(user)n(-space)19 b(threads)h(to)g(one)g(or)g (more)f(k)o(ernel-space)g(L)-6 b(WPs.)0 3338 y FA(Thr)n(ead)27 b(Models)0 3472 y Fx(The)h(v)o(endor)d(threading)h(f)o(acilities)j (under)d Fr(Sun)h(Solaris)p Fx(,)i Fr(IBM)0 3572 y(AIX)p Fx(,)h Fr(DEC)h(T)-5 b(ru64)31 b Fx(\(formerly)d Fr(DIGIT)l(AL)i(UNIX)k Fx(or)d Fr(OSF/1)p Fx(\))0 3671 y(and)20 b Fr(SGI)h(IRIX)i Fx(use)e(a)h Fq(M:N)f Fx(mapping)e([21)o(,)i(30)o(],)g Fr(i.e)o(.)p Fx(,)f Fq(M)i Fx(user)n(-)0 3771 y(space)33 b(threads)g(are)h(mapped)e(onto)g Fq(N)i Fx(k)o(ernel-space)e(L)-6 b(WPs.)0 3871 y(On)20 b(the)h(other)e(hand,)g Fr(LinuxThr)m(eads)g Fx([29)o(])i(under)e Fr(GNU/Linux)0 3970 y Fx(uses)f(a)f Fq(1:1)g Fx(mapping)e(and)i(pure)f(user)n(-space)h(implementations)0 4070 y(lik)o(e)k Fr(GNU)g(Pth)p Fx(,)f Fr(FSU)g(pthr)m(eads)f Fx(or)i Fr(MIT)f(pthr)m(eads)p Fx(,)g Fr(etc.)26 b Fx(use)20 b(a)0 4170 y Fq(M:1)g Fx(mapping)f([25)n(,)i(22)o(,)f(23)o(].)125 4274 y(From)39 b(no)n(w)g(on)h(we)g(focus)g(on)f(such)h Fq(M:1)g Fx(user)g(space)0 4374 y(threading)d(approaches,)42 b(where)c(one)h(or)f(more)h(user)g(space)0 4473 y(threads)16 b(are)g(implemented)f(inside)i(a)g(single)f(k)o(ernel)g(space)h(pro-)0 4573 y(cess.)24 b(The)15 b(e)o(x)o(ercise)f(is)i(to)g(implement)e(this) i(by)e(using)h(standard-)0 4672 y(ized)20 b(Unix)g(system)g(and)g (ANSI-C)g(language)f(f)o(acilities)i Fr(only)p Fx(.)0 4936 y FA(1.2)99 b(The)26 b(Exer)n(cise)0 5101 y Fx(As)d(we)g(ha)n(v)o (e)f(mentioned,)e(a)j(thread)e(shares)i(its)g(state)g(with)g(the)0 5201 y(underlying)i(process)j(e)o(xcept)f(for)g(the)h(machine)f(conte)o (xt.)46 b(So)0 5300 y(the)17 b(major)e(task)i(for)f(a)h(user)n(-space)f (threading)e(system)j(is)g(to)g(cre-)0 5400 y(ate)k(and)e(dispatch)h (those)g(machine)f(conte)o(xts.)2150 83 y(In)d(practice,)g(the)h (second)f(major)g(task)h(it)g(has)g(to)g(do)f(is)i(to)f(en-)2025 183 y(sure)k(that)f(no)h(thread)f(by)g(accident)g(blocks)g(the)h(whole) f(process)2025 282 y(\(and)25 b(thereby)f(all)i(other)f(threads\).)41 b(Instead)25 b(when)g(an)h(opera-)2025 382 y(tion)j(w)o(ould)f(block,)i (the)f(threading)e(library)h(should)g(suspend)2025 482 y(only)19 b(the)g(e)o(x)o(ecution)e(of)i(the)g(current)g(thread)f(and)h (in)g(the)h(mean-)2025 581 y(time)g(dispatch)f(the)h(remaining)e (threads.)24 b(But)d(this)f(task)g(is)h(out-)2025 681 y(side)30 b(the)h(scope)e(of)h(this)h(paper)e(\(see)h([11)o(])h(for)e (details)i(about)2025 780 y(this)24 b(task\).)33 b(W)-7 b(e)24 b(focus)f(only)f(on)h(the)g(aspect)g(of)g(machine)e(con-)2025 880 y(te)o(xt)f(handling.)2025 1112 y FA(1.3)99 b(The)26 b(Curse)g(of)e(P)n(ortability)2025 1267 y Fx(Our)18 b(goal)g(of)f(real) i(portability)d(for)i(a)h(threading)d(system)i(causes)2025 1367 y(some)j(non-tri)n(vial)d(problems)i(which)g(ha)n(v)o(e)g(to)h(be) g(solv)o(ed.)26 b(The)2025 1467 y(most)f(ob)o(vious)e(one)h(is)i(that)f (dealing)f(with)h(machine)f(conte)o(xts)2025 1566 y(usually)33 b(suf)n(fers)g(from)f(portability)-5 b(,)35 b(because)d(it)j(is)f(a)g (highly)2025 1666 y(CPU)18 b(dependent)d(task)i(for)f(which)g(not)h(e)n (v)o(ery)e(Unix)i(\003a)n(v)n(or)g(pro-)2025 1766 y(vides)25 b(a)g(standardized)e(API.)i(Although)e(such)i(an)g(API)g(w)o(ould)2025 1865 y(be)f(not)h(too)f(hard)f(for)h(v)o(endors)f(to)i(pro)o(vide,)e (because)h(in)h(prin-)2025 1965 y(ciple)d(it)h(is)g(just)g(a)g(matter)e (of)h(switching)g(a)h(fe)n(w)f(CPU)h(re)o(gisters)2025 2064 y(\(mainly)c(the)h(program)e(counter)h(and)h(the)g(stack)g (pointer\).)2025 2201 y FA(Assembly)k(Code)i(Consider)n(ed)g(Harmful) 2025 2319 y Fx(Additionally)-5 b(,)21 b(we)j(disallo)n(w)e(the)h(use)h (of)e(an)o(y)g(assembly)h(solu-)2025 2418 y(tions)c(or)g(platform)e (speci\002c)j(tricks,)f(because)f(then)h(the)g(thread-)2025 2518 y(ing)e(system)i(again)d(w)o(ould)h(be)h(only)f(semi-portable,)f Fr(i.e)o(.)p Fx(,)i(it)h(can)2025 2617 y(be)e(ported)f(to)i Fq(N)f Fx(platforms)f(b)n(ut)i(on)f(the)g Fq(\(N+1\))p Fx(th)f(platform)g(one)2025 2717 y(has)28 b(to)g(manually)e(adjust)i (or)f(e)n(v)o(en)g(e)o(xtend)f(it)i(to)g(w)o(ork)f(there,)2025 2817 y(too.)2150 2916 y(This)e(is)h(usually)f(not)g(acceptable,)h(e)n (v)o(en)e(if)i(it)g(also)g(mak)o(es)2025 3016 y(solving)20 b(the)h(problems)f(harder)-5 b(.)26 b(At)c(least)f(most)g(of)g(the)g (kno)n(wn)2025 3116 y(free)26 b(softw)o(are)g(user)n(-space)g (threading)f(systems)i([22)o(,)f(23)o(,)h(24)o(])2025 3215 y(do)15 b(not)g(restrict)h(themself)e(to)i(this)g(and)f(therefore) e(are)j(just)g(semi-)2025 3315 y(portable.)24 b(But)c(real)g (portability)f(should)g(be)h(a)h(major)f(goal.)2025 3589 y Fs(2)119 b(Pr)n(oblem)30 b(Analysis)2025 3791 y FA(2.1)99 b(The)26 b(T)-9 b(ask)25 b(in)g(Detail)2025 3947 y Fx(Our)d(task)h(is)g (simple)f(in)h(principle:)28 b(pro)o(vide)20 b(an)j(API)f(and)g(cor)n (-)2025 4046 y(responding)f(implementation)f(for)j(creating)f(and)g (dispatching)2025 4146 y(machine)15 b(conte)o(xts)g(on)h(which)g(user)n (-space)g(threads)f(can)h(be)h(im-)2025 4246 y(plemented.)2025 4382 y FA(The)26 b(Pr)n(oposed)g(API)2025 4500 y Fx(In)18 b(detail)g(we)g(propose)f(the)h(follo)n(wing)e Fr(Application)g(Pr)l(o) o(gr)o(am-)2025 4599 y(mer)o(s)21 b(Interface)f Fx(\(API\))g(for)f(the) h(machine)f(conte)o(xt)g(handling:)p Black 2120 4749 50 50 v Black 2233 4749 a(A)25 b(data)g(structure)f(of)g(type)h Fp(mctx)p 3266 4749 25 4 v 29 w(t)g Fx(which)f(holds)h(the)2233 4849 y(machine)19 b(conte)o(xt.)p Black 2120 5001 50 50 v Black 2233 5001 a(A)95 b(function)e(\223)p Fq(v)o(oid)h Fp(mctx)p 3248 5001 25 4 v 29 w(create\(mctx)p 3827 5001 V 28 w(t)2233 5101 y(*)p Fr(mctx)p Fx(,)26 b Fq(v)o(oid)g Fp(\(*)p Fr(sf)p 2825 5101 V 30 w(addr)r Fp(\)\()p Fq(v)o(oid)e(*)p Fp(\),)i Fq(v)o(oid)g Fp(*)p Fr(sf)p 3742 5101 V 30 w(ar)m(g)p Fx(,)2233 5201 y Fq(v)o(oid)c Fp(*)p Fr(sk)p 2531 5201 V 30 w(addr)p Fx(,)g Fq(size)p 2891 5201 V 30 w(t)h Fr(sk)p 3041 5201 V 30 w(size)p Fp(\))p Fx(\224)g(which)g(creates)f(and)2233 5300 y(initializes)17 b(a)h(machine)e(conte)o(xt)f(structure)h(in)i Fr(mctx)f Fx(with)2233 5400 y(a)25 b(start)g(function)e Fr(sf)p 2825 5400 V 31 w(addr)p Fx(,)i(a)g(start)h(function)d(ar)o (gument)p Black 1929 5700 a(2)p Black eop d658 59 a716 607 3 2 bop Black Black Black 0 3 3864 4 v 0 2441 4 2439 v 68 642 a Fw(P)o(ackage)553 642 y gsave currentpoint currentpoint translate 45 neg rotate neg exch neg exch translate 553 642 a Fw(Genesis)553 642 y currentpoint grestore moveto 553 642 a 744 642 a gsave currentpoint currentpoint translate 45 neg rotate neg exch neg exch translate 744 642 a Fw(Latest)19 b(V)-7 b(ersion)744 642 y currentpoint grestore moveto 744 642 a 1010 642 a gsave currentpoint currentpoint translate 45 neg rotate neg exch neg exch translate 1010 642 a Fw(Implementation)18 b(Space)1010 642 y currentpoint grestore moveto 1010 642 a 1404 642 a gsave currentpoint currentpoint translate 45 neg rotate neg exch neg exch translate 1404 642 a Fw(Thr)o(ead)g (Mapping)1404 642 y currentpoint grestore moveto 1404 642 a 1562 642 a gsave currentpoint currentpoint translate 45 neg rotate neg exch neg exch translate 1562 642 a Fw(Acti)o(v)o(e)h(De)o(v)o(elopment)1562 642 y currentpoint grestore moveto 1562 642 a 1703 642 a gsave currentpoint currentpoint translate 45 neg rotate neg exch neg exch translate 1703 642 a Fw(Experimental)f(State)1703 642 y currentpoint grestore moveto 1703 642 a 1844 642 a gsave currentpoint currentpoint translate 45 neg rotate neg exch neg exch translate 1844 642 a Fw(Open)g(Sour)o(ce)1844 642 y currentpoint grestore moveto 1844 642 a 1985 642 a gsave currentpoint currentpoint translate 45 neg rotate neg exch neg exch translate 1985 642 a Fw(Pthr)o(ead)g(API)1985 642 y currentpoint grestore moveto 1985 642 a 2126 642 a gsave currentpoint currentpoint translate 45 neg rotate neg exch neg exch translate 2126 642 a Fw(Pthr)o(ead)g(Shar)o(ed)g (Memory)2126 642 y currentpoint grestore moveto 2126 642 a 2267 642 a gsave currentpoint currentpoint translate 45 neg rotate neg exch neg exch translate 2267 642 a Fw(Nati)o(v)o(e)i (API)2267 642 y currentpoint grestore moveto 2267 642 a 2408 642 a gsave currentpoint currentpoint translate 45 neg rotate neg exch neg exch translate 2408 642 a Fw(Nati)o(v)o(e)g(API) e Fo(\025)h Fw(Pthr)o(ead)f(API)2408 642 y currentpoint grestore moveto 2408 642 a 2550 642 a gsave currentpoint currentpoint translate 45 neg rotate neg exch neg exch translate 2550 642 a Fw(Nati)o(v)o(e)h(API)g(is)f(Pthr)o(ead)g(API) 2550 642 y currentpoint grestore moveto 2550 642 a 2765 642 a gsave currentpoint currentpoint translate 45 neg rotate neg exch neg exch translate 2765 642 a Fw(Pr)o(eempti)o(v)o(e)h (Scheduling)2765 642 y currentpoint grestore moveto 2765 642 a 2906 642 a gsave currentpoint currentpoint translate 45 neg rotate neg exch neg exch translate 2906 642 a Fw(P)o(ortability)2906 642 y currentpoint grestore moveto 2906 642 a 3360 642 a gsave currentpoint currentpoint translate 45 neg rotate neg exch neg exch translate 3360 642 a Fw(Assembly)f(Code)3360 642 y currentpoint grestore moveto 3360 642 a 3509 642 a gsave currentpoint currentpoint translate 45 neg rotate neg exch neg exch translate 3509 642 a Fw(SysCall)g(Wrap.)3509 642 y currentpoint grestore moveto 3509 642 a 47 673 3658 4 v 68 737 a Fv(gnu-pth)254 b(1999)43 b(1.3.5)117 b(user)270 b(n:1)63 b(yes)42 b(no)67 b(yes)42 b(yes)g(no)67 b(yes)42 b(yes)h(no)141 b(no)67 b(full/mcsc+sjlj)42 b(no)75 b(partly)68 820 y(cmu-l)o(wp)221 b(1984)43 b(1.4)173 b(user)270 b(n:1)63 b(yes)42 b(no)67 b(yes)42 b(no)67 b(-)116 b(yes)42 b(yes)h(partly)e(no)67 b(semi/\002x)o(ed:8)86 b(yes)50 b(no)68 903 y(fsu-pthread)146 b(1992)43 b(3.13)136 b(user)270 b(n:1)63 b(no)k(no)g(yes)42 b(yes)g(no)67 b(no)g(-)117 b(-)190 b(yes)42 b(semi/\002x)o(ed:6)86 b(yes)50 b(yes)68 986 y(mit-pthread)137 b(1993)43 b(1.8.9)117 b(user)270 b(n:1)63 b(no)k(no)g(yes)42 b(yes)g(no)67 b(no)g(-)117 b(-)190 b(yes)42 b(semi/\002x)o(ed:17)49 b(yes)h(yes)68 1069 y(ptl)406 b(1997)43 b(990622)h(user)270 b(n:1)63 b(no)k(no)g(yes)42 b(yes)g(no)67 b(no)g(-)117 b(-)190 b(yes)42 b(semi/\002x)o(ed:10)49 b(yes)h(yes)68 1152 y(linuxthreads)117 b(1997)43 b(2.1.2)117 b(user+k)o(ernel)43 b(1:1)63 b(yes)42 b(no)67 b(yes)42 b(yes)g(no)67 b(no)g(-)117 b(-)190 b(yes)42 b(semi/\002x)o(ed:5)86 b(yes)50 b(yes)68 1235 y(uthread)262 b(1998)43 b(3.4)173 b(user)270 b(n:1)63 b(yes)42 b(no)67 b(yes)42 b(yes)g(no)67 b(no)g(-)117 b(-)190 b(yes)42 b(semi/\002x)o(ed:2)86 b(yes)50 b(yes)68 1318 y(cthread)266 b(1991)43 b(991115)h(user)270 b(n:1)63 b(no)k(no)g(yes)42 b(no)67 b(-)116 b(yes)42 b(yes)h(no)141 b(no)67 b(semi/\002x)o(ed:8)86 b(yes)50 b(yes)68 1401 y(openthreads/qt)d(1996)c(2.0)173 b(user)270 b(n:1)63 b(no)k(no)g(yes)42 b(no)67 b(-)116 b(yes)42 b(no)68 b(no)141 b(no)67 b(semi/\002x)o(ed:9)86 b(yes)50 b(no)68 1484 y(rt++/qt)276 b(1996)43 b(1.0)173 b(user)270 b(n:1)63 b(no)k(no)g(yes)42 b(no)67 b(-)116 b(yes)42 b(yes)h(no)141 b(no)67 b(semi/\002x)o(ed:9)86 b(yes)50 b(no)68 1567 y(rsthreads)216 b(1996)43 b(980331)h(user)270 b(n:1)63 b(no)k(yes)42 b(yes)g(no)67 b(-)116 b(yes)42 b(no)68 b(no)141 b(no)67 b(semi/\002x)o(ed:9)86 b(yes)50 b(no)68 1650 y(pcthread)229 b(1996)43 b(1.0)173 b(user)270 b(n:1)63 b(no)k(yes)42 b(yes)g(yes)g(no)67 b(no)g(-)117 b(-)190 b(yes)42 b(semi/\002x)o(ed:1)86 b(yes)50 b(no)68 1733 y(bbthreads)196 b(1996)43 b(0.3)173 b(k)o(ernel)209 b(1:1)63 b(no)k(yes)42 b(yes)g(no)67 b(-)116 b(yes)42 b(no)68 b(-)190 b(yes)42 b(semi/\002x)o(ed:1)86 b(yes)50 b(no)68 1816 y(jkthreads)212 b(1998)43 b(1.2)173 b(k)o(ernel)209 b(1:1)63 b(no)k(yes)42 b(yes)g(no)67 b(-)116 b(yes)42 b(no)68 b(-)190 b(yes)42 b(semi/\002x)o(ed:1)86 b(yes)50 b(no)68 1899 y(nthreads)233 b(1997)43 b(970604)h(user)270 b(n:1)63 b(no)k(yes)42 b(yes)g(no)67 b(-)116 b(yes)42 b(no)68 b(-)190 b(no)67 b(semi/\002x)o(ed:9)86 b(yes)50 b(partly)68 1982 y(re)o(xthreads)176 b(1993)43 b(930614)h(user)270 b(n:1)63 b(no)k(yes)42 b(yes)g(no)67 b(-)116 b(yes)42 b(no)68 b(-)190 b(no)67 b(semi/\002x)o(ed:4)86 b(yes)50 b(no)68 2065 y(coro)353 b(1999)43 b(1.0.3)117 b(user)270 b(n:1)63 b(no)k(yes)42 b(yes)g(no)67 b(-)116 b(yes)42 b(no)68 b(-)190 b(no)67 b(semi/\002x)o(ed:1)86 b(yes)50 b(no)68 2148 y(greenthreads)105 b(1995)43 b(1.2)173 b(user)270 b(n:1)63 b(no)k(no)g(no)g(no)g(-)116 b(yes)42 b(yes)h(-)190 b(yes)42 b(full/mcsc)176 b(no)75 b(no)68 2231 y(solaris-pthread)42 b(NN)83 b(2.7)173 b(user+k)o(ernel)43 b(n:m)f(yes)g(no)67 b(no)g(yes)42 b(yes)g(yes)g(yes)h(no)141 b(yes)42 b(NN)346 b(NN)41 b(yes)68 2314 y(tru64-pthread)80 b(NN)j(5.0)173 b(user+k)o(ernel)43 b(n:m)f(yes)g(no)67 b(no)g(yes)42 b(yes)g(no)67 b(no)h(no)141 b(yes)42 b(NN)346 b(NN)41 b(yes)68 2397 y(aix-pthread)146 b(NN)83 b(4.3)173 b(user+k)o(ernel)43 b(1:1)63 b(yes)42 b(no)67 b(no)g(yes)42 b(yes)g(no)67 b(no)h(no)141 b(yes)42 b(NN)346 b(NN)41 b(yes)p 3860 2441 4 2439 v 0 2444 3864 4 v 0 2532 a Fn(T)-6 b(able)18 b(1:)67 b Fm(Summary)20 b(of)h(e)n(v)n(aluated)j(multithreading)g (packages)f(and)e(some)g(of)f(their)i(determined)h(characteristics.)36 b(Notice)22 b(that)g(mostly)f(all)h(packages)g(contain)295 2600 y(assembly)17 b(code)h(and)g(are)g(just)f(semi-portable,)j Fl(i.e)o(.)p Fm(,)c(the)o(y)i(support)g(only)g(a)f(\002x)o(ed)g(set)g (of)g(platforms)i(and)e(do)g(not)h(automatically)j(adjust)d(for)f(ne)n (w)h(ones.)p Black 208 2864 a Fr(sf)p 268 2864 25 4 v 30 w(ar)m(g)p Fx(,)36 b(and)d(a)h(stack)f(starting)g(at)h Fr(sk)p 1343 2864 V 30 w(addr)p Fx(,)i(which)d(is)208 2963 y Fr(sk)p 282 2963 V 30 w(size)21 b Fx(bytes)f(in)g(size.)p Black 95 3115 50 50 v Black 208 3115 a(A)i(function)f(\223)p Fq(v)o(oid)h Fp(mctx)p 1006 3115 25 4 v 30 w(save\(mctx)p 1486 3115 V 28 w(t)h(*)p Fr(mctx)p Fp(\))p Fx(\224)208 3215 y(which)j(sa)n(v)o(es)i(the)g(current)e(machine)g(conte)o(xt)g (into)i(the)208 3314 y(machine)19 b(conte)o(xt)g(structure)g Fr(mctx)p Fx(.)p Black 95 3466 50 50 v Black 208 3466 a(A)78 b(function)e(\223)p Fq(v)o(oid)i Fp(mctx)p 1173 3466 25 4 v 30 w(restore\(mctx)p 1803 3466 V 27 w(t)208 3566 y(*)p Fr(mctx)p Fp(\))p Fx(\224)40 b(which)g(restores)g(the)h(ne)n (w)f(machine)f(con-)208 3666 y(te)o(xt)18 b(from)g(the)h(machine)e (conte)o(xt)h(structure)g Fr(mctx)p Fx(.)24 b(This)208 3765 y(function)29 b(does)i(not)g(return)f(to)h(the)h(caller)-5 b(.)58 b(Instead)31 b(it)208 3865 y(does)18 b(return)g(at)i(the)f (location)g(stored)f(in)i Fr(mctx)f Fx(\(which)g(is)208 3964 y(either)d Fr(sf)p 474 3964 V 30 w(addr)i Fx(from)e(a)h(pre)n (vious)f Fp(mctx)p 1415 3964 V 29 w(create)g Fx(call)208 4064 y(or)j(the)i(location)e(of)h(a)g(pre)n(vious)f Fp(mctx)p 1365 4064 V 29 w(save)h Fx(call\).)p Black 95 4216 50 50 v Black 208 4216 a(A)95 b(function)e(\223)p Fq(v)o(oid)h Fp(mctx)p 1223 4216 25 4 v 29 w(switch\(mctx)p 1802 4216 V 28 w(t)208 4316 y(*)p Fr(mctx)p 420 4316 V 29 w(old)p Fx(,)19 b Fp(mctx)p 796 4316 V 29 w(t)h(*)p Fr(mctx)p 1102 4316 V 30 w(ne)o(w)p Fp(\))p Fx(\224)f(which)g(switches)208 4415 y(from)59 b(the)i(current)e(machine)g(conte)o(xt)h(\(sa)n(v)o(ed)f (to)208 4515 y Fr(mctx)p 370 4515 V 29 w(old)28 b Fx(for)e(later)g (use\))g(to)g(a)h(ne)n(w)f(conte)o(xt)f(\(restored)208 4615 y(from)c Fr(mctx)p 554 4615 V 30 w(ne)o(w)p Fx(\).)31 b(This)22 b(function)f(returns)g(only)h(to)h(the)208 4714 y(caller)39 b(if)h Fp(mctx)p 728 4714 V 30 w(restore)f Fx(or)g Fp(mctx)p 1456 4714 V 29 w(switch)h Fx(is)208 4814 y(again)19 b(used)h(on)f Fr(mctx)p 846 4814 V 30 w(old)p Fx(.)0 5045 y FA(2.2)99 b(T)-9 b(echnical)26 b(P)n(ossibilities)0 5201 y Fx(Poking)18 b(around)f(in)i(the)f (references)g(of)g(the)h(ANSI-C)g(language)0 5300 y(reference)36 b(and)i(the)g(Unix)f(standards)g(sho)n(w)h(the)g(follo)n(wing)0 5400 y(functions)19 b(on)h(which)f(an)h(implementation)e(can)i(be)g (based:)p Black 2120 2864 50 50 v Black 2233 2864 a(There)55 b(is)h(the)g Fp(ucontext)p Fx(\(3\))e(f)o(acility)i(with)g(the)2233 2963 y(functions)34 b Fp(getcontext)p Fx(\(3\),)j Fp(makecontext)p Fx(\(3\),)2233 3063 y Fp(swapcontext)p Fx(\(3\))31 b(and)j Fp(setcontext)p Fx(\(3\))e(which)2233 3162 y(conform)21 b(to)j(the)g Fr(Single)f(Unix)h(Speci\002cation)p Fx(,)e(V)-9 b(ersion)2233 3262 y(2)38 b(\()p Fr(SUSv2)e Fx([20)o(],)42 b(aka)c Fr(Unix95/98)p Fx(\).)76 b(Unfortunately)2233 3362 y(these)16 b(are)g(a)n(v)n(ailable)f(on)h(modern)e(Unix)h (platforms)g(only)-5 b(.)p Black 2120 3625 V Black 2233 3625 a(There)87 b(are)h(the)h Fp(jmp)p 3051 3625 25 4 v 29 w(buf)f Fx(based)g(functions)2233 3724 y Fp(setjmp)p Fx(\(3\))29 b(and)h Fp(longjmp)p Fx(\(3\))f(which)i(conform)d(to)2233 3824 y(ISO)41 b(9899:1990)c(\(ISO-C\))k(and)f(the)h Fp(sigjmp)p 3727 3824 V 29 w(buf)2233 3924 y Fx(based)71 b Fp(sigsetjmp)p Fx(\(3\))f(and)i Fp(siglongjmp)p Fx(\(3\))2233 4023 y(functions)17 b(which)h(conform)f(to)i(IEEE)f(Std1003.1-1988)2233 4123 y(\()p Fr(POSIX)t Fx(\),)f(and)j Fr(Single)f(Unix)h(Speci\002cation)p Fx(,)d(V)-9 b(ersion)19 b(2)2233 4222 y(\()p Fr(SUSv2)j Fx([20)o(],)j(aka)f Fr(Unix95/98)p Fx(\).)35 b(The)24 b(\002rst)h(tw)o(o)g(func-)2233 4322 y(tions)17 b(are)h(a)n(v)n (ailable)f(really)h(on)f(all)h(Unix)g(platforms,)f(the)2233 4422 y(last)k(tw)o(o)f(are)g(a)n(v)n(ailable)g(only)f(on)h(some)g(of)g (them.)2233 4603 y(On)h(some)h(platforms)e Fp(setjmp)p Fx(\(3\))g(and)i Fp(longjmp)p Fx(\(3\))2233 4703 y(sa)n(v)o(e)45 b(and)h(restore)f(also)h(the)f(signal)h(mask)f(\(if)h(one)2233 4802 y(does)e(not)g(w)o(ant)g(this)h(semantics,)50 b(one)44 b(has)h(to)f(call)p 2233 4902 V 2257 4902 a Fp(setjmp)p Fx(\(3\))36 b(and)p 2854 4902 V 67 w Fp(longjmp)p Fx(\(3\))g(there\))g (while)i(on)2233 5001 y(others)23 b(one)h(has)g(to)g(e)o(xplicitly)g (use)g(the)g(superset)g(func-)2233 5101 y(tions)36 b Fp(sigsetjmp)p Fx(\(3\))f(and)h Fp(siglongjmp)p Fx(\(3\))e(for)2233 5201 y(this.)115 b(In)50 b(our)f(discussion)h(we)g(can)g(assume)g(that) 2233 5300 y Fp(setjmp)p Fx(\(3\))40 b(and)j Fp(longjmp)p Fx(\(3\))d(sa)n(v)o(e)j(and)f(restore)2233 5400 y(the)36 b(signal)f(mask,)40 b(because)35 b(if)h(this)h(is)g(not)e(the)h(case)p Black 1929 5700 a(3)p Black eop %%Page: 4 4 4 3 bop Black Black 208 83 a Fx(in)46 b(practice,)53 b(one)46 b(easily)h(can)f(replace)g(them)g(with)208 183 y Fp(sigsetjmp)p Fx(\(3\))41 b(and)i Fp(siglongjmp)p Fx(\(3\))e(calls)k(\(if)208 282 y(a)n(v)n(ailable\))30 b(or)h(\(if)g(not)g(a)n(v)n(ailable\))f(emulate)h(the)g(miss-)208 382 y(ing)d(functionality)g(manually)f(with)j(additional)e Fp(sig-)208 482 y(procmask)p Fx(\(2\))j(calls)i(\(see)h Fp(pth)p 1245 482 25 4 v 29 w(mctx.c)f Fx(in)g Fr(GNU)208 581 y(Pth)20 b Fx([25)n(]\).)p Black 95 739 50 50 v Black 208 739 a(There)37 b(is)i(the)f(function)e Fp(sigaltstack)p Fx(\(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)34 b(2)g(\()p Fr(SUSv2)f Fx([20)n(],)38 b(aka)c Fr(Unix95/98)p Fx(\))e(and)i(its)h(an-)208 1038 y(cestor)25 b(function)f Fp(sigstack)p Fx(\(2\))f(from)h Fr(4.2BSD)p Fx(.)g(The)208 1137 y(last)d(one)e(e)o(xists)i(only)e(on)h Fr(BSD)p Fx(-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)208 1337 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)f Fp(makecontext)p Fx(\(3\))e(function)i(to)h(cre-)0 1926 y(ate)k(threads)f(and)g Fp(switchcontext)p Fx(\(3\))e(or)i Fp(getcon-)0 2025 y(text)p Fx(\(3\)/)p Fp(setcontext)p Fx(\(3\))18 b(to)23 b(dispatch)e(them.)31 b(And)22 b(actu-)0 2125 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.)48 b(W)-7 b(e)29 b(could)e(easily)h(im-)0 2324 y(plement)20 b(our)g(proposed)f(API)i(as)g(follo)n(wing)e(\(all)i(error)f(checks)0 2424 y(omitted)f(for)h(better)g(readability\):)p Black Black 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)f Fk(mctx_st)h Fo(f)179 2710 y Fk(ucontext_t)f(uc;)0 2785 y 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\))e Fo(n)179 3084 y Fk(\()p Fi(void)p Fk(\)getcontext\(&\(mctx\)->uc\))0 3233 y(/*)63 b Fj(restore)20 b(mac)o(hine)e(context)45 b Fk(*/)0 3308 y Fi(#define)f Fk(mctx_restore\(mctx\))d Fo(n)179 3383 y Fk(\()p Fi(void)p Fk(\)setcontext\(&\(mctx\)->uc\))0 3532 y(/*)63 b Fj(switc)o(h)18 b(mac)o(hine)h(context)45 b Fk(*/)0 3607 y Fi(#define)f Fk(mctx_switch\(mctx_old,mctx_new\))39 b Fo(n)179 3681 y Fk(\()p Fi(void)p Fk (\)swapcontext\(&\(\(mctx_old\)->uc\),)g Fo(n)986 3756 y Fk(&\(\(mctx_new\)->uc\)\))0 3906 y(/*)63 b Fj(create)20 b(mac)o(hine)e(context)45 b Fk(*/)0 3980 y Fi(void)f Fk(mctx_create\()179 4055 y(mctx_t)g(*mctx,)179 4130 y Fi(void)g Fk(\(*sf_addr\)\()p Fi(void)f Fk(*\),)h Fi(void)g Fk(*sf_arg,)179 4204 y Fi(void)g Fk(*sk_addr,)g(size_t)g(sk_size\))0 4279 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\)\);)179 4653 y(/*)64 b Fj(adjust)18 b(to)h(new)f(context)45 b Fk(*/)179 4728 y(mctx->uc.uc_link)491 b(=)44 b(NULL;)179 4802 y(mctx->uc.uc_stack.ss_sp)176 b(=)44 b(sk_addr;)179 4877 y(mctx->uc.uc_stack.ss_size)86 b(=)44 b(sk_size;)179 4952 y(mctx->uc.uc_stack.ss_flags)d(=)j(0;)179 5101 y(/*)64 b 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\);)179 5325 y Fi(return)p Fk(;)0 5400 y Fo(g)2025 83 y Fx(Unfortunately)14 b(there)j(are)g(still)h(lots)g(of)f(Unix)f(platforms)g(where)2025 183 y(this)43 b(approach)d(cannot)h(be)h(used,)47 b(because)42 b(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)-5 b(.)80 b(Actually)38 b(the)h(platform)e(test)i(results)g(for)f Fr(GNU)i(Pth)2025 482 y Fx(\(see)29 b(T)-7 b(able)29 b(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)p Fx(\(3\),)19 b Fr(etc.)p Fx(\).)31 b(On)22 b(all)h(other)f(platforms,)f Fr(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)25 b(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 1179 y Fr(etc.)p Fx(\).)p 2025 1271 1899 4 v 2025 3174 4 1904 v 2103 1351 a Fn(Operating)18 b(System)p 2837 1374 4 79 v 277 w(Ar)o(chitectur)o(e\(s\))p 3450 1374 V 188 w(mcsc)p 3694 1374 V 104 w(sjlj)2103 1430 y Fm(FreeBSD)g(2.x/3.x)p 2837 1453 V 339 w(Intel)p 3450 1453 V 494 w(no)p 3694 1453 V 177 w(yes)2103 1508 y(FreeBSD)g(3.x)p 2837 1532 V 440 w(Intel,)g(Alpha)p 3450 1532 V 298 w(no)p 3694 1532 V 177 w(yes)2103 1587 y(NetBSD)g(1.3/1.4)p 2837 1611 V 361 w(Intel,)g(PPC,)e(M68K)p 3450 1611 V 135 w(no)p 3694 1611 V 177 w(yes)2103 1666 y(OpenBSD)i(2.5/2.6)p 2837 1690 V 313 w(Intel,)g(SP)-6 b(ARC)p 3450 1690 V 255 w(no)p 3694 1690 V 177 w(yes)2103 1745 y(BSDI)17 b(4.0)p 2837 1769 V 536 w(Intel)p 3450 1769 V 494 w(no)p 3694 1769 V 177 w(yes)2103 1824 y(Linux)g(2.0.x)g(glibc)h(1.x/2.0)p 2837 1848 V 129 w(Intel,)g(SP)-6 b(ARC,)16 b(PPC)p 3450 1848 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)p 3450 1926 V 112 w(no)p 3694 1926 V 177 w(yes)2103 1982 y(Sun)f(SunOS)g(4.1.x)p 2837 2005 V 329 w(SP)-6 b(ARC)p 3450 2005 V 410 w(no)p 3694 2005 V 177 w(yes)2103 2060 y(Sun)17 b(Solaris)h(2.5/2.6/2.7)p 2837 2084 V 181 w(SP)-6 b(ARC)p 3450 2084 V 410 w(yes)p 3694 2084 V 155 w(yes)2103 2139 y(SCO)17 b(UnixW)-5 b(are)18 b(2.x/7.x)p 2837 2163 V 169 w(Intel)p 3450 2163 V 494 w(yes)p 3694 2163 V 155 w(yes)2103 2218 y(SCO)f(OpenServ)o(er)i(5.0.x)p 2837 2242 V 175 w(Intel)p 3450 2242 V 494 w(no)p 3694 2242 V 177 w(yes)2103 2297 y(IBM)e(AIX)g(4.1/4.2/4.3)p 2837 2321 V 225 w(RS6000,)g(PPC)p 3450 2321 V 249 w(yes)p 3694 2321 V 155 w(yes)2103 2376 y(HP)g(HPUX)f(9.10/10.20)p 2837 2400 V 205 w(HPP)-6 b(A)p 3450 2400 V 450 w(no)p 3694 2400 V 177 w(yes)2103 2455 y(HP)17 b(HPUX)f(11.0)p 2837 2479 V 372 w(HPP)-6 b(A)p 3450 2479 V 450 w(yes)p 3694 2479 V 155 w(yes)2103 2534 y(SGI)17 b(IRIX)g(5.3)p 2837 2557 V 427 w(MIPS)g(32/64)p 3450 2557 V 292 w(no)p 3694 2557 V 177 w(yes)2103 2613 y(SGI)g(IRIX)g(6.2/6.5)p 2837 2636 V 326 w(MIPS)g(32/64)p 3450 2636 V 292 w(yes)p 3694 2636 V 155 w(yes)2103 2691 y(ISC)g(4.0)p 2837 2715 V 584 w(Intel)p 3450 2715 V 494 w(no)p 3694 2715 V 177 w(yes)2103 2770 y(Apple)h(MacOS)f(X)p 2837 2794 V 341 w(PPC)p 3450 2794 V 496 w(no)p 3694 2794 V 177 w(yes)2103 2849 y(DEC)f(OSF1/T)n(ru64)i(4.0/5.0)p 2837 2873 V 103 w(Alpha)p 3450 2873 V 453 w(yes)p 3694 2873 V 155 w(yes)2103 2928 y(SNI)f(ReliantUNIX)p 2837 2952 V 308 w(MIPS)p 3450 2952 V 459 w(yes)p 3694 2952 V 155 w(yes)2103 3007 y(AmigaOS)p 2837 3031 V 522 w(M68K)p 3450 3031 V 441 w(no)p 3694 3031 V 177 w(yes)p 3921 3174 4 1904 v 2025 3177 1899 4 v 2025 3264 a Fn(T)-6 b(able)18 b(2:)67 b Fm(Summary)20 b(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)168 b Fh(makecontext)p Fm(\(3\)/)p Fh(switchcontext)p Fm(\(3\),)2320 3466 y Fh(sjlj)p Fm(:)83 b(functional)51 b Fh(setjmp)p Fm(\(3\)/)p Fh(longjmp)p Fm(\(3\))c(or)i Fh(sig-)2320 3531 y(setjmp)p Fm(\(3\)/)p Fh(siglongjmp)p Fm(\(3\).)25 b(See)19 b(\002le)h Fh(PORTING)d Fm(in)j Fl(GNU)2320 3597 y(Pth)c Fm([25)q(])h(for)g(more)g(details.)2025 3834 y FA(2.4)99 b(Remaining)25 b(P)n(ossibilities)2025 3999 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)k Fp(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(].)57 b(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 4897 y(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(\()p Fp(mctx)p 2980 4996 25 4 v 29 w(t)p Fx(\))i(on)f(their)g Fp(jmp)p 3559 4996 V 29 w(buf)h Fx(data)2025 5096 y(structure.)2150 5201 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 5400 y Fp(mctx)p 2230 5400 V 29 w(save)p Fx(,)73 b Fp(mctx)p 2753 5400 V 29 w(restore)62 b Fx(and)g Fp(mctx)p 3577 5400 V 29 w(switch)p Black 1929 5700 a Fx(4)p Black eop %%Page: 5 5 5 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)20 b Fp(longjmp)p Fx(\(3\).)0 358 y FA(The)26 b(Dif\002cult)g(P)o(art)0 500 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.)56 b(Ev)o(en)29 b(kno)n(wing)g(that)i(our)f(ma-)0 699 y(chine)17 b(conte)o(xt)f(is)i Fp(jmp)p 687 699 25 4 v 30 w(buf)f Fx(based)g(is)i(no)e(adv)n(antage)e(to)j(us.)24 b(A)0 799 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)26 b(\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)p Fx(\(3\))f(and)h Fp(longjmp)p Fx(\(3\))e(calls,)0 1098 y(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)19 b(portable)g(function)g(which)g(actually)h(deals)g(with)h(stacks.)125 1406 y(So)33 b(it)g(is)h(clear)f(that)g(our)f(implementation)f(for)h Fp(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 1505 V 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)39 b(our)g(approach)f(has)i(to)g(be)f(careful)g(to)h(en-)0 1804 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 y Fp(mctx)p 205 2103 V 29 w(save)p Fx(,)g Fp(mctx)p 709 2103 V 29 w(restore)47 b Fx(and)h Fp(mctx)p 1504 2103 V 29 w(switch)p Fx(\),)0 2203 y(because)29 b Fp(setjmp)p Fx(\(3\))f(and)i Fp(sigaltstack)p Fx(\(3\))d(cannot)i(be)0 2302 y(tri)n(vially)20 b(combined)e(to)i(form)f Fp(mctx)p 1102 2302 V 29 w(create)p Fx(.)0 2630 y Fs(3)119 b(Implementation)0 2833 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(\()p Fp(mctx)p 869 2932 V 29 w(save)p Fx(,)i Fp(mctx)p 1351 2932 V 29 w(restore)e Fx(and)0 3032 y Fp(mctx)p 205 3032 V 29 w(switch)p Fx(\))i(and)h(a)h(dif)n(\002cult)e(part)h(\()p Fp(mctx)p 1504 3032 V 29 w(create)p Fx(\).)0 3131 y(Let)23 b(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 Black 0 3532 a Fk(/*)63 b Fj(mac)o(hine)19 b(context)g(data)g(structure)44 b Fk(*/)0 3607 y Fi(typedef)g(struct)f Fk(mctx_st)h Fo(f)179 3681 y Fk(jmp_buf)g(jb;)0 3756 y Fo(g)h Fk(mctx_t;)0 3906 y(/*)63 b Fj(sav)o(e)20 b(mac)o(hine)e(context)45 b Fk(*/)0 3980 y Fi(#define)f Fk(mctx_save\(mctx\))e Fo(n)179 4055 y Fk(\()p Fi(void)p Fk(\)setjmp\(\(mctx\)->jb\))0 4204 y(/*)63 b Fj(restore)20 b(mac)o(hine)e(context)45 b Fk(*/)0 4279 y Fi(#define)f Fk(mctx_restore\(mctx\))d Fo(n)179 4354 y Fk(longjmp\(\(mctx\)->jb,)h(1\))0 4503 y(/*)63 b Fj(switc)o(h)18 b(mac)o(hine)h(context)45 b Fk(*/)0 4578 y Fi(#define)f Fk(mctx_switch\(mctx_old,mctx_new\))39 b 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\))0 4877 y(/*)63 b Fj(create)20 b(mac)o(hine)e(context)45 b Fk(*/)0 4952 y Fi(void)f Fk(mctx_create\()179 5026 y(mctx_t)g(*mctx,)179 5101 y Fi(void)g Fk(\(*sf_addr\)\()p Fi(void)f Fk(*\),)h Fi(void)g Fk(*sf_arg,)179 5176 y Fi(void)g Fk(*sk_addr,)g(size_t)g(sk_size\))0 5251 y Fo(f)198 5325 y Fj(...initialization)16 b(of)j Fg(mctx)f Fj(to)h(be)g(\002lled)f(in...)0 5400 y Fo(g)2025 83 y Fx(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)36 b(to)g(implement)f Fp(mctx)p 2881 282 V 29 w(save)p Fx(,)40 b Fp(mctx)p 3371 282 V 29 w(restore)35 b Fx(and)2025 382 y Fp(mctx)p 2230 382 V 29 w(switch)40 b Fx(is)i(intentional.)86 b(F)o(or)40 b(technical)g(reasons)2025 482 y(related)35 b(to)h Fp(setjmp)p Fx(\(3\))e(semantics)h(and)g Fp(return)g Fx(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)g Fx(implement)f(these)h(three)g(functions)e(\(at)i(least)h(not)2025 780 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)25 b(portability)g(across)i(all)g(platforms.)43 b(Instead)26 b(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)25 b(Ov)o(er)o(view)2025 1373 y Fx(The)g(general)e(idea)i(for)f Fp(mctx)p 2940 1373 V 29 w(create)h Fx(is)h(to)f(con\002gure)e(the)2025 1472 y(gi)n(v)o(en)14 b(stack)i(as)g(a)g(signal)f(stack)h(via)f Fp(sigaltstack)p Fx(\(2\),)f(send)2025 1572 y(the)33 b(current)f(process)g(a)i(signal)e(to)i(transfer)e(e)o(x)o(ecution)e (con-)2025 1672 y(trol)e(onto)g(this)h(stack,)h(sa)n(v)o(e)f(the)f (machine)f(conte)o(xt)h(there)g(via)2025 1771 y Fp(setjmp)p Fx(\(3\),)16 b(get)h(rid)h(of)f(the)g(signal)h(handler)e(scope)h(and)g (boot-)2025 1871 y(strap)j(into)g(the)g(startup)g(routine.)2150 1970 y(The)31 b(real)g(problem)f(in)i(this)g(approach)d(comes)i(from)g (the)2025 2070 y(signal)e(handler)f(scope)h(which)g(implies)h(v)n (arious)e(restrictions)2025 2170 y(on)21 b(Unix)g(platforms)f(\(the)h (signal)g(handler)f(scope)g(often)h(is)h(just)2025 2269 y(a)i(\003ag)h(in)f(the)g(process)f(control)g(block)g(\(PCB\))i(which)f (v)n(arious)2025 2369 y(system)30 b(calls,)j(lik)o(e)e Fp(sigaltstack)p Fx(\(2\),)e(check)h(before)e(al-)2025 2469 y(lo)n(wing)c(the)h(operation)f(\226)h(b)n(ut)g(because)g(it)h(is) g(part)f(of)f(the)i(pro-)2025 2568 y(cess)20 b(state)g(the)f(k)o(ernel) g(manages,)f(the)h(process)g(cannot)f(change)2025 2668 y(it)i(itself\).)25 b(As)20 b(we)f(will)h(see,)g(we)g(ha)n(v)o(e)e(to)i (perform)d(a)i(fe)n(w)h(tricks)2025 2767 y(to)i(get)f(rid)h(of)f(it.)29 b(The)22 b(second)e(main)h(problem)f(is:)29 b(ho)n(w)21 b(do)g(we)2025 2867 y(prepare)k(the)h(calling)g(of)g(the)g(start)h (routine)e(without)h(immedi-)2025 2967 y(ately)20 b(entering)f(it?)2025 3204 y FA(3.2)99 b(Algorithm)2025 3360 y Fx(The)20 b(input)f(to)i(the)f Fp(mctx)p 2779 3360 V 29 w(create)g Fx(function)e(is)j(the)g(machine) 2025 3459 y(conte)o(xt)30 b(structure)h Fr(mctx)h Fx(which)f(should)f (be)i(initialized,)i(the)2025 3559 y(thread)15 b(startup)h(function)e (address)i Fr(sf)p 3111 3559 V 30 w(addr)p Fx(,)h(the)f(thread)f (startup)2025 3659 y(function)24 b(ar)o(gument)g Fr(sf)p 2727 3659 V 30 w(ar)m(g)i Fx(and)f(a)i(chunk)d(of)i(memory)e(start-) 2025 3758 y(ing)31 b(at)i Fr(sk)p 2330 3758 V 30 w(addr)g Fx(and)e Fr(sk)p 2772 3758 V 30 w(size)i Fx(bytes)e(in)h(size,)j(which) d(should)2025 3858 y(become)19 b(the)h(threads)g(stack.)2150 3958 y(The)33 b(follo)n(wing)f(algorithm)g(for)h Fp(mctx)p 3361 3958 V 29 w(create)g Fx(is)i(di-)2025 4057 y(rectly)21 b(modeled)f(after)h(the)h(implemented)e(algorithm)g(one)h(can)2025 4157 y(\002nd)28 b(in)h Fr(GNU)h(Pth)e Fx([25)o(],)j(which)d(in)h(turn) f(w)o(as)i(deri)n(v)o(ed)d(from)2025 4256 y(techniques)19 b(originally)f(found)h(in)h Fr(r)o(sthr)m(eads)h Fx([27)n(]:)p Black 2129 4438 a(1.)p Black 41 w(Preserv)o(e)i(the)h(current)g(signal) g(mask)g(and)g(block)f(an)i(ar)n(-)2233 4537 y(bitrary)f(w)o(ork)o(er)h (signal)h(\(we)g(use)g Fp(SIGUSR1)p Fx(,)g(b)n(ut)g(an)o(y)2233 4637 y(signal)d(can)f(be)i(used)e(for)h(this)g(\226)h(e)n(v)o(en)e(an)h (already)f(used)2233 4736 y(one\).)53 b(This)30 b(w)o(ork)o(er)f (signal)g(is)i(later)f(temporarily)e(re-)2233 4836 y(quired)18 b(for)i(the)g(trampoline)f(step.)p Black 2129 5001 a(2.)p Black 41 w(Preserv)o(e)28 b(a)h(possibly)g(e)o(xisting)f(signal)h (action)g(for)g(the)2233 5101 y(w)o(ork)o(er)19 b(signal)i(and)g (con\002gure)e(a)i(trampoline)e(function)2233 5201 y(as)g(the)h(ne)n(w) f(temporary)e(signal)i(action.)24 b(The)19 b(signal)g(de-)2233 5300 y(li)n(v)o(ery)g(is)j(con\002gured)d(to)i(occur)f(on)g(an)h (alternate)g(signal)2233 5400 y(stack)f(\(see)g(ne)o(xt)g(step\).)p Black 1929 5700 a(5)p Black eop %%Page: 6 6 6 5 bop Black Black Black 104 83 a Fx(3.)p Black 41 w(Preserv)o(e)35 b(a)h(possibly)g(acti)n(v)o(e)f(alternate)g(signal)h(stack)208 183 y(and)56 b(con\002gure)f(the)h(memory)f(chunk)h(starting)g(at)208 282 y Fr(sk)p 282 282 25 4 v 30 w(addr)47 b Fx(as)f(the)g(ne)n(w)f (temporary)f(alternate)h(signal)208 382 y(stack)20 b(of)g(length)f Fr(sk)p 791 382 V 30 w(size)p Fx(.)p Black 104 566 a(4.)p Black 41 w(Sa)n(v)o(e)40 b(parameters)g(for)g(the)h(trampoline)e(step)i (\()p Fr(mctx)p Fx(,)208 666 y Fr(sf)p 268 666 V 30 w(addr)p Fx(,)25 b Fr(sf)p 557 666 V 30 w(ar)m(g)p Fx(,)h Fr(etc.)p Fx(\))39 b(in)26 b(global)e(v)n(ariables,)h(send)g(the)208 766 y(current)20 b(process)g(the)i(w)o(ork)o(er)e(signal,)h (temporarily)e(un-)208 865 y(block)d(it)i(and)f(this)h(w)o(ay)f(allo)n (w)h(it)g(to)g(be)f(deli)n(v)o(ered)f(on)h(the)208 965 y(signal)25 b(stack)h(in)g(order)f(to)h(transfer)f(e)o(x)o(ecution)f (control)208 1065 y(to)c(the)g(trampoline)f(function.)p Black 104 1249 a(5.)p Black 41 w(After)24 b(the)h(trampoline)e (function)g(asynchronously)f(en-)208 1349 y(tered,)27 b(sa)n(v)o(e)g(its)h(machine)e(conte)o(xt)g(in)h(the)g Fr(mctx)g Fx(struc-)208 1448 y(ture)j(and)g(immediately)g(return)f (from)h(it)h(to)g(terminate)208 1548 y(the)20 b(signal)g(handler)f (scope.)p Black 104 1732 a(6.)p Black 41 w(Restore)29 b(the)g(preserv)o(ed)f(alternate)h(signal)g(stack,)i(pre-)208 1832 y(serv)o(ed)18 b(signal)h(action)g(and)g(preserv)o(ed)e(signal)i (mask)g(for)208 1932 y(w)o(ork)o(er)30 b(signal.)59 b(This)32 b(w)o(ay)g(an)f(e)o(xisting)g(application)208 2031 y(con\002guration)17 b(for)j(the)g(w)o(ork)o(er)f(signal)h(is)h(restored.)p Black 104 2216 a(7.)p Black 41 w(Sa)n(v)o(e)115 b(the)g(current)f (machine)g(conte)o(xt)g(of)208 2315 y Fp(mctx)p 413 2315 V 29 w(create)p Fx(.)52 b(This)30 b(allo)n(ws)g(us)g(to)g(return)e(to)i (this)208 2415 y(point)19 b(after)h(the)g(ne)o(xt)g(trampoline)e(step.) p Black 104 2599 a(8.)p Black 41 w(Restore)30 b(the)g(pre)n(viously)e (sa)n(v)o(ed)i(machine)f(conte)o(xt)g(of)208 2699 y(the)15 b(trampoline)g(function)f(\()p Fr(mctx)p Fx(\))h(to)h(again)f(transfer) g(e)o(x-)208 2799 y(ecution)31 b(control)h(onto)g(the)h(alternate)f (stack,)k(b)n(ut)d(this)208 2898 y(time)20 b(without\(!\))j(signal)d (handler)f(scope.)p Black 104 3083 a(9.)p Black 41 w(After)50 b(reaching)f(the)i(trampoline)e(function)g(\()p Fr(mctx)p Fx(\))208 3182 y(again,)40 b(immediately)c(bootstrap)g(into)h(a)g (clean)g(stack)208 3282 y(frame)19 b(by)h(just)g(calling)g(a)h(second)e (function.)p Black 62 3467 a(10.)p Black 41 w(Set)51 b(the)h(ne)n(w)f(signal)g(mask)g(to)g(be)g(the)g(same)h(as)208 3566 y(the)32 b(original)e(signal)i(mask)g(which)g(w)o(as)h(acti)n(v)o (e)e(when)208 3666 y Fp(mctx)p 413 3666 V 29 w(create)e Fx(w)o(as)i(called.)53 b(This)30 b(is)h(required)d(be-)208 3765 y(cause)19 b(in)g(the)g(\002rst)h(trampoline)e(step)h(we)h (usually)e(had)h(at)208 3865 y(least)h(the)h(w)o(ork)o(er)e(signal)h (block)o(ed.)p Black 62 4050 a(11.)p Black 41 w(Load)51 b(the)i(passed)f(startup)g(information)f(\()p Fr(sf)p 1673 4050 V 29 w(addr)p Fx(,)208 4149 y Fr(sf)p 268 4149 V 30 w(ar)m(g)p Fx(\))46 b(from)g Fp(mctx)p 894 4149 V 29 w(create)g Fx(into)g(local)h(\(stack-)208 4249 y(based\))27 b(v)n(ariables.)47 b(This)28 b(is)h(important)e(because)g(their)208 4348 y(v)n(alues)17 b(ha)n(v)o(e)f(to)i(be)f(preserv)o(ed)f(in)i (machine)e(conte)o(xt)g(de-)208 4448 y(pendent)g(memory)g(until)h(the)h (created)f(machine)g(conte)o(xt)208 4548 y(is)k(the)f(\002rst)h(time)f (restored)f(by)h(the)g(application.)p Black 62 4732 a(12.)p Black 41 w(Sa)n(v)o(e)26 b(the)g(current)f(machine)h(conte)o(xt)f(for)h (later)g(restor)n(-)208 4832 y(ing)19 b(by)h(the)g(calling)g (application.)p Black 62 5016 a(13.)p Black 41 w(Restore)30 b(the)g(pre)n(viously)e(sa)n(v)o(ed)i(machine)f(conte)o(xt)g(of)208 5116 y Fp(mctx)p 413 5116 V 29 w(create)20 b Fx(to)i(transfer)e(e)o(x)o (ecution)f(control)h(back)208 5216 y(to)g(it.)p Black 62 5400 a(14.)p Black 41 w(Return)f(to)i(the)f(calling)g(application.) 2025 83 y(When)d(the)h(calling)f(application)e(no)n(w)i(again)g (switches)g(into)h(the)2025 183 y(established)25 b(machine)f(conte)o (xt)g Fr(mctx)p Fx(,)i(the)f(thread)g(starts)h(run-)2025 282 y(ning)h(at)i(routine)e Fr(sf)p 2614 282 V 31 w(addr)i Fx(with)g(ar)o(gument)d Fr(sf)p 3405 282 V 30 w(ar)m(g)p Fx(.)49 b(Figure)27 b(1)2025 382 y(illustrates)g(the)g(algorithm)e (\(the)i(numbers)e(refer)h(to)i(the)e(algo-)2025 482 y(rithm)20 b(steps)g(listed)h(abo)o(v)o(e\).)2025 1015 y 14669332 30325391 0 0 14669332 30325391 startTexFig 2025 1015 a %%BeginDocument: pmt-fig.eps d718 3 a720 3 %%Title: pmt-fig.eps %%Creator: fig2dev Version 3.2.3 Patchlevel %%CreationDate: Sat Apr 22 23:14:14 2000 d722 5 a726 1 %%BoundingBox: 0 0 223 461 d765 1 d769 1 a769 2 newpath 0 461 moveto 0 0 lineto 223 0 lineto 223 461 lineto closepath clip newpath -21.0 477.0 translate d805 1 a807 1 %%Page: 1 1 d809 2 a810 89 0.06299 0.06299 sc % Polyline 15.000 slw n 780 705 m 750 705 750 855 30 arcto 4 {pop} repeat 750 885 900 885 30 arcto 4 {pop} repeat 930 885 930 735 30 arcto 4 {pop} repeat 930 705 780 705 30 arcto 4 {pop} repeat cp gs 0.00 setgray ef gr gs col0 s gr % Polyline n 1035 1185 m 1005 1185 1005 1335 30 arcto 4 {pop} repeat 1005 1365 1155 1365 30 arcto 4 {pop} repeat 1185 1365 1185 1215 30 arcto 4 {pop} repeat 1185 1185 1035 1185 30 arcto 4 {pop} repeat cp gs 0.00 setgray ef gr gs col0 s gr % Polyline n 3075 2760 m 3045 2760 3045 2910 30 arcto 4 {pop} repeat 3045 2940 3195 2940 30 arcto 4 {pop} repeat 3225 2940 3225 2790 30 arcto 4 {pop} repeat 3225 2760 3075 2760 30 arcto 4 {pop} repeat cp gs 0.00 setgray ef gr gs col0 s gr % Polyline n 3105 885 m 3075 885 3075 1035 30 arcto 4 {pop} repeat 3075 1065 3225 1065 30 arcto 4 {pop} repeat 3255 1065 3255 915 30 arcto 4 {pop} repeat 3255 885 3105 885 30 arcto 4 {pop} repeat cp gs 0.00 setgray ef gr gs col0 s gr % Polyline n 2040 1500 m 2010 1500 2010 1650 30 arcto 4 {pop} repeat 2010 1680 2160 1680 30 arcto 4 {pop} repeat 2190 1680 2190 1530 30 arcto 4 {pop} repeat 2190 1500 2040 1500 30 arcto 4 {pop} repeat cp gs 0.00 setgray ef gr gs col0 s gr % Polyline n 2040 2100 m 2010 2100 2010 2250 30 arcto 4 {pop} repeat 2010 2280 2160 2280 30 arcto 4 {pop} repeat 2190 2280 2190 2130 30 arcto 4 {pop} repeat 2190 2100 2040 2100 30 arcto 4 {pop} repeat cp gs 0.00 setgray ef gr gs col0 s gr % Polyline n 1035 705 m 1005 705 1005 855 30 arcto 4 {pop} repeat 1005 885 1155 885 30 arcto 4 {pop} repeat 1185 885 1185 735 30 arcto 4 {pop} repeat 1185 705 1035 705 30 arcto 4 {pop} repeat cp gs 0.00 setgray ef gr gs col0 s gr % Polyline n 2040 2640 m 2010 2640 2010 2790 30 arcto 4 {pop} repeat 2010 2820 2160 2820 30 arcto 4 {pop} repeat 2190 2820 2190 2670 30 arcto 4 {pop} repeat 2190 2640 2040 2640 30 arcto 4 {pop} repeat cp gs 0.00 setgray ef gr gs col0 s gr % Polyline n 2040 525 m 2010 525 2010 675 30 arcto 4 {pop} repeat 2010 705 2160 705 30 arcto 4 {pop} repeat 2190 705 2190 555 30 arcto 4 {pop} repeat 2190 525 2040 525 30 arcto 4 {pop} repeat cp gs 0.00 setgray ef gr gs col0 s gr % Polyline n 3075 2505 m 3045 2505 3045 2655 30 arcto 4 {pop} repeat 3045 2685 3195 2685 30 arcto 4 {pop} repeat 3225 2685 3225 2535 30 arcto 4 {pop} repeat 3225 2505 3075 2505 30 arcto 4 {pop} repeat cp gs 0.00 setgray ef gr gs col0 s gr % Polyline n 1305 1500 m 1275 1500 1275 1650 30 arcto 4 {pop} repeat 1275 1680 1425 1680 30 arcto 4 {pop} repeat 1455 1680 1455 1530 30 arcto 4 {pop} repeat 1455 1500 1305 1500 30 arcto 4 {pop} repeat cp gs 0.00 setgray ef gr gs col0 s gr % Polyline n 1545 3225 m 1515 3225 1515 3375 30 arcto 4 {pop} repeat 1515 3405 1665 3405 30 arcto 4 {pop} repeat 1695 3405 1695 3255 30 arcto 4 {pop} repeat 1695 3225 1545 3225 30 arcto 4 {pop} repeat cp gs 0.00 setgray ef gr gs col0 s gr % Polyline n 1350 570 m 1320 570 1320 720 30 arcto 4 {pop} repeat 1320 750 1470 750 30 arcto 4 {pop} repeat 1500 750 1500 600 30 arcto 4 {pop} repeat 1500 570 1350 570 30 arcto 4 {pop} repeat cp gs 0.00 setgray ef gr gs col0 s gr % Polyline n 3345 2505 m 3315 2505 3315 2655 30 arcto 4 {pop} repeat 3315 2685 3465 2685 30 arcto 4 {pop} repeat 3495 2685 3495 2535 30 arcto 4 {pop} repeat 3495 2505 3345 2505 30 arcto 4 {pop} repeat cp gs 0.00 setgray ef gr gs col0 s gr /Times-Bold ff 150.00 scf sf 3068 2895 m gs 1 -1 sc (12) col7 sh gr d813 1 a813 2 n 630 270 m 1890 270 l 1890 3600 l 630 3600 l cp gs col0 s gr d815 1 a815 2 n 2340 270 m 3600 270 l 3600 1800 l 2340 1800 l cp gs col0 s gr d817 1 a817 2 n 2340 2070 m 3600 2070 l 3600 3600 l 2340 3600 l cp gs col0 s gr d819 1 a819 7 [15 15] 15 sd n 1980 810 m 2025 720 l 2070 900 l 2115 720 l 2160 900 l 2205 810 l gs col0 s gr [] 0 sd % Polyline [15 15] 15 sd n 1980 1170 m 2014 1081 l 2059 1261 l 2104 1081 l 2149 1261 l 2205 1170 l gs col0 s gr [] 0 sd a820 1 15.000 slw d822 58 a879 5 1275 660 m 1275 600 l 1124 600 l 1244 630 l 1124 660 l cp eoclip n 1260 4586 m 1980 4593 l 1980 3690 l 450 3690 l 450 630 l 990 630 l 1260 630 l gs col0 s gr gr d882 1 a882 2 7.500 slw n 1124 660 m 1244 630 l 1124 600 l 1124 660 l cp gs 0.00 setgray ef gr col0 s a883 1 15.000 slw d885 3 a887 4 1995 840 m 1995 780 l 1844 780 l 1964 810 l 1844 840 l cp eoclip n 1260 630 m 1260 810 l 1980 810 l gs col0 s gr gr d890 378 d1269 39 a1307 1 n 1844 840 m 1964 810 l 1844 780 l 1844 840 l cp gs 0.00 setgray ef gr col0 s a1308 1 15.000 slw d1310 3 a1312 4 2985 840 m 2985 780 l 2834 780 l 2954 810 l 2834 840 l cp eoclip n 2205 810 m 2970 810 l gs col0 s gr gr d1315 1 a1315 2 7.500 slw n 2834 840 m 2954 810 l 2834 780 l 2834 840 l cp gs 0.00 setgray ef gr col0 s a1316 1 15.000 slw d1318 3 a1320 4 2940 1185 m 3000 1185 l 3000 1034 l 2970 1154 l 2940 1034 l cp eoclip n 2970 810 m 2970 1170 l gs col0 s gr gr d1323 1 a1323 2 7.500 slw n 2940 1034 m 2970 1154 l 3000 1034 l 2940 1034 l cp gs 0.00 setgray ef gr col0 s a1324 1 15.000 slw d1326 3 a1328 4 2190 1140 m 2190 1200 l 2341 1200 l 2221 1170 l 2341 1140 l cp eoclip n 2205 1170 m 2970 1170 l gs col0 s gr gr d1331 1 a1331 2 7.500 slw n 2341 1140 m 2221 1170 l 2341 1200 l 2341 1140 l cp gs 0.00 setgray ef gr col0 s d1333 1 a1333 1 15.000 slw d1335 3 a1337 4 1245 1140 m 1245 1200 l 1396 1200 l 1276 1170 l 1396 1140 l cp eoclip n 1260 1170 m 1980 1170 l gs col0 s gr gr d1340 2 a1341 2 7.500 slw n 1396 1140 m 1276 1170 l 1396 1200 l 1396 1140 l cp gs 0.00 setgray ef gr col0 s d1343 1 a1343 1 15.000 slw d1345 3 a1347 4 2985 1470 m 2985 1410 l 2834 1410 l 2954 1440 l 2834 1470 l cp eoclip n 1260 1440 m 2970 1440 l gs col0 s gr gr a1349 3 7.500 slw n 2834 1470 m 2954 1440 l 2834 1410 l 2834 1470 l cp gs 0.00 setgray ef gr col0 s % Polyline d1351 1 a1351 2 n 1260 1170 m 1260 1440 l gs col0 s gr d1353 1 d1355 3 a1357 4 2985 2550 m 2985 2490 l 2834 2490 l 2954 2520 l 2834 2550 l cp eoclip n 2970 1440 m 2970 1890 l 2250 1890 l 2250 2520 l 2970 2520 l gs col0 s gr gr a1359 3 7.500 slw n 2834 2550 m 2954 2520 l 2834 2490 l 2834 2550 l cp gs 0.00 setgray ef gr col0 s % Polyline d1361 1 a1361 2 n 2970 2520 m 2970 2883 l gs col0 s gr d1363 1 d1365 3 a1367 4 1230 2852 m 1229 2912 l 1380 2913 l 1261 2883 l 1381 2853 l cp eoclip n 2970 2891 m 1245 2883 l gs col0 s gr gr d1370 3 a1373 3 n 1381 2853 m 1261 2883 l 1380 2913 l 1381 2853 l cp gs 0.00 setgray ef gr col0 s % Polyline 15.000 slw d1375 3 a1377 4 1229 4751 m 1230 4811 l 1381 4809 l 1261 4781 l 1380 4749 l cp eoclip n 1260 2880 m 1260 3150 l 2070 3150 l 2062 4773 l 1245 4781 l gs col0 s gr gr d1380 3 a1382 2 7.500 slw n 1380 4749 m 1261 4781 l 1381 4809 l 1380 4749 l cp gs 0.00 setgray ef gr col0 s d1384 1 a1384 1 15.000 slw d1386 3 a1388 4 1230 4601 m 1290 4601 l 1290 4450 l 1260 4570 l 1230 4450 l cp eoclip n 450 4050 m 1260 4050 l 1260 4586 l gs col0 s gr gr d1391 2 a1392 2 7.500 slw n 1230 4450 m 1260 4570 l 1290 4450 l 1230 4450 l cp gs 0.00 setgray ef gr col0 s d1394 1 a1394 1 15.000 slw d1396 3 a1398 4 1230 5235 m 1290 5234 l 1287 5083 l 1260 5204 l 1227 5084 l cp eoclip n 1252 4773 m 1260 5220 l gs col0 s gr gr d1401 3 a1404 5 n 1227 5084 m 1260 5204 l 1287 5083 l 1227 5084 l cp gs 0.00 setgray ef gr col0 s % Polyline 1 slc 15.000 slw [15 45] 45 sd d1406 4 a1409 5 2985 4260 m 2985 4200 l 2834 4200 l 2954 4230 l 2834 4260 l cp eoclip n 2970 3150 m 2970 3690 l 2250 3690 l 2250 4230 l 2970 4230 l gs col0 s gr gr [] 0 sd d1411 1 a1411 6 0 slc 7.500 slw n 2834 4260 m 2954 4230 l 2834 4200 l 2834 4260 l cp gs 0.00 setgray ef gr col0 s % Polyline n 630 6300 m 1890 6300 l 1890 7290 l gs col0 s gr d1413 1 d1415 3 a1417 5 3660 7575 m 3720 7575 l 3720 7424 l 3690 7544 l 3660 7424 l cp 3720 6285 m 3660 6285 l 3660 6436 l 3690 6316 l 3720 6436 l cp eoclip n 3690 6300 m 3690 7560 l gs col0 s gr gr a1419 14 n 3720 6436 m 3690 6316 l 3660 6436 l 3720 6436 l cp gs 0.00 setgray ef gr col0 s % arrowhead n 3660 7424 m 3690 7544 l 3720 7424 l 3660 7424 l cp gs 0.00 setgray ef gr col0 s % Polyline n 2340 6300 m 3600 6300 l 3600 7560 l 2340 7560 l cp gs col0 s gr % Polyline n 630 3780 m 1890 3780 l 1890 6120 l 630 6120 l cp gs col0 s gr % Polyline n 2340 3780 m 3600 3780 l 3600 6120 l 2340 6120 l cp gs col0 s gr % Polyline 1 slc d1421 1 a1421 39 [15 45] 45 sd gs clippath 2985 3180 m 2985 3120 l 2834 3120 l 2954 3150 l 2834 3180 l cp eoclip n 1260 5220 m 2160 5220 l 2160 3150 l 2970 3150 l gs col0 s gr gr [] 0 sd % arrowhead 0 slc 7.500 slw n 2834 3180 m 2954 3150 l 2834 3120 l 2834 3180 l cp gs 0.00 setgray ef gr col0 s % Polyline n 630 6300 m 630 7380 l gs col0 s gr % Polyline 1 slc n 630 6570 m 900 6300 l gs col0 s gr % Polyline n 630 6840 m 1170 6300 l gs col0 s gr % Polyline n 630 7110 m 1440 6300 l gs col0 s gr % Polyline n 630 7380 m 1710 6300 l gs col0 s gr % Polyline n 810 7470 m 1890 6435 l gs col0 s gr % Polyline n 1080 7515 m 1890 6750 l gs col0 s gr % Polyline n 1665 7245 m 1890 7020 l gs col0 s gr % Polyline n 2340 6570 m 2610 6300 l gs col0 s gr d1423 1 a1423 20 n 2340 6840 m 2925 6300 l gs col0 s gr % Polyline n 2340 7110 m 3240 6300 l gs col0 s gr % Polyline n 2340 7380 m 3510 6300 l gs col0 s gr % Polyline n 2520 7560 m 3600 6570 l gs col0 s gr % Polyline n 2880 7560 m 3600 6930 l gs col0 s gr % Polyline n 3240 7560 m 3600 7245 l gs col0 s gr % Polyline 15.000 slw [15 45] 45 sd d1425 4 a1428 5 2940 5325 m 3000 5325 l 3000 5174 l 2970 5294 l 2940 5174 l cp eoclip n 2970 4230 m 2970 5310 l gs col0 s gr gr [] 0 sd a1429 5 0 slc 7.500 slw n 2940 5174 m 2970 5294 l 3000 5174 l 2940 5174 l cp gs 0.00 setgray ef gr col0 s % Polyline 1 slc d1431 1 a1431 11 [15 45] 45 sd gs clippath 1245 5280 m 1245 5340 l 1396 5340 l 1276 5310 l 1396 5280 l cp eoclip n 2970 5310 m 1260 5310 l gs col0 s gr gr [] 0 sd % arrowhead 0 slc 7.500 slw n 1396 5280 m 1276 5310 l 1396 5340 l 1396 5280 l cp gs 0.00 setgray ef gr col0 s a1432 11 1 slc 15.000 slw [15 45] 45 sd gs clippath 1245 5640 m 1245 5700 l 1396 5700 l 1276 5670 l 1396 5640 l cp eoclip n 2970 5580 m 2970 5670 l 1260 5670 l gs col0 s gr gr [] 0 sd % arrowhead 0 slc d1434 1 a1434 1 n 1396 5640 m 1276 5670 l 1396 5700 l 1396 5640 l cp gs 0.00 setgray ef gr col0 s a1435 1 1 slc d1437 1 a1437 1 [15 45] 45 sd d1439 3 a1441 4 345 5910 m 345 5970 l 496 5970 l 376 5940 l 496 5910 l cp eoclip n 1260 5670 m 1260 5940 l 360 5940 l gs col0 s gr gr d1444 16 a1459 3 0 slc 7.500 slw n 496 5910 m 376 5940 l 496 5970 l 496 5910 l cp gs 0.00 setgray ef gr col0 s a1460 11 1 slc 15.000 slw [15 45] 45 sd gs clippath 2985 5610 m 2985 5550 l 2834 5550 l 2954 5580 l 2834 5610 l cp eoclip n 1260 5355 m 1260 5580 l 2970 5580 l gs col0 s gr gr [] 0 sd % arrowhead 0 slc d1462 28 a1489 74 n 2834 5610 m 2954 5580 l 2834 5550 l 2834 5610 l cp gs 0.00 setgray ef gr col0 s % Polyline n 630 7380 m 631 7381 l 634 7383 l 639 7388 l 647 7394 l 657 7402 l 670 7412 l 686 7423 l 703 7435 l 723 7448 l 744 7460 l 766 7472 l 791 7483 l 817 7493 l 845 7502 l 877 7509 l 911 7514 l 949 7517 l 991 7518 l 1035 7515 l 1076 7510 l 1115 7502 l 1150 7493 l 1181 7483 l 1207 7473 l 1228 7463 l 1245 7453 l 1259 7443 l 1269 7433 l 1278 7423 l 1286 7414 l 1294 7404 l 1303 7394 l 1314 7384 l 1327 7373 l 1344 7362 l 1364 7350 l 1390 7338 l 1419 7326 l 1454 7313 l 1491 7301 l 1530 7290 l 1576 7279 l 1619 7271 l 1657 7265 l 1690 7262 l 1720 7261 l 1747 7261 l 1771 7263 l 1793 7266 l 1813 7269 l 1831 7273 l 1847 7277 l 1861 7280 l 1872 7284 l 1880 7286 l 1885 7288 l 1889 7289 l 1890 7290 l gs col0 s gr /Times-Bold ff 150.00 scf sf 2430 6480 m gs 1 -1 sc (sk_addr) col0 sh gr /Times-Bold ff 150.00 scf sf 720 6480 m gs 1 -1 sc (main stack) col0 sh gr /Times-Bold ff 150.00 scf sf 720 3960 m gs 1 -1 sc (main) col0 sh gr /Times-Bold ff 150.00 scf sf 2430 3960 m gs 1 -1 sc (sf_addr\(sf_arg\)) col0 sh gr /Times-Bold ff 150.00 scf sf 2430 2250 m gs 1 -1 sc (mctx_create) col0 sh gr /Times-Bold ff 150.00 scf sf 2430 450 m gs 1 -1 sc (mctx_create) col0 sh gr /Times-Bold ff 150.00 scf sf 720 450 m gs 1 -1 sc (mctx_create) col0 sh gr /Times-Bold ff 150.00 scf sf 3150 2250 m gs 1 -1 sc (_boot) col0 sh gr /Times-Bold ff 150.00 scf sf 2790 585 m gs 1 -1 sc (_trampoline) col0 sh gr /Times-Bold ff 150.00 scf sf 3870 7110 m gs 1 -1 sc 90.0 rot (sk_size) col0 sh gr /Times-Bold ff 150.00 scf sf 1350 5175 m gs 1 -1 sc (mctx_switch) col0 sh gr /Times-Bold ff 150.00 scf sf 2385 5445 m gs 1 -1 sc (mctx_switch) col0 sh gr /Times-Bold ff 150.00 scf sf 1350 5535 m gs 1 -1 sc (mctx_switch) col0 sh gr /Times-Bold ff 150.00 scf sf 2385 5805 m gs 1 -1 sc (mctx_switch) col0 sh gr /Times-Bold ff 150.00 scf sf 1305 4725 m gs 1 -1 sc (mctx_create) col0 sh gr /Times-Bold ff 150.00 scf sf 810 840 m gs 1 -1 sc (1) col7 sh gr /Times-Bold ff 150.00 scf sf 1065 840 m gs 1 -1 sc (2) col7 sh gr /Times-Bold ff 150.00 scf sf 1065 1320 m gs 1 -1 sc (6) col7 sh gr /Times-Bold ff 150.00 scf sf 1335 1635 m gs 1 -1 sc (7) col7 sh gr /Times-Bold ff 150.00 scf sf 2070 1635 m d1491 2 a1492 2 /Times-Bold ff 150.00 scf sf 2070 660 m d1494 2 a1495 2 /Times-Bold ff 150.00 scf sf 1380 705 m d1497 2 a1498 2 /Times-Bold ff 150.00 scf sf 3135 1020 m d1500 2 a1501 2 /Times-Bold ff 150.00 scf sf 2070 2235 m d1503 18 a1520 5 /Times-Bold ff 150.00 scf sf 2040 2775 m gs 1 -1 sc (13) col7 sh gr /Times-Bold ff 150.00 scf sf 3068 2640 m d1522 5 a1526 5 /Times-Bold ff 150.00 scf sf 1545 3360 m gs 1 -1 sc (14) col7 sh gr /Times-Bold ff 150.00 scf sf 3337 2640 m d1528 38 d1572 108 a1679 10 2025 4944 a Fn(Figur)o(e)d(1:)66 b Fm(Illustration)18 b(of)c(the)h(machine)h(conte)o(xt)g(creation)h(procedure.)22 b(The)2350 5012 y(thick)h(solid)f(lines)h(and)e(numeric)i(marks)e (correspond)i(to)f(the)g(al-)2350 5080 y(gorithm)c(steps)g(as)f (described)i(in)f(section)h(3.2.)h(The)d(thick)i(dotted)2350 5148 y(lines)h(sho)n(w)f(a)g(possible)h(further)g(processing)h(where)f (a)f(fe)n(w)g(con-)2350 5216 y(te)o(xt)i(switches)h(are)e(performed)i (to)e(dispatch)i(between)f(the)g(main)2350 5284 y(thread)e(and)e(the)h (ne)n(w)g(created)h(thread.)p Black 1929 5700 a Fx(6)p Black eop d1681 148 a1828 102 7 6 bop Black Black 0 83 a FA(3.3)99 b(Sour)n(ce)27 b(Code)0 239 y Fx(The)43 b(corresponding)d(ANSI-C)k(code,)49 b(which)43 b(implements)0 338 y Fp(mctx)p 205 338 25 4 v 29 w(create)p Fx(,)37 b(is)d(a)h(little)f(bit)h(more)e(complicated.)64 b(But)0 438 y(with)23 b(the)g(presented)e(algorithm)g(in)i(mind,)f(it)i (is)f(no)n(w)f(straight-)0 538 y(forw)o(ard.)p Black Black 0 693 a Fi(static)44 b Fk(mctx_t)313 b(mctx_caller;)0 767 y Fi(static)44 b Fk(sig_atomic_t)f(mctx_called;)0 917 y Fi(static)h Fk(mctx_t)268 b(*mctx_creat;)0 992 y Fi(static)44 b(void)313 b Fk(\(*mctx_creat_func\)\()p Fi(void)41 b Fk(*\);)0 1066 y Fi(static)j(void)358 b Fk(*mctx_creat_arg;)0 1141 y Fi(static)44 b Fk(sigset_t)223 b(mctx_creat_sigs;)0 1290 y Fi(void)44 b Fk(mctx_create\()179 1365 y(mctx_t)g(*mctx,)179 1440 y Fi(void)g Fk(\(*sf_addr\)\()p Fi(void)f Fk(*\),)h Fi(void)g Fk(*sf_arg,)179 1515 y Fi(void)g Fk(*sk_addr,)g Fi(size_t)g Fk(sk_size\))0 1589 y Fo(f)179 1664 y Fi(struct)g Fk(sigaction)f(sa;)179 1739 y Fi(struct)h Fk(sigaction)f(osa;)179 1813 y Fi(struct)h Fk(sigaltstack)f(ss;)179 1888 y Fi(struct)h Fk(sigaltstack)f(oss;)179 1963 y(sigset_t)h(osigs;)179 2038 y(sigset_t)g(sigs;)179 2187 y(/*)64 b Fj(Step)18 b(1:)45 b Fk(*/)179 2262 y (sigemptyset\(&sigs\);)179 2336 y(sigaddset\(&sigs,)d(SIGUSR1\);)179 2411 y(sigprocmask\(SIG_BLOCK,)f(&sigs,)j(&osigs\);)179 2561 y(/*)64 b Fj(Step)18 b(2:)45 b Fk(*/)179 2635 y(memset\(\()p Fi(void)e Fk(*\)&sa,)h(0,)493 2710 y Fi(sizeof)p Fk(\()p Fi(struct)f Fk(sigaction\)\);)179 2785 y(sa.sa_handler)g(=)i (mctx_create_trampoline;)179 2860 y(sa.sa_flags)e(=)i(SA_ONSTACK;)179 2934 y(sigemptyset\(&sa.sa_mask\);)179 3009 y(sigaction\(SIGUSR1,)d (&sa,)i(&osa\);)179 3158 y(/*)64 b Fj(Step)18 b(3:)45 b Fk(*/)179 3233 y(ss.ss_sp)178 b(=)45 b(sk_addr;)179 3308 y(ss.ss_size)88 b(=)45 b(sk_size;)179 3383 y(ss.ss_flags)e(=)i(0;) 179 3457 y(sigaltstack\(&ss,)d(&oss\);)179 3607 y(/*)64 b Fj(Step)18 b(4:)45 b Fk(*/)179 3681 y(mctx_creat)268 b(=)44 b(mctx;)179 3756 y(mctx_creat_func)f(=)h(sf_addr;)179 3831 y(mctx_creat_arg)88 b(=)44 b(sf_arg;)179 3906 y(mctx_creat_sigs)f (=)h(osigs;)179 3980 y(mctx_called)223 b(=)44 b(FALSE;)179 4055 y(kill\(getpid\(\),)f(SIGUSR1\);)179 4130 y(sigfillset\(&sigs\);) 179 4204 y(sigdelset\(&sigs,)f(SIGUSR1\);)179 4279 y Fi(while)i Fk(\(!mctx_called\))359 4354 y(sigsuspend\(&sigs\);)179 4503 y(/*)64 b Fj(Step)18 b(6:)45 b Fk(*/)179 4578 y (sigaltstack\(NULL,)d(&ss\);)179 4653 y(ss.ss_flags)h(=)i(SS_DISABLE;) 179 4728 y(sigaltstack\(&ss,)d(NULL\);)179 4802 y(if)j (\(!\(oss.ss_flags)d(&)j(SS_DISABLE\)\))359 4877 y(sigaltstack\(&oss,)d (NULL\);)179 4952 y(sigaction\(SIGUSR1,)g(&osa,)i(NULL\);)179 5026 y(sigprocmask\(SIG_SETMASK,)717 5101 y(&osigs,)g(NULL\);)179 5251 y(/*)64 b Fj(Step)18 b(7)h(&)g(Step)f(8:)45 b Fk(*/)179 5325 y(mctx_switch\(&mctx_caller,)c(mctx\);)2204 83 y(/*)64 b Fj(Step)18 b(14:)45 b Fk(*/)2204 158 y Fi(return)p Fk(;)2025 232 y Fo(g)2025 382 y Fi(void)f Fk(mctx_create_trampoline\()p Fi(int)d Fk(sig\))2025 457 y Fo(f)2204 531 y Fk(/*)64 b Fj(Step)18 b(5:)45 b Fk(*/)2204 606 y Fi(if)g Fk (\(mctx_save\(mctx_creat\))c(==)j(0\))h Fo(f)2384 681 y Fk(mctx_called)e(=)h(TRUE;)2384 756 y Fi(return)p Fk(;)2204 830 y Fo(g)2204 980 y Fk(/*)64 b Fj(Step)18 b(9:)45 b Fk(*/)2204 1054 y(mctx_create_boot\(\);)2025 1129 y Fo(g)2025 1279 y Fi(void)f Fk(mctx_create_boot\()p Fi(void)p Fk(\))2025 1353 y Fo(f)2204 1428 y Fi(void)g Fk(\(*mctx_start_func\)\()p Fi(void)d Fk(*\);)2204 1503 y Fi(void)j Fk(*mctx_start_arg;)2204 1652 y(/*)64 b Fj(Step)18 b(10:)45 b Fk(*/)2204 1727 y(sigprocmask\(SIG_SETMASK,)2742 1802 y(&mctx_creat_sigs,)d(NULL\);) 2204 1951 y(/*)64 b Fj(Step)18 b(11:)45 b Fk(*/)2204 2026 y(mctx_start_func)e(=)h(mctx_creat_func;)2204 2100 y(mctx_start_arg)88 b(=)44 b(mctx_creat_arg;)2204 2250 y(/*)64 b Fj(Step)18 b(12)h(&)g(Step)g(13:)45 b Fk(*/)2204 2325 y(mctx_switch\(mctx_creat,)c(&mctx_caller\);)2204 2474 y(/*)64 b Fj(The)18 b(thread)h(``magically'')g(starts...)62 b Fk(*/)2204 2549 y(mctx_start_func\(mctx_start_arg\);)2204 2698 y(/*)45 b(NOTREACHED)e(*/)2204 2773 y(abort\(\);)2025 2848 y Fo(g)2025 3124 y FA(3.4)99 b(Run-time)26 b(P)n(enalty)2025 3294 y Fx(After)31 b(this)h(discussion)f(of)g(the)g(implementation)e (details,)35 b(an)2025 3393 y(ob)o(viously)29 b(occuring)h(question)g (no)n(w)h(is)h(what)g(the)f(e)o(xpected)2025 3493 y(run-time)g(penalty) h(is.)63 b(That)32 b(is,)k(what)d(does)f(our)g(presented)2025 3592 y(machine)43 b(conte)o(xt)f(implementation)g(cost)i(compared)e(to) i(a)2025 3692 y Fp(ucontext)p Fx(\(3\))33 b(based)j(solution.)70 b(From)35 b(the)g(already)f(dis-)2025 3792 y(cussed)21 b(details)h(we)g(can)f(easily)h(guess)f(that)h(our)e(comple)o(x)g(ma-) 2025 3891 y(chine)27 b(conte)o(xt)f(creation)h(procedure)d(\()p Fp(mctx)p 3392 3891 V 29 w(create)p Fx(\))j(will)2025 3991 y(be)20 b(certainly)f(noticeably)f(slo)n(wer)i(than)g(a)g (solution)f(based)h(on)f(a)2025 4091 y Fp(ucontext)p Fx(\(3\))f(f)o(acility)-5 b(.)2150 4197 y(But)48 b(a)g(wild)g(guess)f (is)i(not)e(suf)n(\002cing)g(for)g(a)h(reason-)2025 4297 y(able)d(statement.)101 b(So)45 b(we)h(ha)n(v)o(e)f(written)g(a)h Fr(Simple)f(Ma-)2025 4397 y(c)o(hine)21 b(Conte)n(xt)g(Benc)o(hmark)g Fx(\(SMCB)i([32)n(]\))f(which)f(w)o(as)h(used)2025 4496 y(to)36 b(compare)e(run-time)g(costs)j(of)e(the)h Fp(mctx)p 3421 4496 V 29 w(create)f Fx(and)2025 4596 y Fp(mctx)p 2230 4596 V 29 w(switch)27 b Fx(functions)f(if)h(once)f(implemented)g (through)2025 4696 y(the)17 b Fr(POSIX)i Fp(makecontext)p Fx(\(3\)/)p Fp(swapcontext)p Fx(\(3\))11 b(func-)2025 4795 y(tions)55 b(\(as)h(sho)n(wn)f(in)g(section)h(2.3\),)63 b(and)55 b(once)f(imple-)2025 4895 y(mented)44 b(with)g(our)g(based)g (f)o(allback)g(implementation)f(\(for)2025 4994 y(con)m(v)o(enience)c (reasons)j(we)h(directly)e(used)h Fp(sigjmp)p 3706 4994 V 29 w(buf)p Fx(,)2025 5094 y Fp(sigsetjmp)p Fx(\(3\))g(and)h Fp(siglongjmp)p Fx(\(3\))f(in)i(the)g(bench-)2025 5194 y(mark,)31 b(because)d(all)i(tested)g(platforms)e(pro)o(vided)e (this\).)53 b(The)2025 5293 y(results)21 b(are)f(sho)n(wn)f(T)-7 b(able)20 b(3)h(belo)n(w)-5 b(.)2150 5400 y(As)32 b(one)e(can)h(deri)n (v)o(e)f(from)g(these)i(e)n(v)n(aluations,)g(our)e(sig-)p Black 1929 5700 a(7)p Black eop d1830 148 a1977 162 8 7 bop Black Black 0 83 a Fx(nal)25 b(stack)f(trick)h(to)f(implement)f Fp(mctx)p 1174 83 25 4 v 30 w(create)h Fx(in)g(practice)0 183 y(is)31 b(approximately)c(15)j(times)h(slo)n(wer)f(than)f(the)h Fp(makecon-)0 282 y(text)p Fx(\(3\))36 b(based)i(v)n(ariant.)77 b(This)38 b(cost)g(should)f(not)g(be)h(ne-)0 382 y(glected.)f(On)25 b(the)f(other)g(hand,)g(the)h Fp(sigsetjmp)p Fx(\(3\)/)d Fp(sig-)0 482 y(longjmp)p Fx(\(3\))29 b(based)i Fp(mctx)p 903 482 V 29 w(switch)g Fx(performs)f(about)g(as)0 581 y(good)17 b(as)h(the)g Fp(swapcontext)p Fx(\(3\))e(based)i(v)n(ariant)f (\(the)g(reason)0 681 y(why)29 b(on)g(most)g(of)h(the)f(tested)h (platforms)e(it)j(is)f(e)n(v)o(en)f(slightly)0 780 y(f)o(aster)37 b(is)h(not)f(kno)n(wn)e(\226)i(b)n(ut)g(we)g(guess)g(it)h(is)g(related) e(to)h(a)0 880 y(greater)26 b(management)f(o)o(v)o(erhead)f(in)k(the)f Fp(ucontext)p Fx(\(3\))e(f)o(a-)0 980 y(cility)-5 b(,)16 b(which)f(is)i(a)f(superset)f(of)g(the)h(functionality)e(we)h (require\).)0 1079 y(Or)h(in)g(short:)22 b(our)15 b(presented)g(f)o (allback)g(approach)e(costs)j(notice-)0 1179 y(able)24 b(e)o(xtra)g(CPU)h(c)o(ycles)f(on)g(thread)f(creation)g(time,)i(b)n(ut) g(is)g(as)0 1279 y(f)o(ast)17 b(as)g(the)g(standardized)d(solution)i (under)f(thread)g(dispatching)0 1378 y(time.)p 0 1492 1899 4 v 0 2931 4 1440 v 78 1562 a Fn(10000)j Ff(\002)e Fn(mctx)p 475 1562 20 4 v 25 w(cr)o(eate)j(\(in)e(seconds\):)78 1710 y(Platf)n(orm)p 827 1734 4 79 v 561 w(mcsc)p 1080 1734 V 165 w(sjlj)p 1332 1734 V 103 w(o)o(v)o(erhead)78 1789 y Fm(Sun)g(Solaris)h(2.6)f(\(SP)-6 b(ARC\))p 827 1813 V 133 w(0.076)p 1080 1813 V 104 w(1.268)p 1332 1813 V 247 w(16.7)78 1868 y(DEC)16 b(T)n(ru64)h(5.0)g(\(Alpha\))p 827 1892 V 171 w(0.019)p 1080 1892 V 104 w(0.235)p 1332 1892 V 247 w(12.4)78 1947 y(SGI)g(IRIX)g(6.5)f(\(MIPS\))p 827 1971 V 227 w(0.105)p 1080 1971 V 104 w(1.523)p 1332 1971 V 247 w(14.5)78 2026 y(SCO)h(UnixW)-5 b(are)18 b(7.0)e(\(Intel\))p 827 2050 V 105 w(0.204)p 1080 2050 V 104 w(3.827)p 1332 2050 V 247 w(18.8)78 2105 y(HP)h(HP/UX)f(11.0)h(\(HPP)-6 b(A\))p 827 2128 V 144 w(0.057)p 1080 2128 V 104 w(0.667)p 1332 2128 V 247 w(11.8)1192 2184 y Fn(A)f(v)o(erage:)89 b(14.8)78 2311 y(10000)18 b Ff(\002)e Fn(mctx)p 475 2311 20 4 v 25 w(switch)i(\(in)f(seconds\):)78 2410 y(Platf)n(orm)p 827 2433 4 79 v 561 w(mcsc)p 1080 2433 V 165 w(sjlj)p 1332 2433 V 103 w(o)o(v)o(erhead)78 2488 y Fm(Sun)g(Solaris)h(2.6)f (\(SP)-6 b(ARC\))p 827 2512 V 133 w(0.137)p 1080 2512 V 104 w(0.210)p 1332 2512 V 281 w(1.5)78 2567 y(DEC)16 b(T)n(ru64)h(5.0)g(\(Alpha\))p 827 2591 V 171 w(0.034)p 1080 2591 V 104 w(0.022)p 1332 2591 V 281 w(0.6)78 2646 y(SGI)g(IRIX)g(6.5)f(\(MIPS\))p 827 2670 V 227 w(0.235)p 1080 2670 V 104 w(0.190)p 1332 2670 V 281 w(0.8)78 2725 y(SCO)h(UnixW)-5 b(are)18 b(7.0)e(\(Intel\))p 827 2749 V 105 w(0.440)p 1080 2749 V 104 w(0.398)p 1332 2749 V 281 w(0.9)78 2804 y(HP)h(HP/UX)f(11.0)h(\(HPP)-6 b(A\))p 827 2828 V 144 w(0.106)p 1080 2828 V 104 w(0.065)p 1332 2828 V 281 w(0.6)1225 2883 y Fn(A)f(v)o(erage:)90 b(0.9)p 1896 2931 4 1440 v 0 2934 1899 4 v 0 3022 a(T)-6 b(able)18 b(3:)67 b Fm(Summary)20 b(of)h Fl(Simple)i(Mac)o(hine)g(Conte)o(xt)g (Benc)o(hmark)g Fm(\(SMCB,)295 3090 y([32]\).)d(The)d(speed)g(of)g (machine)h(conte)o(xt)h(creation)g(and)e(switching)295 3158 y(found)25 b(on)g(each)h(tested)g(operating)i(system.)44 b Fn(mcsc)p Fm(:)38 b(functional)295 3226 y Fh(makecontext)p Fm(\(3\))11 b(/)i Fh(switchcontext)p Fm(\(3\),)f Fn(sjlj)p Fm(:)20 b(functional)295 3293 y Fh(sigsetjmp)p Fm(\(3\)/)p Fh(siglongjmp)p Fm(\(3\).)58 b Fn(o)o(v)o(erhead)p Fm(:)49 b(the)31 b(o)o(v)o(er)o(-)295 3359 y(head)18 b(of)f(using)g Fn(sjlj)g Fm(instead)i(of)e Fn(mcsc)p Fm(.)0 3573 y FA(3.5)99 b(Remaining)25 b(Issues)0 3730 y Fx(The)20 b(presented)e(algorithm)h (and)g(source)g(code)h(can)f(be)h(directly)0 3829 y(used)29 b(in)h(practice)f(for)f(implementing)g(a)i(minimal)e(threading)0 3929 y(system)e(or)g(the)g(concept)e(of)i(co-routines.)40 b(Its)27 b(big)f(adv)n(antage)0 4029 y(is)20 b(that)f(if)g(the)g (operating)e(system)i(pro)o(vides)e(the)i(required)e(stan-)0 4128 y(dardized)j(primiti)n(v)o(es,)g(we)h(do)g(not)g(need)f(to)i(kno)n (w)e(an)o(ything)f(at)0 4228 y(all)28 b(about)e(the)h(machine)g(we)g (are)g(running)f(on)g(\227)i(e)n(v)o(erything)0 4327 y(just)d(w)o(orks.)38 b(Ne)n(v)o(ertheless,)25 b(there)f(remain)g(a)h (fe)n(w)f(special)h(is-)0 4427 y(sues)c(we)f(still)i(ha)n(v)o(e)d(to)i (discuss.)0 4577 y FA(The)26 b(W)-6 b(aggly)23 b(longjmp\(3\))i(after)h (Retur)o(n)0 4702 y Fx(On)e(some)f(platforms,)h Fp(longjmp)p Fx(\(3\))e(may)h(not)g(be)h(called)g(af-)0 4802 y(ter)30 b(the)g(function)e(which)i(called)f(the)h Fp(setjmp)p Fx(\(3\))f(returned.)0 4902 y(When)22 b(this)i(is)f(done,)f(the)h (stack)g(frame)e(situation)i(is)g(not)f(guar)n(-)0 5001 y(anteed)k(to)h(be)g(in)f(a)i(clean)e(and)g(consistent)h(state.)45 b(But)27 b(this)h(is)0 5101 y(e)o(xactly)c(the)h(mechanism)f(we)h(use)g (in)g(order)f(to)h(get)g(rid)g(of)g(the)0 5200 y(signal)20 b(handler)f(scope)h(in)g(step)g(5.)125 5300 y(The)c(only)g(alternati)n (v)o(e)g(w)o(ould)g(be)h(to)g(lea)n(v)o(e)g(the)g(signal)f(han-)0 5400 y(dler)27 b(via)g Fp(longjmp)p Fx(\(3\),)g(b)n(ut)g(then)g(we)h(w) o(ould)e(ha)n(v)o(e)h(another)2025 83 y(problem,)f(as)h(e)o(xperience)d (sho)n(wed.)41 b(F)o(or)26 b(instance,)j(R)q Fm(O)t(B)t(E)t(RT)2027 183 y Fx(S)t(.)k(T)t Fm(H)t(AU)r Fx(')-5 b(s)29 b Fr(Really)e(Simple)g (Thr)m(eads)h Fx(\()p Fr(r)o(sthr)m(eads)p Fx(\))f([27)o(])h(w)o(as) 2025 282 y(ported)17 b(to)h(se)n(v)o(eral)f(platforms)g(and)g(w)o(as)i (used)f(to)g(run)f(an)h(e)o(xper)n(-)2025 382 y(imental)23 b(multithreaded)d(v)o(ersion)i(of)h(the)g(Apache)f(webserv)o(er)-5 b(.)2027 482 y(T)t Fm(H)t(AU)r Fx(')g(s)18 b(approach)d(w)o(as)i (similar)g(to)g(ours,)g(b)n(ut)f(dif)n(fered)f(signif-)2025 581 y(icantly)k(in)h(the)f(w)o(ay)g(the)h(signal)f(handler)f(is)i (left.)25 b(In)19 b(particular)m(,)2025 681 y(in)27 b(an)f(attempt)g (to)h(a)n(v)n(oid)f(the)g(unsafe)g(stack)h(frame,)g(it)g(used)f(a)2025 780 y Fp(longjmp)p Fx(\(3\))19 b(call)i(to)g(lea)n(v)o(e)f(the)h (signal)f(handler)m(,)f(rather)h(than)2025 880 y(returning)27 b(from)h(it.)51 b(But)30 b(this)f(approach)e(does)h(not)h(w)o(ork)f(on) 2025 980 y(some)20 b Fr(SysV)6 b Fx(-deri)n(v)o(ed)18 b(k)o(ernels,)h(as)i(we)g(already)e(mentioned.)2150 1079 y(The)31 b(problem)e(is)k(that)e(these)h(k)o(ernels)f(do)g(not)g (\223belie)n(v)o(e\224)2025 1179 y(that)e(the)g(code)g(is)h(out)e(of)h (the)g(signal-handling)e(conte)o(xt,)i(un-)2025 1279 y(til)k(the)f(signal)f(handler)g(has)h(returned)e(\227)i(and)g (accordingly)-5 b(,)2025 1378 y(refuse)28 b(to)g(allo)n(w)g (readjustment)f(of)g(the)i(signal)f(stack)g(until)g(it)2025 1478 y(has.)61 b(But)32 b(with)g(the)h Fr(r)o(sthr)m(eads)f Fx(approach,)g(the)h(signal)e(han-)2025 1577 y(dler)23 b(that)f(created)h(the)f(\002rst)i(thread)e(ne)n(v)o(er)f(returns,)i (and)f(when)2025 1677 y Fr(r)o(sthr)m(eads)17 b Fx(w)o(ants)g(to)g (create)g(the)g(second)f(thread,)g(these)h(k)o(ernels)2025 1777 y(refuse)23 b(to)g(readjust)g(the)h(signal)f(stack,)h(and)f(we)h (are)f(stuck.)34 b(So)2025 1876 y(with)17 b(portability)e(in)i(mind,)f (we)h(decided)e(that)i(it)g(is)h(better)e(to)h(get)2025 1976 y(rid)23 b(of)f(the)h(signal)g(handler)f(scope)g(with)h(the)g (straight-forw)o(ard)2025 2076 y(\223)p Fp(return)p Fx(\224)38 b(and)h(instead)g(\002ght)g(the)g(mentioned)f(\(simpler\))2025 2175 y(problem)18 b(of)i(an)h(unsafe)e(stack)h(frame.)2150 2275 y(F)o(ortunately)-5 b(,)24 b(in)i(practice)e(this)i(is)h(not)e(as) h(problematic)e(as)2025 2374 y(it)j(seems,)h(because)e(e)n(v)n (aluations)f(\(for)h Fr(GNU)h(Pth)p Fx(\))f(on)g(a)h(wide)2025 2474 y(range)35 b(of)g(current)g(Unix)g(platforms)g(sho)n(wed)g(that)h (one)f(can)2025 2574 y(reach)21 b(a)g(safe)h(stack)f(frame)f(again)h (by)f(just)i(calling)f(a)h(function.)2025 2673 y(That)k(is)i(the)e (reason)g(why)f(our)h(algorithm)f(enters)h(the)g(second)2025 2773 y(trampoline)18 b(function)h(in)h(step)h(9.)2025 2918 y FA(The)26 b(Uncooperati)o(v)o(e)g(longjmp\(3\))2025 3040 y Fx(Ev)o(en)e(on)i(operating)d(systems)j(which)f(ha)n(v)o(e)g(w)o (orking)f Fr(POSIX)2025 3140 y Fx(functions,)c(our)g(approach)f(may)h (theoretically)g(still)i(not)e(w)o(ork,)2025 3240 y(because)48 b Fp(longjmp)p Fx(\(3\))f(does)i(not)f(cooperate.)109 b(F)o(or)49 b(in-)2025 3339 y(stance,)17 b(on)e(some)h(platforms)f(the) h(standard)f Fr(libc)i Fp(longjmp)p Fx(\(3\))2025 3439 y(branches)d(to)h(error)n(-handling)d(code)j(if)g(it)h(detects)g(that)f (the)h(caller)2025 3539 y(tries)j(to)h(jump)e Fr(up)g Fx(the)h(stack,)g Fr(i.e)o(.)p Fx(,)g(into)g(a)g(stack)g(frame)f(that)h (has)2025 3638 y(already)g(returned.)2150 3738 y(This)28 b(is)h(usually)f(implemented)f(by)h(comparing)e(the)i(cur)n(-)2025 3837 y(rent)f(stack)g(pointer)f(to)h(the)g(one)f(in)h(the)g Fp(jmp)p 3387 3837 25 4 v 30 w(buf)g Fx(structure.)2025 3937 y(That)j(is)h(why)e(it)i(is)g(important)e(for)g(our)h(algorithm)e (to)j(return)2025 4037 y(from)19 b(the)g(signal)h(handler)e(and)h(this) h(w)o(ay)f(enter)g(the)h(\(dif)n(ferent\))2025 4136 y(stack)31 b(of)g(the)h(parent)e(thread.)57 b(In)31 b(practice,)i(the)e(implemen-) 2025 4236 y(tation)26 b(in)g Fr(GNU)h(Pth)f Fx(sho)n(wed)g(that)g(then) g(one)f(no)h(longer)f(suf-)2025 4336 y(fers)16 b(from)f(those)g (uncooperati)n(v)o(e)d Fp(longjmp)p Fx(\(3\))i(implementa-)2025 4435 y(tions,)k(b)n(ut)f(one)f(should)h(k)o(eep)f(this)i(point)e(in)i (mind)e(when)h(reach-)2025 4535 y(ing)29 b(e)n(v)o(en)f(more)h (uncooperati)n(v)o(e)d(v)n(ariants)j(on)g(esoteric)g(Unix)2025 4634 y(platforms.)22 b(If)16 b(it)h(still)g(occurs,)f(one)g(can)g(only) f(try)h(to)g(resume)g(the)2025 4734 y(operation)24 b(by)h(using)f(a)i (possibly)f(e)o(xisting)g(platform-speci\002c)2025 4834 y(error)19 b(handling)f(hook.)2025 4979 y FA(Garbage)25 b(at)g(Bottom)g(of)g(Stacks)2025 5101 y Fx(There)17 b(is)i(a)f(subtle)f (side-ef)n(fect)g(of)g(our)g(implementation:)22 b(there)2025 5201 y(remains)16 b(some)h(garbage)e(at)j(the)f(bottom)e(of)i(each)g (thread)f(stack.)2025 5300 y(The)33 b(reason)g(is)i(that)e(if)h(a)g (signal)g(is)g(deli)n(v)o(ered,)h(the)f(operat-)2025 5400 y(ing)d(system)g(pushes)f(some)h(state)h(onto)e(the)h(stack,)i (which)e(is)p Black 1929 5700 a(8)p Black eop d1979 81 a2059 373 9 8 bop Black Black 0 83 a Fx(restored)27 b(later)m(,)j(when)e(the)h (signal)f(handler)f(returns.)48 b(But)29 b(al-)0 183 y(though)h(we)h(return)f(from)h(the)g(signal)g(handler)m(,)h(we)g(jump) e(in)0 282 y(again,)c(and)g(this)h(time)f(we)g(enter)g(not)g(directly)f (at)i(the)f(bottom)0 382 y(of)17 b(the)g(stack,)g(because)f(of)h(the)g Fp(setjmp)p Fx(\(3\))e(in)i(the)g(trampoline)0 482 y(function.)125 587 y(Since)31 b(the)h(operating)d(system)j(has)f(to)h(capture)e(all)j (CPU)0 687 y(re)o(gisters)22 b(\(including)d(those)j(that)g(are)g (ordinarily)e(scratch)i(re)o(g-)0 787 y(isters)f(or)f(caller)n(-sa)n(v) o(e)g(re)o(gisters\),)f(there)h(can)g(be)g(a)h(f)o(air)f(amount)0 886 y(of)h(memory)e(at)i(the)h(bottom)d(of)i(the)g(established)g (thread)f(stack.)0 986 y(F)o(or)d(some)h(systems)g(this)g(can)g(be)g(e) n(v)o(en)e(up)i(to)g(1)g(KB)g(of)g(garbage)0 1085 y([27)o(].)25 b(But)19 b(e)o(xcept)g(for)f(the)i(additional)d(memory)h(consumption)0 1185 y(it)j(does)f(not)g(hurt.)125 1291 y(W)-7 b(e)32 b(just)g(ha)n(v)o(e)f(to)h(k)o(eep)f(in)h(mind)f(this)h(additional)e (stack)0 1391 y(consumption)25 b(when)j(deciding)e(the)i(stack)g(size)h (\()p Fr(sk)p 1569 1391 25 4 v 29 w(size)p Fx(\).)49 b(A)0 1490 y(reasonable)23 b(stack)i(size)g(usually)f(is)i(between)e (16)g(and)g(32)g(KB.)0 1590 y(Less)31 b(is)h(neither)e(reasonable)f (nor)h(al)o(w)o(ays)h(allo)n(wed)f(\(current)0 1689 y(Unix)c(platforms) f(usually)h(require)f(a)i(stack)g(to)f(be)h(at)g(least)g(16)0 1789 y(KB)21 b(in)f(size\).)0 1957 y FA(Stack)26 b(Ov)o(er\003o)o(ws)0 2094 y Fx(There)47 b(is)j(a)e(noticeable)f(dif)n(ference)g(between)g (the)h(initial)0 2193 y Fp(main)p Fx(\(\))29 b(thread)f(and)h(the)h(e)o (xplicitly)f(spa)o(wned)f(threads:)44 b(the)0 2293 y(initial)31 b(thread)f(runs)g(on)h(the)f(standard)g(process)g(stack.)57 b(This)0 2393 y(stack)43 b(automatically)e(can)h(gro)n(w)g(under)f (Unix,)47 b(while)c(the)0 2492 y(stacks)22 b(of)g(the)g(spa)o(wned)f (threads)g(are)h(\002x)o(ed)f(in)h(size.)31 b(So)22 b(stack)0 2592 y(o)o(v)o(er\003o)n(ws)k(can)h(occur)f(for)h(the)g(spa)o(wned)f (threads.)45 b(This)28 b(im-)0 2692 y(plies)g(that)g(the)g(parent)f (has)h(to)h(mak)o(e)e(a)h(reasonable)f(guess)h(of)0 2791 y(the)22 b(threads)f(stack)h(space)g(requirement)d(already)i(at)h(spa)o (wning)0 2891 y(time.)125 2997 y(And)g(there)g(is)i(no)e(really)h (portable)e(solution)h(to)h(this)g(prob-)0 3096 y(lem,)32 b(because)d(e)n(v)o(en)f(if)i(the)g(thread)f(library')-5 b(s)28 b(scheduler)h(can)0 3196 y(detect)16 b(the)g(stack)g(o)o(v)o (er\003o)n(w)-5 b(,)15 b(it)i(cannot)e(easily)h(resize)g(the)h(stack.)0 3296 y(The)34 b(reason)g(is)h(simply)f(that)g(the)h(stack)f (initialization)g(goes)0 3395 y(hand)26 b(in)h(hand)f(with)i(the)f (initialization)f(of)h(the)g(start)h(routine,)0 3495 y(as)d(we)g(discussed)f(before.)36 b(And)24 b(this)h(start)g(routine)e (has)i(to)g(be)0 3594 y(a)e(real)g(C)h(function)d(in)i(order)f(to)g Fr(call)p Fx(.)33 b(But)24 b(once)e(the)h(thread)f(is)0 3694 y(running,)i(there)g(no)h(longer)f(e)o(xists)h(such)g(an)g(entry)f (point.)39 b(So,)0 3794 y(e)n(v)o(en)30 b(if)h(the)g(scheduler)f(w)o (ould)g(be)h(able)f(to)h(gi)n(v)o(e)g(the)g(thread)0 3893 y(a)g(ne)n(w)g(enlar)o(ged)e(stack,)34 b(there)c(is)i(no)e(chance) g(to)i(restart)f(the)0 3993 y(thread)19 b(on)h(this)h(ne)n(w)f(stack.) 125 4099 y(Or)i(more)f(correct,)g(there)g(is)i(no)f Fr(portable)f Fx(w)o(ay)h(to)g(achie)n(v)o(e)0 4198 y(it.)j(As)18 b(with)g(the)g(pre) n(vious)e(problems,)g(there)h(is)i(a)f(non-portable)0 4298 y(solution.)49 b(That)28 b(is)i(why)d(our)h(implementation)e(did)j (not)f(deal)0 4398 y(with)g(this)h(issue.)48 b(Instead)28 b(in)g(practice)f(one)g(usually)h(lets)h(the)0 4497 y(scheduler)16 b(just)h(detect)g(the)g(stack)h(o)o(v)o(er\003o)n(w)d(and)h(terminate)h (the)0 4597 y(thread.)25 b(This)c(is)h(done)d(by)i(using)f(a)h(red)f (zone)g(at)h(the)g(top)f(of)h(the)0 4696 y(stack)i(which)g(is)h(mark)o (ed)e(with)h(a)h(magic)e(v)n(alue)h(the)g(scheduler)0 4796 y(checks)d(between)f(thread)g(dispatching)g(operations.)125 4902 y(Resizing)g(solutions)f(are)h(only)f(possible)h(in)g (semi-portable)0 5001 y(w)o(ays.)39 b(One)25 b(approach)e(is)j(to)f (place)f(the)h(thread)f(stacks)i(into)e(a)0 5101 y(memory)g(mapped)h (area)h(\(see)h Fp(mmap)p Fx(\(2\)\))d(of)i(the)g(process)g(ad-)0 5201 y(dress)31 b(space)f(and)g(let)h(the)g(scheduler)e(catch)i Fp(SIGSEGV)e Fx(sig-)0 5300 y(nals.)70 b(When)34 b(such)h(a)g(signal)g (occurs,)j(because)c(of)h(a)g(stack)0 5400 y(o)o(v)o(er\003o)n(w)i(in)j (this)f(area,)44 b(the)39 b(scheduler)f(e)o(xplicitly)g(resizes)2025 83 y(the)c(memory)e(mapped)h(area.)66 b(This)34 b(resizing)g(can)g(be)g (done)2025 183 y(either)e(by)f(cop)o(ying)f(the)i(stack)h(contents)e (into)h(a)g(ne)n(w)g(lar)o(ger)2025 282 y(area)c(which)g(is)h(then)f (re-mapped)e(to)i(the)h(old)f(address)g(or)g(via)2025 382 y(an)k(e)n(v)o(en)f(more)g(ele)o(gant)f(w)o(ay)-5 b(,)34 b(as)f(the)f(v)o(endor)d(threading)h(li-)2025 482 y(braries)e(of)g Fr(Sun)f(Solaris)p Fx(,)j Fr(F)-5 b(r)m(eeBSD)28 b Fx(and)g Fr(DEC)h(T)-5 b(ru64)28 b Fx(do)g(it:)2025 581 y(the)j(thread)g(stacks)h(are)f(allocated)g(inside)g(memory)f (mapped)2025 681 y(areas)g(which)f(are)h(already)e(initially)i(a)g(fe)n (w)g(MB)g(in)g(\(virtual\))2025 780 y(size)25 b(and)f(then)g(one)h (just)g(relies)g(on)f(the)g(virtual)g(memory)f(sys-)2025 880 y(tem')-5 b(s)31 b(feature)e(that)h(only)f(the)h(actually)g (consumed)e(memory)2025 980 y(space)20 b(is)h(mapped.)2025 1149 y FA(Startup)26 b(Routine)g(T)-9 b(ermination)2025 1287 y Fx(There)48 b(is)i(a)g(cruel)e Fp(abort)p Fx(\(3\))g(call)h(at)h (the)f(end)g(of)f(our)2025 1387 y Fp(mctx)p 2230 1387 V 29 w(create)p 2559 1387 V 29 w(boot)d Fx(function.)97 b(This)45 b(means,)50 b(if)c(the)2025 1486 y(startup)18 b(routine)g(w)o(ould)g(return,)f(the)i(process)f(is)i(aborted.)j(That) 2025 1586 y(is)29 b(ob)o(viously)e(not)h(reasonable,)h(so)f(why)g(ha)n (v)o(e)g(we)h(written)f(it)2025 1685 y(this)21 b(w)o(ay?)2150 1792 y(If)15 b(the)g(thread)f(returns)h(from)f(the)h(startup)g (routine,)g(it)h(should)2025 1891 y(be)22 b(cleanly)f(terminated.)29 b(But)23 b(it)g(cannot)e(terminate)g(itself)i(\(for)2025 1991 y(instance,)g(because)f(it)h(cannot)f(free)g(its)i(o)n(wn)e(stack) h(while)g(run-)2025 2091 y(ning)31 b(on)g(it,)j Fr(etc.)p Fx(\).)59 b(So)31 b(the)h(termination)e(handling)f(actually)2025 2190 y(is)g(the)f(task)g(of)g(the)f(thread)g(library)g(scheduler)-5 b(.)47 b(As)29 b(a)f(conse-)2025 2290 y(quence,)21 b(the)h(thread)f (spa)o(wning)g(function)f(of)i(a)h(thread)e(library)2025 2389 y(should)e(be)h(not)g(directly)g Fp(mctx)p 2973 2389 V 29 w(create)p Fx(.)2150 2496 y(Instead)27 b(the)g(thread)g(spa)o (wning)f(function)g(should)h(use)h(an)2025 2595 y(additional)17 b(trampoline)g(function)f(as)j(the)g(higher)n(-le)n(v)o(el)d(startup) 2025 2695 y(routine.)23 b(And)18 b(this)h(trampoline)e(function)g (performs)g(a)i(conte)o(xt)2025 2795 y(switch)26 b(back)f(into)h(the)f (thread)g(library)g(scheduler)f(before)h(the)2025 2894 y(lo)n(wer)n(-le)n(v)o(el)f(startup)h(routine)f(w)o(ould)g(return.)39 b(The)25 b(scheduler)2025 2994 y(then)g(can)h(safely)f(remo)o(v)o(e)f (the)i(thread)e(and)h(its)i(machine)d(con-)2025 3094 y(te)o(xt.)35 b(That)24 b(is)g(why)f(the)h Fp(abort)p Fx(\(3\))e(call)i(is)h(ne)n(v)o(er)d(reached)g(in)2025 3193 y(practice)27 b(\(more)f(details)i(can)f(be)h(found)d(in)j(the)f (implementa-)2025 3293 y(tions)22 b(of)f Fp(pth)p 2455 3293 V 29 w(spawn)h Fx(and)f Fp(pth)p 3048 3293 V 29 w(exit)g Fx(in)h Fp(pth)p 3535 3293 V 30 w(lib.c)f Fx(of)2025 3392 y Fr(GNU)g(Pth)f Fx([25)o(]\))2025 3562 y FA(The)26 b(sigstack\(2\))f(F)n(allback)f(Situation)2025 3700 y Fx(Not)37 b(all)h(platforms)e(pro)o(vide)g(the)h(standardized)e Fp(sigalt-)2025 3799 y(stack)p Fx(\(2\).)d(Instead)22 b(the)o(y)g(at)i(least)g(pro)o(vide)c(the)j Fr(4.2BSD)f Fx(an-)2025 3899 y(cestor)i(function)f Fp(sigstack)p Fx(\(2\).)35 b(But)24 b(one)g(cannot)f(tri)n(vially)2025 3999 y(replace)c Fp(sigaltstack)p Fx(\(2\))e(by)i Fp(sigstack)p Fx(\(2\))f(in)i(this)g(sit-)2025 4098 y(uation,)c(because)f(in)i (contrast)e(to)i Fp(sigaltstack)p Fx(\(2\),)d(the)i(old)2025 4198 y Fp(sigstack)p Fx(\(2\))34 b(does)h(not)g(automatically)g(handle) f(the)i(ma-)2025 4297 y(chine)20 b(dependent)e(direction)g(of)i(stack)h (gro)n(wth.)2150 4404 y(Instead,)44 b(the)c(caller)g(has)g(to)g(kno)n (w)f(the)h(direction)f(and)2025 4503 y(al)o(w)o(ays)52 b(call)f Fp(sigstack)p Fx(\(2\))e(with)j(the)f(address)g(of)g(the)2025 4603 y(bottom)h(of)g(the)h(stack.)123 b(So,)61 b(in)54 b(a)f(real-w)o(orld)e(imple-)2025 4703 y(mentation)38 b(one)h(\002rst)h(has)g(to)g(determine)e(the)i(direction)e(of)2025 4802 y(stack)i(gro)n(wth)e(in)i(order)f(to)h(use)g Fp(sigstack)p Fx(\(2\))d(as)k(a)f(re-)2025 4902 y(placement)49 b(for)h Fp(sigaltstack)p Fx(\(2\).)113 b(F)o(ortunately)49 b(this)2025 5001 y(is)55 b(easier)e(than)h(it)g(seems)g(on)f(the)h(\002rst)g(look)f (\(for)g(de-)2025 5101 y(tails)43 b(see)g(the)f(macros)f Fp(AC)p 2886 5101 V 30 w(CHECK)p 3166 5101 V 29 w(STACKGROWTH)f Fx(and)2025 5201 y Fp(AC)p 2130 5201 V 30 w(CHECK)p 2410 5201 V 28 w(STACKSETUP)33 b Fx(in)h(\002le)h Fp(aclocal.m4)e Fx(from)2025 5300 y Fr(GNU)24 b(Pth)f Fx([25)o(]\).)33 b(Alternati)n(v)o(ely)21 b(if)j(one)e(can)h(af)n(ford)f(to)h(w)o(aste) 2025 5400 y(memory)-5 b(,)16 b(one)h(can)h(use)g(an)g(ele)o(gant)f (trick:)24 b(to)18 b(set)h(up)e(a)i(stack)f(of)p Black 1929 5700 a(9)p Black eop %%Page: 10 10 10 9 bop Black Black 0 83 a Fx(size)21 b Fe(N)9 b Fx(,)21 b(one)f(allocates)g(a)h(chunk)e(of)h(memory)f(\(starting)h(at)h(ad-)0 183 y(dress)c Fe(A)p Fx(\))h(of)f(size)h Fe(N)f Fd(\002)8 b Fc(2)17 b Fx(and)f(then)h(calls)h Fp(sigstack)p Fx(\(2\))d(with)0 282 y(the)20 b(parameters)f Fr(sk)p 581 282 25 4 v 30 w(addr=)p Fe(A)d Fc(+)h Fe(N)30 b Fx(and)19 b Fr(sk)p 1291 282 V 30 w(size=)p Fe(N)9 b Fx(,)21 b Fr(i.e)o(.)p Fx(,)e(one)0 382 y(speci\002es)29 b(the)g(middle)g(of)f(the)h(memory)e (chunk)h(as)h(the)g(stack)0 482 y(base.)0 651 y FA(The)d(Blind)g(Alley) e(of)h(Brain-Dead)h(Platf)n(orms)0 789 y Fx(The)31 b(w)o(orld)g(w)o (ould)f(not)i(be)f(as)h(funn)o(y)d(as)j(it)g(is,)j(if)d(really)f(all)0 888 y(Unix)f(platforms)e(w)o(ould)i(be)g(f)o(air)g(to)g(us.)55 b(Instead,)31 b(currently)0 988 y(at)24 b(least)g(one)e(platform)g(e)o (xists)i(which)f(plays)g(unf)o(air:)30 b(unfortu-)0 1088 y(nately)-5 b(,)26 b(ancient)e(v)o(ersions)h(of)g(the)g(popular)f Fr(GNU/Linux)p Fx(.)40 b(Al-)0 1187 y(though)21 b(we)i(will)h(disco)o (v)o(er)d(that)i(it)h(both)e(pro)o(vides)f Fp(sigalt-)0 1287 y(stack)p Fx(\(2\))c(and)i Fp(sigstack)p Fx(\(2\),)d(our)j (approach)d(w)o(on')o(t)i(w)o(ork)0 1387 y(on)27 b Fr(Linux)g Fx(k)o(ernels)g(prior)f(to)i(v)o(ersion)e(2.2)g(and)h Fr(glibc)g Fx(prior)f(to)0 1486 y(v)o(ersion)19 b(2.1.)125 1592 y(Why?)42 b(Because)26 b(its)h Fr(libc)g Fx(pro)o(vides)d(only)h (stubs)i(of)e(these)0 1692 y(functions)h(which)i(al)o(w)o(ays)g(return) f(just)h Fp(-1)g Fx(with)g Fp(errno)g Fx(set)0 1792 y(to)g Fp(ENOSYS)p Fx(.)f(So,)j(this)e(de\002nitely)f(means)g(that)h(our)f (nifty)g(al-)0 1891 y(gorithm)40 b(is)i(useless)h(there,)j(because)40 b(its)j(central)e(point)g Fr(is)0 1991 y Fp(sigaltstack)p Fx(\(2\)/)p Fp(sigstack)p Fx(\(2\).)31 b(Ne)n(v)o(ertheless)23 b(we)h(do)0 2091 y(not)17 b(need)g(to)h(gi)n(v)o(e)f(up.)23 b(At)18 b(least)h(not,)e(if)h(we,)g(for)f(a)h(single)g(brain-)0 2190 y(dead)29 b(platform,)i(accept)f(to)g(break)f(our)g(general)g (goal)g(of)h(not)0 2290 y(using)20 b(an)o(y)f(platform)g(dependent)f (code.)125 2396 y(So,)g(what)h(can)g(we)g(actually)f(do)g(here?)24 b(All)19 b(we)g(ha)n(v)o(e)f(to)h(do,)0 2496 y(is)f(to)f(\002ddle)g (around)e(a)j(little)g(bit)f(with)h(the)f(machine-dependent)0 2595 y Fp(jmp)p 155 2595 V 29 w(buf)23 b Fx(ingredients)f(\(by)h (poking)e(around)g(in)i Fp(setjmp.h)0 2695 y Fx(or)16 b(by)f(disassembling)g Fp(longjmp)p Fx(\(3\))f(in)j(the)f(deb)n (ugger\).)21 b(Usu-)0 2795 y(ally)e(one)f(just)h(has)g(to)f(do)g(a)h Fp(setjmp)p Fx(\(3\))e(to)i(get)f(an)h(initial)g(state)0 2894 y(in)24 b(the)g Fp(jmp)p 370 2894 V 29 w(buf)g Fx(structure)f(and) h(then)f(manually)f(adjust)i(tw)o(o)0 2994 y(of)34 b(its)h(\002elds:)54 b(the)35 b(program)d(counter)g(\(usually)i(a)h(structure)0 3094 y(member)29 b(with)i(\223)p Fp(pc)p Fx(\224)g(in)g(the)f(name\))g (and)g(the)h(stack)g(pointer)0 3193 y(\(usually)19 b(a)i(structure)e (member)g(with)h(\223)p Fp(sp)p Fx(\224)g(in)h(the)f(name\).)125 3299 y(That)f(is)i(all)f(and)f(can)g(be)h(acceptable)e(for)h(a)i (real-w)o(orld)d(im-)0 3399 y(plementation)e(which)g(really)i(w)o(ants) f(to)h(co)o(v)o(er)e(mostly)h Fr(all)h Fx(plat-)0 3499 y(forms)f(\226)i(at)f(least)h(as)g(long)e(as)i(the)f(special)h (treatment)e(is)i(needed)0 3598 y(just)31 b(for)f(one)h(or)f(tw)o(o)h (platforms.)56 b(But)31 b(one)f(has)h(to)g(k)o(eep)f(in)0 3698 y(mind)e(that)h(it)h(at)g(least)f(breaks)g(one)f(of)h(the)g (initial)g(goals)g(and)0 3798 y(has)20 b(to)h(be)f(treated)g(as)g(a)h (last)g(chance)e(solution.)0 3967 y FA(Functions)26 b(sigsetjmp\(3\))f (and)g(siglongjmp\(3\))125 4105 y Fx(One)42 b(certainly)f(w)o(ants)i (the)g Fr(POSIX)i Fx(thread)d(semantics)0 4204 y(where)50 b(a)h(thread)e(has)i(its)g(o)n(wn)f(signal)g(mask.)115 b(As)51 b(al-)0 4304 y(ready)36 b(mentioned,)i(on)e(some)h(platforms)e Fp(setjmp)p Fx(\(3\))g(and)0 4404 y Fp(longjmp)p Fx(\(3\))e(do)h(not)h (pro)o(vide)d(this)j(and)g(instead)f(one)g(has)0 4503 y(to)23 b(e)o(xplicitly)f(call)i Fp(sigsetjmp)p Fx(\(3\))d(and)h Fp(siglongjmp)p Fx(\(3\))0 4603 y(instead.)111 b(There)49 b(is)h(only)e(one)g(snare:)83 b(on)49 b(some)g(plat-)0 4703 y(forms)36 b Fp(sigsetjmp)p Fx(\(3\)/)p Fp(siglongjmp)p Fx(\(3\))31 b(sa)n(v)o(e)36 b(also)h(in-)0 4802 y(formation)g(about)i (the)g(alternate)g(signals)h(stack.)82 b(So)40 b(here)0 4902 y(one)45 b(has)h(to)f(mak)o(e)g(sure)g(that)h(although)e(the)h (thread)f(dis-)0 5001 y(patching)36 b(later)i(uses)g Fp(sigsetjmp)p Fx(\(3\)/)p Fp(siglongjmp)p Fx(\(3\),)0 5101 y(the)43 b(thread)f(creation)g(step)h(in)g Fp(mctx)p 1197 5101 V 29 w(create)g Fx(still)h(uses)0 5201 y(plain)h Fp(setjmp)p Fx(\(3\)/)p Fp(longjmp)p Fx(\(3\))d(calls)k(for)f(the)h (trampo-)0 5300 y(line)g(trick.)100 b(One)46 b(just)g(has)g(to)g(be)f (careful)g(because)g(the)0 5400 y Fp(jmp)p 155 5400 V 29 w(buf)20 b Fx(and)e Fp(sigjmp)p 793 5400 V 29 w(buf)i Fx(structures)e(cannot)g(be)i(mix)o(ed)2025 83 y(between)28 b(calls)h(to)g(the)g Fp(sigsetjmp)p Fx(\(3\)/)p Fp(siglongjmp)p Fx(\(3\))2025 183 y(and)20 b Fp(setjmp)p Fx(\(3\)/)p Fp(longjmp)p Fx(\(3\).)2025 340 y FA(Mor)n(e)25 b(Machine)h(Context)g (Ingr)n(edients)2025 470 y Fx(Finally)-5 b(,)33 b(for)d(a)h(real-w)o (orld)f(threading)f(implementation)f(one)2025 570 y(usually)j(w)o(ants) h(to)g(put)f(more)g(state)h(into)f(the)h(machine)e(con-)2025 670 y(te)o(xt)43 b(structure)f Fp(mctx)p 2731 670 V 29 w(t)p Fx(.)94 b(F)o(or)42 b(instance)h(to)g(ful\002ll)g(more)2025 769 y Fr(POSIX)25 b Fx(threading)20 b(semantics,)i(it)g(is)h (reasonable)d(to)i(also)g(sa)n(v)o(e)2025 869 y(and)37 b(restore)g(the)g(global)g Fp(errno)g Fx(v)n(ariable.)76 b(All)38 b(this)g(can)2025 968 y(be)32 b(easily)h(achie)n(v)o(ed)e(by)h (e)o(xtending)e(the)j Fp(mctx)p 3503 968 V 29 w(t)g Fx(structure)2025 1068 y(with)d(additional)f(\002elds)h(and)f(by)h(making)e(the)i Fp(mctx)p 3656 1068 V 29 w(save)p Fx(,)2025 1168 y Fp(mctx)p 2230 1168 V 29 w(restore)38 b Fx(and)h Fp(mctx)p 3007 1168 V 29 w(switch)f Fx(functions)g(to)h(be)2025 1267 y(a)o(w)o(are)20 b(of)g(them.)2025 1520 y FA(3.6)99 b(Related)26 b(W)-7 b(ork)2025 1681 y Fx(Beside)38 b Fr(GNU)h(Pth)e Fx([25)o(],)42 b(there)37 b(are)h(other)f(multithreading)2025 1781 y(libraries)c(which)f(use)i(v)n(ariants)e(of)h(the)g(presented)f (approach)2025 1880 y(for)23 b(implementing)e(machine)h(conte)o(xts)h (in)g(user)n(-space.)34 b(Most)2025 1980 y(notably)40 b(there)g(are)j(R)q Fm(O)t(B)t(E)t(RT)k Fx(S)t(.)f(T)t Fm(H)t(AU)r Fx(')-5 b(s)42 b Fr(Really)f(Simple)2025 2079 y(Thr)m(eads)18 b Fx(\()p Fr(r)o(sthr)m(eads)p Fx(,)h([27)o(]\))f (package)f(which)h(uses)h Fp(sigalt-)2025 2179 y(stack)p Fx(\(2\))h(in)i(a)g(v)o(ery)f(similar)h(w)o(ay)f(for)h(thread)e (creation,)h(and)2027 2279 y(K)r Fm(O)q(T)n(A)h Fx(A)t Fm(B)t(E)r Fx(')-5 b(s)19 b Fr(P)-7 b(ortable)17 b(Thr)m(ead)g(Libr)o (ary)g Fx(\()p Fr(PTL)p Fx(,)h([24)o(]\))f(which)2025 2378 y(uses)30 b(a)g Fp(sigstack)p Fx(\(2\))d(approach.)51 b(But)30 b(because)f(their)g(ap-)2025 2478 y(proaches)14 b(handle)h(the)h(signal)g(handler)e(scope)h(dif)n(ferently)-5 b(,)14 b(the)o(y)2025 2578 y(are)30 b(not)g(able)h(to)f(achie)n(v)o(e)f (the)i(same)f(amount)f(of)h(portability)2025 2677 y(and)23 b(this)h(w)o(ay)f(do)g(not)g(w)o(ork)g(for)g(instance)g(on)g(some)g (System-)2025 2777 y(V)-8 b(-deri)n(v)o(ed)18 b(platforms.)2025 3030 y FA(3.7)99 b(Summary)26 b(&)f(A)-10 b(v)o(ailability)2025 3190 y Fx(W)j(e)22 b(ha)n(v)o(e)e(presented)f(a)i(pragmatic)f(and)g (mostly)g(portable)g(f)o(all-)2025 3290 y(back)e(approach)e(for)i (implementing)e(the)j(machine)e(conte)o(xt)g(for)2025 3390 y(user)n(-space)30 b(threads,)j(based)e(entirely)f(on)g(Unix)h (system)g(and)2025 3489 y(ANSI-C)24 b(language)d(f)o(acilities.)35 b(The)23 b(approach)e(w)o(as)j(success-)2025 3589 y(fully)i(tested)h (in)g(practice)f(on)h(a)g(wide)g(range)e(of)i(Unix)f(\003a)n(v)n(ors) 2025 3689 y(by)17 b Fr(GNU)h(Pth)f Fx(and)g(should)f(also)h(adapt)g(to) g(the)h(remaining)d(Unix)2025 3788 y(platforms)f(as)i(long)f(as)h(the)o (y)f(adhere)f(to)i(the)f(rele)n(v)n(ant)g(standards.)2150 3891 y(The)50 b Fr(GNU)i(Pth)e Fx(package)g(is)h(distrib)n(uted)f (under)g(the)2025 3990 y(GNU)32 b(Library)f(General)g(Public)g(License) h(\(LGPL)f(2.1\))g(and)2025 4090 y(freely)40 b(a)n(v)n(ailable)h(from)e Fr(http://www)-6 b(.gnu.or)m(g/softwar)m(e/pth/)2025 4189 y Fx(and)20 b Fr(ftp://ftp.gnu.or)m(g/gnu/pth/)p Fx(.)2025 4442 y FA(3.8)99 b(Ackno)o(wledgements)2025 4603 y Fx(I)17 b(w)o(ould)f(lik)o(e)h(to)f(thank)i(R)q Fm(O)t(B)t(E)t(RT)k Fx(S)t(.)f(T)t Fm(H)t(AU)r Fx(,)f(D)q Fm(A)-5 b(V)t(I)t(D)22 b Fx(B)s Fm(U)t(T)t(E)t(N)t Fx(-)2027 4703 y Fm(H)t(O)t(F)r Fx(,)k(M)t Fm(A)t(RT)t(I)t(N)i Fx(K)t Fm(R)t(A)t(E)t(M)t(E)t(R)r Fx(,)f(E)t Fm(R)t(I)t(C)h Fx(N)t Fm(E)t(W)t(T)s(O)t(N)d Fx(and)g(B)t Fm(R)q(U)t(N)t(O)2027 4802 y Fx(H)t Fm(A)t(I)t(B)t(L)t(E)33 b Fx(for)c(their)g(comments)g (which)h(helped)e(to)i(write)h(the)2025 4902 y(initial)d(v)o(ersion)f (of)g(this)h(paper)-5 b(.)48 b(Additionally)-5 b(,)27 b(credit)g(has)h(to)2025 5001 y(be)21 b(gi)n(v)o(en)e(to)k(C)t Fm(H)t(R)t(I)t(S)t(T)s(O)t(P)t(H)t(E)t(R)j Fx(S)t Fm(M)t(A)t(L)t(L)d Fx(and)d(the)g(USENIX)h(re-)2025 5101 y(vie)n(wers)f(for)g(their)g(in)m (v)n(aluable)e(feedback)h(which)g(allo)n(wed)h(this)2025 5201 y(paper)28 b(to)h(be)f(e)o(xtended,)h(cleaned)f(up)g(and)h (\002nally)f(published)2025 5300 y(at)c(the)f(USENIX)g(Annual)f(T)-6 b(echnical)22 b(Conference)g(2000.)32 b(Fi-)2025 5400 y(nally)-5 b(,)27 b(thanks)e(go)h(to)h(all)f(users)h(of)f Fr(GNU)h(Pth)f Fx(for)f(their)h(feed-)p Black 1908 5700 a(10)p Black eop %%Page: 11 11 11 10 bop Black Black 0 83 a Fx(back)19 b(on)f(the)i(implementation,)c (which)j(helped)f(in)i(\002ne-tuning)0 183 y(the)g(presented)f (approach.)927 b([)p Fb(rse)p Fx(])0 446 y Fs(Refer)n(ences)p Black 71 615 a Fv([1])p Black 41 w Fu(POSIX)18 b(1003.1c)j(Thr)m (eading)p Fv(,)e(IEEE)f(POSIX)f(1003.1c-1995,)199 706 y(ISO/IEC)g(9945-1:1996)p Black 71 821 a([2])p Black 43 w(M)t(.)t(E)t(.)46 b(C)t Fa(O)t(N)t(W)m(A)n(Y)r Fv(:)70 b Fu(Design)44 b(of)f(a)h(separ)o(able)h(tr)o(ansition-)199 913 y(dia)o(gr)o(am)20 b(compiler)-8 b(.)p Fv(,)19 b(Comm.)f(A)m(CM)h (6:7,)g(1963,)h(p.396-408)p Black 71 1028 a([3])p Black 43 w(E)t(.)t(W)m(.)h(D)t Fa(I)t(J)t(K)t(S)t(T)t(R)t(A)r Fv(:)f Fu(Co-oper)o(ating)g(sequential)g(pr)m(ocesses)p Fv(,)g(in)199 1119 y(F)-6 b(.)24 b(Genuys)j(\(Ed.\),)e Fu(Pr)m(o)o(gr)o(amming)h(Langua)o(g)o(es)p Fv(,)j(N)m(A)-8 b(T)o(O)24 b(Ad-)199 1211 y(v)n(anced)f(Study)f(Institute,)f(Academic)i (Press,)e(London,)i(1965,)199 1302 y(p.42-112.)p Black 71 1417 a([4])p Black 43 w(B)t(.)34 b(N)t Fa(I)t(C)t(H)t(O)t(L)t(S)t Fv(,)i(D)t(.)e(B)s Fa(U)t(T)t(T)t(L)t(A)t(R)t Fv(,)h(J)t(.)t(P)l(.)e(F) o Fa(A)t(R)t(R)t(E)t(L)r Fv(:)45 b Fu(Pthr)m(eads)199 1509 y(Pr)m(o)o(gr)o(amming)23 b(-)g(A)e(POSIX)h(Standar)m(d)j(for)d (Better)g(Multipr)m(o-)199 1600 y(cessing)p Fv(,)e(O'Reilly)-5 b(,)18 b(1996;)h(ISBN)f(1-56592-115-1)p Black 71 1715 a([5])p Black 43 w(B)t(.)27 b(L)t Fa(E)t(W)t(I)t(S)t Fv(,)g(D)t(.)f(J)t(.)h(B)t Fa(E)t(R)t(G)r Fv(:)k Fu(Thr)m(eads)25 b(Primer)f(-)f(A)h(Guide)g(T)-7 b(o)199 1807 y(Multithr)m(eaded)19 b(Pr)m(o)o(gr)o(amming)p Fv(,)f(Prentice)f(Hall,)f(1996;)j(ISBN)199 1898 y(0-13-443698-9)p Black 71 2013 a([6])p Black 43 w(S)t(.)g(J)t(.)g(N)t Fa(O)t(RT)s(O)t(N)t Fv(,)f(M)t(.)i(D)t(.)f(D)t Fa(I)t(P)o(A)t(S)t(Q)s(U)r(A)t(L)t(E)r Fv(:)f Fu(Thr)m(ead)f(T)l(ime)e (-)h(The)199 2105 y(Multithr)m(eaded)g(Pr)m(o)o(gr)o(amming)g(Guide)p Fv(,)f(Prentice)f(Hall,)h(1997;)199 2196 y(ISBN)j(0-13-190067-6)p Black 71 2312 a([7])p Black 43 w(D)t(.)24 b(R)t(.)g(B)s Fa(U)t(T)t(E)t(N)t(H)t(O)t(F)r Fv(:)g Fu(Pr)m(o)o(gr)o(amming)e(with)e (POSIX)g(Thr)m(eads)p Fv(,)199 2403 y(Addison)g(W)-6 b(esle)o(y)h(,)19 b(1997;)g(ISBN)f(0-201-63392-2)p Black 71 2518 a([8])p Black 43 w(S)t(.)27 b(P)t Fa(R)t(A)t(S)t(A)t(D)r Fv(:)32 b Fu(Multithr)m(eading)26 b(Pr)m(o)o(gr)o(amming)f(T)-7 b(ec)o(hniques)p Fv(,)199 2610 y(McGra)o(w-Hill,)18 b(1996;)i(ISBN)e (0-079-12250-7)p Black 71 2725 a([9])p Black 43 w(S)t(.)32 b(K)t Fa(L)t(E)t(I)t(N)t(M)t(A)t(N)t Fv(,)g(B)t(.)h(S)t Fa(M)t(A)t(L)t(D)t(E)t(R)t(S)t Fv(,)e(D)t(.)i(S)t Fa(H)t(A)t(H)r Fv(:)43 b Fu(Pr)m(o)o(gr)o(am-)199 2816 y(ming)29 b(with)e(Thr)m(eads)p Fv(,)k(Prentice)d(Hall,)i(1995;)k(ISBN)27 b(0-131-)199 2908 y(72389-8)p Black 33 3023 a([10])p Black 44 w(C)t(.)t(J)t(.)40 b(N)t Fa(O)t(RT)t(H)t(R)r(U)t(P)r Fv(:)58 b Fu(Pr)m(o)o(gr)o(amming)38 b(W)l(ith)g(Unix)f(Thr)m(eads)p Fv(,)199 3114 y(John)20 b(W)m(ile)o(y)e(&)h(Sons,)g(1996;)h(ISBN)d(0-471-13751-0)p Black 33 3230 a([11])p Black 44 w(P)l(.)29 b(B)r Fa(A)t(RT)s(O)t(N)t Fv(-)t(D)q Fa(A)l(V)t(I)t(S)t Fv(,)g(D)t(.)g(M)t Fa(C)t Fv(N)r Fa(A)t(M)t(E)t(E)t Fv(,)g(R)t(.)h(V)-5 b Fa(A)t(S)t(W)m(A)t(N)t (I)t Fv(,)30 b(E)t(.)201 3321 y(L)t Fa(A)t(Z)t(O)r(W)t(S)t(K)t(A)r Fv(:)19 b Fu(Adding)d(Sc)o(heduler)g(Activations)f(to)f(Mac)o(h)i(3.0)p Fv(,)199 3412 y(Uni)n(v)o(ersity)27 b(of)g(W)-6 b(ashington,)29 b(1992;)j(T)-5 b(echnical)27 b(Report)f(92-)199 3504 y(08-03)p Black 33 3619 a([12])p Black 44 w(D)t(.)18 b(S)t Fa(T)t(E)t(I)t(N)t Fv(,)e(D)t(.)h(S)t Fa(H)t(A)t(H)r Fv(:)j Fu(Implementing)c(Lightwight)e(Thr)m(eads)p Fv(,)199 3710 y(SunSoft)19 b(Inc.,)f(1992)i(\(published)g(at)f(USENIX'92\).)p Black 33 3826 a([13])p Black 44 w(W)m(.)t(R)t(.)t(S)t Fa(T)t(E)t(V)t(E)t(N)t(S)r Fv(:)48 b Fu(Advanced)35 b(Pr)m(o)o(gr)o (amming)g(in)f(the)h(Unix)199 3917 y(En)m(vir)m(onment)p Fv(,)58 b(Addison-W)-6 b(esle)o(y)h(,)58 b(1992;)66 b(ISBN)49 b(0-201-)199 4008 y(56317-7)p Black 33 4124 a([14])p Black 44 w(D)t(.)42 b(L)t Fa(E)t(W)t(I)t(N)t(E)r Fv(:)59 b Fu(POSIX)38 b(Pr)m(o)o(gr)o(ammer')m(s)h(Guide:)63 b(Writing)199 4215 y(P)-6 b(ortable)34 b(Unix)g(Pr)m(o)o(gr)o(ams)p Fv(,)k(O'Reilly)33 b(&)h(Associates,Inc.,)199 4306 y(1994;)20 b(ISBN)e(0-937175-73-0)p Black 33 4422 a([15])p Black 44 w(B)t Fa(RY)m(A)t(N)137 b Fv(O)t(')t(S)t Fa(U)t(L)t(L)t(I)t(V)l(A)t (N)r Fv(:)250 b Fu(F)l(r)m(equently)136 b(ask)o(ed)199 4513 y(questions)187 b(for)e(comp.os.r)m(esear)m(c)o(h)p Fv(,)229 b(1995;)199 4604 y(http://www)-5 b(.serpentine.com/)p Fk(\230)p Fv(bos/os-f)o(aq/,)199 4696 y d2061 28 a2088 28 33 4811 a([16])p Black 44 w(S)t Fa(U)t(N)24 b Fv(M)t Fa(I)t(C)t(R)r(O)t(S)t(Y)t(S)t(T)t(E)t(M)t(S)t Fv(,)e(I)t Fa(N)t(C)r Fv(:)27 b Fu(Thr)m(eads)22 b(F)l(r)m(equently)g(Ask)o(ed)199 4902 y(Questions)p Fv(,)111 b(1995,)f(http://www.sun.com/w)o(orkshop/-) 199 4994 y(threads/f)o(aq.html)p Black 2058 83 a([17])p Black 44 w(B)t Fa(RY)m(A)t(N)78 b Fv(O)t(')t(S)t Fa(U)t(L)t(L)t(I)t(V)l (A)t(N)r Fv(:)132 b Fu(F)l(r)m(equently)76 b(ask)o(ed)g(ques-)2224 174 y(tions)134 b(for)g(comp.pr)m(o)o(gr)o(amming)o(.thr)m(eads)p Fv(,)165 b(1997;)2224 266 y(http://www)-5 b(.serpentine.com/)p Fk(\230)p Fv(bos/threads-f)o(aq/.)p Black 2058 410 a([18])p Black 44 w(B)t Fa(I)t(L)83 b Fv(L)t Fa(E)t(W)t(I)t(S)r Fv(:)144 b Fu(F)l(r)m(equently)82 b(ask)o(ed)f(questions)h(for)2224 502 y(comp.pr)m(o)o(gr)o(amming)o(.thr)m(eads)p Fv(,)213 b(1999;)250 b(http://-)2224 593 y(www)-5 b(.lambdacs.com/ne)n (wsgroup/F)f(A)l(Q.html)p Black 2058 738 a([19])p Black 44 w(N)t Fa(U)t(M)t(E)t(R)t(I)t(C)57 b Fv(Q)s Fa(U)t(E)t(S)t(T)h Fv(I)t Fa(N)t(C)r Fv(:)96 b Fu(Multithr)m(eading)58 b(-)e(De\002ni-) 2224 829 y(tions)k(and)h(Guidelines)p Fv(;)81 b(1998;)g(http://www)-5 b(.numeric-)2224 920 y(quest.com/lang/multi-frame.html)p Black 2058 1065 a([20])p Black 44 w(T)t Fa(H)t(E)42 b Fv(O)t Fa(P)t(E)t(N)f Fv(G)t Fa(R)r(O)t(U)t(P)r Fv(:)62 b Fu(The)39 b(Single)h(Unix)f(Speci\002cation,)2224 1156 y(V)-8 b(er)o(sion)46 b(2)g(-)f(Thr)m(eads)p Fv(;)60 b(1997;)f(http://www.opengroup-)2224 1248 y(.or)o (g/onlinepubs/007908799/xsh/threads.html)p Black 2058 1393 a([21])p Black 44 w(S)t Fa(U)t(N)41 b Fv(M)t Fa(I)t(C)t(R)r(O)t(S) t(Y)t(S)t(T)t(E)t(M)t(S)d Fv(I)t Fa(N)t(C)r Fv(:)61 b Fu(SMI)39 b(Thr)m(ead)g(Resour)m(ces)p Fv(;)2224 1484 d2090 48 a2137 53 2058 1629 a([22])p Black 44 w(F)t Fa(R)t(A)t(N)t(K)29 b Fv(M)t Fa(U)t(E)t(L)t(L)t(E)t(R)r Fv(:)35 b Fu(FSU)26 b(pthr)m(eads)p Fv(;)32 b(1997;)g(http://www-)2224 1720 y(.cs.fsu.edu/)p Fk(\230)p Fv(mueller/pthreads/)p Black 2058 1865 a([23])p Black 44 w(C)t Fa(H)t(R)t(I)t(S)85 b Fv(P)t Fa(R)r(O)q(V)t(E)t(N)t(Z)t(A)t(N)t(O)r Fv(:)145 b Fu(MIT)82 b(pthr)m(eads)p Fv(;)115 b(1993;)2224 1956 y(http://www)-5 b(.mit.edu/people/prov)o(en/pthreads.html)2224 2047 y(\(old\),)209 b(http://www.humanf)o(actor.com/pthreads/mit-)2224 2139 y(pthreads.html)20 b(\(updated\))p Black 2058 2283 a([24])p Black 44 w(K)r Fa(O)r(T)n(A)29 b Fv(A)t Fa(B)t(E)r Fv(:)35 b Fu(P)-6 b(ortable)27 b(Thr)m(eading)g(Libr)o(ary)g Fv(\(PTL\);)d(1999;)2224 2375 y(http://www.media.osaka-cu.ac.jp/)p Fk(\230)p Fv(k-abe/PTL/)p Black 2058 2519 a([25])p Black 44 w(R)t Fa(A)t(L)t(F)19 b Fv(S)t(.)h(E)t Fa(N)t(G)t(E)t(L)t(S)t(C)t(H) t(A)t(L)t(L)r Fv(:)d Fu(GNU)g(P)-6 b(ortable)17 b(Thr)m(eads)h Fv(\(Pth\);)2224 2611 y(1999;)55 b(http://www.gnu.or)o(g/softw)o (are/pth/,)47 b(ftp://ftp.gnu-)2224 2702 y(.or)o(g/gnu/pth/)p Black 2058 2847 a([26])p Black 44 w(M)t Fa(I)t(C)t(H)t(A)t(E)t(L)36 b Fv(T)n(.)i(P)t Fa(E)t(T)t(E)t(R)t(S)t(O)t(N)r Fv(:)50 b Fu(POSIX)34 b(and)h(DCE)f(Thr)m(eads)2224 2938 y(F)-8 b(or)57 b(Linux)h Fv(\(PCThreads\);)75 b(1995;)j(http://members.aa-) 2224 3029 y(.net/)p Fk(\230)p Fv(mtp/PCthreads.html)p Black 2058 3174 a([27])p Black 44 w(R)q Fa(O)t(B)t(E)t(RT)27 b Fv(S)t(.)h(T)t Fa(H)t(A)q(U)r Fv(:)35 b Fu(Really)25 b(Simple)g(Thr)m(eads)i Fv(\(rsthreads\);)2224 3265 y(1996;)20 b(ftp://ftp.ai.mit.edu/pub/rst/)p Black 2058 3410 a([28])p Black 44 w(J)t Fa(O)t(H)t(N)94 b Fv(B)t Fa(I)t(R)t(R)t(E)t(L)t(L)r Fv(:)163 b Fu(F)l(r)m(eeBSD)91 b(uthr)m(eads)p Fv(;)129 b(1998;)2224 3502 y(ftp://ftp.freebsd.or)o(g/pub/FreeBSD/FreeBSD-)2224 3593 y(current/src/lib/libc)p 2780 3593 23 4 v 27 w(r/uthread/)p Black 2058 3738 a([29])p Black 44 w(X)t Fa(A)l(V)t(I)t(E)t(R)46 b Fv(L)t Fa(E)t(R)r(O)q(Y)r Fv(:)70 b Fu(The)44 b(LinuxThr)m(eads)h (libr)o(ary)p Fv(;)57 b(1999;)2224 3829 y(http://pauillac.inria.fr/)p Fk(\230)p Fv(xlero)o(y/linuxthreads/)p Black 2058 3974 a([30])p Black 44 w(I)t(B)t(M)r(:)17 b Fu(AIX)h(V)-8 b(er)o(sion)18 b(4.3)h(Gener)o(al)g(Pr)m(o)o(gr)o(amming)g(Concepts:) 2224 4065 y(Writing)56 b(and)g(Deb)o(ug)o(ging)h(Pr)m(o)o(gr)o(ams;)74 b(Under)o(standing)2224 4156 y(Thr)m(eads)p Fv(;)41 b(1998;)h (http://www.rs6000.ibm.com/doc)p 3714 4156 V 28 w(link/-)2224 4248 y(en)p 2298 4248 V 28 w(US/a)p 2476 4248 V 26 w(doc)p 2609 4248 V 28 w(lib/aixpr)o(ggd/genprogc/understanding-)p 2224 4339 V 2247 4339 a(threads.htm)p Black 2058 4484 a([31])p Black 42 w Fu(Netscape)110 b(P)-6 b(ortable)110 b(Runtime)f Fv(\(NSPR\);)f(http://-)2224 4575 y(www)-5 b(.mozilla.or)o(g/docs/refList/refNSPR/,)246 b(http://-)2224 4666 y(lxr)l(.mozilla.or)o(g/seamonk)o(e)o(y/source/nsprpub/)p Black 2058 4811 a([32])p Black 44 w(R)t Fa(A)t(L)t(F)45 b Fv(S)t(.)f(E)t Fa(N)t(G)t(E)t(L)t(S)t(C)t(H)t(A)t(L)t(L)r Fv(:)65 b Fu(Simple)42 b(Mac)o(hine)h(Conte)o(xt)2224 4902 y(Benc)o(hmark)q Fv(;)73 b(2000;)f(http://www)-5 b(.gnu.or)o(g/softw)o(are/pth-)2224 4994 y(/smcb)m(.tar)l(.gz)p Black 1908 5700 a Fx(11)p Black eop @ 1.4 log @*** empty log message *** @ text @d4 1 a4 1 %%Pages: 10 d13 1 a13 1 %DVIPSSource: TeX output 1999.08.19:1607 d377 2 a378 2 48 48 59 3[22 48 48 37 41 48 44 44 48 6[18 33 2[33 33 33 33 1[33 33 18 17 22 42[37 2[{TeXBase1Encoding ReEncodeFont}53 d421 1 a421 1 3[37 3[37 37 1[18 1[18 44[{TeXBase1Encoding ReEncodeFont}32 d428 2 a429 2 rf /FB 138[50 28 39 33 2[50 50 78 28 3[50 50 33 44 1[44 1[44 13[55 66 12[61 3[72 92 6[50 7[50 2[25 46[{ d444 2 a445 2 1 0 bop Black Black Black Black 800 432 a FD(P)-8 b(or)t(tab)n(le)57 b(Multithreading)721 614 y FC(The)29 b(Signal)g(Stac)n(k)f(T)-12 d447 4 a450 4 855 y FB(Ralf)c(S.)g(Engelschall)1524 971 y(rse@@engelschall.com)1273 1088 y FA(W)-6 b(eblinger)19 b(W)-6 b(e)o(g)18 b(28,)i(85221)g(Dachau,) g(German)o(y)-5 b(.)1694 1282 y FB(August)24 b(1999)1157 1398 y Fz(Cop)n(yr)q(ight)1478 1396 y(c)1459 1398 y Fy(\015)18 d452 2 a453 2 1491 y(Document)g(V)-5 b(ersion)18 b(0.9.15)p Black Black 1798 1789 a Fx(Abstract)p Black Black 208 1926 a FA(This)e(paper)h d456 1 a456 1 2018 y(threads)25 b(of)f(e)o(x)o(ecution)i(on)f(Unix)g(systems)g(which) d458 1 a458 1 (acility)-5 b(.)40 b(The)24 b(described)208 2109 y(approach)d(closely)e d461 2 a462 2 b(ortable)208 2200 y(Thr)m(eads)27 b FA(\(Pth\))626 2169 y Fv(1)680 2200 y FA(and)g(usually)f(can)h(be)f(easily)g(adapted)i(by)e d464 1 a464 1 (approach.)208 2292 y(It)g(is)h(entirely)h(based)g(on)g(Unix)g(system)f d466 1 a466 1 (especially)g(does)g(not)g(require)g(an)o(y)208 2383 d469 2 a470 2 (creating)g(threads,)h(which)208 2474 y(is)f(e)o(xplained)i(in)f (detail.)208 2616 y Fx(K)o(e)o(yw)o(or)o(ds:)39 b FA(portability)-5 d473 1 a473 1 2707 y(signal)19 b(handler)m(,)g(stack,)g(mak)o(econte)o(xt,)h d475 3 a477 3 3020 y Fu(1)143 b(Intr)m(oduction)0 3208 y Ft(1.1)121 b(Multithreading)0 3364 y Fs(The)29 b(paradigm)f(of)h(programming)e (with)i(multiple)g(threads)g(of)0 3464 y(e)o(x)o(ecution)c(\(aka)h d479 2 a480 2 3563 y(and)h(dates)g(back)f(to)h(the)g(decades)g(of)g(programming)c (with)k Fr(co-)0 3663 y(r)l(outines)c Fs([1)o(,)h(2)o(].)37 d482 5 a486 5 3763 y(platforms)15 b(became)h(popular)f(the)h(\002rst)h(time)g(in)g (the)f(early)g(1990s.)0 3964 y Fq(Multithreading)25 b(Adv)n(antages)0 4090 y Fs(Multithreading)14 b(can)j(pro)o(vide)d(man)o(y)i(bene\002ts)g (for)g(applications)0 4189 y(\(good)30 b(runtime)h(concurrenc)o(y)-5 b(,)31 b(parallel)g(programming)e(tech-)0 4289 y(niques)19 d488 2 a489 2 4389 y(programming)12 b(style)17 b(can)e(be)h(combined)e(with)i (multiple)f(threads)0 4488 y(of)24 b(e)o(x)o(ecution,)f(etc.\))38 d491 3 a493 3 4588 y(ally)33 b(performance)c(gains)j(and)g(reduced)f(resource)g (consump-)0 4688 y(tion.)67 b(Because)35 b(in)g(contrast)f(to)g (multiprocess)f(applications,)0 4787 y(multithreaded)22 d495 5 a499 5 4887 y(\(mainly)i(memory\))g(and)h(their)g(internal)g(communication)d (part)1992 3204 y(can)20 b(le)n(v)o(erage)e(from)h(the)h(shared)g (address)g(space.)1992 3506 y Fq(Multithreading)25 b(and)f(Application) 1992 3671 y Fs(Ne)n(v)o(ertheless)j(there)h(still)i(e)o(xist)e(just)h (a)g(fe)n(w)f(real)h(applications)1992 3771 y(in)g(the)g(free)f(softw)o d501 2 a502 2 3870 y(their)18 b(bene\002t,)g(although)f(their)i(application)e(areas)i (are)g(predesti-)1992 3970 y(nated)c(for)h(multithreading.)22 d504 1 a504 1 4069 y(webserv)o(er)2354 4039 y Fp(2)2410 4069 y Fs(as)24 d506 3 a508 3 4169 y(cess)28 b(model)e(for)h(serving)f(HTTP)h(requests,)i(although)c (e)o(xperi-)1992 4269 y(ments)15 b(with)g(multithreaded)f(Apache)g(v)n (ariants)h(already)f(in)i(1996)1992 4368 y(sho)n(wed)29 d510 1 a510 1 4468 y(other)19 b(applications.)2116 4588 y(The)f(reason)f(mainly)g d512 2 a513 2 4688 y(ing)i(f)o(acilities)h(under)d(Unix)i(were)h(rare.)28 b(The)21 b(situation)f(became)1992 4787 y(better)g(after)h(some)f(v)o d515 10 a524 5 4887 y(threading)37 b(f)o(acilities)j(into)f(their)h(Unix)f(\003a)n(v)n (ors)g(and)g(POSIX)p Black 0 4940 764 4 v 90 4995 a Fv(1)120 5019 y Fo(see)17 b(http://www)l(.gnu.or)o(g/softw)o(are/pth/)90 5077 y Fv(2)120 5100 y Fo(see)g(http://www)l(.apache.or)o(g/)p Black Black 1929 5400 a Fs(1)p Black eop d526 2 a527 5 2 1 bop Black Black 0 83 a Fs(standardized)35 b(a)h(threading)f(API)h (\(kno)n(wn)f(as)i(Pthreads\).)1855 53 y Fp(3)1888 83 y Fs(.)0 183 y(But)h(an)f(API)g(and)g(a)g(fe)n(w)g(v)o(endor)e (implementation)g(are)i(not)0 282 y(enough)20 b(to)j(ful\002ll)f(the)h (portability)e(requirements)f(of)i(free)g(soft-)0 382 d529 2 a530 2 (are)0 482 y(needed)g(\002rst.)125 586 y(The)52 b(author)g(currently)g (kno)n(ws)g(about)g(o)o(v)o(er)g(fourteen)0 685 y(\(mostly)25 d532 5 a536 5 785 y(tions)k(for)f(Unix)g(systems)796 755 y Fp(4)829 785 y Fs(,)j(b)n(ut)d(only)g(a)h(fe)n(w)f(of)h(them)f(are)g(re-)0 885 y(ally)i(portable.)48 b(And)28 b(e)n(v)o(en)f(the)h(mostly)g (portable)f(ones)h(suf)n(fer)0 984 y(from)h(the)h(f)o(act)g(that)g(the) o(y)f(partly)g(depend)f(on)h(assembly)h(code)0 1084 y(or)c(platform)f d538 3 a540 3 1183 y(and)18 b(dispatching)e(of)i(the)g(indi)n(vidual)e(threads.)24 b(This)18 b(means)g(that)0 1283 y(the)24 b(number)f(of)h(platforms)e (the)o(y)i(are)g(w)o(orking)f(on)h(are)g(usually)0 1383 d542 2 a543 2 g(them)g(are)0 1482 y(only)c(portable)f(to)h(those)h(platforms.)j(This) d(situation)f(is)h(not)g(sat-)0 1582 y(isf)o(actory)-5 d545 3 a547 3 (multi-)0 1682 y(threading)j(when)g(the)o(y)h(w)o(ant)h(to)g(\(or)e(ha) n(v)o(e)h(to\))h(achie)n(v)o(e)e(maxi-)0 1781 y(mum)19 b(portability)g(for)h(their)g(application.)125 1885 y(So)f(a)g d549 3 a551 3 1985 y(for)i(implementing)e(user)n(-space)i(threads)g(w)o(ould)f(be)i (useful,)e(be-)0 2084 y(cause)29 b(this)h(w)o(ay)g(the)f(possibilities) h(to)f(use)h(multithreading)d(in)0 2184 y(free)20 b(softw)o(are)g d553 1 a553 1 2406 y Fq(Ing)o(redients)24 b(Of)f(A)g(Thread)0 2539 d555 2 a556 2 (the)g(most)g(im-)0 2639 y(portant)17 b(ones)h(are)f(its)i(memory)e (mapping)f(table,)i(the)g(signal)g(dis-)0 2738 y(patch)h(table,)g(the)g d558 3 a560 3 2838 y(the)j(machine)f(conte)o(xt,)f(which)i(in)g(turn)f(consists)h(of) g(at)g(least)h(the)0 2938 y(CPU)17 b(re)o(gisters)e(including)f(the)h (program)f(counter)g(and)h(the)h(stack)0 3037 y(pointer)-5 d562 2 a563 2 e(pro-)0 3137 y(cesses)24 b(\(L)-6 b(WP\))23 b(or)g(threads)g(which)f (share)h(all)h(ingredients)e(with)0 3237 y(the)f(underlying)d(\(hea)n d565 3 a567 3 3336 y(chine)g(conte)o(xt.)0 3558 y Fq(K)m(er)r(nel-Space)k(vs)o(.)j (User-Space)0 3691 y Fs(Those)k(L)-6 b(WPs)32 b(or)f(threads,)i(on)e(a) g(Unix)g(platform)f(classically)0 3791 y(can)k(be)g(implemented)e d569 3 a571 3 3891 y(space.)53 b(When)30 b(implemented)e(in)h(k)o(ernel-space,)i(one) e(usually)0 3990 y(calls)22 b(them)e(L)-6 b(WPs,)22 b(else)f(\(user)n (-space\))f(threads.)26 b(If)21 b(threads)f(are)0 4090 d573 2 a574 2 (switches)0 4189 y(are)17 b(performed)e(by)i(the)h(k)o(ernel)e(without) h(notice)g(by)g(the)g(applica-)0 4289 y(tion,)h(similar)g(to)h(the)f d576 3 a578 3 4389 y(implemented)31 b(in)i(user)n(-space,)i(the)d(thread)g(conte)o (xt)g(switches)0 4488 y(are)e(performed)e(usually)i(by)g(an)g (application)f(library)g(without)0 4588 y(notice)38 b(by)g(the)h(k)o d580 19 a598 19 4688 y(threading)14 b(approaches,)h(where)g(typically)g(a)i(user)n (-space)e(library)0 4787 y(binds)38 b(one)h(or)g(more)f(user)n(-space)g (threads)h(to)g(one)f(or)h(more)0 4887 y(k)o(ernel-space)18 b(L)-6 b(WPs.)1992 83 y Fq(Thread)23 b(Models)1992 209 y Fs(F)o(or)40 b(instance)h(the)h(v)o(endor)d(threading)g(f)o (acilities)j(under)e Fr(So-)1992 308 y(laris)p Fs(,)24 b Fr(AIX)p Fs(,)f Fr(T)-5 b(ru64)23 b Fs(and)g Fr(IRIX)j Fs(use)e(a)g Fn(M:N)g Fs(mapping)d([15)o(,)j(24)o(],)1992 408 y(i.e.,)42 b Fn(M)d Fs(user)n(-space)e(threads)g(are)h(mapped)f (onto)g Fn(N)i Fs(k)o(ernel-)1992 508 y(space)29 b(L)-6 b(WPs.)52 b(On)29 b(the)g(other)g(hand)f Fr(LinuxThr)m(eads)f Fs([23)o(])i(un-)1992 607 y(der)d Fr(GNU/Linux)h Fs(uses)g(a)g Fn(1:1)f Fs(mapping)f(and)h(pure)f(user)n(-space)1992 707 y(implementations)35 b(lik)o(e)i Fr(GNU)h(Pth)p Fs(,)j Fr(FSU)c(pthr)m(eads)f Fs(or)h Fr(MIT)1992 806 y(pthr)m(eads)p Fs(,)19 b(etc.)25 b(use)20 b(a)h Fn(M:1)f Fs(mapping)e([19)o(,)j(16)o (,)f(17)o(].)2116 907 y(From)38 b(no)n(w)g(on)g(we)h(talk)g(about)e (such)i Fn(M:1)f Fs(user)h(space)1992 1006 y(threading)i(approaches,)48 b(where)43 b(one)g(or)g(more)g(user)h(space)1992 1106 d600 6 a605 6 (space)h(pro-)1992 1206 y(cess.)k(The)19 b(e)o(x)o(ercise)f(is)i(to)g (implement)e(this)i(by)e(using)h(standard-)1992 1305 y(ized)h(Unix)f(system)i(and)e(ANSI)i(C)g(language)d(f)o(acilities)j Fr(only)p Fs(.)1992 1579 y Ft(1.2)120 b(The)34 b(Ex)l(ercise)1992 1736 y Fs(As)20 b(we)h(ha)n(v)o(e)f(mentioned,)e(a)i(thread)f(shares)i (all)f(things)g(with)h(the)1992 1836 y(underlying)14 d607 2 a608 2 b(So)18 b(the)1992 1935 y(major)25 b(task)j(for)e(a)h(user)n(-space)f (threading)f(library)g(is)j(to)f(create)1992 2035 y(and)19 d610 3 a612 3 2116 2135 y(In)31 b(practice,)h(the)f(second)f(major)g(task)h(it)g(has) g(to)g(do)g(is)g(to)1992 2235 y(mak)o(e)g(sure)h(that)h(no)f(thread)f (by)h(accident)f(blocks)h(the)g(whole)1992 2334 y(process)24 d614 2 a615 2 b(when)g(an)1992 2434 y(operation)i(w)o(ould)i(block,)h(the)g(task)g (of)f(the)g(threading)f(library)1992 2534 y(is)22 b(to)f(suspend)g d617 2 a618 2 2633 y(in)j(the)g(meantime)f(dispatch)g(the)h(remaining)f(threads.)32 b(But)24 b(this)1992 2733 y(task)g(is)g(outside)g(the)f(scope)h(of)f d620 4 a623 4 2832 y(the)d(aspect)g(of)g(machine)f(conte)o(xt)g(handling.)1992 3106 y Ft(1.3)120 b(The)34 b(Curse)h(of)e(P)-6 b(or)5 b(tability)1992 3263 y Fs(Our)22 b(goal)h(of)f(real)h(portability)f (for)g(a)h(threading)e(library)h(causes)1992 3363 y(some)h(non-tri)n d625 2 a626 2 b(The)1992 3462 y(most)29 b(ob)o(vious)e(one)h(is)i(that)f(dealing)f (with)i(machine)e(conte)o(xts)1992 3562 y(usually)34 d628 1 a628 1 b(because)e(it)h(is)g(a)1992 3662 y(highly)16 b(CPU)j(dependent)d d630 5 a634 5 3761 y(a)d(matter)g(of)g(switching)g(a)h(fe)n(w)f(CPU)h(re)o(gisters)f (\(mainly)f(the)i(pro-)1992 3861 y(gram)i(counter)g(and)g(the)h(stack)h (pointer\).)1992 4063 y Fq(Assemb)n(ly)i(Code)g(Considered)h(Har)r (mful)1992 4189 y Fs(Additionally)j(we)i(disallo)n(w)g(the)h(use)f(of)g (an)o(y)f(assembly)h(solu-)1992 4288 y(tions)23 b(or)g(platform)e d636 2 a637 2 4388 y(ing)f(library)f(again)g(w)o(ould)h(be)g(only)g(semi-portable,)f (i.e.,)i(it)g(can)1992 4488 y(be)d(ported)f(to)i Fn(N)g d639 4 a642 4 1992 4587 y(has)c(to)h(manually)f(adjust)g(or)h(e)n(v)o(en)f(e)o(xtend) f(the)i(threading)e(library)1992 4687 y(to)20 b(w)o(ork)f(there,)h (too.)2116 4787 y(That)g(is)g(not)g(acceptable)f(for)g(us,)h(e)n(v)o (en)f(when)g(it)i(also)f(mak)o(es)1992 4887 y(solving)41 d644 12 a655 8 b(least)g(all)p Black 0 4940 764 4 v 90 4995 a Fv(3)120 5019 y Fo(IEEE)15 b(POSIX)h(1003.1c-1995)j(aka)f(ISO/IEC)f(9945-1:1996) 90 5077 y Fv(4)120 5100 y Fo(see)g(the)h(GNU)e(Pth)h(webpage)i(for)e (references)j(to)d(them)p Black Black 0 5400 a Fm(P)n(or)q(table)11 b(Multithreading)0 5458 y Fl(The)h(Signal)g(Stac)o(k)e(T)-5 b(r)q(ic)o(k)12 b(F)o(or)f(User-Space)g(Thread)h(Creation)1929 5400 y Fs(2)1221 b Fl(Cop)o(yr)q(ight)3398 5399 y(c)3381 5400 y Fk(\015)11 b Fl(1999)g(Ralf)h(S)o(.)f(Engelschall)3524 d658 4 a661 8 3 2 bop Black Black 0 83 a Fs(other)26 b(kno)n(wn)g(free)g(softw)o(are) h(user)n(-space)f(threading)f(libraries)0 183 y([16)o(,)g(17)o(,)g(18)o (])g(do)f(not)g(restrict)h(themself)g(to)f(this)i(and)e(therefore)0 282 y(are)29 b(just)h(semi-portable.)49 b(But)30 b(real)f(portability)e (is)k(our)d(major)0 382 y(goal.)0 784 y Fu(2)143 b(Pr)m(ob)o(lem)38 b(Anal)n(ysis)0 1049 y Ft(2.1)121 b(The)34 b(T)-14 b(ask)33 b(in)g(Detail)0 1236 y Fs(Our)26 b(task)g(sounds)g(simple:)37 b(Create)27 b(and)e(initialize)i(a)f(machine)0 1335 y(conte)o(xt)d(\()p d663 2 a664 2 (the)0 1435 y(\002rst)h(time,)h(lets)g(the)e(thread)g(start)h(e)o(x)o (ecuting)d(at)j(a)g(startup)f(rou-)0 1534 y(tine)h(\()p d666 1 a666 1 Fq(arg)p Fs(\))h(on)g(a)h(particular)0 1634 y(run-time)17 d668 2 a669 2 h(again)f(yields)0 1734 y(e)o(x)o(ecution)30 b(its)j(indi)n(vidual)e (thread)g(ingredients)g(are)h(stored)g(in)0 1833 y Fq(mctx)23 d671 3 a673 3 (restored)0 1933 y(from)d(there.)0 2213 y Fq(The)k(Proposed)h(API)0 2369 y Fs(In)f(detail)f(we)i(propose)d(the)i(follo)n(wing)e Fr(Application)g(Pr)l(o)o(gr)o(am-)0 2469 y(mer)o(s)g(Interface)f d675 2 a676 2 Black 0.7 0.7 0.7 TeXcolorrgb 95 2716 50 50 v Black Black 208 2716 a(A)30 b(data)g(structure)f Fn(mctx)p 969 2716 d678 4 a681 4 2815 y(conte)o(xt.)p Black 0.7 0.7 0.7 TeXcolorrgb 95 3045 50 50 v Black Black 208 3045 a(A)78 b(function)f(\223)p Fn(mctx)p 920 3045 25 4 v 29 w(t)h Fj(*mctx)p 1305 3045 V 29 w(create\()g Fn(v)o(oid)208 3145 y Fj(\(*)p Fr(func)p d684 1 a684 1 Fn(size)p 1856 3145 V 30 w(t)208 3245 y Fr(sksize)p Fj(\))p d686 2 a687 2 3344 y(out)g(of)g(a)h(start)h(routine)d Fr(func)p Fs(,)h(a)i(start)f (ar)o(gument)d Fr(ar)m(g)j Fs(and)208 3444 y(a)27 b(stack)f(starting)h d689 7 a695 7 3544 y(size.)e(It)c(returns)e(the)h(created)g Fn(mctx)p 1270 3544 V 29 w(t)g Fs(data)g(structure.)p Black 0.7 0.7 0.7 TeXcolorrgb 95 3774 50 50 v Black Black 208 3774 a(A)56 b(function)e(\223)p Fn(int)j Fj(mctx)p 1053 3774 25 4 v 29 w(save\()p Fn(mctx)p 1508 3774 V 28 w(t)f Fj(*)p Fr(mctx)p Fj(\))p Fs(\224)208 3873 y(which)31 b(sa)n(v)o(es)i(the)f (current)f(machine)g(conte)o(xt)g(into)h(the)208 3973 d697 1 a697 1 b(It)27 b(returns)e Fj(FALSE)208 4073 y Fs(when)c(the)i(machine)e d699 9 a707 9 4172 y(the)e(\002rst)h(time,)g(and)f Fj(TRUE)g Fs(when)g(the)h(machine) e(conte)o(xt)208 4272 y(w)o(as)h(restored)e(by)h Fj(mctx)p 954 4272 V 29 w(restore)g Fs(and)g(it)h(returns)e(the)208 4372 y(second)g(time)h(\(see)h(belo)n(w\).)p Black 0.7 0.7 0.7 TeXcolorrgb 95 4602 50 50 v Black Black 208 4602 a(A)78 b(function)f(\223)p Fn(v)o(oid)h Fj(mctx)p 1174 4602 25 4 v 29 w(restore\()g Fn(mctx)p 1857 4602 V 29 w(t)208 4701 y Fj(*)p Fr(mctx)p Fj(\))p Fs(\224)26 b(which)f(restores)h(the)g (machine)e(conte)o(xt)h(from)208 4801 y(the)h(machine)g(conte)o(xt)g d709 4 a712 4 4901 y(does)22 b(not)f(return)g(at)i(all.)32 b(Instead)21 b(it)i(returns)f(at)g(the)h(loca-)208 5000 y(tion)i(of)g(the)h Fj(mctx)p 791 5000 V 29 w(save)f Fs(function)f(b)n(ut)i(with)f(a)h (return)208 5100 y(of)19 b Fj(TRUE)p Fs(.)2237 0 y d714 2 a715 2 2237 0 a d897 1 a897 1 1992 1496 a Fq(Conte)n(xt)j(Switching)1992 1629 y Fs(There)38 d899 1 a899 1 1729 y(tics)18 b(of)g Fj(mctx)p 2418 1729 25 4 v 29 w(save)p d901 14 a914 14 1829 y(we)31 b(will)i(choose)d(later)m(,)k(will)f(support)d(e)o(xactly) h(these)g(seman-)1992 1928 y(tics.)25 b(Second,)19 b(we)h(w)o(ant)g(to) g(use)g(an)g(easy)g(dispatching)e(function)1992 2028 y(which)h(looks)h(lik)o(e)g(this:)p Black Black 1992 2224 a Fi(v)o(oid)1992 2315 y Fh(mctx_switch\()p Fi(mctx)p 2693 2315 23 4 v 25 w(t)45 b Fh(*)p Fw(fr)m(om)p Fh(,)2529 2407 y Fi(mctx)p 2690 2407 V 28 w(t)g Fh(*)p Fw(to)p Fh(\))1992 2498 y Fg(f)2081 2589 y Fi(if)g Fh(\(mctx_save\()p Fw(fr)m(om)p Fh(\))d(==)j(FALSE\))2216 2681 y(mctx_restore\()p Fw(to)p Fh(\);)2081 2772 y Fi(r)o(etur)o(n)p Fh(;)1992 2863 y Fg(g)1992 3158 y Ft(2.2)120 b(P)-6 b(ossibilities)1992 3322 y Fs(When)28 b(we)i(pok)o(e)e(around)f(in)i(the)g(references)f(of) g(the)h(ANSI)h(C)1992 3421 y(language)g(and)h(the)h(Unix)f(standards)g (we)i(\002nd)e(the)h(follo)n(wing)1992 3521 y(functions)18 d916 3 a918 3 Black 0.7 0.7 0.7 TeXcolorrgb 2087 3721 50 50 v Black Black 2199 3721 a(There)25 b(are)g(the)g(functions)f Fj(getcontext)p Fs(\(3\),)g Fj(make-)2199 3821 y(context)p d920 7 a926 7 3920 y(text)p Fs(\(3\))25 b(which)h(conform)e(to)i(the)g(Single)g(Unix) g(Spec-)2199 4020 y(i\002cation,)j(V)-9 b(ersion)27 b(2)g(\(SUSv2)g ([14)o(],)i(aka)e(Unix95/98\).)2199 4120 y(Unfortunately)d(these)j(are) g(a)n(v)n(ailable)f(on)h(modern)e(Unix)2199 4219 y(platforms)19 b(only)-5 b(.)p Black 0.7 0.7 0.7 TeXcolorrgb 2087 4403 V Black Black 2199 4403 a(There)37 b(are)h(the)g(functions)e Fj(setjmp)p Fs(\(3\))h(and)g Fj(long-)2199 4502 y(jmp)p d928 4 a931 4 4602 y(and)29 b Fj(sigsetjmp)p Fs(\(3\))d(and)j Fj(siglongjmp)p Fs(\(3\))d(which)2199 4701 y(conform)39 b(to)i(IEEE)f(Std1003.1-1988)c (\(POSIX\),)k(and)2199 4801 y(Single)24 b(Unix)g(Speci\002cation,)h(V) -9 b(ersion)23 b(2)i(\(SUSv2)f([14)o(],)2199 4901 y(aka)i(Unix95/98\).) d933 17 a949 8 5000 y(able)i(really)g(on)g(all)g(Unix)g(platforms,)h(the)f(last)h(tw)o (o)g(are)2199 5100 y(a)n(v)n(ailable)20 b(only)f(on)h(some)g(of)g (them.)p Black 0 5400 a Fm(P)n(or)q(table)11 b(Multithreading)0 5458 y Fl(The)h(Signal)g(Stac)o(k)e(T)-5 b(r)q(ic)o(k)12 b(F)o(or)f(User-Space)g(Thread)h(Creation)1929 5400 y Fs(3)1221 b Fl(Cop)o(yr)q(ight)3398 5399 y(c)3381 5400 y Fk(\015)11 b Fl(1999)g(Ralf)h(S)o(.)f(Engelschall)3524 5458 y(All)g(Rights)h(Reser)q(v)o(ed.)p Black eop d951 6 a956 15 4 3 bop Black Black Black 0.7 0.7 0.7 TeXcolorrgb 95 83 50 50 v Black Black 208 83 a Fs(There)43 b(is)j(the)f(function)e Fj(sigaltstack)p Fs(\(2\))f(which)208 183 y(conforms)c(to)i(the)f (Single)h(Unix)g(Speci\002cation,)j(V)-9 b(er)n(-)208 282 y(sion)48 b(2)g(\(SUSv2)f([14)o(]\))h(and)f(its)j(ancestor)d (function)208 382 y Fj(sigstack)p Fs(\(2\))31 b(from)i(4.2BSD.)g(The)h (last)g(one)g(e)o(xists)208 482 y(only)25 b(on)g(BSD-deri)n(v)o(ed)f (platforms,)i(b)n(ut)g(the)g(\002rst)g(func-)208 581 y(tion)19 b(already)h(e)o(xists)g(on)g(all)h(current)e(Unix)g (platforms.)0 852 y Ft(2.3)121 b(Maxim)o(um)32 b(P)-6 b(or)5 b(tability)33 b(Solution)0 1007 y Fs(The)49 b(maximum)e (portable)g(solution)h(ob)o(viously)f(is)j(to)f(use)0 1107 y(the)54 b(standardized)e Fj(makecontext)p Fs(\(3\))f(function)h (to)i(cre-)0 1207 y(ate)k(threads)e(and)h Fj(switchcontext)p Fs(\(2\))e(or)i Fj(getcon-)0 1306 y(text)p Fs(\(2\)/)p d958 2 a959 2 b(And)27 b(actu-)0 1406 y(ally)g(these)h(are)f(the)g(preferred)e (functions)h(modern)g(user)n(-space)0 1506 y(multithreading)14 d961 7 a967 7 1605 y(still)i(a)f(lot)g(of)f(platforms)g(where)g(this)h(approach)d (cannot)i(be)h(used.)0 1705 y(So)24 b(our)f(f)o(allback)f(approach)g (has)h(to)h(use)g(the)f(remaining)f(possi-)0 1804 y(bilities.)0 2075 y Ft(2.4)121 b(Remaining)34 b(P)-6 b(ossibilities)0 2231 y Fs(Our)27 b(problem)f(can)h(be)g(di)n(vided)f(into)h(tw)o(o)h (parts,)h(an)e(easy)h(one)0 2330 y(and)20 b(a)g(dif)n(\002cult)g(one.)0 2529 y Fq(The)j(Easy)g(P)m(ar)s(t)0 2653 y Fs(That)d d969 2 a970 2 (to)h(imple-)0 2752 y(ment)26 b(user)n(-space)g(threads)g(is)i (commonly)c(kno)n(wn,)j(of)f(course.)0 2852 y(Mostly)15 d972 3 a974 3 0 2952 y(based)24 b(on)g(them,)h(although)e(some)h(problems)f(are)i (kno)n(wn)e(with)0 3051 y(these)i(f)o(acilities)h(\(see)f(belo)n(w\).) 38 b(So)25 b(it)h(becomes)e(clear)m(,)i(that)f(we)0 3151 d976 3 a978 3 (our)f(machine)f(con-)0 3251 y(te)o(xt)j(\()p Fn(mctx)p 353 3251 25 4 v 29 w(t)p Fs(\))g(on)g(their)g Fj(jmp)p 885 3251 V 29 w(buf)g Fs(data)g(structure.)125 3350 y(W)-7 d980 7 a986 7 (e)0 3450 y(at)i(least)f(solv)o(ed)g(the)g(dispatching)e(problem,)49 b(because)c(our)0 3549 y Fj(mctx)p 205 3549 V 29 w(save)27 b Fs(and)f Fj(mctx)p 808 3549 V 29 w(restore)g Fs(functions)g(were)g (mod-)0 3649 y(eled)20 b(wisely)h(after)e Fj(setjmp)p Fs(\(3\))g(and)h Fj(longjmp)p Fs(\(3\).)0 3847 y Fq(The)j(Dif\002cult)g (P)m(ar)s(t)0 3972 y Fs(Ne)n(v)o(ertheless)32 b(the)h(dif)n(\002cult)g (problem)e(of)i(ho)n(w)g(to)g(create)g(the)0 4071 y(machine)g(conte)o d988 1 a988 1 4171 y(chine)20 b(conte)o(xt)g(is)i Fj(jmp)p 698 4171 d990 1 a990 1 b(A)0 4270 y Fj(jmp)p 155 4270 V 29 w(buf)21 b Fs(has)f(to)g(be)h d992 2 a993 2 4370 y(ture)29 b(\227)h(for)f(portability)f(reasons.)53 b(The)29 b(only)g(operations)f(we)0 4470 y(can)17 b(perform)e(on)h(it,) d995 3 a997 3 4569 y(of)26 b(course.)42 b(Additionally)-5 b(,)26 b(we)g(are)g(forced) f(to)h(use)h Fj(sigalt-)0 4669 y(stack)p Fs(\(3\))f(for)h(our)g(stack)h (manipulations,)f(because)g(it)h(is)h(the)0 4769 y(only)19 d999 2 a1000 2 4868 y(It)32 b(should)g(be)g(noted,)j(that)d(other)g(thread)f(packages) h(ha)n(v)o(e)0 4968 y(tried)48 b(similar)g(approaches.)107 d1002 40 a1041 40 Fs(S)t(.)1994 83 y(T)t Fo(H)t(AU)r Fs(')-5 b(s)40 b Fr(Really)e(Simple) g(Thr)m(eads)g Fs(\(rsthreads,)k([21)o(]\))d(pack-)1992 183 y(age)c(uses)i Fj(sigaltstack)p Fs(\(2\))c(in)j(a)h(v)o(ery)e (similar)h(w)o(ay)g(for)1992 282 y(thread)28 b(creation.)54 b(And)31 b(K)r Fo(O)q(T)n(A)j Fs(A)t Fo(B)t(E)r Fs(')-5 b(s)31 b Fr(P)-7 b(ortable)30 b(Thr)m(ead)f(Li-)1992 382 y(br)o(ary)21 b Fs(\(PTL,)g([18)n(]\))g(uses)h(a)g(similar)g Fj(sigstack)p Fs(\(2\))d(approach)1992 482 y(as)f(its)i(f)o(allback)d (approach)f(for)i(thread)f(creation.)23 b(But)c(both)e(v)n(ari-)1992 581 y(ants)31 b(do)g(not)f(w)o(ork)h(on)f(some)h(System-V)-8 b(-deri)n(v)o(ed)28 b(platforms,)1992 681 y(for)19 b(reasons)h(we)g (will)h(discuss)g(later)-5 b(.)2116 787 y(It)21 b(is)h(clear)e(our)g (implementation)f(for)h Fj(mctx)p 3445 787 V 29 w(create)g Fs(has)1992 886 y(to)32 b(play)h(a)g(fe)n(w)f(tricks)h(to)g(use)g(a)g Fj(jmp)p 3176 886 V 29 w(buf)g Fs(for)f(passing)g(e)o(x-)1992 986 y(ecution)i(control)h(to)h(an)g(arbitrary)f(startup)g(routine.)71 b(And)36 b(it)1992 1086 y(has)c(to)h(be)f(carefully)g(to)g(not)h(suf)n (fer)e(from)h(une)o(xpected)e(side-)1992 1185 y(ef)n(fects.)54 b(It)30 b(should)f(be)h(also)h(ob)o(vious)d(that)i(we)h(cannot)e(again) 1992 1285 y(e)o(xpect)e(to)i(\002nd)g(an)f(easy)h(solution)f(\(as)h (for)f Fj(mctx)p 3527 1285 V 30 w(save)g Fs(and)1992 1385 y Fj(mctx)p 2197 1385 V 29 w(restore)p Fs(\),)40 b(because)c Fj(setjmp)p Fs(\(3\))g(and)g Fj(sigalt-)1992 1484 y(stack)p Fs(\(3\))15 b(cannot)i(be)g(tri)n(vially)g(combined)f (to)h(achie)n(v)o(e)g(our)f(e)o(x-)1992 1584 y(pected)j(semantics)h (for)g Fj(mctx)p 2900 1584 V 29 w(create)p Fs(.)1992 1932 y Fu(3)143 b(Implementation)1992 2130 y Fs(As)23 b(we)g(ha)n(v)o(e)f(already)g(disco)o(v)o(ered,)f(our)h(implementation) e(con-)1992 2229 y(tains)35 b(an)g(easy)h(part)f(\()p Fj(mctx)p 2880 2229 V 29 w(save)g Fs(and)g Fj(mctx)p 3500 2229 V 29 w(restore)p Fs(\))1992 2329 y(and)30 b(a)i(dif)n (\002cult)f(part)g(\()p Fj(mctx)p 2899 2329 V 29 w(create)p Fs(\).)57 b(Let)32 b(us)g(start)f(with)1992 2428 y(the)20 b(easy)g(part,)g(whose)f(implementation)f(is)k(ob)o(vious:)3614 2398 y Fp(5)p Black Black 1992 2635 a Ff(typedef)43 b(struct)h Fh(mctx_st)g Fg(f)2171 2726 y Fh(jmp_buf)f(jb;)1992 2817 y Fg(g)i Fh(mctx_t;)1992 3000 y Ff(#define)e Fh(mctx_save\(mctx\))2171 3091 y(\(setjmp\(mctx->jb\)\))1992 3274 y Ff(#define)g Fh(mctx_restore\(mctx\))2171 3365 y(\(longjmp\(mctx->jb,)f(TRUE\)\)) 1992 3548 y(mctx)p 2176 3548 23 4 v 26 w(t)i(*)1992 3639 d1043 7 a1049 7 Fh(\),)2529 3730 y Ff(void)h Fh(*arg,)2529 3822 y Ff(void)g Fh(*skaddr,)2529 3913 y Ff(size_t)f Fh(sksize)223 b(\))1992 4004 y Fg(f)2171 4096 y Fh(mctx)p 2355 4096 V 26 w(t)45 b(*mctx;)2190 4278 y Fe(...to)17 b(be)j(\002lled)e(in...)2171 4370 y Ff(return)44 b Fh(mctx;)1992 4461 y Fg(g)1992 4669 y Fs(There)29 b(is)i(only)f(one)f(subtle)h(b)n(ut)h(important)d (point)i(we)h(should)1992 4769 y(mention:)42 b(The)29 d1051 18 a1068 7 4868 y(recti)n(v)o(e)d(to)i(implement)e Fj(mctx)p 2923 4868 25 4 v 29 w(save)h Fs(and)g Fj(mctx)p 3527 4868 V 29 w(restore)1992 4968 y Fs(is)j(not)g(indiscriminately)-5 b(.)52 b(F)o(or)30 b(technical)f(reasons)h(related)f(to)p Black 0 5021 764 4 v 90 5077 a Fv(5)120 5100 y Fo(All)17 b(error)h(checks)g(are)g(omitted)g(for)f(better)i(readability)l(.)p Black Black 0 5400 a Fm(P)n(or)q(table)11 b(Multithreading)0 d1075 25 a1099 34 5 4 bop Black Black 0 83 a Fj(setjmp)p Fs(\(3\))33 b(and)h Fj(return)g Fs(\(which)g(we)h(will)h(e)o(xplain)d(later)0 183 y(in)25 b(detail\))f(we)g Fr(cannot)h Fs(implement)e(these)h(tw)o (o)h(functions)e(as)i(C)0 282 y(functions)19 b(if)h(we)h(w)o(ant)f(to)g (achie)n(v)o(e)f(maximum)g(portability)-5 b(.)125 382 y(Before)29 b(we)i(no)n(w)f(can)g(proceed)f(with)h(\002lling)h(in)f (the)h(gaps)0 482 y(of)19 b(the)g(implementation)e(sk)o(eleton)i(for)f Fj(mctx)p 1368 482 25 4 v 29 w(create)p Fs(,)h(let)h(us)0 581 y(\002rst)h(discuss)g(the)f(proposed)e(algorithm.)0 850 y Ft(3.1)121 b(Ov)m(er)t(vie)n(w)0 1006 y Fs(The)26 b(general)f(idea)h(for)g Fj(mctx)p 921 1006 V 29 w(create)p Fs(,)h(is)g(to)g(con\002gure)d(the)0 1105 y(gi)n(v)o(en)18 b(stack)i(as)h(a)f(signal)g(stack)g(via)f Fj(sigaltstack)p Fs(\(2\),)e(send)0 1205 y(the)37 b(current)f(process)h(a)g(signal)g(to) h(transfer)e(e)o(x)o(ecution)f(con-)0 1305 y(trol)d(onto)f(this)i (stack,)i(sa)n(v)o(e)d(the)g(machine)f(conte)o(xt)f(there)i(via)0 1404 y Fj(setjmp)p Fs(\(3\),)19 b(get)j(rid)f(of)g(the)g(signal)g (handler)f(scope)h(and)f(boot-)0 1504 y(strap)g(into)g(the)g(startup)g (routine.)125 1603 y(As)46 b(mentioned,)k(the)45 b(real)h(problem)d(in) j(this)g(approach)0 1703 y(comes)27 b(from)e(the)i(signal)g(handler)f (scope)g(which)h(implies)g(v)n(ar)n(-)0 1803 y(ious)g(restrictions)g (on)g(Unix)g(platforms.)45 b(As)28 b(we)f(will)h(see,)i(we)0 1902 y(ha)n(v)o(e)25 b(to)g(perform)e(a)j(fe)n(w)f(tricks)g(to)h(get)f (rid)g(of)g(it.)41 b(The)25 b(second)0 2002 y(main)30 b(problem)e(is:)45 b(ho)n(w)30 b(do)f(we)i(prepare)d(the)i(calling)f (of)h(the)0 2102 y(start)21 b(routine)e(without)g(actually)h(entering)f (it.)0 2370 y Ft(3.2)121 b(Algor)r(ithm)0 2526 y Fs(The)33 b(input)g(to)h(the)g Fj(mctx)p 808 2526 V 29 w(create)f Fs(function)f(is)i(the)g(thread)0 2626 y(startup)24 b(routine)g Fr(func)p Fs(,)h(the)g(thread)f(startup)g(ar)o(gument)f Fr(ar)m(g)i Fs(and)0 2725 y(a)g(chunk)e(of)i(memory)e(starting)h(at)i Fr(skaddr)g Fs(and)e(of)h(size)g Fr(sksize)p Fs(,)0 2825 y(which)20 b(should)f(become)g(the)h(threads)g(stack.)26 b(The)20 b(algorithm)e(of)0 2925 y Fj(mctx)p 205 2925 d1101 2 a1102 2 0 3024 y(algorithm)j(one)g(can)h(\002nd)g(in)h Fr(GNU)g(Pth)p Fs(:)p Black 104 3196 a(1.)p Black 41 w(Allocate)f(a)g(ne)n(w)g d1104 3 a1106 3 Black 104 3358 a(2.)p Black 41 w(Preserv)o(e)g(the)h(current)f(signal)h (mask)h(and)e(block)h(an)g(arbi-)208 3457 y(trary)f(w)o(ork)o(er)g (signal.)p Black 104 3619 a(3.)p Black 41 w(Preserv)o(e)33 d1108 4 a1111 4 3718 y(w)o(ork)o(er)14 b(signal)h(con\002gure)f(a)i(trampoline)d (function)h(as)i(the)208 3818 y(ne)n(w)k(temporary)e(signal)i(action.)p Black 104 3980 a(4.)p Black 41 w(Preserv)o(e)d(a)i(possibly)f(acti)n(v) o(e)g(alternate)g(signal)g(stack)h(and)208 4079 y(con\002gure)35 d1113 3 a1115 3 4179 y(signal)20 b(stack)g(of)g(length)f Fr(sksize)p Fs(.)p Black 104 4341 a(5.)p Black 41 w(Send)33 b(the)h(current)e (process)h(the)h(w)o(ork)o(er)f(signal,)j(tem-)208 4440 d1117 2 a1118 2 (to)g(be)g(deli)n(v-)208 4540 y(ered)24 b(on)g(the)h(signal)g(stack)g (in)f(order)g(to)h(transfer)f(e)o(x)o(ecu-)208 4639 y(tion)19 d1120 2 a1121 2 104 4801 a(6.)p Black 41 w(After)31 b(the)g(trampoline)f(function)g (asynchronously)e(en-)208 4901 y(tered,)18 b(sa)n(v)o(e)g(its)i d1123 13 a1135 13 5000 y(and)f(immediately)g(return)f(from)h(it)i(to)f(terminate)f(the)h (sig-)208 5100 y(nal)h(handler)e(scope.)p Black 2095 83 a(7.)p Black 41 w(Restore)j(the)g(preserv)o(ed)d(alternate)i(signal) h(stack)f(and)h(the)2199 183 y(preserv)o(ed)c(signal)h(action)g(and)g (mask)g(for)g(w)o(ork)o(er)g(signal.)p Black 2095 358 a(8.)p Black 41 w(Sa)n(v)o(e)122 b(the)g(current)e(machine)h(conte)o (xt)g(of)2199 458 y Fj(mctx)p 2404 458 V 29 w(create)p Fs(.)p Black 2095 633 a(9.)p Black 41 w(Restore)16 b(the)g(pre)n (viously)e(sa)n(v)o(ed)i(machine)f(conte)o(xt)g(of)h(the)2199 733 y(trampoline)k(function)h(\()p Fr(mctx)p Fs(\))h(to)g(again)f (transfer)g(e)o(x)o(ecu-)2199 833 y(tion)29 b(control)e(onto)i(the)g (alternate)f(stack,)j(b)n(ut)e(this)g(time)2199 932 y(without\(!\))23 b(signal)e(handler)d(scope.)p Black 2054 1108 a(10.)p d1137 8 a1144 8 Fr(mctx)p Fs(\))2199 1207 y(again,)47 b(immediately)41 b(bootstrap)h(into)g(a)h(clean)f(stack)2199 1307 y(frame)k(situation)h (by)f(calling)h(a)g(second)f(trampoline)2199 1407 y(function.)p Black 2054 1582 a(11.)p Black 40 w(Set)56 b(the)f(ne)n(w)f(signal)h (mask)g(to)g(be)g(the)g(same)g(as)2199 1682 y(the)37 b(original)f(signal)g(mask)h(which)f(w)o(as)i(acti)n(v)o(e)e(when)2199 1781 y Fj(mctx)p 2404 1781 V 29 w(create)20 b Fs(w)o(as)h(called.)p Black 2054 1957 a(12.)p Black 40 w(Load)50 b(the)i(startup)e d1146 1 a1146 1 b(from)2199 2057 y Fj(mctx)p 2404 2057 V 29 w(create)20 d1148 7 a1154 7 Black 2054 2232 a(13.)p Black 40 w(Restore)26 b(the)g(machine)f(conte)o (xt)g(of)h Fj(mctx)p 3486 2232 V 29 w(create)f Fs(to)2199 2332 y(transfer)19 b(e)o(x)o(ecution)f(control)h(back)h(to)g(it.)p Black 2054 2507 a(14.)p Black 40 w(Return)28 b(the)h(generated)d (machine)i(conte)o(xt)f Fr(mctx)i Fs(to)g(the)2199 2607 y(caller)-5 b(.)1992 2799 y(When)23 b(we)h(no)n(w)g(again)f(switch)h (into)f(the)h(established)g(machine)1992 2899 y(conte)o(xt)15 d1156 1 a1156 1 Fr(func)g Fs(with)1992 2998 y(ar)o(gument)22 b Fr(ar)m(g)p d1158 1 a1158 1 (the)1992 3098 y(algorithm:)2001 3343 y d1160 1 a1160 1 2001 3343 a d1572 7 a1578 1 Black 0 5400 a Fm(P)n(or)q(table)11 b(Multithreading)0 d1585 68 a1652 74 6 5 bop Black Black 0 88 a Ft(3.3)121 b(Source)34 b(Code)0 244 y Fs(The)46 b(corresponding)d(ANSI)j(C)h(code,)52 b(which)46 b(implements)0 343 y Fj(mctx)p 205 343 25 4 v 29 w(create)p Fs(,)56 b(is)49 b(a)h(little)g(bit)f(more)f (complicated,)54 b(of)0 443 y(course.)g(But)31 b(with)f(the)g (presented)f(algorithm)f(in)j(mind,)g(it)g(is)0 543 y(no)n(w)20 b(straight-forw)o(ard.)p Black Black 0 692 a Ff(static)44 b Fh(mctx_t)716 b(mctx_caller;)0 766 y Ff(static)44 b(volatile)f Fh(sig_atomic_t)g(mctx_called;)0 916 y Ff(static)h Fh(mctx_t)88 b(*mctx_creat;)0 990 y Ff(static)44 b(void)134 b Fh (\(*mctx_creat_func\)\()p Ff(void)41 b Fh(*\);)0 1065 y Ff(static)j(void)178 b Fh(*mctx_creat_arg;)0 1140 y Ff(static)44 b Fh(sigset_t)f(mctx_creat_sigs;)0 1289 y(mctx_t)h(*mctx_create\()179 1364 y Ff(void)g Fh(\(*func\)\()p Ff(void)f Fh(*\),)i Ff(void)f Fh(*arg,)179 1439 y Ff(void)g Fh(*skaddr,)g Ff(size_t)g Fh(sksize\))0 1513 y Fg(f)179 1588 y Fh(mctx_t)g(*mctx;)179 1663 y Ff(struct)g Fh(sigaction)f(sa;)179 1738 y Ff(struct)h Fh(sigaction)f(osa;)179 1812 y Ff(struct)h Fh(sigaltstack)f(ss;)179 1887 y Ff(struct)h Fh(sigaltstack)f(oss;)179 1962 y(sigset_t)h(osigs;)179 2036 y(sigset_t)g(sigs;)179 2186 y(/*)64 b Fe(Step)18 b(1:)45 b Fh(*/)179 2261 y(mctx)f(=)h (malloc\()p Ff(sizeof)p Fh(\(mctx_t\)\);)179 2410 y(/*)64 b Fe(Step)18 b(2:)45 b Fh(*/)179 2485 y(sigemptyset\(&sigs\);)179 2560 y(sigaddset\(&sigs,)d(SIGUSR1\);)179 2634 y (sigprocmask\(SIG_BLOCK,)717 2709 y(&sigs,)i(&osigs\);)179 2858 y(/*)64 b Fe(Step)18 b(3:)45 b Fh(*/)179 2933 y(memset\()p Ff(void)e Fh(*\)&sa,)h(0,)493 3008 y Ff(sizeof)p Fh(\()p Ff(struct)807 3083 y Fh(sigaction\)\);)179 3157 y(sa.sa_handler)f(=)359 3232 y(mctx_create_trampoline;)179 3307 y(sa.sa_flags)g(=)i (SA_ONSTACK;)179 3381 y(sigemptyset\(&sa.sa_mask\);)179 3456 y(sigaction\(SIGUSR1,)d(&sa,)i(&osa\);)179 3606 y(/*)64 b Fe(Step)18 b(4:)45 b Fh(*/)179 3680 y(ss.ss_sp)178 b(=)45 b(skaddr;)179 3755 y(ss.ss_size)88 b(=)45 b(sksize;)179 3830 y(ss.ss_flags)e(=)i(0;)179 3904 y(sigaltstack\(&ss,)d(&oss\);)179 4054 y(/*)64 b Fe(Step)18 b(5:)45 b Fh(*/)179 4129 y(mctx_creat)268 b(=)44 b(mctx;)179 4203 y(mctx_creat_func)f(=)h(func;)179 4278 y(mctx_creat_arg)88 b(=)44 b(arg;)179 4353 y(mctx_creat_sigs)f(=)h (osigs;)179 4428 y(mctx_called)223 b(=)44 b(FALSE;)179 4502 y(kill\(getpid\(\),)f(SIGUSR1\);)179 4577 y(sigfillset\(&sigs\);) 179 4652 y(sigdelset\(&sigs,)f(SIGUSR1\);)179 4726 y Ff(while)i Fh(\(!mctx_called\))359 4801 y(sigsuspend\(&sigs\);)179 4951 y(/*)64 b Fe(Step)18 b(7:)45 b Fh(*/)179 5025 y (sigaltstack\(NULL,)d(&ss\);)179 5100 y(ss.ss_flags)h(=)i(SS_DISABLE;) 2171 83 y(sigaltstack\(&ss,)d(NULL\);)2171 158 y(if)i (\(!\(oss.ss_flags)f(&)h(SS_DISABLE\)\))2350 232 y(sigaltstack\(&oss,)e (NULL\);)2171 307 y(sigaction\(SIGUSR1,)g(&osa,)i(NULL\);)2171 382 y(sigprocmask\(SIG_SETMASK,)2709 457 y(&osigs,)f(NULL\);)2171 606 y(/*)63 b Fe(Step)19 b(8,9:)45 b Fh(*/)2171 681 y (mctx_switch\(&mctx_caller,)40 b(mctx\);)2171 830 y(/*)63 b Fe(Step)19 b(14:)45 b Fh(*/)2171 905 y Ff(return)f Fh(mctx;)1992 980 y Fg(g)1992 1129 y Ff(void)g Fh (mctx_create_trampoline\()p Ff(int)c Fh(sig\))1992 1204 y Fg(f)2171 1279 y Fh(/*)63 b Fe(Step)19 b(6:)45 b Fh(*/)2171 1353 y Ff(if)f Fh(\(mctx_save\(mctx_creat\))d(==)k(0\))f Fg(f)2350 1428 y Fh(mctx_called)f(=)i(TRUE;)2350 1503 y Ff(return)p Fh(;)2171 1577 y Fg(g)2171 1727 y Fh(/*)63 b Fe(Step)19 b(10:)45 b Fh(*/)2171 1802 y(mctx_create_boot\(\);)1992 1876 y Fg(g)1992 2026 y Ff(void)f Fh(mctx_create_boot\()p Ff(void)p Fh(\))1992 2100 y Fg(f)2171 2175 y Fh(mctx_t)g(*mctx_start;) 2171 2250 y Ff(void)g Fh(\(*mctx_start_func\)\()p Ff(void)d Fh(*\);)2171 2325 y Ff(void)j Fh(*mctx_start_arg;)2171 2474 y(/*)63 b Fe(Step)19 b(11:)45 b Fh(*/)2171 2549 y(sigprocmask\(SIG_SETMASK,)2709 2623 y(&mctx_creat_sigs,)2709 2698 y(NULL\);)2171 2848 y(/*)63 b Fe(Step)19 b(12:)45 b Fh(*/)2171 2922 y(mctx_start)267 b(=)45 b(mctx_creat;)2171 2997 y(mctx_start_func)d(=)j(mctx_creat_func;)2171 3072 y(mctx_start_arg)87 b(=)45 b(mctx_creat_arg;)2171 3221 y(/*)63 b Fe(Step)19 b(13:)45 b Fh(*/)2171 3296 y (mctx_switch\(mctx_start,)2709 3371 y(&mctx_caller\);)2171 3520 y(/*)63 b Fe(The)18 b(thread)h(``magically'')g(starts...)63 b Fh(*/)2171 3595 y(mctx_start_func\(mctx_start_arg\);)2171 3670 y(abort\(\);)1992 3744 y Fg(g)1992 4030 y Ft(3.4)120 b(Remaining)35 b(Issues)1992 4190 y Fs(The)23 b(presented)g(algorithm)g (and)g(source)h(code)f(can)h(be)g(directly)1992 4290 d1654 2 a1655 2 (threading)1992 4389 y(system)19 b(or)g(the)h(concept)e(of)h (co-routines.)k(Its)d(big)f(adv)n(antage)f(is,)1992 4489 d1657 2 a1658 2 (POSIX)1992 4589 y(primiti)n(v)o(es,)i(we)i(do)f(not)f(need)h(to)g(kno) n(w)f(an)o(ything)f(at)j(all)g(about)1992 4688 y(the)d(machine)g(we)h d1660 19 a1678 8 4788 y(Ne)n(v)o(ertheless,)32 b(there)f(remain)f(a)i(fe)n(w)f(special)g (issues)h(we)f(still)1992 4888 y(ha)n(v)o(e)19 b(to)h(discuss.)1992 5100 y Fq(The)j(W)m(aggly)g(longjmp\(3\))h(after)g(Retur)r(n)p Black 0 5400 a Fm(P)n(or)q(table)11 b(Multithreading)0 5458 y Fl(The)h(Signal)g(Stac)o(k)e(T)-5 b(r)q(ic)o(k)12 b(F)o(or)f(User-Space)g(Thread)h(Creation)1929 5400 y Fs(6)1221 b Fl(Cop)o(yr)q(ight)3398 5399 y(c)3381 5400 y Fk(\015)11 b Fl(1999)g(Ralf)h(S)o(.)f(Engelschall)3524 d1681 21 a1701 32 7 6 bop Black Black 0 83 a Fs(On)21 b(some)f(platforms,)f Fj(longjmp)p Fs(\(3\))g(may)h(not)g(be)h(called)f(after)0 183 y(the)e(function)e(which)i(called)g(the)g Fj(setjmp)p Fs(\(3\))e(returned.)22 b(When)0 282 y(this)30 b(is)h(done,)f(the)g (stack)g(frame)e(situation)i(is)g(not)f(guaranteed)0 382 y(to)f(be)f(in)h(a)g(clean)g(and)f(consistent)g(state.)48 b(But)28 b(this)g(is)h(e)o(xactly)0 482 y(on)22 b(what)h(our)f(trick)h (is)h(based)e(to)h(get)g(rid)f(of)h(the)g(signal)g(handler)0 581 y(scope,)d(when)f(you)g(remember)-5 b(.)125 682 y(The)20 b(only)g(alternati)n(v)o(e)f(w)o(ould)h(be)g(to)h(lea)n(v)o(e)g(the)f (signal)h(han-)0 781 y(dler)31 b(via)g Fj(longjmp)p Fs(\(3\),)h(b)n(ut) g(then)e(we)i(w)o(ould)f(ha)n(v)o(e)g(another)0 881 y(problem)g(as)j(e) o(xperience)d(sho)n(wed.)62 b(F)o(or)32 b(instance,)38 b(R)q Fo(O)t(B)t(E)t(RT)2 981 y Fs(S)t(.)h(T)t Fo(H)t(AU)r Fs(')-5 b(s)35 b Fr(Really)f(Simple)f(Thr)m(eads)h Fs(\(rsthreads\))e ([21)o(])i(w)o(as)0 1080 y(ported)20 b(to)h(se)n(v)o(eral)g(platforms)f (and)h(w)o(as)h(used)f(to)h(run)e(an)h(e)o(xper)n(-)0 1180 y(imental)j(multithreaded)e(v)o(ersion)h(of)h(the)h(Apache)e(web)i (serv)o(er)-5 b(.)2 1280 y(T)t Fo(H)t(AU)r Fs(')g(s)32 b(approach)c(dif)n(fered)g(mainly)i(in)g(the)h(w)o(ay)f(the)g(signal)0 1379 y(handler)17 b(is)j(left.)25 b(In)18 b(particular)m(,)f(in)i(an)g (attempt)f(to)h(a)n(v)n(oid)f(the)h(un-)0 1479 y(safe)27 b(stack)g(frame)f(situation,)i(it)f(uses)h(a)f Fj(longjmp)p Fs(\(3\))e(call)i(to)0 1578 y(lea)n(v)o(e)19 b(the)g(signal)f(handler)m (,)f(rather)h(than)h(returning)e(from)g(it.)26 b(But)0 1678 y(this)j(approach)d(does)h(not)h(w)o(ork)g(on)f(some)h(System-V)-8 b(-deri)n(v)o(ed)0 1778 y(k)o(ernels,)19 b(as)i(we)g(already)e (mentioned.)125 1878 y(The)e(problem)f(is)j(that)f(these)f(k)o(ernels)h (do)f(not)g(\223belie)n(v)o(e\224)g(that)0 1978 y(the)g(code)g(is)i (out)e(of)g(the)g(signal-handling)e(conte)o(xt)h(until)h(the)h(sig-)0 2078 y(nal)k(handler)f(has)h(returned)f(\227)i(and)e(accordingly)-5 b(,)20 b(refuse)i(to)g(al-)0 2177 y(lo)n(w)f(readjustment)e(of)i(the)g d1703 24 a1726 24 2277 y(the)f(rsthreads)g(approach,)e(the)j(signal)f(handler)f(for)h (the)g(creation)0 2376 y(of)f(the)g(\002rst)h(thread)e(ne)n(v)o(er)f (returns,)h(and)h(when)f(rsthreads)h(w)o(ants)0 2476 y(to)d(create)f(the)g(second)g(thread,)g(these)h(k)o(ernels)f(refuse)g (to)h(readjust)0 2576 y(the)24 b(signal)g(stack,)g(and)g(we)g(are)g (stuck.)35 b(So)24 b(with)h(portability)d(in)0 2675 y(mind,)i(we)h (decided,)e(that)i(it)g(is)g(better)f(to)g(get)g(rid)g(of)g(the)g (signal)0 2775 y(handler)30 b(scope)g(with)i(the)f(straight-forw)o(ard) d(\223)p Fj(return)p Fs(\224)j(and)0 2875 y(instead)g(\002ght)g(the)f (mentioned)g(\(simpler\))f(stack)j(frame)e(prob-)0 2974 y(lem.)125 3075 y(F)o(ortunately)f(in)j(practice)f(this)i(is)f(not)g (as)g(problematic)e(as)0 3174 y(it)c(seems,)h(because)e(e)n(v)n (aluations)f(on)g(mostly)h(all)h(current)e(Unix)0 3274 y(platforms)c(for)h Fr(GNU)h(Pth)g Fs(sho)n(wed,)e(that)i(one)f(can)g (reach)g(a)h(safe)0 3374 y(stack)17 b(frame)f(situation)g(again)g(by)g (just)i(calling)e(a)h(function.)22 b(That)0 3473 y(is)d(the)f(reason)f (why)g(our)g(algorithm)f(enters)i(the)g(second)f(trampo-)0 3573 y(line)j(function.)0 3777 y Fq(The)j(Uncooper)o(ativ)n(e)h (longjmp\(3\))0 3904 y Fs(Ev)o(en)k(on)h(operating)e(systems)i(which)g (ha)n(v)o(e)f(w)o(orking)g(POSIX)0 4003 y(functions,)d(our)g(approach)f (may)h(theoretically)f(still)j(not)e(w)o(ork,)0 4103 y(because)52 b Fj(longjmp)p Fs(\(3\))f(does)i(not)g(cooperate.)122 b(F)o(or)52 b(in-)0 4202 y(stance,)21 b(on)g(some)f(platforms)g(the)h (standard)f Fr(libc)h Fj(longjmp)p Fs(\(3\))0 4302 y(branches)d(to)h d1728 7 a1734 7 4402 y(tries)j(to)g(jump)f Fr(up)g Fs(the)h(stack,)f(i.e.,)h(into)g(a)g (stack)f(frame)g(that)h(has)0 4501 y(already)d(returned.)125 4602 y(This)f(is)h(usually)f(implemented)f(by)h(comparing)e(the)i (current)0 4701 y(stack)26 b(pointer)e(to)i(the)g(one)f(in)h(the)g Fj(jmp)p 1197 4701 25 4 v 29 w(buf)p Fs(.)41 b(That)26 b(is)h(why)d(it)0 4801 y(is)31 b(important)e(for)g(our)g(algorithm)g (to)h(return)f(from)g(the)h(signal)0 4901 y(handler)18 d1736 1 a1736 1 (the)f(par)n(-)0 5000 y(ent.)29 b(In)21 b(practice,)g(the)g d1738 17 a1754 17 5100 y(that)j(then)g(one)g(no)g(longer)f(suf)n(fers)g(from)g(those)h (uncooperati)n(v)o(e)1992 83 y Fj(longjmp)p Fs(\(3\))h (implementations,)h(b)n(ut)h(one)g(should)f(k)o(eep)g(this)1992 183 y(point)33 b(in)i(mind)f(when)g(reaching)f(e)n(v)o(en)g(more)h (uncooperati)n(v)o(e)1992 282 y(v)n(ariants)21 b(on)h(esoteric)h(Unix)f (platforms.)31 b(If)22 b(it)h(still)h(occurs,)e(one)1992 382 y(can)i(only)h(try)g(to)g(resume)g(the)g(operation)e(by)i(using)g (a)g(possibly)1992 482 y(e)o(xisting)19 b(platform-speci\002c)f(error)h (handling)f(hook.)1992 675 y Fq(Garbage)23 b(at)g(Bottom)h(of)f(Stac)n (ks)1992 798 y Fs(When)c(you)h(carefully)e(follo)n(wed)h(the)h (algorithms)f(control)g(\003o)n(w)-5 b(,)1992 898 y(you)25 b(certainly)h(ha)n(v)o(e)g(recognized)f(a)i(subtle)g(side-ef)n(fect:)37 b(there)1992 997 y(remains)20 b(some)g(garbage)f(at)i(the)g(bottom)e (of)i(each)f(thread)g(stack.)1992 1097 y(The)h(reason)h(is,)h(that)g (when)e(a)i(signal)f(is)h(deli)n(v)o(ered,)e(the)h(operat-)1992 1197 y(ing)f(system)i(pushes)e(some)h(state)h(onto)e(the)i(stack,)f (which)g(is)h(re-)1992 1296 y(stored)16 b(later)m(,)i(when)e(the)i d1756 2 a1757 2 1396 y(we)23 b(return)e(from)h(the)g(signal)h(handler)m(,)e(we)i(jump)f (in)h(again,)f(and)1992 1496 y(this)h(time)g(we)h(enter)e(not)h d1759 3 a1761 3 1595 y(because)c(of)h(the)g Fj(setjmp)p Fs(\(3\))f(in)h(the)g (trampoline)f(function.)2116 1695 y(Since)e(the)g(operating)e(system)i (has)g(to)g(capture)f(all)i(CPU)g(re)o(g-)1992 1794 y(isters)34 d1763 5 a1767 5 1992 1894 y(ters)d(or)f(caller)n(-sa)n(v)o(e)h(re)o(gisters\),)h(there) e(can)h(be)g(a)g(f)o(air)g(amount)1992 1994 y(of)24 b(memory)f(at)i (the)g(bottom)e(of)i(the)g(established)f(thread)g(stack.)1992 2093 y(F)o(or)c(some)g(systems)h(this)g(can)g(be)f(e)n(v)o(en)g(up)g (to)g(1)h(KB)g(of)g(garbage)1992 2193 y([21)n(].)35 b(But)23 d1769 1 a1769 1 2293 y(it)e(does)g(not)g(hurt,)f(of)h(course.)2116 2392 d1771 1 a1771 1 (stack)h(con-)1992 2492 y(sumption)17 b(when)h(deciding)e(the)j(stack)g d1773 3 a1775 3 2592 y(able)k(stack)h(size)h(usually)e(is)i(between)e(16)h(and)f(32)g (KB.)i(Less)g(is)1992 2691 y(neither)18 b(reasonable)h(nor)f(al)o(w)o (ays)j(allo)n(wed)e(\(current)f(Unix)h(plat-)1992 2791 d1777 3 a1779 3 (KB)h(in)g(size\).)1992 2984 y Fq(Stac)n(k)j(Ov)n(er\003o)o(ws)1992 3107 y Fs(There)52 b(is)i(a)f(noticeable)f(dif)n(ference)f(between)h (the)h(initial)1992 3207 y(\223thread\224)30 b(and)i(the)g(e)o d1781 2 a1782 2 3307 y(thread)22 b(runs)h(on)g(the)h(standard)e(process)h(stack.)35 b(This)23 b(stack)h(au-)1992 3406 y(tomatically)e(can)h(gro)n(w)g d1784 2 a1785 2 3506 y(spa)o(wned)19 b(threads)i(are)g(\002x)o(ed)g(in)g(size.)28 b(So)22 b(stack)f(o)o(v)o(er\003o)n(ws)f(can)1992 3606 d1787 1 a1787 1 b(This)16 b(implies)f(that)1992 3705 y(the)25 b(parent)f(has)i(to)f d1789 3 a1791 3 3805 y(stack)20 b(space)g(requirement)e(already)h(at)i(spa)o(wning)d (time.)2116 3904 y(And)26 b(there)g(is)i(no)e(really)g(portable)f (solution)g(to)i(this)g(prob-)1992 4004 y(lem,)18 b(because)f(e)n(v)o d1793 1 a1793 1 4104 y(detect)i(the)h(stack)g(o)o(v)o(er\003o)n(w)-5 d1795 2 a1796 2 4203 y(The)15 b(reason)h(is)h(simply)f(that)g(the)h(stack)f (initialization)g(goes)g(hand)1992 4303 y(in)28 b(hand)g(with)h(the)f d1798 2 a1799 2 4403 y(you)c(remember)-5 b(.)44 b(And)26 b(this)i(start)f(routine)f (has)h(to)g(be)f(a)i(real)f(C)1992 4502 y(function)22 d1801 1 a1801 1 (is)h(running,)1992 4602 y(there)20 b(no)h(longer)f(e)o(xists)i(such)f d1803 2 a1804 2 4701 y(the)k(scheduler)f(w)o(ould)g(be)i(able)f(to)g(gi)n(v)o(e)g(the)g (thread)f(a)i(ne)n(w)f(en-)1992 4801 y(lar)o(ged)15 b(stack,)i(there)g d1806 1 a1806 1 4901 y(ne)n(w)k(stack.)2116 5000 y(Or)h(more)f(correct,)f(there)i(is)g d1808 16 a1823 2 5100 y(the)h(problems)f(before,)g(when)h(one)g(accept)f(platform)g (speci\002cs,)p Black 0 5400 a Fm(P)n(or)q(table)11 b(Multithreading)0 d1830 20 a1849 28 8 7 bop Black Black 0 83 a Fs(there)22 b(is)g(a)h(chance,)e(of)h (course.)29 b(That')-5 b(s)22 b(why)g(our)f(implementa-)0 183 y(tion)26 b(did)g(not)g(deal)h(with)f(this)h(issue.)45 b(Instead)25 b(in)i(practice)f(one)0 282 y(usually)k(lets)i(the)f (scheduler)f(just)i(detect)f(the)g(stack)g(o)o(v)o(er\003o)n(w)0 382 y(and)18 b(terminate)g(the)h(thread.)k(This)c(is)h(done)e(by)g (using)g(a)h(red)g(zone)0 482 y(at)g(the)f(top)h(of)f(the)g(stack)h (which)f(is)h(mark)o(ed)e(with)i(a)g(magic)f(v)n(alue)0 581 y(the)30 b(scheduler)f(checks)h(between)f(thread)g(dispatching)f (opera-)0 681 y(tions.)125 784 y(More)i(adv)n(anced)f(solutions)i(are)g (only)g(possible)g(in)g(semi-)0 883 y(portable)19 b(w)o(ays.)27 b(One)20 b(approach)f(is)i(to)g(place)f(the)h(thread)f(stacks)0 983 y(into)i(shared)f(memory)f(se)o(gments)i(and)f(let)i(the)f (scheduler)e(catch)0 1082 y Fj(SIGSEGV)27 b Fs(signals.)49 b(When)28 b(such)f(a)i(signal)f(occurs,)h(because)0 1182 y(of)k(a)g(stack)g(o)o(v)o(er\003o)n(w)-5 b(,)33 b(the)g(scheduler)e (manually)h(resizes)h(the)0 1282 y(mapped)d(memory)g(re)o(gion.)57 b(Or)32 b(e)n(v)o(en)e(more)h(ele)o(gant,)i(as)f Fr(So-)0 1381 y(laris)p Fs(,)17 b Fr(F)-5 b(r)m(eeBSD)15 b Fs(and)g Fr(T)-5 b(ru64)15 b Fs(do)g(it:)24 b(the)15 b(scheduler)g(allocates)g (the)0 1481 y(threads)21 b(inside)g(shared)f(memory)g(se)o(gments)g (which)h(are)g(e)n(v)o(en)g(a)0 1581 y(fe)n(w)15 b(MB)i(in)e (\(virtual\))f(size)j(and)e(just)h(relies)f(on)g(the)h(virtual)f(mem-)0 1680 y(ory)i(system')-5 b(s)18 b(feature)f(of)h(actually)f(mapping)f (only)h(the)h(actually)0 1780 y(consumed)g(stack)j(space.)0 1995 y Fq(Star)s(tup)j(Routine)g(T)-10 b(er)r(mination)0 2125 y Fs(When)27 b(you)g(ha)n(v)o(e)g(read)g(the)g(proposed)e (implementation)h(care-)0 2225 y(fully)-5 b(,)23 b(you)f(should)g(ha)n d1851 4 a1854 4 2325 y(call)31 b(at)g(the)g(end)f(of)h Fj(mctx)p 832 2325 25 4 v 29 w(trampoline)p 1361 2325 V 28 w(jumpin)p Fs(.)56 b(This)0 2424 y(means,)36 b(when)c(the)h(startup)g(routine)f (returns,)j(the)e(process)g(is)0 2524 y(aborted.)47 b(That)28 d1856 1 a1856 1 2624 y(we)21 b(written)f(it)g(this)h(w)o(ay?)125 2726 d1858 2 a1859 2 (it)0 2826 y(should)18 b(be)i(cleanly)e(terminated,)g(of)i(course.)k (But)19 b(it)i(cannot)d(ter)n(-)0 2926 y(minate)23 b(itself)i(\(for)e d1861 2 a1862 2 3025 y(stack)e(while)g(running)d(on)j(it,)h(etc\).)32 b(The)23 b(termination)e(handling)0 3125 y(actually)g(is)i(the)f(task)g d1864 3 a1866 3 b(Usu-)0 3224 y(ally)c(the)f(thread)g(spa)o(wning)f(function)g(of)h (the)h(thread)f(library)f(is)0 3324 y(not)d(directly)f Fj(mctx)p 606 3324 V 29 w(create)p Fs(.)125 3427 y(Instead)34 d1868 3 a1870 3 3526 y(tional)19 b(trampoline)f(function)g(as)j(the)f(startup)f (routine.)k(And)d(this)0 3626 y(trampoline)25 b(function)g(performs)f (a)j(conte)o(xt)f(switch)g(back)g(into)0 3726 y(the)h(thread)f(library) d1872 3 a1874 3 3825 y(routine)g(returned.)49 b(The)28 b(scheduler)f(then)h(can)h (safely)f(remo)o(v)o(e)0 3925 y(the)40 b(thread)e(and)h(its)i(machine)d (conte)o(xt.)82 b(That)39 b(is)h(why)f(the)0 4025 y Fj(abort)p d1876 4 a1879 4 4239 y Fq(The)j(sigstac)n(k\(2\))f(F)l(allbac)n(k)h(Situation)0 4370 y Fs(Not)45 b(all)g(platforms)e(pro)o(vide)f(POSIX)j Fj(sigaltstack)p Fs(\(2\).)0 4470 y(Instead)59 b(the)o(y)f(at)i(least)g (pro)o(vide)d(the)j(4.2BSD)f(ancestor)0 4569 y(function)40 d1881 2 a1882 2 (re-)0 4669 y(place)29 b Fj(sigaltstack)p Fs(\(2\))d(by)j Fj(sigstack)p Fs(\(2\))e(in)i(this)h(situ-)0 4769 y(ation,)i(because)d d1884 40 a1923 41 4868 y Fj(sigstack)p Fs(\(2\))14 b(does)h(not)h(automatically)e(handle) h(the)h(machine)0 4968 y(dependent)i(direction)h(of)h(stack)g(gro)n (wth.)2116 83 y(Instead,)31 b(the)f(caller)g(has)g(to)g(kno)n(w)f(the)h (direction)e(and)h(al-)1992 183 y(w)o(ays)e(call)g Fj(sigstack)p Fs(\(2\))e(with)i(the)g(address)f(of)h(the)g(bottom)1992 282 y(of)34 b(the)h(stack.)68 b(So,)38 b(in)d(a)g(real-w)o(orld)e (implementation)g(\(lik)o(e)1992 382 y Fr(GNU)20 b(Pth)p Fs(\))e(one)h(\002rst)h(has)f(to)h(determine)d(the)j(direction)d(of)i (stack)1992 482 y(gro)n(wth)29 b(in)j(order)d(to)j(use)f Fj(sigstack)p Fs(\(2\))e(as)j(a)f(replacement)1992 581 y(for)25 b Fj(sigaltstack)p Fs(\(2\).)41 b(F)o(ortunately)25 b(this)i(is)g(easier)g(than)f(it)1992 681 y(seems)20 b(on)g(the)g(\002rst)h(look.)2785 651 y Fp(6)1992 925 y Fq(The)i(Blind)h(Alle)n(y)h(of)e(Br)o(ain-Dead)h(Platf)n(or)r(ms)1992 1067 y Fs(Finally)31 b(the)h(w)o(orld)f(w)o(ould)g(not)h(be)g(as)g (funn)o(y)e(as)j(it)f(is,)k(if)c(re-)1992 1167 y(ally)38 b(all)i(Unix)e(platforms)g(w)o(ould)g(be)h(f)o(air)g(to)g(us.)81 b(Instead,)1992 1267 y(currently)36 b(at)k(least)f(one)f(platform)f(e)o (xists)j(which)e(plays)g(un-)1992 1366 y(f)o(air:)28 b(unfortunately)19 b(the)j(popular)e Fr(GNU/Linux)p Fs(.)30 b(Although)20 b(we)1992 1466 y(will)f(disco)o(v)o(er)d(that)j(it)g (both)f(pro)o(vides)e Fj(sigaltstack)p Fs(\(2\))g(and)1992 1566 y Fj(sigstack)p Fs(\(2\),)29 b(our)g(approach)e(w)o(on')o(t)h(w)o (ork)h(on)g Fr(Linux)h Fs(k)o(er)n(-)1992 1665 y(nels)20 b(prior)f(to)i(v)o(ersion)d(2.2)i(and)g Fr(glibc)f Fs(prior)h(to)g(v)o (ersion)f(2.1.)2116 1774 y(Why?)75 b(Because)37 b(its)h Fr(libc)f Fs(pro)o(vides)e(only)h(dummies)g(of)1992 1873 y(these)e(functions)g(which)g(al)o(w)o(ays)h(return)f(just)h Fj(-1)g Fs(with)g Fj(er-)1992 1973 y(rno)c Fs(set)i(to)f Fj(ENOSYS)p Fs(.)e(So,)35 b(this)d(de\002nitely)f(means)h(that)f(our) 1992 2073 y(nifty)16 b(algorithm)f(is)j(useless)g(there,)f(because)f (its)i(central)e(point)h Fr(is)1992 2172 y Fj(sigaltstack)p Fs(\(2\)/)p Fj(sigstack)p Fs(\(2\).)50 b(Ne)n(v)o(ertheless)30 b(we)h(do)1992 2272 y(not)f(need)f(to)i(gi)n(v)o(e)e(up,)j(of)f (course.)54 b(At)31 b(least)g(not,)i(if)d(we,)j(for)1992 2372 y(a)24 b(single)f(brain-dead)f(platform,)h(accept)g(to)h(break)f (our)g(general)1992 2471 y(goal)c(of)h(not)g(using)g(an)o(y)f(platform) g(dependent)f(things.)2116 2580 y(So,)k(what)f(can)h(we)g(actually)f (do)g(here?)28 b(All)23 b(we)f(ha)n(v)o(e)f(to)g(do,)1992 2679 y(is)g(to)g(\002ddle)g(around)e(a)i(little)h(bit)f(with)g(the)g (machine-dependent)1992 2779 y Fj(jmp)p 2147 2779 V 29 d1925 1 a1925 1 Fj(setjmp.h)1992 2879 y Fs(or)20 b(by)h(disassembling)f d1927 4 a1930 4 2978 y(ally)k(one)f(just)i(has)g(do)e(a)i Fj(setjmp)p Fs(\(3\))e(to)h(get)g(an)g(initial)h(state)1992 3078 y(in)e(the)g Fj(jmp)p 2370 3078 V 29 w(buf)g Fs(structure)f(and)h(then) g(manually)e(adjust)i(tw)o(o)1992 3178 y(of)g(its)h(ingredients:)40 d1932 2 a1933 2 3277 y(ture)17 b(member)g(with)h(\223)p Fj(pc)p Fs(\224)g(in)g(the)g (name\))f(and)h(the)g(stack)g(pointer)1992 3377 y(\(usually)h(a)h d1935 2 a1936 2 (name\).)2116 3485 y(That')-5 b(s)26 b(all)g(and)f(can)g(be)h (acceptable)f(for)g(a)h(real-w)o(orld)e(im-)1992 3585 d1938 1 a1938 1 (platforms)e(\226)1992 3685 y(at)g(least)g(as)h(long)e(as)i(the)f d1940 2 a1941 2 3784 y(one)e(or)h(tw)o(o)g(platforms.)29 b(But)23 b(one)e(has)i(to)f(k) o(eep)f(in)h(mind)g(that)g(it)1992 3884 y(at)c(least)g(breaks)g(one)f d1943 7 a1949 7 3983 y(as)i(a)h(last)g(chance)e(solution,)g(of)h(course.)1992 4228 y Fq(More)j(Machine)h(Conte)n(xt)e(Ing)o(redients)1992 4370 y Fs(Finally)39 b(for)g(a)g(real-w)o(orld)f(threading)g (implementation)f(one)1992 4470 y(usually)d(w)o(ant)h(to)h(put)f(more)f (ingredients)g(into)h(the)g(machine)1992 4569 y(state)47 b Fn(mctx)p 2372 4569 V 29 w(t)h Fs(structure,)53 b(of)46 b(course.)106 b(F)o(or)46 b(instance)h(one)1992 4669 d1951 2 a1952 2 (to)g(ful\002ll)1992 4769 y(the)d(POSIX.1c)h(signal)f(handling)f (requirements)g(for)h(threads.)1992 4868 y(When)d Fj(sigsetjmp)p d1954 17 a1970 3 4968 y(can)15 b(just)i(use)f(them)f(instead)h(of)f Fj(setjmp)p Fs(\(3\))f(and)i Fj(longjmp)p Fs(\(2\))p Black 0 5021 764 4 v 90 5077 a Fv(6)120 5100 y Fo(See)h(the)h(\002le)f d1979 72 a2050 81 9 8 bop Black Black 0 83 a Fs(to)29 b(achie)n(v)o(e)e(this.)51 b(There)28 b(is)i(only)d(one)i(snare:)42 b(on)28 b(some)g(plat-)0 183 y(forms)19 b Fj(sigsetjmp)p Fs(/)p Fj(siglongjmp)p Fs(\(3\))c(sa)n(v)o(e)k(also)h(informa-)0 282 y(tion)k(about)f(the)h (alternate)g(signals)g(stack.)37 b(So)24 b(here)g(one)g(has)g(to)0 382 y(mak)o(e)j(sure)f(that)i(e)n(v)o(en)e(the)h(thread)f(dispatching)f (later)i(can)g(use)0 482 y Fj(sigsetjmp)p Fs(/)p Fj(siglongjmp)p Fs(\(3\),)d(the)j(thread)f(creation)g(step)0 581 y(has)18 b(to)h(use)f(plain)g Fj(setjmp)p Fs(/)p Fj(longjmp)p Fs(\(3\))d(for)j(the)g(trampoline)0 681 y(trick.)125 784 y(And)30 b(if)i Fj(sigsetjmp)p Fs(/)p Fj(siglongjmp)p Fs(\(3\))27 b(do)j(not)h(e)o(xist,)0 884 y(one)25 b(can)h(easily)g (emulate)f(them)g(by)g(sa)n(ving)h(and)f(restoring)f(the)0 984 y(thread)k(signal)g(masks)h(manually)e(via)i Fj(sigprocmask)p Fs(\(2\))d(in)0 1083 y Fj(mctx)p 205 1083 25 4 v 29 w(switch)p Fs(.)e(One)c(just)h(has)f(to)h(be)f(a)g(little)h(bit)f(carefully)f(to)0 1183 y(pre)n(v)o(ent)f(race)i(conditions)f(at)i(dispatching.)125 1286 y(Additionally)k(it)i(is)h(reasonable)d(to)i(also)g(sa)n(v)o(e)g (and)g(restore)0 1386 y(the)15 b(global)g Fj(errno)g Fs(v)n(ariable.)22 b(F)o(or)15 b(a)h(sophisticated)f(implemen-)0 1486 y(tation)24 b(which)g(does)g(also)h(all)g(those)f(additional)f (things,)i(ha)n(v)o(e)f(a)0 1585 y(look)c(at)i(the)f(source)g(\002le)g Fj(pth)p 895 1585 V 30 w(mctx.c)f Fs(in)i(the)f Fr(GNU)h(Pth)f Fs(dis-)0 1685 y(trib)n(ution)e([19)o(].)0 1977 y Ft(3.5)121 b(Summar)t(y)0 2140 y Fs(W)-7 b(e)26 b(ha)n(v)o(e)e(presented)g(a)h (pragmatic)f(and)g(mostly)h(portable)e(f)o(all-)0 2240 y(back)i(approach)e(for)i(implementing)e(user)n(-space)i(multithread-)0 2340 y(ing,)20 b(based)g(entirely)g(on)g(Unix)h(system)f(and)g(ANSI)h (C)h(language)0 2439 y(f)o(acilities.)44 b(The)26 b(approach)f(w)o(as)i (successfully)f(tested)g(in)h(prac-)0 2539 y(tice)34 b(on)e(a)i(wide)f(range)f(of)h(Unix)g(\003a)n(v)n(ors)h(by)e Fr(GNU)j(Pth)e Fs(and)0 2639 y(should)17 b(also)i(adapt)f(to)g(the)g (remaining)f(Unix)h(platforms)f(as)i(long)0 2738 y(as)j(the)o(y)e(beha) n(v)o(e)f(mostly)i(identical)f(to)h(the)g(current)e(major)h(Unix)0 2838 y(platforms.)0 3130 y Ft(3.6)121 b(Ac)n(kno)n(wledgements)0 3293 y Fs(I)21 b(w)o(ould)e(lik)o(e)i(to)g(thank)g(R)q Fo(O)t(B)t(E)t(RT)26 b Fs(S)t(.)f(T)t Fo(H)t(AU)r Fs(,)e(D)q Fo(A)-5 b(V)t(I)t(D)25 b Fs(B)s Fo(U)t(T)t(E)t(N)t Fs(-)2 3393 y Fo(H)t(O)t(F)r Fs(,)34 b(M)t Fo(A)t(RT)t(I)t(N)g Fs(K)t Fo(R)t(A)t(E)t(M)t(E)t(R)e Fs(and)e(E)t Fo(R)t(I)t(C)35 b Fs(N)t Fo(E)t(W)t(T)s(O)t(N)c Fs(for)d(their)0 3493 y(comments)16 b(which)g(helped)f(to)i(impro)o(v)o(e)e(this)i(paper)-5 b(.)23 b(Additional)0 3592 y(thanks)h(go)h(to)g(the)g(users)g(of)g Fr(GNU)g(Pth)g Fs(for)g(their)f(feedback)f(on)0 3692 y(the)i(implementation)e(which)i(helped)f(a)i(lot)f(in)h(\002ne-tuning) d(the)0 3791 y(presented)c(approach.)1082 b([)p Fb(rse)p Fs(])0 4112 y Fu(Ref)o(erences)p Black 71 4287 a FA([1])p Black 43 w(M)t(.)t(E)t(.)50 b(C)t Fa(O)t(N)t(W)m(A)n(Y)r FA(:)81 b Fw(Design)48 b(of)g(a)h(separ)o(able)g(tr)o(ansition-)199 4379 y(dia)o(gr)o(am)20 b(compiler)-8 b(.)p FA(,)19 b(Comm.)f(A)m(CM)h (6:7,)g(1963,)h(p.396-408)p Black 71 4511 a([2])p Black 43 w(E)t(.)t(W)m(.)26 b(D)t Fa(I)t(J)t(K)t(S)t(T)t(R)t(A)r FA(:)31 b Fw(Co-oper)o(ating)26 b(sequential)f(pr)m(ocesses)p FA(,)h(in)199 4602 y(F)-6 b(.)29 b(Genuys)j(\(Ed.\),)g Fw(Pr)m(o)o(gr)o(amming)f(Langua)o(g)o(es)p FA(,)k(N)m(A)-8 b(T)o(O)29 b(Ad-)199 4694 y(v)n(anced)f(Study)f(Institute,)g(Academic)h (Press,)f(London,)i(1965,)199 4785 y(p.42-112.)p Black 71 4917 a([3])p Black 43 w(B)t(.)18 b(N)t Fa(I)t(C)t(H)t(O)t(L)t(S)t FA(,)f(D)t(.)g(B)s Fa(U)t(T)t(T)t(L)t(A)t(R)t FA(,)f(J)t(.)t(P)l(.)g(F) o Fa(A)t(R)t(R)t(E)t(L)r FA(:)i Fw(Pthr)m(eads)e(Pr)m(o-)199 5009 y(gr)o(amming)26 b(-)e(A)h(POSIX)f(Standar)m(d)j(for)e(Better)f (Multipr)m(ocess-)199 5100 y(ing)p FA(,)19 b(O'Reilly)f(1996;)i(ISBN)e (1-56592-115-1)p Black 2062 83 a([4])p Black 44 w(B)t(.)29 b(L)t Fa(E)t(W)t(I)t(S)t FA(,)i(D)t(.)f(J)t(.)f(B)t Fa(E)t(R)t(G)r FA(:)38 b Fw(Thr)m(eads)27 b(Primer)g(-)f(A)h(Guide)g(T)-7 b(o)2191 174 y(Multithr)m(eaded)28 b(Pr)m(o)o(gr)o(amming)p FA(,)h(Prentice)d(Hall)g(1996;)32 b(ISBN)2191 266 y(0-13-443698-9)p Black 2062 389 a([5])p Black 44 w(S)t(.)22 b(J)t(.)h(N)t Fa(O)t(RT)s(O)t(N)t FA(,)f(M)t(.)h(D)t(.)g(D)t Fa(I)t(P)o(A)t(S)t(Q)s (U)r(A)t(L)t(E)r FA(:)e Fw(Thr)m(ead)g(T)l(ime)e(-)h(The)2191 480 y(Multithr)m(eaded)26 b(Pr)m(o)o(gr)o(amming)g(Guide)p FA(,)h(Prentice)d(Hall)h(1997;)2191 572 y(ISBN)18 b(0-13-190067-6)p Black 2062 695 a([6])p Black 44 w(D)t(.)28 b(R)t(.)h(B)s Fa(U)t(T)t(E)t(N)t(H)t(O)t(F)r FA(:)34 b Fw(Pr)m(o)o(gr)o(amming)26 b(with)f(POSIX)g(Thr)m(eads)p FA(,)2191 786 y(Addison)20 b(W)-6 b(esle)o(y)18 b(1997;)i(ISBN)e(0-201-63392-2)p Black 2062 909 a([7])p Black 44 w(W)m(.)t(R)t(.)t(S)t Fa(T)t(E)t(V)t(E)t(N)t(S)r FA(:)f Fw(Advanced)j(Pr)m(o)o(gr)o(amming)f (in)g(the)f(Unix)h(En-)2191 1001 y(vir)m(onment)p FA(,)g(Addison-W)-6 b(esle)o(y)h(,)19 b(1992;)h(ISBN)e(0-201-56317-7)p Black 2062 1124 a([8])p Black 44 w(D)t(.)46 b(L)t Fa(E)t(W)t(I)t(N)t(E)r d2052 3 a2054 3 b(Writing)2191 1215 y(P)-6 b(ortable)40 b(Unix)h(Pr)m(o)o(gr)o(ams)p FA(,)k(O'Reilly)40 b(&)g(Associates,Inc.,)2191 1306 y(1994;)20 b(ISBN)d(0-937175-73-0)p Black 2062 1430 a([9])p Black d2057 3 a2059 3 1521 y(questions)196 b(for)g(comp.os.r)m(esear)m(c)o(h)p FA(,)242 b(1995;)2191 1612 y(http://www)-5 b(.serpentine.com/)p Fh(\230)p FA(bos/os-f)o(aq/,)2191 1704 y d2061 1 a2061 1 2025 1827 a([10])p Black 44 w(S)t Fa(U)t(N)68 b FA(M)t d2064 3 a2066 3 1918 y(Ask)o(ed)25 b(Questions)p FA(,)h(1995,)g(http://www.sun.com/w)o (orkshop/-)2191 2010 y(threads/f)o(aq.html)p Black 2025 2133 a([11])p Black 44 w(B)t Fa(RY)m(A)t(N)84 b FA(O)t(')t(S)t d2068 2 a2069 2 b(ask)o(ed)g(ques-)2191 2224 y(tions)144 b(for)g(comp.pr)m(o)o(gr)o (amming)o(.thr)m(eads)p FA(,)178 b(1997;)2191 2315 y(http://www)-5 d2071 1 a2071 1 Black 2025 2439 a([12])p Black 44 w(B)t Fa(I)t(L)147 d2073 2 a2074 2 b(ask)o(ed)h(ques-)2191 2530 y(tions)e(for)g(comp.pr)m(o)o(gr)o(amming) o(.thr)m(eads)p FA(,)178 b(1999;)2191 2621 y(http://www)-5 d2076 1 a2076 1 2744 a([13])p Black 44 w(N)t Fa(U)t(M)t(E)t(R)t(I)t(C)62 d2078 1 a2078 1 b Fw(Multithr)m(eading)63 b(-)f(De\002ni-)2191 2836 y(tions)k(and)i d2080 2 a2081 2 2927 y(quest.com/lang/multi-frame.html)p Black 2025 3050 a([14])p Black 44 w(T)t Fa(H)t(E)46 b FA(O)t Fa(P)t(E)t(N)g d2083 2 a2084 2 (Speci\002cation,)2191 3142 y(V)-8 b(er)o(sion)51 b(2)g(-)g(Thr)m(eads) p FA(;)68 b(1997;)g(http://www.opengroup-)2191 3233 y(.or)o d2086 1 a2086 1 3356 a([15])p Black 44 w(S)t Fa(U)t(N)46 b FA(M)t Fa(I)t(C)t(R)r(O)t(S) d2088 1 a2088 1 Fw(SMI)44 b(Thr)m(ead)h(Resour)m(ces)p FA(;)2191 3447 d2090 1 a2090 1 2025 3571 a([16])p Black 44 w(F)t Fa(R)t(A)t(N)t(K)33 d2092 1 a2092 1 b(pthr)m(eads)p FA(;)39 b(1997;)g(http://www-)2191 3662 d2094 1 a2094 1 2025 3785 a([17])p Black 44 w(C)t Fa(H)t(R)t(I)t(S)90 d2096 1 a2096 1 b Fw(MIT)88 b(pthr)m(eads)p FA(;)124 b(1993;)2191 3877 d2098 3 a2100 3 3968 y(\(old\),)242 b(http://www.humanf)o(actor.com/pthreads/mit-)2191 4059 y(pthreads.html)19 b(\(updated\))p Black 2025 4182 a([18])p Black 44 w(K)r Fa(O)r(T)n(A)33 b FA(A)t Fa(B)t(E)r d2102 2 a2103 2 FA(\(PTL\);)e(1999;)2191 4274 y(http://www.media.osaka-cu.ac.jp/)p Fh(\230)p FA(k-abe/PTL/)p Black 2025 4397 a([19])p Black d2106 3 a2108 3 FA(\(Pth\);)2191 4488 y(1999;)72 b(http://www.gnu.or)o(g/softw)o (are/pth/,)63 b(ftp://ftp.gnu-)2191 4580 y(.or)o(g/gnu/pth/)p Black 2025 4703 a([20])p Black 44 w(M)t Fa(I)t(C)t(H)t(A)t(E)t(L)40 d2110 1 a2110 1 b Fw(POSIX)39 b(and)h(DCE)f(Thr)m(eads)2191 4794 y(F)-8 d2112 2 a2113 2 2191 4885 y(.net/)p Fh(\230)p FA(mtp/PCthreads.html)p Black 2025 5009 a([21])p Black 44 w(R)q Fa(O)t(B)t(E)t(RT)31 d2115 11 a2125 18 b(Simple)h(Thr)m(eads)g FA(\(rsthreads\);)2191 5100 y(1996;)20 b(ftp://ftp.ai.mit.edu/pub/rst/)p Black 0 5400 a Fm(P)n(or)q(table)11 b(Multithreading)0 5458 y Fl(The)h(Signal)g(Stac)o(k)e(T)-5 b(r)q(ic)o(k)12 b(F)o(or)f(User-Space)g(Thread)h(Creation)1929 5400 y Fs(9)1221 b Fl(Cop)o(yr)q(ight)3398 5399 y(c)3381 5400 y Fk(\015)11 b Fl(1999)g(Ralf)h(S)o(.)f(Engelschall)3524 5458 y(All)g(Rights)h(Reser)q(v)o(ed.)p Black eop %%Page: 10 10 10 9 bop Black Black Black 33 83 a FA([22])p Black 44 w(J)t Fa(O)t(H)t(N)100 b FA(B)t Fa(I)t(R)t(R)t(E)t(L)t(L)r FA(:)176 b Fw(F)l(r)m(eeBSD)97 b(uthr)m(eads)p FA(;)137 b(1998;)199 174 y(ftp://ftp.freebsd.or)o(g/pub/FreeBSD/FreeBSD-)199 266 y(current/src/lib/libc)p 755 266 23 4 v 27 w(r/uthread/)p Black 33 390 a([23])p Black 44 w(X)t Fa(A)l(V)t(I)t(E)t(R)51 b FA(L)t Fa(E)t(R)r(O)q(Y)r FA(:)81 b Fw(The)49 b(LinuxThr)m(eads)h (libr)o(ary)p FA(;)64 b(1999;)199 482 y(http://pauillac.inria.fr/)p Fh(\230)p FA(xlero)o(y/linuxthreads/)p Black 2025 83 a([24])p Black 44 w(I)t(B)t(M)r(:)23 b Fw(AIX)f(V)-8 d2127 10 a2136 10 2191 174 y(Writing)63 b(and)h(Deb)o(ug)o(ging)g(Pr)m(o)o(gr)o(ams;)85 b(Under)o(standing)2191 266 y(Thr)m(eads)p FA(;)58 b(1998;)g (http://www.rs6000.ibm.com/doc)p 3714 266 V 28 w(link/-)2191 357 y(en)p 2265 357 V 27 w(US/a)p 2442 357 V 26 w(doc)p 2575 357 V 28 w(lib/aixpr)o(ggd/genprogc/understanding-)p 2191 448 V 2213 448 a(threads.htm)p Black 0 5400 a Fm(P)n(or)q(table)11 b(Multithreading)0 5458 y Fl(The)h(Signal)g(Stac)o(k)e(T)-5 b(r)q(ic)o(k)12 b(F)o(or)f(User-Space)g(Thread)h(Creation)1908 5400 y Fs(10)1200 b Fl(Cop)o(yr)q(ight)3398 5399 y(c)3381 5400 y Fk(\015)11 b Fl(1999)g(Ralf)h(S)o(.)f(Engelschall)3524 @ 1.3 log @*** empty log message *** @ text @d13 1 a13 1 %DVIPSSource: TeX output 1999.08.18:1627 d387 8 a394 8 46 42 51 1[51 1[55 69 46 2[28 1[60 51 4[51 17[23 1[28 45[{TeXBase1Encoding ReEncodeFont}37 83.022 /Times-Italic rf /Fs 104[83 42 1[37 37 24[37 42 42 60 42 42 23 32 28 42 42 42 42 65 23 42 23 23 42 42 28 37 42 37 42 37 3[28 1[28 2[60 78 60 60 51 46 55 1[46 60 60 74 51 60 1[28 60 60 46 51 60 55 55 60 1[37 4[23 42 42 42 42 42 42 42 42 42 42 23 21 28 21 2[28 28 28 5[28 29[46 46 2[{ TeXBase1Encoding ReEncodeFont}76 83.022 /Times-Roman d421 1 a421 1 4[37 2[37 37 1[18 1[18 44[{TeXBase1Encoding ReEncodeFont}32 d452 1 a452 1 1491 y(Document)g(V)-5 b(ersion)18 b(0.9.14)p Black Black d521 8 a528 8 2 1 bop Black Black 0 83 a Fs(standardized)18 b(a)j(threading)d(API)j (\(kno)n(wn)d(as)j(Pthreads\).)1709 53 y Fp(3)1742 83 y Fs(.)26 b(But)0 183 y(an)21 b(API)g(and)f(a)h(fe)n(w)g(v)o(endor)d (implementation)h(are)i(not)f(enought)0 282 y(to)c(ful\002l)h(the)f (portability)e(requirements)h(of)h(free)f(softw)o(are)h(pack-)0 382 y(ages.)55 b(Here)30 b(portable)f(multithreading)e(libraries)j(are) g(needed)0 482 y(\002rst.)125 586 y(The)52 b(author)g(currently)g(kno)n (ws)g(about)g(o)o(v)o(er)g(fourteen)0 685 y(\(mostly)25 d582 14 a595 13 y Fs(F)o(or)g(instance)g(the)g(v)o(endor)f(threading)f(f)o(acilities)k (under)d Fr(Solaris)1992 308 y Fs(and)g Fr(AIX)27 b Fs(use)c(a)h Fn(M:N)g Fs(mapping)d([15)o(,)i(24)o(],)h(i.e.,)g Fn(M)g Fs(user)n(-space)1992 408 y(threads)j(are)i(mapped)d(onto)i Fn(N)h Fs(k)o(ernel-space)e(L)-6 b(WPs.)50 b(On)29 b(the)1992 508 y(other)h(hand)g Fr(LinuxThr)m(eads)g Fs([23)o(])i(under)e Fr(GNU/Linux)h Fs(uses)h(a)1992 607 y Fn(1:1)i Fs(mapping)f(and)i(pure) f(user)n(-space)g(implementations)f(lik)o(e)1992 707 y Fr(GNU)21 b(Pth)p Fs(,)h Fr(FSU)e(pthr)m(eads)h Fs(or)f Fr(MIT)i(pthr)m(eads)p Fs(,)e(etc.)29 b(use)21 b(a)g Fn(M:1)1992 806 y Fs(mapping)d([19)o(,)i(16)o(,)h(17)o(].)2116 907 y(From)38 b(no)n(w)g(on)g(we)h(talk)g(about)e(such)i Fn(M:1)f Fs(user)h(space)1992 1006 y(threading)i(approaches,)48 d895 1 a895 1 1992 1447 a Fq(Conte)n(xt)j(Switching)1992 1570 y Fs(There)38 d897 1 a897 1 1670 y(tics)18 b(of)g Fj(mctx)p 2418 1670 25 4 v 29 w(save)p d899 3 a901 3 1770 y(we)31 b(will)i(choose)d(later)m(,)k(will)f(support)d(e)o(xactly) h(these)g(seman-)1992 1869 y(tics.)25 b(Second,)19 b(we)h(w)o(ant)g(to) g(use)g(an)g(easy)g(dispatching)e(function)1992 1969 d903 10 a912 10 2137 a Fi(v)o(oid)1992 2228 y Fh(mctx_switch\()p Fi(mctx)p 2693 2228 23 4 v 25 w(t)45 b Fh(*)p Fw(fr)m(om)p Fh(,)2529 2320 y Fi(mctx)p 2690 2320 V 28 w(t)g Fh(*)p Fw(to)p Fh(\))1992 2411 y Fg(f)2081 2502 y Fi(if)g Fh(\(mctx_save\()p Fw(fr)m(om)p Fh(\))d(==)j(FALSE\))2216 2594 y(mctx_restore\()p Fw(to)p Fh(\);)2081 2685 y Fi(r)o(etur)o(n)p Fh(;)1992 2776 y Fg(g)1992 3046 y Ft(2.2)120 b(P)-6 b(ossibilities)1992 3202 y Fs(When)28 b(we)i(pok)o(e)e(around)f(in)i(the)g(references)f(of) g(the)h(ANSI)h(C)1992 3301 y(language)g(and)h(the)h(Unix)f(standards)g (we)i(\002nd)e(the)h(follo)n(wing)1992 3401 y(functions)18 d914 3 a916 3 Black 0.7 0.7 0.7 TeXcolorrgb 2087 3577 50 50 v Black Black 2199 3577 a(There)25 b(are)g(the)g(functions)f Fj(getcontext)p Fs(\(3\),)g Fj(make-)2199 3677 y(context)p d918 7 a924 7 3777 y(text)p Fs(\(3\))25 b(which)h(conform)e(to)i(the)g(Single)g(Unix) g(Spec-)2199 3876 y(i\002cation,)j(V)-9 b(ersion)27 b(2)g(\(SUSv2)g ([14)o(],)i(aka)e(Unix95/98\).)2199 3976 y(Unfortunately)d(these)j(are) g(a)n(v)n(ailable)f(on)h(modern)e(Unix)2199 4076 y(platforms)19 b(only)-5 b(.)p Black 0.7 0.7 0.7 TeXcolorrgb 2087 4239 V Black Black 2199 4239 a(There)37 b(are)h(the)g(functions)e Fj(setjmp)p Fs(\(3\))h(and)g Fj(long-)2199 4339 y(jmp)p d926 8 a933 11 4438 y(and)29 b Fj(sigsetjmp)p Fs(\(3\))d(and)j Fj(siglongjmp)p Fs(\(3\))d(which)2199 4538 y(conform)37 b(to)i(IEEE)f(Std1003.1-1988)d (\(POSIX\).)j(The)2199 4638 y(\002rst)24 b(tw)o(o)g(are)f(a)n(v)n (ailable)g(really)f(on)h(all)h(Unix)f(platforms,)2199 4737 y(the)d(last)h(tw)o(o)g(are)f(a)n(v)n(ailable)g(only)f(on)h(some)g (of)g(them.)p Black 0.7 0.7 0.7 TeXcolorrgb 2087 4901 V Black Black 2199 4901 a(There)44 b(is)i(the)e(function)f Fj(sigaltstack)p Fs(\(2\))f(which)2199 5000 y(conforms)c(to)i(the)g (Single)g(Unix)f(Speci\002cation,)44 b(V)-9 b(er)n(-)2199 5100 y(sion)48 b(2)g(\(SUSv2)g([14)n(]\))g(and)g(its)h(ancestor)e (function)p Black 0 5400 a Fm(P)n(or)q(table)11 b(Multithreading)0 d940 15 a954 10 4 3 bop Black Black 208 83 a Fj(sigstack)p Fs(\(2\))31 b(from)i(4.2BSD.)g(The)h(last)g(one)g(e)o(xists)208 183 y(only)25 b(on)g(BSD-deri)n(v)o(ed)f(platforms,)i(b)n(ut)g(the)g (\002rst)g(func-)208 282 y(tion)19 b(already)h(e)o(xists)g(on)g(all)h (current)e(Unix)g(platforms.)0 570 y Ft(2.3)121 b(Maxim)o(um)32 b(P)-6 b(or)5 b(tability)33 b(Solution)0 732 y Fs(The)49 b(maximum)e(portable)g(solution)h(ob)o(viously)f(is)j(to)f(use)0 832 y(the)54 b(standardized)e Fj(makecontext)p Fs(\(3\))f(function)h (to)i(cre-)0 931 y(ate)k(threads)e(and)h Fj(switchcontext)p Fs(\(2\))e(or)i Fj(getcon-)0 1031 y(text)p Fs(\(2\)/)p d956 2 a957 2 b(And)27 b(actu-)0 1131 y(ally)g(these)h(are)f(the)g(preferred)e (functions)h(modern)g(user)n(-space)0 1230 y(multithreading)14 d959 7 a965 7 1330 y(still)i(a)f(lot)g(of)f(platforms)g(where)g(this)h(approach)d (cannot)i(be)h(used.)0 1429 y(So)24 b(our)f(f)o(allback)f(approach)g (has)h(to)h(use)g(the)f(remaining)f(possi-)0 1529 y(bilities.)0 1817 y Ft(2.4)121 b(Remaining)34 b(P)-6 b(ossibilities)0 1979 y Fs(Our)26 b(problem)e(can)i(be)g(de)n(vided)e(into)i(tw)o(o)g (parts,)h(an)f(easy)h(one)0 2078 y(and)20 b(a)g(dif)n(\002cult)g(one.)0 2293 y Fq(The)j(Easy)g(P)m(ar)s(t)0 2424 y Fs(That)d d967 2 a968 2 (to)h(imple-)0 2524 y(ment)26 b(user)n(-space)g(threads)g(is)i (commonly)c(kno)n(wn,)j(of)f(course.)0 2623 y(Mostly)15 d970 3 a972 3 0 2723 y(based)24 b(on)g(them,)h(although)e(some)h(problems)f(are)i (kno)n(wn)e(with)0 2823 y(these)i(f)o(acilities)h(\(see)f(belo)n(w\).) 38 b(So)25 b(it)h(becomes)e(clear)m(,)i(that)f(we)0 2922 d974 3 a976 3 (our)f(machine)f(con-)0 3022 y(te)o(xt)j(\()p Fn(mctx)p 353 3022 25 4 v 29 w(t)p Fs(\))g(on)g(their)g Fj(jmp)p 885 3022 V 29 w(buf)g Fs(data)g(structure.)125 3125 y(W)-7 d978 7 a984 7 (e)0 3224 y(at)i(least)f(solv)o(ed)g(the)g(dispatching)e(problem,)49 b(because)c(our)0 3324 y Fj(mctx)p 205 3324 V 29 w(save)27 b Fs(and)f Fj(mctx)p 808 3324 V 29 w(restore)g Fs(functions)g(were)g (mod-)0 3423 y(eled)20 b(wisely)h(after)e Fj(setjmp)p Fs(\(3\))g(and)h Fj(longjmp)p Fs(\(3\).)0 3638 y Fq(The)j(Dif\002cult)g (P)m(ar)s(t)0 3769 y Fs(Ne)n(v)o(ertheless)32 b(the)h(dif)n(\002cult)g (problem)e(of)i(ho)n(w)g(to)g(create)g(the)0 3869 y(machine)g(conte)o d986 1 a986 1 3968 y(chine)20 b(conte)o(xt)g(is)i Fj(jmp)p 698 3968 d988 1 a988 1 b(A)0 4068 y Fj(jmp)p 155 4068 V 29 w(buf)21 b Fs(has)f(to)g(be)h d990 2 a991 2 4168 y(ture)29 b(\227)h(for)f(portability)f(reasons.)53 b(The)29 b(only)g(operations)f(we)0 4267 y(can)17 b(perform)e(on)h(it,) d993 3 a995 3 4367 y(of)26 b(course.)42 b(Additionally)-5 b(,)26 b(we)g(are)g(forced) f(to)h(use)h Fj(sigalt-)0 4467 y(stack)p Fs(\(3\))f(for)h(our)g(stack)h (manipulations,)f(because)g(it)h(is)h(the)0 4566 y(only)19 d997 2 a998 2 4669 y(It)32 b(should)g(be)g(noted,)j(that)d(other)g(thread)f(packages) h(ha)n(v)o(e)0 4769 y(tried)48 b(similar)g(approaches.)107 d1000 18 a1017 18 Fs(S)t(.)2 4868 y(T)t Fo(H)t(AU)r Fs(')-5 b(s)40 b Fr(Really)e(Simple)h (Thr)m(eads)f Fs(\(rsthreads,)k([21)o(]\))c(pack-)0 4968 y(age)e(uses)g Fj(sigaltstack)p Fs(\(2\))e(in)i(a)g(v)o(ery)f(similar)i (w)o(ay)f(for)1992 83 y(thread)28 b(creation.)54 b(And)31 b(K)r Fo(O)q(T)n(A)j Fs(A)t Fo(B)t(E)r Fs(')-5 b(s)31 b Fr(P)-7 b(ortable)30 b(Thr)m(ead)f(Li-)1992 183 y(br)o(ary)21 b Fs(\(PTL,)g([18)n(]\))g(uses)h(a)g(similar)g Fj(sigstack)p Fs(\(2\))d(approach)1992 282 y(as)f(its)i(f)o(allback)d(approach)f(for) i(thread)f(creation.)23 b(But)c(both)e(v)n(ari-)1992 382 y(ants)31 b(do)g(not)f(w)o(ork)h(on)f(some)h(System-V)-8 b(-deri)n(v)o(ed)28 b(platforms,)1992 482 y(for)19 b(reasons)h(we)g (will)h(discuss)g(later)-5 b(.)2116 582 y(It)21 b(is)h(clear)e(our)g (implementation)f(for)h Fj(mctx)p 3445 582 V 29 w(create)g Fs(has)1992 681 y(to)32 b(play)h(a)g(fe)n(w)f(tricks)h(to)g(use)g(a)g Fj(jmp)p 3176 681 V 29 w(buf)g Fs(for)f(passing)g(e)o(x-)1992 781 y(ecution)i(control)h(to)h(an)g(arbitrary)f(startup)g(routine.)71 b(And)36 b(it)1992 881 y(has)c(to)h(be)f(carefully)g(to)g(not)h(suf)n (fer)e(from)h(une)o(xpected)e(side-)1992 980 y(ef)n(fects.)54 d1019 3 a1021 3 1992 1080 y(e)o(xpect)e(to)i(\002nd)g(an)f(easy)h(solution)f(\(as)h (for)f Fj(mctx)p 3527 1080 V 30 w(save)g Fs(and)1992 1180 y Fj(mctx)p 2197 1180 V 29 w(restore)p Fs(\),)40 d1023 4 a1026 4 1279 y(stack)p Fs(\(3\))15 b(cannot)i(be)g(tri)n(vially)g(combined)f (to)h(achie)n(v)o(e)g(our)f(e)o(x-)1992 1379 y(pected)j(semantics)h (for)g Fj(mctx)p 2900 1379 V 29 w(create)p Fs(.)1992 1696 y Fu(3)143 b(Implementation)1992 1882 y Fs(As)23 d1028 5 a1032 5 e(con-)1992 1982 y(tains)35 b(an)g(easy)h(part)f(\()p Fj(mctx)p 2880 1982 V 29 w(save)g Fs(and)g Fj(mctx)p 3500 1982 V 29 w(restore)p Fs(\))1992 2082 y(and)30 b(a)i(dif)n (\002cult)f(part)g(\()p Fj(mctx)p 2899 2082 V 29 w(create)p Fs(\).)57 b(Let)32 b(us)g(start)f(with)1992 2181 y(the)20 d1034 6 a1039 6 2151 y Fp(5)p Black Black 1992 2359 a Ff(typedef)43 b(struct)h Fh(mctx_st)g Fg(f)2171 2450 y Fh(jmp_buf)f(jb;)1992 2541 y Fg(g)i Fh(mctx_t;)1992 2724 y Ff(#define)e Fh(mctx_save\(mctx\))2171 2815 y(\(setjmp\(mctx->jb\)\))1992 2998 y Ff(#define)g Fh(mctx_restore\(mctx\))2171 3089 y(\(longjmp\(mctx->jb,)f(TRUE\)\)) 1992 3272 y(mctx)p 2176 3272 23 4 v 26 w(t)i(*)1992 3363 d1041 7 a1047 7 Fh(\),)2529 3454 y Ff(void)h Fh(*arg,)2529 3546 y Ff(void)g Fh(*skaddr,)2529 3637 y Ff(size_t)f Fh(sksize)223 b(\))1992 3728 y Fg(f)2171 3820 y Fh(mctx)p 2355 3820 V 26 w(t)45 b(*mctx;)2190 4002 y Fe(...to)17 b(be)j(\002lled)e(in...)2171 4094 y Ff(return)44 b Fh(mctx;)1992 4185 y Fg(g)1992 4370 y Fs(There)29 b(is)i(only)f(one)f(subtle)h(b)n(ut)h(important)d (point)i(we)h(should)1992 4470 y(mention:)42 b(The)29 d1049 11 a1059 15 4569 y(recti)n(v)o(e)d(to)i(implement)e Fj(mctx)p 2923 4569 25 4 v 29 w(save)h Fs(and)g Fj(mctx)p 3527 4569 V 29 w(restore)1992 4669 y Fs(is)j(not)g(indiscriminately)-5 b(.)52 b(F)o(or)30 b(technical)f(reasons)h(related)f(to)1992 4769 y Fj(setjmp)p Fs(\(3\))j(and)j Fj(return)f Fs(\(which)g(we)h(will) g(e)o(xplain)e(later)1992 4868 y(in)24 b(detail\))g(we)h Fr(cannot)f Fs(implement)f(these)i(tw)o(o)g(functions)e(as)i(C)1992 4968 y(functions)18 b(if)j(we)f(w)o(ant)h(to)f(achie)n(v)o(e)f(maximum) f(portability)-5 b(.)p Black 0 5021 764 4 v 90 5077 a Fv(5)120 5100 y Fo(All)17 b(error)h(checks)g(are)g(omitted)g(for)f (better)i(readability)l(.)p Black Black 0 5400 a Fm(P)n(or)q(table)11 b(Multithreading)0 5458 y Fl(The)h(Signal)g(Stac)o(k)e(T)-5 b(r)q(ic)o(k)12 b(F)o(or)f(User-Space)g(Thread)h(Creation)1929 5400 y Fs(4)1221 b Fl(Cop)o(yr)q(ight)3398 5399 y(c)3381 5400 y Fk(\015)11 b Fl(1999)g(Ralf)h(S)o(.)f(Engelschall)3524 d1062 34 a1095 29 5 4 bop Black Black 125 83 a Fs(Before)29 b(we)i(no)n(w)f(can)g (proceed)f(with)h(\002lling)h(in)f(the)h(gaps)0 183 y(of)19 b(the)g(implementation)e(sk)o(eleton)i(for)f Fj(mctx)p 1368 183 25 4 v 29 w(create)p Fs(,)h(let)h(us)0 282 y(\002rst)h (discuss)g(the)f(proposed)e(algorithm.)0 553 y Ft(3.1)121 b(Ov)m(er)t(vie)n(w)0 709 y Fs(The)26 b(general)f(idea)h(for)g Fj(mctx)p 921 709 V 29 w(create)p Fs(,)h(is)g(to)g(con\002gure)d(the)0 808 y(gi)n(v)o(en)18 b(stack)i(as)h(a)f(signal)g(stack)g(via)f Fj(sigaltstack)p Fs(\(2\),)e(send)0 908 y(the)37 b(current)f(process)h (a)g(signal)g(to)h(transfer)e(e)o(x)o(ecution)f(con-)0 1007 y(trol)d(onto)f(this)i(stack,)i(sa)n(v)o(e)d(the)g(machine)f (conte)o(xt)f(there)i(via)0 1107 y Fj(setjmp)p Fs(\(3\),)19 b(get)j(rid)f(of)g(the)g(signal)g(handler)f(scope)h(and)f(boot-)0 1207 y(strap)g(into)g(the)g(startup)g(routine.)125 1306 y(As)46 b(mentioned,)k(the)45 b(real)h(problem)d(in)j(this)g(approach)0 1406 y(comes)27 b(from)e(the)i(signal)g(handler)f(scope)g(which)h (implies)g(v)n(ar)n(-)0 1506 y(ious)g(restrictions)g(on)g(Unix)g (platforms.)45 b(As)28 b(we)f(will)h(see,)i(we)0 1605 y(ha)n(v)o(e)25 b(to)g(perform)e(a)j(fe)n(w)f(tricks)g(to)h(get)f(rid)g (of)g(it.)41 b(The)25 b(second)0 1705 y(main)30 b(problem)e(is:)45 b(ho)n(w)30 b(do)f(we)i(prepare)d(the)i(calling)f(of)h(the)0 1805 y(start)21 b(routine)e(without)g(actually)h(entering)f(it.)0 2075 y Ft(3.2)121 b(Algor)r(ithm)0 2231 y Fs(The)33 b(input)g(to)h(the) g Fj(mctx)p 808 2231 V 29 w(create)f Fs(function)f(is)i(the)g(thread)0 2330 y(startup)24 b(routine)g Fr(func)p Fs(,)h(the)g(thread)f(startup)g (ar)o(gument)f Fr(ar)m(g)i Fs(and)0 2430 y(a)g(chunk)e(of)i(memory)e (starting)h(at)i Fr(skaddr)g Fs(and)e(of)h(size)g Fr(sksize)p Fs(,)0 2530 y(which)20 b(should)f(become)g(the)h(threads)g(stack.)26 b(The)20 b(algorithm)e(of)0 2629 y Fj(mctx)p 205 2629 d1097 2 a1098 2 0 2729 y(algorithm)j(one)g(can)h(\002nd)g(in)h Fr(GNU)g(Pth)p Fs(:)p Black 104 2911 a(1.)p Black 41 w(Allocate)f(a)g(ne)n(w)g d1100 3 a1102 3 Black 104 3076 a(2.)p Black 41 w(Preserv)o(e)g(the)h(current)f(signal)h (mask)h(and)e(block)h(an)g(arbi-)208 3176 y(trary)f(w)o(ork)o(er)g (signal.)p Black 104 3342 a(3.)p Black 41 w(Preserv)o(e)33 d1104 4 a1107 4 3441 y(w)o(ork)o(er)14 b(signal)h(con\002gure)f(a)i(trampoline)d (function)h(as)i(the)208 3541 y(ne)n(w)k(temporary)e(signal)i(action.)p Black 104 3706 a(4.)p Black 41 w(Preserv)o(e)d(a)i(possibly)f(acti)n(v) o(e)g(alternate)g(signal)g(stack)h(and)208 3806 y(con\002gure)35 d1109 3 a1111 3 3906 y(signal)20 b(stack)g(of)g(length)f Fr(sksize)p Fs(.)p Black 104 4071 a(5.)p Black 41 w(Send)33 b(the)h(current)e (process)h(the)h(w)o(ork)o(er)f(signal,)j(tem-)208 4171 d1113 2 a1114 2 (to)g(be)g(deli)n(v-)208 4271 y(ered)24 b(on)g(the)h(signal)g(stack)g (in)f(order)g(to)h(transfer)f(e)o(x)o(ecu-)208 4370 y(tion)19 d1116 2 a1117 2 104 4536 a(6.)p Black 41 w(After)31 b(the)g(trampoline)f(function)g (asynchronously)e(en-)208 4635 y(tered,)18 b(sa)n(v)o(e)g(its)i d1119 5 a1123 5 4735 y(and)f(immediately)g(return)f(from)h(it)i(to)f(terminate)f(the)h (sig-)208 4835 y(nal)h(handler)e(scope.)p Black 104 5000 a(7.)p Black 41 w(Restore)i(the)h(preserv)o(ed)e(alternate)h(signal)g (stack)h(and)f(the)208 5100 y(preserv)o(ed)c(signal)j(action)f(and)g (mask)g(for)g(w)o(ork)o(er)f(signal.)p Black 2095 83 d1125 2 a1126 2 (xt)g(of)2199 183 y Fj(mctx)p 2404 183 V 29 w(create)p Fs(.)p Black 2095 389 a(9.)p Black 41 w(Restore)16 b(the)g(pre)n d1128 4 a1131 4 488 y(trampoline)k(function)h(\()p Fr(mctx)p Fs(\))h(to)g(again)f (transfer)g(e)o(x)o(ecu-)2199 588 y(tion)29 b(control)e(onto)i(the)g (alternate)f(stack,)j(b)n(ut)e(this)g(time)2199 688 y(without\(!\))23 b(signal)e(handler)d(scope.)p Black 2054 894 a(10.)p d1133 5 a1137 5 Fr(mctx)p Fs(\))2199 993 y(again,)47 b(immediately)41 b(bootstrap)h(into)g(a)h(clean)f(stack)2199 1093 y(frame)k(situation)h (by)f(calling)h(a)g(second)f(trampoline)2199 1193 y(function.)p Black 2054 1399 a(11.)p Black 40 w(Set)56 b(the)f(ne)n(w)f(signal)h (mask)g(to)g(be)g(the)g(same)g(as)2199 1498 y(the)37 d1139 2 a1140 2 1598 y Fj(mctx)p 2404 1598 V 29 w(create)20 b Fs(w)o(as)h(called.)p Black 2054 1804 a(12.)p Black 40 w(Load)50 b(the)i(startup)e d1142 1 a1142 1 b(from)2199 1904 y Fj(mctx)p 2404 1904 V 29 w(create)20 d1144 7 a1150 7 Black 2054 2110 a(13.)p Black 40 w(Restore)26 b(the)g(machine)f(conte)o (xt)g(of)h Fj(mctx)p 3486 2110 V 29 w(create)f Fs(to)2199 2210 y(transfer)19 b(e)o(x)o(ecution)f(control)h(back)h(to)g(it.)p Black 2054 2416 a(14.)p Black 40 w(Return)28 b(the)h(generated)d (machine)i(conte)o(xt)f Fr(mctx)i Fs(to)g(the)2199 2515 y(caller)-5 b(.)1992 2738 y(When)23 b(we)h(no)n(w)g(again)f(switch)h (into)f(the)h(established)g(machine)1992 2838 y(conte)o(xt)15 d1152 1 a1152 1 Fr(func)g Fs(with)1992 2937 y(ar)o(gument)22 b Fr(ar)m(g)p d1154 1 a1154 1 (the)1992 3037 y(algorithm:)2001 3343 y d1657 1 a1657 1 (issues)h(we)f(still)1992 4888 y(ha)n(v)o(e)19 b(to)h(disuss.)1992 d1828 11 a1838 10 (manually)h(resizes)h(the)0 1282 y(mapped)18 b(memory)g(re)o(gion.)23 b(Or)d(e)n(v)o(en)f(more)f(ele)o(gant,)h(as)h(Solaris)0 1381 y(and)31 b(FreeBSD)i(do)e(it:)49 b(the)32 b(scheduler)f(allocates) h(the)g(threads)0 1481 y(inside)24 b(shared)g(memory)e(se)o(gments)i (which)g(are)g(1)g(MB)h(in)g(\(vir)n(-)0 1581 y(tual\))h(size)h(and)f (just)h(relies)g(on)f(the)h(virtual)f(memory)e(system')-5 b(s)0 1680 y(feature)28 b(of)h(actually)f(mapping)f(only)h(the)h (actually)g(consumed)0 1780 y(stack)20 b(space.)0 1995 y Fq(Star)s(tup)k(Routine)g(T)-10 b(er)r(mination)0 2125 y Fs(When)27 b(you)g(ha)n(v)o(e)g(read)g(the)g(proposed)e d1868 2 a1869 2 Fj(sigaltstack)p Fs(\(2\).)0 4470 y(Instead)53 b(the)o(y)f(at)i(least)g (pro)o(vide)e(the)h(4.2BSD)g(anchestor)0 4569 y(function)40 d1892 14 a1905 13 (xists)j(which)e(plays)g(un-)1992 1366 y(f)o(air:)25 b(unfortunately)17 b(the)k(popular)d(GNU/Linux.)25 b(Although)18 b(we)1992 1466 y(will)h(disco)o(v)o(er)d(that)j(it)g(both)f(pro)o (vides)e Fj(sigaltstack)p Fs(\(2\))g(and)1992 1566 y Fj(sigstack)p Fs(\(2\),)27 b(our)h(approach)e(w)o(on')o(t)h(w)o(ork)h (on)g(Linux)f(k)o(er)n(-)1992 1665 y(nels)20 b(prior)f(to)i(v)o(ersion) d(2.2.)2116 1774 y(Why?)75 b(Because)37 b(its)h Fr(libc)f Fs(pro)o(vides)e(only)h(dummies)g(of)1992 1873 y(these)e(functions)g (which)g(al)o(w)o(ays)h(return)f(just)h Fj(-1)g Fs(with)g Fj(er-)1992 1973 y(rno)c Fs(set)i(to)f Fj(ENOSYS)p Fs(.)e(So,)35 b(this)d(de\002nitely)f(means)h(that)f(our)1992 2073 y(nifty)16 b(algorithm)f(is)j(useless)g(there,)f(because)f(its)i (central)e(point)h Fr(is)1992 2172 y Fj(sigaltstack)p d1907 6 a1912 6 b(we)h(do)1992 2272 y(not)17 b(need)g(to)h(gi)n(v)o(e)f(up,)g(of)h (course.)23 b(At)18 b(least)h(not)e(if)h(we,)g(for)f(a)h(sin-)1992 2372 y(gle)k(brain-dead)e(platform,)i(accept)g(to)h(break)f(our)f (general)h(goal)1992 2471 y(of)d(not)h(using)g(an)o(y)f(platform)g (dependent)f(things.)2116 2580 y(So,)k(what)f(can)h(we)g(actually)f(do) g(here?)28 b(All)23 b(we)f(ha)n(v)o(e)f(to)g(do,)1992 @ 1.2 log @*** empty log message *** @ text @d4 1 a4 1 %%Pages: 9 d13 1 a13 1 %DVIPSSource: TeX output 1999.08.18:1052 d376 1 a376 1 1[18 33 33 22 29 33 29 33 29 8[48 1[48 48 41 37 44 1[37 d378 1 a378 1 33 33 1[33 33 18 17 22 42[37 2[{TeXBase1Encoding ReEncodeFont}52 d421 1 a421 1 5[37 1[37 37 1[18 1[18 44[{TeXBase1Encoding ReEncodeFont}32 d428 2 a429 2 rf /FB 134[50 3[50 1[39 33 2[50 50 78 28 3[50 50 33 44 1[44 1[44 13[55 66 7[39 4[61 4[92 6[50 7[50 2[25 46[{ d449 1 a449 1 g(German)o(y)-5 b(.)1755 1282 y FB(July)24 b(1999)1157 d452 1 a452 1 1491 y(Document)g(V)-5 b(ersion)18 b(0.9.13)p Black Black d1881 1 a1881 1 b(on)g(the)g(\002rst)h(look.)2785 651 y Fp(6)1992 896 d1883 2 a1884 2 1026 y Fs(Finally)31 b(the)h(w)o(orld)f(w)o(ould)g(not)h(be)g(as)g (funn)o(y)e(as)j(it)f(is,)k(if)c(re-)1992 1126 y(ally)38 d1886 2 a1887 2 b(Instead,)1992 1226 y(currently)36 b(at)k(least)f(one)f(platform)f(e)o (xists)j(which)e(plays)g(un-)1992 1325 y(f)o(air:)25 d1889 2 a1890 2 b(we)1992 1425 y(will)h(disco)o(v)o(er)d(that)j(it)g(both)f(pro)o (vides)e Fj(sigaltstack)p Fs(\(2\))g(and)1992 1525 y d1892 3 a1894 3 (on)g(Linux)f(k)o(er)n(-)1992 1624 y(nels)20 b(prior)f(to)i(v)o(ersion) d(2.2.)2116 1727 y(Why?)75 b(Because)37 b(its)h Fr(libc)f Fs(pro)o(vides)e(only)h(dummies)g(of)1992 1827 y(these)e(functions)g d1896 2 a1897 2 Fj(er-)1992 1926 y(rno)c Fs(set)i(to)f Fj(ENOSYS)p Fs(.)e(So,)35 b(this)d(de\002nitely)f(means)h(that)f(our)1992 2026 d1899 1 a1899 1 (central)e(point)h Fr(is)1992 2125 y Fj(sigaltstack)p d1901 1 a1901 1 b(we)h(do)1992 2225 y(not)17 b(need)g(to)h(gi)n(v)o(e)f(up,)g(of)h d1903 3 a1905 3 2325 y(gle)k(brain-dead)e(platform,)i(accept)g(to)h(break)f(our)f (general)h(goal)1992 2424 y(of)d(not)h(using)g(an)o(y)f(platform)g (dependent)f(things.)2116 2527 y(So,)k(what)f(can)h(we)g(actually)f(do) d1907 2 a1908 2 2627 y(is)g(to)g(\002ddle)g(around)e(a)i(little)h(bit)f(with)g(the)g (machine-dependent)1992 2726 y Fj(jmp)p 2147 2726 V 29 d1910 1 a1910 1 Fj(setjmp.h)1992 2826 y Fs(or)20 b(by)h(disassembling)f d1912 4 a1915 4 2926 y(ally)k(one)f(just)i(has)g(do)e(a)i Fj(setjmp)p Fs(\(3\))e(to)h(get)g(an)g(initial)h(state)1992 3025 y(in)e(the)g Fj(jmp)p 2370 3025 V 29 w(buf)g Fs(structure)f(and)h(then) g(manually)e(adjust)i(tw)o(o)1992 3125 y(of)g(its)h(ingredients:)40 d1917 2 a1918 2 3224 y(ture)17 b(member)g(with)h(\223)p Fj(pc)p Fs(\224)g(in)g(the)g (name\))f(and)h(the)g(stack)g(pointer)1992 3324 y(\(usually)h(a)h d1920 2 a1921 2 (name\).)2116 3427 y(That')-5 b(s)26 b(all)g(and)f(can)g(be)h (acceptable)f(for)g(a)h(real-w)o(orld)e(im-)1992 3526 d1923 1 a1923 1 (platforms)e(\226)1992 3626 y(at)g(least)g(as)h(long)e(as)i(the)f d1925 2 a1926 2 3726 y(one)e(or)h(tw)o(o)g(platforms.)29 b(But)23 b(one)e(has)i(to)f(k) o(eep)f(in)h(mind)g(that)g(it)1992 3825 y(at)c(least)g(breaks)g(one)f d1928 7 a1934 7 3925 y(as)i(a)h(last)g(chance)e(solution,)g(of)h(course.)1992 4140 y Fq(More)j(Machine)h(Conte)n(xt)e(Ing)o(redients)1992 4270 y Fs(Finally)39 b(for)g(a)g(real-w)o(orld)f(threading)g (implementation)f(one)1992 4370 y(usually)d(w)o(ant)h(to)h(put)f(more)f (ingredients)g(into)h(the)g(machine)1992 4470 y(state)47 b Fn(mctx)p 2372 4470 V 29 w(t)h Fs(structure,)53 b(of)46 b(course.)106 b(F)o(or)46 b(instance)h(one)1992 4569 d1936 12 a1947 13 (to)g(ful\002ll)1992 4669 y(the)d(POSIX.1c)h(signal)f(handling)f (requirements)g(for)h(threads.)1992 4769 y(When)d Fj(sigsetjmp)p Fs(\(3\))f(and)h Fj(siglongjmp)p Fs(\(2\))f(e)o(xists)i(one)1992 4868 y(can)15 b(just)i(use)f(them)f(instead)h(of)f Fj(setjmp)p Fs(\(3\))f(and)i Fj(longjmp)p Fs(\(2\))1992 4968 y(to)k(achie)n(v)o(e)f (this.)p Black 0 5021 764 4 v 90 5077 a Fv(6)120 5100 y Fo(See)e(the)h(\002le)f Fd(aclocal.m4)e Fo(in)j(the)f Fc(GNU)g(Pth)g Fo(distrib)o(ution)j(for)d(details)p Black Black 0 5400 a Fm(P)n(or)q(table)11 b(Multithreading)0 5458 y Fl(The)h(Signal)g(Stac)o(k)e(T)-5 b(r)q(ic)o(k)12 b(F)o(or)f(User-Space)g(Thread)h(Creation)1929 5400 y Fs(8)1221 b Fl(Cop)o(yr)q(ight)3398 5399 y(c)3381 5400 y Fk(\015)11 b Fl(1999)g(Ralf)h(S)o(.)f(Engelschall)3524 d1950 81 a2030 69 9 8 bop Black Black 125 83 a Fs(If)35 b(not,)j(one)d(can)g(easily)g (emulate)g(them)g(by)g(sa)n(ving)g(and)0 183 y(restoring)k(the)i (thread)f(signal)g(masks)h(manually)f(via)g Fj(sig-)0 282 y(procmask)p Fs(\(2\))26 b(in)i Fj(mctx)p 822 282 25 4 v 30 w(switch)p Fs(.)48 b(One)28 b(just)h(has)f(to)g(be)h(a)0 382 y(little)21 b(bit)f(carefully)f(to)i(pre)n(v)o(ent)d(race)i (conditions.)125 482 y(Additionally)25 b(it)i(is)h(reasonable)d(to)i (also)g(sa)n(v)o(e)g(and)g(restore)0 581 y(the)15 b(global)g Fj(errno)g Fs(v)n(ariable.)22 b(F)o(or)15 b(a)h(sophisticated)f (implemen-)0 681 y(tation)24 b(which)g(does)g(also)h(all)g(those)f (additional)f(things,)i(ha)n(v)o(e)f(a)0 780 y(look)c(at)i(the)f (source)g(\002le)g Fj(pth)p 895 780 V 30 w(mctx.c)f Fs(in)i(the)f Fr(GNU)h(Pth)f Fs(dis-)0 880 y(trib)n(ution)e([19)o(].)0 1143 y Ft(3.5)121 b(Summar)t(y)0 1299 y Fs(W)-7 b(e)26 b(ha)n(v)o(e)e(presented)g(a)h(pragmatic)f(and)g(mostly)h(portable)e(f) o(all-)0 1399 y(back)i(approach)e(for)i(implementing)e(user)n(-space)i (multithread-)0 1498 y(ing,)20 b(based)g(entirely)g(on)g(Unix)h(system) f(and)g(ANSI)h(C)h(language)0 1598 y(f)o(acilities.)44 b(The)26 b(approach)f(w)o(as)i(successfully)f(tested)g(in)h(prac-)0 1698 y(tice)34 b(on)e(a)i(wide)f(range)f(of)h(Unix)g(\003a)n(v)n(ors)h (by)e Fr(GNU)j(Pth)e Fs(and)0 1797 y(should)17 b(also)i(adapt)f(to)g (the)g(remaining)f(Unix)h(platforms)f(as)i(long)0 1897 y(as)j(the)o(y)e(beha)n(v)o(e)f(mostly)i(identical)f(to)h(the)g (current)e(major)h(Unix)0 1996 y(platforms.)0 2260 y Ft(3.6)121 b(Ac)n(kno)n(wledgements)0 2416 y Fs(I)31 b(w)o(ould)f(lik)o(e)h(to)f(thank)i(M)t Fo(A)t(RT)t(I)t(N)j Fs(K)t Fo(R)t(A)t(E)t(M)t(E)t(R)r Fs(,)h(R)q Fo(O)t(B)t(E)t(RT)g Fs(S)t(.)2 2515 y(T)t Fo(H)t(AU)25 b Fs(and)e(D)q Fo(A)-5 b(V)t(I)t(D)27 b Fs(B)s Fo(U)t(T)t(E)t(N)t(H)t(O)t(F)d Fs(for)d(their)h(comments)f(which)0 2615 y(helped)f(to)i(impro)o(v)o(e) e(this)i(paper)-5 b(.)28 b(Additional)20 b(thanks)h(go)g(to)h(the)0 2714 y(users)j(of)f Fr(GNU)h(Pth)g Fs(for)e(their)i(feedback)d(on)i (the)h(implementa-)0 2814 y(tion)30 b(which)f(helped)g(a)i(lot)f(in)h (\002ne-tuning)d(the)i(presented)e(ap-)0 2914 y(proach.)1500 b([)p Fb(rse)p Fs(])0 3207 y Fu(Ref)o(erences)p Black 71 3376 a FA([1])p Black 43 w(M)t(.)t(E)t(.)50 b(C)t Fa(O)t(N)t(W)m(A)n(Y)r FA(:)81 b Fw(Design)48 b(of)g(a)h(separ)o(able)g (tr)o(ansition-)199 3467 y(dia)o(gr)o(am)20 b(compiler)-8 b(.)p FA(,)19 b(Comm.)f(A)m(CM)h(6:7,)g(1963,)h(p.396-408)p Black 71 3575 a([2])p Black 43 w(E)t(.)t(W)m(.)26 b(D)t Fa(I)t(J)t(K)t(S)t(T)t(R)t(A)r FA(:)31 b Fw(Co-oper)o(ating)26 b(sequential)f(pr)m(ocesses)p FA(,)h(in)199 3666 y(F)-6 b(.)29 b(Genuys)j(\(Ed.\),)g Fw(Pr)m(o)o(gr)o(amming)f(Langua)o(g)o(es) p FA(,)k(N)m(A)-8 b(T)o(O)29 b(Ad-)199 3758 y(v)n(anced)f(Study)f (Institute,)g(Academic)h(Press,)f(London,)i(1965,)199 3849 y(p.42-112.)p Black 71 3957 a([3])p Black 43 w(B)t(.)18 b(N)t Fa(I)t(C)t(H)t(O)t(L)t(S)t FA(,)f(D)t(.)g(B)s Fa(U)t(T)t(T)t(L)t (A)t(R)t FA(,)f(J)t(.)t(P)l(.)g(F)o Fa(A)t(R)t(R)t(E)t(L)r FA(:)i Fw(Pthr)m(eads)e(Pr)m(o-)199 4048 y(gr)o(amming)26 b(-)e(A)h(POSIX)f(Standar)m(d)j(for)e(Better)f(Multipr)m(ocess-)199 4140 y(ing)p FA(,)19 b(O'Reilly)f(1996;)i(ISBN)e(1-56592-115-1)p Black 71 4248 a([4])p Black 43 w(B)t(.)30 b(L)t Fa(E)t(W)t(I)t(S)t FA(,)g(D)t(.)g(J)t(.)g(B)t Fa(E)t(R)t(G)r FA(:)37 b Fw(Thr)m(eads)28 b(Primer)e(-)h(A)f(Guide)h(T)-7 b(o)199 4339 y(Multithr)m(eaded)28 b(Pr)m(o)o(gr)o(amming)p FA(,)h(Prentice)e(Hall)f(1996;)31 b(ISBN)199 4430 y(0-13-443698-9)p Black 71 4538 a([5])p Black 43 w(S)t(.)23 b(J)t(.)g(N)t Fa(O)t(RT)s(O)t(N)t FA(,)e(M)t(.)j(D)t(.)e(D)t Fa(I)t(P)o(A)t(S)t(Q)s(U)r(A)t(L)t(E)r FA(:)g Fw(Thr)m(ead)f(T)l(ime)e(-)g(The)199 4630 y(Multithr)m(eaded)27 b(Pr)m(o)o(gr)o(amming)f(Guide)p FA(,)g(Prentice)f(Hall)f(1997;)199 4721 y(ISBN)18 b(0-13-190067-6)p Black 71 4829 a([6])p Black 43 w(D)t(.)29 b(R)t(.)f(B)s Fa(U)t(T)t(E)t(N)t(H)t(O)t(F)r FA(:)34 b Fw(Pr)m(o)o(gr)o(amming)27 b(with)e(POSIX)g(Thr)m(eads)p FA(,)199 4920 y(Addison)20 b(W)-6 b(esle)o(y)19 b(1997;)h(ISBN)e (0-201-63392-2)p Black 71 5028 a([7])p Black 43 w(W)m(.)t(R)t(.)t(S)t Fa(T)t(E)t(V)t(E)t(N)t(S)r FA(:)f Fw(Advanced)j(Pr)m(o)o(gr)o(amming)g (in)e(the)h(Unix)g(En-)199 5119 y(vir)m(onment)p FA(,)g(Addison-W)-6 b(esle)o(y)h(,)20 b(1992;)g(ISBN)e(0-201-56317-7)p Black 2062 83 a([8])p Black 44 w(D)t(.)46 b(L)t Fa(E)t(W)t(I)t(N)t(E)r d2032 3 a2034 3 b(Writing)2191 174 y(P)-6 b(ortable)40 b(Unix)h(Pr)m(o)o(gr)o(ams)p FA(,)k(O'Reilly)40 b(&)g(Associates,Inc.,)2191 266 y(1994;)20 b(ISBN)d(0-937175-73-0)p Black 2062 391 a([9])p Black d2037 3 a2039 3 482 y(questions)196 b(for)g(comp.os.r)m(esear)m(c)o(h)p FA(,)242 b(1995;)2191 574 y(http://www)-5 b(.serpentine.com/)p Fh(\230)p FA(bos/os-f)o(aq/,)2191 665 y d2041 1 a2041 1 2025 790 a([10])p Black 44 w(S)t Fa(U)t(N)68 b FA(M)t d2044 3 a2046 3 881 y(Ask)o(ed)25 b(Questions)p FA(,)h(1995,)g(http://www.sun.com/w)o (orkshop/-)2191 973 y(threads/f)o(aq.html)p Black 2025 1098 a([11])p Black 44 w(B)t Fa(RY)m(A)t(N)84 b FA(O)t(')t(S)t d2048 2 a2049 2 b(ask)o(ed)g(ques-)2191 1189 y(tions)144 b(for)g(comp.pr)m(o)o(gr)o (amming)o(.thr)m(eads)p FA(,)178 b(1997;)2191 1281 y(http://www)-5 d2051 1 a2051 1 Black 2025 1406 a([12])p Black 44 w(B)t Fa(I)t(L)147 d2053 2 a2054 2 b(ask)o(ed)h(ques-)2191 1497 y(tions)e(for)g(comp.pr)m(o)o(gr)o(amming) o(.thr)m(eads)p FA(,)178 b(1999;)2191 1588 y(http://www)-5 d2056 1 a2056 1 1714 a([13])p Black 44 w(N)t Fa(U)t(M)t(E)t(R)t(I)t(C)62 d2058 1 a2058 1 b Fw(Multithr)m(eading)63 b(-)f(De\002ni-)2191 1805 y(tions)k(and)i d2060 1 a2060 1 1896 y(quest.com/lang/multi-frame.html)p Black 2025 2021 d2063 2 a2064 2 (Speci\002cation,)2191 2113 y(V)-8 b(er)o(sion)51 b(2)g(-)g(Thr)m(eads) p FA(;)68 b(1997;)g(http://www.opengroup-)2191 2204 y(.or)o d2066 1 a2066 1 2329 a([15])p Black 44 w(S)t Fa(U)t(N)46 b FA(M)t Fa(I)t(C)t(R)r(O)t(S) d2068 1 a2068 1 Fw(SMI)44 b(Thr)m(ead)h(Resour)m(ces)p FA(;)2191 2421 d2070 1 a2070 1 2025 2546 a([16])p Black 44 w(F)t Fa(R)t(A)t(N)t(K)33 d2072 1 a2072 1 b(pthr)m(eads)p FA(;)39 b(1997;)g(http://www-)2191 2637 d2074 1 a2074 1 2025 2762 a([17])p Black 44 w(C)t Fa(H)t(R)t(I)t(S)90 d2076 1 a2076 1 b Fw(MIT)88 b(pthr)m(eads)p FA(;)124 b(1993;)2191 2854 d2078 2 a2079 2 2945 y(\(old\),)242 b(http://www.humanf)o(actor.com/pthreads/mit-)2191 3036 y(pthreads.html)19 b(\(updated\))p Black 2025 3162 d2082 2 a2083 2 FA(\(PTL\);)e(1999;)2191 3253 y(http://www.media.osaka-cu.ac.jp/)p Fh(\230)p FA(k-abe/PTL/)p Black 2025 3378 a([19])p Black d2086 3 a2088 3 FA(\(Pth\);)2191 3469 y(1999;)72 b(http://www.gnu.or)o(g/softw)o (are/pth/,)63 b(ftp://ftp.gnu-)2191 3561 y(.or)o(g/gnu/pth/)p Black 2025 3686 a([20])p Black 44 w(M)t Fa(I)t(C)t(H)t(A)t(E)t(L)40 d2090 1 a2090 1 b Fw(POSIX)39 b(and)h(DCE)f(Thr)m(eads)2191 3777 y(F)-8 d2092 2 a2093 2 2191 3869 y(.net/)p Fh(\230)p FA(mtp/PCthreads.html)p Black 2025 3994 a([21])p Black 44 w(R)q Fa(O)t(B)t(E)t(RT)31 d2095 17 a2111 10 b(Simple)h(Thr)m(eads)g FA(\(rsthreads\);)2191 4085 y(1996;)20 b(ftp://ftp.ai.mit.edu/pub/rst/)p Black 2025 4210 a([22])p Black 44 w(J)t Fa(O)t(H)t(N)100 b FA(B)t Fa(I)t(R)t(R)t(E)t(L)t(L)r FA(:)175 b Fw(F)l(r)m(eeBSD)97 b(uthr)m(eads)p FA(;)138 b(1998;)2191 4302 y(ftp://ftp.freebsd.or)o(g/pub/FreeBSD/FreeBSD-)2191 4393 y(current/src/lib/libc)p 2747 4393 23 4 v 27 w(r/uthread/)p Black 2025 4518 a([23])p Black 44 w(X)t Fa(A)l(V)t(I)t(E)t(R)50 b FA(L)t Fa(E)t(R)r(O)q(Y)r FA(:)81 b Fw(The)49 b(LinuxThr)m(eads)i (libr)o(ary)p FA(;)64 b(1999;)2191 4609 y(http://pauillac.inria.fr/)p Fh(\230)p FA(xlero)o(y/linuxthreads/)p Black 2025 4735 d2114 10 a2123 10 2191 4826 y(Writing)63 b(and)h(Deb)o(ug)o(ging)g(Pr)m(o)o(gr)o(ams;)85 b(Under)o(standing)2191 4917 y(Thr)m(eads)p FA(;)58 b(1998;)g (http://www.rs6000.ibm.com/doc)p 3714 4917 V 28 w(link/-)2191 5009 y(en)p 2265 5009 V 27 w(US/a)p 2442 5009 V 26 w(doc)p 2575 5009 V 28 w(lib/aixpr)o(ggd/genprogc/understanding-)p 2191 5100 V 2213 5100 a(threads.htm)p Black 0 5400 a Fm(P)n(or)q(table)11 b(Multithreading)0 5458 y Fl(The)h(Signal)g(Stac)o (k)e(T)-5 b(r)q(ic)o(k)12 b(F)o(or)f(User-Space)g(Thread)h(Creation) 1929 5400 y Fs(9)1221 b Fl(Cop)o(yr)q(ight)3398 5399 y(c)3381 5400 y Fk(\015)11 b Fl(1999)g(Ralf)h(S)o(.)f(Engelschall)3524 @ 1.1 log @*** empty log message *** @ text @d13 1 a13 1 %DVIPSSource: TeX output 1999.08.17:1012 d395 4 a398 4 rf /Ft 134[60 60 86 1[66 33 60 40 2[66 66 100 27 60 1[27 66 66 33 66 66 60 66 66 12[73 80 86 1[80 2[100 3[33 1[93 1[80 86 86 1[80 10[66 66 66 66 66 66 2[33 46[{ TeXBase1Encoding ReEncodeFont}39 119.552 /Helvetica rf d421 1 a421 1 6[37 37 37 1[18 1[18 44[{TeXBase1Encoding ReEncodeFont}32 d452 1 a452 1 1491 y(Document)g(V)-5 b(ersion)18 b(0.9.12)p Black Black d536 42 a577 42 1183 y(and)21 b(dispatching)g(of)g(the)h(indi)n(vidual)e(threads.)30 b(This)22 b(mean)f(that)0 1283 y(the)j(number)f(of)h(platforms)e(the)o (y)i(are)g(w)o(orking)f(on)h(are)g(usually)0 1383 y(a)h(\002x)o(ed)f (set)h(and)f(applications)f(which)h(are)g(based)g(on)g(them)g(are)0 1482 y(only)c(portable)f(to)h(those)h(platforms.)j(This)d(situation)f (is)h(not)g(sat-)0 1582 y(isf)o(actory)-5 b(,)23 b(so)h(application)f (authors)f(still)j(a)n(v)n(oid)f(the)g(use)g(multi-)0 1682 y(threading)j(when)g(the)o(y)h(w)o(ant)h(to)g(\(or)e(ha)n(v)o(e)h (to\))h(achie)n(v)o(e)e(maxi-)0 1781 y(mum)19 b(portability)g(for)h (their)g(application.)125 1885 y(So)f(a)g(pragmatic)e(b)n(ut)i(mostly)f (portable)g(f)o(allback)g(technique)0 1985 y(for)i(implementing)e(user) n(-space)i(threads)g(w)o(ould)f(be)i(useful,)e(be-)0 2084 y(cause)29 b(this)h(w)o(ay)g(the)f(possibilities)h(to)f(use)h (multithreading)d(in)0 2184 y(free)20 b(softw)o(are)g(applications)f (could)g(be)h(greatly)f(impro)o(v)o(ed.)0 2406 y Fq(Ing)o(redients)24 b(Of)f(A)g(Thread)0 2539 y Fs(A)29 b(Unix)g(process)f(has)h(man)o(y)f (ingredients,)h(b)n(ut)g(the)g(most)g(im-)0 2639 y(portant)17 b(ones)h(are)f(its)i(memory)e(mapping)f(table,)i(the)g(signal)g(dis-)0 2738 y(patch)h(table,)g(the)g(signal)g(mask,)g(the)g(set)h(of)f(\002le) h(descriptors)e(and)0 2838 y(the)j(machine)f(conte)o(xt,)f(which)i(in)g (turn)f(consists)h(of)g(at)g(least)h(the)0 2938 y(CPU)17 b(re)o(gisters)e(including)f(the)h(program)f(counter)g(and)h(the)h (stack)0 3037 y(pointer)-5 b(.)26 b(On)20 b(the)h(other)f(hand,)g (there)g(can)g(be)h(light-weight)e(pro-)0 3137 y(cesses)24 b(\(L)-6 b(WP\))23 b(or)g(threads)g(which)f(share)h(all)h(ingredients)e (with)0 3237 y(the)f(underlying)d(\(hea)n(vy-weight\))g(process)i(e)o (xcept)g(for)h(the)f(ma-)0 3336 y(chine)g(conte)o(xt.)0 3558 y Fq(K)m(er)r(nel-Space)k(vs)o(.)j(User-Space)0 3691 y Fs(Those)k(L)-6 b(WPs)32 b(or)f(threads,)i(on)e(a)g(Unix)g (platform)f(classically)0 3791 y(can)k(be)g(implemented)e(either)h (inside)h(k)o(ernel-space)e(or)i(user)n(-)0 3891 y(space.)53 b(When)30 b(implemented)e(in)h(k)o(ernel-space,)i(one)e(usually)0 3990 y(calls)22 b(them)e(L)-6 b(WPs,)22 b(else)f(\(user)n(-space\))f (threads.)26 b(If)21 b(threads)f(are)0 4090 y(implemented)29 b(by)i(the)g(k)o(ernel,)i(the)e(thread)g(conte)o(xt)f(switches)0 4189 y(are)17 b(performed)e(by)i(the)h(k)o(ernel)e(without)h(notice)g (by)g(the)g(applica-)0 4289 y(tion,)h(similar)g(to)h(the)f(dispatching) e(of)i(processes.)24 b(If)18 b(threads)f(are)0 4389 y(implemented)31 b(in)i(user)n(-space,)i(the)d(thread)g(conte)o(xt)g(switches)0 4488 y(are)e(performed)e(usually)i(by)g(an)g(application)f(library)g (without)0 4588 y(notice)38 b(by)g(the)h(k)o(ernel.)79 b(Additionally)-5 b(,)41 b(there)d(e)o(xist)h(hybrid)0 d934 1 a934 1 5100 y(sion)42 b(2)g(\(SUSv2)g([14)o(]\))g(and)f(its)i(anchestor)e d975 21 a995 23 (e)0 3224 y(at)71 b(least)f(solv)o(ed)g(the)g(dispatching)e(problem,)81 b(because)0 3324 y(our)39 b Fj(mctx)p 356 3324 V 29 w(save)h Fs(and)f Fj(mctx)p 985 3324 V 29 w(restore)g Fs(functions)f(were)0 3423 y(modeled)65 b(in)h(wise)h(prediction)e(after)h Fj(setjmp)p Fs(\(3\))f(and)0 3523 y Fj(longjmp)p Fs(\(3\).)0 3738 y Fq(The)23 b(Dif\002cult)g(P)m(ar)s(t)0 3869 y Fs(Ne)n(v)o(ertheless)32 b(the)h(dif)n(\002cult)g(problem)e(of)i(ho)n (w)g(to)g(create)g(the)0 3968 y(machine)j(conte)o(xt)h(remains.)76 b(Ev)o(en)36 b(the)i(f)o(act)f(that)h(we)g(no)n(w)0 4068 y(kno)n(w)-5 b(,)26 b(that)h(our)e(machine)h(conte)o(xt)f(is)i Fj(jmp)p 1331 4068 V 29 w(buf)g Fs(based,)g(pro-)0 4168 y(vides)17 b(no)g(adv)n(antage)f(to)i(us.)24 b(A)18 b Fj(jmp)p 1082 4168 V 29 w(buf)g Fs(has)f(to)h(be)g(treated)e(by)0 4267 y(us)25 b(as)g(an)f(opaque)e(data)i(structure)g(\227)g(for)g (portability)f(reasons.)0 4367 y(The)17 b(only)f(operations)g(we)h(can) g(perform)e(on)i(it,)h(are)f Fj(setjmp)p Fs(\(3\))0 4467 y(and)28 b Fj(longjmp)p Fs(\(3\))f(calls,)k(of)d(course.)50 b(Additionally)-5 b(,)28 b(we)h(are)0 4566 y(forced)21 b(to)i(use)g Fj(sigaltstack)p Fs(\(3\))c(for)j(our)g(stack)h(manipula-) 0 4666 y(tions,)e(because)g(it)g(is)h(the)g(only)e(portable)g(function) f(which)i(actu-)0 4765 y(ally)f(deals)h(with)f(stacks.)125 4868 y(It)32 b(should)g(be)g(noted,)j(that)d(other)g(thread)f(packages) h(ha)n(v)o(e)0 4968 y(tried)48 b(similar)g(approaches.)107 d997 40 a1036 41 Fs(S)t(.)1994 83 y(T)t Fo(H)t(AU)r Fs(')-5 b(s)40 b Fr(Really)e(Simple) g(Thr)m(eads)g Fs(\(rsthreads,)k([21)o(]\))d(pack-)1992 183 y(age)c(uses)i Fj(sigaltstack)p Fs(\(2\))c(in)j(a)h(v)o(ery)e (similar)h(w)o(ay)g(for)1992 282 y(thread)28 b(creation.)54 b(And)31 b(K)r Fo(O)q(T)n(A)j Fs(A)t Fo(B)t(E)r Fs(')-5 b(s)31 b Fr(P)-7 b(ortable)30 b(Thr)m(ead)f(Li-)1992 382 y(br)o(ary)16 b Fs(\(PTL,)h([18)o(]\))f(uses)i(a)f(some)n(what)f (similar)i Fj(sigstack)p Fs(\(2\))1992 482 y(approach)g(as)j(its)g(f)o (allback)f(approach)e(for)i(thread)f(creation.)25 b(But)1992 581 y(both)15 b(v)n(ariants)g(do)h(not)g(w)o(ork)f(on)h(some)g (System-V)-8 b(-deri)n(v)o(ed)13 b(plat-)1992 681 y(forms,)19 b(for)g(reasons)h(we)h(will)f(discuss)h(later)-5 b(.)2116 781 y(So,)86 b(it)74 b(is)g(clear)m(,)86 b(that)73 b(our)f (implementation)f(for)1992 881 y Fj(mctx)p 2197 881 V 29 w(create)16 b Fs(has)i(to)f(play)g(a)g(fe)n(w)h(tricks)f(to)g(use)h (a)f Fj(jmp)p 3726 881 V 30 w(buf)1992 980 y Fs(for)25 b(passing)h(e)o(x)o(ecution)e(control)h(to)h(an)g(arbitrary)f(startup)h (rou-)1992 1080 y(tine.)61 b(And)32 b(it)h(has)f(to)h(be)f(carefully)f (to)i(not)f(suf)n(fer)f(from)g(un-)1992 1180 y(e)o(xpected)37 b(side-ef)n(fects.)79 b(It)40 b(should)d(be)i(also)g(ob)o(vious,)i (that)1992 1279 y(we)31 b(cannot)f(again)f(e)o(xpect)h(to)h(\002nd)g (an)g(easy)g(solution)f(\(as)h(for)1992 1379 y Fj(mctx)p 2197 1379 V 29 w(save)18 b Fs(and)f Fj(mctx)p 2782 1379 V 29 w(restore)p Fs(\),)h(because)f Fj(setjmp)p Fs(\(3\))1992 1478 y(and)30 b Fj(sigaltstack)p Fs(\(3\))f(cannot)h(be)h(tri)n(vially) g(combined)e(to)1992 1578 y(achie)n(v)o(e)18 b(our)i(e)o(xpected)e (semantics)j(for)e Fj(mctx)p 3379 1578 V 29 w(create)p Fs(.)1992 1895 y Fu(3)143 b(Implementation)1992 2082 y Fs(As)23 b(we)g(ha)n(v)o(e)f(already)g(disco)o(v)o(ered,)f(our)h (implementation)e(con-)1992 2181 y(tains)35 b(an)g(easy)h(part)f(\()p Fj(mctx)p 2880 2181 V 29 w(save)g Fs(and)g Fj(mctx)p 3500 2181 V 29 w(restore)p Fs(\))1992 2281 y(and)30 b(a)i(dif)n (\002cult)f(part)g(\()p Fj(mctx)p 2899 2281 V 29 w(create)p Fs(\).)57 b(Let)32 b(us)g(start)f(with)1992 2380 y(the)20 b(easy)g(part,)g(which)f(is)i(ob)o(vious)e(ho)n(w)g(to)i(implement:) 3686 2350 y Fp(5)p Black Black 1992 2558 a Ff(typedef)43 b(struct)h Fh(mctx_st)g Fg(f)2171 2649 y Fh(jmp_buf)f(jb;)1992 2740 y Fg(g)i Fh(mctx_t;)1992 2923 y Ff(#define)e Fh(mctx_save\(mctx\)) 2171 3014 y(\(setjmp\(mctx->jb\)\))1992 3197 y Ff(#define)g Fh(mctx_restore\(mctx\))2171 3288 y(\(longjmp\(mctx->jb,)f(TRUE\)\)) 1992 3471 y(mctx)p 2176 3471 23 4 v 26 w(t)i(*)1992 3562 d1038 7 a1044 7 Fh(\),)2529 3654 y Ff(void)h Fh(*arg,)2529 3745 y Ff(void)g Fh(*skaddr,)2529 3836 y Ff(size_t)f Fh(sksize)223 b(\))1992 3928 y Fg(f)2171 4019 y Fh(mctx)p 2355 4019 V 26 w(t)45 b(*mctx;)2190 4202 y Fe(...to)17 b(be)j(\002lled)e(in...)2171 4293 y Ff(return)44 b Fh(mctx;)1992 4384 y Fg(g)1992 4569 y Fs(There)29 b(is)i(only)f(one)f(subtle)h(b)n(ut)h(important)d (point)i(we)h(should)1992 4669 y(mention:)42 b(The)29 d1046 16 a1061 13 4769 y(recti)n(v)o(e)d(to)i(implement)e Fj(mctx)p 2923 4769 25 4 v 29 w(save)h Fs(and)g Fj(mctx)p 3527 4769 V 29 w(restore)1992 4868 y Fs(is)47 b(not)g(at)g(pleasure.)104 b(F)o(or)46 b(technical)g(reasons)g(related)g(to)1992 4968 y Fj(setjmp)p Fs(\(3\))32 b(and)j Fj(return)f Fs(\(which)g(we)h (will)g(e)o(xplain)e(later)p Black 0 5021 764 4 v 90 5077 a Fv(5)120 5100 y Fo(All)17 b(error)h(checks)g(are)g(omitted)g (for)f(better)i(readability)l(.)p Black Black 0 5400 a Fm(P)n(or)q(table)11 b(Multithreading)0 5458 y Fl(The)h(Signal)g (Stac)o(k)e(T)-5 b(r)q(ic)o(k)12 b(F)o(or)f(User-Space)g(Thread)h (Creation)1929 5400 y Fs(4)1221 b Fl(Cop)o(yr)q(ight)3398 5399 y(c)3381 5400 y Fk(\015)11 b Fl(1999)g(Ralf)h(S)o(.)f(Engelschall) 3524 5458 y(All)g(Rights)h(Reser)q(v)o(ed.)p Black eop d1063 9 a1071 12 5 4 bop Black Black 0 83 a Fs(in)25 b(detail\))f(we)g Fr(cannot)h Fs(implement)e(these)h(tw)o(o)h(functions)e(as)i(C)0 183 y(functions)19 b(if)h(we)h(w)o(ant)f(to)g(achie)n(v)o(e)f(maximum)g (portability)-5 b(.)125 284 y(Before)29 b(we)i(no)n(w)f(can)g(proceed)f (with)h(\002lling)h(in)f(the)h(gaps)0 383 y(of)19 b(the)g (implementation)e(sk)o(eleton)i(for)f Fj(mctx)p 1368 383 25 4 v 29 w(create)p Fs(,)h(let)h(us)0 483 y(\002rst)h(discuss)g (the)f(proposed)e(algorithm.)0 762 y Ft(3.1)121 b(Gener)o(al)34 b(Idea)0 921 y Fs(The)26 b(general)f(idea)h(for)g Fj(mctx)p 921 921 V 29 w(create)p Fs(,)h(is)g(to)g(con\002gure)d(the)0 1021 y(gi)n(v)o(en)18 b(stack)i(as)h(a)f(signal)g(stack)g(via)f Fj(sigaltstack)p Fs(\(2\),)e(send)0 1120 y(the)37 b(current)f(process)h d1073 2 a1074 2 1220 y(trol)d(onto)f(this)i(stack,)i(sa)n(v)o(e)d(the)g(machine)f (conte)o(xt)f(there)i(via)0 1319 y Fj(setjmp)p Fs(\(3\),)19 d1076 1 a1076 1 1419 y(strap)g(into)g(the)g(startup)g(routine.)125 1520 d1078 3 a1080 3 1620 y(comes)27 b(from)e(the)i(signal)g(handler)f(scope)g(which)h (implies)g(v)n(ar)n(-)0 1719 y(ious)g(restrictions)g(on)g(Unix)g (platforms.)45 b(As)28 b(we)f(will)h(see,)i(we)0 1819 d1082 7 a1088 7 (of)g(it.)41 b(The)25 b(second)0 1919 y(main)j(problem)f(is,)32 b(ho)n(w)c(to)h(prepare)e(the)h(calling)g(of)h(the)g(start)0 2018 y(routine)19 b(without)g(actually)h(entering)f(it.)0 2298 y Ft(3.2)121 b(Algor)r(ithm)0 2456 y Fs(The)33 b(input)g(to)h(the) g Fj(mctx)p 808 2456 V 29 w(create)f Fs(function)f(is)i(the)g(thread)0 2556 y(startup)24 b(routine)g Fr(func)p Fs(,)h(the)g(thread)f(startup)g (ar)o(gument)f Fr(ar)m(g)i Fs(and)0 2656 y(a)g(chunk)e(of)i(memory)e d1090 2 a1091 2 Fs(,)0 2755 y(which)20 b(should)f(become)g(the)h(threads)g(stack.)26 b(The)20 b(algorithm)e(of)0 2855 y Fj(mctx)p 205 2855 d1093 2 a1094 2 0 2954 y(algorithm)j(one)g(can)h(\002nd)g(in)h Fr(GNU)g(Pth)p Fs(:)p Black 104 3143 a(1.)p Black 41 w(Allocate)f(a)g(ne)n(w)g d1096 3 a1098 3 Black 104 3315 a(2.)p Black 41 w(Preserv)o(e)g(the)h(current)f(signal)h (mask)h(and)e(block)h(an)g(arbi-)208 3415 y(trary)f(w)o(ork)o(er)g (signal.)p Black 104 3587 a(3.)p Black 41 w(Preserv)o(e)33 d1100 4 a1103 4 3687 y(w)o(ork)o(er)14 b(signal)h(con\002gure)f(a)i(trampoline)d (function)h(as)i(the)208 3786 y(ne)n(w)k(temporary)e(signal)i(action.)p Black 104 3959 a(4.)p Black 41 w(Preserv)o(e)d(a)i(possibly)f(acti)n(v) o(e)g(alternate)g(signal)g(stack)h(and)208 4058 y(con\002gure)35 d1105 3 a1107 3 4158 y(signal)20 b(stack)g(of)g(length)f Fr(sksize)p Fs(.)p Black 104 4330 a(5.)p Black 41 w(Send)33 b(the)h(current)e (process)h(the)h(w)o(ork)o(er)f(signal,)j(tem-)208 4430 d1109 2 a1110 2 (to)g(be)g(deli)n(v-)208 4529 y(ered)24 b(on)g(the)h(signal)g(stack)g (in)f(order)g(to)h(transfer)f(e)o(x)o(ecu-)208 4629 y(tion)19 d1112 2 a1113 2 104 4801 a(6.)p Black 41 w(After)31 b(the)g(trampoline)f(function)g (asynchronously)e(en-)208 4901 y(tered,)18 b(sa)n(v)o(e)g(its)i d1115 5 a1119 5 5000 y(and)f(immediately)g(return)f(from)h(it)i(to)f(terminate)f(the)h (sig-)208 5100 y(nal)h(handler)e(scope.)p Black 2095 83 a(7.)p Black 41 w(Restore)j(the)g(preserv)o(ed)d(alternate)i(signal) h(stack)f(and)h(the)2199 183 y(preserv)o(ed)c(signal)h(action)g(and)g (mask)g(for)g(w)o(ork)o(er)g(signal.)p Black 2095 368 d1121 2 a1122 2 (xt)g(of)2199 468 y Fj(mctx)p 2404 468 V 29 w(create)p Fs(.)p Black 2095 653 a(9.)p Black 41 w(Restore)16 b(the)g(pre)n d1124 25 a1148 25 753 y(trampoline)k(function)h(\()p Fr(mctx)p Fs(\))h(to)g(again)f (transfer)g(e)o(x)o(ecu-)2199 853 y(tion)29 b(control)e(onto)i(the)g (alternate)f(stack,)j(b)n(ut)e(this)g(time)2199 952 y(without\(!\))23 b(signal)e(handler)d(scope.)p Black 2054 1138 a(10.)p Black 40 w(In)25 b(the)g(again)f(reached)f(trampoline)g(function,)i (immedi-)2199 1237 y(ately)19 b(bootstrap)e(into)i(a)h(clean)e(stack)h (frame)f(situation)h(by)2199 1337 y(calling)h(a)g(second)g(trampoline)e (function.)p Black 2054 1522 a(11.)p Black 40 w(Set)56 b(the)f(ne)n(w)f(signal)h(mask)g(to)g(be)g(the)g(same)g(as)2199 1622 y(the)37 b(original)f(signal)g(mask)h(which)f(w)o(as)i(acti)n(v)o (e)e(when)2199 1722 y Fj(mctx)p 2404 1722 V 29 w(create)20 b Fs(w)o(as)h(called.)p Black 2054 1907 a(12.)p Black 40 w(Load)50 b(the)i(startup)e(information)f(\()p Fr(func)p Fs(,)58 b Fr(ar)m(g)p Fs(\))51 b(from)2199 2007 y Fj(mctx)p 2404 2007 V 29 w(create)20 b Fs(into)g(local)g(\(stack-based\))e(v)n (ariables.)p Black 2054 2192 a(13.)p Black 40 w(Restore)26 b(the)g(machine)f(conte)o(xt)g(of)h Fj(mctx)p 3486 2192 V 29 w(create)f Fs(to)2199 2292 y(transfer)19 b(e)o(x)o(ecution)f (control)h(back)h(to)g(it.)p Black 2054 2477 a(14.)p Black 40 w(Return)28 b(the)h(generated)d(machine)i(conte)o(xt)f Fr(mctx)i Fs(to)g(the)2199 2577 y(caller)-5 b(.)1992 2779 y(When)23 b(we)h(no)n(w)g(again)f(switch)h(into)f(the)h (established)g(machine)1992 2879 y(conte)o(xt)15 b Fr(mctx)p Fs(,)j(the)f(thread)f(starts)i(running)d(at)j(routine)e Fr(func)g Fs(with)1992 2978 y(ar)o(gument)22 b Fr(ar)m(g)p d1150 1 a1150 1 (the)1992 3078 y(algorithm:)2001 3343 y d1670 3 a1672 3 581 y(scope,)d(when)f(you)g(remember)-5 b(.)125 682 y(The)17 b(only)h(alternati)n(v)o(e)f(w)o(ould)h(be,)g(to)g(lea)n(v)o(e)g(the)h (signal)f(han-)0 781 y(dler)31 b(via)g Fj(longjmp)p Fs(\(3\),)h(b)n(ut) d1704 1 a1704 1 (mentioned)g(\(slighter\))f(stack)j(frame)e(prob-)0 2974 d1728 3 a1730 3 (the)f(par)n(-)0 5000 y(ent.)35 b(In)23 b(practice)g(the)h (implementation)d(in)i Fr(GNU)i(Pth)e Fs(sho)n(wed,)0 5100 y(that)h(then)g(one)g(no)g(longer)f(suf)n(fers)g(from)g(those)h @