source: doc/theses/thierry_delisle_MMath/text/together.tex @ 0588d8c

ADTarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 0588d8c was 67982887, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

specialize thesis directory-names

  • Property mode set to 100644
File size: 4.3 KB
RevLine 
[d67cdb7]1% ======================================================================
2% ======================================================================
[6090518]3\chapter{Putting It All Together}
[d67cdb7]4% ======================================================================
5% ======================================================================
6
7
[6090518]8\section{Threads As Monitors}
[5c4f2c2]9As it was subtly alluded in section \ref{threads}, \code{thread}s in \CFA are in fact monitors, which means that all monitor features are available when using threads. For example, here is a very simple two thread pipeline that could be used for a simulator of a game engine:
[cf966b5]10\begin{figure}[H]
11\begin{cfacode}[caption={Toy simulator using \code{thread}s and \code{monitor}s.},label={lst:engine-v1}]
[d67cdb7]12// Visualization declaration
13thread Renderer {} renderer;
14Frame * simulate( Simulator & this );
15
16// Simulation declaration
17thread Simulator{} simulator;
18void render( Renderer & this );
19
20// Blocking call used as communication
21void draw( Renderer & mutex this, Frame * frame );
22
[07c1e595]23// Simulation loop
[d67cdb7]24void main( Simulator & this ) {
25        while( true ) {
26                Frame * frame = simulate( this );
27                draw( renderer, frame );
28        }
29}
30
31// Rendering loop
32void main( Renderer & this ) {
33        while( true ) {
34                waitfor( draw, this );
35                render( this );
36        }
37}
38\end{cfacode}
[cf966b5]39\end{figure}
[5c4f2c2]40One of the obvious complaints of the previous code snippet (other than its toy-like simplicity) is that it does not handle exit conditions and just goes on forever. Luckily, the monitor semantics can also be used to clearly enforce a shutdown order in a concise manner:
[cf966b5]41\begin{figure}[H]
42\begin{cfacode}[caption={Same toy simulator with proper termination condition.},label={lst:engine-v2}]
[d67cdb7]43// Visualization declaration
44thread Renderer {} renderer;
45Frame * simulate( Simulator & this );
46
47// Simulation declaration
48thread Simulator{} simulator;
49void render( Renderer & this );
50
51// Blocking call used as communication
52void draw( Renderer & mutex this, Frame * frame );
53
[07c1e595]54// Simulation loop
[d67cdb7]55void main( Simulator & this ) {
56        while( true ) {
57                Frame * frame = simulate( this );
58                draw( renderer, frame );
59
60                // Exit main loop after the last frame
61                if( frame->is_last ) break;
62        }
63}
64
65// Rendering loop
66void main( Renderer & this ) {
67        while( true ) {
68                   waitfor( draw, this );
69                or waitfor( ^?{}, this ) {
70                        // Add an exit condition
71                        break;
72                }
73
74                render( this );
75        }
76}
[20ffcf3]77
78// Call destructor for simulator once simulator finishes
79// Call destructor for renderer to signify shutdown
80\end{cfacode}
[cf966b5]81\end{figure}
[20ffcf3]82
83\section{Fibers \& Threads}
[07c1e595]84As mentioned in section \ref{preemption}, \CFA uses preemptive threads by default but can use fibers on demand. Currently, using fibers is done by adding the following line of code to the program~:
[20ffcf3]85\begin{cfacode}
86unsigned int default_preemption() {
87        return 0;
88}
[d67cdb7]89\end{cfacode}
[cf966b5]90This function is called by the kernel to fetch the default preemption rate, where 0 signifies an infinite time-slice, i.e., no preemption. However, once clusters are fully implemented, it will be possible to create fibers and \glspl{uthread} in the same system, as in listing \ref{lst:fiber-uthread}
[20ffcf3]91\begin{figure}
[cf966b5]92\begin{cfacode}[caption={Using fibers and \glspl{uthread} side-by-side in \CFA},label={lst:fiber-uthread}]
[20ffcf3]93//Cluster forward declaration
94struct cluster;
95
96//Processor forward declaration
97struct processor;
98
99//Construct clusters with a preemption rate
100void ?{}(cluster& this, unsigned int rate);
101//Construct processor and add it to cluster
102void ?{}(processor& this, cluster& cluster);
103//Construct thread and schedule it on cluster
104void ?{}(thread& this, cluster& cluster);
[d67cdb7]105
[20ffcf3]106//Declare two clusters
107cluster thread_cluster = { 10`ms };                     //Preempt every 10 ms
108cluster fibers_cluster = { 0 };                         //Never preempt
109
110//Construct 4 processors
111processor processors[4] = {
112        //2 for the thread cluster
113        thread_cluster;
114        thread_cluster;
115        //2 for the fibers cluster
116        fibers_cluster;
117        fibers_cluster;
118};
119
120//Declares thread
121thread UThread {};
122void ?{}(UThread& this) {
123        //Construct underlying thread to automatically
124        //be scheduled on the thread cluster
125        (this){ thread_cluster }
126}
127
128void main(UThread & this);
129
130//Declares fibers
131thread Fiber {};
132void ?{}(Fiber& this) {
133        //Construct underlying thread to automatically
134        //be scheduled on the fiber cluster
135        (this.__thread){ fibers_cluster }
136}
137
138void main(Fiber & this);
139\end{cfacode}
[5c4f2c2]140\end{figure}
Note: See TracBrowser for help on using the repository browser.