ADT
ast-experimental
pthread-emulation
Last change
on this file since 80d16f8 was a7aea7d, checked in by Thierry Delisle <tdelisle@…>, 5 years ago |
Fixed abandon function and futures and added test for it.
|
-
Property mode
set to
100644
|
File size:
1.5 KB
|
Rev | Line | |
---|
[a7aea7d] | 1 | #include <thread.hfa>
|
---|
| 2 |
|
---|
| 3 | enum {NFUTURES = 10};
|
---|
| 4 |
|
---|
| 5 | thread Server {
|
---|
| 6 | int cnt;
|
---|
| 7 | future_t * requests[NFUTURES];
|
---|
| 8 | };
|
---|
| 9 |
|
---|
| 10 | void ?{}( Server & this ) {
|
---|
| 11 | this.cnt = 0;
|
---|
| 12 | for(i; NFUTURES) {
|
---|
| 13 | this.requests[i] = 0p;
|
---|
| 14 | }
|
---|
| 15 | }
|
---|
| 16 |
|
---|
| 17 | void ^?{}( Server & mutex this ) {
|
---|
| 18 | assert(this.cnt == 0);
|
---|
| 19 | for(i; NFUTURES) {
|
---|
| 20 | this.requests[i] = 0p;
|
---|
| 21 | }
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | void process( Server & this, int i ) {
|
---|
| 25 | if( this.requests[i] == 0p ) return;
|
---|
| 26 | future_t * f = this.requests[i];
|
---|
| 27 | this.requests[i] = 0p;
|
---|
| 28 | this.cnt--;
|
---|
| 29 | fulfil( *f );
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | void call( Server & mutex this, future_t & f ) {
|
---|
| 33 | for(i; NFUTURES) {
|
---|
| 34 | if( this.requests[i] == 0p ) {
|
---|
| 35 | this.requests[i] = &f;
|
---|
| 36 | this.cnt++;
|
---|
| 37 | return;
|
---|
| 38 | }
|
---|
| 39 | }
|
---|
| 40 | abort("Monitor Error");
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | void main( Server & this ) {
|
---|
| 44 | unsigned i = 0;
|
---|
| 45 | for() {
|
---|
| 46 | waitfor( ^?{} : this ) {
|
---|
| 47 | break;
|
---|
| 48 | }
|
---|
| 49 | or when( this.cnt < NFUTURES ) waitfor( call: this ) {}
|
---|
| 50 | or else {
|
---|
| 51 | process( this, i % NFUTURES );
|
---|
| 52 | i++;
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | for(i; NFUTURES) {
|
---|
| 57 | process( this, i );
|
---|
| 58 | }
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | Server * the_server;
|
---|
| 62 | thread Worker {};
|
---|
| 63 |
|
---|
| 64 | void thrash(void) {
|
---|
| 65 | volatile int locals[250];
|
---|
| 66 | for(i; 250) {
|
---|
| 67 | locals[i] = 0xdeadbeef;
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | void work(int i) {
|
---|
| 72 | future_t *mine = new();
|
---|
| 73 | call( *the_server, *mine );
|
---|
| 74 | if (i % 2 == 0) {
|
---|
| 75 | abandon( *mine );
|
---|
| 76 | } else {
|
---|
| 77 | wait( *mine );
|
---|
| 78 | delete( mine );
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | void main( Worker & ) {
|
---|
| 83 | for(i;150) {
|
---|
| 84 | thrash();
|
---|
| 85 | work(i);
|
---|
| 86 | thrash();
|
---|
| 87 | }
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | int main() {
|
---|
| 91 | printf( "start\n" ); // non-empty .expect file
|
---|
| 92 | processor procs[2];
|
---|
| 93 | {
|
---|
| 94 | Server server;
|
---|
| 95 | the_server = &server;
|
---|
| 96 | {
|
---|
| 97 | Worker workers[17];
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 | printf( "done\n" ); // non-empty .expect file
|
---|
| 101 |
|
---|
| 102 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.