[2ba0bc7] | 1 | // -*- Mode: C -*- |
---|
| 2 | // |
---|
| 3 | // The contents of this file are covered under the licence agreement in the |
---|
| 4 | // file "LICENCE" distributed with Cforall. |
---|
| 5 | // |
---|
| 6 | // datingService.c -- |
---|
| 7 | // |
---|
| 8 | // Author : Peter A. Buhr |
---|
| 9 | // Created On : Mon Oct 30 12:56:20 2017 |
---|
| 10 | // Last Modified By : Peter A. Buhr |
---|
| 11 | // Last Modified On : Mon Oct 30 17:58:41 2017 |
---|
| 12 | // Update Count : 14 |
---|
| 13 | // |
---|
| 14 | |
---|
| 15 | #include <stdlib> // rand48 |
---|
| 16 | #include <fstream> |
---|
| 17 | #include <kernel> |
---|
| 18 | #include <thread> |
---|
| 19 | #include <unistd.h> // getpid |
---|
| 20 | |
---|
| 21 | bool empty( condition & c ) { |
---|
| 22 | return c.blocked.head == NULL; |
---|
| 23 | } |
---|
| 24 | |
---|
| 25 | enum { NoOfPairs = 20 }; |
---|
| 26 | |
---|
| 27 | monitor DatingService { |
---|
| 28 | condition Girls[NoOfPairs], Boys[NoOfPairs]; |
---|
| 29 | unsigned int GirlPhoneNo, BoyPhoneNo; |
---|
| 30 | }; // DatingService |
---|
| 31 | |
---|
| 32 | unsigned int girl( DatingService & mutex ds, unsigned int PhoneNo, unsigned int ccode ) { |
---|
| 33 | if ( empty( ds.Boys[ccode] ) ) { |
---|
| 34 | wait( &ds.Girls[ccode] ); |
---|
| 35 | ds.GirlPhoneNo = PhoneNo; |
---|
| 36 | } else { |
---|
| 37 | ds.GirlPhoneNo = PhoneNo; |
---|
| 38 | signal_block( &ds.Boys[ccode] ); |
---|
| 39 | } // if |
---|
| 40 | return ds.BoyPhoneNo; |
---|
| 41 | } // DatingService girl |
---|
| 42 | |
---|
| 43 | unsigned int boy( DatingService & mutex ds, unsigned int PhoneNo, unsigned int ccode ) { |
---|
| 44 | if ( empty( ds.Girls[ccode] ) ) { |
---|
| 45 | wait( &ds.Boys[ccode] ); |
---|
| 46 | ds.BoyPhoneNo = PhoneNo; |
---|
| 47 | } else { |
---|
| 48 | ds.BoyPhoneNo = PhoneNo; |
---|
| 49 | signal_block( &ds.Girls[ccode] ); |
---|
| 50 | } // if |
---|
| 51 | return ds.GirlPhoneNo; |
---|
| 52 | } // DatingService boy |
---|
| 53 | |
---|
| 54 | unsigned int girlck[NoOfPairs]; |
---|
| 55 | unsigned int boyck[NoOfPairs]; |
---|
| 56 | |
---|
| 57 | thread Girl { |
---|
| 58 | DatingService & TheExchange; |
---|
| 59 | unsigned int id, ccode; |
---|
| 60 | }; // Girl |
---|
| 61 | |
---|
| 62 | void main( Girl & g ) { |
---|
| 63 | yield( rand48( 100 ) ); // don't all start at the same time |
---|
| 64 | unsigned int partner = girl( g.TheExchange, g.id, g.ccode ); |
---|
| 65 | //sout | "Girl:" | g.id | "is dating Boy at" | partner | "with ccode" | g.ccode | endl; |
---|
| 66 | girlck[g.id] = partner; |
---|
| 67 | } // Girl main |
---|
| 68 | |
---|
| 69 | void ?{}( Girl & g, DatingService * TheExchange, unsigned int id, unsigned int ccode ) { |
---|
| 70 | &g.TheExchange = TheExchange; |
---|
| 71 | g.id = id; |
---|
| 72 | g.ccode = ccode; |
---|
| 73 | } // Girl ?{} |
---|
| 74 | |
---|
| 75 | thread Boy { |
---|
| 76 | DatingService &TheExchange; |
---|
| 77 | unsigned int id, ccode; |
---|
| 78 | }; // Boy |
---|
| 79 | |
---|
| 80 | void main( Boy & b ) { |
---|
| 81 | yield( rand48( 100 ) ); // don't all start at the same time |
---|
| 82 | unsigned int partner = boy( b.TheExchange, b.id, b.ccode ); |
---|
| 83 | //sout | " Boy:" | b.id | "is dating Girl" | partner | "with ccode" | b.ccode | endl; |
---|
| 84 | boyck[b.id] = partner; |
---|
| 85 | } // Boy main |
---|
| 86 | |
---|
| 87 | void ?{}( Boy & b, DatingService * TheExchange, unsigned int id, unsigned int ccode ) { |
---|
| 88 | &b.TheExchange = TheExchange; |
---|
| 89 | b.id = id; |
---|
| 90 | b.ccode = ccode; |
---|
| 91 | } // Boy ?{} |
---|
| 92 | |
---|
| 93 | int main() { |
---|
| 94 | DatingService TheExchange; |
---|
| 95 | Girl *girls[NoOfPairs]; |
---|
| 96 | Boy *boys[NoOfPairs]; |
---|
| 97 | |
---|
| 98 | rand48seed( getpid() ); |
---|
| 99 | |
---|
| 100 | for ( unsigned int i = 0; i < NoOfPairs; i += 1 ) { |
---|
| 101 | girls[i] = new( &TheExchange, i, i ); |
---|
| 102 | boys[i] = new( &TheExchange, i, NoOfPairs - ( i + 1 ) ); |
---|
| 103 | } // for |
---|
| 104 | |
---|
| 105 | for ( unsigned int i = 0; i < NoOfPairs; i += 1 ) { |
---|
| 106 | delete( boys[i] ); |
---|
| 107 | delete( girls[i] ); |
---|
| 108 | } // for |
---|
| 109 | |
---|
| 110 | for ( unsigned int i = 0; i < NoOfPairs; i += 1 ) { |
---|
| 111 | if ( girlck[ boyck[i] ] != boyck[ girlck[i] ] ) abort(); |
---|
| 112 | } // for |
---|
| 113 | } // main |
---|
| 114 | |
---|
| 115 | // Local Variables: // |
---|
| 116 | // tab-width: 4 // |
---|
| 117 | // compile-command: "cfa datingService.c" // |
---|
| 118 | // End: // |
---|