{-# LINE 1 "src-flock/Lukko/FLock.hsc" #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE InterruptibleFFI #-}
{-# LANGUAGE Trustworthy #-}
module Lukko.FLock (
FileLockingNotSupported(..),
fileLockingSupported,
FileLockingSupported,
FileLockingMethod (..),
fileLockingMethod,
LockMode(..),
FD,
fdOpen,
fdClose,
fdLock,
fdTryLock,
fdUnlock,
hLock,
hTryLock,
hUnlock,
) where
import Control.Monad (void)
import System.IO (Handle)
import Data.Bits
import Data.Function
import Foreign.C.Error
import Foreign.C.Types
import GHC.Base
import GHC.IO.Exception
import Lukko.Internal.FD
import Lukko.Internal.Types
fileLockingSupported :: Bool
fileLockingSupported = True
type FileLockingSupported = True
fileLockingMethod :: FileLockingMethod
fileLockingMethod = MethodFLock
fdLock :: FD -> LockMode -> IO ()
fdLock fd mode = void (lockImpl Nothing fd "fdLock" mode True)
fdTryLock :: FD -> LockMode -> IO Bool
fdTryLock fd mode = lockImpl Nothing fd "fdTryLock" mode False
fdUnlock :: FD -> IO ()
fdUnlock = unlockImpl
hLock :: Handle -> LockMode -> IO ()
hLock h mode = do
fd <- handleToFd h
void (lockImpl (Just h) fd "hLock" mode True)
hTryLock :: Handle -> LockMode -> IO Bool
hTryLock h mode = do
fd <- handleToFd h
lockImpl (Just h) fd "hTryLock" mode False
hUnlock :: Handle -> IO ()
hUnlock h = do
fd <- handleToFd h
unlockImpl fd
lockImpl :: Maybe Handle -> FD -> String -> LockMode -> Bool -> IO Bool
lockImpl mh (FD fd) ctx mode block = do
let flags = cmode .|. (if block then 0 else 4)
{-# LINE 106 "src-flock/Lukko/FLock.hsc" #-}
fix $ \retry -> c_flock fd flags >>= \res -> case res of
0 -> return True
_ -> getErrno >>= \errno -> case () of
_ | not block
, errno == eAGAIN || errno == eACCES -> return False
| errno == eINTR -> retry
| otherwise -> ioException $ errnoToIOError ctx errno mh Nothing
where
cmode = case mode of
SharedLock -> 1
{-# LINE 116 "src-flock/Lukko/FLock.hsc" #-}
ExclusiveLock -> 2
{-# LINE 117 "src-flock/Lukko/FLock.hsc" #-}
unlockImpl :: FD -> IO ()
unlockImpl (FD fd) = do
throwErrnoIfMinus1_ "flock" $ c_flock fd 8
{-# LINE 121 "src-flock/Lukko/FLock.hsc" #-}
foreign import ccall interruptible "flock"
c_flock :: CInt -> CInt -> IO CInt