Add DRD suppression patterns for races triggered by std::ostream
[valgrind.git] / drd / tests / free_is_write.c
blob6988057cb02d4a0fc9a3c8dc50f5793de77f3f74
1 /* Stress test for the --free-is-write command-line option. */
3 #include <assert.h>
4 #include <limits.h>
5 #include <pthread.h>
6 #include <pthread.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <unistd.h>
12 #define MALLOC_SIZE 22816
13 #define THREAD_COUNT 10
14 #define MALLOC_COUNT 1000
16 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
17 // 'mutex' protects 'count'.
18 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
19 static unsigned count;
21 void* thread_func(void* arg)
23 unsigned i;
25 for (i = 0; i < MALLOC_COUNT; ++i) {
26 void* ptr;
28 ptr = malloc(MALLOC_SIZE);
29 memset(ptr, 0, MALLOC_SIZE);
30 free(ptr);
33 pthread_mutex_lock(&mutex);
34 ++count;
35 pthread_cond_signal(&cond);
36 pthread_mutex_unlock(&mutex);
38 return 0;
41 int main(int argc, char **argv)
43 pthread_t thread[THREAD_COUNT];
44 int result;
45 int i;
47 for (i = 0; i < THREAD_COUNT; i++) {
48 pthread_attr_t attr;
49 pthread_attr_init(&attr);
50 pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + 4096);
51 result = pthread_create(&thread[i], &attr, thread_func, 0);
52 pthread_attr_destroy(&attr);
53 assert(result == 0);
56 pthread_mutex_lock(&mutex);
57 while (count < THREAD_COUNT && pthread_cond_wait(&cond, &mutex) == 0)
59 pthread_mutex_unlock(&mutex);
61 for (i = 0; i < THREAD_COUNT; i++)
62 pthread_join(thread[i], 0);
64 fflush(stdout);
66 fprintf(stderr, "Done.\n");
68 return 0;