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