source: CIVL/examples/translation/pthread/dekker_true.cvl@ d87ec9c

1.23 2.0 acw/focus-triggers main test-branch
Last change on this file since d87ec9c was 8d512ae, checked in by John Edenhofner <johneden@…>, 12 years ago

Changed location of pthread.cvh, and updated files to use pthread.h (which includes pthread.cvh). New translations from pthread-atomic, and updated pthread.cvh

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

  • Property mode set to 100644
File size: 1.2 KB
Line 
1/* Testcase from Threader's distribution. For details see:
2 http://www.model.in.tum.de/~popeea/research/threader
3*/
4#include <civlc.h>
5#include "pthread.h"
6#define assert(e) if (!(e)) $assert($false);
7
8int flag1 = 0, flag2 = 0; // boolean flags
9$input int turn; // integer variable to hold the ID of the thread whose turn is it
10$assume 0<=turn && turn<=1;
11int turn1 = turn;
12int x; // boolean variable to test mutual exclusion
13void *thr1(void *arg) {
14 flag1 = 1;
15 while (flag2 >= 1) {
16 if (turn1 != 0) {
17 flag1 = 0;
18 while (turn1 != 0) {};
19 flag1 = 1;
20 }
21 }
22 // begin: critical section
23 x = 0;
24 assert(x<=0);
25 // end: critical section
26 turn1 = 1;
27 flag1 = 0;
28 return NULL; //Added this, should be here or pthread_exit should be called
29}
30
31void *thr2(void *arg) {
32 flag2 = 1;
33 while (flag1 >= 1) {
34 if (turn1 != 1) {
35 flag2 = 0;
36 while (turn1 != 1) {};
37 flag2 = 1;
38 }
39 }
40 // begin: critical section
41 x = 1;
42 assert(x>=1);
43 // end: critical section
44 turn1 = 1;
45 flag2 = 0;
46 return NULL; //Added this, should be here or pthread_exit should be called
47}
48
49int main(void) {
50 pthread_t t1, t2;
51 pthread_create(&t1, 0, thr1, 0);
52 pthread_create(&t2, 0, thr2, 0);
53 pthread_join(t1, 0);
54 pthread_join(t2, 0);
55 return 0;
56}
Note: See TracBrowser for help on using the repository browser.