Last change
on this file since ecaedf35 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.5 KB
|
Rev | Line | |
---|
[c06551b] | 1 | #include <thread.hfa> |
---|
| 2 | #include <fstream.hfa> |
---|
| 3 | #include <stdlib.hfa> |
---|
| 4 | #include <mutex_stmt.hfa> |
---|
| 5 | #include <locks.hfa> |
---|
| 6 | |
---|
| 7 | simple_owner_lock l; |
---|
| 8 | pthread_cond_var( simple_owner_lock ) c; |
---|
| 9 | |
---|
| 10 | size_t num_futures = 10; |
---|
| 11 | |
---|
| 12 | future_t * futures; |
---|
| 13 | |
---|
| 14 | size_t * shuffle_arr; |
---|
| 15 | |
---|
| 16 | size_t numtimes = 10; |
---|
| 17 | |
---|
| 18 | volatile bool done = false; |
---|
| 19 | |
---|
| 20 | void synchronize() { |
---|
| 21 | lock(l); |
---|
| 22 | if (empty(c)) { |
---|
| 23 | wait(c,l); |
---|
| 24 | } else { |
---|
| 25 | for ( i; num_futures ) { |
---|
| 26 | reset(futures[i]); |
---|
| 27 | } |
---|
| 28 | notify_one(c); |
---|
| 29 | } |
---|
| 30 | unlock(l); |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | thread Waiter {}; |
---|
| 34 | void main( Waiter & this ) { |
---|
| 35 | for (numtimes) { |
---|
| 36 | wait_any(futures, num_futures); |
---|
| 37 | synchronize(); |
---|
| 38 | } |
---|
| 39 | done = true; |
---|
| 40 | while (done) notify_one(c); |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | thread Deliverer {}; |
---|
| 44 | void main( Deliverer & this ) { |
---|
| 45 | while (!done) { |
---|
| 46 | size_t num_satisfy = random(1,num_futures); |
---|
| 47 | for ( i; num_satisfy ) { // random shuffle a few values |
---|
| 48 | swap( shuffle_arr[random(num_futures)], shuffle_arr[random(num_futures)] ); |
---|
| 49 | } |
---|
| 50 | for ( i; num_satisfy ) { |
---|
[1f0ee71] | 51 | fulfil(futures[shuffle_arr[i]]); |
---|
[c06551b] | 52 | } |
---|
| 53 | synchronize(); |
---|
| 54 | } |
---|
| 55 | done = false; |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | int main() { |
---|
| 59 | sout | "start"; |
---|
| 60 | futures = alloc( num_futures ); |
---|
| 61 | shuffle_arr = alloc( num_futures ); |
---|
| 62 | for ( i; num_futures ) { // random shuffle a few values |
---|
| 63 | futures[i]{}; |
---|
| 64 | swap( shuffle_arr[random(num_futures)], shuffle_arr[random(num_futures)] ); |
---|
| 65 | } |
---|
| 66 | for ( i; num_futures ) { |
---|
| 67 | shuffle_arr[i] = i; |
---|
| 68 | } |
---|
| 69 | processor procs[1]; |
---|
| 70 | { |
---|
| 71 | Waiter w; |
---|
| 72 | Deliverer d; |
---|
| 73 | } |
---|
| 74 | free( futures ); |
---|
| 75 | free( shuffle_arr ); |
---|
| 76 | sout | "done"; |
---|
| 77 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.