6 signature MLTON_THREAD =
10 datatype t = NonAtomic | Atomic of int
13 val atomically: (unit -> 'a) -> 'a
14 val atomicBegin: unit -> unit
15 val atomicEnd: unit -> unit
16 val atomicState: unit -> AtomicState.t
25 val atomicSwitch: ('a t -> Runnable.t) -> 'a
26 val new: ('a -> unit) -> 'a t
27 val prepend: 'a t * ('b -> 'a) -> 'b t
28 val prepare: 'a t * 'a -> Runnable.t
29 val switch: ('a t -> Runnable.t) -> 'a
33 `MLton.Thread` provides access to MLton's user-level thread
34 implementation (i.e. not OS-level threads). Threads are lightweight
35 data structures that represent a paused computation. Runnable threads
36 are threads that will begin or continue computing when `switch`-ed to.
37 `MLton.Thread` does not include a default scheduling mechanism, but it
38 can be used to implement both preemptive and non-preemptive threads.
40 * `type AtomicState.t`
42 the type of atomic states.
47 runs `f` in a critical section.
51 begins a critical section.
55 ends a critical section.
59 returns the current atomic state.
63 the type of threads that can be resumed.
67 the type of threads that expect a value of type `'a`.
71 like `switch`, but assumes an atomic calling context. Upon
72 `switch`-ing back to the current thread, an implicit `atomicEnd` is
77 creates a new thread that, when run, applies `f` to the value given to
78 the thread. `f` must terminate by `switch`ing to another thread or
83 creates a new thread (destroying `t` in the process) that first
84 applies `f` to the value given to the thread and then continues with
85 `t`. This is a constant time operation.
89 prepares a new runnable thread (destroying `t` in the process) that
90 will evaluate `t` on `v`.
94 applies `f` to the current thread to get `rt`, and then start running
95 thread `rt`. It is an error for `f` to perform another `switch`. `f`
96 is guaranteed to run atomically.
99 == Example of non-preemptive threads ==
103 sys::[./bin/InclGitFile.py mlton master doc/examples/thread/non-preemptive-threads.sml]
107 == Example of preemptive threads ==
111 sys::[./bin/InclGitFile.py mlton master doc/examples/thread/preemptive-threads.sml]