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

1.23 2.0 acw/focus-triggers main test-branch
Last change on this file since 4f22a92 was 72c01cc, checked in by Stephen Siegel <siegel@…>, 13 years ago

Continuing to clean up examples, but just realized the minimal
depth search is not quite right, it won't necessarily find
the minimum, as revealed by dining philosophers.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@268 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
15void init() {
16 for (int i=0; i<N; i++) in_barrier[i] = 0;
17}
18
19void barrier(int tid) {
20 $when (lock==0) lock = 1; // obtains the lock
21 in_barrier[tid] = 1; // I am in the barrier
22 num_in_barrier++; // increment number in barrier
23 if (num_in_barrier == N) { // I am last to enter
24 for (int i=0; i<N; i++) in_barrier[i] = 0; // release all
25 num_in_barrier = 0; // now none are in barrier
26 }
27 lock = 0; // release the lock
28 $when (in_barrier[tid] == 0); // wait till I am released
29}
30
31void run(int tid) {
32 while ($true) {
33 $assert counter == 0;
34 barrier(tid);
35 counter++;
36 barrier(tid);
37 $assert counter == N;
38 barrier(tid);
39 counter--;
40 barrier(tid);
41 }
42}
43
44void main() {
45 init();
46 for (int i=0; i<N; i++) threads[i] = $spawn run(i);
47 for (int i=0; i<N; i++) $wait threads[i];
48}
Note: See TracBrowser for help on using the repository browser.