{-# LINE1"GHC/IO/Handle/Lock.hsc"#-}{-# LANGUAGE CPP #-}{-# LANGUAGE InterruptibleFFI #-}{-# LANGUAGE LambdaCase #-}{-# LANGUAGE MultiWayIf #-}{-# LANGUAGE NoImplicitPrelude #-}moduleGHC.IO.Handle.Lock(FileLockingNotSupported(..),LockMode(..),hLock,hTryLock,hUnlock)where{-# LINE17"GHC/IO/Handle/Lock.hsc"#-}importData.BitsimportData.FunctionimportForeign.C.ErrorimportForeign.C.TypesimportGHC.IO.ExceptionimportGHC.IO.FDimportGHC.IO.Handle.FD{-# LINE56"GHC/IO/Handle/Lock.hsc"#-}importData.FunctorimportGHC.BaseimportGHC.ExceptionimportGHC.IO.Handle.TypesimportGHC.Show-- | Exception thrown by 'hLock' on non-Windows platforms that don't support-- 'flock'.dataFileLockingNotSupported=FileLockingNotSupportedderivingShow-- ^ @since 4.10.0.0-- ^ @since 4.10.0.0instanceExceptionFileLockingNotSupported-- | Indicates a mode in which a file should be locked.dataLockMode=SharedLock|ExclusiveLock-- | If a 'Handle' references a file descriptor, attempt to lock contents of the-- underlying file in appropriate mode. If the file is already locked in-- incompatible mode, this function blocks until the lock is established. The-- lock is automatically released upon closing a 'Handle'.---- Things to be aware of:---- 1) This function may block inside a C call. If it does, in order to be able-- to interrupt it with asynchronous exceptions and/or for other threads to-- continue working, you MUST use threaded version of the runtime system.---- 2) The implementation uses 'LockFileEx' on Windows and 'flock' otherwise,-- hence all of their caveats also apply here.---- 3) On non-Windows plaftorms that don't support 'flock' (e.g. Solaris) this-- function throws 'FileLockingNotImplemented'. We deliberately choose to not-- provide fcntl based locking instead because of its broken semantics.---- @since 4.10.0.0hLock::Handle->LockMode->IO()hLockhmode=void$lockImplh"hLock"modeTrue-- | Non-blocking version of 'hLock'.---- @since 4.10.0.0hTryLock::Handle->LockMode->IOBoolhTryLockhmode=lockImplh"hTryLock"modeFalse-- | Release a lock taken with 'hLock' or 'hTryLock'.hUnlock::Handle->IO()hUnlock=unlockImpl----------------------------------------{-# LINE178"GHC/IO/Handle/Lock.hsc"#-}lockImpl::Handle->String->LockMode->Bool->IOBoollockImplhctxmodeblock=doFD{fdFD=fd}<-handleToFdhletflags=cmode.|.(ifblockthen0else4){-# LINE183"GHC/IO/Handle/Lock.hsc"#-}fix$\retry->c_flockfdflags>>=\case0->returnTrue_->getErrno>>=\errno->if|notblock,errno==eAGAIN||errno==eACCES->returnFalse|errno==eINTR->retry|otherwise->ioException$errnoToIOErrorctxerrno(Justh)Nothingwherecmode=casemodeofSharedLock->1{-# LINE193"GHC/IO/Handle/Lock.hsc"#-}ExclusiveLock->2{-# LINE194"GHC/IO/Handle/Lock.hsc"#-}unlockImpl::Handle->IO()unlockImplh=doFD{fdFD=fd}<-handleToFdhthrowErrnoIfMinus1_"flock"$c_flockfd8{-# LINE199"GHC/IO/Handle/Lock.hsc"#-}foreignimportccallinterruptible"flock"c_flock::CInt->CInt->IOCInt{-# LINE265"GHC/IO/Handle/Lock.hsc"#-}