tst-cond18.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* Copyright (C) 2004 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Jakub Jelinek <jakub@redhat.com>, 2004.
  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 <errno.h>
  16. #include <fcntl.h>
  17. #include <pthread.h>
  18. #include <stdbool.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <unistd.h>
  22. pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
  23. pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
  24. bool exiting;
  25. int fd, count, spins, nn;
  26. void *
  27. tf (void *id)
  28. {
  29. pthread_mutex_lock (&lock);
  30. if ((long) id == 0)
  31. {
  32. while (!exiting)
  33. {
  34. if ((spins++ % 1000) == 0)
  35. write (fd, ".", 1);
  36. pthread_mutex_unlock (&lock);
  37. pthread_mutex_lock (&lock);
  38. int njobs = rand () % (count + 1);
  39. nn = njobs;
  40. if ((rand () % 30) == 0)
  41. pthread_cond_broadcast (&cv);
  42. else
  43. while (njobs--)
  44. pthread_cond_signal (&cv);
  45. }
  46. pthread_cond_broadcast (&cv);
  47. }
  48. else
  49. {
  50. while (!exiting)
  51. {
  52. while (!nn && !exiting)
  53. pthread_cond_wait (&cv, &lock);
  54. --nn;
  55. pthread_mutex_unlock (&lock);
  56. pthread_mutex_lock (&lock);
  57. }
  58. }
  59. pthread_mutex_unlock (&lock);
  60. return NULL;
  61. }
  62. int
  63. do_test (void)
  64. {
  65. fd = open ("/dev/null", O_WRONLY);
  66. if (fd < 0)
  67. {
  68. printf ("couldn't open /dev/null, %m\n");
  69. return 1;
  70. }
  71. count = sysconf (_SC_NPROCESSORS_ONLN);
  72. if (count <= 0)
  73. count = 1;
  74. count *= 8;
  75. pthread_t th[count + 1];
  76. int i, ret;
  77. for (i = 0; i <= count; ++i)
  78. if ((ret = pthread_create (&th[i], NULL, tf, (void *) (long) i)) != 0)
  79. {
  80. errno = ret;
  81. printf ("pthread_create %d failed: %m\n", i);
  82. return 1;
  83. }
  84. struct timespec ts = { .tv_sec = 20, .tv_nsec = 0 };
  85. while (nanosleep (&ts, &ts) != 0);
  86. pthread_mutex_lock (&lock);
  87. exiting = true;
  88. pthread_mutex_unlock (&lock);
  89. for (i = 0; i < count; ++i)
  90. pthread_join (th[i], NULL);
  91. close (fd);
  92. return 0;
  93. }
  94. #define TEST_FUNCTION do_test ()
  95. #define TIMEOUT 40
  96. #include "../test-skeleton.c"