| 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 23:02:11 2017 | 
|---|
| 12 | // Update Count     : 15 | 
|---|
| 13 | // | 
|---|
| 14 |  | 
|---|
| 15 | #include <stdlib>                                                                               // random | 
|---|
| 16 | #include <fstream> | 
|---|
| 17 | #include <kernel> | 
|---|
| 18 | #include <thread> | 
|---|
| 19 | #include <unistd.h>                                                                             // getpid | 
|---|
| 20 |  | 
|---|
| 21 | enum { NoOfPairs = 20 }; | 
|---|
| 22 |  | 
|---|
| 23 | monitor DatingService { | 
|---|
| 24 | condition Girls[NoOfPairs], Boys[NoOfPairs]; | 
|---|
| 25 | unsigned int GirlPhoneNo, BoyPhoneNo; | 
|---|
| 26 | }; // DatingService | 
|---|
| 27 |  | 
|---|
| 28 | unsigned int girl( DatingService & mutex ds, unsigned int PhoneNo, unsigned int ccode ) { | 
|---|
| 29 | if ( is_empty( ds.Boys[ccode] ) ) { | 
|---|
| 30 | wait( ds.Girls[ccode] ); | 
|---|
| 31 | ds.GirlPhoneNo = PhoneNo; | 
|---|
| 32 | } else { | 
|---|
| 33 | ds.GirlPhoneNo = PhoneNo; | 
|---|
| 34 | signal_block( ds.Boys[ccode] ); | 
|---|
| 35 | } // if | 
|---|
| 36 | return ds.BoyPhoneNo; | 
|---|
| 37 | } // DatingService girl | 
|---|
| 38 |  | 
|---|
| 39 | unsigned int boy( DatingService & mutex ds, unsigned int PhoneNo, unsigned int ccode ) { | 
|---|
| 40 | if ( is_empty( ds.Girls[ccode] ) ) { | 
|---|
| 41 | wait( ds.Boys[ccode] ); | 
|---|
| 42 | ds.BoyPhoneNo = PhoneNo; | 
|---|
| 43 | } else { | 
|---|
| 44 | ds.BoyPhoneNo = PhoneNo; | 
|---|
| 45 | signal_block( ds.Girls[ccode] ); | 
|---|
| 46 | } // if | 
|---|
| 47 | return ds.GirlPhoneNo; | 
|---|
| 48 | } // DatingService boy | 
|---|
| 49 |  | 
|---|
| 50 | unsigned int girlck[NoOfPairs]; | 
|---|
| 51 | unsigned int boyck[NoOfPairs]; | 
|---|
| 52 |  | 
|---|
| 53 | thread Girl { | 
|---|
| 54 | DatingService & TheExchange; | 
|---|
| 55 | unsigned int id, ccode; | 
|---|
| 56 | }; // Girl | 
|---|
| 57 |  | 
|---|
| 58 | void main( Girl & g ) { | 
|---|
| 59 | yield( random( 100 ) );                                                         // don't all start at the same time | 
|---|
| 60 | unsigned int partner = girl( g.TheExchange, g.id, g.ccode ); | 
|---|
| 61 | //sout | "Girl:" | g.id | "is dating Boy at" | partner | "with ccode" | g.ccode | endl; | 
|---|
| 62 | girlck[g.id] = partner; | 
|---|
| 63 | } // Girl main | 
|---|
| 64 |  | 
|---|
| 65 | void ?{}( Girl & g, DatingService * TheExchange, unsigned int id, unsigned int ccode ) { | 
|---|
| 66 | &g.TheExchange = TheExchange; | 
|---|
| 67 | g.id = id; | 
|---|
| 68 | g.ccode = ccode; | 
|---|
| 69 | } // Girl ?{} | 
|---|
| 70 |  | 
|---|
| 71 | thread Boy { | 
|---|
| 72 | DatingService &TheExchange; | 
|---|
| 73 | unsigned int id, ccode; | 
|---|
| 74 | }; // Boy | 
|---|
| 75 |  | 
|---|
| 76 | void main( Boy & b ) { | 
|---|
| 77 | yield( random( 100 ) );                                                         // don't all start at the same time | 
|---|
| 78 | unsigned int partner = boy( b.TheExchange, b.id, b.ccode ); | 
|---|
| 79 | //sout | " Boy:" | b.id | "is dating Girl" | partner | "with ccode" | b.ccode | endl; | 
|---|
| 80 | boyck[b.id] = partner; | 
|---|
| 81 | } // Boy main | 
|---|
| 82 |  | 
|---|
| 83 | void ?{}( Boy & b, DatingService * TheExchange, unsigned int id, unsigned int ccode ) { | 
|---|
| 84 | &b.TheExchange = TheExchange; | 
|---|
| 85 | b.id = id; | 
|---|
| 86 | b.ccode = ccode; | 
|---|
| 87 | } // Boy ?{} | 
|---|
| 88 |  | 
|---|
| 89 | int main() { | 
|---|
| 90 | DatingService TheExchange; | 
|---|
| 91 | Girl *girls[NoOfPairs]; | 
|---|
| 92 | Boy  *boys[NoOfPairs]; | 
|---|
| 93 |  | 
|---|
| 94 | random_seed( getpid() ); | 
|---|
| 95 |  | 
|---|
| 96 | for ( unsigned int i = 0; i < NoOfPairs; i += 1 ) { | 
|---|
| 97 | girls[i] = new( &TheExchange, i, i ); | 
|---|
| 98 | boys[i]  = new( &TheExchange, i, NoOfPairs - ( i + 1 ) ); | 
|---|
| 99 | } // for | 
|---|
| 100 |  | 
|---|
| 101 | for ( unsigned int i = 0; i < NoOfPairs; i += 1 ) { | 
|---|
| 102 | delete( boys[i] ); | 
|---|
| 103 | delete( girls[i] ); | 
|---|
| 104 | } // for | 
|---|
| 105 |  | 
|---|
| 106 | for ( unsigned int i = 0; i < NoOfPairs; i += 1 ) { | 
|---|
| 107 | if ( girlck[ boyck[i] ] != boyck[ girlck[i] ] ) abort(); | 
|---|
| 108 | } // for | 
|---|
| 109 | } // main | 
|---|
| 110 |  | 
|---|
| 111 | // Local Variables: // | 
|---|
| 112 | // tab-width: 4 // | 
|---|
| 113 | // compile-command: "cfa datingService.c" // | 
|---|
| 114 | // End: // | 
|---|