tst-mode-switch-2.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* Copyright (C) 2014-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #include <errno.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <unistd.h>
  19. #include <limits.h>
  20. #include <pthread.h>
  21. #include <sys/prctl.h>
  22. #if __mips_fpr != 0 || _MIPS_SPFPSET != 16
  23. # error This test requires -mfpxx -mno-odd-spreg
  24. #endif
  25. /* This test verifies that all threads in a process see a mode
  26. change when any thread causes a mode change. */
  27. static int mode[6] =
  28. {
  29. 0,
  30. PR_FP_MODE_FR,
  31. PR_FP_MODE_FR | PR_FP_MODE_FRE,
  32. PR_FP_MODE_FR,
  33. 0,
  34. PR_FP_MODE_FR | PR_FP_MODE_FRE
  35. };
  36. static volatile int current_mode;
  37. static volatile int finished;
  38. static pthread_barrier_t barr_ready;
  39. static pthread_barrier_t barr_cont;
  40. static void *
  41. thread_function (void * arg __attribute__ ((unused)))
  42. {
  43. while (!finished)
  44. {
  45. int res = pthread_barrier_wait (&barr_ready);
  46. if (res != 0 && res != PTHREAD_BARRIER_SERIAL_THREAD)
  47. {
  48. printf ("barrier wait failed: %m\n");
  49. exit (1);
  50. }
  51. int mode = prctl (PR_GET_FP_MODE);
  52. if (mode != current_mode)
  53. {
  54. printf ("unexpected mode: %d != %d\n", mode, current_mode);
  55. exit (1);
  56. }
  57. res = pthread_barrier_wait (&barr_cont);
  58. if (res != 0 && res != PTHREAD_BARRIER_SERIAL_THREAD)
  59. {
  60. printf ("barrier wait failed: %m\n");
  61. exit (1);
  62. }
  63. }
  64. return NULL;
  65. }
  66. static int
  67. do_test (void)
  68. {
  69. int count = sysconf (_SC_NPROCESSORS_ONLN);
  70. if (count <= 0)
  71. count = 1;
  72. count *= 4;
  73. pthread_t th[count];
  74. int i;
  75. int result = 0;
  76. if (pthread_barrier_init (&barr_ready, NULL, count + 1) != 0)
  77. {
  78. printf ("failed to initialize barrier: %m\n");
  79. exit (1);
  80. }
  81. if (pthread_barrier_init (&barr_cont, NULL, count + 1) != 0)
  82. {
  83. printf ("failed to initialize barrier: %m\n");
  84. exit (1);
  85. }
  86. for (i = 0; i < count; ++i)
  87. if (pthread_create (&th[i], NULL, thread_function, 0) != 0)
  88. {
  89. printf ("creation of thread %d failed\n", i);
  90. exit (1);
  91. }
  92. for (i = 0 ; i < 7 ; i++)
  93. {
  94. if (prctl (PR_SET_FP_MODE, mode[i % 6]) != 0)
  95. {
  96. if (errno != ENOTSUP)
  97. {
  98. printf ("prctl PR_SET_FP_MODE failed: %m");
  99. exit (1);
  100. }
  101. }
  102. else
  103. current_mode = mode[i % 6];
  104. int res = pthread_barrier_wait (&barr_ready);
  105. if (res != 0 && res != PTHREAD_BARRIER_SERIAL_THREAD)
  106. {
  107. printf ("barrier wait failed: %m\n");
  108. exit (1);
  109. }
  110. if (i == 6)
  111. finished = 1;
  112. res = pthread_barrier_wait (&barr_cont);
  113. if (res != 0 && res != PTHREAD_BARRIER_SERIAL_THREAD)
  114. {
  115. printf ("barrier wait failed: %m\n");
  116. exit (1);
  117. }
  118. }
  119. for (i = 0; i < count; ++i)
  120. {
  121. void *v;
  122. if (pthread_join (th[i], &v) != 0)
  123. {
  124. printf ("join of thread %d failed\n", i);
  125. result = 1;
  126. }
  127. else if (v != NULL)
  128. {
  129. printf ("join %d successful, but child failed\n", i);
  130. result = 1;
  131. }
  132. else
  133. printf ("join %d successful\n", i);
  134. }
  135. return result;
  136. }
  137. #define TEST_FUNCTION do_test ()
  138. #include "../../test-skeleton.c"