| [9319a23] | 1 | #include <locks.hfa> | 
|---|
|  | 2 | #include <fstream.hfa> | 
|---|
|  | 3 | #include <stdio.h> | 
|---|
|  | 4 | #include <channel.hfa> | 
|---|
|  | 5 | #include <thread.hfa> | 
|---|
|  | 6 | #include <time.hfa> | 
|---|
|  | 7 | #include <string.h> | 
|---|
|  | 8 |  | 
|---|
|  | 9 | size_t total_operations = 0; | 
|---|
|  | 10 | int Processors = 1, Tasks = 5, BarrierSize = 2; | 
|---|
|  | 11 |  | 
|---|
|  | 12 | typedef channel( int ) Channel; | 
|---|
|  | 13 |  | 
|---|
|  | 14 | Channel * barWait; | 
|---|
|  | 15 | Channel * entryWait; | 
|---|
|  | 16 | owner_lock o; | 
|---|
|  | 17 |  | 
|---|
|  | 18 | bool done = false; | 
|---|
|  | 19 | size_t tasks_done = 0; | 
|---|
|  | 20 |  | 
|---|
|  | 21 | static inline void initBarrier() { | 
|---|
|  | 22 | for ( j; BarrierSize ) | 
|---|
|  | 23 | insert( *entryWait, j ); | 
|---|
|  | 24 | } | 
|---|
|  | 25 |  | 
|---|
|  | 26 | static inline void barrier() { | 
|---|
|  | 27 | int ticket = remove( *entryWait ); | 
|---|
|  | 28 |  | 
|---|
|  | 29 | if ( ticket == BarrierSize - 1 ) { | 
|---|
|  | 30 | for ( j; BarrierSize - 1 ) | 
|---|
|  | 31 | insert( *barWait, j ); | 
|---|
|  | 32 | return; | 
|---|
|  | 33 | } | 
|---|
|  | 34 | ticket = remove( *barWait ); | 
|---|
|  | 35 |  | 
|---|
|  | 36 | // last one out | 
|---|
|  | 37 | if ( BarrierSize == 1 || ticket == BarrierSize - 2 ) { | 
|---|
|  | 38 | for ( j; BarrierSize ) | 
|---|
|  | 39 | insert( *entryWait, j ); | 
|---|
|  | 40 | } | 
|---|
|  | 41 | } | 
|---|
|  | 42 |  | 
|---|
|  | 43 | thread Task {}; | 
|---|
|  | 44 | static inline void ?{}( Task & p, cluster & clu ) { | 
|---|
|  | 45 | ((thread &)p){ clu }; | 
|---|
|  | 46 | } | 
|---|
|  | 47 | void main(Task & this) { | 
|---|
|  | 48 | size_t runs = 0; | 
|---|
|  | 49 | try { | 
|---|
|  | 50 | for ( ;; ) { | 
|---|
|  | 51 | if ( done ) break; | 
|---|
|  | 52 | barrier(); | 
|---|
|  | 53 | runs++; | 
|---|
|  | 54 | } | 
|---|
|  | 55 | } catch ( channel_closed * e ) {} | 
|---|
|  | 56 | lock(o); | 
|---|
|  | 57 | total_operations += runs; | 
|---|
|  | 58 | // sout | "P: " | runs; | 
|---|
|  | 59 | unlock(o); | 
|---|
|  | 60 | } | 
|---|
|  | 61 |  | 
|---|
|  | 62 |  | 
|---|
|  | 63 | int main( int argc, char * argv[] ) { | 
|---|
|  | 64 | switch ( argc ) { | 
|---|
|  | 65 | case 3: | 
|---|
|  | 66 | if ( strcmp( argv[2], "d" ) != 0 ) {                    // default ? | 
|---|
|  | 67 | BarrierSize = atoi( argv[2] ); | 
|---|
|  | 68 | if ( Processors < 1 ) goto Usage; | 
|---|
|  | 69 | } // if | 
|---|
|  | 70 | case 2: | 
|---|
|  | 71 | if ( strcmp( argv[1], "d" ) != 0 ) {                    // default ? | 
|---|
|  | 72 | Processors = atoi( argv[1] ); | 
|---|
|  | 73 | if ( Processors < 1 ) goto Usage; | 
|---|
|  | 74 | } // if | 
|---|
|  | 75 | case 1:                                                                                       // use defaults | 
|---|
|  | 76 | break; | 
|---|
|  | 77 | default: | 
|---|
|  | 78 | Usage: | 
|---|
|  | 79 | sout | "Usage: " | argv[0] | 
|---|
|  | 80 | | " [ processors (> 0) | 'd' (default " | Processors | 
|---|
|  | 81 | | ") ] [ BarrierSize (> 0) | 'd' (default " | BarrierSize | 
|---|
|  | 82 | | ") ]" ; | 
|---|
|  | 83 | exit( EXIT_FAILURE ); | 
|---|
|  | 84 | } // switch | 
|---|
|  | 85 | if ( Tasks < BarrierSize ) | 
|---|
|  | 86 | Tasks = BarrierSize; | 
|---|
|  | 87 |  | 
|---|
|  | 88 | size_t Clusters = 1; | 
|---|
|  | 89 | // create a cluster | 
|---|
|  | 90 | cluster clus[Clusters]; | 
|---|
|  | 91 | processor * proc[Processors]; | 
|---|
|  | 92 | for ( i; Processors ) { | 
|---|
|  | 93 | (*(proc[i] = malloc())){clus[i % Clusters]}; | 
|---|
|  | 94 | } | 
|---|
|  | 95 |  | 
|---|
|  | 96 | Channel entry{ BarrierSize }; | 
|---|
|  | 97 | Channel wait{ BarrierSize }; | 
|---|
|  | 98 |  | 
|---|
|  | 99 | entryWait = &entry; | 
|---|
|  | 100 | barWait = &wait; | 
|---|
|  | 101 |  | 
|---|
|  | 102 | initBarrier(); | 
|---|
|  | 103 |  | 
|---|
|  | 104 | sout | "start"; | 
|---|
|  | 105 | Task * t[Tasks]; | 
|---|
|  | 106 |  | 
|---|
|  | 107 | for ( i; Tasks ) { | 
|---|
|  | 108 | (*(t[i] = malloc())){ clus[i % Clusters] }; | 
|---|
|  | 109 | } | 
|---|
|  | 110 |  | 
|---|
|  | 111 | sleep(10`s); | 
|---|
|  | 112 | done = true; | 
|---|
|  | 113 |  | 
|---|
|  | 114 | sout | "sleep"; | 
|---|
|  | 115 |  | 
|---|
|  | 116 | close( entry ); | 
|---|
|  | 117 | close( wait ); | 
|---|
|  | 118 |  | 
|---|
|  | 119 | for ( i; Tasks ) { | 
|---|
|  | 120 | delete(t[i]); | 
|---|
|  | 121 | } | 
|---|
|  | 122 |  | 
|---|
|  | 123 | // sout | total_operations; | 
|---|
|  | 124 |  | 
|---|
|  | 125 | for ( i; Processors ) { | 
|---|
|  | 126 | delete(proc[i]); | 
|---|
|  | 127 | } | 
|---|
|  | 128 | sout | "done"; | 
|---|
|  | 129 | return 0; | 
|---|
|  | 130 | } | 
|---|