dejafu-0.5.1.0: Systematic testing for Haskell concurrency.

Copyright(c) 2016 Michael Walker
LicenseMIT
MaintainerMichael Walker <mike@barrucadu.co.uk>
Stabilityexperimental
Portabilityportable
Safe HaskellSafe
LanguageHaskell2010

Test.DejaFu.Common

Contents

Description

Common types and functions used throughout DejaFu. This module is NOT considered to form part of the public interface of this library.

Synopsis

Identifiers

data ThreadId Source #

Every live thread has a unique identitifer.

Constructors

ThreadId (Maybe String) Int 

data CRefId Source #

Every CRef has a unique identifier.

Constructors

CRefId (Maybe String) Int 

data MVarId Source #

Every MVar has a unique identifier.

Constructors

MVarId (Maybe String) Int 

data TVarId Source #

Every TVar has a unique identifier.

Constructors

TVarId (Maybe String) Int 

initialThread :: ThreadId Source #

The ID of the initial thread.

Identifier source

data IdSource Source #

The number of ID parameters was getting a bit unwieldy, so this hides them all away.

nextCRId :: String -> IdSource -> (IdSource, CRefId) Source #

Get the next free CRefId.

nextMVId :: String -> IdSource -> (IdSource, MVarId) Source #

Get the next free MVarId.

nextTVId :: String -> IdSource -> (IdSource, TVarId) Source #

Get the next free TVarId.

nextTId :: String -> IdSource -> (IdSource, ThreadId) Source #

Get the next free ThreadId.

initialIdSource :: IdSource Source #

The initial ID source.

Actions

Thread actions

data ThreadAction Source #

All the actions that a thread can perform.

Constructors

Fork ThreadId

Start a new thread.

MyThreadId

Get the ThreadId of the current thread.

GetNumCapabilities Int

Get the number of Haskell threads that can run simultaneously.

SetNumCapabilities Int

Set the number of Haskell threads that can run simultaneously.

Yield

Yield the current thread.

NewMVar MVarId

Create a new MVar.

PutMVar MVarId [ThreadId]

Put into a MVar, possibly waking up some threads.

BlockedPutMVar MVarId

Get blocked on a put.

TryPutMVar MVarId Bool [ThreadId]

Try to put into a MVar, possibly waking up some threads.

ReadMVar MVarId

Read from a MVar.

TryReadMVar MVarId Bool

Try to read from a MVar.

BlockedReadMVar MVarId

Get blocked on a read.

TakeMVar MVarId [ThreadId]

Take from a MVar, possibly waking up some threads.

BlockedTakeMVar MVarId

Get blocked on a take.

TryTakeMVar MVarId Bool [ThreadId]

Try to take from a MVar, possibly waking up some threads.

NewCRef CRefId

Create a new CRef.

ReadCRef CRefId

Read from a CRef.

ReadCRefCas CRefId

Read from a CRef for a future compare-and-swap.

ModCRef CRefId

Modify a CRef.

ModCRefCas CRefId

Modify a CRef using a compare-and-swap.

WriteCRef CRefId

Write to a CRef without synchronising.

CasCRef CRefId Bool

Attempt to to a CRef using a compare-and-swap, synchronising it.

CommitCRef ThreadId CRefId

Commit the last write to the given CRef by the given thread, so that all threads can see the updated value.

STM TTrace [ThreadId]

An STM transaction was executed, possibly waking up some threads.

BlockedSTM TTrace

Got blocked in an STM transaction.

Catching

Register a new exception handler

PopCatching

Pop the innermost exception handler from the stack.

Throw

Throw an exception.

ThrowTo ThreadId

Throw an exception to a thread.

BlockedThrowTo ThreadId

Get blocked on a throwTo.

Killed

Killed by an uncaught exception.

SetMasking Bool MaskingState

Set the masking state. If True, this is being used to set the masking state to the original state in the argument passed to a masked function.

ResetMasking Bool MaskingState

Return to an earlier masking state. If True, this is being used to return to the state of the masked block in the argument passed to a masked function.

LiftIO

Lift an IO action. Note that this can only happen with ConcIO.

Return

A return or pure action was executed.

Stop

Cease execution and terminate.

Subconcurrency

Start executing an action with subconcurrency.

StopSubconcurrency

Stop executing an action with subconcurrency.

isBlock :: ThreadAction -> Bool Source #

Check if a ThreadAction immediately blocks.

tvarsOf :: ThreadAction -> Set TVarId Source #

Get the TVars affected by a ThreadAction.

Lookahead

data Lookahead Source #

A one-step look-ahead at what a thread will do next.

Constructors

WillFork

Will start a new thread.

WillMyThreadId

Will get the ThreadId.

WillGetNumCapabilities

Will get the number of Haskell threads that can run simultaneously.

WillSetNumCapabilities Int

Will set the number of Haskell threads that can run simultaneously.

WillYield

Will yield the current thread.

WillNewMVar

Will create a new MVar.

WillPutMVar MVarId

Will put into a MVar, possibly waking up some threads.

WillTryPutMVar MVarId

Will try to put into a MVar, possibly waking up some threads.

WillReadMVar MVarId

Will read from a MVar.

WillTryReadMVar MVarId

Will try to read from a MVar.

WillTakeMVar MVarId

Will take from a MVar, possibly waking up some threads.

WillTryTakeMVar MVarId

Will try to take from a MVar, possibly waking up some threads.

WillNewCRef

Will create a new CRef.

WillReadCRef CRefId

Will read from a CRef.

WillReadCRefCas CRefId

Will read from a CRef for a future compare-and-swap.

WillModCRef CRefId

Will modify a CRef.

WillModCRefCas CRefId

Will modify a CRef using a compare-and-swap.

WillWriteCRef CRefId

Will write to a CRef without synchronising.

WillCasCRef CRefId

Will attempt to to a CRef using a compare-and-swap, synchronising it.

WillCommitCRef ThreadId CRefId

Will commit the last write by the given thread to the CRef.

WillSTM

Will execute an STM transaction, possibly waking up some threads.

WillCatching

Will register a new exception handler

WillPopCatching

Will pop the innermost exception handler from the stack.

WillThrow

Will throw an exception.

WillThrowTo ThreadId

Will throw an exception to a thread.

WillSetMasking Bool MaskingState

Will set the masking state. If True, this is being used to set the masking state to the original state in the argument passed to a masked function.

WillResetMasking Bool MaskingState

Will return to an earlier masking state. If True, this is being used to return to the state of the masked block in the argument passed to a masked function.

WillLiftIO

Will lift an IO action. Note that this can only happen with ConcIO.

WillReturn

Will execute a return or pure action.

WillStop

Will cease execution and terminate.

WillSubconcurrency

Will execute an action with subconcurrency.

WillStopSubconcurrency

Will stop executing an extion with subconcurrency.

rewind :: ThreadAction -> Maybe Lookahead Source #

Convert a ThreadAction into a Lookahead: "rewind" what has happened. Killed has no Lookahead counterpart.

willRelease :: Lookahead -> Bool Source #

Check if an operation could enable another thread.

Simplified actions

data ActionType Source #

A simplified view of the possible actions a thread can perform.

Constructors

UnsynchronisedRead CRefId

A readCRef or a readForCAS.

UnsynchronisedWrite CRefId

A writeCRef.

UnsynchronisedOther

Some other action which doesn't require cross-thread communication.

PartiallySynchronisedCommit CRefId

A commit.

PartiallySynchronisedWrite CRefId

A casCRef

PartiallySynchronisedModify CRefId

A modifyCRefCAS

SynchronisedModify CRefId

An atomicModifyCRef.

SynchronisedRead MVarId

A readMVar or takeMVar (or try/blocked variants).

SynchronisedWrite MVarId

A putMVar (or try/blocked variant).

SynchronisedOther

Some other action which does require cross-thread communication.

isBarrier :: ActionType -> Bool Source #

Check if an action imposes a write barrier.

isCommit :: ActionType -> CRefId -> Bool Source #

Check if an action commits a given CRef.

synchronises :: ActionType -> CRefId -> Bool Source #

Check if an action synchronises a given CRef.

crefOf :: ActionType -> Maybe CRefId Source #

Get the CRef affected.

mvarOf :: ActionType -> Maybe MVarId Source #

Get the MVar affected.

simplifyAction :: ThreadAction -> ActionType Source #

Throw away information from a ThreadAction and give a simplified view of what is happening.

This is used in the SCT code to help determine interesting alternative scheduling decisions.

STM actions

type TTrace = [TAction] Source #

A trace of an STM transaction is just a list of actions that occurred, as there are no scheduling decisions to make.

data TAction Source #

All the actions that an STM transaction can perform.

Constructors

TNew

Create a new TVar

TRead TVarId

Read from a TVar.

TWrite TVarId

Write to a TVar.

TRetry

Abort and discard effects.

TOrElse TTrace (Maybe TTrace)

Execute a transaction until it succeeds (STMStop) or aborts (STMRetry) and, if it aborts, execute the other transaction.

TThrow

Throw an exception, abort, and discard effects.

TCatch TTrace (Maybe TTrace)

Execute a transaction until it succeeds (STMStop) or aborts (STMThrow). If the exception is of the appropriate type, it is handled and execution continues; otherwise aborts, propagating the exception upwards.

TStop

Terminate successfully and commit effects.

Instances

Traces

type Trace = [(Decision, [(ThreadId, NonEmpty Lookahead)], ThreadAction)] Source #

One of the outputs of the runner is a Trace, which is a log of decisions made, all the runnable threads and what they would do, and the action a thread took in its step.

data Decision Source #

Scheduling decisions are based on the state of the running program, and so we can capture some of that state in recording what specific decision we made.

Constructors

Start ThreadId

Start a new thread, because the last was blocked (or it's the start of computation).

Continue

Continue running the last thread for another step.

SwitchTo ThreadId

Pre-empt the running thread, and switch to another.

showTrace :: Trace -> String Source #

Pretty-print a trace, including a key of the thread IDs (not including thread 0). Each line of the key is indented by two spaces.

preEmpCount :: [(Decision, ThreadAction)] -> (Decision, Lookahead) -> Int Source #

Count the number of pre-emptions in a schedule prefix.

Commit threads complicate this a bit. Conceptually, commits are happening truly in parallel, nondeterministically. The commit thread implementation is just there to unify the two sources of nondeterminism: commit timing and thread scheduling.

SO, we don't count a switch TO a commit thread as a preemption. HOWEVER, the switch FROM a commit thread counts as a preemption if it is not to the thread that the commit interrupted.

Failures

data Failure Source #

An indication of how a concurrent computation failed.

Constructors

InternalError

Will be raised if the scheduler does something bad. This should never arise unless you write your own, faulty, scheduler! If it does, please file a bug report.

Abort

The scheduler chose to abort execution. This will be produced if, for example, all possible decisions exceed the specified bounds (there have been too many pre-emptions, the computation has executed for too long, or there have been too many yields).

Deadlock

The computation became blocked indefinitely on MVars.

STMDeadlock

The computation became blocked indefinitely on TVars.

UncaughtException

An uncaught exception bubbled to the top of the computation.

IllegalSubconcurrency

Calls to subconcurrency were nested, or attempted when multiple threads existed.

showFail :: Failure -> String Source #

Pretty-print a failure

Memory models

data MemType Source #

The memory model to use for non-synchronised CRef operations.

Constructors

SequentialConsistency

The most intuitive model: a program behaves as a simple interleaving of the actions in different threads. When a CRef is written to, that write is immediately visible to all threads.

TotalStoreOrder

Each thread has a write buffer. A thread sees its writes immediately, but other threads will only see writes when they are committed, which may happen later. Writes are committed in the same order that they are created.

PartialStoreOrder

Each CRef has a write buffer. A thread sees its writes immediately, but other threads will only see writes when they are committed, which may happen later. Writes to different CRefs are not necessarily committed in the same order that they are created.