blob: 4a73bfc54e83e4f4bbed3e605bc66be3c3b2b73c [file] [log] [blame]
bart5bd9f2d2008-03-03 20:31:58 +00001/** 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
bart5bd9f2d2008-03-03 20:31:58 +000011#include <pthread.h>
bart68a8a202008-06-28 11:53:01 +000012#include <stdio.h>
13#include <unistd.h>
bart5f57be92008-07-01 08:48:56 +000014
bart5bd9f2d2008-03-03 20:31:58 +000015
16
17static pthread_rwlock_t s_rwlock;
18static int s_racy;
19
bart68a8a202008-06-28 11:53:01 +000020static void sleep_ms(const int ms)
21{
22 struct timespec delay = { ms / 1000, (ms % 1000) * 1000 * 1000 };
23 nanosleep(&delay, 0);
24}
25
26static void* thread_func(void* arg)
bart5bd9f2d2008-03-03 20:31:58 +000027{
28 pthread_rwlock_rdlock(&s_rwlock);
29 s_racy++;
30 pthread_rwlock_unlock(&s_rwlock);
bart68a8a202008-06-28 11:53:01 +000031 sleep_ms(100);
bart5bd9f2d2008-03-03 20:31:58 +000032 return 0;
33}
34
35int main(int argc, char** argv)
36{
37 pthread_t thread1;
38 pthread_t thread2;
39
40#if 0
bart575ce8e2011-05-15 07:04:03 +000041
sewardj4b3a7422011-10-24 13:21:57 +000042 VALGRIND_DO_CLIENT_REQUEST_STMT(VG_USERREQ__DRD_TRACE_ADDR,
bart575ce8e2011-05-15 07:04:03 +000043 &s_racy, 0, 0, 0, 0);
bart5bd9f2d2008-03-03 20:31:58 +000044#endif
45
46 pthread_rwlock_init(&s_rwlock, 0);
bart68a8a202008-06-28 11:53:01 +000047 pthread_create(&thread1, 0, thread_func, 0);
48 pthread_create(&thread2, 0, thread_func, 0);
bart5bd9f2d2008-03-03 20:31:58 +000049 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}