source: src/tests/datingService.c@ 4f748c5

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 4f748c5 was 6c7b1e7, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

change random name

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