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 fb0be05 was             e235429, checked in by Thierry Delisle <tdelisle@…>, 5 years ago           | 
        
        
          | 
             
Removed last parker/unparker information is it was not particularly useful 
 
           | 
        
        
          
            
              - 
Property                 mode
 set to                 
100644
               
             
           | 
        
        
          | 
            File size:
            1.1 KB
           | 
        
      
      
| Rev | Line |   | 
|---|
| [3381ed7] | 1 | #include <thread.hfa>
 | 
|---|
 | 2 | #include <fstream.hfa>
 | 
|---|
 | 3 | 
 | 
|---|
 | 4 | volatile int global = 0;
 | 
|---|
 | 5 | 
 | 
|---|
 | 6 | thread Poller {};
 | 
|---|
 | 7 | void main(Poller & this) {
 | 
|---|
 | 8 |         while(true) {
 | 
|---|
 | 9 |                 waitfor( ^?{} : this ) {
 | 
|---|
 | 10 |                         break;
 | 
|---|
 | 11 |                 } or else {
 | 
|---|
 | 12 |                         global = (global + 1) % 10;
 | 
|---|
 | 13 |                         yield();
 | 
|---|
 | 14 |                 }
 | 
|---|
 | 15 |         }
 | 
|---|
 | 16 | }
 | 
|---|
 | 17 | 
 | 
|---|
 | 18 | thread Waiter {};
 | 
|---|
 | 19 | 
 | 
|---|
| [b0c7419] | 20 | volatile int count = 0;
 | 
|---|
 | 21 | 
 | 
|---|
 | 22 | void main(Waiter & this) {
 | 
|---|
 | 23 |         // Get a unique id
 | 
|---|
 | 24 |         int id = __atomic_fetch_add(&count, 1, __ATOMIC_SEQ_CST);
 | 
|---|
| [a254fa56] | 25 |         int id_hash = id | (id << 8) | (id << 16) | (id << 24);
 | 
|---|
 | 26 |         int mask = 0xCAFEBABA;
 | 
|---|
| [50b8885] | 27 | 
 | 
|---|
| [3381ed7] | 28 |         for(int i = 0; i < 5; i++) {
 | 
|---|
| [a254fa56] | 29 |                 assert(mask == 0xCAFEBABA);
 | 
|---|
 | 30 | 
 | 
|---|
| [3381ed7] | 31 |                 // Unpark this thread, don't force a yield
 | 
|---|
| [e235429] | 32 |                 unpark( this );
 | 
|---|
| [a254fa56] | 33 |                 assert(mask == 0xCAFEBABA);
 | 
|---|
 | 34 | 
 | 
|---|
 | 35 |                 // Hash the mask to make sure no one else messes with them
 | 
|---|
 | 36 |                 mask ^= id_hash;
 | 
|---|
 | 37 |                 assert(mask == (id_hash ^ 0xCAFEBABA));
 | 
|---|
| [3381ed7] | 38 | 
 | 
|---|
 | 39 |                 // Force a preemption before the call to park
 | 
|---|
 | 40 |                 int prev = global;
 | 
|---|
 | 41 |                 while(prev == global) {}
 | 
|---|
 | 42 | 
 | 
|---|
 | 43 |                 // Park this thread,
 | 
|---|
| [a254fa56] | 44 |                 assert(mask == (id_hash ^ 0xCAFEBABA));
 | 
|---|
| [e235429] | 45 |                 park();
 | 
|---|
| [a254fa56] | 46 |                 assert(mask == (id_hash ^ 0xCAFEBABA));
 | 
|---|
 | 47 | 
 | 
|---|
 | 48 |                 // Reset the hash and recheck it
 | 
|---|
 | 49 |                 mask ^= id_hash;
 | 
|---|
 | 50 |                 assert(mask == 0xCAFEBABA);
 | 
|---|
| [3381ed7] | 51 |         }
 | 
|---|
 | 52 | }
 | 
|---|
 | 53 | 
 | 
|---|
 | 54 | int main() {
 | 
|---|
 | 55 |         Poller p;
 | 
|---|
 | 56 |         {
 | 
|---|
 | 57 |                 Waiter waiters[5];
 | 
|---|
 | 58 |         }
 | 
|---|
| [66812dd] | 59 |         printf( "done\n" );                             // non-empty .expect file
 | 
|---|
 | 60 | }
 | 
|---|
       
      
  Note:
 See   
TracBrowser
 for help on using the repository browser.