Skip to content

Commit 49bb1c8

Browse files
committed
Prepare mechanisms to release AI sessions.
refs #44. Not used yet.
1 parent 5a4742e commit 49bb1c8

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/AI/Session.hs

+20-2
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,36 @@ newAiSession = do
2828
}
2929
return (sessionId, newSession)
3030

31+
releaseAiSession :: AiSessionId -> Checkers ()
32+
releaseAiSession sessionId = do
33+
var <- askSupervisor
34+
ok <- liftIO $ atomically $ stateTVar var $ \st -> do
35+
case M.lookup sessionId (ssAiSessions st) of
36+
Nothing -> (False, st)
37+
Just session ->
38+
let st' = st {
39+
ssAiSessions = M.delete sessionId (ssAiSessions st)
40+
}
41+
in (True, st')
42+
when (not ok) $
43+
throwError NoSuchAiSession
44+
45+
returnBoard :: AiSession -> Board -> Checkers ()
46+
returnBoard session board =
47+
liftIO $ putMVar (aiResult session) board
48+
3149
signalStopAiSession :: AiSessionId -> Checkers ()
3250
signalStopAiSession sessionId = do
3351
var <- askSupervisor
34-
st <- liftIO $ atomically $ readTVar var
52+
st <- liftIO $ readTVarIO var
3553
case M.lookup sessionId (ssAiSessions st) of
3654
Nothing -> throwError NoSuchAiSession
3755
Just session -> liftIO $ void $ tryPutMVar (aiStopSignal session) ()
3856

3957
getAiSessionStatus :: AiSessionId -> Checkers AiSessionStatus
4058
getAiSessionStatus sessionId = do
4159
var <- askSupervisor
42-
st <- liftIO $ atomically $ readTVar var
60+
st <- liftIO $ readTVarIO var
4361
case M.lookup sessionId (ssAiSessions st) of
4462
Nothing -> return NoAiHere
4563
Just state -> do

src/Core/Supervisor.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ letAiMove separateThread gameId side mbBoard = do
762762
$debug "Messages: {}" (Single $ show messages)
763763
afterMoveSelected ai storage gameId side board' aiMove
764764
queueNotifications (getGameId game) messages
765-
liftIO $ putMVar (aiResult aiSession) board'
765+
returnBoard aiSession board'
766766
afterMove gameId side
767767
return ()
768768
return (Just sessionId)

0 commit comments

Comments
 (0)