fix: bug???

This commit is contained in:
orion 2023-11-06 17:42:16 -06:00
parent 3b347fe473
commit fc10c858f8
Signed by: orion
GPG Key ID: 6D4165AE4C928719

View File

@ -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