pthread_delay_np.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * pthreads_delay_np.c
  3. *
  4. * Description:
  5. * This translation unit implements non-portable thread functions.
  6. *
  7. * --------------------------------------------------------------------------
  8. *
  9. * Pthreads-embedded (PTE) - POSIX Threads Library for embedded systems
  10. * Copyright(C) 2008 Jason Schmidlapp
  11. *
  12. * Contact Email: [email protected]
  13. *
  14. *
  15. * Based upon Pthreads-win32 - POSIX Threads Library for Win32
  16. * Copyright(C) 1998 John E. Bossom
  17. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  18. *
  19. * Contact Email: [email protected]
  20. *
  21. * The original list of contributors to the Pthreads-win32 project
  22. * is contained in the file CONTRIBUTORS.ptw32 included with the
  23. * source code distribution. The list can also be seen at the
  24. * following World Wide Web location:
  25. * http://sources.redhat.com/pthreads-win32/contributors.html
  26. *
  27. * This library is free software; you can redistribute it and/or
  28. * modify it under the terms of the GNU Lesser General Public
  29. * License as published by the Free Software Foundation; either
  30. * version 2 of the License, or (at your option) any later version.
  31. *
  32. * This library is distributed in the hope that it will be useful,
  33. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  34. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  35. * Lesser General Public License for more details.
  36. *
  37. * You should have received a copy of the GNU Lesser General Public
  38. * License along with this library in the file COPYING.LIB;
  39. * if not, write to the Free Software Foundation, Inc.,
  40. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  41. */
  42. #include <stdio.h>
  43. #include "pthread.h"
  44. #include "implement.h"
  45. /*
  46. * pthread_delay_np
  47. *
  48. * DESCRIPTION
  49. *
  50. * This routine causes a thread to delay execution for a specific period of time.
  51. * This period ends at the current time plus the specified interval. The routine
  52. * will not return before the end of the period is reached, but may return an
  53. * arbitrary amount of time after the period has gone by. This can be due to
  54. * system load, thread priorities, and system timer granularity.
  55. *
  56. * Specifying an interval of zero (0) seconds and zero (0) nanoseconds is
  57. * allowed and can be used to force the thread to give up the processor or to
  58. * deliver a pending cancelation request.
  59. *
  60. * The timespec structure contains the following two fields:
  61. *
  62. * tv_sec is an integer number of seconds.
  63. * tv_nsec is an integer number of nanoseconds.
  64. *
  65. * Return Values
  66. *
  67. * If an error condition occurs, this routine returns an integer value indicating
  68. * the type of error. Possible return values are as follows:
  69. *
  70. * 0
  71. * Successful completion.
  72. * [EINVAL]
  73. * The value specified by interval is invalid.
  74. *
  75. * Example
  76. *
  77. * The following code segment would wait for 5 and 1/2 seconds
  78. *
  79. * struct timespec tsWait;
  80. * int intRC;
  81. *
  82. * tsWait.tv_sec = 5;
  83. * tsWait.tv_nsec = 500000000L;
  84. * intRC = pthread_delay_np(&tsWait);
  85. */
  86. int
  87. pthread_delay_np (struct timespec *interval)
  88. {
  89. unsigned int wait_time;
  90. unsigned int secs_in_millisecs;
  91. unsigned int millisecs;
  92. pthread_t self;
  93. pte_thread_t * sp;
  94. if (interval == NULL)
  95. {
  96. return EINVAL;
  97. }
  98. if (interval->tv_sec == 0L && interval->tv_nsec == 0L)
  99. {
  100. pthread_testcancel ();
  101. pte_osThreadSleep (1);
  102. pthread_testcancel ();
  103. return (0);
  104. }
  105. /* convert secs to millisecs */
  106. secs_in_millisecs = interval->tv_sec * 1000L;
  107. /* convert nanosecs to millisecs (rounding up) */
  108. millisecs = (interval->tv_nsec + 999999L) / 1000000L;
  109. wait_time = secs_in_millisecs + millisecs;
  110. if (NULL == (self = pthread_self ()))
  111. {
  112. return ENOMEM;
  113. }
  114. sp = (pte_thread_t *) self;
  115. if (sp->cancelState == PTHREAD_CANCEL_ENABLE)
  116. {
  117. pte_osResult cancelStatus;
  118. /*
  119. * Async cancelation won't catch us until wait_time is up.
  120. * Deferred cancelation will cancel us immediately.
  121. */
  122. cancelStatus = pte_osThreadCheckCancel(sp->threadId);
  123. if (cancelStatus == PTE_OS_INTERRUPTED)
  124. {
  125. /*
  126. * Canceling!
  127. */
  128. (void) pthread_mutex_lock (&sp->cancelLock);
  129. if (sp->state < PThreadStateCanceling)
  130. {
  131. sp->state = PThreadStateCanceling;
  132. sp->cancelState = PTHREAD_CANCEL_DISABLE;
  133. (void) pthread_mutex_unlock (&sp->cancelLock);
  134. pte_throw (PTE_EPS_CANCEL);
  135. }
  136. (void) pthread_mutex_unlock (&sp->cancelLock);
  137. return ESRCH;
  138. }
  139. else if (cancelStatus != PTE_OS_OK)
  140. {
  141. return EINVAL;
  142. }
  143. }
  144. else
  145. {
  146. pte_osThreadSleep (wait_time);
  147. }
  148. return (0);
  149. }