| 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 |
|
|---|
| 13 | int block;
|
|---|
| 14 | int busy=1; // boolean flag indicating whether the block has been allocated to an inode
|
|---|
| 15 | int inode=1;
|
|---|
| 16 | pthread_mutex_t m_inode; // protects the inode
|
|---|
| 17 | pthread_mutex_t m_busy; // protects the busy flag
|
|---|
| 18 |
|
|---|
| 19 | void *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 |
|
|---|
| 33 | void *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 |
|
|---|
| 43 | int 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 | }
|
|---|