source: tests/concurrent/examples/datingService.cfa @ 66ba544

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 66ba544 was 3381ed7, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Added park/unpark primitives thread and removed BlockInternal?.
Converted monitors to use park unpark.
Intrusive Queue now mark next field when thread is inside queue.
Added several asserts to kernel and monitor.
Added a few tests for park and unpark.

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