module GHC.RTS.Events.Analysis.Thread
  ( ThreadState (..)
  , threadMachine
  )
 where

import GHC.RTS.Events
import GHC.RTS.Events.Analysis

--------------------------------------------------------------------------------
-- | This datatype defines the state machine for a single thread.
data ThreadState
  = ThreadInitial
  | ThreadQueued
  | ThreadRunning
  | ThreadStopped
  | ThreadFinal
  deriving (Int -> ThreadState -> ShowS
[ThreadState] -> ShowS
ThreadState -> String
(Int -> ThreadState -> ShowS)
-> (ThreadState -> String)
-> ([ThreadState] -> ShowS)
-> Show ThreadState
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ThreadState] -> ShowS
$cshowList :: [ThreadState] -> ShowS
show :: ThreadState -> String
$cshow :: ThreadState -> String
showsPrec :: Int -> ThreadState -> ShowS
$cshowsPrec :: Int -> ThreadState -> ShowS
Show, ThreadState -> ThreadState -> Bool
(ThreadState -> ThreadState -> Bool)
-> (ThreadState -> ThreadState -> Bool) -> Eq ThreadState
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ThreadState -> ThreadState -> Bool
$c/= :: ThreadState -> ThreadState -> Bool
== :: ThreadState -> ThreadState -> Bool
$c== :: ThreadState -> ThreadState -> Bool
Eq, Eq ThreadState
Eq ThreadState
-> (ThreadState -> ThreadState -> Ordering)
-> (ThreadState -> ThreadState -> Bool)
-> (ThreadState -> ThreadState -> Bool)
-> (ThreadState -> ThreadState -> Bool)
-> (ThreadState -> ThreadState -> Bool)
-> (ThreadState -> ThreadState -> ThreadState)
-> (ThreadState -> ThreadState -> ThreadState)
-> Ord ThreadState
ThreadState -> ThreadState -> Bool
ThreadState -> ThreadState -> Ordering
ThreadState -> ThreadState -> ThreadState
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: ThreadState -> ThreadState -> ThreadState
$cmin :: ThreadState -> ThreadState -> ThreadState
max :: ThreadState -> ThreadState -> ThreadState
$cmax :: ThreadState -> ThreadState -> ThreadState
>= :: ThreadState -> ThreadState -> Bool
$c>= :: ThreadState -> ThreadState -> Bool
> :: ThreadState -> ThreadState -> Bool
$c> :: ThreadState -> ThreadState -> Bool
<= :: ThreadState -> ThreadState -> Bool
$c<= :: ThreadState -> ThreadState -> Bool
< :: ThreadState -> ThreadState -> Bool
$c< :: ThreadState -> ThreadState -> Bool
compare :: ThreadState -> ThreadState -> Ordering
$ccompare :: ThreadState -> ThreadState -> Ordering
$cp1Ord :: Eq ThreadState
Ord)

-- | This state machine tracks the events processed by a thread.
threadMachine :: Machine ThreadState EventInfo
threadMachine :: Machine ThreadState EventInfo
threadMachine = Machine :: forall s i.
s
-> (s -> Bool) -> (i -> Bool) -> (s -> i -> Maybe s) -> Machine s i
Machine
  { initial :: ThreadState
initial = ThreadState
ThreadInitial
  , final :: ThreadState -> Bool
final   = ThreadState -> Bool
threadFinal
  , alpha :: EventInfo -> Bool
alpha   = EventInfo -> Bool
threadAlpha
  , delta :: ThreadState -> EventInfo -> Maybe ThreadState
delta   = ThreadState -> EventInfo -> Maybe ThreadState
threadDelta
  }
 where
  threadFinal :: ThreadState -> Bool
threadFinal ThreadState
ThreadFinal   = Bool
True
  threadFinal ThreadState
_             = Bool
False

  threadAlpha :: EventInfo -> Bool
threadAlpha (CreateThread ThreadId
_)   = Bool
True
  threadAlpha (RunThread ThreadId
_)      = Bool
True
  threadAlpha (StopThread ThreadId
_ ThreadStopStatus
_)   = Bool
True
  threadAlpha (WakeupThread ThreadId
_ Int
_) = Bool
True
  threadAlpha EventInfo
_                  = Bool
False

  -- ThreadInitial
  threadDelta :: ThreadState -> EventInfo -> Maybe ThreadState
threadDelta ThreadState
ThreadInitial (CreateThread ThreadId
_) = ThreadState -> Maybe ThreadState
forall a. a -> Maybe a
Just ThreadState
ThreadQueued
  -- ThreadQueued
  threadDelta ThreadState
ThreadQueued (RunThread ThreadId
_)      = ThreadState -> Maybe ThreadState
forall a. a -> Maybe a
Just ThreadState
ThreadRunning
  threadDelta ThreadState
ThreadQueued (WakeupThread ThreadId
_ Int
_) = ThreadState -> Maybe ThreadState
forall a. a -> Maybe a
Just ThreadState
ThreadQueued
  -- ThreadRunning
  threadDelta ThreadState
ThreadRunning (StopThread ThreadId
_ ThreadStopStatus
StackOverflow)  = ThreadState -> Maybe ThreadState
forall a. a -> Maybe a
Just ThreadState
ThreadQueued
  threadDelta ThreadState
ThreadRunning (StopThread ThreadId
_ ThreadStopStatus
HeapOverflow)   = ThreadState -> Maybe ThreadState
forall a. a -> Maybe a
Just ThreadState
ThreadQueued
  threadDelta ThreadState
ThreadRunning (StopThread ThreadId
_ ThreadStopStatus
ForeignCall)    = ThreadState -> Maybe ThreadState
forall a. a -> Maybe a
Just ThreadState
ThreadQueued
  threadDelta ThreadState
ThreadRunning (StopThread ThreadId
_ ThreadStopStatus
ThreadFinished) = ThreadState -> Maybe ThreadState
forall a. a -> Maybe a
Just ThreadState
ThreadFinal
  threadDelta ThreadState
ThreadRunning (StopThread ThreadId
_ ThreadStopStatus
_)              = ThreadState -> Maybe ThreadState
forall a. a -> Maybe a
Just ThreadState
ThreadStopped
  -- ThreadStopped
  threadDelta ThreadState
ThreadStopped (RunThread ThreadId
_)      = ThreadState -> Maybe ThreadState
forall a. a -> Maybe a
Just ThreadState
ThreadRunning
  threadDelta ThreadState
ThreadStopped (WakeupThread ThreadId
_ Int
_) = ThreadState -> Maybe ThreadState
forall a. a -> Maybe a
Just ThreadState
ThreadQueued
  -- Unknown
  threadDelta ThreadState
_ EventInfo
_ = Maybe ThreadState
forall a. Maybe a
Nothing