You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
4.1 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2008, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "talk/base/event.h"
  28. #if defined(WIN32)
  29. #include <windows.h>
  30. #elif defined(POSIX)
  31. #include <pthread.h>
  32. #include <sys/time.h>
  33. #include <time.h>
  34. #else
  35. #error "Must define either WIN32 or POSIX."
  36. #endif
  37. namespace talk_base {
  38. #if defined(WIN32)
  39. Event::Event(bool manual_reset, bool initially_signaled)
  40. : is_manual_reset_(manual_reset),
  41. is_initially_signaled_(initially_signaled) {
  42. event_handle_ = ::CreateEvent(NULL, // Security attributes.
  43. is_manual_reset_,
  44. is_initially_signaled_,
  45. NULL); // Name.
  46. ASSERT(event_handle_ != NULL);
  47. }
  48. Event::~Event() {
  49. CloseHandle(event_handle_);
  50. }
  51. void Event::Set() {
  52. SetEvent(event_handle_);
  53. }
  54. void Event::Reset() {
  55. ResetEvent(event_handle_);
  56. }
  57. bool Event::Wait(int cms) {
  58. DWORD ms = (cms == kForever)? INFINITE : cms;
  59. return (WaitForSingleObject(event_handle_, ms) == WAIT_OBJECT_0);
  60. }
  61. #elif defined(POSIX)
  62. Event::Event(bool manual_reset, bool initially_signaled)
  63. : is_manual_reset_(manual_reset),
  64. event_status_(initially_signaled) {
  65. VERIFY(pthread_mutex_init(&event_mutex_, NULL) == 0);
  66. VERIFY(pthread_cond_init(&event_cond_, NULL) == 0);
  67. }
  68. Event::~Event() {
  69. pthread_mutex_destroy(&event_mutex_);
  70. pthread_cond_destroy(&event_cond_);
  71. }
  72. void Event::Set() {
  73. pthread_mutex_lock(&event_mutex_);
  74. event_status_ = true;
  75. pthread_cond_broadcast(&event_cond_);
  76. pthread_mutex_unlock(&event_mutex_);
  77. }
  78. void Event::Reset() {
  79. pthread_mutex_lock(&event_mutex_);
  80. event_status_ = false;
  81. pthread_mutex_unlock(&event_mutex_);
  82. }
  83. bool Event::Wait(int cms) {
  84. pthread_mutex_lock(&event_mutex_);
  85. int error = 0;
  86. if (cms != kForever) {
  87. // Converting from seconds and microseconds (1e-6) plus
  88. // milliseconds (1e-3) to seconds and nanoseconds (1e-9).
  89. struct timeval tv;
  90. gettimeofday(&tv, NULL);
  91. struct timespec ts;
  92. ts.tv_sec = tv.tv_sec + (cms / 1000);
  93. ts.tv_nsec = tv.tv_usec * 1000 + (cms % 1000) * 1000000;
  94. // Handle overflow.
  95. if (ts.tv_nsec >= 1000000000) {
  96. ts.tv_sec++;
  97. ts.tv_nsec -= 1000000000;
  98. }
  99. while (!event_status_ && error == 0)
  100. error = pthread_cond_timedwait(&event_cond_, &event_mutex_, &ts);
  101. } else {
  102. while (!event_status_ && error == 0)
  103. error = pthread_cond_wait(&event_cond_, &event_mutex_);
  104. }
  105. // NOTE(liulk): Exactly one thread will auto-reset this event. All
  106. // the other threads will think it's unsignaled. This seems to be
  107. // consistent with auto-reset events in WIN32.
  108. if (error == 0 && !is_manual_reset_)
  109. event_status_ = false;
  110. pthread_mutex_unlock(&event_mutex_);
  111. return (error == 0);
  112. }
  113. #endif
  114. } // namespace talk_base