pthread_timechange_handler_np.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * pthread_timechange_handler_np.c
  3. *
  4. * Description:
  5. * This translation unit implements miscellaneous 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: jschmidlapp@users.sourceforge.net
  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: rpj@callisto.canberra.edu.au
  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 "pthread.h"
  43. #include "implement.h"
  44. /*
  45. * Notes on handling system time adjustments (especially negative ones).
  46. * ---------------------------------------------------------------------
  47. *
  48. * This solution was suggested by Alexander Terekhov, but any errors
  49. * in the implementation are mine - [Ross Johnson]
  50. *
  51. * 1) The problem: threads doing a timedwait on a CV may expect to timeout
  52. * at a specific absolute time according to a system timer. If the
  53. * system clock is adjusted backwards then those threads sleep longer than
  54. * expected. Also, pthreads-embedded converts absolute times to intervals in
  55. * order to make use of the underlying OS, and so waiting threads may
  56. * awake before their proper abstimes.
  57. *
  58. * 2) We aren't able to distinquish between threads on timed or untimed waits,
  59. * so we wake them all at the time of the adjustment so that they can
  60. * re-evaluate their conditions and re-compute their timeouts.
  61. *
  62. * 3) We rely on correctly written applications for this to work. Specifically,
  63. * they must be able to deal properly with spurious wakeups. That is,
  64. * they must re-test their condition upon wakeup and wait again if
  65. * the condition is not satisfied.
  66. */
  67. void *
  68. pthread_timechange_handler_np (void *arg)
  69. /*
  70. * ------------------------------------------------------
  71. * DOCPUBLIC
  72. * Broadcasts all CVs to force re-evaluation and
  73. * new timeouts if required.
  74. *
  75. * PARAMETERS
  76. * NONE
  77. *
  78. *
  79. * DESCRIPTION
  80. * Broadcasts all CVs to force re-evaluation and
  81. * new timeouts if required.
  82. *
  83. * This routine may be passed directly to pthread_create()
  84. * as a new thread in order to run asynchronously.
  85. *
  86. *
  87. * RESULTS
  88. * 0 successfully broadcast all CVs
  89. * EAGAIN Not all CVs were broadcast
  90. *
  91. * ------------------------------------------------------
  92. */
  93. {
  94. int result = 0;
  95. pthread_cond_t cv;
  96. pte_osMutexLock (pte_cond_list_lock);
  97. cv = pte_cond_list_head;
  98. while (cv != NULL && 0 == result)
  99. {
  100. result = pthread_cond_broadcast (&cv);
  101. cv = cv->next;
  102. }
  103. pte_osMutexUnlock(pte_cond_list_lock);
  104. return (void *) (result != 0 ? EAGAIN : 0);
  105. }