ADT
        aaron-thesis
        arm-eh
        ast-experimental
        cleanup-dtors
        deferred_resn
        demangler
        enum
        forall-pointer-decay
        jacob/cs343-translation
        jenkins-sandbox
        new-ast
        new-ast-unique-expr
        new-env
        no_list
        persistent-indexer
        pthread-emulation
        qualifiedEnum
        with_gc
      
      
        
          | Last change
 on this file since b9f383f was             0aa1b89, checked in by Thierry Delisle <tdelisle@…>, 8 years ago | 
        
          | 
Renamed concurrent tests
 | 
        
          | 
              
Property                 mode
 set to                 100644 | 
        
          | File size:
            1.7 KB | 
      
      
| Line |  | 
|---|
| 1 | //--------------------------------------------------------- | 
|---|
| 2 | // Barging test | 
|---|
| 3 | // Ensures that no barging can occur between : | 
|---|
| 4 | //   - the frontend of the waitfor and the waited call | 
|---|
| 5 | //   - the waited call and the backend of the waitfor | 
|---|
| 6 | //--------------------------------------------------------- | 
|---|
| 7 |  | 
|---|
| 8 | #include <fstream> | 
|---|
| 9 | #include <kernel> | 
|---|
| 10 | #include <monitor> | 
|---|
| 11 | #include <stdlib> | 
|---|
| 12 | #include <thread> | 
|---|
| 13 |  | 
|---|
| 14 | #include <stdbool.h> | 
|---|
| 15 |  | 
|---|
| 16 | static const unsigned long N = 5_000ul; | 
|---|
| 17 |  | 
|---|
| 18 | enum state_t { WAITFOR, CALL, BARGE }; | 
|---|
| 19 |  | 
|---|
| 20 | monitor global_t { | 
|---|
| 21 | bool done; | 
|---|
| 22 | bool started; | 
|---|
| 23 | state_t state; | 
|---|
| 24 | }; | 
|---|
| 25 |  | 
|---|
| 26 | void ?{} ( global_t & this ) { | 
|---|
| 27 | this.done = false; | 
|---|
| 28 | this.started = false; | 
|---|
| 29 | this.state = BARGE; | 
|---|
| 30 | } | 
|---|
| 31 |  | 
|---|
| 32 | void ^?{} ( global_t & mutex this ) {} | 
|---|
| 33 |  | 
|---|
| 34 | global_t global; | 
|---|
| 35 |  | 
|---|
| 36 | bool barge( global_t & mutex this ) { | 
|---|
| 37 | this.state = BARGE; | 
|---|
| 38 | return !this.done; | 
|---|
| 39 | } | 
|---|
| 40 |  | 
|---|
| 41 | thread barger_t {}; | 
|---|
| 42 | void main( barger_t & this ) { | 
|---|
| 43 | yield(); | 
|---|
| 44 | while( barge( global ) ) { yield(random( 10 )); } | 
|---|
| 45 | } | 
|---|
| 46 |  | 
|---|
| 47 | bool do_call( global_t & mutex this ) { | 
|---|
| 48 | yield(random( 10 )); | 
|---|
| 49 | if( this.state != WAITFOR && !this.done && this.started ) { | 
|---|
| 50 | serr | "Barging before caller detected" | endl; | 
|---|
| 51 | } | 
|---|
| 52 |  | 
|---|
| 53 | this.state = CALL; | 
|---|
| 54 | return !this.done; | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 | thread caller_t {}; | 
|---|
| 58 | void main( caller_t & this ) { | 
|---|
| 59 | while( do_call(global) ) { yield(random( 10 )); } | 
|---|
| 60 | } | 
|---|
| 61 |  | 
|---|
| 62 | void do_wait( global_t & mutex this ) { | 
|---|
| 63 | this.started = true; | 
|---|
| 64 | for( int i = 0; i < N; i++) { | 
|---|
| 65 | yield(random( 10 )); | 
|---|
| 66 | this.state = WAITFOR; | 
|---|
| 67 | waitfor(do_call, this) { | 
|---|
| 68 | sout | i | endl; | 
|---|
| 69 | } | 
|---|
| 70 |  | 
|---|
| 71 | if( this.state != CALL ) { | 
|---|
| 72 | serr | "Barging after caller detected" | endl; | 
|---|
| 73 | } | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | this.done = true; | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | thread waiter_t{}; | 
|---|
| 80 | void main( waiter_t & this ) { | 
|---|
| 81 | do_wait(global); | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | int main() { | 
|---|
| 85 | sout | "Starting" | endl; | 
|---|
| 86 | { | 
|---|
| 87 | barger_t bargers[17]; | 
|---|
| 88 | caller_t callers[7]; | 
|---|
| 89 | waiter_t waiters; | 
|---|
| 90 | } | 
|---|
| 91 | sout | "Stopping" | endl; | 
|---|
| 92 | } | 
|---|
       
      
  Note:
 See   
TracBrowser
 for help on using the repository browser.