bart | 55df6e7 | 2009-02-16 19:42:17 +0000 | [diff] [blame] | 1 | /* Test whether detached threads are handled properly. */ |
bart | 25200ff | 2008-03-08 13:15:03 +0000 | [diff] [blame] | 2 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 3 | #include <assert.h> |
bart | f071e5e | 2011-05-17 16:45:08 +0000 | [diff] [blame] | 4 | #include <limits.h> /* PTHREAD_STACK_MIN */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 5 | #include <pthread.h> |
| 6 | #include <stdio.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <unistd.h> |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 9 | |
bart | db57889 | 2011-07-27 08:49:47 +0000 | [diff] [blame] | 10 | static int s_finished_count; /* protected by s_mutex */ |
bart | 6c87f31 | 2008-03-16 07:46:36 +0000 | [diff] [blame] | 11 | static pthread_mutex_t s_mutex; |
bart | 76fcb5a | 2011-03-17 07:39:00 +0000 | [diff] [blame] | 12 | static pthread_cond_t s_cond; |
bart | 25200ff | 2008-03-08 13:15:03 +0000 | [diff] [blame] | 13 | |
bart | a547c82 | 2008-03-13 17:34:43 +0000 | [diff] [blame] | 14 | static void increment_finished_count() |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 15 | { |
bart | 6c87f31 | 2008-03-16 07:46:36 +0000 | [diff] [blame] | 16 | pthread_mutex_lock(&s_mutex); |
| 17 | s_finished_count++; |
bart | 76fcb5a | 2011-03-17 07:39:00 +0000 | [diff] [blame] | 18 | pthread_cond_signal(&s_cond); |
bart | 6c87f31 | 2008-03-16 07:46:36 +0000 | [diff] [blame] | 19 | pthread_mutex_unlock(&s_mutex); |
| 20 | } |
| 21 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 22 | static void* thread_func1(void* arg) |
| 23 | { |
bart | a547c82 | 2008-03-13 17:34:43 +0000 | [diff] [blame] | 24 | write(STDOUT_FILENO, ".", 1); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 25 | increment_finished_count(); |
| 26 | return 0; |
| 27 | } |
| 28 | |
| 29 | static void* thread_func2(void* arg) |
| 30 | { |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 31 | pthread_detach(pthread_self()); |
bart | a547c82 | 2008-03-13 17:34:43 +0000 | [diff] [blame] | 32 | write(STDOUT_FILENO, ".", 1); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 33 | increment_finished_count(); |
| 34 | return 0; |
| 35 | } |
| 36 | |
| 37 | int 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; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 41 | int i; |
| 42 | int detachstate; |
| 43 | pthread_attr_t attr; |
| 44 | |
bart | 6c87f31 | 2008-03-16 07:46:36 +0000 | [diff] [blame] | 45 | pthread_mutex_init(&s_mutex, 0); |
bart | 76fcb5a | 2011-03-17 07:39:00 +0000 | [diff] [blame] | 46 | pthread_cond_init(&s_cond, 0); |
bart | 31b983d | 2010-02-21 14:52:59 +0000 | [diff] [blame] | 47 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 48 | 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); |
bart | f071e5e | 2011-05-17 16:45:08 +0000 | [diff] [blame] | 52 | pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + 4096); |
bart | 31b983d | 2010-02-21 14:52:59 +0000 | [diff] [blame] | 53 | // Create count1 detached threads by setting the "detached" property via |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 54 | // thread attributes. |
| 55 | for (i = 0; i < count1; i++) |
| 56 | { |
| 57 | pthread_t thread; |
bart | f071e5e | 2011-05-17 16:45:08 +0000 | [diff] [blame] | 58 | pthread_create(&thread, &attr, thread_func1, NULL); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 59 | } |
| 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; |
bart | f071e5e | 2011-05-17 16:45:08 +0000 | [diff] [blame] | 67 | pthread_create(&thread, &attr, thread_func2, NULL); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 68 | } |
| 69 | pthread_attr_destroy(&attr); |
| 70 | |
| 71 | // Wait until all detached threads have written their output to stdout. |
bart | 76fcb5a | 2011-03-17 07:39:00 +0000 | [diff] [blame] | 72 | pthread_mutex_lock(&s_mutex); |
bart | db57889 | 2011-07-27 08:49:47 +0000 | [diff] [blame] | 73 | while (s_finished_count < count1 + count2) { |
| 74 | const int ret = pthread_cond_wait(&s_cond, &s_mutex); |
| 75 | assert(ret == 0); |
| 76 | } |
bart | 76fcb5a | 2011-03-17 07:39:00 +0000 | [diff] [blame] | 77 | pthread_mutex_unlock(&s_mutex); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 78 | |
bart | 76fcb5a | 2011-03-17 07:39:00 +0000 | [diff] [blame] | 79 | pthread_cond_destroy(&s_cond); |
bart | 6c87f31 | 2008-03-16 07:46:36 +0000 | [diff] [blame] | 80 | pthread_mutex_destroy(&s_mutex); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 81 | |
bart | 76fcb5a | 2011-03-17 07:39:00 +0000 | [diff] [blame] | 82 | write(STDOUT_FILENO, "\n", 1); |
bart | 689a868 | 2011-07-28 18:06:44 +0000 | [diff] [blame] | 83 | fprintf(stderr, "Done.\n"); |
bart | 76fcb5a | 2011-03-17 07:39:00 +0000 | [diff] [blame] | 84 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 85 | return 0; |
| 86 | } |