Last change
on this file since 65ef0cd was
c26bea2a,
checked in by Peter A. Buhr <pabuhr@…>, 18 months ago
|
first attempt at renaming directory tests/concurrent to tests/concurrency to harmonize with other concurrency directory names
|
-
Property mode set to
100644
|
File size:
1.1 KB
|
Rev | Line | |
---|
[3381ed7] | 1 | #include <kernel.hfa> |
---|
| 2 | #include <thread.hfa> |
---|
| 3 | |
---|
[1bcbf02] | 4 | __thread drand48_data buffer = { 0 }; |
---|
[3381ed7] | 5 | int myrand() { |
---|
| 6 | long int result; |
---|
| 7 | lrand48_r(&buffer, &result); |
---|
| 8 | return result; |
---|
| 9 | } |
---|
| 10 | |
---|
| 11 | |
---|
| 12 | thread Thread {}; |
---|
| 13 | void ^?{}(Thread & mutex this) {} |
---|
| 14 | |
---|
| 15 | enum Constants { blocked_size = 20 }; |
---|
| 16 | Thread * volatile blocked[blocked_size]; |
---|
| 17 | |
---|
| 18 | void main( Thread & this ) { |
---|
| 19 | for(int i = 0; i < 1000; i++) { |
---|
| 20 | int idx = myrand() % blocked_size; |
---|
| 21 | if(blocked[idx]) { |
---|
| 22 | Thread * thrd = __atomic_exchange_n(&blocked[idx], 0p, __ATOMIC_SEQ_CST); |
---|
[e235429] | 23 | unpark( *thrd ); |
---|
[3381ed7] | 24 | } else { |
---|
| 25 | Thread * thrd = __atomic_exchange_n(&blocked[idx], &this, __ATOMIC_SEQ_CST); |
---|
[e235429] | 26 | unpark( *thrd ); |
---|
| 27 | park(); |
---|
[3381ed7] | 28 | } |
---|
| 29 | } |
---|
| 30 | printf("Done\n"); |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | // Extra thread to avoid deadlocking |
---|
| 34 | thread Unparker {}; |
---|
| 35 | |
---|
| 36 | void main( Unparker & this ) { |
---|
| 37 | while(true) { |
---|
| 38 | waitfor( ^?{} : this ) { |
---|
| 39 | break; |
---|
| 40 | } or else { |
---|
| 41 | int idx = myrand() % blocked_size; |
---|
| 42 | Thread * thrd = __atomic_exchange_n(&blocked[idx], 0p, __ATOMIC_SEQ_CST); |
---|
[e235429] | 43 | unpark( *thrd ); |
---|
[3381ed7] | 44 | yield( myrand() % 20 ); |
---|
| 45 | } |
---|
| 46 | } |
---|
| 47 | printf("Done Unparker\n"); |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | |
---|
| 51 | int main() { |
---|
| 52 | for(i ; blocked_size) { |
---|
| 53 | blocked[i] = 0p; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | processor p[3]; |
---|
| 57 | |
---|
| 58 | Unparker u; |
---|
| 59 | { |
---|
| 60 | Thread t[20]; |
---|
| 61 | } |
---|
| 62 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.