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 Prelude
import Control.Monad.Error.Class (liftMaybe, throwError) import Control.Monad.Error.Class (liftMaybe, throwError)
import Control.Monad.Rec.Class (whileJust)
import Data.Array as Array import Data.Array as Array
import Data.Async.Class (class AsyncState, class AsyncStateLock, class AsyncStateReadable, class AsyncStateWritable, lock) import Data.Async.Class (class AsyncState, class AsyncStateLock, class AsyncStateReadable, class AsyncStateWritable, lock)
import Data.Either (Either(..)) import Data.Either (Either(..))
@ -70,17 +71,16 @@ instance AsyncState RwLock WriteGuard ReadGuard where
pure $ RwLock { state, nextReaderId, readers, writerQueue } pure $ RwLock { state, nextReaderId, readers, writerQueue }
instance AsyncStateWritable RwLock WriteGuard where instance AsyncStateWritable RwLock WriteGuard where
write _ (WriteGuard stateCell) s = write _ (WriteGuard stateCell) s = do
liftAff void $ liftAff $ liftMaybe (error "WriteGuard used after `unlock` invoked!") =<< AVar.tryTake stateCell
$ const (AVar.put s stateCell) liftAff $ void $ whileJust $ void <$> AVar.tryTake stateCell
=<< liftMaybe (error "WriteGuard used after `unlock` invoked!") liftAff $ AVar.put s stateCell
=<< AVar.tryTake stateCell
instance AsyncStateReadable RwLock WriteGuard where instance AsyncStateReadable RwLock WriteGuard where
read _ (WriteGuard stateCell) = read _ (WriteGuard stateCell) =
liftAff liftAff
$ liftMaybe (error "WriteGuard used after `unlock` invoked!") $ liftMaybe (error "WriteGuard used after `unlock` invoked!")
=<< AVar.tryTake stateCell =<< AVar.tryRead stateCell
instance AsyncStateReadable RwLock ReadGuard where instance AsyncStateReadable RwLock ReadGuard where
read (RwLock { readers: readersCell }) (ReadGuard id a) = liftAff do read (RwLock { readers: readersCell }) (ReadGuard id a) = liftAff do