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
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
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
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
Ord)

-- | This state machine tracks the events processed by a thread.
threadMachine :: Machine ThreadState EventInfo
threadMachine :: Machine ThreadState EventInfo
threadMachine = 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
_) = forall a. a -> Maybe a
Just ThreadState
ThreadQueued
  -- ThreadQueued
  threadDelta ThreadState
ThreadQueued (RunThread ThreadId
_)      = forall a. a -> Maybe a
Just ThreadState
ThreadRunning
  threadDelta ThreadState
ThreadQueued (WakeupThread ThreadId
_ Int
_) = forall a. a -> Maybe a
Just ThreadState
ThreadQueued
  -- ThreadRunning
  threadDelta ThreadState
ThreadRunning (StopThread ThreadId
_ ThreadStopStatus
StackOverflow)  = forall a. a -> Maybe a
Just ThreadState
ThreadQueued
  threadDelta ThreadState
ThreadRunning (StopThread ThreadId
_ ThreadStopStatus
HeapOverflow)   = forall a. a -> Maybe a
Just ThreadState
ThreadQueued
  threadDelta ThreadState
ThreadRunning (StopThread ThreadId
_ ThreadStopStatus
ForeignCall)    = forall a. a -> Maybe a
Just ThreadState
ThreadQueued
  threadDelta ThreadState
ThreadRunning (StopThread ThreadId
_ ThreadStopStatus
ThreadFinished) = forall a. a -> Maybe a
Just ThreadState
ThreadFinal
  threadDelta ThreadState
ThreadRunning (StopThread ThreadId
_ ThreadStopStatus
_)              = forall a. a -> Maybe a
Just ThreadState
ThreadStopped
  -- ThreadStopped
  threadDelta ThreadState
ThreadStopped (RunThread ThreadId
_)      = forall a. a -> Maybe a
Just ThreadState
ThreadRunning
  threadDelta ThreadState
ThreadStopped (WakeupThread ThreadId
_ Int
_) = forall a. a -> Maybe a
Just ThreadState
ThreadQueued
  -- Unknown
  threadDelta ThreadState
_ EventInfo
_ = forall a. Maybe a
Nothing