source: CIVL/examples/concurrency/barrier.cvl@ 8d298d8

1.23 2.0 main test-branch
Last change on this file since 8d298d8 was e6b02c8, checked in by Manchun Zheng <zmanchun@…>, 12 years ago

updates CIVL to use the new ABC FrontEnd. tests updated accordingly when necessary.

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

  • Property mode set to 100644
File size: 1.2 KB
Line 
1/* Basic shared-variable flag barrier. Run as follows:
2 * civl verify -inputB=4 barrier.cvl
3 */
4#include <civlc.cvh>
5
6$input int B; // upper bound on number of threads
7$input int N; // number of threads
8$assume 1<=N && N<=B;
9$proc threads[N]; // the threads
10int lock = 0; // 0=available
11int in_barrier[N]; // am I inside the barrier?
12int num_in_barrier = 0; // how many are inside barrier?
13int counter = 0; // shared variable used to test barrier
14
15
16void init() {
17 for (int i=0; i<N; i++) in_barrier[i] = 0;
18}
19
20void barrier(int tid) {
21 $atomic {
22 in_barrier[tid] = 1; // I am in the barrier
23 num_in_barrier++; // increment number in barrier
24 if (num_in_barrier == N) { // I am last to enter
25 for (int i=0; i<N; i++) in_barrier[i] = 0; // release all
26 num_in_barrier = 0; // now none are in barrier
27 }
28 }
29 $when (in_barrier[tid] == 0); // wait till I am released
30}
31
32void run(int tid) {
33 while ($true) {
34 $assert(counter == 0);
35 barrier(tid);
36 counter++;
37 barrier(tid);
38 $assert(counter == N);
39 barrier(tid);
40 counter--;
41 barrier(tid);
42 }
43}
44
45void main() {
46 init();
47 for (int i=0; i<N; i++) threads[i] = $spawn run(i);
48
49 for (int i=0; i<N; i++) $wait(threads[i]);
50}
Note: See TracBrowser for help on using the repository browser.