tst-cnd-broadcast.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* C11 threads condition broadcast variable tests.
  2. Copyright (C) 2018-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  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 <threads.h>
  16. #include <stdbool.h>
  17. #include <stdio.h>
  18. #include <unistd.h>
  19. #include <support/check.h>
  20. /* Condition variable where child threads will wait. */
  21. static cnd_t cond;
  22. /* Mutex to control wait on cond. */
  23. static mtx_t mutex;
  24. /* Number of threads which have entered the cnd_wait region. */
  25. static unsigned int waiting_threads;
  26. /* Code executed by each thread. */
  27. static int
  28. child_wait (void* data)
  29. {
  30. /* Wait until parent thread sends broadcast here. */
  31. mtx_lock (&mutex);
  32. ++waiting_threads;
  33. cnd_wait (&cond, &mutex);
  34. mtx_unlock (&mutex);
  35. thrd_exit (thrd_success);
  36. }
  37. #define N 5
  38. static int
  39. do_test (void)
  40. {
  41. thrd_t ids[N];
  42. unsigned char i;
  43. if (cnd_init (&cond) != thrd_success)
  44. FAIL_EXIT1 ("cnd_init failed");
  45. if (mtx_init (&mutex, mtx_plain) != thrd_success)
  46. FAIL_EXIT1 ("mtx_init failed");
  47. /* Create N new threads. */
  48. for (i = 0; i < N; ++i)
  49. {
  50. if (thrd_create (&ids[i], child_wait, NULL) != thrd_success)
  51. FAIL_EXIT1 ("thrd_create failed");
  52. }
  53. /* Wait for other threads to reach their wait func. */
  54. while (true)
  55. {
  56. mtx_lock (&mutex);
  57. TEST_VERIFY (waiting_threads <= N);
  58. bool done_waiting = waiting_threads == N;
  59. mtx_unlock (&mutex);
  60. if (done_waiting)
  61. break;
  62. thrd_sleep (&((struct timespec){.tv_nsec = 100 * 1000 * 1000}), NULL);
  63. }
  64. mtx_lock (&mutex);
  65. if (cnd_broadcast (&cond) != thrd_success)
  66. FAIL_EXIT1 ("cnd_broadcast failed");
  67. mtx_unlock (&mutex);
  68. for (i = 0; i < N; ++i)
  69. {
  70. if (thrd_join (ids[i], NULL) != thrd_success)
  71. FAIL_EXIT1 ("thrd_join failed");
  72. }
  73. mtx_destroy (&mutex);
  74. cnd_destroy (&cond);
  75. return 0;
  76. }
  77. #include <support/test-driver.c>