source: tests/concurrent/examples/datingService.cfa @ e73d449

ADTarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since e73d449 was f8cd310, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

update file names in emacs compile directive

  • Property mode set to 100644
File size: 3.3 KB
RevLine 
[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
[f8cd310]12// Last Modified On : Fri Mar 22 13:41:39 2019
13// Update Count     : 31
[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
[a109bb47]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
[a109bb47]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
[1213f21]97        for ( unsigned int i = 0; i < CompCodes; i += 1 ) {
[2ba0bc7]98                girls[i] = new( &TheExchange, i, i );
[1213f21]99                boys[i]  = new( &TheExchange, i, CompCodes - ( i + 1 ) );
[2ba0bc7]100        } // for
101
[1213f21]102        for ( unsigned int i = 0; i < CompCodes; i += 1 ) {
[2ba0bc7]103                delete( boys[i] );
104                delete( girls[i] );
105        } // for
106
[1213f21]107        for ( unsigned int i = 0; i < CompCodes; i += 1 ) {
[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: //
Note: See TracBrowser for help on using the repository browser.