dist-install/build/GHC/IO/Handle/Lock/Flock.hs

    {-# LINE 1 "libraries/base/GHC/IO/Handle/Lock/Flock.hsc" #-}
    {-# LANGUAGE CPP #-}
    {-# LANGUAGE InterruptibleFFI #-}
    {-# LANGUAGE LambdaCase #-}
    {-# LANGUAGE MultiWayIf #-}
    {-# LANGUAGE NoImplicitPrelude #-}

    -- | File locking via POSIX @flock@.
    module GHC.IO.Handle.Lock.Flock where

    {-# LINE 15 "libraries/base/GHC/IO/Handle/Lock/Flock.hsc" #-}

    import Data.Bits
    import Data.Function
    import Foreign.C.Error
    import Foreign.C.Types
    import GHC.Base
    import GHC.IO.Exception
    import GHC.IO.FD
    import GHC.IO.Handle.FD
    import GHC.IO.Handle.Lock.Common
    import GHC.IO.Handle.Types (Handle)

    lockImpl :: Handle -> String -> LockMode -> Bool -> IO Bool
    lockImpl h ctx mode block = do
      FD{fdFD = fd} <- handleToFd h
      let flags = cmode .|. (if block then 0 else 4)
    {-# LINE 33 "libraries/base/GHC/IO/Handle/Lock/Flock.hsc" #-}
      fix $ \retry -> c_flock fd flags >>= \case
        0 -> return True
        _ -> getErrno >>= \errno -> if
          | not block
          , errno == eAGAIN || errno == eACCES -> return False
          | errno == eINTR -> retry
          | otherwise -> ioException $ errnoToIOError ctx errno (Just h) Nothing
      where
        cmode = case mode of
          SharedLock    -> 1
    {-# LINE 43 "libraries/base/GHC/IO/Handle/Lock/Flock.hsc" #-}
          ExclusiveLock -> 2
    {-# LINE 44 "libraries/base/GHC/IO/Handle/Lock/Flock.hsc" #-}

    unlockImpl :: Handle -> IO ()
    unlockImpl h = do
      FD{fdFD = fd} <- handleToFd h
      throwErrnoIfMinus1_ "flock" $ c_flock fd 8
    {-# LINE 49 "libraries/base/GHC/IO/Handle/Lock/Flock.hsc" #-}

    foreign import ccall interruptible "flock"
      c_flock :: CInt -> CInt -> IO CInt

    {-# LINE 54 "libraries/base/GHC/IO/Handle/Lock/Flock.hsc" #-}