2 * Beat up the pthread_key_create and pthread_key_delete
16 const int beatup_iterations
= 10000;
17 const int num_threads
= 30;
18 const int max_keys
= 500;
21 struct key_list
*next
;
25 struct key_list
*key_list
;
26 pthread_mutex_t key_lock
= PTHREAD_MUTEX_INITIALIZER
;
29 * Create a new key and put it at the tail of a linked list.
30 * If the linked list grows to a certain length, delete a key from the
37 struct key_list
*new = malloc(sizeof *new);
38 struct key_list
**iter
, *old_key
= 0;
42 fprintf(stderr
, "malloc failed\n");
48 if (pthread_key_create(&new->key
, 0) != 0) {
49 fprintf(stderr
, "pthread_key_create failed\n");
53 if (pthread_getspecific(new->key
) != 0) {
54 fprintf(stderr
, "new pthread_key_t resolves to non-null value\n");
58 pthread_setspecific(new->key
, (void *) 1);
61 printf("created key\n");
64 pthread_mutex_lock(&key_lock
);
66 for (iter
= &key_list
; *iter
!= 0; iter
= &(*iter
)->next
)
71 if (key_count
> max_keys
) {
73 key_list
= key_list
->next
;
76 pthread_mutex_unlock(&key_lock
);
80 printf("deleting key\n");
82 pthread_key_delete(old_key
->key
);
90 for (i
= 0; i
< beatup_iterations
; i
++)
99 pthread_attr_t detached_thread
;
101 pthread_attr_init(&detached_thread
);
102 pthread_attr_setdetachstate(&detached_thread
, PTHREAD_CREATE_DETACHED
);
104 for (i
= 0; i
< num_threads
; i
++) {
106 while (pthread_create(&thread_id
, &detached_thread
, thread
, 0) == EAGAIN
) {
107 /* let some threads die, so system can breathe. :) */