mirror of
https://github.com/simplex-chat/simplexmq.git
synced 2026-04-25 18:32:21 +00:00
* xftp: agent API for xftp commands and events * fix tests * fix tests 2 * xftp: update agent send api * update API to make temp path optional * revert tmp path changes (fixes send) --------- Co-authored-by: spacedandy <8711996+spaced4ndy@users.noreply.github.com>
29 lines
710 B
Haskell
29 lines
710 B
Haskell
{-# LANGUAGE NamedFieldPuns #-}
|
|
|
|
module Simplex.Messaging.Agent.Lock where
|
|
|
|
import Control.Monad (void)
|
|
import Control.Monad.IO.Unlift
|
|
import Data.Functor (($>))
|
|
import qualified UnliftIO.Exception as E
|
|
import UnliftIO.STM
|
|
|
|
type Lock = TMVar String
|
|
|
|
createLock :: STM Lock
|
|
createLock = newEmptyTMVar
|
|
{-# INLINE createLock #-}
|
|
|
|
withLock :: MonadUnliftIO m => Lock -> String -> m a -> m a
|
|
withLock lock name =
|
|
E.bracket_
|
|
(atomically $ putTMVar lock name)
|
|
(void . atomically $ takeTMVar lock)
|
|
|
|
withGetLock :: MonadUnliftIO m => STM Lock -> String -> m a -> m a
|
|
withGetLock getLock name a =
|
|
E.bracket
|
|
(atomically $ getLock >>= \l -> putTMVar l name $> l)
|
|
(atomically . takeTMVar)
|
|
(const a)
|