4 Features

4.7 New Threads features: mutual exclusion primitives

MLWorks 1.0r2 adds mutual exclusion primitives, or mutexes, to the MLWorks threads facility.

The files mutex.mo and __mutex.mo in the utils directory implement mutual exclusion primitives. They work with the thread mechanism running in pre-emption mode.

There are five different demonstration programs in MLW/demo/threads:

dining_philosophers.sml
sleeping_barber.sml
bounded_buffer.sml
cigarette_smokers.sml
readers_writers.sml 

Brief documentation of the mutex interface follows.

Mutex Exception

The exception of mutexes.

mutex Type

The type of mutexes.

newCountingMutex Function

newCountingMutex counter -> mutex 

Returns a new counting mutex with initial value counter, an int. The mutex returned is of type mutex.

newBinaryMutex Function

newBinaryMutex isClaimed -> mutex 

Returns a new binary mutex with initial value isClaimed, a bool. The mutex returned is of type mutex.

test Function

test mutex-list -> bool 

Returns true if all mutexes in mutex-list (type mutex list) are free at the time of the call, and false otherwise. It does not block.

testAndClaim Function

testAndClaim mutex-list -> bool 

Like test, but also claims the mutexes in mutex-list if they are all available, returning true. Returns false otherwise.

wait Function

wait mutex-list -> () 

Blocks the current thread until all mutexes in mutex-list (type mutex list) are simultaneously free. Returns unit.

Note that a blocked thread will be added to the waiting list of only one of the mutexes.

signal Function

signal mutex-list -> () 

Signals that all the mutexes in mutex-list (type mutex list) are free, waking every thread waiting on the mutexes in the list. Returns unit.

query Function

query mutex -> thread-list 

Returns the list of the threads that are waiting on mutex. The thread-list is of type MLWorks.Threads.Internal.thread_id list.

allSleeping Function

allSleeping thread-list -> bool

Returns true if every thread in thread-list is sleeping at the time of the call, and false otherwise. The thread-list must be of type MLWorks.Threads.Internal.thread_id list.

This function can be used for deadlock detection if thread-list is the list of every currently running thread.

cleanUp Function

cleanUp () -> ()

Kills off all threads except those belonging to MLWorks. Takes unit and returns unit.

critical Function

critical (mutex-list, f) a -> value 

Evaluates a function call between a wait and a signal, with appropriate behavior on exceptions.

This function first waits for the mutexes in mutex-list. Then it applies f to a. Then it signals that the mutexes in mutex-list are free. Finally, it returns the result of the application of f to a.

The mutex-list is of type mutex list; f is a function (type 'a -> 'b) and a must be a valid argument to f.

await Function

await (mutex-list, c) -> ()

Waits until every mutex in mutex-list is free and an arbitrary condition is true. The await function calls c to test for the condition.

If c() evaluates to true, await does not release the mutexes. The mutexes are only released if an exception occurs during the evaluation of c().

The mutex-list is of type mutex list. The c function is of type unit -> bool. Returns unit.


MLWorks Version 1.0r2 Release Notes - 1 JUL 1997

Generated with Harlequin WebMaker