Module Fthread


module Fthread: sig .. end
Fair Threads module : mixing cooperative and preemptive threads.


Types

type 'a event_t 
The type of events (associated to values of type 'a).
type scheduler_t 
The type of schedulers
type thread_t 
The type of threads

Exceptions

exception No_value
Exception raised by get_value_n evt k when no value is associated to the event evt after k instants.
exception FT_error of string
Exception raised when an error occurs in the Fair Threads module.

Constructors

val create_scheduler : unit -> scheduler_t
create_scheduler () returns a new scheduler.
val create_thread : scheduler_t -> ('a -> unit) -> 'a -> thread_t
create_thread sched f a returns a new fair thread and links it to the scheduler sched. The new thread applies the function f to a.
val create_event : scheduler_t -> 'a event_t
create_event sched returns a new event associated to the scheduler sched. If no value is ever used in association with this event in the whole program, it is necessary to give to the type checker a type for 'a.

For example: let event : unit event_t = create_event sched.

val start_scheduler : scheduler_t -> unit
start_scheduler sched starts running the scheduler sched

Cooperation

val cooperate : unit -> unit
cooperate () returns the control to the scheduler in which it is running. It has no effect if the thread is unlinked.
val cooperate_n : int -> unit
cooperate_n n behaves like for i = 1 to n do cooperate done

Events

val generate : 'a event_t -> unit
generate e generates the event e in the associated scheduler.
val generate_value : 'a event_t -> 'a -> unit
generate e v generates the event e and associates the value v with it.
val await : 'a event_t -> unit
await e suspends the execution of the calling thread until the generation of the event e.
val await_n : 'a event_t -> int -> unit
await_n suspends the execution of the calling thread until the generation of the event e, or until n instants have elapsed.
val get_value : 'a event_t -> 'a
get_value e suspends the execution of the calling thread until the event e is generated AND a value is associated with it. Returns this value.
val get_value_n : 'a event_t -> int -> 'a
get_value e n suspends the execution of the calling thread until the event e is generated AND a value is associated with it, or until n instants have elapsed. In the former case, the function returns the value. In the latter case, exception Fthread.No_value is raised.
val unlink : unit -> unit
unlink () unlinks the current threads from its scheduler. The thread now behave preemptively.
val link : scheduler_t -> unit
link sched links the calling thread to the scheduler sched. If the thread is already linked to a scheduler, it is first unlinked.

Thread control

val thread_stop : thread_t -> unit
thread_stop th asks the scheduler running the thread th to stop it. The termination takes place at the beginning of the next instant of the scheduler.
val thread_suspend : thread_t -> unit
thread_suspend th asks the scheduler running the thread th to suspend its execution. The suspension takes place at the beginning of the next instant of the scheduler.
val thread_resume : thread_t -> unit
thread_resume th asks the scheduler running the thread th to resume its execution. The thread is actually resumed at the beginning of the next instant of the scheduler.
val join : thread_t -> unit
thread_join th suspends the execution of the current thread until the termination of the thread th.

Exit

val exit : unit -> unit
The basic use of exit is to terminate the pthread which is running the function main, without exiting from the whole process.