<commit_before>module Control.Monad.RLimits (

    -- Monad
    CM,
    startCM,
    getCurrentLabel,

    -- Data types
    RC,
    RCSet,
    RCRef,
    RCMVar,
    RCResult,

    -- RCRef
    newRCRef,
    readRCRef,
    copyRCRef,
    writeRCRef,
    newRCMVar,

    -- RCMVar
    newEmptyRCMVar,
    takeRCMVar,
    copyRCMVar,
    putRCMVar,

    -- Actions
    withRC,
    newRC,
    rcKill,
    rcFork,
    rcCopyResult,

    -- Convenience
    su, ss,

    -- Unsafe
    liftIOTCB

) where

import qualified Data.IntSet as S
import Data.IntMap.Strict (IntMap)
import Data.IntSet (IntSet)
import Data.IORef
import Data.Maybe
import System.Mem.Weak
import Control.Exception
import Control.Concurrent
import Control.Monad
import Control.Applicative
import Control.Concurrent.MVar
import System.IO.Unsafe
import qualified Data.IntMap.Strict as M

import qualified Control.RLimits as R
import Data.SafeCopy

-- Modeled off of LIO proper, but with many simplifications.

-- XXX TODO logic for transfers is not quite right; they should
-- get killed too but softly

-- | A chain of RCRefs, so that if any single container is dead, then
-- the entire reference chain is dead.
data AllRCRef a = RCTerminal a
                | RCDead
                | RCGate (R.RCRef (AllRCRef a))

data StateTCB = StateTCB {
    label :: IORef RCSet, -- synchronized by 'killed'
    tid :: Int,
    global :: MVar GlobalStateTCB
}

data GlobalStateTCB = GlobalStateTCB {
    killed :: IntSet,
    tidpool :: Int,
    threads :: IntMap (Weak ThreadId, IORef RCSet)
}

startCM :: CM () -> IO ()
startCM m = do
    evaluate globalRCMap
    g <- newMVar (GlobalStateTCB S.empty 0 M.empty)
    label <- newIORef (S.singleton 0)
    let st = StateTCB label (-1) g
    runCM (rcFork (S.singleton 0) m >>= \x -> rcCopyResult x trPrim >> return ()) st

newtype CM a = CM { runCM :: StateTCB -> IO a }
instance Functor CM where<commit_msg>Minor comment fix.<commit_after>module Control.Monad.RLimits (

    -- Monad
    CM,
    startCM,
    getCurrentLabel,

    -- Data types
    RC,
    RCSet,
    RCRef,
    RCMVar,
    RCResult,

    -- RCRef
    newRCRef,
    readRCRef,
    copyRCRef,
    writeRCRef,
    newRCMVar,

    -- RCMVar
    newEmptyRCMVar,
    takeRCMVar,
    copyRCMVar,
    putRCMVar,

    -- Actions
    withRC,
    newRC,
    rcKill,
    rcFork,
    rcCopyResult,

    -- Convenience
    su, ss,

    -- Unsafe
    liftIOTCB

) where

import qualified Data.IntSet as S
import Data.IntMap.Strict (IntMap)
import Data.IntSet (IntSet)
import Data.IORef
import Data.Maybe
import System.Mem.Weak
import Control.Exception
import Control.Concurrent
import Control.Monad
import Control.Applicative
import Control.Concurrent.MVar
import System.IO.Unsafe
import qualified Data.IntMap.Strict as M

import qualified Control.RLimits as R
import Data.SafeCopy

-- Modeled off of LIO proper, but with many simplifications.

-- XXX TODO logic for transfers is not quite right; they should
-- get killed too but softly

-- | A chain of RCRefs, so that if any single container is dead, then
-- the entire reference chain is dead.
data AllRCRef a = RCTerminal a
                | RCDead
                | RCGate (R.RCRef (AllRCRef a))

data StateTCB = StateTCB {
    label :: IORef RCSet, -- per thread state, but synchronized by 'global'
    tid :: Int,
    global :: MVar GlobalStateTCB
}

data GlobalStateTCB = GlobalStateTCB {
    killed :: IntSet,
    tidpool :: Int,
    threads :: IntMap (Weak ThreadId, IORef RCSet)
}

startCM :: CM () -> IO ()
startCM m = do
    evaluate globalRCMap
    g <- newMVar (GlobalStateTCB S.empty 0 M.empty)
    label <- newIORef (S.singleton 0)
    let st = StateTCB label (-1) g
    runCM (rcFork (S.singleton 0) m >>= \x -> rcCopyResult x trPrim >> return ()) st

newtype CM a = CM { runCM :: StateTCB -> IO a }
instance Functor CM where
{'fraction_non_alphanumeric': 0.08824292758889177, 'fraction_numerical': 0.002076304178562159, 'mean_word_length': 2.601869158878505, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1646775', 'n_tokens_mistral': 1328, 'n_tokens_neox': 1307, 'n_words': 415}