-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreadtest.cc
832 lines (729 loc) · 26.5 KB
/
threadtest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
// threadtest.cc
// Simple test case for the threads assignment.
//
// Create two threads, and have them context switch
// back and forth between themselves by calling Thread::Yield,
// to illustratethe inner workings of the thread system.
//
// Copyright (c) 1992-1993 The Regents of the University of California.
// All rights reserved. See copyright.h for copyright notice and limitation
// of liability and disclaimer of warranty provisions.
#include <iostream>
#include <sstream>
#include <string>
#include <stdlib.h>
#include "copyright.h"
#include "system.h"
#ifdef CHANGED
#include "synch.h"
#endif
using namespace std;
void ApplicationClerk(int myLine);
void createClerkThreads(Thread* t);
void clerkFactory();
void clerkSignalsNextCustomer(int myLine);
int chooseCustomerFromLine(int myLine);
void Part2();
void PassportClerk(int myLine);
void PictureClerk(int myLine);
void Cashier(int myLine);
void Customer(int custNumber);
void Senator(int custNumber);
void wakeUpClerks();
void printMoney();
void Manager();
class CustomerAttribute {
public:
CustomerAttribute() {}
CustomerAttribute(int ssn) {
SSN = ssn;
applicationIsFiled = false;
likesPicture = false;
hasCertification = false;
isDone = false;
int moneyArray[4] = {100, 600, 1100, 1600};
int randomIndex = rand() % 4; //0 to 3
money = moneyArray[randomIndex];
}
~CustomerAttribute() {}
int SSN;
bool likesPicture;
bool applicationIsFiled;
bool hasCertification;
bool isDone;
int money;
int currentLine;
};
//----------------------------------------------------------------------
// SimpleThread
// Loop 5 times, yielding the CPU to another ready thread
// each iteration.
//
// "which" is simply a number identifying the thread, for debugging
// purposes.
//----------------------------------------------------------------------
void
SimpleThread(int which)
{
int num;
for (num = 0; num < 5; num++) {
printf("*** thread %d looped %d times\n", which, num);
currentThread->Yield();
}
}
//----------------------------------------------------------OUR STUFF-------------------------------------
#ifdef CHANGED
// --------------------------------------------------
// Test Suite
// --------------------------------------------------
// --------------------------------------------------
// Test 1 - see TestSuite() for details
// --------------------------------------------------
Semaphore t1_s1("t1_s1",0); // To make sure t1_t1 acquires the
// lock before t1_t2
Semaphore t1_s2("t1_s2",0); // To make sure t1_t2 Is waiting on the
// lock before t1_t3 releases it
Semaphore t1_s3("t1_s3",0); // To make sure t1_t1 does not release the
// lock before t1_t3 tries to acquire it
Semaphore t1_done("t1_done",0); // So that TestSuite knows when Test 1 is
// done
Lock t1_l1("t1_l1"); // the lock tested in Test 1
// --------------------------------------------------
// t1_t1() -- test1 thread 1
// This is the rightful lock owner
// --------------------------------------------------
void t1_t1() {
t1_l1.Acquire();
t1_s1.V(); // Allow t1_t2 to try to Acquire Lock
printf ("%s: Acquired Lock %s, waiting for t3\n",currentThread->getName(),
t1_l1.getName());
t1_s3.P();
printf ("%s: working in CS\n",currentThread->getName());
for (int i = 0; i < 1000000; i++) ;
printf ("%s: Releasing Lock %s\n",currentThread->getName(),
t1_l1.getName());
t1_l1.Release();
t1_done.V();
}
// --------------------------------------------------
// t1_t2() -- test1 thread 2
// This thread will wait on the held lock.
// --------------------------------------------------
void t1_t2() {
t1_s1.P(); // Wait until t1 has the lock
t1_s2.V(); // Let t3 try to acquire the lock
printf("%s: trying to acquire lock %s\n",currentThread->getName(),
t1_l1.getName());
t1_l1.Acquire();
printf ("%s: Acquired Lock %s, working in CS\n",currentThread->getName(),
t1_l1.getName());
for (int i = 0; i < 10; i++)
;
printf ("%s: Releasing Lock %s\n",currentThread->getName(),
t1_l1.getName());
t1_l1.Release();
t1_done.V();
}
// --------------------------------------------------
// t1_t3() -- test1 thread 3
// This thread will try to release the lock illegally
// --------------------------------------------------
void t1_t3() {
t1_s2.P(); // Wait until t2 is ready to try to acquire the lock
t1_s3.V(); // Let t1 do it's stuff
for ( int i = 0; i < 3; i++ ) {
printf("%s: Trying to release Lock %s\n",currentThread->getName(),
t1_l1.getName());
t1_l1.Release();
}
}
// --------------------------------------------------
// Test 2 - see TestSuite() for details
// --------------------------------------------------
Lock t2_l1("t2_l1"); // For mutual exclusion
Condition t2_c1("t2_c1"); // The condition variable to test
Semaphore t2_s1("t2_s1",0); // To ensure the Signal comes before the wait
Semaphore t2_done("t2_done",0); // So that TestSuite knows when Test 2 is
// done
// --------------------------------------------------
// t2_t1() -- test 2 thread 1
// This thread will signal a variable with nothing waiting
// --------------------------------------------------
void t2_t1() {
t2_l1.Acquire();
printf("%s: Lock %s acquired, signalling %s\n",currentThread->getName(),
t2_l1.getName(), t2_c1.getName());
t2_c1.Signal(&t2_l1);
printf("%s: Releasing Lock %s\n",currentThread->getName(),
t2_l1.getName());
t2_l1.Release();
t2_s1.V(); // release t2_t2
t2_done.V();
}
// --------------------------------------------------
// t2_t2() -- test 2 thread 2
// This thread will wait on a pre-signalled variable
// --------------------------------------------------
void t2_t2() {
t2_s1.P(); // Wait for t2_t1 to be done with the lock
t2_l1.Acquire();
printf("%s: Lock %s acquired, waiting on %s\n",currentThread->getName(),
t2_l1.getName(), t2_c1.getName());
t2_c1.Wait(&t2_l1);
printf("%s: Releasing Lock %s\n",currentThread->getName(),
t2_l1.getName());
t2_l1.Release();
}
// --------------------------------------------------
// Test 3 - see TestSuite() for details
// --------------------------------------------------
Lock t3_l1("t3_l1"); // For mutual exclusion
Condition t3_c1("t3_c1"); // The condition variable to test
Semaphore t3_s1("t3_s1",0); // To ensure the Signal comes before the wait
Semaphore t3_done("t3_done",0); // So that TestSuite knows when Test 3 is
// done
// --------------------------------------------------
// t3_waiter()
// These threads will wait on the t3_c1 condition variable. Only
// one t3_waiter will be released
// --------------------------------------------------
void t3_waiter() {
t3_l1.Acquire();
t3_s1.V(); // Let the signaller know we're ready to wait
printf("%s: Lock %s acquired, waiting on %s\n",currentThread->getName(),
t3_l1.getName(), t3_c1.getName());
t3_c1.Wait(&t3_l1);
printf("%s: freed from %s\n",currentThread->getName(), t3_c1.getName());
t3_l1.Release();
t3_done.V();
}
// --------------------------------------------------
// t3_signaller()
// This threads will signal the t3_c1 condition variable. Only
// one t3_signaller will be released
// --------------------------------------------------
void t3_signaller() {
// Don't signal until someone's waiting
for ( int i = 0; i < 5 ; i++ )
t3_s1.P();
t3_l1.Acquire();
printf("%s: Lock %s acquired, signalling %s\n",currentThread->getName(),
t3_l1.getName(), t3_c1.getName());
t3_c1.Signal(&t3_l1);
printf("%s: Releasing %s\n",currentThread->getName(), t3_l1.getName());
t3_l1.Release();
t3_done.V();
}
// --------------------------------------------------
// Test 4 - see TestSuite() for details
// --------------------------------------------------
Lock t4_l1("t4_l1"); // For mutual exclusion
Condition t4_c1("t4_c1"); // The condition variable to test
Semaphore t4_s1("t4_s1",0); // To ensure the Signal comes before the wait
Semaphore t4_done("t4_done",0); // So that TestSuite knows when Test 4 is
// done
// --------------------------------------------------
// t4_waiter()
// These threads will wait on the t4_c1 condition variable. All
// t4_waiters will be released
// --------------------------------------------------
void t4_waiter() {
t4_l1.Acquire();
t4_s1.V(); // Let the signaller know we're ready to wait
printf("%s: Lock %s acquired, waiting on %s\n",currentThread->getName(),
t4_l1.getName(), t4_c1.getName());
t4_c1.Wait(&t4_l1);
printf("%s: freed from %s\n",currentThread->getName(), t4_c1.getName());
t4_l1.Release();
t4_done.V();
}
// --------------------------------------------------
// t2_signaller()
// This thread will broadcast to the t4_c1 condition variable.
// All t4_waiters will be released
// --------------------------------------------------
void t4_signaller() {
// Don't broadcast until someone's waiting
for ( int i = 0; i < 5 ; i++ )
t4_s1.P();
t4_l1.Acquire();
printf("%s: Lock %s acquired, broadcasting %s\n",currentThread->getName(),
t4_l1.getName(), t4_c1.getName());
t4_c1.Broadcast(&t4_l1);
printf("%s: Releasing %s\n",currentThread->getName(), t4_l1.getName());
t4_l1.Release();
t4_done.V();
}
// --------------------------------------------------
// Test 5 - see TestSuite() for details
// --------------------------------------------------
Lock t5_l1("t5_l1"); // For mutual exclusion
Lock t5_l2("t5_l2"); // Second lock for the bad behavior
Condition t5_c1("t5_c1"); // The condition variable to test
Semaphore t5_s1("t5_s1",0); // To make sure t5_t2 acquires the lock after
// t5_t1
// --------------------------------------------------
// t5_t1() -- test 5 thread 1
// This thread will wait on a condition under t5_l1
// --------------------------------------------------
void t5_t1() {
t5_l1.Acquire();
t5_s1.V(); // release t5_t2
printf("%s: Lock %s acquired, waiting on %s\n",currentThread->getName(),
t5_l1.getName(), t5_c1.getName());
t5_c1.Wait(&t5_l1);
printf("%s: Releasing Lock %s\n",currentThread->getName(),
t5_l1.getName());
t5_l1.Release();
}
// --------------------------------------------------
// t5_t1() -- test 5 thread 1
// This thread will wait on a t5_c1 condition under t5_l2, which is
// a Fatal error
// --------------------------------------------------
void t5_t2() {
t5_s1.P(); // Wait for t5_t1 to get into the monitor
t5_l1.Acquire();
t5_l2.Acquire();
printf("%s: Lock %s acquired, signalling %s\n",currentThread->getName(),
t5_l2.getName(), t5_c1.getName());
t5_c1.Signal(&t5_l2);
printf("%s: Releasing Lock %s\n",currentThread->getName(),
t5_l2.getName());
t5_l2.Release();
printf("%s: Releasing Lock %s\n",currentThread->getName(),
t5_l1.getName());
t5_l1.Release();
}
// --------------------------------------------------
// TestSuite()
// This is the main thread of the test suite. It runs the
// following tests:
//
// 1. Show that a thread trying to release a lock it does not
// hold does not work
//
// 2. Show that Signals are not stored -- a Signal with no
// thread waiting is ignored
//
// 3. Show that Signal only wakes 1 thread
//
// 4. Show that Broadcast wakes all waiting threads
//
// 5. Show that Signalling a thread waiting under one lock
// while holding another is a Fatal error
//
// Fatal errors terminate the thread in question.
// --------------------------------------------------
void TestSuite() {
Thread *t;
char *name;
int i;
// Test 1
printf("Starting Test 1\n");
t = new Thread("t1_t1");
t->Fork((VoidFunctionPtr)t1_t1,0);
t = new Thread("t1_t2");
t->Fork((VoidFunctionPtr)t1_t2,0);
t = new Thread("t1_t3");
t->Fork((VoidFunctionPtr)t1_t3,0);
// Wait for Test 1 to complete
for ( i = 0; i < 2; i++ )
t1_done.P();
// Test 2
printf("Starting Test 2. Note that it is an error if thread t2_t2\n");
printf("completes\n");
t = new Thread("t2_t1");
t->Fork((VoidFunctionPtr)t2_t1,0);
t = new Thread("t2_t2");
t->Fork((VoidFunctionPtr)t2_t2,0);
// Wait for Test 2 to complete
t2_done.P();
// Test 3
printf("Starting Test 3\n");
for ( i = 0 ; i < 5 ; i++ ) {
name = new char [20];
sprintf(name,"t3_waiter%d",i);
t = new Thread(name);
t->Fork((VoidFunctionPtr)t3_waiter,0);
}
t = new Thread("t3_signaller");
t->Fork((VoidFunctionPtr)t3_signaller,0);
// Wait for Test 3 to complete
for ( i = 0; i < 2; i++ ) {
printf("***************BEFORE****************\n");
if(t3_l1.getLockOwner() != NULL) {
printf("This is the current lockOwner: %s\n", t3_l1.getLockOwner()->getName());
} else {printf("The lock is NULL\n");}
t3_done.P();
scheduler->Print();
printf("^^^^^^^^^^^^^^");
t3_l1.Print();
printf("***************AFTER*****************\n");
}
// Test 4
printf("Starting Test 4\n");
for ( i = 0 ; i < 5 ; i++ ) {
name = new char [20];
sprintf(name,"t4_waiter%d",i);
t = new Thread(name);
t->Fork((VoidFunctionPtr)t4_waiter,0);
}
t = new Thread("t4_signaller");
t->Fork((VoidFunctionPtr)t4_signaller,0);
// Wait for Test 4 to complete
for ( i = 0; i < 6; i++ )
t4_done.P();
// Test 5
printf("Starting Test 5. Note that it is an error if thread t5_t1\n");
printf("completes\n");
t = new Thread("t5_t1");
t->Fork((VoidFunctionPtr)t5_t1,0);
t = new Thread("t5_t2");
t->Fork((VoidFunctionPtr)t5_t2,0);
}
#endif
//----------------------------------------------------------------------
// ThreadTest
// Set up a ping-pong between two threads, by forking a thread
// to call SimpleThread, and then calling SimpleThread ourselves.
//----------------------------------------------------------------------
//HUNG: Some constants, since we are writing our own test cases,
//the number of clerks and customers is totally up to us
const int CLERK_NUMBER = 5;
const int CUSTOMER_NUMBER = 50;
const int CLERK_TYPES = 4;
Lock* clerkLineLock = new Lock("ClerkLineLock");
int clerkLineCount[CLERK_NUMBER];
int clerkBribeLineCount[CLERK_NUMBER];
enum ClerkState {AVAILABLE, BUSY, ONBREAK};
ClerkState clerkStates[CLERK_NUMBER];
Condition* clerkLineCV[CLERK_NUMBER];
Condition* clerkBribeLineCV[CLERK_NUMBER];
CustomerAttribute customerAttributes[CUSTOMER_NUMBER];
int clerkMoney[CLERK_NUMBER] = {0};
//HUNG: senator control variables
int senatorCount = 0;
Condition* senatorLineCV;
//Second monitor
Lock* clerkLock[CLERK_NUMBER];
Condition* clerkCV[CLERK_NUMBER];
int customerData[CLERK_NUMBER]; //HUNG: Every clerk will use this to get the customer's customerAttribute index
string clerkTypes[CLERK_TYPES] = { "ApplicationClerks : ", "PictureClerks : ", "PassportClerks : ", "Cashiers : " };
int clerkArray[CLERK_TYPES];
char* cStringDeepCopy(string str) {
char *cstr = new char[str.length() + 1];
strcpy(cstr, str.c_str());
// do stuff
//delete [] cstr;
return cstr;
}
void createClerkThreads(Thread* t) {
int clerkNumber = 0;
for(int i = 0; i < 4; ++i) {
if(i == 0) {
for(int j = 0; j < clerkArray[i]; ++j) {
stringstream sstm;
sstm << "ApplicationClerk_" << clerkNumber;
t = new Thread(cStringDeepCopy(sstm.str()));
t->Fork((VoidFunctionPtr)ApplicationClerk,clerkNumber);
++clerkNumber;
}
} else if(i == 1) {
for(int j = 0; j < clerkArray[i]; ++j) {
stringstream sstm;
sstm << "PictureClerk_" << clerkNumber;
t = new Thread(cStringDeepCopy(sstm.str()));
t->Fork((VoidFunctionPtr)PictureClerk,clerkNumber);
++clerkNumber;
}
} else if(i == 2) {
for(int j = 0; j < clerkArray[i]; ++j) {
stringstream sstm;
sstm << "PassportClerk_" << clerkNumber;
t = new Thread(cStringDeepCopy(sstm.str()));
t->Fork((VoidFunctionPtr)PassportClerk,clerkNumber);
++clerkNumber;
}
} else { // i == 3
for(int j = 0; j < clerkArray[i]; ++j) {
stringstream sstm;
sstm << "Cashier_" << clerkNumber;
t = new Thread(cStringDeepCopy(sstm.str()));
t->Fork((VoidFunctionPtr)Cashier,clerkNumber);
++clerkNumber;
}
}
clerkArray[i];
}
}
//Monitor variables
//First monitor
void clerkFactory() {
int clerkCount = 0;
int array[4] = {1,1,1,2};
for(int i = 0; i < CLERK_TYPES; ++i) {
cout << clerkTypes[i] << array[i] << endl;
//cin >> clerkCount;
//clerkArray[i] = clerkCount;
clerkArray[i] = array[i];
}
}
void createClerkLocksAndConditions() {
for(int i = 0; i < CLERK_NUMBER; ++i) {
stringstream sstm;
sstm << "ClerkLock_" << i;
clerkLock[i] = new Lock(cStringDeepCopy(sstm.str()));
stringstream sstm2;
sstm2 << "ClerkCV_" << i;
clerkCV[i] = new Condition(cStringDeepCopy(sstm2.str()));
stringstream sstm3;
sstm3 << "ClerkLineCV_" << i;
clerkLineCV[i] = new Condition(cStringDeepCopy(sstm3.str()));
stringstream sstm4;
sstm4 << "ClerkBribeLineCV_" << i;
clerkBribeLineCV[i] = new Condition(cStringDeepCopy(sstm4.str()));
}
}
void createCustomerThreads(Thread *t) {
for(int i = 0 ; i < 10 ; i++){
stringstream sstm;
sstm << "Customer_" << i;
t = new Thread(cStringDeepCopy(sstm.str()));
t->Fork((VoidFunctionPtr)Customer,i);
}
}
void Part2() {
Thread *t;
char *name;
printf("Starting Part 2\n");
createClerkLocksAndConditions();
clerkFactory();
createClerkThreads(t);
createCustomerThreads(t);
//t->Fork((VoidFunctionPtr)Manager,i); // TODO: start Manager thread
//t->Fork((VoidFunctionPtr)Senator,i); // TODO: start Manager thread
printf("Starting Test 1\n");
}
int chooseCustomerFromLine(int myLine) {
clerkLineLock->Acquire();
//if(senatorCount > 0) {//HUNG: senator stuff
//} else
if(clerkBribeLineCount[myLine] > 0) {
cout << currentThread->getName() << ": I'm taking from bribe line" << endl;
clerkBribeLineCV[myLine]->Signal(clerkLineLock);
clerkStates[myLine] = BUSY; //redundant setting
} else if(clerkLineCount[myLine] > 0) {
cout << currentThread->getName() << ": I'm taking from reg line" << endl;
clerkLineCV[myLine]->Signal(clerkLineLock);
clerkStates[myLine] = BUSY; //redundant setting
} else {
//TODO: ONBREAK CODE
cout << currentThread->getName() << ": I'm going on break" << endl;
clerkStates[myLine] = AVAILABLE;
}
clerkLock[myLine]->Acquire();
clerkLineLock->Release();
//wait for customer
clerkCV[myLine]->Wait(clerkLock[myLine]);
//Do my job -> customer waiting
return customerData[myLine]; //HUNG: Customer would have set the customerData[myLine] to be their custNumber
}
void clerkSignalsNextCustomer(int myLine) {
clerkCV[myLine]->Signal(clerkLock[myLine]);
clerkCV[myLine]->Wait(clerkLock[myLine]);
clerkLock[myLine]->Release();
}
void ApplicationClerk(int myLine) {
while(true) {
int custNumber = chooseCustomerFromLine(myLine);
clerkStates[myLine] = BUSY;
int numYields = rand() % 80 + 20;
for(int i = 0; i < numYields; ++i) {
currentThread->Yield();
}
customerAttributes[custNumber].applicationIsFiled = true;
clerkStates[myLine] = AVAILABLE; //TODO: the manager should do this
clerkSignalsNextCustomer(myLine);
}
}
void PictureClerk(int myLine) {
while(true) {
int custNumber = chooseCustomerFromLine(myLine);
clerkStates[myLine] = BUSY;
int numYields = rand() % 80 + 20;
while(!customerAttributes[custNumber].likesPicture) {
for(int i = 0; i < numYields; ++i) {
currentThread->Yield();
}
int probability = rand() % 100;
if(probability >= 25) {
customerAttributes[custNumber].likesPicture = true;
}
}
clerkStates[myLine] = AVAILABLE;
clerkSignalsNextCustomer(myLine);
}
}
void PassportClerk(int myLine) {
while(true) {
int custNumber = chooseCustomerFromLine(myLine);
if(customerAttributes[custNumber].likesPicture && customerAttributes[custNumber].applicationIsFiled) {
clerkStates[myLine] = BUSY;
int numYields = rand() % 80 + 20;
for(int i = 0; i < numYields; ++i) {
currentThread->Yield();
}
clerkStates[myLine] = AVAILABLE;
customerAttributes[custNumber].hasCertification = true;
} else { //customer gets sent to back of line
int waitTime = rand() % 900 + 100;
for(int i = 0; i < waitTime; ++i) {
currentThread->Yield();
}
//TODO send to back of PassportLine
}
clerkSignalsNextCustomer(myLine);
}
}
void Cashier(int myLine) {
while(true) {
int custNumber = chooseCustomerFromLine(myLine);
if(customerAttributes[custNumber].hasCertification) {
customerAttributes[custNumber].money - 100;
clerkMoney[myLine] += 100;
clerkStates[myLine] = BUSY;
int numYields = rand() % 80 + 20;
for(int i = 0; i < numYields; ++i) {
currentThread->Yield();
}
clerkStates[myLine] = AVAILABLE;
customerAttributes[custNumber].isDone = true;
} else { //customer gets sent to back of line
int waitTime = rand() % 900 + 100;
for(int i = 0; i < waitTime; ++i) {
currentThread->Yield();
}
//TODO send to back of CashierLine
}
clerkSignalsNextCustomer(myLine);
}
}
void Customer(int custNumber) {
CustomerAttribute myCustAtt = CustomerAttribute(custNumber); //Hung: Creating a CustomerAttribute for each new customer
customerAttributes[custNumber] = myCustAtt;
while(!customerAttributes[custNumber].isDone) {
clerkLineLock->Acquire();
bool bribe = false; //HUNG: flag to know whether the customer has paid the bribe, can't be arsed to think of a more elegant way of doing this
int myLine = -1;
int lineSize = 1000;
cout << "I'm choosing a line" << endl;
for(int i = 0; i < CLERK_NUMBER; i++) {
if(clerkLineCount[i] < lineSize && clerkStates[i] != ONBREAK) {
myLine = i;
lineSize = clerkLineCount[i];
}
}
if(clerkStates[myLine] == BUSY) {
//I must wait in line
cout << "I'm waiting for the clerk" << endl;
//HUNG: adding code for line bribing TODO: check if all transactions are complete
if(customerAttributes[custNumber].money > 100){
cout << "Has money to bribe with" << endl;
customerAttributes[custNumber].money -=500;
clerkMoney[myLine]+=500;
clerkBribeLineCount[myLine]++;
bribe = true;
clerkBribeLineCV[myLine]->Wait(clerkLineLock);
} else {
cout << currentThread->getName() << ": I'm poor" << endl;
clerkLineCount[myLine]++;
clerkLineCV[myLine]->Wait(clerkLineLock);
}
if(bribe) {
cout << currentThread->getName() << ": I'm bribing" << endl;
clerkLineCount[myLine]--;
} else {
cout << currentThread->getName() << ": I'm not bribing" << endl;
clerkLineCount[myLine]--;
}
} else {
clerkStates[myLine] = BUSY;
}
clerkLineLock->Release();
clerkLock[myLine]->Acquire();
//Give my data to my clerk
customerData[myLine] = custNumber;
clerkCV[myLine]->Signal(clerkLock[myLine]);
//wait for clerk to do their job
clerkCV[myLine]->Wait(clerkLock[myLine]);
//Read my data
clerkCV[myLine]->Signal(clerkLock[myLine]);
clerkLock[myLine]->Release();
}
}
/*
void Senator(int custNumber){
clerkLineLock->Acquire();
CustomerAttribute myCustAtt = CustomerAttribute(custNumber); //Hung: Creating a CustomerAttribute for each new senator
customerAttributes[custNumber]=myCustAtt;
senatorCount++;
clerkLineLock->Release();
for(int i = 0; i < CLERK_NUMBER; i++) {
clerkLock[i]->Acquire();
}
//Give my data to my clerk
//TODO: Need to define appClerk and picClerk
customerData[appClerk] = custNumber;
customerData[picClerk] = custNumber;
//TODO: appClerk or picClerk first
clerkCV[appClerk]->Signal(clerkLock[appClerk]);
//wait for clerk to do their job
clerkCV[appClerk]->Wait(clerkLock[appClerk]);
//Read my data
clerkCV[appClerk]->Signal(clerkLock[appClerk]);
clerkCV[appClerk]->Release();
clerkCV[appClerk]->Signal(clerkLock[appClerk]);
//wait for clerk to do their job
clerkCV[appClerk]->Wait(clerkLock[appClerk]);
//Read my data
clerkCV[myLine]->Signal(clerkLock[appClerk]);
clerkLock[myLine]->Release();
}
*/
void wakeUpClerks() {
for(int i = 0; i < CLERK_NUMBER; ++i) {
if(clerkStates[i] == ONBREAK) {
clerkStates[i] = AVAILABLE;
}
}
}
void printMoney() {
int totalMoney = 0;
for(int i = 0; i < CLERK_NUMBER; ++i) {
totalMoney += clerkMoney[i];
cout << "Money earned by clerk" << i << ": " << clerkMoney[i] << endl;
}
cout << "Total money earned: " << totalMoney << endl << endl;
}
void Manager() {
int totalLineCount = 0;
for(int i = 0; i < CLERK_NUMBER; ++i) {
totalLineCount += clerkLineCount[i] + clerkBribeLineCount[i];
if(totalLineCount > 3) {
wakeUpClerks();
break;
}
}
printMoney();
}
void
ThreadTest()
{
/*DEBUG('t', "Entering SimpleTest");
Thread *t = new Thread("forked thread");
t->Fork(SimpleThread, 1);
SimpleThread(0);*/
//TestSuite();
Part2();
}