blob: f755a8c81a226cd070d00de71dfca0cbbf064308 [file] [log] [blame]
bart55df6e72009-02-16 19:42:17 +00001/* Test whether detached threads are handled properly. */
bart25200ff2008-03-08 13:15:03 +00002
sewardjaf44c822007-11-25 14:01:38 +00003#include <assert.h>
bartf071e5e2011-05-17 16:45:08 +00004#include <limits.h> /* PTHREAD_STACK_MIN */
sewardjaf44c822007-11-25 14:01:38 +00005#include <pthread.h>
6#include <stdio.h>
7#include <stdlib.h>
8#include <unistd.h>
sewardjaf44c822007-11-25 14:01:38 +00009
bartdb578892011-07-27 08:49:47 +000010static int s_finished_count; /* protected by s_mutex */
bart6c87f312008-03-16 07:46:36 +000011static pthread_mutex_t s_mutex;
bart76fcb5a2011-03-17 07:39:00 +000012static pthread_cond_t s_cond;
bart25200ff2008-03-08 13:15:03 +000013
barta547c822008-03-13 17:34:43 +000014static void increment_finished_count()
sewardjaf44c822007-11-25 14:01:38 +000015{
bart6c87f312008-03-16 07:46:36 +000016 pthread_mutex_lock(&s_mutex);
17 s_finished_count++;
bart76fcb5a2011-03-17 07:39:00 +000018 pthread_cond_signal(&s_cond);
bart6c87f312008-03-16 07:46:36 +000019 pthread_mutex_unlock(&s_mutex);
20}
21
sewardjaf44c822007-11-25 14:01:38 +000022static void* thread_func1(void* arg)
23{
barta547c822008-03-13 17:34:43 +000024 write(STDOUT_FILENO, ".", 1);
sewardjaf44c822007-11-25 14:01:38 +000025 increment_finished_count();
26 return 0;
27}
28
29static void* thread_func2(void* arg)
30{
sewardjaf44c822007-11-25 14:01:38 +000031 pthread_detach(pthread_self());
barta547c822008-03-13 17:34:43 +000032 write(STDOUT_FILENO, ".", 1);
sewardjaf44c822007-11-25 14:01:38 +000033 increment_finished_count();
34 return 0;
35}
36
37int main(int argc, char** argv)
38{
39 const int count1 = argc > 1 ? atoi(argv[1]) : 100;
40 const int count2 = argc > 2 ? atoi(argv[2]) : 100;
sewardjaf44c822007-11-25 14:01:38 +000041 int i;
42 int detachstate;
43 pthread_attr_t attr;
44
bart6c87f312008-03-16 07:46:36 +000045 pthread_mutex_init(&s_mutex, 0);
bart76fcb5a2011-03-17 07:39:00 +000046 pthread_cond_init(&s_cond, 0);
bart31b983d2010-02-21 14:52:59 +000047
sewardjaf44c822007-11-25 14:01:38 +000048 pthread_attr_init(&attr);
49 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
50 assert(pthread_attr_getdetachstate(&attr, &detachstate) == 0);
51 assert(detachstate == PTHREAD_CREATE_DETACHED);
bartf071e5e2011-05-17 16:45:08 +000052 pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + 4096);
bart31b983d2010-02-21 14:52:59 +000053 // Create count1 detached threads by setting the "detached" property via
sewardjaf44c822007-11-25 14:01:38 +000054 // thread attributes.
55 for (i = 0; i < count1; i++)
56 {
57 pthread_t thread;
bartf071e5e2011-05-17 16:45:08 +000058 pthread_create(&thread, &attr, thread_func1, NULL);
sewardjaf44c822007-11-25 14:01:38 +000059 }
60 // Create count2 detached threads by letting the threads detach themselves.
61 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
62 assert(pthread_attr_getdetachstate(&attr, &detachstate) == 0);
63 assert(detachstate == PTHREAD_CREATE_JOINABLE);
64 for (i = 0; i < count2; i++)
65 {
66 pthread_t thread;
bartf071e5e2011-05-17 16:45:08 +000067 pthread_create(&thread, &attr, thread_func2, NULL);
sewardjaf44c822007-11-25 14:01:38 +000068 }
69 pthread_attr_destroy(&attr);
70
71 // Wait until all detached threads have written their output to stdout.
bart76fcb5a2011-03-17 07:39:00 +000072 pthread_mutex_lock(&s_mutex);
bartdb578892011-07-27 08:49:47 +000073 while (s_finished_count < count1 + count2) {
74 const int ret = pthread_cond_wait(&s_cond, &s_mutex);
75 assert(ret == 0);
76 }
bart76fcb5a2011-03-17 07:39:00 +000077 pthread_mutex_unlock(&s_mutex);
sewardjaf44c822007-11-25 14:01:38 +000078
bart76fcb5a2011-03-17 07:39:00 +000079 pthread_cond_destroy(&s_cond);
bart6c87f312008-03-16 07:46:36 +000080 pthread_mutex_destroy(&s_mutex);
sewardjaf44c822007-11-25 14:01:38 +000081
bart76fcb5a2011-03-17 07:39:00 +000082 write(STDOUT_FILENO, "\n", 1);
bart689a8682011-07-28 18:06:44 +000083 fprintf(stderr, "Done.\n");
bart76fcb5a2011-03-17 07:39:00 +000084
sewardjaf44c822007-11-25 14:01:38 +000085 return 0;
86}