xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame^] | 1 | /* Copyright (C) 2002-2016 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | Contributed by Ulrich Drepper <drepper@redhat.com>, 2002. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, see |
| 17 | <http://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #include <pthread.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <time.h> |
| 23 | |
| 24 | |
| 25 | #define N 100 |
| 26 | |
| 27 | static pthread_once_t once = PTHREAD_ONCE_INIT; |
| 28 | |
| 29 | static int global; |
| 30 | |
| 31 | static void |
| 32 | once_handler (void) |
| 33 | { |
| 34 | struct timespec ts; |
| 35 | |
| 36 | ++global; |
| 37 | |
| 38 | ts.tv_sec = 2; |
| 39 | ts.tv_nsec = 0; |
| 40 | nanosleep (&ts, NULL); |
| 41 | } |
| 42 | |
| 43 | |
| 44 | static void * |
| 45 | tf (void *arg) |
| 46 | { |
| 47 | pthread_once (&once, once_handler); |
| 48 | |
| 49 | if (global != 1) |
| 50 | { |
| 51 | printf ("thread %ld: global == %d\n", (long int) arg, global); |
| 52 | exit (1); |
| 53 | } |
| 54 | |
| 55 | return NULL; |
| 56 | } |
| 57 | |
| 58 | |
| 59 | static int |
| 60 | do_test (void) |
| 61 | { |
| 62 | pthread_attr_t at; |
| 63 | pthread_t th[N]; |
| 64 | int cnt; |
| 65 | |
| 66 | if (pthread_attr_init (&at) != 0) |
| 67 | { |
| 68 | puts ("attr_init failed"); |
| 69 | return 1; |
| 70 | } |
| 71 | |
| 72 | if (pthread_attr_setstacksize (&at, 1 * 1024 * 1024) != 0) |
| 73 | { |
| 74 | puts ("attr_setstacksize failed"); |
| 75 | return 1; |
| 76 | } |
| 77 | |
| 78 | for (cnt = 0; cnt < N; ++cnt) |
| 79 | if (pthread_create (&th[cnt], &at, tf, (void *) (long int) cnt) != 0) |
| 80 | { |
| 81 | printf ("creation of thread %d failed\n", cnt); |
| 82 | return 1; |
| 83 | } |
| 84 | |
| 85 | if (pthread_attr_destroy (&at) != 0) |
| 86 | { |
| 87 | puts ("attr_destroy failed"); |
| 88 | return 1; |
| 89 | } |
| 90 | |
| 91 | for (cnt = 0; cnt < N; ++cnt) |
| 92 | if (pthread_join (th[cnt], NULL) != 0) |
| 93 | { |
| 94 | printf ("join of thread %d failed\n", cnt); |
| 95 | return 1; |
| 96 | } |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | #define TEST_FUNCTION do_test () |
| 102 | #define TIMEOUT 4 |
| 103 | #include "../test-skeleton.c" |