[d67cdb7] | 1 | % ======================================================================
|
---|
| 2 | % ======================================================================
|
---|
[6090518] | 3 | \chapter{Putting It All Together}
|
---|
[d67cdb7] | 4 | % ======================================================================
|
---|
| 5 | % ======================================================================
|
---|
| 6 |
|
---|
| 7 |
|
---|
[6090518] | 8 | \section{Threads As Monitors}
|
---|
[cf966b5] | 9 | As 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 :
|
---|
| 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
|
---|
| 13 | thread Renderer {} renderer;
|
---|
| 14 | Frame * simulate( Simulator & this );
|
---|
| 15 |
|
---|
| 16 | // Simulation declaration
|
---|
| 17 | thread Simulator{} simulator;
|
---|
| 18 | void render( Renderer & this );
|
---|
| 19 |
|
---|
| 20 | // Blocking call used as communication
|
---|
| 21 | void draw( Renderer & mutex this, Frame * frame );
|
---|
| 22 |
|
---|
[07c1e595] | 23 | // Simulation loop
|
---|
[d67cdb7] | 24 | void main( Simulator & this ) {
|
---|
| 25 | while( true ) {
|
---|
| 26 | Frame * frame = simulate( this );
|
---|
| 27 | draw( renderer, frame );
|
---|
| 28 | }
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | // Rendering loop
|
---|
| 32 | void main( Renderer & this ) {
|
---|
| 33 | while( true ) {
|
---|
| 34 | waitfor( draw, this );
|
---|
| 35 | render( this );
|
---|
| 36 | }
|
---|
| 37 | }
|
---|
| 38 | \end{cfacode}
|
---|
[cf966b5] | 39 | \end{figure}
|
---|
[64b272a] | 40 | One 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
|
---|
| 44 | thread Renderer {} renderer;
|
---|
| 45 | Frame * simulate( Simulator & this );
|
---|
| 46 |
|
---|
| 47 | // Simulation declaration
|
---|
| 48 | thread Simulator{} simulator;
|
---|
| 49 | void render( Renderer & this );
|
---|
| 50 |
|
---|
| 51 | // Blocking call used as communication
|
---|
| 52 | void draw( Renderer & mutex this, Frame * frame );
|
---|
| 53 |
|
---|
[07c1e595] | 54 | // Simulation loop
|
---|
[d67cdb7] | 55 | void 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
|
---|
| 66 | void 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] | 84 | As 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}
|
---|
| 86 | unsigned int default_preemption() {
|
---|
| 87 | return 0;
|
---|
| 88 | }
|
---|
[d67cdb7] | 89 | \end{cfacode}
|
---|
[cf966b5] | 90 | This 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
|
---|
| 94 | struct cluster;
|
---|
| 95 |
|
---|
| 96 | //Processor forward declaration
|
---|
| 97 | struct processor;
|
---|
| 98 |
|
---|
| 99 | //Construct clusters with a preemption rate
|
---|
| 100 | void ?{}(cluster& this, unsigned int rate);
|
---|
| 101 | //Construct processor and add it to cluster
|
---|
| 102 | void ?{}(processor& this, cluster& cluster);
|
---|
| 103 | //Construct thread and schedule it on cluster
|
---|
| 104 | void ?{}(thread& this, cluster& cluster);
|
---|
[d67cdb7] | 105 |
|
---|
[20ffcf3] | 106 | //Declare two clusters
|
---|
| 107 | cluster thread_cluster = { 10`ms }; //Preempt every 10 ms
|
---|
| 108 | cluster fibers_cluster = { 0 }; //Never preempt
|
---|
| 109 |
|
---|
| 110 | //Construct 4 processors
|
---|
| 111 | processor 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
|
---|
| 121 | thread UThread {};
|
---|
| 122 | void ?{}(UThread& this) {
|
---|
| 123 | //Construct underlying thread to automatically
|
---|
| 124 | //be scheduled on the thread cluster
|
---|
| 125 | (this){ thread_cluster }
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | void main(UThread & this);
|
---|
| 129 |
|
---|
| 130 | //Declares fibers
|
---|
| 131 | thread Fiber {};
|
---|
| 132 | void ?{}(Fiber& this) {
|
---|
| 133 | //Construct underlying thread to automatically
|
---|
| 134 | //be scheduled on the fiber cluster
|
---|
| 135 | (this.__thread){ fibers_cluster }
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | void main(Fiber & this);
|
---|
| 139 | \end{cfacode}
|
---|
| 140 | \end{figure}
|
---|