| 1 | extern void __VERIFIER_error() __attribute__ ((__noreturn__));
|
|---|
| 2 |
|
|---|
| 3 | /* Testcase from Threader's distribution. For details see:
|
|---|
| 4 | http://www.model.in.tum.de/~popeea/research/threader
|
|---|
| 5 |
|
|---|
| 6 | This file is adapted from the example introduced in the paper:
|
|---|
| 7 | Thread-Modular Verification for Shared-Memory Programs
|
|---|
| 8 | by Cormac Flanagan, Stephen Freund, Shaz Qadeer.
|
|---|
| 9 | */
|
|---|
| 10 |
|
|---|
| 11 | #include <pthread.h>
|
|---|
| 12 | #define assert(e) if (!(e)) ERROR: __VERIFIER_error();
|
|---|
| 13 |
|
|---|
| 14 | int block;
|
|---|
| 15 | int busy; // boolean flag indicating whether the block has been allocated to an inode
|
|---|
| 16 | int inode;
|
|---|
| 17 | pthread_mutex_t m_inode; // protects the inode
|
|---|
| 18 | pthread_mutex_t m_busy; // protects the busy flag
|
|---|
| 19 |
|
|---|
| 20 | void *allocator(){
|
|---|
| 21 | pthread_mutex_lock(&m_inode);
|
|---|
| 22 | if(inode == 0){
|
|---|
| 23 | pthread_mutex_lock(&m_busy);
|
|---|
| 24 | busy = 1;
|
|---|
| 25 | pthread_mutex_unlock(&m_busy);
|
|---|
| 26 | inode = 1;
|
|---|
| 27 | }
|
|---|
| 28 | block = 1;
|
|---|
| 29 | assert(block == 1);
|
|---|
| 30 | pthread_mutex_unlock(&m_inode);
|
|---|
| 31 | return NULL;
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | void *de_allocator(){
|
|---|
| 35 | pthread_mutex_lock(&m_busy);
|
|---|
| 36 | if(busy == 0){
|
|---|
| 37 | block = 0;
|
|---|
| 38 | assert(block == 0);
|
|---|
| 39 | }
|
|---|
| 40 | pthread_mutex_unlock(&m_busy);
|
|---|
| 41 | return ((void *)0);
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | int main() {
|
|---|
| 45 | pthread_t t1, t2;
|
|---|
| 46 | __VERIFIER_assume(inode == busy);
|
|---|
| 47 | pthread_mutex_init(&m_inode, 0);
|
|---|
| 48 | pthread_mutex_init(&m_busy, 0);
|
|---|
| 49 | pthread_create(&t1, 0, allocator, 0);
|
|---|
| 50 | pthread_create(&t2, 0, de_allocator, 0);
|
|---|
| 51 | pthread_join(t1, 0);
|
|---|
| 52 | pthread_join(t2, 0);
|
|---|
| 53 | pthread_mutex_destroy(&m_inode);
|
|---|
| 54 | pthread_mutex_destroy(&m_busy);
|
|---|
| 55 | return 0;
|
|---|
| 56 | }
|
|---|