tst-tls1.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* Copyright (C) 2003, 2004 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. #include <tls.h>
  19. #if HAVE___THREAD
  20. struct test_s
  21. {
  22. int a;
  23. int b;
  24. };
  25. #define INIT_A 1
  26. #define INIT_B 42
  27. /* Deliberately not static. */
  28. __thread struct test_s s __attribute__ ((tls_model ("initial-exec"))) =
  29. {
  30. .a = INIT_A,
  31. .b = INIT_B
  32. };
  33. static void *
  34. tf (void *arg)
  35. {
  36. if (s.a != INIT_A || s.b != INIT_B)
  37. {
  38. puts ("initial value of s in child thread wrong");
  39. exit (1);
  40. }
  41. ++s.a;
  42. return NULL;
  43. }
  44. #endif
  45. int
  46. do_test (void)
  47. {
  48. #if !HAVE___THREAD
  49. puts ("No __thread support in compiler, test skipped.");
  50. return 0;
  51. #else
  52. if (s.a != INIT_A || s.b != INIT_B)
  53. {
  54. puts ("initial value of s in main thread wrong");
  55. exit (1);
  56. }
  57. pthread_attr_t a;
  58. if (pthread_attr_init (&a) != 0)
  59. {
  60. puts ("attr_init failed");
  61. exit (1);
  62. }
  63. if (pthread_attr_setstacksize (&a, 1 * 1024 * 1024) != 0)
  64. {
  65. puts ("attr_setstacksize failed");
  66. return 1;
  67. }
  68. #define N 10
  69. int i;
  70. for (i = 0; i < N; ++i)
  71. {
  72. #define M 10
  73. pthread_t th[M];
  74. int j;
  75. for (j = 0; j < M; ++j, ++s.a)
  76. if (pthread_create (&th[j], &a, tf, NULL) != 0)
  77. {
  78. puts ("pthread_create failed");
  79. exit (1);
  80. }
  81. for (j = 0; j < M; ++j)
  82. if (pthread_join (th[j], NULL) != 0)
  83. {
  84. puts ("pthread_join failed");
  85. exit (1);
  86. }
  87. }
  88. if (pthread_attr_destroy (&a) != 0)
  89. {
  90. puts ("attr_destroy failed");
  91. exit (1);
  92. }
  93. return 0;
  94. #endif
  95. }
  96. #define TEST_FUNCTION do_test ()
  97. #include "../test-skeleton.c"