source: CIVL/examples/translation/pthread/time_var_mutex_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.3 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#include <civlc.h>
11#define assert(e) if (!(e)) $assert($false);
12
13int block;
14int busy=1; // boolean flag indicating whether the block has been allocated to an inode
15int inode=1;
16pthread_mutex_t m_inode; // protects the inode
17pthread_mutex_t m_busy; // protects the busy flag
18
19void *allocator(void *arg){
20 pthread_mutex_lock(&m_inode);
21 if(inode == 0){
22 pthread_mutex_lock(&m_busy);
23 busy = 1;
24 pthread_mutex_unlock(&m_busy);
25 inode = 1;
26 }
27 block = 1;
28 assert(block == 1);
29 pthread_mutex_unlock(&m_inode);
30 return NULL;
31}
32
33void *de_allocator(void *arg){
34 pthread_mutex_lock(&m_busy);
35 if(busy == 0){
36 block = 0;
37 assert(block == 0);
38 }
39 pthread_mutex_unlock(&m_busy);
40 return NULL;
41}
42
43int main(void) {
44 pthread_t t1, t2;
45 $when(inode == busy);
46 pthread_mutex_init(&m_inode, 0);
47 pthread_mutex_init(&m_busy, 0);
48 pthread_create(&t1, 0, allocator, 0);
49 pthread_create(&t2, 0, de_allocator, 0);
50 pthread_join(t1, 0);
51 pthread_join(t2, 0);
52 pthread_mutex_destroy(&m_inode);
53 pthread_mutex_destroy(&m_busy);
54 return 0;
55}
Note: See TracBrowser for help on using the repository browser.