tst-tls4.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* Copyright (C) 2003 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Jakub Jelinek <jakub@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 <dlfcn.h>
  16. #include <errno.h>
  17. #include <pthread.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <tls.h>
  22. #if HAVE___THREAD && defined HAVE_TLS_MODEL_ATTRIBUTE
  23. #define N 3
  24. void (*test1) (void), (*test2) (void);
  25. pthread_barrier_t b2, b3;
  26. static void *
  27. tf (void *arg)
  28. {
  29. int i;
  30. for (i = 0; i <= (uintptr_t) arg; ++i)
  31. {
  32. int r = pthread_barrier_wait (&b3);
  33. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  34. {
  35. puts ("tf: barrier_wait failed");
  36. exit (1);
  37. }
  38. }
  39. test1 ();
  40. for (i = 0; i < 3; ++i)
  41. {
  42. int r = pthread_barrier_wait (&b3);
  43. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  44. {
  45. puts ("tf: barrier_wait failed");
  46. exit (1);
  47. }
  48. }
  49. test2 ();
  50. for (i = 0; i < 3 - (uintptr_t) arg; ++i)
  51. {
  52. int r = pthread_barrier_wait (&b3);
  53. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  54. {
  55. puts ("tf: barrier_wait failed");
  56. exit (1);
  57. }
  58. }
  59. return NULL;
  60. }
  61. static void *
  62. tf2 (void *arg)
  63. {
  64. int r = pthread_barrier_wait (&b2);
  65. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  66. {
  67. puts ("tf2: barrier_wait failed");
  68. exit (1);
  69. }
  70. int i;
  71. for (i = 0; i < N; ++i)
  72. tf (arg);
  73. return NULL;
  74. }
  75. int
  76. do_test (void)
  77. {
  78. pthread_t th[2];
  79. const char *modules[N]
  80. = { "tst-tls4moda.so", "tst-tls4moda.so", "tst-tls4modb.so" };
  81. if (pthread_barrier_init (&b2, NULL, 2) != 0)
  82. {
  83. puts ("barrier_init failed");
  84. return 1;
  85. }
  86. if (pthread_barrier_init (&b3, NULL, 3) != 0)
  87. {
  88. puts ("barrier_init failed");
  89. return 1;
  90. }
  91. if (pthread_create (&th[0], NULL, tf2, (void *) (uintptr_t) 1))
  92. {
  93. puts ("pthread_create failed");
  94. return 1;
  95. }
  96. int r = pthread_barrier_wait (&b2);
  97. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  98. {
  99. puts ("barrier_wait failed");
  100. return 1;
  101. }
  102. int i;
  103. for (i = 0; i < N; ++i)
  104. {
  105. void *h = dlopen (modules[i], RTLD_LAZY);
  106. if (h == NULL)
  107. {
  108. printf ("dlopen failed %s\n", dlerror ());
  109. return 1;
  110. }
  111. test1 = dlsym (h, "test1");
  112. if (test1 == NULL)
  113. {
  114. printf ("dlsym for test1 failed %s\n", dlerror ());
  115. return 1;
  116. }
  117. test2 = dlsym (h, "test2");
  118. if (test2 == NULL)
  119. {
  120. printf ("dlsym for test2 failed %s\n", dlerror ());
  121. return 1;
  122. }
  123. if (pthread_create (&th[1], NULL, tf, (void *) (uintptr_t) 2))
  124. {
  125. puts ("pthread_create failed");
  126. return 1;
  127. }
  128. tf ((void *) (uintptr_t) 0);
  129. if (pthread_join (th[1], NULL) != 0)
  130. {
  131. puts ("join failed");
  132. return 1;
  133. }
  134. if (dlclose (h))
  135. {
  136. puts ("dlclose failed");
  137. return 1;
  138. }
  139. printf ("test %d with %s succeeded\n", i, modules[i]);
  140. }
  141. if (pthread_join (th[0], NULL) != 0)
  142. {
  143. puts ("join failed");
  144. return 1;
  145. }
  146. return 0;
  147. }
  148. #define TIMEOUT 5
  149. #define TEST_FUNCTION do_test ()
  150. #else
  151. #define TEST_FUNCTION 0
  152. #endif
  153. #include "../test-skeleton.c"