ADT
      
      
        
          | Last change
 on this file since f11010e was             357ab79, checked in by caparsons <caparson@…>, 3 years ago | 
        
          | 
moved unified locking tests to be under concurrent directory since they probably should have been there this whole time
 | 
        
          | 
              
Property                 mode
 set to                 100644 | 
        
          | File size:
            968 bytes | 
      
      
| Line |  | 
|---|
| 1 | #include <fstream.hfa> | 
|---|
| 2 | #include <locks.hfa> | 
|---|
| 3 | #include <thread.hfa> | 
|---|
| 4 |  | 
|---|
| 5 | const unsigned int num_times = 50000; | 
|---|
| 6 |  | 
|---|
| 7 | struct MutexObj { | 
|---|
| 8 | mcs_lock l; | 
|---|
| 9 | thread$ * id; | 
|---|
| 10 | size_t sum; | 
|---|
| 11 | }; | 
|---|
| 12 |  | 
|---|
| 13 | MutexObj mo; | 
|---|
| 14 |  | 
|---|
| 15 | void trash() { | 
|---|
| 16 | unsigned t[100]; | 
|---|
| 17 | for(i; 100) { | 
|---|
| 18 | t[i] = 0xDEADBEEF; | 
|---|
| 19 | } | 
|---|
| 20 | } | 
|---|
| 21 |  | 
|---|
| 22 | unsigned cs() { | 
|---|
| 23 | thread$ * me = active_thread(); | 
|---|
| 24 | unsigned value = (unsigned)me; | 
|---|
| 25 | mcs_node n; | 
|---|
| 26 | lock(mo.l, n); | 
|---|
| 27 | { | 
|---|
| 28 | size_t tsum = mo.sum; | 
|---|
| 29 | mo.id = me; | 
|---|
| 30 | yield(random(5)); | 
|---|
| 31 | if(mo.id != me) sout | "Intruder!"; | 
|---|
| 32 | mo.sum = tsum + value; | 
|---|
| 33 | } | 
|---|
| 34 | unlock(mo.l, n); | 
|---|
| 35 | return value; | 
|---|
| 36 | } | 
|---|
| 37 |  | 
|---|
| 38 | thread LockCheck { | 
|---|
| 39 | size_t sum; | 
|---|
| 40 | }; | 
|---|
| 41 |  | 
|---|
| 42 | void main(LockCheck & this) { | 
|---|
| 43 | this.sum = 0; | 
|---|
| 44 | for(num_times) { | 
|---|
| 45 | trash(); | 
|---|
| 46 | this.sum += cs(); | 
|---|
| 47 | trash(); | 
|---|
| 48 | yield(random(10)); | 
|---|
| 49 | } | 
|---|
| 50 | } | 
|---|
| 51 |  | 
|---|
| 52 | int main() { | 
|---|
| 53 | size_t sum = -32; | 
|---|
| 54 | mo.sum = -32; | 
|---|
| 55 | processor p[2]; | 
|---|
| 56 | sout | "Starting"; | 
|---|
| 57 | { | 
|---|
| 58 | LockCheck checkers[13]; | 
|---|
| 59 | for(i;13) { | 
|---|
| 60 | sum += join(checkers[i]).sum; | 
|---|
| 61 | } | 
|---|
| 62 | } | 
|---|
| 63 | sout | "Done!"; | 
|---|
| 64 | if(sum == mo.sum) sout | "Match!"; | 
|---|
| 65 | else sout | "No Match!" | sum | "vs" | mo.sum; | 
|---|
| 66 | } | 
|---|
       
      
  Note:
 See   
TracBrowser
 for help on using the repository browser.