tst-cond3.c 2.6 KB

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