source: CIVL/examples/concurrency/readerWriter.cvl@ 54ea077

1.23 2.0 main test-branch
Last change on this file since 54ea077 was d980649, checked in by Manchun Zheng <zmanchun@…>, 10 years ago

fixed extra parenthesis.

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

  • Property mode set to 100644
File size: 1.4 KB
Line 
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#include<civlc.cvh>
10
11$input int NREADER = 3;
12$input int NWRITER = 3;
13int writeLock = 0;
14int readLock = 0;
15int writeCount = 0;
16int readCount = 0;
17
18void reader(int rid){
19 while($true){
20 $when(writeLock == 0) readLock = 1;
21 readCount++;
22 // reading
23 $assert(writeCount == 0);
24 readCount--;
25 $atom{
26 if(readCount = 0)
27 readLock = 0;
28 }
29 }
30}
31
32void writer(int wid){
33 while($true){
34 $when(writeLock == 0 && readLock == 0) writeLock = 1;
35 $assert(readCount == 0 && writeCount == 0);
36 writeCount++;
37 // writing
38 $assert(readCount == 0 && writeCount == 1);
39 writeCount--;
40 writeLock = 0;
41 }
42}
43
44void main(){
45 $proc readers[NREADER];
46 $proc writers[NWRITER];
47
48 for(int i = 0; i < NWRITER; i++){
49 writers[i] = $spawn writer(i);
50 }
51 for(int i = 0; i < NREADER; i++){
52 readers[i] = $spawn reader(i);
53 }
54 $waitall(readers, NREADER);
55 $waitall(writers, NWRITER);
56}
Note: See TracBrowser for help on using the repository browser.