source: CIVL/examples/pthread/threader/read_write_lock_true.c@ c9cfd85

1.23 2.0 main test-branch
Last change on this file since c9cfd85 was e3151da, checked in by Ziqing Luo <ziqing@…>, 11 years ago

re-organized example directory

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@1763 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 This file is adapted from the example introduced in the paper:
5 Thread-Modular Verification for Shared-Memory Programs
6 by Cormac Flanagan, Stephen Freund, Shaz Qadeer.
7*/
8
9#include <pthread.h>
10#define assert(e) if (!(e)) ERROR: goto ERROR;
11
12int w=0, r=0, x=0, y=0;
13
14void __VERIFIER_atomic_take_write_lock() {
15 __VERIFIER_assume(w==0 && r==0);
16 w = 1;
17}
18
19void __VERIFIER_atomic_take_read_lock() {
20 __VERIFIER_assume(w==0);
21 r = r+1;
22}
23
24void __VERIFIER_atomic_release_read_lock() {
25 r = r-1;
26}
27
28void *writer(void * arg) { //writer
29 __VERIFIER_atomic_take_write_lock();
30 x = 3;
31 w = 0;
32}
33
34void *reader(void * arg) { //reader
35 int l;
36 __VERIFIER_atomic_take_read_lock();
37 l = x;
38 y = l;
39 assert(y == x);
40 __VERIFIER_atomic_release_read_lock();
41}
42
43int main() {
44 pthread_t t1, t2, t3, t4;
45 pthread_create(&t1, 0, writer, 0);
46 pthread_create(&t2, 0, reader, 0);
47 pthread_create(&t3, 0, writer, 0);
48 pthread_create(&t4, 0, reader, 0);
49 pthread_join(t1, 0);
50 pthread_join(t2, 0);
51 pthread_join(t3, 0);
52 pthread_join(t4, 0);
53 return 0;
54}
Note: See TracBrowser for help on using the repository browser.