tst-cond1.c 2.3 KB

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