bart | 5bd9f2d | 2008-03-03 20:31:58 +0000 | [diff] [blame] | 1 | /** Cause a race inside code protected by a reader lock. |
| 2 | */ |
| 3 | |
| 4 | |
| 5 | /* Needed for older glibc's (2.3 and older, at least) who don't |
| 6 | otherwise "know" about pthread_rwlock_anything or about |
| 7 | PTHREAD_MUTEX_RECURSIVE (amongst things). */ |
| 8 | |
| 9 | #define _GNU_SOURCE 1 |
| 10 | |
bart | 5bd9f2d | 2008-03-03 20:31:58 +0000 | [diff] [blame] | 11 | #include <pthread.h> |
bart | 68a8a20 | 2008-06-28 11:53:01 +0000 | [diff] [blame] | 12 | #include <stdio.h> |
| 13 | #include <unistd.h> |
bart | 5f57be9 | 2008-07-01 08:48:56 +0000 | [diff] [blame] | 14 | |
bart | 5bd9f2d | 2008-03-03 20:31:58 +0000 | [diff] [blame] | 15 | |
| 16 | |
| 17 | static pthread_rwlock_t s_rwlock; |
| 18 | static int s_racy; |
| 19 | |
bart | 68a8a20 | 2008-06-28 11:53:01 +0000 | [diff] [blame] | 20 | static void sleep_ms(const int ms) |
| 21 | { |
| 22 | struct timespec delay = { ms / 1000, (ms % 1000) * 1000 * 1000 }; |
| 23 | nanosleep(&delay, 0); |
| 24 | } |
| 25 | |
| 26 | static void* thread_func(void* arg) |
bart | 5bd9f2d | 2008-03-03 20:31:58 +0000 | [diff] [blame] | 27 | { |
| 28 | pthread_rwlock_rdlock(&s_rwlock); |
| 29 | s_racy++; |
| 30 | pthread_rwlock_unlock(&s_rwlock); |
bart | 68a8a20 | 2008-06-28 11:53:01 +0000 | [diff] [blame] | 31 | sleep_ms(100); |
bart | 5bd9f2d | 2008-03-03 20:31:58 +0000 | [diff] [blame] | 32 | return 0; |
| 33 | } |
| 34 | |
| 35 | int main(int argc, char** argv) |
| 36 | { |
| 37 | pthread_t thread1; |
| 38 | pthread_t thread2; |
| 39 | |
| 40 | #if 0 |
bart | 575ce8e | 2011-05-15 07:04:03 +0000 | [diff] [blame] | 41 | |
sewardj | 4b3a742 | 2011-10-24 13:21:57 +0000 | [diff] [blame] | 42 | VALGRIND_DO_CLIENT_REQUEST_STMT(VG_USERREQ__DRD_TRACE_ADDR, |
bart | 575ce8e | 2011-05-15 07:04:03 +0000 | [diff] [blame] | 43 | &s_racy, 0, 0, 0, 0); |
bart | 5bd9f2d | 2008-03-03 20:31:58 +0000 | [diff] [blame] | 44 | #endif |
| 45 | |
| 46 | pthread_rwlock_init(&s_rwlock, 0); |
bart | 68a8a20 | 2008-06-28 11:53:01 +0000 | [diff] [blame] | 47 | pthread_create(&thread1, 0, thread_func, 0); |
| 48 | pthread_create(&thread2, 0, thread_func, 0); |
bart | 5bd9f2d | 2008-03-03 20:31:58 +0000 | [diff] [blame] | 49 | pthread_join(thread1, 0); |
| 50 | pthread_join(thread2, 0); |
| 51 | pthread_rwlock_destroy(&s_rwlock); |
| 52 | |
| 53 | fprintf(stderr, "Result: %d\n", s_racy); |
| 54 | |
| 55 | return 0; |
| 56 | } |