[c51124b] | 1 | #include <fstream.hfa> |
---|
| 2 | #include <locks.hfa> |
---|
| 3 | #include <thread.hfa> |
---|
| 4 | |
---|
| 5 | enum { num_blockers = 17, num_unblockers = 13 }; |
---|
| 6 | |
---|
| 7 | void thrash() { |
---|
| 8 | unsigned t[100]; |
---|
| 9 | for(i; 100) { |
---|
| 10 | t[i] = 0xDEADBEEF; |
---|
| 11 | } |
---|
| 12 | } |
---|
| 13 | |
---|
| 14 | Semaphore0nary sem; |
---|
| 15 | |
---|
| 16 | const unsigned int num_blocks = 1625; |
---|
| 17 | |
---|
| 18 | thread Blocker { |
---|
| 19 | size_t sum; |
---|
| 20 | }; |
---|
| 21 | void main(Blocker & this); |
---|
| 22 | |
---|
| 23 | Blocker * from_thread($thread * t) { |
---|
| 24 | Blocker & nullb = *(Blocker*)0p; |
---|
| 25 | $thread & nullt = (thread&)nullb; |
---|
| 26 | uintptr_t offset = (uintptr_t)&nullt; |
---|
| 27 | uintptr_t address = ((uintptr_t)t) - offset; |
---|
| 28 | return (Blocker*)address; |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | void main(Blocker & this) { |
---|
| 32 | $thread * me = active_thread(); |
---|
| 33 | Blocker * me1 = &this; |
---|
| 34 | Blocker * me2 = from_thread(me); |
---|
| 35 | if( me1 != me2 ) sout | "Bad casting!" | me1 | "vs" | me2; |
---|
| 36 | this.sum = 0; |
---|
| 37 | for(num_blocks) { |
---|
| 38 | // sout | "b P" | me; |
---|
| 39 | P(sem); |
---|
| 40 | if(((thread&)this).seqable.next != 0p) sout | "Link not invalidated"; |
---|
| 41 | // sout | "a P" | me; |
---|
| 42 | } |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | const unsigned int num_unblocks = 2125; |
---|
| 46 | |
---|
| 47 | thread Unblocker { |
---|
| 48 | size_t sum; |
---|
| 49 | }; |
---|
| 50 | |
---|
| 51 | void main(Unblocker & this) { |
---|
| 52 | this.sum = 0; |
---|
| 53 | unsigned me = (unsigned)&this; |
---|
| 54 | for(num_unblocks) { |
---|
| 55 | $thread * t = V(sem, false); |
---|
| 56 | Blocker * b = from_thread(t); |
---|
| 57 | b->sum += me; |
---|
| 58 | this.sum += (unsigned)b; |
---|
| 59 | unpark(t); |
---|
| 60 | // sout | "a V" | t; |
---|
| 61 | yield(random(10)); |
---|
| 62 | } |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | int main() { |
---|
| 66 | size_t usum = 0; |
---|
| 67 | size_t bsum = 0; |
---|
| 68 | |
---|
| 69 | if((num_unblocks * num_unblockers) != (num_blocks * num_blockers)) sout | "Mismatched Operations: " | (num_unblocks * num_unblockers) | "vs" | (num_blocks * num_blockers); |
---|
| 70 | |
---|
| 71 | sout | "Starting"; |
---|
| 72 | { |
---|
| 73 | Blocker blockers [num_blockers ]; |
---|
| 74 | Unblocker unblockers[num_unblockers]; |
---|
| 75 | |
---|
| 76 | for(i;num_blockers) { |
---|
| 77 | for(num_blocks) |
---|
| 78 | usum += (unsigned)&blockers[i]; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | for(i;num_unblockers) { |
---|
| 82 | for(num_unblocks) |
---|
| 83 | bsum += (unsigned)&unblockers[i]; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | for(i;num_blockers) { |
---|
| 87 | bsum -= join(blockers[i]).sum; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | for(i;num_unblockers) { |
---|
| 91 | usum -= join(unblockers[i]).sum; |
---|
| 92 | } |
---|
| 93 | } |
---|
| 94 | sout | "Done!"; |
---|
| 95 | if(bsum == 0 && usum == 0) sout | "Match!"; |
---|
| 96 | else sout | "No Match!" | usum | "/" | bsum; |
---|
| 97 | } |
---|