source: CIVL/examples/concurrency/readerWriter.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 
[6eefc77]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){
[eaf3273]20 $when(writeLock == 0) {
21 $atomic {
22 readLock = 1;
23 readCount++;
24 }
25 }
[6eefc77]26 // reading
[d980649]27 $assert(writeCount == 0);
[6eefc77]28 readCount--;
[0c90cbf]29 $atomic{
[eaf3273]30 if(readCount == 0)
[6eefc77]31 readLock = 0;
32 }
33 }
34}
35
36void writer(int wid){
37 while($true){
38 $when(writeLock == 0 && readLock == 0) writeLock = 1;
[d980649]39 $assert(readCount == 0 && writeCount == 0);
[6eefc77]40 writeCount++;
41 // writing
[d980649]42 $assert(readCount == 0 && writeCount == 1);
[6eefc77]43 writeCount--;
44 writeLock = 0;
45 }
46}
47
48void main(){
49 $proc readers[NREADER];
50 $proc writers[NWRITER];
51
52 for(int i = 0; i < NWRITER; i++){
53 writers[i] = $spawn writer(i);
54 }
55 for(int i = 0; i < NREADER; i++){
56 readers[i] = $spawn reader(i);
57 }
58 $waitall(readers, NREADER);
59 $waitall(writers, NWRITER);
60}
Note: See TracBrowser for help on using the repository browser.