ADT
        arm-eh
        ast-experimental
        enum
        forall-pointer-decay
        jacob/cs343-translation
        new-ast-unique-expr
        pthread-emulation
        qualifiedEnum
      
      
        
          | 
            Last change
 on this file since c8e37e0 was             44e37ef, checked in by Thierry Delisle <tdelisle@…>, 5 years ago           | 
        
        
          | 
             
Added simple test for multi-future 
 
           | 
        
        
          
            
              - 
Property                 mode
 set to                 
100644
               
             
           | 
        
        
          | 
            File size:
            1.5 KB
           | 
        
      
      
| Line |   | 
|---|
| 1 | #include <thread.hfa>
 | 
|---|
| 2 | #include <future.hfa>
 | 
|---|
| 3 | 
 | 
|---|
| 4 | enum {NFUTURES = 10};
 | 
|---|
| 5 | 
 | 
|---|
| 6 | thread Server {
 | 
|---|
| 7 |         int cnt, iteration;
 | 
|---|
| 8 |         multi_future(int) * request;
 | 
|---|
| 9 | };
 | 
|---|
| 10 | 
 | 
|---|
| 11 | void ?{}( Server & this ) {
 | 
|---|
| 12 |         this.cnt = 0;
 | 
|---|
| 13 |         this.iteration = 0;
 | 
|---|
| 14 |         this.request = 0p;
 | 
|---|
| 15 | }
 | 
|---|
| 16 | 
 | 
|---|
| 17 | void ^?{}( Server & mutex this ) {
 | 
|---|
| 18 |         assert(this.cnt == 0);
 | 
|---|
| 19 |     this.request = 0p;
 | 
|---|
| 20 | }
 | 
|---|
| 21 | 
 | 
|---|
| 22 | void init( Server & this , multi_future(int) * f ) {
 | 
|---|
| 23 |         this.request = f;
 | 
|---|
| 24 | }
 | 
|---|
| 25 | 
 | 
|---|
| 26 | void process( Server & mutex this ) {
 | 
|---|
| 27 |         fulfil( *this.request, this.iteration );
 | 
|---|
| 28 |         this.iteration++;
 | 
|---|
| 29 | }
 | 
|---|
| 30 | 
 | 
|---|
| 31 | void call( Server & mutex this ) {
 | 
|---|
| 32 |         this.cnt++;
 | 
|---|
| 33 | }
 | 
|---|
| 34 | 
 | 
|---|
| 35 | void finish( Server & mutex this ) { }
 | 
|---|
| 36 | 
 | 
|---|
| 37 | void main( Server & this ) {
 | 
|---|
| 38 |         for() {
 | 
|---|
| 39 |                 waitfor( ^?{} : this ) {
 | 
|---|
| 40 |                         break;
 | 
|---|
| 41 |                 }
 | 
|---|
| 42 |                 or when( this.cnt < NFUTURES ) waitfor( call: this ) {
 | 
|---|
| 43 |                         if (this.cnt == NFUTURES) {
 | 
|---|
| 44 |                                 process(this);
 | 
|---|
| 45 |                         }
 | 
|---|
| 46 |                 }
 | 
|---|
| 47 |                 or waitfor( finish: this ) {
 | 
|---|
| 48 |                         if (this.cnt == NFUTURES) {
 | 
|---|
| 49 |                                 reset( *this.request );
 | 
|---|
| 50 |                                 this.cnt = 0;
 | 
|---|
| 51 |                         }
 | 
|---|
| 52 |                 }
 | 
|---|
| 53 |         }
 | 
|---|
| 54 | 
 | 
|---|
| 55 | }
 | 
|---|
| 56 | 
 | 
|---|
| 57 | Server * the_server;
 | 
|---|
| 58 | thread Worker {};
 | 
|---|
| 59 | multi_future(int) * shared_future;
 | 
|---|
| 60 | 
 | 
|---|
| 61 | void thrash(void) {
 | 
|---|
| 62 |         volatile int locals[250];
 | 
|---|
| 63 |         for(i; 250) {
 | 
|---|
| 64 |                 locals[i] = 0xdeadbeef;
 | 
|---|
| 65 |         }
 | 
|---|
| 66 | }
 | 
|---|
| 67 | 
 | 
|---|
| 68 | void work(int num) {
 | 
|---|
| 69 |         call( *the_server );
 | 
|---|
| 70 |         int res = wait( *shared_future );
 | 
|---|
| 71 |         if( res != num ) abort();
 | 
|---|
| 72 |         finish( *the_server );
 | 
|---|
| 73 | }
 | 
|---|
| 74 | 
 | 
|---|
| 75 | void main( Worker & ) {
 | 
|---|
| 76 |         for (i; 10) {
 | 
|---|
| 77 |                 thrash();
 | 
|---|
| 78 |                 work(i);
 | 
|---|
| 79 |                 thrash();
 | 
|---|
| 80 |         }
 | 
|---|
| 81 | }
 | 
|---|
| 82 | 
 | 
|---|
| 83 | int main() {
 | 
|---|
| 84 |         printf( "start\n" );                            // non-empty .expect file
 | 
|---|
| 85 |         processor procs[2];
 | 
|---|
| 86 |         shared_future = new();
 | 
|---|
| 87 |         {
 | 
|---|
| 88 |                 Server server;
 | 
|---|
| 89 |                 the_server = &server;
 | 
|---|
| 90 |                 init(server, shared_future);
 | 
|---|
| 91 |                 {
 | 
|---|
| 92 |                         Worker workers[NFUTURES];
 | 
|---|
| 93 |                 }
 | 
|---|
| 94 |         }
 | 
|---|
| 95 |         delete( shared_future );
 | 
|---|
| 96 |         printf( "done\n" );                             // non-empty .expect file
 | 
|---|
| 97 | }
 | 
|---|
       
      
  Note:
 See   
TracBrowser
 for help on using the repository browser.