pthread_timechange_handler_np.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * pthread_timechange_handler_np.c
  3. *
  4. * Description:
  5. * This translation unit implements miscellaneous thread functions.
  6. *
  7. * --------------------------------------------------------------------------
  8. *
  9. * Pthreads-win32 - POSIX Threads Library for Win32
  10. * Copyright(C) 1998 John E. Bossom
  11. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  12. *
  13. * Contact Email: [email protected]
  14. *
  15. * The current list of contributors is contained
  16. * in the file CONTRIBUTORS included with the source
  17. * code distribution. The list can also be seen at the
  18. * following World Wide Web location:
  19. * http://sources.redhat.com/pthreads-win32/contributors.html
  20. *
  21. * This library is free software; you can redistribute it and/or
  22. * modify it under the terms of the GNU Lesser General Public
  23. * License as published by the Free Software Foundation; either
  24. * version 2 of the License, or (at your option) any later version.
  25. *
  26. * This library is distributed in the hope that it will be useful,
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  29. * Lesser General Public License for more details.
  30. *
  31. * You should have received a copy of the GNU Lesser General Public
  32. * License along with this library in the file COPYING.LIB;
  33. * if not, write to the Free Software Foundation, Inc.,
  34. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  35. */
  36. #include "pthread.h"
  37. #include "implement.h"
  38. /*
  39. * Notes on handling system time adjustments (especially negative ones).
  40. * ---------------------------------------------------------------------
  41. *
  42. * This solution was suggested by Alexander Terekhov, but any errors
  43. * in the implementation are mine - [Ross Johnson]
  44. *
  45. * 1) The problem: threads doing a timedwait on a CV may expect to timeout
  46. * at a specific absolute time according to a system timer. If the
  47. * system clock is adjusted backwards then those threads sleep longer than
  48. * expected. Also, pthreads-win32 converts absolute times to intervals in
  49. * order to make use of the underlying Win32, and so waiting threads may
  50. * awake before their proper abstimes.
  51. *
  52. * 2) We aren't able to distinquish between threads on timed or untimed waits,
  53. * so we wake them all at the time of the adjustment so that they can
  54. * re-evaluate their conditions and re-compute their timeouts.
  55. *
  56. * 3) We rely on correctly written applications for this to work. Specifically,
  57. * they must be able to deal properly with spurious wakeups. That is,
  58. * they must re-test their condition upon wakeup and wait again if
  59. * the condition is not satisfied.
  60. */
  61. void *
  62. pthread_timechange_handler_np (void *arg)
  63. /*
  64. * ------------------------------------------------------
  65. * DOCPUBLIC
  66. * Broadcasts all CVs to force re-evaluation and
  67. * new timeouts if required.
  68. *
  69. * PARAMETERS
  70. * NONE
  71. *
  72. *
  73. * DESCRIPTION
  74. * Broadcasts all CVs to force re-evaluation and
  75. * new timeouts if required.
  76. *
  77. * This routine may be passed directly to pthread_create()
  78. * as a new thread in order to run asynchronously.
  79. *
  80. *
  81. * RESULTS
  82. * 0 successfully broadcast all CVs
  83. * EAGAIN Not all CVs were broadcast
  84. *
  85. * ------------------------------------------------------
  86. */
  87. {
  88. int result = 0;
  89. pthread_cond_t cv;
  90. ptw32_mcs_local_node_t node;
  91. ptw32_mcs_lock_acquire(&ptw32_cond_list_lock, &node);
  92. cv = ptw32_cond_list_head;
  93. while (cv != NULL && 0 == result)
  94. {
  95. result = pthread_cond_broadcast (&cv);
  96. cv = cv->next;
  97. }
  98. ptw32_mcs_lock_release(&node);
  99. return (void *) (size_t) (result != 0 ? EAGAIN : 0);
  100. }