Movatterモバイル変換


[0]ホーム

URL:


{-# LINE1"GHC/Event/Poll.hsc"#-}{-# LANGUAGE Trustworthy #-}{-# LANGUAGE GeneralizedNewtypeDeriving           , NoImplicitPrelude           , BangPatterns  #-}moduleGHC.Event.Poll(new,available)where{-# LINE26"GHC/Event/Poll.hsc"#-}importControl.Concurrent.MVar(MVar,newMVar,swapMVar)importData.Bits(Bits,FiniteBits,(.|.),(.&.))importData.WordimportForeign.C.Types(CInt(..),CShort(..))importForeign.Ptr(Ptr)importForeign.Storable(Storable(..))importGHC.BaseimportGHC.Conc.Sync(withMVar)importGHC.Enum(maxBound)importGHC.Num(Num(..))importGHC.Real(fromIntegral,div)importGHC.Show(Show)importSystem.Posix.Types(Fd(..))importqualifiedGHC.Event.ArrayasAimportqualifiedGHC.Event.InternalasEavailable::Boolavailable=True{-# INLINEavailable#-}dataPoll=Poll{pollChanges::{-# UNPACK#-}!(MVar(A.ArrayPollFd)),pollFd::{-# UNPACK#-}!(A.ArrayPollFd)}new::IOE.Backendnew=E.backendpollmodifyFdmodifyFdOnce(\_->return())`liftM`liftM2Poll(newMVar=<<A.empty)A.emptymodifyFd::Poll->Fd->E.Event->E.Event->IOBoolmodifyFdpfdoevtnevt=withMVar(pollChangesp)$\ary->doA.snocary$PollFdfd(fromEventnevt)(fromEventoevt)returnTruemodifyFdOnce::Poll->Fd->E.Event->IOBoolmodifyFdOnce=errorWithoutStackTrace"modifyFdOnce not supported in Poll backend"reworkFd::Poll->PollFd->IO()reworkFdp(PollFdfdnpevtopevt)=doletary=pollFdpifopevt==0thenA.snocary$PollFdfdnpevt0elsedofound<-A.findIndex((==fd).pfdFd)arycasefoundofNothing->errorWithoutStackTrace"reworkFd: event not found"Just(i,_)|npevt/=0->A.unsafeWritearyi$PollFdfdnpevt0|otherwise->A.removeAtaryipoll::Poll->MaybeE.Timeout->(Fd->E.Event->IO())->IOIntpollpmtoutf=doleta=pollFdpmods<-swapMVar(pollChangesp)=<<A.emptyA.forM_mods(reworkFdp)n<-A.useAsPtra$\ptrlen->E.throwErrnoIfMinus1NoRetry"c_poll"$casemtoutofJusttout->c_pollLoopptr(fromIntegrallen)(fromTimeouttout)Nothing->c_poll_unsafeptr(fromIntegrallen)0when(n/=0)$doA.loopa0$\ie->doletr=pfdReventseifr/=0thendof(pfdFde)(toEventr)leti'=i+1return(i',i'==n)elsereturn(i,True)return(fromIntegraln)where-- The poll timeout is specified as an Int, but c_poll takes a CInt. These-- can't be safely coerced as on many systems (e.g. x86_64) CInt has a-- maxBound of (2^32 - 1), even though Int may have a significantly higher-- bound.---- This function deals with timeouts greater than maxBound :: CInt, by-- looping until c_poll returns a non-zero value (0 indicates timeout-- expired) OR the full timeout has passed.c_pollLoop::PtrPollFd->(Word64)->Int->IOCInt{-# LINE114"GHC/Event/Poll.hsc"#-}c_pollLoopptrlentout|isShortTimeout=c_pollptrlen(fromIntegraltout)|otherwise=doresult<-c_pollptrlen(fromIntegralmaxPollTimeout)ifresult==0thenc_pollLoopptrlen(fromIntegral(tout-maxPollTimeout))elsereturnresultwhere-- maxPollTimeout is smaller than 0 IFF Int is smaller than CInt.-- This means any possible Int input to poll can be safely directly-- converted to CInt.isShortTimeout=tout<=maxPollTimeout||maxPollTimeout<0-- We need to account for 3 cases:--     1. Int and CInt are of equal size.--     2. Int is larger than CInt--     3. Int is smaller than CInt---- In case 1, the value of maxPollTimeout will be the maxBound of Int.---- In case 2, the value of maxPollTimeout will be the maxBound of CInt,-- which is the largest value accepted by c_poll. This will result in-- c_pollLoop recursing if the provided timeout is larger.---- In case 3, "fromIntegral (maxBound :: CInt) :: Int" will result in a-- negative Int. This will cause isShortTimeout to be true and result in-- the timeout being directly converted to a CInt.maxPollTimeout::IntmaxPollTimeout=fromIntegral(maxBound::CInt)fromTimeout::E.Timeout->IntfromTimeoutE.Forever=-1fromTimeout(E.Timeouts)=fromIntegral$s`divRoundUp`1000000wheredivRoundUpnumdenom=(num+denom-1)`div`denomdataPollFd=PollFd{pfdFd::{-# UNPACK#-}!Fd,pfdEvents::{-# UNPACK#-}!Event,pfdRevents::{-# UNPACK#-}!Event}derivingShow-- ^ @since 4.4.0.0newtypeEvent=EventCShortderiving(Eq-- ^ @since 4.4.0.0,Show-- ^ @since 4.4.0.0,Num-- ^ @since 4.4.0.0,Storable-- ^ @since 4.4.0.0,Bits-- ^ @since 4.4.0.0,FiniteBits-- ^ @since 4.7.0.0)-- We have to duplicate the whole enum like this in order for the-- hsc2hs cross-compilation mode to work{-# LINE176"GHC/Event/Poll.hsc"#-}pollIn::EventpollIn=Event1pollOut::EventpollOut=Event4pollErr::EventpollErr=Event8pollHup::EventpollHup=Event16{-# LINE182"GHC/Event/Poll.hsc"#-}{-# LINE183"GHC/Event/Poll.hsc"#-}fromEvent::E.Event->EventfromEvente=remapE.evtReadpollIn.|.remapE.evtWritepollOutwhereremapevtto|e`E.eventIs`evt=to|otherwise=0toEvent::Event->E.EventtoEvente=remap(pollIn.|.pollErr.|.pollHup)E.evtRead`mappend`remap(pollOut.|.pollErr.|.pollHup)E.evtWritewhereremapevtto|e.&.evt/=0=to|otherwise=mempty-- | @since 4.3.1.0instanceStorablePollFdwheresizeOf_=(8){-# LINE201"GHC/Event/Poll.hsc"#-}alignment_=alignment(undefined::CInt)peekptr=dofd<-(\hsc_ptr->peekByteOffhsc_ptr0)ptr{-# LINE205"GHC/Event/Poll.hsc"#-}events<-(\hsc_ptr->peekByteOffhsc_ptr4)ptr{-# LINE206"GHC/Event/Poll.hsc"#-}revents<-(\hsc_ptr->peekByteOffhsc_ptr6)ptr{-# LINE207"GHC/Event/Poll.hsc"#-}let!pollFd'=PollFdfdeventsreventsreturnpollFd'pokeptrp=do(\hsc_ptr->pokeByteOffhsc_ptr0)ptr(pfdFdp){-# LINE212"GHC/Event/Poll.hsc"#-}(\hsc_ptr->pokeByteOffhsc_ptr4)ptr(pfdEventsp){-# LINE213"GHC/Event/Poll.hsc"#-}(\hsc_ptr->pokeByteOffhsc_ptr6)ptr(pfdReventsp){-# LINE214"GHC/Event/Poll.hsc"#-}foreignimportccallsafe"poll.h poll"c_poll::PtrPollFd->(Word64)->CInt->IOCInt{-# LINE217"GHC/Event/Poll.hsc"#-}foreignimportccallunsafe"poll.h poll"c_poll_unsafe::PtrPollFd->(Word64)->CInt->IOCInt{-# LINE220"GHC/Event/Poll.hsc"#-}{-# LINE221"GHC/Event/Poll.hsc"#-}

[8]ページ先頭

©2009-2025 Movatter.jp