semaphore5.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * File: semaphore5.c
  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. * Test Synopsis: Verifies that sem_timedwait returns after timeout.
  30. * -
  31. *
  32. * Test Method (Validation or Falsification):
  33. * - Validation
  34. *
  35. * Requirements Tested:
  36. * -
  37. *
  38. * Features Tested:
  39. * -
  40. *
  41. * Cases Tested:
  42. * -
  43. *
  44. * Description:
  45. * -
  46. *
  47. * Environment:
  48. * -
  49. *
  50. * Input:
  51. * - None.
  52. *
  53. * Output:
  54. * - File name, Line number, and failed expression on failure.
  55. * - No output on success.
  56. *
  57. * Assumptions:
  58. * -
  59. *
  60. * Pass Criteria:
  61. * - Process returns zero exit status.
  62. *
  63. * Fail Criteria:
  64. * - Process returns non-zero exit status.
  65. */
  66. #include "test.h"
  67. static sem_t s;
  68. static void *
  69. thr (void * arg)
  70. {
  71. struct timespec abstime =
  72. {
  73. 0, 0
  74. };
  75. struct _timeb currSysTime;
  76. const long long NANOSEC_PER_MILLISEC = 1000000;
  77. _ftime(&currSysTime);
  78. abstime.tv_sec = currSysTime.time;
  79. abstime.tv_nsec = NANOSEC_PER_MILLISEC * currSysTime.millitm;
  80. abstime.tv_sec += 5;
  81. assert(sem_timedwait(&s, &abstime) == -1);
  82. assert(errno == ETIMEDOUT);
  83. return NULL;
  84. }
  85. int pthread_test_semaphore5(void)
  86. {
  87. int value = 0;
  88. pthread_t handle;
  89. assert(sem_init(&s, PTHREAD_PROCESS_PRIVATE, 0) == 0);
  90. assert(sem_getvalue(&s, &value) == 0);
  91. assert(value == 0);
  92. assert(pthread_create(&handle, NULL, thr, NULL) == 0);
  93. assert(pthread_join(handle, NULL) == 0);
  94. assert(sem_destroy(&s) == 0);
  95. return 0;
  96. }