tst-tls1.c 2.4 KB

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