source: CIVL/examples/concurrency/barrier_atom.cvl@ 4f22a92

1.23 2.0 main test-branch
Last change on this file since 4f22a92 was 2fa36af, checked in by Manchun Zheng <zmanchun@…>, 12 years ago

make examples atomic

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@392 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.h>
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 $atomic {
47 init();
48 for (int i=0; i<N; i++) threads[i] = $spawn run(i);
49 }
50 $atomic {
51 for (int i=0; i<N; i++) $wait threads[i];
52 }
53}
Note: See TracBrowser for help on using the repository browser.