source: CIVL/examples/concurrency/readerWriterBad.cvl@ bb03188

main test-branch
Last change on this file since bb03188 was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago

Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5704 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 1.5 KB
RevLine 
[eaf3273]1/**
2 * This program implements the reader-writer problem.
3 * The program deals with situations in which many threads must
4 * access the same shared memory at one time, some reading and some writing,
5 * with the natural constraint that no process may access the share for reading
6 * or writing while another process is in the act of writing to it.
7 * (In particular, it is allowed for two or more readers to access the share at the same time.)
8 */
9 // This is an 'bad' version with an incorrect protocol causing a datarace problem.
10#include<civlc.cvh>
11
12$input int NREADER = 3;
13$input int NWRITER = 3;
14int writeLock = 0;
15int readLock = 0;
16int writeCount = 0;
17int readCount = 0;
18
19void reader(int rid){
20 while($true){
21 $when(writeLock == 0) readLock = 1;
22 readCount++;
23 // reading
24 $assert(writeCount == 0);
25 readCount--;
[0c90cbf]26 $atomic{
[eaf3273]27 if(readCount == 0)
28 readLock = 0;
29 }
30 }
31}
32
33void writer(int wid){
34 while($true){
35 $when(writeLock == 0 && readLock == 0) writeLock = 1;
36 $assert(readCount == 0 && writeCount == 0);
37 writeCount++;
38 // writing
39 $assert(readCount == 0 && writeCount == 1);
40 writeCount--;
41 writeLock = 0;
42 }
43}
44
45void main(){
46 $proc readers[NREADER];
47 $proc writers[NWRITER];
48
49 for(int i = 0; i < NWRITER; i++){
50 writers[i] = $spawn writer(i);
51 }
52 for(int i = 0; i < NREADER; i++){
53 readers[i] = $spawn reader(i);
54 }
55 $waitall(readers, NREADER);
56 $waitall(writers, NWRITER);
57}
Note: See TracBrowser for help on using the repository browser.