ev.c 126 KB

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