1 /* Stress test for the --free-is-write command-line option. */
2
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>
11
12 #define MALLOC_SIZE 22816
13 #define THREAD_COUNT 10
14 #define MALLOC_COUNT 1000
15
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;
20
thread_func(void * arg)21 void* thread_func(void* arg)
22 {
23 unsigned i;
24
25 for (i = 0; i < MALLOC_COUNT; ++i) {
26 void* ptr;
27
28 ptr = malloc(MALLOC_SIZE);
29 memset(ptr, 0, MALLOC_SIZE);
30 free(ptr);
31 }
32
33 pthread_mutex_lock(&mutex);
34 ++count;
35 pthread_cond_signal(&cond);
36 pthread_mutex_unlock(&mutex);
37
38 return 0;
39 }
40
main(int argc,char ** argv)41 int main(int argc, char **argv)
42 {
43 pthread_t thread[THREAD_COUNT];
44 int result;
45 int i;
46
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);
54 }
55
56 pthread_mutex_lock(&mutex);
57 while (count < THREAD_COUNT && pthread_cond_wait(&cond, &mutex) == 0)
58 ;
59 pthread_mutex_unlock(&mutex);
60
61 for (i = 0; i < THREAD_COUNT; i++)
62 pthread_join(thread[i], 0);
63
64 fflush(stdout);
65
66 fprintf(stderr, "Done.\n");
67
68 return 0;
69 }
70