source: src/tests/concurrent/examples/datingService.c @ 0ad0c55

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 0ad0c55 was 948887f, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

organized concurrent test folder

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