source: tests/concurrent/futures/wait_any.cfa @ 6d41f66

pthread-emulationqualifiedEnum
Last change on this file since 6d41f66 was 6d41f66, checked in by Thierry Delisle <tdelisle@…>, 6 months ago

Fixed wait_any to use fulfil rather than post

  • Property mode set to 100644
File size: 1.5 KB
Line 
1#include <thread.hfa>
2#include <fstream.hfa>
3#include <stdlib.hfa>
4#include <mutex_stmt.hfa>
5#include <locks.hfa>
6
7simple_owner_lock l;
8pthread_cond_var( simple_owner_lock ) c;
9
10size_t num_futures = 10;
11
12future_t * futures;
13
14size_t * shuffle_arr;
15
16size_t numtimes = 10;
17
18volatile bool done = false;
19
20void 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
33thread Waiter {};
34void 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
43thread Deliverer {};
44void 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 ) {
51            fulfil(*futures[shuffle_arr[i]]);
52        }
53        synchronize();
54    }
55    done = false;
56}
57
58int 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.