| 
 
 
 
 | 
Schedulers
FairThreads explicitely introduces schedulers, of typeft_scheduler_t. Before being used, a scheduler must be created
by calling the functionft_scheduler_create. 
 In order to be executed, a scheduler must be started by a call to the
function ft_scheduler_start. Note that several schedulers
can be used without problem simultaneously in the same program. 
Threads
Fair threads are of typeft_thread_t. The callft_thread_create(s,r,c,a)creates a thread in the
schedulers. The thread is automatically started as soon as
it is created.  The functionris executed by the
thread, and the parameterais transmitted to it.
The functioncis executed by the thread if it is stopped
(byft_scheduler_stop). The parameterais also
transmitted to it, if this happens.
Events
Events are of the typeft_event_t. An event is created by
calling the functionft_event_createwhich takes as
parameter the scheduler in charge of it.
Automata
Automata are fair threads of the typeft_thread_tcreated
with the functionft_automaton_create. The thread returned byft_automaton_create(s,r,c,a)is executed as an automaton by
the schedulers.  The functionrexecuted by the
automaton must be defined with the macroDEFINE_AUTOMATON.
Control of Threads
The callft_scheduler_stop(t)gives to the scheduler which
executes the threadtthe order to stop it.  The stop will
become actual at the begining of the next instant of the scheduler, in
order to assure thattis in a stable state when stopped. 
The call ft_scheduler_suspend(t)gives to the scheduler
which executestthe order to suspendt.  The
suspension will become actual at the begining of the next instant of
the scheduler.  The functionft_scheduler_resumeis used to
resume execution of suspended threads. 
Broadcast of Events
The functionft_scheduler_broadcast(e)gives to the
scheduler of the eventethe order to broadcast it to all
threads running in the scheduler. The event will be actually generated
at the begining of the next instant of the scheduler. The callft_scheduler_broadcast_value(e,v)associates the valuevtoe(vcan be read usingft_thread_get_value).
Cooperation
The callft_thread_cooperate()is the explicit way for the
calling thread to return control to the scheduler running it. The
callft_thread_cooperate_n(i)is equivalent to a sequence oficallsft_thread_cooperate().
Termination
The callft_thread_join(t)suspends the execution of the
calling thread until the threadtterminates (either
normally or because it is stopped). Note thattneeds not to be
linked or running in the scheduler of the calling thread. Withft_thread_join_n(t,i)the suspension takes at mostiinstants. 
Generating Events
The callft_thread_generate(e)generates the eventein the scheduler which was associated to it, when created. The
callft_thread_generate_value(e,v)addsvto the list of values associated toeduring the current
instant (these values can be read usingft_thread_get_value).
Awaiting Events
The callft_thread_await(e)suspends the execution of the calling thread
until the generation of the evente. Execution is resumed as soon as the
event is generated. Withft_thread_await_n(e,i), the waiting takes at mostiinstants.
Selecting Events
The callft_thread_select(k,array,mask)suspends the execution of the calling thread
until the generation of one element ofarraywhich is an array ofkevents. Then,mask, which is an array ofkboolean values, is set accordingly.
Withft_thread_select_n(k,array,mask,i), the waiting takes at mostiinstants.
Getting Events Values
The callft_thread_get_value(e,i,r)is an attempt to get theith value
associated to the eventeduring the current instant. If such a
value exists, it is returned inrand the call immediately terminates. Otherwise,
the value NULL is returned at the next instant. The return code of the call indicates if
the call was sucessful or not. 
Link and Unlink
The callft_thread_unlink()unlinks the calling threadtfrom the scheduler in which it was previously
running. Then,twill no longer synchronize, instant after
instant, with other threads linked to the scheduler. Actually, after
unlinking,tbehaves as a standard native thread. 
The call ft_thread_link(s)links the calling thread to the
schedulers.  The calling thread must be unlinked when
executing the call. The linkage becomes actual at the begining of the
next instant ofs. 
Locks
In presence of unlinked threads, locks can be needed to protect data
shared between unlinked and linked threads. Standard mutexes are used
for this purpose.  The callft_thread_mutex_lock(p), wherepis a mutex, suspends the calling thread until the moment
wherepcan be locked. The lock is released usingft_thread_mutex_unlock.  Locks owned by a thread are automatically
released when the thread terminates definitively or is stopped.
Automata are coded using macros. Here are the macros to define the automaton structure:
 
 The following macros start the state whose number is AUTOMATON(aut)declares the automatonaut. DEFINE_AUTOMATON(aut)starts definition of the automatonaut. BEGIN_AUTOMATONstarts the state list. END_AUTOMATONends the state list. num:
 Going from state to state is possible with: STATE(num)introduces a standard state. STATE_AWAIT(num,event)andSTATE_AWAIT_N(num,event,delay)are states 
to awaitevent. STATE_JOIN(num,thread)andSTATE_JOIN_N(num,thread,delay)are states to jointhread. STATE_STAY(num,n)is a state which keeps execution in it forninstants. STATE_GET_VALUE(num,event,n,result)is a state to get thenth value 
associated toevent. STATE_SELECT(num,n,array,mask) andSTATE_SELECT_N(num,n,array,mask,delay) 
generaliseSTATE_AWAITandSTATE_AWAIT_Nto an array of events of lengthn. 
 Finally, the following macros define some special variables: GOTO(num)blocks execution for the current instant 
and sets the state for the next instant to be statenum. GOTO_NEXTblocks execution for the current instant 
and sets the state for the next instant to be the next state. IMMEDIATE(num)forces execution to jump to statenumwhich is immediately executed. RETURNimmediately terminates the automaton. 
  SELFis the automaton. SET_LOCAL(data)sets the local data of the automaton. LOCALis the local data of the automaton. ARGSis the argument that is passed at creation to the automaton. RETURN_CODEis the error code set by macros run during automaton execution. 
Current Thread
The calling thread is returned byft_thread_self().
Current Scheduler
The scheduler of the calling thread is returned byft_thread_scheduler().
Pthread
The callft_pthread(t)returns the native pthread which
executes the fair threadt.  This function gives direct
access to the Pthreads implementation of FairThreads. In the rest of the
paper, native thread and pthread will be considered as synonymous.
Exiting
The functionft_exitis equivalent topthread_exit. The basic use offt_exitis to terminate
the pthread which is running the functionmain, without
exiting from the whole process. |