join4.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Test for pthread_join() returning return value from threads.
  3. *
  4. *
  5. * --------------------------------------------------------------------------
  6. *
  7. * Pthreads-embedded (PTE) - POSIX Threads Library for embedded systems
  8. * Copyright(C) 2008 Jason Schmidlapp
  9. *
  10. * Contact Email: [email protected]
  11. *
  12. * This library is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public
  14. * License as published by the Free Software Foundation; either
  15. * version 2 of the License, or (at your option) any later version.
  16. *
  17. * This library is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public
  23. * License along with this library in the file COPYING.LIB;
  24. * if not, write to the Free Software Foundation, Inc.,
  25. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  26. *
  27. * --------------------------------------------------------------------------
  28. *
  29. * Verifies that pthread_join can be cancelled.
  30. */
  31. #include "test.h"
  32. static pthread_t handle1;
  33. static pthread_t handle2;
  34. static int thread2_status;
  35. static void *
  36. thr1(void * arg)
  37. {
  38. pte_osThreadSleep(5000);
  39. return NULL;
  40. }
  41. static void *
  42. thr2(void * arg)
  43. {
  44. int result;
  45. assert(pthread_join(handle1,NULL) == 0);
  46. thread2_status = 1;
  47. return NULL;
  48. }
  49. int pthread_test_join4()
  50. {
  51. thread2_status = 0;
  52. assert(pthread_create(&handle1, NULL, thr1, NULL) == 0);
  53. assert(pthread_create(&handle2, NULL, thr2, NULL) == 0);
  54. /* Give some time for thread #1 to start sleeping and thread #2 to wait on thread #1 */
  55. pte_osThreadSleep(500);
  56. /* Cancel thread #2 (who is waiting on thread #1) */
  57. assert(pthread_cancel(handle2) == 0);
  58. /* Wait a short amount of time for cancellation to take effect */
  59. pte_osThreadSleep(500);
  60. /* Make sure that pthread_join exited after cancellation */
  61. assert(thread2_status == 1);
  62. assert(pthread_join(handle1, NULL) == 0);
  63. assert(pthread_join(handle2, NULL) == 0);
  64. /* Success. */
  65. return 0;
  66. }