tst-tls4.c 3.8 KB

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