tst-tls1.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* Copyright (C) 2003-2016 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <pthread.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. struct test_s
  19. {
  20. int a;
  21. int b;
  22. };
  23. #define INIT_A 1
  24. #define INIT_B 42
  25. /* Deliberately not static. */
  26. __thread struct test_s s __attribute__ ((tls_model ("initial-exec"))) =
  27. {
  28. .a = INIT_A,
  29. .b = INIT_B
  30. };
  31. static void *
  32. tf (void *arg)
  33. {
  34. if (s.a != INIT_A || s.b != INIT_B)
  35. {
  36. puts ("initial value of s in child thread wrong");
  37. exit (1);
  38. }
  39. ++s.a;
  40. return NULL;
  41. }
  42. int
  43. do_test (void)
  44. {
  45. if (s.a != INIT_A || s.b != INIT_B)
  46. {
  47. puts ("initial value of s in main thread wrong");
  48. exit (1);
  49. }
  50. pthread_attr_t a;
  51. if (pthread_attr_init (&a) != 0)
  52. {
  53. puts ("attr_init failed");
  54. exit (1);
  55. }
  56. if (pthread_attr_setstacksize (&a, 1 * 1024 * 1024) != 0)
  57. {
  58. puts ("attr_setstacksize failed");
  59. return 1;
  60. }
  61. #define N 10
  62. int i;
  63. for (i = 0; i < N; ++i)
  64. {
  65. #define M 10
  66. pthread_t th[M];
  67. int j;
  68. for (j = 0; j < M; ++j, ++s.a)
  69. if (pthread_create (&th[j], &a, tf, NULL) != 0)
  70. {
  71. puts ("pthread_create failed");
  72. exit (1);
  73. }
  74. for (j = 0; j < M; ++j)
  75. if (pthread_join (th[j], NULL) != 0)
  76. {
  77. puts ("pthread_join failed");
  78. exit (1);
  79. }
  80. }
  81. if (pthread_attr_destroy (&a) != 0)
  82. {
  83. puts ("attr_destroy failed");
  84. exit (1);
  85. }
  86. return 0;
  87. }
  88. #define TEST_FUNCTION do_test ()
  89. #include "../test-skeleton.c"