nano-ui-rgfw-0.2.0.0: test/NanoUI/Rgfw/Wake.hs
-- | The RGFW session's wake: another thread can interrupt the loop's event wait.
module NanoUI.Rgfw.Wake (testRgfwWake) where
import Control.Concurrent
(forkIO, getNumCapabilities, killThread, newEmptyMVar, setNumCapabilities, takeMVar, threadDelay, tryPutMVar, yield)
import Control.Exception (SomeException, try)
import Control.Monad (join, void, when)
import Control.Monad.IO.Class (liftIO)
import Data.IORef (atomicModifyIORef', atomicWriteIORef, newIORef, readIORef, writeIORef)
import Foreign.C.Types (CInt (..))
import GHC.Clock (getMonotonicTime)
import NanoUI
(Size (..), WindowMode (..), WindowSettings (..), askWake, defaultWindowSettings, label, newTask, quit, useTask)
import NanoUI.Backend.Rgfw (RgfwOptions (..), defaultRgfwOptions, runRgfwApp)
import NanoUI.Rgfw.Window (hasDisplay)
import System.CPUTime (getCPUTime)
import System.IO (hFlush, stdout)
import System.Timeout (timeout)
foreign import ccall "exit" c_exit :: CInt -> IO ()
-- | A background job's wake shows its result while the loop waits for
-- events; after a stream faster than frames stops, the loop idles instead of
-- spinning; 'quit' ends the session. Skipped on Linux without an X
-- display, and without an OpenGL 3.2 window. A missed wake would block
-- forever, so a failed check exits the process.
testRgfwWake :: IO ()
testRgfwWake =
hasDisplay >>= \case
False -> putStrLn "[SKIP] RGFW wake: no X display"
True -> do
-- Two capabilities let the producer wake the loop while it is drawing,
-- not only while it waits.
caps <- getNumCapabilities
when (caps < 2) (setNumCapabilities 2)
passes <- newIORef (0 :: Int)
phase <- newIORef (0 :: Int)
wakeRef <- newIORef (pure ())
latest <- newIORef (0 :: Int)
shown <- newEmptyMVar
streamed <- newEmptyMVar
job <- newTask
producer <- newTask
let failNow msg = putStrLn ("[FAIL] RGFW wake: " ++ msg) >> hFlush stdout >> c_exit 1
await what signal = timeout 5000000 (takeMVar signal) >>= maybe (failNow (what ++ " never showed")) pure
next p = writeIORef phase p >> join (readIORef wakeRef)
-- For a fifth of a second, write each value where the view reads it
-- and wake, faster than frames; then write -1 to mark the end.
produce wake = do
t0 <- getMonotonicTime
let go i = do
atomicWriteIORef latest i >> wake
t <- getMonotonicTime
if t - t0 < 0.2 then yield >> go (i + 1) else atomicWriteIORef latest (-1) >> wake
go 1
view = do
liftIO (atomicModifyIORef' passes (\n -> (n + 1, ())))
wake <- askWake
liftIO (writeIORef wakeRef wake)
liftIO (readIORef phase) >>= \case
0 -> do
r <- useTask job ("job" :: String) (threadDelay 50000 >> pure (42 :: Int))
when (r == Just 42) (liftIO (void (tryPutMVar shown ())))
label "job"
1 -> do
_ <- useTask producer ("stream" :: String) (produce wake)
v <- liftIO (readIORef latest)
when (v == -1) (liftIO (void (tryPutMVar streamed ())))
label "stream"
_ -> quit
watcher <- forkIO $ do
await "the job's result" shown
next 1
await "the stream's last value" streamed
threadDelay 100000
c0 <- getCPUTime
f0 <- readIORef passes
threadDelay 400000
c1 <- getCPUTime
f1 <- readIORef passes
let cpu = fromIntegral (c1 - c0) / 1e12 :: Double
when (cpu > 0.12 || f1 /= f0) $
failNow ("after the stream the loop used " ++ show cpu ++ " s of CPU and ran " ++ show (f1 - f0) ++ " passes")
next 2
-- Hidden, so pointer motion over it cannot run passes while it idles.
let hidden = defaultWindowSettings {wsSize = Size 200 150, wsMode = Hidden}
result <- try (runRgfwApp defaultRgfwOptions {optWindow = hidden} view)
ran <- readIORef passes
case result of
_ | ran == 0 -> killThread watcher >> putStrLn "[SKIP] RGFW wake: no OpenGL 3.2 window"
Right () ->
readIORef phase >>= \case
2 -> putStrLn "[PASS] RGFW wake: a job's result and a stream show, the loop idles, and quit ends it"
_ -> failNow "the session ended by itself"
Left (e :: SomeException) -> failNow ("the session ended with " ++ show e)