tst-cond18.c 2.6 KB

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