Last active
January 4, 2023 22:41
-
-
Save TerrorJack/88c06bad8580167212b51e1839cf2a75 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ghci -fobject-code StgTicket.cmm Ticket.hs | |
GHCi, version 9.4.4: https://www.haskell.org/ghc/ :? for help | |
[1 of 1] Compiling Ticket ( Ticket.hs, Ticket.o ) [Source file changed] | |
Ok, one module loaded. | |
ghci> import Data.IORef | |
ghci> import System.Mem | |
ghci> ref <- newIORef =<< mkTicket | |
ghci> flip addFinalizer (putStrLn "YOLO") =<< readIORef ref | |
ghci> performGC | |
ghci> writeIORef ref undefined | |
ghci> performGC | |
YOLO | |
ghci> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Cmm.h" | |
INFO_TABLE(stg_TICKET, 0, 0, PRIM, "TICKET", "TICKET") | |
() | |
{ | |
return (); | |
} | |
stg_newTicketzh () | |
{ | |
P_ ticket; | |
ALLOC_PRIM_ (SIZEOF_StgHeader, stg_newTicketzh); | |
ticket = Hp - SIZEOF_StgHeader + WDS(1); | |
SET_HDR(ticket,stg_TICKET_info,CCCS); | |
return (ticket); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE GHCForeignImportPrim #-} | |
{-# LANGUAGE MagicHash #-} | |
{-# LANGUAGE UnboxedTuples #-} | |
{-# LANGUAGE UnliftedFFITypes #-} | |
module Ticket | |
( Ticket, | |
mkTicket, | |
addFinalizer, | |
) | |
where | |
import GHC.Exts | |
import GHC.Types | |
newtype Ticket = MkTicket Any | |
mkTicket :: IO Ticket | |
mkTicket = IO $ \s0 -> case stg_newTicket# s0 of | |
(# s1, t #) -> (# s1, MkTicket t #) | |
addFinalizer :: Ticket -> IO () -> IO () | |
addFinalizer t (IO fin) = IO $ \s0 -> case mkWeak# t t fin s0 of | |
(# s1, _ #) -> (# s1, () #) | |
foreign import prim "stg_newTicketzh" stg_newTicket# :: State# s -> (# State# s, Any #) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment