semaphore6.c 2.7 KB

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