pte_cancellable_wait.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * pte_cancellable_wait.c
  3. *
  4. * Description:
  5. *
  6. * --------------------------------------------------------------------------
  7. *
  8. * Pthreads-embedded (PTE) - POSIX Threads Library for embedded systems
  9. * Copyright(C) 2008 Jason Schmidlapp
  10. *
  11. * Contact Email: [email protected]
  12. *
  13. *
  14. * Based upon Pthreads-win32 - POSIX Threads Library for Win32
  15. * Copyright(C) 1998 John E. Bossom
  16. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  17. *
  18. * Contact Email: [email protected]
  19. *
  20. * The original list of contributors to the Pthreads-win32 project
  21. * is contained in the file CONTRIBUTORS.ptw32 included with the
  22. * source code distribution. The list can also be seen at the
  23. * following World Wide Web location:
  24. * http://sources.redhat.com/pthreads-win32/contributors.html
  25. *
  26. * This library is free software; you can redistribute it and/or
  27. * modify it under the terms of the GNU Lesser General Public
  28. * License as published by the Free Software Foundation; either
  29. * version 2 of the License, or (at your option) any later version.
  30. *
  31. * This library is distributed in the hope that it will be useful,
  32. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  34. * Lesser General Public License for more details.
  35. *
  36. * You should have received a copy of the GNU Lesser General Public
  37. * License along with this library in the file COPYING.LIB;
  38. * if not, write to the Free Software Foundation, Inc.,
  39. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  40. */
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include "pthread.h"
  44. #include "semaphore.h"
  45. #include "implement.h"
  46. int pte_cancellable_wait (pte_osSemaphoreHandle semHandle, unsigned int* timeout)
  47. {
  48. int result = EINVAL;
  49. pte_osResult osResult;
  50. int cancelEnabled = 0;
  51. pthread_t self;
  52. pte_thread_t * sp;
  53. self = pthread_self();
  54. sp = (pte_thread_t *) self;
  55. if (sp != NULL)
  56. {
  57. /*
  58. * Get cancelEvent handle
  59. */
  60. if (sp->cancelState == PTHREAD_CANCEL_ENABLE)
  61. {
  62. cancelEnabled = 1;
  63. }
  64. }
  65. if (cancelEnabled)
  66. {
  67. osResult = pte_osSemaphoreCancellablePend(semHandle, timeout);
  68. }
  69. else
  70. {
  71. osResult = pte_osSemaphorePend(semHandle, timeout);
  72. }
  73. switch (osResult)
  74. {
  75. case PTE_OS_OK:
  76. {
  77. result = 0;
  78. break;
  79. }
  80. case PTE_OS_TIMEOUT:
  81. {
  82. result = ETIMEDOUT;
  83. break;
  84. }
  85. case PTE_OS_INTERRUPTED:
  86. {
  87. if (sp != NULL)
  88. {
  89. /*
  90. * Should handle POSIX and implicit POSIX threads..
  91. * Make sure we haven't been async-canceled in the meantime.
  92. */
  93. (void) pthread_mutex_lock (&sp->cancelLock);
  94. if (sp->state < PThreadStateCanceling)
  95. {
  96. sp->state = PThreadStateCanceling;
  97. sp->cancelState = PTHREAD_CANCEL_DISABLE;
  98. (void) pthread_mutex_unlock (&sp->cancelLock);
  99. pte_throw (PTE_EPS_CANCEL);
  100. /* Never reached */
  101. }
  102. (void) pthread_mutex_unlock (&sp->cancelLock);
  103. }
  104. break;
  105. }
  106. default:
  107. {
  108. result = EINVAL;
  109. }
  110. }
  111. return (result);
  112. } /* CancelableWait */