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