| 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 |
|
|---|
| 12 | int block;
|
|---|
| 13 | int busy = 0; // boolean flag indicating whether the block has been allocated to an inode
|
|---|
| 14 | int inode = 0;
|
|---|
| 15 | pthread_mutex_t m_inode; // protects the inode
|
|---|
| 16 | pthread_mutex_t m_busy; // protects the busy flag
|
|---|
| 17 |
|
|---|
| 18 | void *allocator(void * arg){
|
|---|
| 19 | pthread_mutex_lock(&m_inode);
|
|---|
| 20 | if(inode == 0){
|
|---|
| 21 | pthread_mutex_lock(&m_busy);
|
|---|
| 22 | busy = 1;
|
|---|
| 23 | pthread_mutex_unlock(&m_busy);
|
|---|
| 24 | inode = 1;
|
|---|
| 25 | }
|
|---|
| 26 | block = 1;
|
|---|
| 27 | assert(block == 1);
|
|---|
| 28 | pthread_mutex_unlock(&m_inode);
|
|---|
| 29 | return NULL;
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | void *de_allocator(void * arg){
|
|---|
| 33 | pthread_mutex_lock(&m_busy);
|
|---|
| 34 | if(busy == 0){
|
|---|
| 35 | block = 0;
|
|---|
| 36 | assert(block == 0);
|
|---|
| 37 | }
|
|---|
| 38 | pthread_mutex_unlock(&m_busy);
|
|---|
| 39 | return ((void *)0);
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | int main() {
|
|---|
| 43 | pthread_t t1, t2;
|
|---|
| 44 | __VERIFIER_assume(inode == busy);
|
|---|
| 45 | pthread_mutex_init(&m_inode, 0);
|
|---|
| 46 | pthread_mutex_init(&m_busy, 0);
|
|---|
| 47 | pthread_create(&t1, 0, allocator, 0);
|
|---|
| 48 | pthread_create(&t2, 0, de_allocator, 0);
|
|---|
| 49 | pthread_join(t1, 0);
|
|---|
| 50 | pthread_join(t2, 0);
|
|---|
| 51 | pthread_mutex_destroy(&m_inode);
|
|---|
| 52 | pthread_mutex_destroy(&m_busy);
|
|---|
| 53 | return 0;
|
|---|
| 54 | }
|
|---|