tst-tls4.c 3.7 KB

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