source: CIVL/examples/concurrency/barrier.cvl

main
Last change on this file 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.1 KB
RevLine 
[72c01cc]1/* Basic shared-variable flag barrier. Run as follows:
2 * civl verify -inputB=4 barrier.cvl
3 */
[e6b02c8]4#include <civlc.cvh>
[72c01cc]5
[0baeebd]6$input int B = 4; // upper bound on number of threads
7$input int N; // number of threads
[3ff27cf]8$assume(1<=N && N<=B);
[bf5d35f]9int lock = 0; // 0=available
10int in_barrier[N]; // am I inside the barrier?
11int num_in_barrier = 0; // how many are inside barrier?
12int counter = 0; // shared variable used to test barrier
13
[5c5108c]14
[bf5d35f]15void init() {
[7b9eb4c]16 $for (int i: 0 .. N-1)
17 in_barrier[i] = 0;
[bf5d35f]18}
19
20void barrier(int tid) {
[5c5108c]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 }
[bf5d35f]28 }
29 $when (in_barrier[tid] == 0); // wait till I am released
30}
31
32void run(int tid) {
33 while ($true) {
[d980649]34 $assert(counter == 0);
[bf5d35f]35 barrier(tid);
36 counter++;
37 barrier(tid);
[d980649]38 $assert(counter == N);
[bf5d35f]39 barrier(tid);
40 counter--;
41 barrier(tid);
42 }
43}
44
45void main() {
[17f5ed1]46 init();
[7b9eb4c]47 $parfor(int i: 0 .. N-1)
48 run(i);
[bf5d35f]49}
Note: See TracBrowser for help on using the repository browser.