tst-cond3.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* Copyright (C) 2002 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  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 <pthread.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. /* Note that this test requires more than the standard. It is
  21. required that there are no spurious wakeups if only more readers
  22. are added. This is a reasonable demand. */
  23. static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  24. static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
  25. #define N 10
  26. static void *
  27. tf (void *arg)
  28. {
  29. int i = (long int) arg;
  30. int err;
  31. /* Get the mutex. */
  32. err = pthread_mutex_lock (&mut);
  33. if (err != 0)
  34. {
  35. printf ("child %d mutex_lock failed: %s\n", i, strerror (err));
  36. exit (1);
  37. }
  38. /* This call should never return. */
  39. pthread_cond_wait (&cond, &mut);
  40. /* We should never get here. */
  41. exit (1);
  42. return NULL;
  43. }
  44. static int
  45. do_test (void)
  46. {
  47. int err;
  48. int i;
  49. for (i = 0; i < N; ++i)
  50. {
  51. pthread_t th;
  52. if (i != 0)
  53. {
  54. /* Release the mutex. */
  55. err = pthread_mutex_unlock (&mut);
  56. if (err != 0)
  57. {
  58. printf ("mutex_unlock %d failed: %s\n", i, strerror (err));
  59. return 1;
  60. }
  61. }
  62. err = pthread_create (&th, NULL, tf, (void *) (long int) i);
  63. if (err != 0)
  64. {
  65. printf ("create %d failed: %s\n", i, strerror (err));
  66. return 1;
  67. }
  68. /* Get the mutex. */
  69. err = pthread_mutex_lock (&mut);
  70. if (err != 0)
  71. {
  72. printf ("mutex_lock %d failed: %s\n", i, strerror (err));
  73. return 1;
  74. }
  75. }
  76. /* Set an alarm for 1 second. The wrapper will expect this. */
  77. alarm (1);
  78. /* This call should never return. */
  79. pthread_cond_wait (&cond, &mut);
  80. puts ("cond_wait returned");
  81. return 1;
  82. }
  83. #define EXPECTED_SIGNAL SIGALRM
  84. #define TEST_FUNCTION do_test ()
  85. #include "../test-skeleton.c"