1 /* Stress test for the --free-is-write command-line option. */ 2 3 #include <pthread.h> 4 #include <assert.h> 5 #include <unistd.h> 6 #include <stdlib.h> 7 #include <stdio.h> 8 #include <pthread.h> 9 #include <string.h> 10 11 #define MALLOC_SIZE 22816 12 #define THREAD_COUNT 10 13 #define MALLOC_COUNT 1000 14 15 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; 16 // 'mutex' protects 'count'. 17 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 18 static unsigned count; 19 20 void* thread_func(void* arg) 21 { 22 unsigned i; 23 24 for (i = 0; i < MALLOC_COUNT; ++i) { 25 void* ptr; 26 27 ptr = malloc(MALLOC_SIZE); 28 memset(ptr, 0, MALLOC_SIZE); 29 free(ptr); 30 } 31 32 pthread_mutex_lock(&mutex); 33 ++count; 34 pthread_cond_signal(&cond); 35 pthread_mutex_unlock(&mutex); 36 37 return 0; 38 } 39 40 int main(int argc, char **argv) 41 { 42 pthread_t thread[THREAD_COUNT]; 43 int result; 44 int i; 45 46 for (i = 0; i < THREAD_COUNT; i++) { 47 result = pthread_create(&thread[i], 0, thread_func, 0); 48 assert(result == 0); 49 } 50 51 pthread_mutex_lock(&mutex); 52 while (count < THREAD_COUNT && pthread_cond_wait(&cond, &mutex) == 0) 53 ; 54 pthread_mutex_unlock(&mutex); 55 56 for (i = 0; i < THREAD_COUNT; i++) 57 pthread_join(thread[i], 0); 58 59 fflush(stdout); 60 61 fprintf(stderr, "Done.\n"); 62 63 return 0; 64 } 65