tst-cond16.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 <pthread.h>
  18. #include <stdbool.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
  23. pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
  24. bool n, exiting;
  25. FILE *f;
  26. int count;
  27. void *
  28. tf (void *dummy)
  29. {
  30. bool loop = true;
  31. while (loop)
  32. {
  33. pthread_mutex_lock (&lock);
  34. while (n && !exiting)
  35. pthread_cond_wait (&cv, &lock);
  36. n = true;
  37. pthread_mutex_unlock (&lock);
  38. fputs (".", f);
  39. pthread_mutex_lock (&lock);
  40. n = false;
  41. if (exiting)
  42. loop = false;
  43. #ifdef UNLOCK_AFTER_BROADCAST
  44. pthread_cond_broadcast (&cv);
  45. pthread_mutex_unlock (&lock);
  46. #else
  47. pthread_mutex_unlock (&lock);
  48. pthread_cond_broadcast (&cv);
  49. #endif
  50. }
  51. return NULL;
  52. }
  53. int
  54. do_test (void)
  55. {
  56. f = fopen ("/dev/null", "w");
  57. if (f == NULL)
  58. {
  59. printf ("couldn't open /dev/null, %m\n");
  60. return 1;
  61. }
  62. count = sysconf (_SC_NPROCESSORS_ONLN);
  63. if (count <= 0)
  64. count = 1;
  65. count *= 4;
  66. pthread_t th[count];
  67. int i, ret;
  68. for (i = 0; i < count; ++i)
  69. if ((ret = pthread_create (&th[i], NULL, tf, NULL)) != 0)
  70. {
  71. errno = ret;
  72. printf ("pthread_create %d failed: %m\n", i);
  73. return 1;
  74. }
  75. struct timespec ts = { .tv_sec = 20, .tv_nsec = 0 };
  76. while (nanosleep (&ts, &ts) != 0);
  77. pthread_mutex_lock (&lock);
  78. exiting = true;
  79. pthread_mutex_unlock (&lock);
  80. for (i = 0; i < count; ++i)
  81. pthread_join (th[i], NULL);
  82. fclose (f);
  83. return 0;
  84. }
  85. #define TEST_FUNCTION do_test ()
  86. #define TIMEOUT 40
  87. #include "../test-skeleton.c"