diff --git a/src/Data.Async.RwLock.purs b/src/Data.Async.RwLock.purs index 0d1ebd2..2c738c5 100644 --- a/src/Data.Async.RwLock.purs +++ b/src/Data.Async.RwLock.purs @@ -3,6 +3,7 @@ module Data.Async.RwLock (RwLock, ReadGuard, WriteGuard) where import Prelude import Control.Monad.Error.Class (liftMaybe, throwError) +import Control.Monad.Rec.Class (whileJust) import Data.Array as Array import Data.Async.Class (class AsyncState, class AsyncStateLock, class AsyncStateReadable, class AsyncStateWritable, lock) import Data.Either (Either(..)) @@ -70,17 +71,16 @@ instance AsyncState RwLock WriteGuard ReadGuard where pure $ RwLock { state, nextReaderId, readers, writerQueue } instance AsyncStateWritable RwLock WriteGuard where - write _ (WriteGuard stateCell) s = - liftAff - $ const (AVar.put s stateCell) - =<< liftMaybe (error "WriteGuard used after `unlock` invoked!") - =<< AVar.tryTake stateCell + write _ (WriteGuard stateCell) s = do + void $ liftAff $ liftMaybe (error "WriteGuard used after `unlock` invoked!") =<< AVar.tryTake stateCell + liftAff $ void $ whileJust $ void <$> AVar.tryTake stateCell + liftAff $ AVar.put s stateCell instance AsyncStateReadable RwLock WriteGuard where read _ (WriteGuard stateCell) = liftAff $ liftMaybe (error "WriteGuard used after `unlock` invoked!") - =<< AVar.tryTake stateCell + =<< AVar.tryRead stateCell instance AsyncStateReadable RwLock ReadGuard where read (RwLock { readers: readersCell }) (ReadGuard id a) = liftAff do