source: tests/concurrency/examples/datingService.cfa @ 43ab5fb

Last change on this file since 43ab5fb was c26bea2a, checked in by Peter A. Buhr <pabuhr@…>, 18 months ago

first attempt at renaming directory tests/concurrent to tests/concurrency to harmonize with other concurrency directory names

  • Property mode set to 100644
File size: 3.4 KB
RevLine 
[4cedd9f]1//
[2e457d8]2// Cforall Version 1.0.0 Copyright (C) 2017 University of Waterloo
[75ca7f4]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
[66812dd]12// Last Modified On : Sun Sep 27 15:42:25 2020
13// Update Count     : 40
[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]22enum { CompCodes = 20 };                                                                // number of compatibility codes
[2ba0bc7]23
24monitor DatingService {
[1213f21]25        condition Girls[CompCodes], Boys[CompCodes];
[2ba0bc7]26        unsigned int GirlPhoneNo, BoyPhoneNo;
27}; // DatingService
28
[971d9f2]29unsigned 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
[75ca7f4]37        // sout | "Girl:" | PhoneNo | "is dating Boy at" | BoyPhoneNo | "with ccode" | ccode;
[971d9f2]38        return BoyPhoneNo;
[2ba0bc7]39} // DatingService girl
40
[971d9f2]41unsigned 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
[75ca7f4]49        // sout | " Boy:" | PhoneNo | "is dating Girl" | GirlPhoneNo | "with ccode" | ccode;
[971d9f2]50        return GirlPhoneNo;
[2ba0bc7]51} // DatingService boy
52
[1213f21]53unsigned int girlck[CompCodes];
54unsigned int boyck[CompCodes];
[2ba0bc7]55
56thread Girl {
57        DatingService & TheExchange;
58        unsigned int id, ccode;
59}; // Girl
60
[5e1adb5]61void 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
67void ?{}( 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
73thread Boy {
[200fcb3]74        DatingService & TheExchange;
[2ba0bc7]75        unsigned int id, ccode;
76}; // Boy
77
[5e1adb5]78void 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
84void ?{}( 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
90int 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
[66812dd]110
111        printf( "done\n" );                                                                     // non-empty .expect file
[2ba0bc7]112} // main
113
114// Local Variables: //
115// tab-width: 4 //
[f8cd310]116// compile-command: "cfa datingService.cfa" //
[2ba0bc7]117// End: //
Note: See TracBrowser for help on using the repository browser.