|
Last change
on this file since 9c8afc7 was 00aa122, checked in by Peter A. Buhr <pabuhr@…>, 4 weeks ago |
|
harmonize single_future with other future types, remove multi_future, marks its test as deprecated, and turn off its test
|
-
Property mode
set to
100644
|
|
File size:
1.7 KB
|
| Line | |
|---|
| 1 | // DEPRECATED future type multi_future. Eventually remove this test.
|
|---|
| 2 |
|
|---|
| 3 | #include <thread.hfa>
|
|---|
| 4 | #include <future.hfa>
|
|---|
| 5 |
|
|---|
| 6 | enum {NFUTURES = 10};
|
|---|
| 7 |
|
|---|
| 8 | thread Server {
|
|---|
| 9 | int pending, done, iteration;
|
|---|
| 10 | multi_future(int) * request;
|
|---|
| 11 | };
|
|---|
| 12 |
|
|---|
| 13 | void ?{}( Server & this ) {
|
|---|
| 14 | ((thread&)this){"Server Thread"};
|
|---|
| 15 | this.pending = 0;
|
|---|
| 16 | this.done = 0;
|
|---|
| 17 | this.iteration = 0;
|
|---|
| 18 | this.request = 0p;
|
|---|
| 19 | }
|
|---|
| 20 |
|
|---|
| 21 | void ^?{}( Server & mutex this ) {
|
|---|
| 22 | assert(this.pending == 0);
|
|---|
| 23 | this.request = 0p;
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | void init( Server & this , multi_future(int) * f ) {
|
|---|
| 27 | this.request = f;
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | void call( Server & mutex this ) {
|
|---|
| 31 | this.pending++;
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | void finish( Server & mutex this ) {
|
|---|
| 35 | this.done++;
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | void main( Server & this ) {
|
|---|
| 39 | MAIN_LOOP:
|
|---|
| 40 | for() {
|
|---|
| 41 | waitfor( ^?{} : this ) {
|
|---|
| 42 | break;
|
|---|
| 43 | }
|
|---|
| 44 | or waitfor( call: this ) {
|
|---|
| 45 | if (this.pending != NFUTURES) { continue MAIN_LOOP; }
|
|---|
| 46 |
|
|---|
| 47 | this.pending = 0;
|
|---|
| 48 | fulfil( *this.request, this.iteration );
|
|---|
| 49 | this.iteration++;
|
|---|
| 50 |
|
|---|
| 51 | for(NFUTURES) {
|
|---|
| 52 | waitfor( finish: this );
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | reset( *this.request );
|
|---|
| 56 | this.done = 0;
|
|---|
| 57 | }
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | Server * the_server;
|
|---|
| 63 | thread Worker {};
|
|---|
| 64 | void ?{}(Worker & this) {
|
|---|
| 65 | ((thread&)this){"Worker Thread"};
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | multi_future(int) * shared_future;
|
|---|
| 69 |
|
|---|
| 70 | void thrash(void) {
|
|---|
| 71 | volatile int locals[250];
|
|---|
| 72 | for(i; 250) {
|
|---|
| 73 | locals[i] = 0xdeadbeef;
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | void work(int num) {
|
|---|
| 78 | call( *the_server );
|
|---|
| 79 | int res = wait( *shared_future );
|
|---|
| 80 | if( res != num ) abort();
|
|---|
| 81 | finish( *the_server );
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | void main( Worker & ) {
|
|---|
| 85 | for (i; 10) {
|
|---|
| 86 | thrash();
|
|---|
| 87 | work(i);
|
|---|
| 88 | thrash();
|
|---|
| 89 | }
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | int main() {
|
|---|
| 93 | printf( "start\n" ); // non-empty .expect file
|
|---|
| 94 | processor procs[2];
|
|---|
| 95 | shared_future = new();
|
|---|
| 96 | {
|
|---|
| 97 | Server server;
|
|---|
| 98 | the_server = &server;
|
|---|
| 99 | init(server, shared_future);
|
|---|
| 100 | {
|
|---|
| 101 | Worker workers[NFUTURES];
|
|---|
| 102 | }
|
|---|
| 103 | }
|
|---|
| 104 | delete( shared_future );
|
|---|
| 105 | printf( "done\n" ); // non-empty .expect file
|
|---|
| 106 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.