You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ev.c 127KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174
  1. /*
  2. * libev event processing core, watcher management
  3. *
  4. * Copyright (c) 2007-2018 Marc Alexander Lehmann <libev@schmorp.de>
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modifica-
  8. * tion, are permitted provided that the following conditions are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. *
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
  19. * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  20. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
  21. * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  23. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  24. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
  25. * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  26. * OF THE POSSIBILITY OF SUCH DAMAGE.
  27. *
  28. * Alternatively, the contents of this file may be used under the terms of
  29. * the GNU General Public License ("GPL") version 2 or any later version,
  30. * in which case the provisions of the GPL are applicable instead of
  31. * the above. If you wish to allow the use of your version of this file
  32. * only under the terms of the GPL and not to allow others to use your
  33. * version of this file under the BSD license, indicate your decision
  34. * by deleting the provisions above and replace them with the notice
  35. * and other provisions required by the GPL. If you do not delete the
  36. * provisions above, a recipient may use your version of this file under
  37. * either the BSD or the GPL.
  38. */
  39. /* this big block deduces configuration from config.h */
  40. #ifndef EV_STANDALONE
  41. # ifdef EV_CONFIG_H
  42. # include EV_CONFIG_H
  43. # else
  44. # include "config.h"
  45. # endif
  46. #ifdef __GNUC__
  47. #pragma GCC diagnostic ignored "-Wunused-value"
  48. #pragma GCC diagnostic ignored "-Wstrict-aliasing"
  49. #endif
  50. # if HAVE_FLOOR
  51. # ifndef EV_USE_FLOOR
  52. # define EV_USE_FLOOR 1
  53. # endif
  54. # endif
  55. # if HAVE_CLOCK_SYSCALL
  56. # ifndef EV_USE_CLOCK_SYSCALL
  57. # define EV_USE_CLOCK_SYSCALL 1
  58. # ifndef EV_USE_REALTIME
  59. # define EV_USE_REALTIME 0
  60. # endif
  61. # ifndef EV_USE_MONOTONIC
  62. # define EV_USE_MONOTONIC 1
  63. # endif
  64. # endif
  65. # elif !defined EV_USE_CLOCK_SYSCALL
  66. # define EV_USE_CLOCK_SYSCALL 0
  67. # endif
  68. # if HAVE_CLOCK_GETTIME
  69. # ifndef EV_USE_MONOTONIC
  70. # define EV_USE_MONOTONIC 1
  71. # endif
  72. # ifndef EV_USE_REALTIME
  73. # define EV_USE_REALTIME 0
  74. # endif
  75. # else
  76. # ifndef EV_USE_MONOTONIC
  77. # define EV_USE_MONOTONIC 0
  78. # endif
  79. # ifndef EV_USE_REALTIME
  80. # define EV_USE_REALTIME 0
  81. # endif
  82. # endif
  83. # if HAVE_NANOSLEEP
  84. # ifndef EV_USE_NANOSLEEP
  85. # define EV_USE_NANOSLEEP EV_FEATURE_OS
  86. # endif
  87. # else
  88. # undef EV_USE_NANOSLEEP
  89. # define EV_USE_NANOSLEEP 0
  90. # endif
  91. # if HAVE_SELECT && HAVE_SYS_SELECT_H
  92. # ifndef EV_USE_SELECT
  93. # define EV_USE_SELECT EV_FEATURE_BACKENDS
  94. # endif
  95. # else
  96. # undef EV_USE_SELECT
  97. # define EV_USE_SELECT 0
  98. # endif
  99. # if HAVE_POLL && HAVE_POLL_H
  100. # ifndef EV_USE_POLL
  101. # define EV_USE_POLL EV_FEATURE_BACKENDS
  102. # endif
  103. # else
  104. # undef EV_USE_POLL
  105. # define EV_USE_POLL 0
  106. # endif
  107. # if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H
  108. # ifndef EV_USE_EPOLL
  109. # define EV_USE_EPOLL EV_FEATURE_BACKENDS
  110. # endif
  111. # else
  112. # undef EV_USE_EPOLL
  113. # define EV_USE_EPOLL 0
  114. # endif
  115. # if HAVE_KQUEUE && HAVE_SYS_EVENT_H
  116. # ifndef EV_USE_KQUEUE
  117. # define EV_USE_KQUEUE EV_FEATURE_BACKENDS
  118. # endif
  119. # else
  120. # undef EV_USE_KQUEUE
  121. # define EV_USE_KQUEUE 0
  122. # endif
  123. # if HAVE_PORT_H && HAVE_PORT_CREATE
  124. # ifndef EV_USE_PORT
  125. # define EV_USE_PORT EV_FEATURE_BACKENDS
  126. # endif
  127. # else
  128. # undef EV_USE_PORT
  129. # define EV_USE_PORT 0
  130. # endif
  131. # if HAVE_INOTIFY_INIT && HAVE_SYS_INOTIFY_H
  132. # ifndef EV_USE_INOTIFY
  133. # define EV_USE_INOTIFY EV_FEATURE_OS
  134. # endif
  135. # else
  136. # undef EV_USE_INOTIFY
  137. # define EV_USE_INOTIFY 0
  138. # endif
  139. # if HAVE_SIGNALFD && HAVE_SYS_SIGNALFD_H
  140. # ifndef EV_USE_SIGNALFD
  141. # define EV_USE_SIGNALFD EV_FEATURE_OS
  142. # endif
  143. # else
  144. # undef EV_USE_SIGNALFD
  145. # define EV_USE_SIGNALFD 0
  146. # endif
  147. # if HAVE_EVENTFD
  148. # ifndef EV_USE_EVENTFD
  149. # define EV_USE_EVENTFD EV_FEATURE_OS
  150. # endif
  151. # else
  152. # undef EV_USE_EVENTFD
  153. # define EV_USE_EVENTFD 0
  154. # endif
  155. #endif
  156. /* OS X, in its infinite idiocy, actually HARDCODES
  157. * a limit of 1024 into their select. Where people have brains,
  158. * OS X engineers apparently have a vacuum. Or maybe they were
  159. * ordered to have a vacuum, or they do anything for money.
  160. * This might help. Or not.
  161. * Note that this must be defined early, as other include files
  162. * will rely on this define as well.
  163. */
  164. #define _DARWIN_UNLIMITED_SELECT 1
  165. #include <stdlib.h>
  166. #include <string.h>
  167. #include <fcntl.h>
  168. #include <stddef.h>
  169. #include <stdio.h>
  170. #include <assert.h>
  171. #include <errno.h>
  172. #include <sys/types.h>
  173. #include <time.h>
  174. #include <limits.h>
  175. #include <signal.h>
  176. #ifdef EV_H
  177. # include EV_H
  178. #else
  179. # include "ev.h"
  180. #endif
  181. #if EV_NO_THREADS
  182. # undef EV_NO_SMP
  183. # define EV_NO_SMP 1
  184. # undef ECB_NO_THREADS
  185. # define ECB_NO_THREADS 1
  186. #endif
  187. #if EV_NO_SMP
  188. # undef EV_NO_SMP
  189. # define ECB_NO_SMP 1
  190. #endif
  191. #ifndef _WIN32
  192. # include <sys/time.h>
  193. # include <sys/wait.h>
  194. # include <unistd.h>
  195. #else
  196. # include <io.h>
  197. # define WIN32_LEAN_AND_MEAN
  198. # include <winsock2.h>
  199. # include <windows.h>
  200. # ifndef EV_SELECT_IS_WINSOCKET
  201. # define EV_SELECT_IS_WINSOCKET 1
  202. # endif
  203. # undef EV_AVOID_STDIO
  204. #endif
  205. /* this block tries to deduce configuration from header-defined symbols and defaults */
  206. /* try to deduce the maximum number of signals on this platform */
  207. #if defined EV_NSIG
  208. /* use what's provided */
  209. #elif defined NSIG
  210. # define EV_NSIG (NSIG)
  211. #elif defined _NSIG
  212. # define EV_NSIG (_NSIG)
  213. #elif defined SIGMAX
  214. # define EV_NSIG (SIGMAX+1)
  215. #elif defined SIG_MAX
  216. # define EV_NSIG (SIG_MAX+1)
  217. #elif defined _SIG_MAX
  218. # define EV_NSIG (_SIG_MAX+1)
  219. #elif defined MAXSIG
  220. # define EV_NSIG (MAXSIG+1)
  221. #elif defined MAX_SIG
  222. # define EV_NSIG (MAX_SIG+1)
  223. #elif defined SIGARRAYSIZE
  224. # define EV_NSIG (SIGARRAYSIZE) /* Assume ary[SIGARRAYSIZE] */
  225. #elif defined _sys_nsig
  226. # define EV_NSIG (_sys_nsig) /* Solaris 2.5 */
  227. #else
  228. # define EV_NSIG (8 * sizeof (sigset_t) + 1)
  229. #endif
  230. #ifndef EV_USE_FLOOR
  231. # define EV_USE_FLOOR 0
  232. #endif
  233. #ifndef EV_USE_CLOCK_SYSCALL
  234. # if __linux && __GLIBC__ == 2 && __GLIBC_MINOR__ < 17
  235. # define EV_USE_CLOCK_SYSCALL EV_FEATURE_OS
  236. # else
  237. # define EV_USE_CLOCK_SYSCALL 0
  238. # endif
  239. #endif
  240. #if !(_POSIX_TIMERS > 0)
  241. # ifndef EV_USE_MONOTONIC
  242. # define EV_USE_MONOTONIC 0
  243. # endif
  244. # ifndef EV_USE_REALTIME
  245. # define EV_USE_REALTIME 0
  246. # endif
  247. #endif
  248. #ifndef EV_USE_MONOTONIC
  249. # if defined _POSIX_MONOTONIC_CLOCK && _POSIX_MONOTONIC_CLOCK >= 0
  250. # define EV_USE_MONOTONIC EV_FEATURE_OS
  251. # else
  252. # define EV_USE_MONOTONIC 0
  253. # endif
  254. #endif
  255. #ifndef EV_USE_REALTIME
  256. # define EV_USE_REALTIME !EV_USE_CLOCK_SYSCALL
  257. #endif
  258. #ifndef EV_USE_NANOSLEEP
  259. # if _POSIX_C_SOURCE >= 199309L
  260. # define EV_USE_NANOSLEEP EV_FEATURE_OS
  261. # else
  262. # define EV_USE_NANOSLEEP 0
  263. # endif
  264. #endif
  265. #ifndef EV_USE_SELECT
  266. # define EV_USE_SELECT EV_FEATURE_BACKENDS
  267. #endif
  268. #ifndef EV_USE_POLL
  269. # ifdef _WIN32
  270. # define EV_USE_POLL 0
  271. # else
  272. # define EV_USE_POLL EV_FEATURE_BACKENDS
  273. # endif
  274. #endif
  275. #ifndef EV_USE_EPOLL
  276. # if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4))
  277. # define EV_USE_EPOLL EV_FEATURE_BACKENDS
  278. # else
  279. # define EV_USE_EPOLL 0
  280. # endif
  281. #endif
  282. #ifndef EV_USE_KQUEUE
  283. # define EV_USE_KQUEUE 0
  284. #endif
  285. #ifndef EV_USE_PORT
  286. # define EV_USE_PORT 0
  287. #endif
  288. #ifndef EV_USE_INOTIFY
  289. # if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4))
  290. # define EV_USE_INOTIFY EV_FEATURE_OS
  291. # else
  292. # define EV_USE_INOTIFY 0
  293. # endif
  294. #endif
  295. #ifndef EV_PID_HASHSIZE
  296. # define EV_PID_HASHSIZE EV_FEATURE_DATA ? 16 : 1
  297. #endif
  298. #ifndef EV_INOTIFY_HASHSIZE
  299. # define EV_INOTIFY_HASHSIZE EV_FEATURE_DATA ? 16 : 1
  300. #endif
  301. #ifndef EV_USE_EVENTFD
  302. # if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7))
  303. # define EV_USE_EVENTFD EV_FEATURE_OS
  304. # else
  305. # define EV_USE_EVENTFD 0
  306. # endif
  307. #endif
  308. #ifndef EV_USE_SIGNALFD
  309. # if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7))
  310. # define EV_USE_SIGNALFD EV_FEATURE_OS
  311. # else
  312. # define EV_USE_SIGNALFD 0
  313. # endif
  314. #endif
  315. #if 0 /* debugging */
  316. # define EV_VERIFY 3
  317. # define EV_USE_4HEAP 1
  318. # define EV_HEAP_CACHE_AT 1
  319. #endif
  320. #ifndef EV_VERIFY
  321. # define EV_VERIFY (EV_FEATURE_API ? 1 : 0)
  322. #endif
  323. #ifndef EV_USE_4HEAP
  324. # define EV_USE_4HEAP EV_FEATURE_DATA
  325. #endif
  326. #ifndef EV_HEAP_CACHE_AT
  327. # define EV_HEAP_CACHE_AT EV_FEATURE_DATA
  328. #endif
  329. #ifdef __ANDROID__
  330. /* supposedly, android doesn't typedef fd_mask */
  331. # undef EV_USE_SELECT
  332. # define EV_USE_SELECT 0
  333. /* supposedly, we need to include syscall.h, not sys/syscall.h, so just disable */
  334. # undef EV_USE_CLOCK_SYSCALL
  335. # define EV_USE_CLOCK_SYSCALL 0
  336. #endif
  337. /* aix's poll.h seems to cause lots of trouble */
  338. #ifdef _AIX
  339. /* AIX has a completely broken poll.h header */
  340. # undef EV_USE_POLL
  341. # define EV_USE_POLL 0
  342. #endif
  343. /* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */
  344. /* which makes programs even slower. might work on other unices, too. */
  345. #if EV_USE_CLOCK_SYSCALL
  346. # include <sys/syscall.h>
  347. # ifdef SYS_clock_gettime
  348. # define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts))
  349. # undef EV_USE_MONOTONIC
  350. # define EV_USE_MONOTONIC 1
  351. # else
  352. # undef EV_USE_CLOCK_SYSCALL
  353. # define EV_USE_CLOCK_SYSCALL 0
  354. # endif
  355. #endif
  356. /* this block fixes any misconfiguration where we know we run into trouble otherwise */
  357. #ifndef CLOCK_MONOTONIC
  358. # undef EV_USE_MONOTONIC
  359. # define EV_USE_MONOTONIC 0
  360. #endif
  361. #ifndef CLOCK_REALTIME
  362. # undef EV_USE_REALTIME
  363. # define EV_USE_REALTIME 0
  364. #endif
  365. #if !EV_STAT_ENABLE
  366. # undef EV_USE_INOTIFY
  367. # define EV_USE_INOTIFY 0
  368. #endif
  369. #if !EV_USE_NANOSLEEP
  370. /* hp-ux has it in sys/time.h, which we unconditionally include above */
  371. # if !defined _WIN32 && !defined __hpux
  372. # include <sys/select.h>
  373. # endif
  374. #endif
  375. #if EV_USE_INOTIFY
  376. # include <sys/statfs.h>
  377. # include <sys/inotify.h>
  378. /* some very old inotify.h headers don't have IN_DONT_FOLLOW */
  379. # ifndef IN_DONT_FOLLOW
  380. # undef EV_USE_INOTIFY
  381. # define EV_USE_INOTIFY 0
  382. # endif
  383. #endif
  384. #if EV_USE_EVENTFD
  385. /* our minimum requirement is glibc 2.7 which has the stub, but not the header */
  386. # include <stdint.h>
  387. # ifndef EFD_NONBLOCK
  388. # define EFD_NONBLOCK O_NONBLOCK
  389. # endif
  390. # ifndef EFD_CLOEXEC
  391. # ifdef O_CLOEXEC
  392. # define EFD_CLOEXEC O_CLOEXEC
  393. # else
  394. # define EFD_CLOEXEC 02000000
  395. # endif
  396. # endif
  397. EV_CPP(extern "C") int (eventfd) (unsigned int initval, int flags);
  398. #endif
  399. #if EV_USE_SIGNALFD
  400. /* our minimum requirement is glibc 2.7 which has the stub, but not the header */
  401. # include <stdint.h>
  402. # ifndef SFD_NONBLOCK
  403. # define SFD_NONBLOCK O_NONBLOCK
  404. # endif
  405. # ifndef SFD_CLOEXEC
  406. # ifdef O_CLOEXEC
  407. # define SFD_CLOEXEC O_CLOEXEC
  408. # else
  409. # define SFD_CLOEXEC 02000000
  410. # endif
  411. # endif
  412. EV_CPP (extern "C") int signalfd (int fd, const sigset_t *mask, int flags);
  413. struct signalfd_siginfo
  414. {
  415. uint32_t ssi_signo;
  416. char pad[128 - sizeof (uint32_t)];
  417. };
  418. #endif
  419. /**/
  420. #if EV_VERIFY >= 3
  421. # define EV_FREQUENT_CHECK ev_verify (EV_A)
  422. #else
  423. # define EV_FREQUENT_CHECK do { } while (0)
  424. #endif
  425. /*
  426. * This is used to work around floating point rounding problems.
  427. * This value is good at least till the year 4000.
  428. */
  429. #define MIN_INTERVAL 0.0001220703125 /* 1/2**13, good till 4000 */
  430. #if 0
  431. #define MIN_INTERVAL 0.00000095367431640625 /* 1/2**20, good till 2200 */
  432. #endif
  433. #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
  434. #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */
  435. #define EV_TV_SET(tv,t) do { tv.tv_sec = (long)t; tv.tv_usec = (long)((t - tv.tv_sec) * 1e6); } while (0)
  436. #define EV_TS_SET(ts,t) do { ts.tv_sec = (long)t; ts.tv_nsec = (long)((t - ts.tv_sec) * 1e9); } while (0)
  437. /* the following is ecb.h embedded into libev - use update_ev_c to update from an external copy */
  438. /* ECB.H BEGIN */
  439. /*
  440. * libecb - http://software.schmorp.de/pkg/libecb
  441. *
  442. * Copyright (©) 2009-2015 Marc Alexander Lehmann <libecb@schmorp.de>
  443. * Copyright (©) 2011 Emanuele Giaquinta
  444. * All rights reserved.
  445. *
  446. * Redistribution and use in source and binary forms, with or without modifica-
  447. * tion, are permitted provided that the following conditions are met:
  448. *
  449. * 1. Redistributions of source code must retain the above copyright notice,
  450. * this list of conditions and the following disclaimer.
  451. *
  452. * 2. Redistributions in binary form must reproduce the above copyright
  453. * notice, this list of conditions and the following disclaimer in the
  454. * documentation and/or other materials provided with the distribution.
  455. *
  456. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  457. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
  458. * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  459. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
  460. * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  461. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  462. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  463. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
  464. * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  465. * OF THE POSSIBILITY OF SUCH DAMAGE.
  466. *
  467. * Alternatively, the contents of this file may be used under the terms of
  468. * the GNU General Public License ("GPL") version 2 or any later version,
  469. * in which case the provisions of the GPL are applicable instead of
  470. * the above. If you wish to allow the use of your version of this file
  471. * only under the terms of the GPL and not to allow others to use your
  472. * version of this file under the BSD license, indicate your decision
  473. * by deleting the provisions above and replace them with the notice
  474. * and other provisions required by the GPL. If you do not delete the
  475. * provisions above, a recipient may use your version of this file under
  476. * either the BSD or the GPL.
  477. */
  478. #ifndef ECB_H
  479. #define ECB_H
  480. /* 16 bits major, 16 bits minor */
  481. #define ECB_VERSION 0x00010005
  482. #ifdef _WIN32
  483. typedef signed char int8_t;
  484. typedef unsigned char uint8_t;
  485. typedef signed short int16_t;
  486. typedef unsigned short uint16_t;
  487. typedef signed int int32_t;
  488. typedef unsigned int uint32_t;
  489. #if __GNUC__
  490. typedef signed long long int64_t;
  491. typedef unsigned long long uint64_t;
  492. #else /* _MSC_VER || __BORLANDC__ */
  493. typedef signed __int64 int64_t;
  494. typedef unsigned __int64 uint64_t;
  495. #endif
  496. #ifdef _WIN64
  497. #define ECB_PTRSIZE 8
  498. typedef uint64_t uintptr_t;
  499. typedef int64_t intptr_t;
  500. #else
  501. #define ECB_PTRSIZE 4
  502. typedef uint32_t uintptr_t;
  503. typedef int32_t intptr_t;
  504. #endif
  505. #else
  506. #include <inttypes.h>
  507. #if (defined INTPTR_MAX ? INTPTR_MAX : ULONG_MAX) > 0xffffffffU
  508. #define ECB_PTRSIZE 8
  509. #else
  510. #define ECB_PTRSIZE 4
  511. #endif
  512. #endif
  513. #define ECB_GCC_AMD64 (__amd64 || __amd64__ || __x86_64 || __x86_64__)
  514. #define ECB_MSVC_AMD64 (_M_AMD64 || _M_X64)
  515. /* work around x32 idiocy by defining proper macros */
  516. #if ECB_GCC_AMD64 || ECB_MSVC_AMD64
  517. #if _ILP32
  518. #define ECB_AMD64_X32 1
  519. #else
  520. #define ECB_AMD64 1
  521. #endif
  522. #endif
  523. /* many compilers define _GNUC_ to some versions but then only implement
  524. * what their idiot authors think are the "more important" extensions,
  525. * causing enormous grief in return for some better fake benchmark numbers.
  526. * or so.
  527. * we try to detect these and simply assume they are not gcc - if they have
  528. * an issue with that they should have done it right in the first place.
  529. */
  530. #if !defined __GNUC_MINOR__ || defined __INTEL_COMPILER || defined __SUNPRO_C || defined __SUNPRO_CC || defined __llvm__ || defined __clang__
  531. #define ECB_GCC_VERSION(major,minor) 0
  532. #else
  533. #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
  534. #endif
  535. #define ECB_CLANG_VERSION(major,minor) (__clang_major__ > (major) || (__clang_major__ == (major) && __clang_minor__ >= (minor)))
  536. #if __clang__ && defined __has_builtin
  537. #define ECB_CLANG_BUILTIN(x) __has_builtin (x)
  538. #else
  539. #define ECB_CLANG_BUILTIN(x) 0
  540. #endif
  541. #if __clang__ && defined __has_extension
  542. #define ECB_CLANG_EXTENSION(x) __has_extension (x)
  543. #else
  544. #define ECB_CLANG_EXTENSION(x) 0
  545. #endif
  546. #define ECB_CPP (__cplusplus+0)
  547. #define ECB_CPP11 (__cplusplus >= 201103L)
  548. #define ECB_CPP14 (__cplusplus >= 201402L)
  549. #define ECB_CPP17 (__cplusplus >= 201703L)
  550. #if ECB_CPP
  551. #define ECB_C 0
  552. #define ECB_STDC_VERSION 0
  553. #else
  554. #define ECB_C 1
  555. #define ECB_STDC_VERSION __STDC_VERSION__
  556. #endif
  557. #define ECB_C99 (ECB_STDC_VERSION >= 199901L)
  558. #define ECB_C11 (ECB_STDC_VERSION >= 201112L)
  559. #define ECB_C17 (ECB_STDC_VERSION >= 201710L)
  560. #if ECB_CPP
  561. #define ECB_EXTERN_C extern "C"
  562. #define ECB_EXTERN_C_BEG ECB_EXTERN_C {
  563. #define ECB_EXTERN_C_END }
  564. #else
  565. #define ECB_EXTERN_C extern
  566. #define ECB_EXTERN_C_BEG
  567. #define ECB_EXTERN_C_END
  568. #endif
  569. /*****************************************************************************/
  570. /* ECB_NO_THREADS - ecb is not used by multiple threads, ever */
  571. /* ECB_NO_SMP - ecb might be used in multiple threads, but only on a single cpu */
  572. #if ECB_NO_THREADS
  573. #define ECB_NO_SMP 1
  574. #endif
  575. #if ECB_NO_SMP
  576. #define ECB_MEMORY_FENCE do { } while (0)
  577. #endif
  578. /* http://www-01.ibm.com/support/knowledgecenter/SSGH3R_13.1.0/com.ibm.xlcpp131.aix.doc/compiler_ref/compiler_builtins.html */
  579. #if __xlC__ && ECB_CPP
  580. #include <builtins.h>
  581. #endif
  582. #if 1400 <= _MSC_VER
  583. #include <intrin.h> /* fence functions _ReadBarrier, also bit search functions _BitScanReverse */
  584. #endif
  585. #ifndef ECB_MEMORY_FENCE
  586. #if ECB_GCC_VERSION(2,5) || defined __INTEL_COMPILER || (__llvm__ && __GNUC__) || __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110
  587. #if __i386 || __i386__
  588. #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory")
  589. #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("" : : : "memory")
  590. #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("" : : : "memory")
  591. #elif ECB_GCC_AMD64
  592. #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory")
  593. #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("" : : : "memory")
  594. #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("" : : : "memory")
  595. #elif __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__
  596. #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory")
  597. #elif defined __ARM_ARCH_2__ \
  598. || defined __ARM_ARCH_3__ || defined __ARM_ARCH_3M__ \
  599. || defined __ARM_ARCH_4__ || defined __ARM_ARCH_4T__ \
  600. || defined __ARM_ARCH_5__ || defined __ARM_ARCH_5E__ \
  601. || defined __ARM_ARCH_5T__ || defined __ARM_ARCH_5TE__ \
  602. || defined __ARM_ARCH_5TEJ__
  603. /* should not need any, unless running old code on newer cpu - arm doesn't support that */
  604. #elif defined __ARM_ARCH_6__ || defined __ARM_ARCH_6J__ \
  605. || defined __ARM_ARCH_6K__ || defined __ARM_ARCH_6ZK__ \
  606. || defined __ARM_ARCH_6T2__
  607. #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mcr p15,0,%0,c7,c10,5" : : "r" (0) : "memory")
  608. #elif defined __ARM_ARCH_7__ || defined __ARM_ARCH_7A__ \
  609. || defined __ARM_ARCH_7R__ || defined __ARM_ARCH_7M__
  610. #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb" : : : "memory")
  611. #elif __aarch64__
  612. #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb ish" : : : "memory")
  613. #elif (__sparc || __sparc__) && !(__sparc_v8__ || defined __sparcv8)
  614. #define ECB_MEMORY_FENCE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad | #StoreStore | #StoreLoad" : : : "memory")
  615. #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad" : : : "memory")
  616. #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("membar #LoadStore | #StoreStore")
  617. #elif defined __s390__ || defined __s390x__
  618. #define ECB_MEMORY_FENCE __asm__ __volatile__ ("bcr 15,0" : : : "memory")
  619. #elif defined __mips__
  620. /* GNU/Linux emulates sync on mips1 architectures, so we force its use */
  621. /* anybody else who still uses mips1 is supposed to send in their version, with detection code. */
  622. #define ECB_MEMORY_FENCE __asm__ __volatile__ (".set mips2; sync; .set mips0" : : : "memory")
  623. #elif defined __alpha__
  624. #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mb" : : : "memory")
  625. #elif defined __hppa__
  626. #define ECB_MEMORY_FENCE __asm__ __volatile__ ("" : : : "memory")
  627. #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("")
  628. #elif defined __ia64__
  629. #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mf" : : : "memory")
  630. #elif defined __m68k__
  631. #define ECB_MEMORY_FENCE __asm__ __volatile__ ("" : : : "memory")
  632. #elif defined __m88k__
  633. #define ECB_MEMORY_FENCE __asm__ __volatile__ ("tb1 0,%%r0,128" : : : "memory")
  634. #elif defined __sh__
  635. #define ECB_MEMORY_FENCE __asm__ __volatile__ ("" : : : "memory")
  636. #endif
  637. #endif
  638. #endif
  639. #ifndef ECB_MEMORY_FENCE
  640. #if ECB_GCC_VERSION(4,7)
  641. /* see comment below (stdatomic.h) about the C11 memory model. */
  642. #define ECB_MEMORY_FENCE __atomic_thread_fence (__ATOMIC_SEQ_CST)
  643. #define ECB_MEMORY_FENCE_ACQUIRE __atomic_thread_fence (__ATOMIC_ACQUIRE)
  644. #define ECB_MEMORY_FENCE_RELEASE __atomic_thread_fence (__ATOMIC_RELEASE)
  645. #elif ECB_CLANG_EXTENSION(c_atomic)
  646. /* see comment below (stdatomic.h) about the C11 memory model. */
  647. #define ECB_MEMORY_FENCE __c11_atomic_thread_fence (__ATOMIC_SEQ_CST)
  648. #define ECB_MEMORY_FENCE_ACQUIRE __c11_atomic_thread_fence (__ATOMIC_ACQUIRE)
  649. #define ECB_MEMORY_FENCE_RELEASE __c11_atomic_thread_fence (__ATOMIC_RELEASE)
  650. #elif ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__
  651. #define ECB_MEMORY_FENCE __sync_synchronize ()
  652. #elif _MSC_VER >= 1500 /* VC++ 2008 */
  653. /* apparently, microsoft broke all the memory barrier stuff in Visual Studio 2008... */
  654. #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier)
  655. #define ECB_MEMORY_FENCE _ReadWriteBarrier (); MemoryBarrier()
  656. #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier (); MemoryBarrier() /* according to msdn, _ReadBarrier is not a load fence */
  657. #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier (); MemoryBarrier()
  658. #elif _MSC_VER >= 1400 /* VC++ 2005 */
  659. #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier)
  660. #define ECB_MEMORY_FENCE _ReadWriteBarrier ()
  661. #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier () /* according to msdn, _ReadBarrier is not a load fence */
  662. #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier ()
  663. #elif defined _WIN32
  664. #include <WinNT.h>
  665. #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */
  666. #elif __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110
  667. #include <mbarrier.h>
  668. #define ECB_MEMORY_FENCE __machine_rw_barrier ()
  669. #define ECB_MEMORY_FENCE_ACQUIRE __machine_r_barrier ()
  670. #define ECB_MEMORY_FENCE_RELEASE __machine_w_barrier ()
  671. #elif __xlC__
  672. #define ECB_MEMORY_FENCE __sync ()
  673. #endif
  674. #endif
  675. #ifndef ECB_MEMORY_FENCE
  676. #if ECB_C11 && !defined __STDC_NO_ATOMICS__
  677. /* we assume that these memory fences work on all variables/all memory accesses, */
  678. /* not just C11 atomics and atomic accesses */
  679. #include <stdatomic.h>
  680. /* Unfortunately, neither gcc 4.7 nor clang 3.1 generate any instructions for */
  681. /* any fence other than seq_cst, which isn't very efficient for us. */
  682. /* Why that is, we don't know - either the C11 memory model is quite useless */
  683. /* for most usages, or gcc and clang have a bug */
  684. /* I *currently* lean towards the latter, and inefficiently implement */
  685. /* all three of ecb's fences as a seq_cst fence */
  686. /* Update, gcc-4.8 generates mfence for all c++ fences, but nothing */
  687. /* for all __atomic_thread_fence's except seq_cst */
  688. #define ECB_MEMORY_FENCE atomic_thread_fence (memory_order_seq_cst)
  689. #endif
  690. #endif
  691. #ifndef ECB_MEMORY_FENCE
  692. #if !ECB_AVOID_PTHREADS
  693. /*
  694. * if you get undefined symbol references to pthread_mutex_lock,
  695. * or failure to find pthread.h, then you should implement
  696. * the ECB_MEMORY_FENCE operations for your cpu/compiler
  697. * OR provide pthread.h and link against the posix thread library
  698. * of your system.
  699. */
  700. #include <pthread.h>
  701. #define ECB_NEEDS_PTHREADS 1
  702. #define ECB_MEMORY_FENCE_NEEDS_PTHREADS 1
  703. static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER;
  704. #define ECB_MEMORY_FENCE do { pthread_mutex_lock (&ecb_mf_lock); pthread_mutex_unlock (&ecb_mf_lock); } while (0)
  705. #endif
  706. #endif
  707. #if !defined ECB_MEMORY_FENCE_ACQUIRE && defined ECB_MEMORY_FENCE
  708. #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE
  709. #endif
  710. #if !defined ECB_MEMORY_FENCE_RELEASE && defined ECB_MEMORY_FENCE
  711. #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE
  712. #endif
  713. /*****************************************************************************/
  714. #if ECB_CPP
  715. #define ecb_inline static inline
  716. #elif ECB_GCC_VERSION(2,5)
  717. #define ecb_inline static __inline__
  718. #elif ECB_C99
  719. #define ecb_inline static inline
  720. #else
  721. #define ecb_inline static
  722. #endif
  723. #if ECB_GCC_VERSION(3,3)
  724. #define ecb_restrict __restrict__
  725. #elif ECB_C99
  726. #define ecb_restrict restrict
  727. #else
  728. #define ecb_restrict
  729. #endif
  730. typedef int ecb_bool;
  731. #define ECB_CONCAT_(a, b) a ## b
  732. #define ECB_CONCAT(a, b) ECB_CONCAT_(a, b)
  733. #define ECB_STRINGIFY_(a) # a
  734. #define ECB_STRINGIFY(a) ECB_STRINGIFY_(a)
  735. #define ECB_STRINGIFY_EXPR(expr) ((expr), ECB_STRINGIFY_ (expr))
  736. #define ecb_function_ ecb_inline
  737. #if ECB_GCC_VERSION(3,1) || ECB_CLANG_VERSION(2,8)
  738. #define ecb_attribute(attrlist) __attribute__ (attrlist)
  739. #else
  740. #define ecb_attribute(attrlist)
  741. #endif
  742. #if ECB_GCC_VERSION(3,1) || ECB_CLANG_BUILTIN(__builtin_constant_p)
  743. #define ecb_is_constant(expr) __builtin_constant_p (expr)
  744. #else
  745. /* possible C11 impl for integral types
  746. typedef struct ecb_is_constant_struct ecb_is_constant_struct;
  747. #define ecb_is_constant(expr) _Generic ((1 ? (struct ecb_is_constant_struct *)0 : (void *)((expr) - (expr)), ecb_is_constant_struct *: 0, default: 1)) */
  748. #define ecb_is_constant(expr) 0
  749. #endif
  750. #if ECB_GCC_VERSION(3,1) || ECB_CLANG_BUILTIN(__builtin_expect)
  751. #define ecb_expect(expr,value) __builtin_expect ((expr),(value))
  752. #else
  753. #define ecb_expect(expr,value) (expr)
  754. #endif
  755. #if ECB_GCC_VERSION(3,1) || ECB_CLANG_BUILTIN(__builtin_prefetch)
  756. #define ecb_prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality)
  757. #else
  758. #define ecb_prefetch(addr,rw,locality)
  759. #endif
  760. /* no emulation for ecb_decltype */
  761. #if ECB_CPP11
  762. // older implementations might have problems with decltype(x)::type, work around it
  763. template<class T> struct ecb_decltype_t { typedef T type; };
  764. #define ecb_decltype(x) ecb_decltype_t<decltype (x)>::type
  765. #elif ECB_GCC_VERSION(3,0) || ECB_CLANG_VERSION(2,8)
  766. #define ecb_decltype(x) __typeof__ (x)
  767. #endif
  768. #if _MSC_VER >= 1300
  769. #define ecb_deprecated __declspec (deprecated)
  770. #else
  771. #define ecb_deprecated ecb_attribute ((__deprecated__))
  772. #endif
  773. #if _MSC_VER >= 1500
  774. #define ecb_deprecated_message(msg) __declspec (deprecated (msg))
  775. #elif ECB_GCC_VERSION(4,5)
  776. #define ecb_deprecated_message(msg) ecb_attribute ((__deprecated__ (msg))
  777. #else
  778. #define ecb_deprecated_message(msg) ecb_deprecated
  779. #endif
  780. #if _MSC_VER >= 1400
  781. #define ecb_noinline __declspec (noinline)
  782. #else
  783. #define ecb_noinline ecb_attribute ((__noinline__))
  784. #endif
  785. #define ecb_unused ecb_attribute ((__unused__))
  786. #define ecb_const ecb_attribute ((__const__))
  787. #define ecb_pure ecb_attribute ((__pure__))
  788. #if ECB_C11 || __IBMC_NORETURN
  789. /* http://www-01.ibm.com/support/knowledgecenter/SSGH3R_13.1.0/com.ibm.xlcpp131.aix.doc/language_ref/noreturn.html */
  790. #define ecb_noreturn _Noreturn
  791. #elif ECB_CPP11
  792. #define ecb_noreturn [[noreturn]]
  793. #elif _MSC_VER >= 1200
  794. /* http://msdn.microsoft.com/en-us/library/k6ktzx3s.aspx */
  795. #define ecb_noreturn __declspec (noreturn)
  796. #else
  797. #define ecb_noreturn ecb_attribute ((__noreturn__))
  798. #endif
  799. #if ECB_GCC_VERSION(4,3)
  800. #define ecb_artificial ecb_attribute ((__artificial__))
  801. #define ecb_hot ecb_attribute ((__hot__))
  802. #define ecb_cold ecb_attribute ((__cold__))
  803. #else
  804. #define ecb_artificial
  805. #define ecb_hot
  806. #define ecb_cold
  807. #endif
  808. /* put around conditional expressions if you are very sure that the */
  809. /* expression is mostly true or mostly false. note that these return */
  810. /* booleans, not the expression. */
  811. #define ecb_expect_false(expr) ecb_expect (!!(expr), 0)
  812. #define ecb_expect_true(expr) ecb_expect (!!(expr), 1)
  813. /* for compatibility to the rest of the world */
  814. #define ecb_likely(expr) ecb_expect_true (expr)
  815. #define ecb_unlikely(expr) ecb_expect_false (expr)
  816. /* count trailing zero bits and count # of one bits */
  817. #if ECB_GCC_VERSION(3,4) \
  818. || (ECB_CLANG_BUILTIN(__builtin_clz) && ECB_CLANG_BUILTIN(__builtin_clzll) \
  819. && ECB_CLANG_BUILTIN(__builtin_ctz) && ECB_CLANG_BUILTIN(__builtin_ctzll) \
  820. && ECB_CLANG_BUILTIN(__builtin_popcount))
  821. /* we assume int == 32 bit, long == 32 or 64 bit and long long == 64 bit */
  822. #define ecb_ld32(x) (__builtin_clz (x) ^ 31)
  823. #define ecb_ld64(x) (__builtin_clzll (x) ^ 63)
  824. #define ecb_ctz32(x) __builtin_ctz (x)
  825. #define ecb_ctz64(x) __builtin_ctzll (x)
  826. #define ecb_popcount32(x) __builtin_popcount (x)
  827. /* no popcountll */
  828. #else
  829. ecb_function_ ecb_const int ecb_ctz32 (uint32_t x);
  830. ecb_function_ ecb_const int
  831. ecb_ctz32 (uint32_t x)
  832. {
  833. #if 1400 <= _MSC_VER && (_M_IX86 || _M_X64 || _M_IA64 || _M_ARM)
  834. unsigned long r;
  835. _BitScanForward (&r, x);
  836. return (int)r;
  837. #else
  838. int r = 0;
  839. x &= ~x + 1; /* this isolates the lowest bit */
  840. #if ECB_branchless_on_i386
  841. r += !!(x & 0xaaaaaaaa) << 0;
  842. r += !!(x & 0xcccccccc) << 1;
  843. r += !!(x & 0xf0f0f0f0) << 2;
  844. r += !!(x & 0xff00ff00) << 3;
  845. r += !!(x & 0xffff0000) << 4;
  846. #else
  847. if (x & 0xaaaaaaaa) r += 1;
  848. if (x & 0xcccccccc) r += 2;
  849. if (x & 0xf0f0f0f0) r += 4;
  850. if (x & 0xff00ff00) r += 8;
  851. if (x & 0xffff0000) r += 16;
  852. #endif
  853. return r;
  854. #endif
  855. }
  856. ecb_function_ ecb_const int ecb_ctz64 (uint64_t x);
  857. ecb_function_ ecb_const int
  858. ecb_ctz64 (uint64_t x)
  859. {
  860. #if 1400 <= _MSC_VER && (_M_X64 || _M_IA64 || _M_ARM)
  861. unsigned long r;
  862. _BitScanForward64 (&r, x);
  863. return (int)r;
  864. #else
  865. int shift = x & 0xffffffff ? 0 : 32;
  866. return ecb_ctz32 (x >> shift) + shift;
  867. #endif
  868. }
  869. ecb_function_ ecb_const int ecb_popcount32 (uint32_t x);
  870. ecb_function_ ecb_const int
  871. ecb_popcount32 (uint32_t x)
  872. {
  873. x -= (x >> 1) & 0x55555555;
  874. x = ((x >> 2) & 0x33333333) + (x & 0x33333333);
  875. x = ((x >> 4) + x) & 0x0f0f0f0f;
  876. x *= 0x01010101;
  877. return x >> 24;
  878. }
  879. ecb_function_ ecb_const int ecb_ld32 (uint32_t x);
  880. ecb_function_ ecb_const int ecb_ld32 (uint32_t x)
  881. {
  882. #if 1400 <= _MSC_VER && (_M_IX86 || _M_X64 || _M_IA64 || _M_ARM)
  883. unsigned long r;
  884. _BitScanReverse (&r, x);
  885. return (int)r;
  886. #else
  887. int r = 0;
  888. if (x >> 16) { x >>= 16; r += 16; }
  889. if (x >> 8) { x >>= 8; r += 8; }
  890. if (x >> 4) { x >>= 4; r += 4; }
  891. if (x >> 2) { x >>= 2; r += 2; }
  892. if (x >> 1) { r += 1; }
  893. return r;
  894. #endif
  895. }
  896. ecb_function_ ecb_const int ecb_ld64 (uint64_t x);
  897. ecb_function_ ecb_const int ecb_ld64 (uint64_t x)
  898. {
  899. #if 1400 <= _MSC_VER && (_M_X64 || _M_IA64 || _M_ARM)
  900. unsigned long r;
  901. _BitScanReverse64 (&r, x);
  902. return (int)r;
  903. #else
  904. int r = 0;
  905. if (x >> 32) { x >>= 32; r += 32; }
  906. return r + ecb_ld32 (x);
  907. #endif
  908. }
  909. #endif
  910. ecb_function_ ecb_const ecb_bool ecb_is_pot32 (uint32_t x);
  911. ecb_function_ ecb_const ecb_bool ecb_is_pot32 (uint32_t x) { return !(x & (x - 1)); }
  912. ecb_function_ ecb_const ecb_bool ecb_is_pot64 (uint64_t x);
  913. ecb_function_ ecb_const ecb_bool ecb_is_pot64 (uint64_t x) { return !(x & (x - 1)); }
  914. ecb_function_ ecb_const uint8_t ecb_bitrev8 (uint8_t x);
  915. ecb_function_ ecb_const uint8_t ecb_bitrev8 (uint8_t x)
  916. {
  917. return ( (x * 0x0802U & 0x22110U)
  918. | (x * 0x8020U & 0x88440U)) * 0x10101U >> 16;
  919. }
  920. ecb_function_ ecb_const uint16_t ecb_bitrev16 (uint16_t x);
  921. ecb_function_ ecb_const uint16_t ecb_bitrev16 (uint16_t x)
  922. {
  923. x = ((x >> 1) & 0x5555) | ((x & 0x5555) << 1);
  924. x = ((x >> 2) & 0x3333) | ((x & 0x3333) << 2);
  925. x = ((x >> 4) & 0x0f0f) | ((x & 0x0f0f) << 4);
  926. x = ( x >> 8 ) | ( x << 8);
  927. return x;
  928. }
  929. ecb_function_ ecb_const uint32_t ecb_bitrev32 (uint32_t x);
  930. ecb_function_ ecb_const uint32_t ecb_bitrev32 (uint32_t x)
  931. {
  932. x = ((x >> 1) & 0x55555555) | ((x & 0x55555555) << 1);
  933. x = ((x >> 2) & 0x33333333) | ((x & 0x33333333) << 2);
  934. x = ((x >> 4) & 0x0f0f0f0f) | ((x & 0x0f0f0f0f) << 4);
  935. x = ((x >> 8) & 0x00ff00ff) | ((x & 0x00ff00ff) << 8);
  936. x = ( x >> 16 ) | ( x << 16);
  937. return x;
  938. }
  939. /* popcount64 is only available on 64 bit cpus as gcc builtin */
  940. /* so for this version we are lazy */
  941. ecb_function_ ecb_const int ecb_popcount64 (uint64_t x);
  942. ecb_function_ ecb_const int
  943. ecb_popcount64 (uint64_t x)
  944. {
  945. return ecb_popcount32 (x) + ecb_popcount32 (x >> 32);
  946. }
  947. ecb_inline ecb_const uint8_t ecb_rotl8 (uint8_t x, unsigned int count);
  948. ecb_inline ecb_const uint8_t ecb_rotr8 (uint8_t x, unsigned int count);
  949. ecb_inline ecb_const uint16_t ecb_rotl16 (uint16_t x, unsigned int count);
  950. ecb_inline ecb_const uint16_t ecb_rotr16 (uint16_t x, unsigned int count);
  951. ecb_inline ecb_const uint32_t ecb_rotl32 (uint32_t x, unsigned int count);
  952. ecb_inline ecb_const uint32_t ecb_rotr32 (uint32_t x, unsigned int count);
  953. ecb_inline ecb_const uint64_t ecb_rotl64 (uint64_t x, unsigned int count);
  954. ecb_inline ecb_const uint64_t ecb_rotr64 (uint64_t x, unsigned int count);
  955. ecb_inline ecb_const uint8_t ecb_rotl8 (uint8_t x, unsigned int count) { return (x >> ( 8 - count)) | (x << count); }
  956. ecb_inline ecb_const uint8_t ecb_rotr8 (uint8_t x, unsigned int count) { return (x << ( 8 - count)) | (x >> count); }
  957. ecb_inline ecb_const uint16_t ecb_rotl16 (uint16_t x, unsigned int count) { return (x >> (16 - count)) | (x << count); }
  958. ecb_inline ecb_const uint16_t ecb_rotr16 (uint16_t x, unsigned int count) { return (x << (16 - count)) | (x >> count); }
  959. ecb_inline ecb_const uint32_t ecb_rotl32 (uint32_t x, unsigned int count) { return (x >> (32 - count)) | (x << count); }
  960. ecb_inline ecb_const uint32_t ecb_rotr32 (uint32_t x, unsigned int count) { return (x << (32 - count)) | (x >> count); }
  961. ecb_inline ecb_const uint64_t ecb_rotl64 (uint64_t x, unsigned int count) { return (x >> (64 - count)) | (x << count); }
  962. ecb_inline ecb_const uint64_t ecb_rotr64 (uint64_t x, unsigned int count) { return (x << (64 - count)) | (x >> count); }
  963. #if ECB_GCC_VERSION(4,3) || (ECB_CLANG_BUILTIN(__builtin_bswap32) && ECB_CLANG_BUILTIN(__builtin_bswap64))
  964. #if ECB_GCC_VERSION(4,8) || ECB_CLANG_BUILTIN(__builtin_bswap16)
  965. #define ecb_bswap16(x) __builtin_bswap16 (x)
  966. #else
  967. #define ecb_bswap16(x) (__builtin_bswap32 (x) >> 16)
  968. #endif
  969. #define ecb_bswap32(x) __builtin_bswap32 (x)
  970. #define ecb_bswap64(x) __builtin_bswap64 (x)
  971. #elif _MSC_VER
  972. #include <stdlib.h>
  973. #define ecb_bswap16(x) ((uint16_t)_byteswap_ushort ((uint16_t)(x)))
  974. #define ecb_bswap32(x) ((uint32_t)_byteswap_ulong ((uint32_t)(x)))
  975. #define ecb_bswap64(x) ((uint64_t)_byteswap_uint64 ((uint64_t)(x)))
  976. #else
  977. ecb_function_ ecb_const uint16_t ecb_bswap16 (uint16_t x);
  978. ecb_function_ ecb_const uint16_t
  979. ecb_bswap16 (uint16_t x)
  980. {
  981. return ecb_rotl16 (x, 8);
  982. }
  983. ecb_function_ ecb_const uint32_t ecb_bswap32 (uint32_t x);
  984. ecb_function_ ecb_const uint32_t
  985. ecb_bswap32 (uint32_t x)
  986. {
  987. return (((uint32_t)ecb_bswap16 (x)) << 16) | ecb_bswap16 (x >> 16);
  988. }
  989. ecb_function_ ecb_const uint64_t ecb_bswap64 (uint64_t x);
  990. ecb_function_ ecb_const uint64_t
  991. ecb_bswap64 (uint64_t x)
  992. {
  993. return (((uint64_t)ecb_bswap32 (x)) << 32) | ecb_bswap32 (x >> 32);
  994. }
  995. #endif
  996. #if ECB_GCC_VERSION(4,5) || ECB_CLANG_BUILTIN(__builtin_unreachable)
  997. #define ecb_unreachable() __builtin_unreachable ()
  998. #else
  999. /* this seems to work fine, but gcc always emits a warning for it :/ */
  1000. ecb_inline ecb_noreturn void ecb_unreachable (void);
  1001. ecb_inline ecb_noreturn void ecb_unreachable (void) { }
  1002. #endif
  1003. /* try to tell the compiler that some condition is definitely true */
  1004. #define ecb_assume(cond) if (!(cond)) ecb_unreachable (); else 0
  1005. ecb_inline ecb_const uint32_t ecb_byteorder_helper (void);
  1006. ecb_inline ecb_const uint32_t
  1007. ecb_byteorder_helper (void)
  1008. {
  1009. /* the union code still generates code under pressure in gcc, */
  1010. /* but less than using pointers, and always seems to */
  1011. /* successfully return a constant. */
  1012. /* the reason why we have this horrible preprocessor mess */
  1013. /* is to avoid it in all cases, at least on common architectures */
  1014. /* or when using a recent enough gcc version (>= 4.6) */
  1015. #if (defined __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) \
  1016. || ((__i386 || __i386__ || _M_IX86 || ECB_GCC_AMD64 || ECB_MSVC_AMD64) && !__VOS__)
  1017. #define ECB_LITTLE_ENDIAN 1
  1018. return 0x44332211;
  1019. #elif (defined __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) \
  1020. || ((__AARCH64EB__ || __MIPSEB__ || __ARMEB__) && !__VOS__)
  1021. #define ECB_BIG_ENDIAN 1
  1022. return 0x11223344;
  1023. #else
  1024. union
  1025. {
  1026. uint8_t c[4];
  1027. uint32_t u;
  1028. } u = { 0x11, 0x22, 0x33, 0x44 };
  1029. return u.u;
  1030. #endif
  1031. }
  1032. ecb_inline ecb_const ecb_bool ecb_big_endian (void);
  1033. ecb_inline ecb_const ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11223344; }
  1034. ecb_inline ecb_const ecb_bool ecb_little_endian (void);
  1035. ecb_inline ecb_const ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44332211; }
  1036. #if ECB_GCC_VERSION(3,0) || ECB_C99
  1037. #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0))
  1038. #else
  1039. #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n)))
  1040. #endif
  1041. #if ECB_CPP
  1042. template<typename T>
  1043. static inline T ecb_div_rd (T val, T div)
  1044. {
  1045. return val < 0 ? - ((-val + div - 1) / div) : (val ) / div;
  1046. }
  1047. template<typename T>
  1048. static inline T ecb_div_ru (T val, T div)
  1049. {
  1050. return val < 0 ? - ((-val ) / div) : (val + div - 1) / div;
  1051. }
  1052. #else
  1053. #define ecb_div_rd(val,div) ((val) < 0 ? - ((-(val) + (div) - 1) / (div)) : ((val) ) / (div))
  1054. #define ecb_div_ru(val,div) ((val) < 0 ? - ((-(val) ) / (div)) : ((val) + (div) - 1) / (div))
  1055. #endif
  1056. #if ecb_cplusplus_does_not_suck
  1057. /* does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) */
  1058. template<typename T, int N>
  1059. static inline int ecb_array_length (const T (&arr)[N])
  1060. {
  1061. return N;
  1062. }
  1063. #else
  1064. #define ecb_array_length(name) (sizeof (name) / sizeof (name [0]))
  1065. #endif
  1066. ecb_function_ ecb_const uint32_t ecb_binary16_to_binary32 (uint32_t x);
  1067. ecb_function_ ecb_const uint32_t
  1068. ecb_binary16_to_binary32 (uint32_t x)
  1069. {
  1070. unsigned int s = (x & 0x8000) << (31 - 15);
  1071. int e = (x >> 10) & 0x001f;
  1072. unsigned int m = x & 0x03ff;
  1073. if (ecb_expect_false (e == 31))
  1074. /* infinity or NaN */
  1075. e = 255 - (127 - 15);
  1076. else if (ecb_expect_false (!e))
  1077. {
  1078. if (ecb_expect_true (!m))
  1079. /* zero, handled by code below by forcing e to 0 */
  1080. e = 0 - (127 - 15);
  1081. else
  1082. {
  1083. /* subnormal, renormalise */
  1084. unsigned int s = 10 - ecb_ld32 (m);
  1085. m = (m << s) & 0x3ff; /* mask implicit bit */
  1086. e -= s - 1;
  1087. }
  1088. }
  1089. /* e and m now are normalised, or zero, (or inf or nan) */
  1090. e += 127 - 15;
  1091. return s | (e << 23) | (m << (23 - 10));
  1092. }
  1093. ecb_function_ ecb_const uint16_t ecb_binary32_to_binary16 (uint32_t x);
  1094. ecb_function_ ecb_const uint16_t
  1095. ecb_binary32_to_binary16 (uint32_t x)
  1096. {
  1097. unsigned int s = (x >> 16) & 0x00008000; /* sign bit, the easy part */
  1098. unsigned int e = ((x >> 23) & 0x000000ff) - (127 - 15); /* the desired exponent */
  1099. unsigned int m = x & 0x007fffff;
  1100. x &= 0x7fffffff;
  1101. /* if it's within range of binary16 normals, use fast path */
  1102. if (ecb_expect_true (0x38800000 <= x && x <= 0x477fefff))
  1103. {
  1104. /* mantissa round-to-even */
  1105. m += 0x00000fff + ((m >> (23 - 10)) & 1);
  1106. /* handle overflow */
  1107. if (ecb_expect_false (m >= 0x00800000))
  1108. {
  1109. m >>= 1;
  1110. e += 1;
  1111. }
  1112. return s | (e << 10) | (m >> (23 - 10));
  1113. }
  1114. /* handle large numbers and infinity */
  1115. if (ecb_expect_true (0x477fefff < x && x <= 0x7f800000))
  1116. return s | 0x7c00;
  1117. /* handle zero, subnormals and small numbers */
  1118. if (ecb_expect_true (x < 0x38800000))
  1119. {
  1120. /* zero */
  1121. if (ecb_expect_true (!x))
  1122. return s;
  1123. /* handle subnormals */
  1124. /* too small, will be zero */
  1125. if (e < (14 - 24)) /* might not be sharp, but is good enough */
  1126. return s;
  1127. m |= 0x00800000; /* make implicit bit explicit */
  1128. /* very tricky - we need to round to the nearest e (+10) bit value */
  1129. {
  1130. unsigned int bits = 14 - e;
  1131. unsigned int half = (1 << (bits - 1)) - 1;
  1132. unsigned int even = (m >> bits) & 1;
  1133. /* if this overflows, we will end up with a normalised number */
  1134. m = (m + half + even) >> bits;
  1135. }
  1136. return s | m;
  1137. }
  1138. /* handle NaNs, preserve leftmost nan bits, but make sure we don't turn them into infinities */
  1139. m >>= 13;
  1140. return s | 0x7c00 | m | !m;
  1141. }
  1142. /*******************************************************************************/
  1143. /* floating point stuff, can be disabled by defining ECB_NO_LIBM */
  1144. /* basically, everything uses "ieee pure-endian" floating point numbers */
  1145. /* the only noteworthy exception is ancient armle, which uses order 43218765 */
  1146. #if 0 \
  1147. || __i386 || __i386__ \
  1148. || ECB_GCC_AMD64 \
  1149. || __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__ \
  1150. || defined __s390__ || defined __s390x__ \
  1151. || defined __mips__ \
  1152. || defined __alpha__ \
  1153. || defined __hppa__ \
  1154. || defined __ia64__ \
  1155. || defined __m68k__ \
  1156. || defined __m88k__ \
  1157. || defined __sh__ \
  1158. || defined _M_IX86 || defined ECB_MSVC_AMD64 || defined _M_IA64 \
  1159. || (defined __arm__ && (defined __ARM_EABI__ || defined __EABI__ || defined __VFP_FP__ || defined _WIN32_WCE || defined __ANDROID__)) \
  1160. || defined __aarch64__
  1161. #define ECB_STDFP 1
  1162. #include <string.h> /* for memcpy */
  1163. #else
  1164. #define ECB_STDFP 0
  1165. #endif
  1166. #ifndef ECB_NO_LIBM
  1167. #include <math.h> /* for frexp*, ldexp*, INFINITY, NAN */
  1168. /* only the oldest of old doesn't have this one. solaris. */
  1169. #ifdef INFINITY
  1170. #define ECB_INFINITY INFINITY
  1171. #else
  1172. #define ECB_INFINITY HUGE_VAL
  1173. #endif
  1174. #ifdef NAN
  1175. #define ECB_NAN NAN
  1176. #else
  1177. #define ECB_NAN ECB_INFINITY
  1178. #endif
  1179. #if ECB_C99 || _XOPEN_VERSION >= 600 || _POSIX_VERSION >= 200112L
  1180. #define ecb_ldexpf(x,e) ldexpf ((x), (e))
  1181. #define ecb_frexpf(x,e) frexpf ((x), (e))
  1182. #else
  1183. #define ecb_ldexpf(x,e) (float) ldexp ((double) (x), (e))
  1184. #define ecb_frexpf(x,e) (float) frexp ((double) (x), (e))
  1185. #endif
  1186. /* convert a float to ieee single/binary32 */
  1187. ecb_function_ ecb_const uint32_t ecb_float_to_binary32 (float x);
  1188. ecb_function_ ecb_const uint32_t
  1189. ecb_float_to_binary32 (float x)
  1190. {
  1191. uint32_t r;
  1192. #if ECB_STDFP
  1193. memcpy (&r, &x, 4);
  1194. #else
  1195. /* slow emulation, works for anything but -0 */
  1196. uint32_t m;
  1197. int e;
  1198. if (x == 0e0f ) return 0x00000000U;
  1199. if (x > +3.40282346638528860e+38f) return 0x7f800000U;
  1200. if (x < -3.40282346638528860e+38f) return 0xff800000U;
  1201. if (x != x ) return 0x7fbfffffU;
  1202. m = ecb_frexpf (x, &e) * 0x1000000U;
  1203. r = m & 0x80000000U;
  1204. if (r)
  1205. m = -m;
  1206. if (e <= -126)
  1207. {
  1208. m &= 0xffffffU;
  1209. m >>= (-125 - e);
  1210. e = -126;
  1211. }
  1212. r |= (e + 126) << 23;
  1213. r |= m & 0x7fffffU;
  1214. #endif
  1215. return r;
  1216. }
  1217. /* converts an ieee single/binary32 to a float */
  1218. ecb_function_ ecb_const float ecb_binary32_to_float (uint32_t x);
  1219. ecb_function_ ecb_const float
  1220. ecb_binary32_to_float (uint32_t x)
  1221. {
  1222. float r;
  1223. #if ECB_STDFP
  1224. memcpy (&r, &x, 4);
  1225. #else
  1226. /* emulation, only works for normals and subnormals and +0 */
  1227. int neg = x >> 31;
  1228. int e = (x >> 23) & 0xffU;
  1229. x &= 0x7fffffU;
  1230. if (e)
  1231. x |= 0x800000U;
  1232. else
  1233. e = 1;
  1234. /* we distrust ldexpf a bit and do the 2**-24 scaling by an extra multiply */
  1235. r = ecb_ldexpf (x * (0.5f / 0x800000U), e - 126);
  1236. r = neg ? -r : r;
  1237. #endif
  1238. return r;
  1239. }
  1240. /* convert a double to ieee double/binary64 */
  1241. ecb_function_ ecb_const uint64_t ecb_double_to_binary64 (double x);
  1242. ecb_function_ ecb_const uint64_t
  1243. ecb_double_to_binary64 (double x)
  1244. {
  1245. uint64_t r;
  1246. #if ECB_STDFP
  1247. memcpy (&r, &x, 8);
  1248. #else
  1249. /* slow emulation, works for anything but -0 */
  1250. uint64_t m;
  1251. int e;
  1252. if (x == 0e0 ) return 0x0000000000000000U;
  1253. if (x > +1.79769313486231470e+308) return 0x7ff0000000000000U;
  1254. if (x < -1.79769313486231470e+308) return 0xfff0000000000000U;
  1255. if (x != x ) return 0X7ff7ffffffffffffU;
  1256. m = frexp (x, &e) * 0x20000000000000U;
  1257. r = m & 0x8000000000000000;;
  1258. if (r)
  1259. m = -m;
  1260. if (e <= -1022)
  1261. {
  1262. m &= 0x1fffffffffffffU;
  1263. m >>= (-1021 - e);
  1264. e = -1022;
  1265. }
  1266. r |= ((uint64_t)(e + 1022)) << 52;
  1267. r |= m & 0xfffffffffffffU;
  1268. #endif
  1269. return r;
  1270. }
  1271. /* converts an ieee double/binary64 to a double */
  1272. ecb_function_ ecb_const double ecb_binary64_to_double (uint64_t x);
  1273. ecb_function_ ecb_const double
  1274. ecb_binary64_to_double (uint64_t x)
  1275. {
  1276. double r;
  1277. #if ECB_STDFP
  1278. memcpy (&r, &x, 8);
  1279. #else
  1280. /* emulation, only works for normals and subnormals and +0 */
  1281. int neg = x >> 63;
  1282. int e = (x >> 52) & 0x7ffU;
  1283. x &= 0xfffffffffffffU;
  1284. if (e)
  1285. x |= 0x10000000000000U;
  1286. else
  1287. e = 1;
  1288. /* we distrust ldexp a bit and do the 2**-53 scaling by an extra multiply */
  1289. r = ldexp (x * (0.5 / 0x10000000000000U), e - 1022);
  1290. r = neg ? -r : r;
  1291. #endif
  1292. return r;
  1293. }
  1294. /* convert a float to ieee half/binary16 */
  1295. ecb_function_ ecb_const uint16_t ecb_float_to_binary16 (float x);
  1296. ecb_function_ ecb_const uint16_t
  1297. ecb_float_to_binary16 (float x)
  1298. {
  1299. return ecb_binary32_to_binary16 (ecb_float_to_binary32 (x));
  1300. }
  1301. /* convert an ieee half/binary16 to float */
  1302. ecb_function_ ecb_const float ecb_binary16_to_float (uint16_t x);
  1303. ecb_function_ ecb_const float
  1304. ecb_binary16_to_float (uint16_t x)
  1305. {
  1306. return ecb_binary32_to_float (ecb_binary16_to_binary32 (x));
  1307. }
  1308. #endif
  1309. #endif
  1310. /* ECB.H END */
  1311. #if ECB_MEMORY_FENCE_NEEDS_PTHREADS
  1312. /* if your architecture doesn't need memory fences, e.g. because it is
  1313. * single-cpu/core, or if you use libev in a project that doesn't use libev
  1314. * from multiple threads, then you can define ECB_AVOID_PTHREADS when compiling
  1315. * libev, in which cases the memory fences become nops.
  1316. * alternatively, you can remove this #error and link against libpthread,
  1317. * which will then provide the memory fences.
  1318. */
  1319. # error "memory fences not defined for your architecture, please report"
  1320. #endif
  1321. #ifndef ECB_MEMORY_FENCE
  1322. # define ECB_MEMORY_FENCE do { } while (0)
  1323. # define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE
  1324. # define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE
  1325. #endif
  1326. #define expect_false(cond) ecb_expect_false (cond)
  1327. #define expect_true(cond) ecb_expect_true (cond)
  1328. #define noinline ecb_noinline
  1329. #define inline_size ecb_inline
  1330. #if EV_FEATURE_CODE
  1331. # define inline_speed ecb_inline
  1332. #else
  1333. # define inline_speed noinline static
  1334. #endif
  1335. #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
  1336. #if EV_MINPRI == EV_MAXPRI
  1337. # define ABSPRI(w) (((W)w), 0)
  1338. #else
  1339. # define ABSPRI(w) (((W)w)->priority - EV_MINPRI)
  1340. #endif
  1341. #define EMPTY /* required for microsofts broken pseudo-c compiler */
  1342. #define EMPTY2(a,b) /* used to suppress some warnings */
  1343. typedef ev_watcher *W;
  1344. typedef ev_watcher_list *WL;
  1345. typedef ev_watcher_time *WT;
  1346. #define ev_active(w) ((W)(w))->active
  1347. #define ev_at(w) ((WT)(w))->at
  1348. #if EV_USE_REALTIME
  1349. /* sig_atomic_t is used to avoid per-thread variables or locking but still */
  1350. /* giving it a reasonably high chance of working on typical architectures */
  1351. static EV_ATOMIC_T have_realtime; /* did clock_gettime (CLOCK_REALTIME) work? */
  1352. #endif
  1353. #if EV_USE_MONOTONIC
  1354. static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
  1355. #endif
  1356. #ifndef EV_FD_TO_WIN32_HANDLE
  1357. # define EV_FD_TO_WIN32_HANDLE(fd) _get_osfhandle (fd)
  1358. #endif
  1359. #ifndef EV_WIN32_HANDLE_TO_FD
  1360. # define EV_WIN32_HANDLE_TO_FD(handle) _open_osfhandle (handle, 0)
  1361. #endif
  1362. #ifndef EV_WIN32_CLOSE_FD
  1363. # define EV_WIN32_CLOSE_FD(fd) close (fd)
  1364. #endif
  1365. #ifdef _WIN32
  1366. # include "ev_win32.c"
  1367. #endif
  1368. /*****************************************************************************/
  1369. /* define a suitable floor function (only used by periodics atm) */
  1370. #if EV_USE_FLOOR
  1371. # include <math.h>
  1372. # define ev_floor(v) floor (v)
  1373. #else
  1374. #include <float.h>
  1375. /* a floor() replacement function, should be independent of ev_tstamp type */
  1376. noinline
  1377. static ev_tstamp
  1378. ev_floor (ev_tstamp v)
  1379. {
  1380. /* the choice of shift factor is not terribly important */
  1381. #if FLT_RADIX != 2 /* assume FLT_RADIX == 10 */
  1382. const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 10000000000000000000. : 1000000000.;
  1383. #else
  1384. const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 18446744073709551616. : 4294967296.;
  1385. #endif
  1386. /* argument too large for an unsigned long? */
  1387. if (expect_false (v >= shift))
  1388. {
  1389. ev_tstamp f;
  1390. if (v == v - 1.)
  1391. return v; /* very large number */
  1392. f = shift * ev_floor (v * (1. / shift));
  1393. return f + ev_floor (v - f);
  1394. }
  1395. /* special treatment for negative args? */
  1396. if (expect_false (v < 0.))
  1397. {
  1398. ev_tstamp f = -ev_floor (-v);
  1399. return f - (f == v ? 0 : 1);
  1400. }
  1401. /* fits into an unsigned long */
  1402. return (unsigned long)v;
  1403. }
  1404. #endif
  1405. /*****************************************************************************/
  1406. #ifdef __linux
  1407. # include <sys/utsname.h>
  1408. #endif
  1409. noinline ecb_cold
  1410. static unsigned int
  1411. ev_linux_version (void)
  1412. {
  1413. #ifdef __linux
  1414. unsigned int v = 0;
  1415. struct utsname buf;
  1416. int i;
  1417. char *p = buf.release;
  1418. if (uname (&buf))
  1419. return 0;
  1420. for (i = 3+1; --i; )
  1421. {
  1422. unsigned int c = 0;
  1423. for (;;)
  1424. {
  1425. if (*p >= '0' && *p <= '9')
  1426. c = c * 10 + *p++ - '0';
  1427. else
  1428. {
  1429. p += *p == '.';
  1430. break;
  1431. }
  1432. }
  1433. v = (v << 8) | c;
  1434. }
  1435. return v;
  1436. #else
  1437. return 0;
  1438. #endif
  1439. }
  1440. /*****************************************************************************/
  1441. #if EV_AVOID_STDIO
  1442. noinline ecb_cold
  1443. static void
  1444. ev_printerr (const char *msg)
  1445. {
  1446. write (STDERR_FILENO, msg, strlen (msg));
  1447. }
  1448. #endif
  1449. static void (*syserr_cb)(const char *msg) EV_NOEXCEPT;
  1450. ecb_cold
  1451. void
  1452. ev_set_syserr_cb (void (*cb)(const char *msg) EV_NOEXCEPT) EV_NOEXCEPT
  1453. {
  1454. syserr_cb = cb;
  1455. }
  1456. noinline ecb_cold
  1457. static void
  1458. ev_syserr (const char *msg)
  1459. {
  1460. if (!msg)
  1461. msg = "(libev) system error";
  1462. if (syserr_cb)
  1463. syserr_cb (msg);
  1464. else
  1465. {
  1466. #if EV_AVOID_STDIO
  1467. ev_printerr (msg);
  1468. ev_printerr (": ");
  1469. ev_printerr (strerror (errno));
  1470. ev_printerr ("\n");
  1471. #else
  1472. perror (msg);
  1473. #endif
  1474. abort ();
  1475. }
  1476. }
  1477. static void *
  1478. ev_realloc_emul (void *ptr, long size) EV_NOEXCEPT
  1479. {
  1480. /* some systems, notably openbsd and darwin, fail to properly
  1481. * implement realloc (x, 0) (as required by both ansi c-89 and
  1482. * the single unix specification, so work around them here.
  1483. * recently, also (at least) fedora and debian started breaking it,
  1484. * despite documenting it otherwise.
  1485. */
  1486. if (size)
  1487. return realloc (ptr, size);
  1488. free (ptr);
  1489. return 0;
  1490. }
  1491. static void *(*alloc)(void *ptr, long size) EV_NOEXCEPT = ev_realloc_emul;
  1492. ecb_cold
  1493. void
  1494. ev_set_allocator (void *(*cb)(void *ptr, long size) EV_NOEXCEPT) EV_NOEXCEPT
  1495. {
  1496. alloc = cb;
  1497. }
  1498. inline_speed void *
  1499. ev_realloc (void *ptr, long size)
  1500. {
  1501. ptr = alloc (ptr, size);
  1502. if (!ptr && size)
  1503. {
  1504. #if EV_AVOID_STDIO
  1505. ev_printerr ("(libev) memory allocation failed, aborting.\n");
  1506. #else
  1507. fprintf (stderr, "(libev) cannot allocate %ld bytes, aborting.", size);
  1508. #endif
  1509. abort ();
  1510. }
  1511. return ptr;
  1512. }
  1513. #define ev_malloc(size) ev_realloc (0, (size))
  1514. #define ev_free(ptr) ev_realloc ((ptr), 0)
  1515. /*****************************************************************************/
  1516. /* set in reify when reification needed */
  1517. #define EV_ANFD_REIFY 1
  1518. /* file descriptor info structure */
  1519. typedef struct
  1520. {
  1521. WL head;
  1522. unsigned char events; /* the events watched for */
  1523. unsigned char reify; /* flag set when this ANFD needs reification (EV_ANFD_REIFY, EV__IOFDSET) */
  1524. unsigned char emask; /* the epoll backend stores the actual kernel mask in here */
  1525. unsigned char unused;
  1526. #if EV_USE_EPOLL
  1527. unsigned int egen; /* generation counter to counter epoll bugs */
  1528. #endif
  1529. #if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP
  1530. SOCKET handle;
  1531. #endif
  1532. #if EV_USE_IOCP
  1533. OVERLAPPED or, ow;
  1534. #endif
  1535. } ANFD;
  1536. /* stores the pending event set for a given watcher */
  1537. typedef struct
  1538. {
  1539. W w;
  1540. int events; /* the pending event set for the given watcher */
  1541. } ANPENDING;
  1542. #if EV_USE_INOTIFY
  1543. /* hash table entry per inotify-id */
  1544. typedef struct
  1545. {
  1546. WL head;
  1547. } ANFS;
  1548. #endif
  1549. /* Heap Entry */
  1550. #if EV_HEAP_CACHE_AT
  1551. /* a heap element */
  1552. typedef struct {
  1553. ev_tstamp at;
  1554. WT w;
  1555. } ANHE;
  1556. #define ANHE_w(he) (he).w /* access watcher, read-write */
  1557. #define ANHE_at(he) (he).at /* access cached at, read-only */
  1558. #define ANHE_at_cache(he) (he).at = (he).w->at /* update at from watcher */
  1559. #else
  1560. /* a heap element */
  1561. typedef WT ANHE;
  1562. #define ANHE_w(he) (he)
  1563. #define ANHE_at(he) (he)->at
  1564. #define ANHE_at_cache(he)
  1565. #endif
  1566. #if EV_MULTIPLICITY
  1567. struct ev_loop
  1568. {
  1569. ev_tstamp ev_rt_now;
  1570. #define ev_rt_now ((loop)->ev_rt_now)
  1571. #define VAR(name,decl) decl;
  1572. #include "ev_vars.h"
  1573. #undef VAR
  1574. };
  1575. #include "ev_wrap.h"
  1576. static struct ev_loop default_loop_struct;
  1577. static struct ev_loop *ev_default_loop_ptr = 0;
  1578. #else
  1579. EV_API_DECL ev_tstamp ev_rt_now = 0; /* needs to be initialised to make it a definition despite extern */
  1580. #define VAR(name,decl) static decl;
  1581. #include "ev_vars.h"
  1582. #undef VAR
  1583. static int ev_default_loop_ptr;
  1584. #endif
  1585. #if EV_FEATURE_API
  1586. # define EV_RELEASE_CB if (expect_false (release_cb)) release_cb (EV_A)
  1587. # define EV_ACQUIRE_CB if (expect_false (acquire_cb)) acquire_cb (EV_A)
  1588. # define EV_INVOKE_PENDING invoke_cb (EV_A)
  1589. #else
  1590. # define EV_RELEASE_CB (void)0
  1591. # define EV_ACQUIRE_CB (void)0
  1592. # define EV_INVOKE_PENDING ev_invoke_pending (EV_A)
  1593. #endif
  1594. #define EVBREAK_RECURSE 0x80
  1595. /*****************************************************************************/
  1596. #ifndef EV_HAVE_EV_TIME
  1597. ev_tstamp
  1598. ev_time (void) EV_NOEXCEPT
  1599. {
  1600. #if EV_USE_REALTIME
  1601. if (expect_true (have_realtime))
  1602. {
  1603. struct timespec ts;
  1604. clock_gettime (CLOCK_REALTIME, &ts);
  1605. return ts.tv_sec + ts.tv_nsec * 1e-9;
  1606. }
  1607. #endif
  1608. struct timeval tv;
  1609. gettimeofday (&tv, 0);
  1610. return tv.tv_sec + tv.tv_usec * 1e-6;
  1611. }
  1612. #endif
  1613. inline_size ev_tstamp
  1614. get_clock (void)
  1615. {
  1616. #if EV_USE_MONOTONIC
  1617. if (expect_true (have_monotonic))
  1618. {
  1619. struct timespec ts;
  1620. clock_gettime (CLOCK_MONOTONIC, &ts);
  1621. return ts.tv_sec + ts.tv_nsec * 1e-9;
  1622. }
  1623. #endif
  1624. return ev_time ();
  1625. }
  1626. #if EV_MULTIPLICITY
  1627. ev_tstamp
  1628. ev_now (EV_P) EV_NOEXCEPT
  1629. {
  1630. return ev_rt_now;
  1631. }
  1632. #endif
  1633. void
  1634. ev_sleep (ev_tstamp delay) EV_NOEXCEPT
  1635. {
  1636. if (delay > 0.)
  1637. {
  1638. #if EV_USE_NANOSLEEP
  1639. struct timespec ts;
  1640. EV_TS_SET (ts, delay);
  1641. nanosleep (&ts, 0);
  1642. #elif defined _WIN32
  1643. /* maybe this should round up, as ms is very low resolution */
  1644. /* compared to select (µs) or nanosleep (ns) */
  1645. Sleep ((unsigned long)(delay * 1e3));
  1646. #else
  1647. struct timeval tv;
  1648. /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */
  1649. /* something not guaranteed by newer posix versions, but guaranteed */
  1650. /* by older ones */
  1651. EV_TV_SET (tv, delay);
  1652. select (0, 0, 0, 0, &tv);
  1653. #endif
  1654. }
  1655. }
  1656. /*****************************************************************************/
  1657. #define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */
  1658. /* find a suitable new size for the given array, */
  1659. /* hopefully by rounding to a nice-to-malloc size */
  1660. inline_size int
  1661. array_nextsize (int elem, int cur, int cnt)
  1662. {
  1663. int ncur = cur + 1;
  1664. do
  1665. ncur <<= 1;
  1666. while (cnt > ncur);
  1667. /* if size is large, round to MALLOC_ROUND - 4 * longs to accommodate malloc overhead */
  1668. if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4)
  1669. {
  1670. ncur *= elem;
  1671. ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1);
  1672. ncur = ncur - sizeof (void *) * 4;
  1673. ncur /= elem;
  1674. }
  1675. return ncur;
  1676. }
  1677. noinline ecb_cold
  1678. static void *
  1679. array_realloc (int elem, void *base, int *cur, int cnt)
  1680. {
  1681. *cur = array_nextsize (elem, *cur, cnt);
  1682. return ev_realloc (base, elem * *cur);
  1683. }
  1684. #define array_init_zero(base,count) \
  1685. memset ((void *)(base), 0, sizeof (*(base)) * (count))
  1686. #define array_needsize(type,base,cur,cnt,init) \
  1687. if (expect_false ((cnt) > (cur))) \
  1688. { \
  1689. ecb_unused int ocur_ = (cur); \
  1690. (base) = (type *)array_realloc \
  1691. (sizeof (type), (base), &(cur), (cnt)); \
  1692. init ((base) + (ocur_), (cur) - ocur_); \
  1693. }
  1694. #if 0
  1695. #define array_slim(type,stem) \
  1696. if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
  1697. { \
  1698. stem ## max = array_roundsize (stem ## cnt >> 1); \
  1699. base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
  1700. fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
  1701. }
  1702. #endif
  1703. #define array_free(stem, idx) \
  1704. ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; stem ## s idx = 0
  1705. /*****************************************************************************/
  1706. /* dummy callback for pending events */
  1707. noinline
  1708. static void
  1709. pendingcb (EV_P_ ev_prepare *w, int revents)
  1710. {
  1711. }
  1712. noinline
  1713. void
  1714. ev_feed_event (EV_P_ void *w, int revents) EV_NOEXCEPT
  1715. {
  1716. W w_ = (W)w;
  1717. int pri = ABSPRI (w_);
  1718. if (expect_false (w_->pending))
  1719. pendings [pri][w_->pending - 1].events |= revents;
  1720. else
  1721. {
  1722. w_->pending = ++pendingcnt [pri];
  1723. array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2);
  1724. pendings [pri][w_->pending - 1].w = w_;
  1725. pendings [pri][w_->pending - 1].events = revents;
  1726. }
  1727. pendingpri = NUMPRI - 1;
  1728. }
  1729. inline_speed void
  1730. feed_reverse (EV_P_ W w)
  1731. {
  1732. array_needsize (W, rfeeds, rfeedmax, rfeedcnt + 1, EMPTY2);
  1733. rfeeds [rfeedcnt++] = w;
  1734. }
  1735. inline_size void
  1736. feed_reverse_done (EV_P_ int revents)
  1737. {
  1738. do
  1739. ev_feed_event (EV_A_ rfeeds [--rfeedcnt], revents);
  1740. while (rfeedcnt);
  1741. }
  1742. inline_speed void
  1743. queue_events (EV_P_ W *events, int eventcnt, int type)
  1744. {
  1745. int i;
  1746. for (i = 0; i < eventcnt; ++i)
  1747. ev_feed_event (EV_A_ events [i], type);
  1748. }
  1749. /*****************************************************************************/
  1750. inline_speed void
  1751. fd_event_nocheck (EV_P_ int fd, int revents)
  1752. {
  1753. ANFD *anfd = anfds + fd;
  1754. ev_io *w;
  1755. for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
  1756. {
  1757. int ev = w->events & revents;
  1758. if (ev)
  1759. ev_feed_event (EV_A_ (W)w, ev);
  1760. }
  1761. }
  1762. /* do not submit kernel events for fds that have reify set */
  1763. /* because that means they changed while we were polling for new events */
  1764. inline_speed void
  1765. fd_event (EV_P_ int fd, int revents)
  1766. {
  1767. ANFD *anfd = anfds + fd;
  1768. if (expect_true (!anfd->reify))
  1769. fd_event_nocheck (EV_A_ fd, revents);
  1770. }
  1771. void
  1772. ev_feed_fd_event (EV_P_ int fd, int revents) EV_NOEXCEPT
  1773. {
  1774. if (fd >= 0 && fd < anfdmax)
  1775. fd_event_nocheck (EV_A_ fd, revents);
  1776. }
  1777. /* make sure the external fd watch events are in-sync */
  1778. /* with the kernel/libev internal state */
  1779. inline_size void
  1780. fd_reify (EV_P)
  1781. {
  1782. int i;
  1783. #if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP
  1784. for (i = 0; i < fdchangecnt; ++i)
  1785. {
  1786. int fd = fdchanges [i];
  1787. ANFD *anfd = anfds + fd;
  1788. if (anfd->reify & EV__IOFDSET && anfd->head)
  1789. {
  1790. SOCKET handle = EV_FD_TO_WIN32_HANDLE (fd);
  1791. if (handle != anfd->handle)
  1792. {
  1793. unsigned long arg;
  1794. assert (("libev: only socket fds supported in this configuration", ioctlsocket (handle, FIONREAD, &arg) == 0));
  1795. /* handle changed, but fd didn't - we need to do it in two steps */
  1796. backend_modify (EV_A_ fd, anfd->events, 0);
  1797. anfd->events = 0;
  1798. anfd->handle = handle;
  1799. }
  1800. }
  1801. }
  1802. #endif
  1803. for (i = 0; i < fdchangecnt; ++i)
  1804. {
  1805. int fd = fdchanges [i];
  1806. ANFD *anfd = anfds + fd;
  1807. ev_io *w;
  1808. unsigned char o_events = anfd->events;
  1809. unsigned char o_reify = anfd->reify;
  1810. anfd->reify = 0;
  1811. /*if (expect_true (o_reify & EV_ANFD_REIFY)) probably a deoptimisation */
  1812. {
  1813. anfd->events = 0;
  1814. for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
  1815. anfd->events |= (unsigned char)w->events;
  1816. if (o_events != anfd->events)
  1817. o_reify = EV__IOFDSET; /* actually |= */
  1818. }
  1819. if (o_reify & EV__IOFDSET)
  1820. backend_modify (EV_A_ fd, o_events, anfd->events);
  1821. }
  1822. fdchangecnt = 0;
  1823. }
  1824. /* something about the given fd changed */
  1825. inline_size
  1826. void
  1827. fd_change (EV_P_ int fd, int flags)
  1828. {
  1829. unsigned char reify = anfds [fd].reify;
  1830. anfds [fd].reify |= flags;
  1831. if (expect_true (!reify))
  1832. {
  1833. ++fdchangecnt;
  1834. array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
  1835. fdchanges [fdchangecnt - 1] = fd;
  1836. }
  1837. }
  1838. /* the given fd is invalid/unusable, so make sure it doesn't hurt us anymore */
  1839. inline_speed ecb_cold void
  1840. fd_kill (EV_P_ int fd)
  1841. {
  1842. ev_io *w;
  1843. while ((w = (ev_io *)anfds [fd].head))
  1844. {
  1845. ev_io_stop (EV_A_ w);
  1846. ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
  1847. }
  1848. }
  1849. /* check whether the given fd is actually valid, for error recovery */
  1850. inline_size ecb_cold int
  1851. fd_valid (int fd)
  1852. {
  1853. #ifdef _WIN32
  1854. return EV_FD_TO_WIN32_HANDLE (fd) != -1;
  1855. #else
  1856. return fcntl (fd, F_GETFD) != -1;
  1857. #endif
  1858. }
  1859. /* called on EBADF to verify fds */
  1860. noinline ecb_cold
  1861. static void
  1862. fd_ebadf (EV_P)
  1863. {
  1864. int fd;
  1865. for (fd = 0; fd < anfdmax; ++fd)
  1866. if (anfds [fd].events)
  1867. if (!fd_valid (fd) && errno == EBADF)
  1868. fd_kill (EV_A_ fd);
  1869. }
  1870. /* called on ENOMEM in select/poll to kill some fds and retry */
  1871. noinline ecb_cold
  1872. static void
  1873. fd_enomem (EV_P)
  1874. {
  1875. int fd;
  1876. for (fd = anfdmax; fd--; )
  1877. if (anfds [fd].events)
  1878. {
  1879. fd_kill (EV_A_ fd);
  1880. break;
  1881. }
  1882. }
  1883. /* usually called after fork if backend needs to re-arm all fds from scratch */
  1884. noinline
  1885. static void
  1886. fd_rearm_all (EV_P)
  1887. {
  1888. int fd;
  1889. for (fd = 0; fd < anfdmax; ++fd)
  1890. if (anfds [fd].events)
  1891. {
  1892. anfds [fd].events = 0;
  1893. anfds [fd].emask = 0;
  1894. fd_change (EV_A_ fd, EV__IOFDSET | EV_ANFD_REIFY);
  1895. }
  1896. }
  1897. /* used to prepare libev internal fd's */
  1898. /* this is not fork-safe */
  1899. inline_speed void
  1900. fd_intern (int fd)
  1901. {
  1902. #ifdef _WIN32
  1903. unsigned long arg = 1;
  1904. ioctlsocket (EV_FD_TO_WIN32_HANDLE (fd), FIONBIO, &arg);
  1905. #else
  1906. fcntl (fd, F_SETFD, FD_CLOEXEC);
  1907. fcntl (fd, F_SETFL, O_NONBLOCK);
  1908. #endif
  1909. }
  1910. /*****************************************************************************/
  1911. /*
  1912. * the heap functions want a real array index. array index 0 is guaranteed to not
  1913. * be in-use at any time. the first heap entry is at array [HEAP0]. DHEAP gives
  1914. * the branching factor of the d-tree.
  1915. */
  1916. /*
  1917. * at the moment we allow libev the luxury of two heaps,
  1918. * a small-code-size 2-heap one and a ~1.5kb larger 4-heap
  1919. * which is more cache-efficient.
  1920. * the difference is about 5% with 50000+ watchers.
  1921. */
  1922. #if EV_USE_4HEAP
  1923. #define DHEAP 4
  1924. #define HEAP0 (DHEAP - 1) /* index of first element in heap */
  1925. #define HPARENT(k) ((((k) - HEAP0 - 1) / DHEAP) + HEAP0)
  1926. #define UPHEAP_DONE(p,k) ((p) == (k))
  1927. /* away from the root */
  1928. inline_speed void
  1929. downheap (ANHE *heap, int N, int k)
  1930. {
  1931. ANHE he = heap [k];
  1932. ANHE *E = heap + N + HEAP0;
  1933. for (;;)
  1934. {
  1935. ev_tstamp minat;
  1936. ANHE *minpos;
  1937. ANHE *pos = heap + DHEAP * (k - HEAP0) + HEAP0 + 1;
  1938. /* find minimum child */
  1939. if (expect_true (pos + DHEAP - 1 < E))
  1940. {
  1941. /* fast path */ (minpos = pos + 0), (minat = ANHE_at (*minpos));
  1942. if ( ANHE_at (pos [1]) < minat) (minpos = pos + 1), (minat = ANHE_at (*minpos));
  1943. if ( ANHE_at (pos [2]) < minat) (minpos = pos + 2), (minat = ANHE_at (*minpos));
  1944. if ( ANHE_at (pos [3]) < minat) (minpos = pos + 3), (minat = ANHE_at (*minpos));
  1945. }
  1946. else if (pos < E)
  1947. {
  1948. /* slow path */ (minpos = pos + 0), (minat = ANHE_at (*minpos));
  1949. if (pos + 1 < E && ANHE_at (pos [1]) < minat) (minpos = pos + 1), (minat = ANHE_at (*minpos));
  1950. if (pos + 2 < E && ANHE_at (pos [2]) < minat) (minpos = pos + 2), (minat = ANHE_at (*minpos));
  1951. if (pos + 3 < E && ANHE_at (pos [3]) < minat) (minpos = pos + 3), (minat = ANHE_at (*minpos));
  1952. }
  1953. else
  1954. break;
  1955. if (ANHE_at (he) <= minat)
  1956. break;
  1957. heap [k] = *minpos;
  1958. ev_active (ANHE_w (*minpos)) = k;
  1959. k = minpos - heap;
  1960. }
  1961. heap [k] = he;
  1962. ev_active (ANHE_w (he)) = k;
  1963. }
  1964. #else /* 4HEAP */
  1965. #define HEAP0 1
  1966. #define HPARENT(k) ((k) >> 1)
  1967. #define UPHEAP_DONE(p,k) (!(p))
  1968. /* away from the root */
  1969. inline_speed void
  1970. downheap (ANHE *heap, int N, int k)
  1971. {
  1972. ANHE he = heap [k];
  1973. for (;;)
  1974. {
  1975. int c = k << 1;
  1976. if (c >= N + HEAP0)
  1977. break;
  1978. c += c + 1 < N + HEAP0 && ANHE_at (heap [c]) > ANHE_at (heap [c + 1])
  1979. ? 1 : 0;
  1980. if (ANHE_at (he) <= ANHE_at (heap [c]))
  1981. break;
  1982. heap [k] = heap [c];
  1983. ev_active (ANHE_w (heap [k])) = k;
  1984. k = c;
  1985. }
  1986. heap [k] = he;
  1987. ev_active (ANHE_w (he)) = k;
  1988. }
  1989. #endif
  1990. /* towards the root */
  1991. inline_speed void
  1992. upheap (ANHE *heap, int k)
  1993. {
  1994. ANHE he = heap [k];
  1995. for (;;)
  1996. {
  1997. int p = HPARENT (k);
  1998. if (UPHEAP_DONE (p, k) || ANHE_at (heap [p]) <= ANHE_at (he))
  1999. break;
  2000. heap [k] = heap [p];
  2001. ev_active (ANHE_w (heap [k])) = k;
  2002. k = p;
  2003. }
  2004. heap [k] = he;
  2005. ev_active (ANHE_w (he)) = k;
  2006. }
  2007. /* move an element suitably so it is in a correct place */
  2008. inline_size void
  2009. adjustheap (ANHE *heap, int N, int k)
  2010. {
  2011. if (k > HEAP0 && ANHE_at (heap [k]) <= ANHE_at (heap [HPARENT (k)]))
  2012. upheap (heap, k);
  2013. else
  2014. downheap (heap, N, k);
  2015. }
  2016. /* rebuild the heap: this function is used only once and executed rarely */
  2017. inline_size void
  2018. reheap (ANHE *heap, int N)
  2019. {
  2020. int i;
  2021. /* we don't use floyds algorithm, upheap is simpler and is more cache-efficient */
  2022. /* also, this is easy to implement and correct for both 2-heaps and 4-heaps */
  2023. for (i = 0; i < N; ++i)
  2024. upheap (heap, i + HEAP0);
  2025. }
  2026. /*****************************************************************************/
  2027. /* associate signal watchers to a signal signal */
  2028. typedef struct
  2029. {
  2030. EV_ATOMIC_T pending;
  2031. #if EV_MULTIPLICITY
  2032. EV_P;
  2033. #endif
  2034. WL head;
  2035. } ANSIG;
  2036. static ANSIG signals [EV_NSIG - 1];
  2037. /*****************************************************************************/
  2038. #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
  2039. noinline ecb_cold
  2040. static void
  2041. evpipe_init (EV_P)
  2042. {
  2043. if (!ev_is_active (&pipe_w))
  2044. {
  2045. int fds [2];
  2046. # if EV_USE_EVENTFD
  2047. fds [0] = -1;
  2048. fds [1] = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
  2049. if (fds [1] < 0 && errno == EINVAL)
  2050. fds [1] = eventfd (0, 0);
  2051. if (fds [1] < 0)
  2052. # endif
  2053. {
  2054. while (pipe (fds))
  2055. ev_syserr ("(libev) error creating signal/async pipe");
  2056. fd_intern (fds [0]);
  2057. }
  2058. evpipe [0] = fds [0];
  2059. if (evpipe [1] < 0)
  2060. evpipe [1] = fds [1]; /* first call, set write fd */
  2061. else
  2062. {
  2063. /* on subsequent calls, do not change evpipe [1] */
  2064. /* so that evpipe_write can always rely on its value. */
  2065. /* this branch does not do anything sensible on windows, */
  2066. /* so must not be executed on windows */
  2067. dup2 (fds [1], evpipe [1]);
  2068. close (fds [1]);
  2069. }
  2070. fd_intern (evpipe [1]);
  2071. ev_io_set (&pipe_w, evpipe [0] < 0 ? evpipe [1] : evpipe [0], EV_READ);
  2072. ev_io_start (EV_A_ &pipe_w);
  2073. ev_unref (EV_A); /* watcher should not keep loop alive */
  2074. }
  2075. }
  2076. inline_speed void
  2077. evpipe_write (EV_P_ EV_ATOMIC_T *flag)
  2078. {
  2079. ECB_MEMORY_FENCE; /* push out the write before this function was called, acquire flag */
  2080. if (expect_true (*flag))
  2081. return;
  2082. *flag = 1;
  2083. ECB_MEMORY_FENCE_RELEASE; /* make sure flag is visible before the wakeup */
  2084. pipe_write_skipped = 1;
  2085. ECB_MEMORY_FENCE; /* make sure pipe_write_skipped is visible before we check pipe_write_wanted */
  2086. if (pipe_write_wanted)
  2087. {
  2088. int old_errno;
  2089. pipe_write_skipped = 0;
  2090. ECB_MEMORY_FENCE_RELEASE;
  2091. old_errno = errno; /* save errno because write will clobber it */
  2092. #if EV_USE_EVENTFD
  2093. if (evpipe [0] < 0)
  2094. {
  2095. uint64_t counter = 1;
  2096. write (evpipe [1], &counter, sizeof (uint64_t));
  2097. }
  2098. else
  2099. #endif
  2100. {
  2101. #ifdef _WIN32
  2102. WSABUF buf;
  2103. DWORD sent;
  2104. buf.buf = (char *)&buf;
  2105. buf.len = 1;
  2106. WSASend (EV_FD_TO_WIN32_HANDLE (evpipe [1]), &buf, 1, &sent, 0, 0, 0);
  2107. #else
  2108. write (evpipe [1], &(evpipe [1]), 1);
  2109. #endif
  2110. }
  2111. errno = old_errno;
  2112. }
  2113. }
  2114. /* called whenever the libev signal pipe */
  2115. /* got some events (signal, async) */
  2116. static void
  2117. pipecb (EV_P_ ev_io *iow, int revents)
  2118. {
  2119. int i;
  2120. if (revents & EV_READ)
  2121. {
  2122. #if EV_USE_EVENTFD
  2123. if (evpipe [0] < 0)
  2124. {
  2125. uint64_t counter;
  2126. read (evpipe [1], &counter, sizeof (uint64_t));
  2127. }
  2128. else
  2129. #endif
  2130. {
  2131. char dummy[4];
  2132. #ifdef _WIN32
  2133. WSABUF buf;
  2134. DWORD recvd;
  2135. DWORD flags = 0;
  2136. buf.buf = dummy;
  2137. buf.len = sizeof (dummy);
  2138. WSARecv (EV_FD_TO_WIN32_HANDLE (evpipe [0]), &buf, 1, &recvd, &flags, 0, 0);
  2139. #else
  2140. read (evpipe [0], &dummy, sizeof (dummy));
  2141. #endif
  2142. }
  2143. }
  2144. pipe_write_skipped = 0;
  2145. ECB_MEMORY_FENCE; /* push out skipped, acquire flags */
  2146. #if EV_SIGNAL_ENABLE
  2147. if (sig_pending)
  2148. {
  2149. sig_pending = 0;
  2150. ECB_MEMORY_FENCE;
  2151. for (i = EV_NSIG - 1; i--; )
  2152. if (expect_false (signals [i].pending))
  2153. ev_feed_signal_event (EV_A_ i + 1);
  2154. }
  2155. #endif
  2156. #if EV_ASYNC_ENABLE
  2157. if (async_pending)
  2158. {
  2159. async_pending = 0;
  2160. ECB_MEMORY_FENCE;
  2161. for (i = asynccnt; i--; )
  2162. if (asyncs [i]->sent)
  2163. {
  2164. asyncs [i]->sent = 0;
  2165. ECB_MEMORY_FENCE_RELEASE;
  2166. ev_feed_event (EV_A_ asyncs [i], EV_ASYNC);
  2167. }
  2168. }
  2169. #endif
  2170. }
  2171. /*****************************************************************************/
  2172. void
  2173. ev_feed_signal (int signum) EV_NOEXCEPT
  2174. {
  2175. #if EV_MULTIPLICITY
  2176. EV_P;
  2177. ECB_MEMORY_FENCE_ACQUIRE;
  2178. EV_A = signals [signum - 1].loop;
  2179. if (!EV_A)
  2180. return;
  2181. #endif
  2182. signals [signum - 1].pending = 1;
  2183. evpipe_write (EV_A_ &sig_pending);
  2184. }
  2185. static void
  2186. ev_sighandler (int signum)
  2187. {
  2188. #ifdef _WIN32
  2189. signal (signum, ev_sighandler);
  2190. #endif
  2191. ev_feed_signal (signum);
  2192. }
  2193. noinline
  2194. void
  2195. ev_feed_signal_event (EV_P_ int signum) EV_NOEXCEPT
  2196. {
  2197. WL w;
  2198. if (expect_false (signum <= 0 || signum >= EV_NSIG))
  2199. return;
  2200. --signum;
  2201. #if EV_MULTIPLICITY
  2202. /* it is permissible to try to feed a signal to the wrong loop */
  2203. /* or, likely more useful, feeding a signal nobody is waiting for */
  2204. if (expect_false (signals [signum].loop != EV_A))
  2205. return;
  2206. #endif
  2207. signals [signum].pending = 0;
  2208. ECB_MEMORY_FENCE_RELEASE;
  2209. for (w = signals [signum].head; w; w = w->next)
  2210. ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
  2211. }
  2212. #if EV_USE_SIGNALFD
  2213. static void
  2214. sigfdcb (EV_P_ ev_io *iow, int revents)
  2215. {
  2216. struct signalfd_siginfo si[2], *sip; /* these structs are big */
  2217. for (;;)
  2218. {
  2219. ssize_t res = read (sigfd, si, sizeof (si));
  2220. /* not ISO-C, as res might be -1, but works with SuS */
  2221. for (sip = si; (char *)sip < (char *)si + res; ++sip)
  2222. ev_feed_signal_event (EV_A_ sip->ssi_signo);
  2223. if (res < (ssize_t)sizeof (si))
  2224. break;
  2225. }
  2226. }
  2227. #endif
  2228. #endif
  2229. /*****************************************************************************/
  2230. #if EV_CHILD_ENABLE
  2231. static WL childs [EV_PID_HASHSIZE];
  2232. static ev_signal childev;
  2233. #ifndef WIFCONTINUED
  2234. # define WIFCONTINUED(status) 0
  2235. #endif
  2236. /* handle a single child status event */
  2237. inline_speed void
  2238. child_reap (EV_P_ int chain, int pid, int status)
  2239. {
  2240. ev_child *w;
  2241. int traced = WIFSTOPPED (status) || WIFCONTINUED (status);
  2242. for (w = (ev_child *)childs [chain & ((EV_PID_HASHSIZE) - 1)]; w; w = (ev_child *)((WL)w)->next)
  2243. {
  2244. if ((w->pid == pid || !w->pid)
  2245. && (!traced || (w->flags & 1)))
  2246. {
  2247. ev_set_priority (w, EV_MAXPRI); /* need to do it *now*, this *must* be the same prio as the signal watcher itself */
  2248. w->rpid = pid;
  2249. w->rstatus = status;
  2250. ev_feed_event (EV_A_ (W)w, EV_CHILD);
  2251. }
  2252. }
  2253. }
  2254. #ifndef WCONTINUED
  2255. # define WCONTINUED 0
  2256. #endif
  2257. /* called on sigchld etc., calls waitpid */
  2258. static void
  2259. childcb (EV_P_ ev_signal *sw, int revents)
  2260. {
  2261. int pid, status;
  2262. /* some systems define WCONTINUED but then fail to support it (linux 2.4) */
  2263. if (0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
  2264. if (!WCONTINUED
  2265. || errno != EINVAL
  2266. || 0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED)))
  2267. return;
  2268. /* make sure we are called again until all children have been reaped */
  2269. /* we need to do it this way so that the callback gets called before we continue */
  2270. ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
  2271. child_reap (EV_A_ pid, pid, status);
  2272. if ((EV_PID_HASHSIZE) > 1)
  2273. child_reap (EV_A_ 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */
  2274. }
  2275. #endif
  2276. /*****************************************************************************/
  2277. #if EV_USE_IOCP
  2278. # include "ev_iocp.c"
  2279. #endif
  2280. #if EV_USE_PORT
  2281. # include "ev_port.c"
  2282. #endif
  2283. #if EV_USE_KQUEUE
  2284. # include "ev_kqueue.c"
  2285. #endif
  2286. #if EV_USE_EPOLL
  2287. # include "ev_epoll.c"
  2288. #endif
  2289. #if EV_USE_POLL
  2290. # include "ev_poll.c"
  2291. #endif
  2292. #if EV_USE_SELECT
  2293. # include "ev_select.c"
  2294. #endif
  2295. ecb_cold int
  2296. ev_version_major (void) EV_NOEXCEPT
  2297. {
  2298. return EV_VERSION_MAJOR;
  2299. }
  2300. ecb_cold int
  2301. ev_version_minor (void) EV_NOEXCEPT
  2302. {
  2303. return EV_VERSION_MINOR;
  2304. }
  2305. /* return true if we are running with elevated privileges and should ignore env variables */
  2306. inline_size ecb_cold int
  2307. enable_secure (void)
  2308. {
  2309. #ifdef _WIN32
  2310. return 0;
  2311. #else
  2312. return getuid () != geteuid ()
  2313. || getgid () != getegid ();
  2314. #endif
  2315. }
  2316. ecb_cold
  2317. unsigned int
  2318. ev_supported_backends (void) EV_NOEXCEPT
  2319. {
  2320. unsigned int flags = 0;
  2321. if (EV_USE_PORT ) flags |= EVBACKEND_PORT;
  2322. if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE;
  2323. if (EV_USE_EPOLL ) flags |= EVBACKEND_EPOLL;
  2324. if (EV_USE_POLL ) flags |= EVBACKEND_POLL;
  2325. if (EV_USE_SELECT) flags |= EVBACKEND_SELECT;
  2326. return flags;
  2327. }
  2328. ecb_cold
  2329. unsigned int
  2330. ev_recommended_backends (void) EV_NOEXCEPT
  2331. {
  2332. unsigned int flags = ev_supported_backends ();
  2333. #ifndef __NetBSD__
  2334. /* kqueue is borked on everything but netbsd apparently */
  2335. /* it usually doesn't work correctly on anything but sockets and pipes */
  2336. flags &= ~EVBACKEND_KQUEUE;
  2337. #endif
  2338. #ifdef __APPLE__
  2339. /* only select works correctly on that "unix-certified" platform */
  2340. flags &= ~EVBACKEND_KQUEUE; /* horribly broken, even for sockets */
  2341. flags &= ~EVBACKEND_POLL; /* poll is based on kqueue from 10.5 onwards */
  2342. #endif
  2343. #ifdef __FreeBSD__
  2344. flags &= ~EVBACKEND_POLL; /* poll return value is unusable (http://forums.freebsd.org/archive/index.php/t-10270.html) */
  2345. #endif
  2346. return flags;
  2347. }
  2348. ecb_cold
  2349. unsigned int
  2350. ev_embeddable_backends (void) EV_NOEXCEPT
  2351. {
  2352. int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT;
  2353. /* epoll embeddability broken on all linux versions up to at least 2.6.23 */
  2354. if (ev_linux_version () < 0x020620) /* disable it on linux < 2.6.32 */
  2355. flags &= ~EVBACKEND_EPOLL;
  2356. return flags;
  2357. }
  2358. unsigned int
  2359. ev_backend (EV_P) EV_NOEXCEPT
  2360. {
  2361. return backend;
  2362. }
  2363. #if EV_FEATURE_API
  2364. unsigned int
  2365. ev_iteration (EV_P) EV_NOEXCEPT
  2366. {
  2367. return loop_count;
  2368. }
  2369. unsigned int
  2370. ev_depth (EV_P) EV_NOEXCEPT
  2371. {
  2372. return loop_depth;
  2373. }
  2374. void
  2375. ev_set_io_collect_interval (EV_P_ ev_tstamp interval) EV_NOEXCEPT
  2376. {
  2377. io_blocktime = interval;
  2378. }
  2379. void
  2380. ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) EV_NOEXCEPT
  2381. {
  2382. timeout_blocktime = interval;
  2383. }
  2384. void
  2385. ev_set_userdata (EV_P_ void *data) EV_NOEXCEPT
  2386. {
  2387. userdata = data;
  2388. }
  2389. void *
  2390. ev_userdata (EV_P) EV_NOEXCEPT
  2391. {
  2392. return userdata;
  2393. }
  2394. void
  2395. ev_set_invoke_pending_cb (EV_P_ ev_loop_callback invoke_pending_cb) EV_NOEXCEPT
  2396. {
  2397. invoke_cb = invoke_pending_cb;
  2398. }
  2399. void
  2400. ev_set_loop_release_cb (EV_P_ void (*release)(EV_P) EV_NOEXCEPT, void (*acquire)(EV_P) EV_NOEXCEPT) EV_NOEXCEPT
  2401. {
  2402. release_cb = release;
  2403. acquire_cb = acquire;
  2404. }
  2405. #endif
  2406. EV_INLINE struct ev_loop *
  2407. ev_default_loop_uc_ (void) EV_NOEXCEPT
  2408. {
  2409. return ev_default_loop_ptr;
  2410. }
  2411. EV_INLINE int
  2412. ev_is_default_loop (EV_P) EV_NOEXCEPT
  2413. {
  2414. return EV_A == EV_DEFAULT_UC;
  2415. }
  2416. /* initialise a loop structure, must be zero-initialised */
  2417. noinline ecb_cold
  2418. static void
  2419. loop_init (EV_P_ unsigned int flags) EV_NOEXCEPT
  2420. {
  2421. if (!backend)
  2422. {
  2423. origflags = flags;
  2424. #if EV_USE_REALTIME
  2425. if (!have_realtime)
  2426. {
  2427. struct timespec ts;
  2428. if (!clock_gettime (CLOCK_REALTIME, &ts))
  2429. have_realtime = 1;
  2430. }
  2431. #endif
  2432. #if EV_USE_MONOTONIC
  2433. if (!have_monotonic)
  2434. {
  2435. struct timespec ts;
  2436. if (!clock_gettime (CLOCK_MONOTONIC, &ts))
  2437. have_monotonic = 1;
  2438. }
  2439. #endif
  2440. /* pid check not overridable via env */
  2441. #ifndef _WIN32
  2442. if (flags & EVFLAG_FORKCHECK)
  2443. curpid = getpid ();
  2444. #endif
  2445. if (!(flags & EVFLAG_NOENV)
  2446. && !enable_secure ()
  2447. && getenv ("LIBEV_FLAGS"))
  2448. flags = atoi (getenv ("LIBEV_FLAGS"));
  2449. ev_rt_now = ev_time ();
  2450. mn_now = get_clock ();
  2451. now_floor = mn_now;
  2452. rtmn_diff = ev_rt_now - mn_now;
  2453. #if EV_FEATURE_API
  2454. invoke_cb = ev_invoke_pending;
  2455. #endif
  2456. io_blocktime = 0.;
  2457. timeout_blocktime = 0.;
  2458. backend = 0;
  2459. backend_fd = -1;
  2460. sig_pending = 0;
  2461. #if EV_ASYNC_ENABLE
  2462. async_pending = 0;
  2463. #endif
  2464. pipe_write_skipped = 0;
  2465. pipe_write_wanted = 0;
  2466. evpipe [0] = -1;
  2467. evpipe [1] = -1;
  2468. #if EV_USE_INOTIFY
  2469. fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2;
  2470. #endif
  2471. #if EV_USE_SIGNALFD
  2472. sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1;
  2473. #endif
  2474. if (!(flags & EVBACKEND_MASK))
  2475. flags |= ev_recommended_backends ();
  2476. #if EV_USE_IOCP
  2477. if (!backend && (flags & EVBACKEND_IOCP )) backend = iocp_init (EV_A_ flags);
  2478. #endif
  2479. #if EV_USE_PORT
  2480. if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags);
  2481. #endif
  2482. #if EV_USE_KQUEUE
  2483. if (!backend && (flags & EVBACKEND_KQUEUE)) backend = kqueue_init (EV_A_ flags);
  2484. #endif
  2485. #if EV_USE_EPOLL
  2486. if (!backend && (flags & EVBACKEND_EPOLL )) backend = epoll_init (EV_A_ flags);
  2487. #endif
  2488. #if EV_USE_POLL
  2489. if (!backend && (flags & EVBACKEND_POLL )) backend = poll_init (EV_A_ flags);
  2490. #endif
  2491. #if EV_USE_SELECT
  2492. if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags);
  2493. #endif
  2494. ev_prepare_init (&pending_w, pendingcb);
  2495. #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
  2496. ev_init (&pipe_w, pipecb);
  2497. ev_set_priority (&pipe_w, EV_MAXPRI);
  2498. #endif
  2499. }
  2500. }
  2501. /* free up a loop structure */
  2502. ecb_cold
  2503. void
  2504. ev_loop_destroy (EV_P)
  2505. {
  2506. int i;
  2507. #if EV_MULTIPLICITY
  2508. /* mimic free (0) */
  2509. if (!EV_A)
  2510. return;
  2511. #endif
  2512. #if EV_CLEANUP_ENABLE
  2513. /* queue cleanup watchers (and execute them) */
  2514. if (expect_false (cleanupcnt))
  2515. {
  2516. queue_events (EV_A_ (W *)cleanups, cleanupcnt, EV_CLEANUP);
  2517. EV_INVOKE_PENDING;
  2518. }
  2519. #endif
  2520. #if EV_CHILD_ENABLE
  2521. if (ev_is_default_loop (EV_A) && ev_is_active (&childev))
  2522. {
  2523. ev_ref (EV_A); /* child watcher */
  2524. ev_signal_stop (EV_A_ &childev);
  2525. }
  2526. #endif
  2527. if (ev_is_active (&pipe_w))
  2528. {
  2529. /*ev_ref (EV_A);*/
  2530. /*ev_io_stop (EV_A_ &pipe_w);*/
  2531. if (evpipe [0] >= 0) EV_WIN32_CLOSE_FD (evpipe [0]);
  2532. if (evpipe [1] >= 0) EV_WIN32_CLOSE_FD (evpipe [1]);
  2533. }
  2534. #if EV_USE_SIGNALFD
  2535. if (ev_is_active (&sigfd_w))
  2536. close (sigfd);
  2537. #endif
  2538. #if EV_USE_INOTIFY
  2539. if (fs_fd >= 0)
  2540. close (fs_fd);
  2541. #endif
  2542. if (backend_fd >= 0)
  2543. close (backend_fd);
  2544. #if EV_USE_IOCP
  2545. if (backend == EVBACKEND_IOCP ) iocp_destroy (EV_A);
  2546. #endif
  2547. #if EV_USE_PORT
  2548. if (backend == EVBACKEND_PORT ) port_destroy (EV_A);
  2549. #endif
  2550. #if EV_USE_KQUEUE
  2551. if (backend == EVBACKEND_KQUEUE) kqueue_destroy (EV_A);
  2552. #endif
  2553. #if EV_USE_EPOLL
  2554. if (backend == EVBACKEND_EPOLL ) epoll_destroy (EV_A);
  2555. #endif
  2556. #if EV_USE_POLL
  2557. if (backend == EVBACKEND_POLL ) poll_destroy (EV_A);
  2558. #endif
  2559. #if EV_USE_SELECT
  2560. if (backend == EVBACKEND_SELECT) select_destroy (EV_A);
  2561. #endif
  2562. for (i = NUMPRI; i--; )
  2563. {
  2564. array_free (pending, [i]);
  2565. #if EV_IDLE_ENABLE
  2566. array_free (idle, [i]);
  2567. #endif
  2568. }
  2569. ev_free (anfds); anfds = 0; anfdmax = 0;
  2570. /* have to use the microsoft-never-gets-it-right macro */
  2571. array_free (rfeed, EMPTY);
  2572. array_free (fdchange, EMPTY);
  2573. array_free (timer, EMPTY);
  2574. #if EV_PERIODIC_ENABLE
  2575. array_free (periodic, EMPTY);
  2576. #endif
  2577. #if EV_FORK_ENABLE
  2578. array_free (fork, EMPTY);
  2579. #endif
  2580. #if EV_CLEANUP_ENABLE
  2581. array_free (cleanup, EMPTY);
  2582. #endif
  2583. array_free (prepare, EMPTY);
  2584. array_free (check, EMPTY);
  2585. #if EV_ASYNC_ENABLE
  2586. array_free (async, EMPTY);
  2587. #endif
  2588. backend = 0;
  2589. #if EV_MULTIPLICITY
  2590. if (ev_is_default_loop (EV_A))
  2591. #endif
  2592. ev_default_loop_ptr = 0;
  2593. #if EV_MULTIPLICITY
  2594. else
  2595. ev_free (EV_A);
  2596. #endif
  2597. }
  2598. #if EV_USE_INOTIFY
  2599. inline_size void infy_fork (EV_P);
  2600. #endif
  2601. inline_size void
  2602. loop_fork (EV_P)
  2603. {
  2604. #if EV_USE_PORT
  2605. if (backend == EVBACKEND_PORT ) port_fork (EV_A);
  2606. #endif
  2607. #if EV_USE_KQUEUE
  2608. if (backend == EVBACKEND_KQUEUE) kqueue_fork (EV_A);
  2609. #endif
  2610. #if EV_USE_EPOLL
  2611. if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A);
  2612. #endif
  2613. #if EV_USE_INOTIFY
  2614. infy_fork (EV_A);
  2615. #endif
  2616. #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
  2617. if (ev_is_active (&pipe_w) && postfork != 2)
  2618. {
  2619. /* pipe_write_wanted must be false now, so modifying fd vars should be safe */
  2620. ev_ref (EV_A);
  2621. ev_io_stop (EV_A_ &pipe_w);
  2622. if (evpipe [0] >= 0)
  2623. EV_WIN32_CLOSE_FD (evpipe [0]);
  2624. evpipe_init (EV_A);
  2625. /* iterate over everything, in case we missed something before */
  2626. ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM);
  2627. }
  2628. #endif
  2629. postfork = 0;
  2630. }
  2631. #if EV_MULTIPLICITY
  2632. ecb_cold
  2633. struct ev_loop *
  2634. ev_loop_new (unsigned int flags) EV_NOEXCEPT
  2635. {
  2636. EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
  2637. memset (EV_A, 0, sizeof (struct ev_loop));
  2638. loop_init (EV_A_ flags);
  2639. if (ev_backend (EV_A))
  2640. return EV_A;
  2641. ev_free (EV_A);
  2642. return 0;
  2643. }
  2644. #endif /* multiplicity */
  2645. #if EV_VERIFY
  2646. noinline ecb_cold
  2647. static void
  2648. verify_watcher (EV_P_ W w)
  2649. {
  2650. assert (("libev: watcher has invalid priority", ABSPRI (w) >= 0 && ABSPRI (w) < NUMPRI));
  2651. if (w->pending)
  2652. assert (("libev: pending watcher not on pending queue", pendings [ABSPRI (w)][w->pending - 1].w == w));
  2653. }
  2654. noinline ecb_cold
  2655. static void
  2656. verify_heap (EV_P_ ANHE *heap, int N)
  2657. {
  2658. int i;
  2659. for (i = HEAP0; i < N + HEAP0; ++i)
  2660. {
  2661. assert (("libev: active index mismatch in heap", ev_active (ANHE_w (heap [i])) == i));
  2662. assert (("libev: heap condition violated", i == HEAP0 || ANHE_at (heap [HPARENT (i)]) <= ANHE_at (heap [i])));
  2663. assert (("libev: heap at cache mismatch", ANHE_at (heap [i]) == ev_at (ANHE_w (heap [i]))));
  2664. verify_watcher (EV_A_ (W)ANHE_w (heap [i]));
  2665. }
  2666. }
  2667. noinline ecb_cold
  2668. static void
  2669. array_verify (EV_P_ W *ws, int cnt)
  2670. {
  2671. while (cnt--)
  2672. {
  2673. assert (("libev: active index mismatch", ev_active (ws [cnt]) == cnt + 1));
  2674. verify_watcher (EV_A_ ws [cnt]);
  2675. }
  2676. }
  2677. #endif
  2678. #if EV_FEATURE_API
  2679. void ecb_cold
  2680. ev_verify (EV_P) EV_NOEXCEPT
  2681. {
  2682. #if EV_VERIFY
  2683. int i;
  2684. WL w, w2;
  2685. assert (activecnt >= -1);
  2686. assert (fdchangemax >= fdchangecnt);
  2687. for (i = 0; i < fdchangecnt; ++i)
  2688. assert (("libev: negative fd in fdchanges", fdchanges [i] >= 0));
  2689. assert (anfdmax >= 0);
  2690. for (i = 0; i < anfdmax; ++i)
  2691. {
  2692. int j = 0;
  2693. for (w = w2 = anfds [i].head; w; w = w->next)
  2694. {
  2695. verify_watcher (EV_A_ (W)w);
  2696. if (j++ & 1)
  2697. {
  2698. assert (("libev: io watcher list contains a loop", w != w2));
  2699. w2 = w2->next;
  2700. }
  2701. assert (("libev: inactive fd watcher on anfd list", ev_active (w) == 1));
  2702. assert (("libev: fd mismatch between watcher and anfd", ((ev_io *)w)->fd == i));
  2703. }
  2704. }
  2705. assert (timermax >= timercnt);
  2706. verify_heap (EV_A_ timers, timercnt);
  2707. #if EV_PERIODIC_ENABLE
  2708. assert (periodicmax >= periodiccnt);
  2709. verify_heap (EV_A_ periodics, periodiccnt);
  2710. #endif
  2711. for (i = NUMPRI; i--; )
  2712. {
  2713. assert (pendingmax [i] >= pendingcnt [i]);
  2714. #if EV_IDLE_ENABLE
  2715. assert (idleall >= 0);
  2716. assert (idlemax [i] >= idlecnt [i]);
  2717. array_verify (EV_A_ (W *)idles [i], idlecnt [i]);
  2718. #endif
  2719. }
  2720. #if EV_FORK_ENABLE
  2721. assert (forkmax >= forkcnt);
  2722. array_verify (EV_A_ (W *)forks, forkcnt);
  2723. #endif
  2724. #if EV_CLEANUP_ENABLE
  2725. assert (cleanupmax >= cleanupcnt);
  2726. array_verify (EV_A_ (W *)cleanups, cleanupcnt);
  2727. #endif
  2728. #if EV_ASYNC_ENABLE
  2729. assert (asyncmax >= asynccnt);
  2730. array_verify (EV_A_ (W *)asyncs, asynccnt);
  2731. #endif
  2732. #if EV_PREPARE_ENABLE
  2733. assert (preparemax >= preparecnt);
  2734. array_verify (EV_A_ (W *)prepares, preparecnt);
  2735. #endif
  2736. #if EV_CHECK_ENABLE
  2737. assert (checkmax >= checkcnt);
  2738. array_verify (EV_A_ (W *)checks, checkcnt);
  2739. #endif
  2740. # if 0
  2741. #if EV_CHILD_ENABLE
  2742. for (w = (ev_child *)childs [chain & ((EV_PID_HASHSIZE) - 1)]; w; w = (ev_child *)((WL)w)->next)
  2743. for (signum = EV_NSIG; signum--; ) if (signals [signum].pending)
  2744. #endif
  2745. # endif
  2746. #endif
  2747. }
  2748. #endif
  2749. #if EV_MULTIPLICITY
  2750. ecb_cold
  2751. struct ev_loop *
  2752. #else
  2753. int
  2754. #endif
  2755. ev_default_loop (unsigned int flags) EV_NOEXCEPT
  2756. {
  2757. if (!ev_default_loop_ptr)
  2758. {
  2759. #if EV_MULTIPLICITY
  2760. EV_P = ev_default_loop_ptr = &default_loop_struct;
  2761. #else
  2762. ev_default_loop_ptr = 1;
  2763. #endif
  2764. loop_init (EV_A_ flags);
  2765. if (ev_backend (EV_A))
  2766. {
  2767. #if EV_CHILD_ENABLE
  2768. ev_signal_init (&childev, childcb, SIGCHLD);
  2769. ev_set_priority (&childev, EV_MAXPRI);
  2770. ev_signal_start (EV_A_ &childev);
  2771. ev_unref (EV_A); /* child watcher should not keep loop alive */
  2772. #endif
  2773. }
  2774. else
  2775. ev_default_loop_ptr = 0;
  2776. }
  2777. return ev_default_loop_ptr;
  2778. }
  2779. void
  2780. ev_loop_fork (EV_P) EV_NOEXCEPT
  2781. {
  2782. postfork = 1;
  2783. }
  2784. /*****************************************************************************/
  2785. void
  2786. ev_invoke (EV_P_ void *w, int revents)
  2787. {
  2788. EV_CB_INVOKE ((W)w, revents);
  2789. }
  2790. unsigned int
  2791. ev_pending_count (EV_P) EV_NOEXCEPT
  2792. {
  2793. int pri;
  2794. unsigned int count = 0;
  2795. for (pri = NUMPRI; pri--; )
  2796. count += pendingcnt [pri];
  2797. return count;
  2798. }
  2799. noinline
  2800. void
  2801. ev_invoke_pending (EV_P)
  2802. {
  2803. pendingpri = NUMPRI;
  2804. do
  2805. {
  2806. --pendingpri;
  2807. /* pendingpri possibly gets modified in the inner loop */
  2808. while (pendingcnt [pendingpri])
  2809. {
  2810. ANPENDING *p = pendings [pendingpri] + --pendingcnt [pendingpri];
  2811. p->w->pending = 0;
  2812. EV_CB_INVOKE (p->w, p->events);
  2813. EV_FREQUENT_CHECK;
  2814. }
  2815. }
  2816. while (pendingpri);
  2817. }
  2818. #if EV_IDLE_ENABLE
  2819. /* make idle watchers pending. this handles the "call-idle */
  2820. /* only when higher priorities are idle" logic */
  2821. inline_size void
  2822. idle_reify (EV_P)
  2823. {
  2824. if (expect_false (idleall))
  2825. {
  2826. int pri;
  2827. for (pri = NUMPRI; pri--; )
  2828. {
  2829. if (pendingcnt [pri])
  2830. break;
  2831. if (idlecnt [pri])
  2832. {
  2833. queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
  2834. break;
  2835. }
  2836. }
  2837. }
  2838. }
  2839. #endif
  2840. /* make timers pending */
  2841. inline_size void
  2842. timers_reify (EV_P)
  2843. {
  2844. EV_FREQUENT_CHECK;
  2845. if (timercnt && ANHE_at (timers [HEAP0]) < mn_now)
  2846. {
  2847. do
  2848. {
  2849. ev_timer *w = (ev_timer *)ANHE_w (timers [HEAP0]);
  2850. /*assert (("libev: inactive timer on timer heap detected", ev_is_active (w)));*/
  2851. /* first reschedule or stop timer */
  2852. if (w->repeat)
  2853. {
  2854. ev_at (w) += w->repeat;
  2855. if (ev_at (w) < mn_now)
  2856. ev_at (w) = mn_now;
  2857. assert (("libev: negative ev_timer repeat value found while processing timers", w->repeat > 0.));
  2858. ANHE_at_cache (timers [HEAP0]);
  2859. downheap (timers, timercnt, HEAP0);
  2860. }
  2861. else
  2862. ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
  2863. EV_FREQUENT_CHECK;
  2864. feed_reverse (EV_A_ (W)w);
  2865. }
  2866. while (timercnt && ANHE_at (timers [HEAP0]) < mn_now);
  2867. feed_reverse_done (EV_A_ EV_TIMER);
  2868. }
  2869. }
  2870. #if EV_PERIODIC_ENABLE
  2871. noinline
  2872. static void
  2873. periodic_recalc (EV_P_ ev_periodic *w)
  2874. {
  2875. ev_tstamp interval = w->interval > MIN_INTERVAL ? w->interval : MIN_INTERVAL;
  2876. ev_tstamp at = w->offset + interval * ev_floor ((ev_rt_now - w->offset) / interval);
  2877. /* the above almost always errs on the low side */
  2878. while (at <= ev_rt_now)
  2879. {
  2880. ev_tstamp nat = at + w->interval;
  2881. /* when resolution fails us, we use ev_rt_now */
  2882. if (expect_false (nat == at))
  2883. {
  2884. at = ev_rt_now;
  2885. break;
  2886. }
  2887. at = nat;
  2888. }
  2889. ev_at (w) = at;
  2890. }
  2891. /* make periodics pending */
  2892. inline_size void
  2893. periodics_reify (EV_P)
  2894. {
  2895. EV_FREQUENT_CHECK;
  2896. while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now)
  2897. {
  2898. do
  2899. {
  2900. ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]);
  2901. /*assert (("libev: inactive timer on periodic heap detected", ev_is_active (w)));*/
  2902. /* first reschedule or stop timer */
  2903. if (w->reschedule_cb)
  2904. {
  2905. ev_at (w) = w->reschedule_cb (w, ev_rt_now);
  2906. assert (("libev: ev_periodic reschedule callback returned time in the past", ev_at (w) >= ev_rt_now));
  2907. ANHE_at_cache (periodics [HEAP0]);
  2908. downheap (periodics, periodiccnt, HEAP0);
  2909. }
  2910. else if (w->interval)
  2911. {
  2912. periodic_recalc (EV_A_ w);
  2913. ANHE_at_cache (periodics [HEAP0]);
  2914. downheap (periodics, periodiccnt, HEAP0);
  2915. }
  2916. else
  2917. ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
  2918. EV_FREQUENT_CHECK;
  2919. feed_reverse (EV_A_ (W)w);
  2920. }
  2921. while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now);
  2922. feed_reverse_done (EV_A_ EV_PERIODIC);
  2923. }
  2924. }
  2925. /* simply recalculate all periodics */
  2926. /* TODO: maybe ensure that at least one event happens when jumping forward? */
  2927. noinline ecb_cold
  2928. static void
  2929. periodics_reschedule (EV_P)
  2930. {
  2931. int i;
  2932. /* adjust periodics after time jump */
  2933. for (i = HEAP0; i < periodiccnt + HEAP0; ++i)
  2934. {
  2935. ev_periodic *w = (ev_periodic *)ANHE_w (periodics [i]);
  2936. if (w->reschedule_cb)
  2937. ev_at (w) = w->reschedule_cb (w, ev_rt_now);
  2938. else if (w->interval)
  2939. periodic_recalc (EV_A_ w);
  2940. ANHE_at_cache (periodics [i]);
  2941. }
  2942. reheap (periodics, periodiccnt);
  2943. }
  2944. #endif
  2945. /* adjust all timers by a given offset */
  2946. noinline ecb_cold
  2947. static void
  2948. timers_reschedule (EV_P_ ev_tstamp adjust)
  2949. {
  2950. int i;
  2951. for (i = 0; i < timercnt; ++i)
  2952. {
  2953. ANHE *he = timers + i + HEAP0;
  2954. ANHE_w (*he)->at += adjust;
  2955. ANHE_at_cache (*he);
  2956. }
  2957. }
  2958. /* fetch new monotonic and realtime times from the kernel */
  2959. /* also detect if there was a timejump, and act accordingly */
  2960. inline_speed void
  2961. time_update (EV_P_ ev_tstamp max_block)
  2962. {
  2963. #if EV_USE_MONOTONIC
  2964. if (expect_true (have_monotonic))
  2965. {
  2966. int i;
  2967. ev_tstamp odiff = rtmn_diff;
  2968. mn_now = get_clock ();
  2969. /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */
  2970. /* interpolate in the meantime */
  2971. if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
  2972. {
  2973. ev_rt_now = rtmn_diff + mn_now;
  2974. return;
  2975. }
  2976. now_floor = mn_now;
  2977. ev_rt_now = ev_time ();
  2978. /* loop a few times, before making important decisions.
  2979. * on the choice of "4": one iteration isn't enough,
  2980. * in case we get preempted during the calls to
  2981. * ev_time and get_clock. a second call is almost guaranteed
  2982. * to succeed in that case, though. and looping a few more times
  2983. * doesn't hurt either as we only do this on time-jumps or
  2984. * in the unlikely event of having been preempted here.
  2985. */
  2986. for (i = 4; --i; )
  2987. {
  2988. ev_tstamp diff;
  2989. rtmn_diff = ev_rt_now - mn_now;
  2990. diff = odiff - rtmn_diff;
  2991. if (expect_true ((diff < 0. ? -diff : diff) < MIN_TIMEJUMP))
  2992. return; /* all is well */
  2993. ev_rt_now = ev_time ();
  2994. mn_now = get_clock ();
  2995. now_floor = mn_now;
  2996. }
  2997. /* no timer adjustment, as the monotonic clock doesn't jump */
  2998. /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
  2999. # if EV_PERIODIC_ENABLE
  3000. periodics_reschedule (EV_A);
  3001. # endif
  3002. }
  3003. else
  3004. #endif
  3005. {
  3006. ev_rt_now = ev_time ();
  3007. if (expect_false (mn_now > ev_rt_now || ev_rt_now > mn_now + max_block + MIN_TIMEJUMP))
  3008. {
  3009. /* adjust timers. this is easy, as the offset is the same for all of them */
  3010. timers_reschedule (EV_A_ ev_rt_now - mn_now);
  3011. #if EV_PERIODIC_ENABLE
  3012. periodics_reschedule (EV_A);
  3013. #endif
  3014. }
  3015. mn_now = ev_rt_now;
  3016. }
  3017. }
  3018. int
  3019. ev_run (EV_P_ int flags)
  3020. {
  3021. #if EV_FEATURE_API
  3022. ++loop_depth;
  3023. #endif
  3024. assert (("libev: ev_loop recursion during release detected", loop_done != EVBREAK_RECURSE));
  3025. loop_done = EVBREAK_CANCEL;
  3026. EV_INVOKE_PENDING; /* in case we recurse, ensure ordering stays nice and clean */
  3027. do
  3028. {
  3029. #if EV_VERIFY >= 2
  3030. ev_verify (EV_A);
  3031. #endif
  3032. #ifndef _WIN32
  3033. if (expect_false (curpid)) /* penalise the forking check even more */
  3034. if (expect_false (getpid () != curpid))
  3035. {
  3036. curpid = getpid ();
  3037. postfork = 1;
  3038. }
  3039. #endif
  3040. #if EV_FORK_ENABLE
  3041. /* we might have forked, so queue fork handlers */
  3042. if (expect_false (postfork))
  3043. if (forkcnt)
  3044. {
  3045. queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
  3046. EV_INVOKE_PENDING;
  3047. }
  3048. #endif
  3049. #if EV_PREPARE_ENABLE
  3050. /* queue prepare watchers (and execute them) */
  3051. if (expect_false (preparecnt))
  3052. {
  3053. queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
  3054. EV_INVOKE_PENDING;
  3055. }
  3056. #endif
  3057. if (expect_false (loop_done))
  3058. break;
  3059. /* we might have forked, so reify kernel state if necessary */
  3060. if (expect_false (postfork))
  3061. loop_fork (EV_A);
  3062. /* update fd-related kernel structures */
  3063. fd_reify (EV_A);
  3064. /* calculate blocking time */
  3065. {
  3066. ev_tstamp waittime = 0.;
  3067. ev_tstamp sleeptime = 0.;
  3068. /* remember old timestamp for io_blocktime calculation */
  3069. ev_tstamp prev_mn_now = mn_now;
  3070. /* update time to cancel out callback processing overhead */
  3071. time_update (EV_A_ 1e100);
  3072. /* from now on, we want a pipe-wake-up */
  3073. pipe_write_wanted = 1;
  3074. ECB_MEMORY_FENCE; /* make sure pipe_write_wanted is visible before we check for potential skips */
  3075. if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt || pipe_write_skipped)))
  3076. {
  3077. waittime = MAX_BLOCKTIME;
  3078. if (timercnt)
  3079. {
  3080. ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now;
  3081. if (waittime > to) waittime = to;
  3082. }
  3083. #if EV_PERIODIC_ENABLE
  3084. if (periodiccnt)
  3085. {
  3086. ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now;
  3087. if (waittime > to) waittime = to;
  3088. }
  3089. #endif
  3090. /* don't let timeouts decrease the waittime below timeout_blocktime */
  3091. if (expect_false (waittime < timeout_blocktime))
  3092. waittime = timeout_blocktime;
  3093. /* at this point, we NEED to wait, so we have to ensure */
  3094. /* to pass a minimum nonzero value to the backend */
  3095. if (expect_false (waittime < backend_mintime))
  3096. waittime = backend_mintime;
  3097. /* extra check because io_blocktime is commonly 0 */
  3098. if (expect_false (io_blocktime))
  3099. {
  3100. sleeptime = io_blocktime - (mn_now - prev_mn_now);
  3101. if (sleeptime > waittime - backend_mintime)
  3102. sleeptime = waittime - backend_mintime;
  3103. if (expect_true (sleeptime > 0.))
  3104. {
  3105. ev_sleep (sleeptime);
  3106. waittime -= sleeptime;
  3107. }
  3108. }
  3109. }
  3110. #if EV_FEATURE_API
  3111. ++loop_count;
  3112. #endif
  3113. assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */
  3114. backend_poll (EV_A_ waittime);
  3115. assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */
  3116. pipe_write_wanted = 0; /* just an optimisation, no fence needed */
  3117. ECB_MEMORY_FENCE_ACQUIRE;
  3118. if (pipe_write_skipped)
  3119. {
  3120. assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w)));
  3121. ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM);
  3122. }
  3123. /* update ev_rt_now, do magic */
  3124. time_update (EV_A_ waittime + sleeptime);
  3125. }
  3126. /* queue pending timers and reschedule them */
  3127. timers_reify (EV_A); /* relative timers called last */
  3128. #if EV_PERIODIC_ENABLE
  3129. periodics_reify (EV_A); /* absolute timers called first */
  3130. #endif
  3131. #if EV_IDLE_ENABLE
  3132. /* queue idle watchers unless other events are pending */
  3133. idle_reify (EV_A);
  3134. #endif
  3135. #if EV_CHECK_ENABLE
  3136. /* queue check watchers, to be executed first */
  3137. if (expect_false (checkcnt))
  3138. queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
  3139. #endif
  3140. EV_INVOKE_PENDING;
  3141. }
  3142. while (expect_true (
  3143. activecnt
  3144. && !loop_done
  3145. && !(flags & (EVRUN_ONCE | EVRUN_NOWAIT))
  3146. ));
  3147. if (loop_done == EVBREAK_ONE)
  3148. loop_done = EVBREAK_CANCEL;
  3149. #if EV_FEATURE_API
  3150. --loop_depth;
  3151. #endif
  3152. return activecnt;
  3153. }
  3154. void
  3155. ev_break (EV_P_ int how) EV_NOEXCEPT
  3156. {
  3157. loop_done = how;
  3158. }
  3159. void
  3160. ev_ref (EV_P) EV_NOEXCEPT
  3161. {
  3162. ++activecnt;
  3163. }
  3164. void
  3165. ev_unref (EV_P) EV_NOEXCEPT
  3166. {
  3167. --activecnt;
  3168. }
  3169. int
  3170. ev_active_cnt (EV_P) EV_NOEXCEPT
  3171. {
  3172. return activecnt;
  3173. }
  3174. void
  3175. ev_now_update (EV_P) EV_NOEXCEPT
  3176. {
  3177. time_update (EV_A_ 1e100);
  3178. }
  3179. void
  3180. ev_suspend (EV_P) EV_NOEXCEPT
  3181. {
  3182. ev_now_update (EV_A);
  3183. }
  3184. void
  3185. ev_resume (EV_P) EV_NOEXCEPT
  3186. {
  3187. ev_tstamp mn_prev = mn_now;
  3188. ev_now_update (EV_A);
  3189. timers_reschedule (EV_A_ mn_now - mn_prev);
  3190. #if EV_PERIODIC_ENABLE
  3191. /* TODO: really do this? */
  3192. periodics_reschedule (EV_A);
  3193. #endif
  3194. }
  3195. /*****************************************************************************/
  3196. /* singly-linked list management, used when the expected list length is short */
  3197. inline_size void
  3198. wlist_add (WL *head, WL elem)
  3199. {
  3200. elem->next = *head;
  3201. *head = elem;
  3202. }
  3203. inline_size void
  3204. wlist_del (WL *head, WL elem)
  3205. {
  3206. while (*head)
  3207. {
  3208. if (expect_true (*head == elem))
  3209. {
  3210. *head = elem->next;
  3211. break;
  3212. }
  3213. head = &(*head)->next;
  3214. }
  3215. }
  3216. /* internal, faster, version of ev_clear_pending */
  3217. inline_speed void
  3218. clear_pending (EV_P_ W w)
  3219. {
  3220. if (w->pending)
  3221. {
  3222. pendings [ABSPRI (w)][w->pending - 1].w = (W)&pending_w;
  3223. w->pending = 0;
  3224. }
  3225. }
  3226. int
  3227. ev_clear_pending (EV_P_ void *w) EV_NOEXCEPT
  3228. {
  3229. W w_ = (W)w;
  3230. int pending = w_->pending;
  3231. if (expect_true (pending))
  3232. {
  3233. ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1;
  3234. p->w = (W)&pending_w;
  3235. w_->pending = 0;
  3236. return p->events;
  3237. }
  3238. else
  3239. return 0;
  3240. }
  3241. inline_size void
  3242. pri_adjust (EV_P_ W w)
  3243. {
  3244. int pri = ev_priority (w);
  3245. pri = pri < EV_MINPRI ? EV_MINPRI : pri;
  3246. pri = pri > EV_MAXPRI ? EV_MAXPRI : pri;
  3247. ev_set_priority (w, pri);
  3248. }
  3249. inline_speed void
  3250. ev_start (EV_P_ W w, int active)
  3251. {
  3252. pri_adjust (EV_A_ w);
  3253. w->active = active;
  3254. ev_ref (EV_A);
  3255. }
  3256. inline_size void
  3257. ev_stop (EV_P_ W w)
  3258. {
  3259. ev_unref (EV_A);
  3260. w->active = 0;
  3261. }
  3262. /*****************************************************************************/
  3263. noinline
  3264. void
  3265. ev_io_start (EV_P_ ev_io *w) EV_NOEXCEPT
  3266. {
  3267. int fd = w->fd;
  3268. if (expect_false (ev_is_active (w)))
  3269. return;
  3270. assert (("libev: ev_io_start called with negative fd", fd >= 0));
  3271. assert (("libev: ev_io_start called with illegal event mask", !(w->events & ~(EV__IOFDSET | EV_READ | EV_WRITE))));
  3272. EV_FREQUENT_CHECK;
  3273. ev_start (EV_A_ (W)w, 1);
  3274. array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero);
  3275. wlist_add (&anfds[fd].head, (WL)w);
  3276. /* common bug, apparently */
  3277. assert (("libev: ev_io_start called with corrupted watcher", ((WL)w)->next != (WL)w));
  3278. fd_change (EV_A_ fd, w->events & (EV__IOFDSET | EV_ANFD_REIFY));
  3279. w->events &= ~EV__IOFDSET;
  3280. EV_FREQUENT_CHECK;
  3281. }
  3282. noinline
  3283. void
  3284. ev_io_stop (EV_P_ ev_io *w) EV_NOEXCEPT
  3285. {
  3286. clear_pending (EV_A_ (W)w);
  3287. if (expect_false (!ev_is_active (w)))
  3288. return;
  3289. assert (("libev: ev_io_stop called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
  3290. EV_FREQUENT_CHECK;
  3291. wlist_del (&anfds[w->fd].head, (WL)w);
  3292. ev_stop (EV_A_ (W)w);
  3293. fd_change (EV_A_ w->fd, EV_ANFD_REIFY);
  3294. EV_FREQUENT_CHECK;
  3295. }
  3296. noinline
  3297. void
  3298. ev_timer_start (EV_P_ ev_timer *w) EV_NOEXCEPT
  3299. {
  3300. if (expect_false (ev_is_active (w)))
  3301. return;
  3302. ev_at (w) += mn_now;
  3303. assert (("libev: ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
  3304. EV_FREQUENT_CHECK;
  3305. ++timercnt;
  3306. ev_start (EV_A_ (W)w, timercnt + HEAP0 - 1);
  3307. array_needsize (ANHE, timers, timermax, ev_active (w) + 1, EMPTY2);
  3308. ANHE_w (timers [ev_active (w)]) = (WT)w;
  3309. ANHE_at_cache (timers [ev_active (w)]);
  3310. upheap (timers, ev_active (w));
  3311. EV_FREQUENT_CHECK;
  3312. /*assert (("libev: internal timer heap corruption", timers [ev_active (w)] == (WT)w));*/
  3313. }
  3314. noinline
  3315. void
  3316. ev_timer_stop (EV_P_ ev_timer *w) EV_NOEXCEPT
  3317. {
  3318. clear_pending (EV_A_ (W)w);
  3319. if (expect_false (!ev_is_active (w)))
  3320. return;
  3321. EV_FREQUENT_CHECK;
  3322. {
  3323. int active = ev_active (w);
  3324. assert (("libev: internal timer heap corruption", ANHE_w (timers [active]) == (WT)w));
  3325. --timercnt;
  3326. if (expect_true (active < timercnt + HEAP0))
  3327. {
  3328. timers [active] = timers [timercnt + HEAP0];
  3329. adjustheap (timers, timercnt, active);
  3330. }
  3331. }
  3332. ev_at (w) -= mn_now;
  3333. ev_stop (EV_A_ (W)w);
  3334. EV_FREQUENT_CHECK;
  3335. }
  3336. noinline
  3337. void
  3338. ev_timer_again (EV_P_ ev_timer *w) EV_NOEXCEPT
  3339. {
  3340. EV_FREQUENT_CHECK;
  3341. clear_pending (EV_A_ (W)w);
  3342. if (ev_is_active (w))
  3343. {
  3344. if (w->repeat)
  3345. {
  3346. ev_at (w) = mn_now + w->repeat;
  3347. ANHE_at_cache (timers [ev_active (w)]);
  3348. adjustheap (timers, timercnt, ev_active (w));
  3349. }
  3350. else
  3351. ev_timer_stop (EV_A_ w);
  3352. }
  3353. else if (w->repeat)
  3354. {
  3355. ev_at (w) = w->repeat;
  3356. ev_timer_start (EV_A_ w);
  3357. }
  3358. EV_FREQUENT_CHECK;
  3359. }
  3360. ev_tstamp
  3361. ev_timer_remaining (EV_P_ ev_timer *w) EV_NOEXCEPT
  3362. {
  3363. return ev_at (w) - (ev_is_active (w) ? mn_now : 0.);
  3364. }
  3365. #if EV_PERIODIC_ENABLE
  3366. noinline
  3367. void
  3368. ev_periodic_start (EV_P_ ev_periodic *w) EV_NOEXCEPT
  3369. {
  3370. if (expect_false (ev_is_active (w)))
  3371. return;
  3372. if (w->reschedule_cb)
  3373. ev_at (w) = w->reschedule_cb (w, ev_rt_now);
  3374. else if (w->interval)
  3375. {
  3376. assert (("libev: ev_periodic_start called with negative interval value", w->interval >= 0.));
  3377. periodic_recalc (EV_A_ w);
  3378. }
  3379. else
  3380. ev_at (w) = w->offset;
  3381. EV_FREQUENT_CHECK;
  3382. ++periodiccnt;
  3383. ev_start (EV_A_ (W)w, periodiccnt + HEAP0 - 1);
  3384. array_needsize (ANHE, periodics, periodicmax, ev_active (w) + 1, EMPTY2);
  3385. ANHE_w (periodics [ev_active (w)]) = (WT)w;
  3386. ANHE_at_cache (periodics [ev_active (w)]);
  3387. upheap (periodics, ev_active (w));
  3388. EV_FREQUENT_CHECK;
  3389. /*assert (("libev: internal periodic heap corruption", ANHE_w (periodics [ev_active (w)]) == (WT)w));*/
  3390. }
  3391. noinline
  3392. void
  3393. ev_periodic_stop (EV_P_ ev_periodic *w) EV_NOEXCEPT
  3394. {
  3395. clear_pending (EV_A_ (W)w);
  3396. if (expect_false (!ev_is_active (w)))
  3397. return;
  3398. EV_FREQUENT_CHECK;
  3399. {
  3400. int active = ev_active (w);
  3401. assert (("libev: internal periodic heap corruption", ANHE_w (periodics [active]) == (WT)w));
  3402. --periodiccnt;
  3403. if (expect_true (active < periodiccnt + HEAP0))
  3404. {
  3405. periodics [active] = periodics [periodiccnt + HEAP0];
  3406. adjustheap (periodics, periodiccnt, active);
  3407. }
  3408. }
  3409. ev_stop (EV_A_ (W)w);
  3410. EV_FREQUENT_CHECK;
  3411. }
  3412. noinline
  3413. void
  3414. ev_periodic_again (EV_P_ ev_periodic *w) EV_NOEXCEPT
  3415. {
  3416. /* TODO: use adjustheap and recalculation */
  3417. ev_periodic_stop (EV_A_ w);
  3418. ev_periodic_start (EV_A_ w);
  3419. }
  3420. #endif
  3421. #ifndef SA_RESTART
  3422. # define SA_RESTART 0
  3423. #endif
  3424. #if EV_SIGNAL_ENABLE
  3425. noinline
  3426. void
  3427. ev_signal_start (EV_P_ ev_signal *w) EV_NOEXCEPT
  3428. {
  3429. if (expect_false (ev_is_active (w)))
  3430. return;
  3431. assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0 && w->signum < EV_NSIG));
  3432. #if EV_MULTIPLICITY
  3433. assert (("libev: a signal must not be attached to two different loops",
  3434. !signals [w->signum - 1].loop || signals [w->signum - 1].loop == loop));
  3435. signals [w->signum - 1].loop = EV_A;
  3436. ECB_MEMORY_FENCE_RELEASE;
  3437. #endif
  3438. EV_FREQUENT_CHECK;
  3439. #if EV_USE_SIGNALFD
  3440. if (sigfd == -2)
  3441. {
  3442. sigfd = signalfd (-1, &sigfd_set, SFD_NONBLOCK | SFD_CLOEXEC);
  3443. if (sigfd < 0 && errno == EINVAL)
  3444. sigfd = signalfd (-1, &sigfd_set, 0); /* retry without flags */
  3445. if (sigfd >= 0)
  3446. {
  3447. fd_intern (sigfd); /* doing it twice will not hurt */
  3448. sigemptyset (&sigfd_set);
  3449. ev_io_init (&sigfd_w, sigfdcb, sigfd, EV_READ);
  3450. ev_set_priority (&sigfd_w, EV_MAXPRI);
  3451. ev_io_start (EV_A_ &sigfd_w);
  3452. ev_unref (EV_A); /* signalfd watcher should not keep loop alive */
  3453. }
  3454. }
  3455. if (sigfd >= 0)
  3456. {
  3457. /* TODO: check .head */
  3458. sigaddset (&sigfd_set, w->signum);
  3459. sigprocmask (SIG_BLOCK, &sigfd_set, 0);
  3460. signalfd (sigfd, &sigfd_set, 0);
  3461. }
  3462. #endif
  3463. ev_start (EV_A_ (W)w, 1);
  3464. wlist_add (&signals [w->signum - 1].head, (WL)w);
  3465. if (!((WL)w)->next)
  3466. # if EV_USE_SIGNALFD
  3467. if (sigfd < 0) /*TODO*/
  3468. # endif
  3469. {
  3470. # ifdef _WIN32
  3471. evpipe_init (EV_A);
  3472. signal (w->signum, ev_sighandler);
  3473. # else
  3474. struct sigaction sa;
  3475. evpipe_init (EV_A);
  3476. sa.sa_handler = ev_sighandler;
  3477. sigfillset (&sa.sa_mask);
  3478. sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
  3479. sigaction (w->signum, &sa, 0);
  3480. if (origflags & EVFLAG_NOSIGMASK)
  3481. {
  3482. sigemptyset (&sa.sa_mask);
  3483. sigaddset (&sa.sa_mask, w->signum);
  3484. sigprocmask (SIG_UNBLOCK, &sa.sa_mask, 0);
  3485. }
  3486. #endif
  3487. }
  3488. EV_FREQUENT_CHECK;
  3489. }
  3490. noinline
  3491. void
  3492. ev_signal_stop (EV_P_ ev_signal *w) EV_NOEXCEPT
  3493. {
  3494. clear_pending (EV_A_ (W)w);
  3495. if (expect_false (!ev_is_active (w)))
  3496. return;
  3497. EV_FREQUENT_CHECK;
  3498. wlist_del (&signals [w->signum - 1].head, (WL)w);
  3499. ev_stop (EV_A_ (W)w);
  3500. if (!signals [w->signum - 1].head)
  3501. {
  3502. #if EV_MULTIPLICITY
  3503. signals [w->signum - 1].loop = 0; /* unattach from signal */
  3504. #endif
  3505. #if EV_USE_SIGNALFD
  3506. if (sigfd >= 0)
  3507. {
  3508. sigset_t ss;
  3509. sigemptyset (&ss);
  3510. sigaddset (&ss, w->signum);
  3511. sigdelset (&sigfd_set, w->signum);
  3512. signalfd (sigfd, &sigfd_set, 0);
  3513. sigprocmask (SIG_UNBLOCK, &ss, 0);
  3514. }
  3515. else
  3516. #endif
  3517. signal (w->signum, SIG_DFL);
  3518. }
  3519. EV_FREQUENT_CHECK;
  3520. }
  3521. #endif
  3522. #if EV_CHILD_ENABLE
  3523. void
  3524. ev_child_start (EV_P_ ev_child *w) EV_NOEXCEPT
  3525. {
  3526. #if EV_MULTIPLICITY
  3527. assert (("libev: child watchers are only supported in the default loop", loop == ev_default_loop_ptr));
  3528. #endif
  3529. if (expect_false (ev_is_active (w)))
  3530. return;
  3531. EV_FREQUENT_CHECK;
  3532. ev_start (EV_A_ (W)w, 1);
  3533. wlist_add (&childs [w->pid & ((EV_PID_HASHSIZE) - 1)], (WL)w);
  3534. EV_FREQUENT_CHECK;
  3535. }
  3536. void
  3537. ev_child_stop (EV_P_ ev_child *w) EV_NOEXCEPT
  3538. {
  3539. clear_pending (EV_A_ (W)w);
  3540. if (expect_false (!ev_is_active (w)))
  3541. return;
  3542. EV_FREQUENT_CHECK;
  3543. wlist_del (&childs [w->pid & ((EV_PID_HASHSIZE) - 1)], (WL)w);
  3544. ev_stop (EV_A_ (W)w);
  3545. EV_FREQUENT_CHECK;
  3546. }
  3547. #endif
  3548. #if EV_STAT_ENABLE
  3549. # ifdef _WIN32
  3550. # undef lstat
  3551. # define lstat(a,b) _stati64 (a,b)
  3552. # endif
  3553. #define DEF_STAT_INTERVAL 5.0074891
  3554. #define NFS_STAT_INTERVAL 30.1074891 /* for filesystems potentially failing inotify */
  3555. #define MIN_STAT_INTERVAL 0.1074891
  3556. noinline static void stat_timer_cb (EV_P_ ev_timer *w_, int revents);
  3557. #if EV_USE_INOTIFY
  3558. /* the * 2 is to allow for alignment padding, which for some reason is >> 8 */
  3559. # define EV_INOTIFY_BUFSIZE (sizeof (struct inotify_event) * 2 + NAME_MAX)
  3560. noinline
  3561. static void
  3562. infy_add (EV_P_ ev_stat *w)
  3563. {
  3564. w->wd = inotify_add_watch (fs_fd, w->path,
  3565. IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF | IN_MODIFY
  3566. | IN_CREATE | IN_DELETE | IN_MOVED_FROM | IN_MOVED_TO
  3567. | IN_DONT_FOLLOW | IN_MASK_ADD);
  3568. if (w->wd >= 0)
  3569. {
  3570. struct statfs sfs;
  3571. /* now local changes will be tracked by inotify, but remote changes won't */
  3572. /* unless the filesystem is known to be local, we therefore still poll */
  3573. /* also do poll on <2.6.25, but with normal frequency */
  3574. if (!fs_2625)
  3575. w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL;
  3576. else if (!statfs (w->path, &sfs)
  3577. && (sfs.f_type == 0x1373 /* devfs */
  3578. || sfs.f_type == 0x4006 /* fat */
  3579. || sfs.f_type == 0x4d44 /* msdos */
  3580. || sfs.f_type == 0xEF53 /* ext2/3 */
  3581. || sfs.f_type == 0x72b6 /* jffs2 */
  3582. || sfs.f_type == 0x858458f6 /* ramfs */
  3583. || sfs.f_type == 0x5346544e /* ntfs */
  3584. || sfs.f_type == 0x3153464a /* jfs */
  3585. || sfs.f_type == 0x9123683e /* btrfs */
  3586. || sfs.f_type == 0x52654973 /* reiser3 */
  3587. || sfs.f_type == 0x01021994 /* tmpfs */
  3588. || sfs.f_type == 0x58465342 /* xfs */))
  3589. w->timer.repeat = 0.; /* filesystem is local, kernel new enough */
  3590. else
  3591. w->timer.repeat = w->interval ? w->interval : NFS_STAT_INTERVAL; /* remote, use reduced frequency */
  3592. }
  3593. else
  3594. {
  3595. /* can't use inotify, continue to stat */
  3596. w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL;
  3597. /* if path is not there, monitor some parent directory for speedup hints */
  3598. /* note that exceeding the hardcoded path limit is not a correctness issue, */
  3599. /* but an efficiency issue only */
  3600. if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096)
  3601. {
  3602. char path [4096];
  3603. strcpy (path, w->path);
  3604. do
  3605. {
  3606. int mask = IN_MASK_ADD | IN_DELETE_SELF | IN_MOVE_SELF
  3607. | (errno == EACCES ? IN_ATTRIB : IN_CREATE | IN_MOVED_TO);
  3608. char *pend = strrchr (path, '/');
  3609. if (!pend || pend == path)
  3610. break;
  3611. *pend = 0;
  3612. w->wd = inotify_add_watch (fs_fd, path, mask);
  3613. }
  3614. while (w->wd < 0 && (errno == ENOENT || errno == EACCES));
  3615. }
  3616. }
  3617. if (w->wd >= 0)
  3618. wlist_add (&fs_hash [w->wd & ((EV_INOTIFY_HASHSIZE) - 1)].head, (WL)w);
  3619. /* now re-arm timer, if required */
  3620. if (ev_is_active (&w->timer)) ev_ref (EV_A);
  3621. ev_timer_again (EV_A_ &w->timer);
  3622. if (ev_is_active (&w->timer)) ev_unref (EV_A);
  3623. }
  3624. noinline
  3625. static void
  3626. infy_del (EV_P_ ev_stat *w)
  3627. {
  3628. int slot;
  3629. int wd = w->wd;
  3630. if (wd < 0)
  3631. return;
  3632. w->wd = -2;
  3633. slot = wd & ((EV_INOTIFY_HASHSIZE) - 1);
  3634. wlist_del (&fs_hash [slot].head, (WL)w);
  3635. /* remove this watcher, if others are watching it, they will rearm */
  3636. inotify_rm_watch (fs_fd, wd);
  3637. }
  3638. noinline
  3639. static void
  3640. infy_wd (EV_P_ int slot, int wd, struct inotify_event *ev)
  3641. {
  3642. if (slot < 0)
  3643. /* overflow, need to check for all hash slots */
  3644. for (slot = 0; slot < (EV_INOTIFY_HASHSIZE); ++slot)
  3645. infy_wd (EV_A_ slot, wd, ev);
  3646. else
  3647. {
  3648. WL w_;
  3649. for (w_ = fs_hash [slot & ((EV_INOTIFY_HASHSIZE) - 1)].head; w_; )
  3650. {
  3651. ev_stat *w = (ev_stat *)w_;
  3652. w_ = w_->next; /* lets us remove this watcher and all before it */
  3653. if (w->wd == wd || wd == -1)
  3654. {
  3655. if (ev->mask & (IN_IGNORED | IN_UNMOUNT | IN_DELETE_SELF))
  3656. {
  3657. wlist_del (&fs_hash [slot & ((EV_INOTIFY_HASHSIZE) - 1)].head, (WL)w);
  3658. w->wd = -1;
  3659. infy_add (EV_A_ w); /* re-add, no matter what */
  3660. }
  3661. stat_timer_cb (EV_A_ &w->timer, 0);
  3662. }
  3663. }
  3664. }
  3665. }
  3666. static void
  3667. infy_cb (EV_P_ ev_io *w, int revents)
  3668. {
  3669. char buf [EV_INOTIFY_BUFSIZE];
  3670. int ofs;
  3671. int len = read (fs_fd, buf, sizeof (buf));
  3672. for (ofs = 0; ofs < len; )
  3673. {
  3674. struct inotify_event *ev = (struct inotify_event *)(buf + ofs);
  3675. infy_wd (EV_A_ ev->wd, ev->wd, ev);
  3676. ofs += sizeof (struct inotify_event) + ev->len;
  3677. }
  3678. }
  3679. inline_size ecb_cold
  3680. void
  3681. ev_check_2625 (EV_P)
  3682. {
  3683. /* kernels < 2.6.25 are borked
  3684. * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html
  3685. */
  3686. if (ev_linux_version () < 0x020619)
  3687. return;
  3688. fs_2625 = 1;
  3689. }
  3690. inline_size int
  3691. infy_newfd (void)
  3692. {
  3693. #if defined IN_CLOEXEC && defined IN_NONBLOCK
  3694. int fd = inotify_init1 (IN_CLOEXEC | IN_NONBLOCK);
  3695. if (fd >= 0)
  3696. return fd;
  3697. #endif
  3698. return inotify_init ();
  3699. }
  3700. inline_size void
  3701. infy_init (EV_P)
  3702. {
  3703. if (fs_fd != -2)
  3704. return;
  3705. fs_fd = -1;
  3706. ev_check_2625 (EV_A);
  3707. fs_fd = infy_newfd ();
  3708. if (fs_fd >= 0)
  3709. {
  3710. fd_intern (fs_fd);
  3711. ev_io_init (&fs_w, infy_cb, fs_fd, EV_READ);
  3712. ev_set_priority (&fs_w, EV_MAXPRI);
  3713. ev_io_start (EV_A_ &fs_w);
  3714. ev_unref (EV_A);
  3715. }
  3716. }
  3717. inline_size void
  3718. infy_fork (EV_P)
  3719. {
  3720. int slot;
  3721. if (fs_fd < 0)
  3722. return;
  3723. ev_ref (EV_A);
  3724. ev_io_stop (EV_A_ &fs_w);
  3725. close (fs_fd);
  3726. fs_fd = infy_newfd ();
  3727. if (fs_fd >= 0)
  3728. {
  3729. fd_intern (fs_fd);
  3730. ev_io_set (&fs_w, fs_fd, EV_READ);
  3731. ev_io_start (EV_A_ &fs_w);
  3732. ev_unref (EV_A);
  3733. }
  3734. for (slot = 0; slot < (EV_INOTIFY_HASHSIZE); ++slot)
  3735. {
  3736. WL w_ = fs_hash [slot].head;
  3737. fs_hash [slot].head = 0;
  3738. while (w_)
  3739. {
  3740. ev_stat *w = (ev_stat *)w_;
  3741. w_ = w_->next; /* lets us add this watcher */
  3742. w->wd = -1;
  3743. if (fs_fd >= 0)
  3744. infy_add (EV_A_ w); /* re-add, no matter what */
  3745. else
  3746. {
  3747. w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL;
  3748. if (ev_is_active (&w->timer)) ev_ref (EV_A);
  3749. ev_timer_again (EV_A_ &w->timer);
  3750. if (ev_is_active (&w->timer)) ev_unref (EV_A);
  3751. }
  3752. }
  3753. }
  3754. }
  3755. #endif
  3756. #ifdef _WIN32
  3757. # define EV_LSTAT(p,b) _stati64 (p, b)
  3758. #else
  3759. # define EV_LSTAT(p,b) lstat (p, b)
  3760. #endif
  3761. void
  3762. ev_stat_stat (EV_P_ ev_stat *w) EV_NOEXCEPT
  3763. {
  3764. if (lstat (w->path, &w->attr) < 0)
  3765. w->attr.st_nlink = 0;
  3766. else if (!w->attr.st_nlink)
  3767. w->attr.st_nlink = 1;
  3768. }
  3769. noinline
  3770. static void
  3771. stat_timer_cb (EV_P_ ev_timer *w_, int revents)
  3772. {
  3773. ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer));
  3774. ev_statdata prev = w->attr;
  3775. ev_stat_stat (EV_A_ w);
  3776. /* memcmp doesn't work on netbsd, they.... do stuff to their struct stat */
  3777. if (
  3778. prev.st_dev != w->attr.st_dev
  3779. || prev.st_ino != w->attr.st_ino
  3780. || prev.st_mode != w->attr.st_mode
  3781. || prev.st_nlink != w->attr.st_nlink
  3782. || prev.st_uid != w->attr.st_uid
  3783. || prev.st_gid != w->attr.st_gid
  3784. || prev.st_rdev != w->attr.st_rdev
  3785. || prev.st_size != w->attr.st_size
  3786. || prev.st_atime != w->attr.st_atime
  3787. || prev.st_mtime != w->attr.st_mtime
  3788. || prev.st_ctime != w->attr.st_ctime
  3789. ) {
  3790. /* we only update w->prev on actual differences */
  3791. /* in case we test more often than invoke the callback, */
  3792. /* to ensure that prev is always different to attr */
  3793. w->prev = prev;
  3794. #if EV_USE_INOTIFY
  3795. if (fs_fd >= 0)
  3796. {
  3797. infy_del (EV_A_ w);
  3798. infy_add (EV_A_ w);
  3799. ev_stat_stat (EV_A_ w); /* avoid race... */
  3800. }
  3801. #endif
  3802. ev_feed_event (EV_A_ w, EV_STAT);
  3803. }
  3804. }
  3805. void
  3806. ev_stat_start (EV_P_ ev_stat *w) EV_NOEXCEPT
  3807. {
  3808. if (expect_false (ev_is_active (w)))
  3809. return;
  3810. ev_stat_stat (EV_A_ w);
  3811. if (w->interval < MIN_STAT_INTERVAL && w->interval)
  3812. w->interval = MIN_STAT_INTERVAL;
  3813. ev_timer_init (&w->timer, stat_timer_cb, 0., w->interval ? w->interval : DEF_STAT_INTERVAL);
  3814. ev_set_priority (&w->timer, ev_priority (w));
  3815. #if EV_USE_INOTIFY
  3816. infy_init (EV_A);
  3817. if (fs_fd >= 0)
  3818. infy_add (EV_A_ w);
  3819. else
  3820. #endif
  3821. {
  3822. ev_timer_again (EV_A_ &w->timer);
  3823. ev_unref (EV_A);
  3824. }
  3825. ev_start (EV_A_ (W)w, 1);
  3826. EV_FREQUENT_CHECK;
  3827. }
  3828. void
  3829. ev_stat_stop (EV_P_ ev_stat *w) EV_NOEXCEPT
  3830. {
  3831. clear_pending (EV_A_ (W)w);
  3832. if (expect_false (!ev_is_active (w)))
  3833. return;
  3834. EV_FREQUENT_CHECK;
  3835. #if EV_USE_INOTIFY
  3836. infy_del (EV_A_ w);
  3837. #endif
  3838. if (ev_is_active (&w->timer))
  3839. {
  3840. ev_ref (EV_A);
  3841. ev_timer_stop (EV_A_ &w->timer);
  3842. }
  3843. ev_stop (EV_A_ (W)w);
  3844. EV_FREQUENT_CHECK;
  3845. }
  3846. #endif
  3847. #if EV_IDLE_ENABLE
  3848. void
  3849. ev_idle_start (EV_P_ ev_idle *w) EV_NOEXCEPT
  3850. {
  3851. if (expect_false (ev_is_active (w)))
  3852. return;
  3853. pri_adjust (EV_A_ (W)w);
  3854. EV_FREQUENT_CHECK;
  3855. {
  3856. int active = ++idlecnt [ABSPRI (w)];
  3857. ++idleall;
  3858. ev_start (EV_A_ (W)w, active);
  3859. array_needsize (ev_idle *, idles [ABSPRI (w)], idlemax [ABSPRI (w)], active, EMPTY2);
  3860. idles [ABSPRI (w)][active - 1] = w;
  3861. }
  3862. EV_FREQUENT_CHECK;
  3863. }
  3864. void
  3865. ev_idle_stop (EV_P_ ev_idle *w) EV_NOEXCEPT
  3866. {
  3867. clear_pending (EV_A_ (W)w);
  3868. if (expect_false (!ev_is_active (w)))
  3869. return;
  3870. EV_FREQUENT_CHECK;
  3871. {
  3872. int active = ev_active (w);
  3873. idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]];
  3874. ev_active (idles [ABSPRI (w)][active - 1]) = active;
  3875. ev_stop (EV_A_ (W)w);
  3876. --idleall;
  3877. }
  3878. EV_FREQUENT_CHECK;
  3879. }
  3880. #endif
  3881. #if EV_PREPARE_ENABLE
  3882. void
  3883. ev_prepare_start (EV_P_ ev_prepare *w) EV_NOEXCEPT
  3884. {
  3885. if (expect_false (ev_is_active (w)))
  3886. return;
  3887. EV_FREQUENT_CHECK;
  3888. ev_start (EV_A_ (W)w, ++preparecnt);
  3889. array_needsize (ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
  3890. prepares [preparecnt - 1] = w;
  3891. EV_FREQUENT_CHECK;
  3892. }
  3893. void
  3894. ev_prepare_stop (EV_P_ ev_prepare *w) EV_NOEXCEPT
  3895. {
  3896. clear_pending (EV_A_ (W)w);
  3897. if (expect_false (!ev_is_active (w)))
  3898. return;
  3899. EV_FREQUENT_CHECK;
  3900. {
  3901. int active = ev_active (w);
  3902. prepares [active - 1] = prepares [--preparecnt];
  3903. ev_active (prepares [active - 1]) = active;
  3904. }
  3905. ev_stop (EV_A_ (W)w);
  3906. EV_FREQUENT_CHECK;
  3907. }
  3908. #endif
  3909. #if EV_CHECK_ENABLE
  3910. void
  3911. ev_check_start (EV_P_ ev_check *w) EV_NOEXCEPT
  3912. {
  3913. if (expect_false (ev_is_active (w)))
  3914. return;
  3915. EV_FREQUENT_CHECK;
  3916. ev_start (EV_A_ (W)w, ++checkcnt);
  3917. array_needsize (ev_check *, checks, checkmax, checkcnt, EMPTY2);
  3918. checks [checkcnt - 1] = w;
  3919. EV_FREQUENT_CHECK;
  3920. }
  3921. void
  3922. ev_check_stop (EV_P_ ev_check *w) EV_NOEXCEPT
  3923. {
  3924. clear_pending (EV_A_ (W)w);
  3925. if (expect_false (!ev_is_active (w)))
  3926. return;
  3927. EV_FREQUENT_CHECK;
  3928. {
  3929. int active = ev_active (w);
  3930. checks [active - 1] = checks [--checkcnt];
  3931. ev_active (checks [active - 1]) = active;
  3932. }
  3933. ev_stop (EV_A_ (W)w);
  3934. EV_FREQUENT_CHECK;
  3935. }
  3936. #endif
  3937. #if EV_EMBED_ENABLE
  3938. noinline
  3939. void
  3940. ev_embed_sweep (EV_P_ ev_embed *w) EV_NOEXCEPT
  3941. {
  3942. ev_run (w->other, EVRUN_NOWAIT);
  3943. }
  3944. static void
  3945. embed_io_cb (EV_P_ ev_io *io, int revents)
  3946. {
  3947. ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io));
  3948. if (ev_cb (w))
  3949. ev_feed_event (EV_A_ (W)w, EV_EMBED);
  3950. else
  3951. ev_run (w->other, EVRUN_NOWAIT);
  3952. }
  3953. static void
  3954. embed_prepare_cb (EV_P_ ev_prepare *prepare, int revents)
  3955. {
  3956. ev_embed *w = (ev_embed *)(((char *)prepare) - offsetof (ev_embed, prepare));
  3957. {
  3958. EV_P = w->other;
  3959. while (fdchangecnt)
  3960. {
  3961. fd_reify (EV_A);
  3962. ev_run (EV_A_ EVRUN_NOWAIT);
  3963. }
  3964. }
  3965. }
  3966. static void
  3967. embed_fork_cb (EV_P_ ev_fork *fork_w, int revents)
  3968. {
  3969. ev_embed *w = (ev_embed *)(((char *)fork_w) - offsetof (ev_embed, fork));
  3970. ev_embed_stop (EV_A_ w);
  3971. {
  3972. EV_P = w->other;
  3973. ev_loop_fork (EV_A);
  3974. ev_run (EV_A_ EVRUN_NOWAIT);
  3975. }
  3976. ev_embed_start (EV_A_ w);
  3977. }
  3978. #if 0
  3979. static void
  3980. embed_idle_cb (EV_P_ ev_idle *idle, int revents)
  3981. {
  3982. ev_idle_stop (EV_A_ idle);
  3983. }
  3984. #endif
  3985. void
  3986. ev_embed_start (EV_P_ ev_embed *w) EV_NOEXCEPT
  3987. {
  3988. if (expect_false (ev_is_active (w)))
  3989. return;
  3990. {
  3991. EV_P = w->other;
  3992. assert (("libev: loop to be embedded is not embeddable", backend & ev_embeddable_backends ()));
  3993. ev_io_init (&w->io, embed_io_cb, backend_fd, EV_READ);
  3994. }
  3995. EV_FREQUENT_CHECK;
  3996. ev_set_priority (&w->io, ev_priority (w));
  3997. ev_io_start (EV_A_ &w->io);
  3998. ev_prepare_init (&w->prepare, embed_prepare_cb);
  3999. ev_set_priority (&w->prepare, EV_MINPRI);
  4000. ev_prepare_start (EV_A_ &w->prepare);
  4001. ev_fork_init (&w->fork, embed_fork_cb);
  4002. ev_fork_start (EV_A_ &w->fork);
  4003. /*ev_idle_init (&w->idle, e,bed_idle_cb);*/
  4004. ev_start (EV_A_ (W)w, 1);
  4005. EV_FREQUENT_CHECK;
  4006. }
  4007. void
  4008. ev_embed_stop (EV_P_ ev_embed *w) EV_NOEXCEPT
  4009. {
  4010. clear_pending (EV_A_ (W)w);
  4011. if (expect_false (!ev_is_active (w)))
  4012. return;
  4013. EV_FREQUENT_CHECK;
  4014. ev_io_stop (EV_A_ &w->io);
  4015. ev_prepare_stop (EV_A_ &w->prepare);
  4016. ev_fork_stop (EV_A_ &w->fork);
  4017. ev_stop (EV_A_ (W)w);
  4018. EV_FREQUENT_CHECK;
  4019. }
  4020. #endif
  4021. #if EV_FORK_ENABLE
  4022. void
  4023. ev_fork_start (EV_P_ ev_fork *w) EV_NOEXCEPT
  4024. {
  4025. if (expect_false (ev_is_active (w)))
  4026. return;
  4027. EV_FREQUENT_CHECK;
  4028. ev_start (EV_A_ (W)w, ++forkcnt);
  4029. array_needsize (ev_fork *, forks, forkmax, forkcnt, EMPTY2);
  4030. forks [forkcnt - 1] = w;
  4031. EV_FREQUENT_CHECK;
  4032. }
  4033. void
  4034. ev_fork_stop (EV_P_ ev_fork *w) EV_NOEXCEPT
  4035. {
  4036. clear_pending (EV_A_ (W)w);
  4037. if (expect_false (!ev_is_active (w)))
  4038. return;
  4039. EV_FREQUENT_CHECK;
  4040. {
  4041. int active = ev_active (w);
  4042. forks [active - 1] = forks [--forkcnt];
  4043. ev_active (forks [active - 1]) = active;
  4044. }
  4045. ev_stop (EV_A_ (W)w);
  4046. EV_FREQUENT_CHECK;
  4047. }
  4048. #endif
  4049. #if EV_CLEANUP_ENABLE
  4050. void
  4051. ev_cleanup_start (EV_P_ ev_cleanup *w) EV_NOEXCEPT
  4052. {
  4053. if (expect_false (ev_is_active (w)))
  4054. return;
  4055. EV_FREQUENT_CHECK;
  4056. ev_start (EV_A_ (W)w, ++cleanupcnt);
  4057. array_needsize (ev_cleanup *, cleanups, cleanupmax, cleanupcnt, EMPTY2);
  4058. cleanups [cleanupcnt - 1] = w;
  4059. /* cleanup watchers should never keep a refcount on the loop */
  4060. ev_unref (EV_A);
  4061. EV_FREQUENT_CHECK;
  4062. }
  4063. void
  4064. ev_cleanup_stop (EV_P_ ev_cleanup *w) EV_NOEXCEPT
  4065. {
  4066. clear_pending (EV_A_ (W)w);
  4067. if (expect_false (!ev_is_active (w)))
  4068. return;
  4069. EV_FREQUENT_CHECK;
  4070. ev_ref (EV_A);
  4071. {
  4072. int active = ev_active (w);
  4073. cleanups [active - 1] = cleanups [--cleanupcnt];
  4074. ev_active (cleanups [active - 1]) = active;
  4075. }
  4076. ev_stop (EV_A_ (W)w);
  4077. EV_FREQUENT_CHECK;
  4078. }
  4079. #endif
  4080. #if EV_ASYNC_ENABLE
  4081. void
  4082. ev_async_start (EV_P_ ev_async *w) EV_NOEXCEPT
  4083. {
  4084. if (expect_false (ev_is_active (w)))
  4085. return;
  4086. w->sent = 0;
  4087. evpipe_init (EV_A);
  4088. EV_FREQUENT_CHECK;
  4089. ev_start (EV_A_ (W)w, ++asynccnt);
  4090. array_needsize (ev_async *, asyncs, asyncmax, asynccnt, EMPTY2);
  4091. asyncs [asynccnt - 1] = w;
  4092. EV_FREQUENT_CHECK;
  4093. }
  4094. void
  4095. ev_async_stop (EV_P_ ev_async *w) EV_NOEXCEPT
  4096. {
  4097. clear_pending (EV_A_ (W)w);
  4098. if (expect_false (!ev_is_active (w)))
  4099. return;
  4100. EV_FREQUENT_CHECK;
  4101. {
  4102. int active = ev_active (w);
  4103. asyncs [active - 1] = asyncs [--asynccnt];
  4104. ev_active (asyncs [active - 1]) = active;
  4105. }
  4106. ev_stop (EV_A_ (W)w);
  4107. EV_FREQUENT_CHECK;
  4108. }
  4109. void
  4110. ev_async_send (EV_P_ ev_async *w) EV_NOEXCEPT
  4111. {
  4112. w->sent = 1;
  4113. evpipe_write (EV_A_ &async_pending);
  4114. }
  4115. #endif
  4116. /*****************************************************************************/
  4117. struct ev_once
  4118. {
  4119. ev_io io;
  4120. ev_timer to;
  4121. void (*cb)(int revents, void *arg);
  4122. void *arg;
  4123. };
  4124. static void
  4125. once_cb (EV_P_ struct ev_once *once, int revents)
  4126. {
  4127. void (*cb)(int revents, void *arg) = once->cb;
  4128. void *arg = once->arg;
  4129. ev_io_stop (EV_A_ &once->io);
  4130. ev_timer_stop (EV_A_ &once->to);
  4131. ev_free (once);
  4132. cb (revents, arg);
  4133. }
  4134. static void
  4135. once_cb_io (EV_P_ ev_io *w, int revents)
  4136. {
  4137. struct ev_once *once = (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io));
  4138. once_cb (EV_A_ once, revents | ev_clear_pending (EV_A_ &once->to));
  4139. }
  4140. static void
  4141. once_cb_to (EV_P_ ev_timer *w, int revents)
  4142. {
  4143. struct ev_once *once = (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to));
  4144. once_cb (EV_A_ once, revents | ev_clear_pending (EV_A_ &once->io));
  4145. }
  4146. void
  4147. ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) EV_NOEXCEPT
  4148. {
  4149. struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once));
  4150. if (expect_false (!once))
  4151. {
  4152. cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMER, arg);
  4153. return;
  4154. }
  4155. once->cb = cb;
  4156. once->arg = arg;
  4157. ev_init (&once->io, once_cb_io);
  4158. if (fd >= 0)
  4159. {
  4160. ev_io_set (&once->io, fd, events);
  4161. ev_io_start (EV_A_ &once->io);
  4162. }
  4163. ev_init (&once->to, once_cb_to);
  4164. if (timeout >= 0.)
  4165. {
  4166. ev_timer_set (&once->to, timeout, 0.);
  4167. ev_timer_start (EV_A_ &once->to);
  4168. }
  4169. }
  4170. /*****************************************************************************/
  4171. #if EV_WALK_ENABLE
  4172. ecb_cold
  4173. void
  4174. ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) EV_NOEXCEPT
  4175. {
  4176. int i, j;
  4177. ev_watcher_list *wl, *wn;
  4178. if (types & (EV_IO | EV_EMBED))
  4179. for (i = 0; i < anfdmax; ++i)
  4180. for (wl = anfds [i].head; wl; )
  4181. {
  4182. wn = wl->next;
  4183. #if EV_EMBED_ENABLE
  4184. if (ev_cb ((ev_io *)wl) == embed_io_cb)
  4185. {
  4186. if (types & EV_EMBED)
  4187. cb (EV_A_ EV_EMBED, ((char *)wl) - offsetof (struct ev_embed, io));
  4188. }
  4189. else
  4190. #endif
  4191. #if EV_USE_INOTIFY
  4192. if (ev_cb ((ev_io *)wl) == infy_cb)
  4193. ;
  4194. else
  4195. #endif
  4196. if ((ev_io *)wl != &pipe_w)
  4197. if (types & EV_IO)
  4198. cb (EV_A_ EV_IO, wl);
  4199. wl = wn;
  4200. }
  4201. if (types & (EV_TIMER | EV_STAT))
  4202. for (i = timercnt + HEAP0; i-- > HEAP0; )
  4203. #if EV_STAT_ENABLE
  4204. /*TODO: timer is not always active*/
  4205. if (ev_cb ((ev_timer *)ANHE_w (timers [i])) == stat_timer_cb)
  4206. {
  4207. if (types & EV_STAT)
  4208. cb (EV_A_ EV_STAT, ((char *)ANHE_w (timers [i])) - offsetof (struct ev_stat, timer));
  4209. }
  4210. else
  4211. #endif
  4212. if (types & EV_TIMER)
  4213. cb (EV_A_ EV_TIMER, ANHE_w (timers [i]));
  4214. #if EV_PERIODIC_ENABLE
  4215. if (types & EV_PERIODIC)
  4216. for (i = periodiccnt + HEAP0; i-- > HEAP0; )
  4217. cb (EV_A_ EV_PERIODIC, ANHE_w (periodics [i]));
  4218. #endif
  4219. #if EV_IDLE_ENABLE
  4220. if (types & EV_IDLE)
  4221. for (j = NUMPRI; j--; )
  4222. for (i = idlecnt [j]; i--; )
  4223. cb (EV_A_ EV_IDLE, idles [j][i]);
  4224. #endif
  4225. #if EV_FORK_ENABLE
  4226. if (types & EV_FORK)
  4227. for (i = forkcnt; i--; )
  4228. if (ev_cb (forks [i]) != embed_fork_cb)
  4229. cb (EV_A_ EV_FORK, forks [i]);
  4230. #endif
  4231. #if EV_ASYNC_ENABLE
  4232. if (types & EV_ASYNC)
  4233. for (i = asynccnt; i--; )
  4234. cb (EV_A_ EV_ASYNC, asyncs [i]);
  4235. #endif
  4236. #if EV_PREPARE_ENABLE
  4237. if (types & EV_PREPARE)
  4238. for (i = preparecnt; i--; )
  4239. # if EV_EMBED_ENABLE
  4240. if (ev_cb (prepares [i]) != embed_prepare_cb)
  4241. # endif
  4242. cb (EV_A_ EV_PREPARE, prepares [i]);
  4243. #endif
  4244. #if EV_CHECK_ENABLE
  4245. if (types & EV_CHECK)
  4246. for (i = checkcnt; i--; )
  4247. cb (EV_A_ EV_CHECK, checks [i]);
  4248. #endif
  4249. #if EV_SIGNAL_ENABLE
  4250. if (types & EV_SIGNAL)
  4251. for (i = 0; i < EV_NSIG - 1; ++i)
  4252. for (wl = signals [i].head; wl; )
  4253. {
  4254. wn = wl->next;
  4255. cb (EV_A_ EV_SIGNAL, wl);
  4256. wl = wn;
  4257. }
  4258. #endif
  4259. #if EV_CHILD_ENABLE
  4260. if (types & EV_CHILD)
  4261. for (i = (EV_PID_HASHSIZE); i--; )
  4262. for (wl = childs [i]; wl; )
  4263. {
  4264. wn = wl->next;
  4265. cb (EV_A_ EV_CHILD, wl);
  4266. wl = wn;
  4267. }
  4268. #endif
  4269. }
  4270. #endif
  4271. #if EV_MULTIPLICITY
  4272. #include "ev_wrap.h"
  4273. #endif