tst-cond1.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 <error.h>
  16. #include <pthread.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  20. static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
  21. static void *
  22. tf (void *p)
  23. {
  24. int err;
  25. err = pthread_mutex_lock (&mut);
  26. if (err != 0)
  27. error (EXIT_FAILURE, err, "child: cannot get mutex");
  28. puts ("child: got mutex; signalling");
  29. pthread_cond_signal (&cond);
  30. puts ("child: unlock");
  31. err = pthread_mutex_unlock (&mut);
  32. if (err != 0)
  33. error (EXIT_FAILURE, err, "child: cannot unlock");
  34. puts ("child: done");
  35. return NULL;
  36. }
  37. static int
  38. do_test (void)
  39. {
  40. pthread_t th;
  41. int err;
  42. printf ("&cond = %p\n&mut = %p\n", &cond, &mut);
  43. puts ("parent: get mutex");
  44. err = pthread_mutex_lock (&mut);
  45. if (err != 0)
  46. error (EXIT_FAILURE, err, "parent: cannot get mutex");
  47. puts ("parent: create child");
  48. err = pthread_create (&th, NULL, tf, NULL);
  49. if (err != 0)
  50. error (EXIT_FAILURE, err, "parent: cannot create thread");
  51. puts ("parent: wait for condition");
  52. err = pthread_cond_wait (&cond, &mut);
  53. if (err != 0)
  54. error (EXIT_FAILURE, err, "parent: cannot wait fir signal");
  55. puts ("parent: got signal");
  56. err = pthread_join (th, NULL);
  57. if (err != 0)
  58. error (EXIT_FAILURE, err, "parent: failed to join");
  59. puts ("done");
  60. return 0;
  61. }
  62. #define TEST_FUNCTION do_test ()
  63. #include "../test-skeleton.c"