source: tests/concurrent/examples/datingService.cfa @ 6a9d4b4

ADTarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 6a9d4b4 was a109bb47, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Hopefully fixed the instability of the DatingService? Test

  • Property mode set to 100644
File size: 3.3 KB
RevLine 
[4cedd9f]1//
[2ba0bc7]2// The contents of this file are covered under the licence agreement in the
3// file "LICENCE" distributed with Cforall.
[4cedd9f]4//
5// datingService.c --
6//
[2ba0bc7]7// Author           : Peter A. Buhr
8// Created On       : Mon Oct 30 12:56:20 2017
9// Last Modified By : Peter A. Buhr
[200fcb3]10// Last Modified On : Tue Dec 11 21:55:34 2018
11// Update Count     : 28
[4cedd9f]12//
[2ba0bc7]13
[73abe95]14#include <stdlib.hfa>                                                                           // random
15#include <fstream.hfa>
16#include <kernel.hfa>
17#include <thread.hfa>
[2ba0bc7]18#include <unistd.h>                                                                             // getpid
19
[1213f21]20enum { CompCodes = 20 };                                                                // number of compatibility codes
[2ba0bc7]21
22monitor DatingService {
[1213f21]23        condition Girls[CompCodes], Boys[CompCodes];
[2ba0bc7]24        unsigned int GirlPhoneNo, BoyPhoneNo;
25}; // DatingService
26
[971d9f2]27unsigned int girl( DatingService & mutex ds, unsigned int PhoneNo, unsigned int ccode ) with( ds ) {
[5e1adb5]28        if ( is_empty( Boys[ccode] ) ) {                                        // no compatible boy ?
29                wait( Girls[ccode] );                                                   // wait for boy
30                GirlPhoneNo = PhoneNo;                                                  // make phone number available
[2ba0bc7]31        } else {
[5e1adb5]32                GirlPhoneNo = PhoneNo;                                                  // make phone number available
33                signal_block( Boys[ccode] );                                    // restart boy to set phone number
[2ba0bc7]34        } // if
[a109bb47]35        sout | "Girl:" | PhoneNo | "is dating Boy at" | BoyPhoneNo | "with ccode" | ccode;
[971d9f2]36        return BoyPhoneNo;
[2ba0bc7]37} // DatingService girl
38
[971d9f2]39unsigned int boy( DatingService & mutex ds, unsigned int PhoneNo, unsigned int ccode ) with( ds ) {
[5e1adb5]40        if ( is_empty( Girls[ccode] ) ) {                                       // no compatible girl ?
41                wait( Boys[ccode] );                                                    // wait for girl
42                BoyPhoneNo = PhoneNo;                                                   // make phone number available
[2ba0bc7]43        } else {
[5e1adb5]44                BoyPhoneNo = PhoneNo;                                                   // make phone number available
45                signal_block( Girls[ccode] );                                   // restart girl to set phone number
[2ba0bc7]46        } // if
[a109bb47]47        sout | " Boy:" | PhoneNo | "is dating Girl" | GirlPhoneNo | "with ccode" | ccode;
[971d9f2]48        return GirlPhoneNo;
[2ba0bc7]49} // DatingService boy
50
[1213f21]51unsigned int girlck[CompCodes];
52unsigned int boyck[CompCodes];
[2ba0bc7]53
54thread Girl {
55        DatingService & TheExchange;
56        unsigned int id, ccode;
57}; // Girl
58
[5e1adb5]59void main( Girl & g ) with( g ) {
[6c7b1e7]60        yield( random( 100 ) );                                                         // don't all start at the same time
[5e1adb5]61        unsigned int partner = girl( TheExchange, id, ccode );
62        girlck[id] = partner;
[2ba0bc7]63} // Girl main
64
65void ?{}( 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
71thread Boy {
[200fcb3]72        DatingService & TheExchange;
[2ba0bc7]73        unsigned int id, ccode;
74}; // Boy
75
[5e1adb5]76void main( Boy & b ) with( b ) {
[6c7b1e7]77        yield( random( 100 ) );                                                         // don't all start at the same time
[5e1adb5]78        unsigned int partner = boy( TheExchange, id, ccode );
79        boyck[id] = partner;
[2ba0bc7]80} // Boy main
81
82void ?{}( 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
88int main() {
89        DatingService TheExchange;
[1213f21]90        Girl * girls[CompCodes];
91        Boy  * boys[CompCodes];
[2ba0bc7]92
[54aba8d]93        srandom( /*getpid()*/ 103 );
[2ba0bc7]94
[1213f21]95        for ( unsigned int i = 0; i < CompCodes; i += 1 ) {
[2ba0bc7]96                girls[i] = new( &TheExchange, i, i );
[1213f21]97                boys[i]  = new( &TheExchange, i, CompCodes - ( i + 1 ) );
[2ba0bc7]98        } // for
99
[1213f21]100        for ( unsigned int i = 0; i < CompCodes; i += 1 ) {
[2ba0bc7]101                delete( boys[i] );
102                delete( girls[i] );
103        } // for
104
[1213f21]105        for ( unsigned int i = 0; i < CompCodes; i += 1 ) {
[2ba0bc7]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: //
Note: See TracBrowser for help on using the repository browser.