| OLD | NEW | 
|    1 /* |    1 /* | 
|    2  *  Copyright 2004 The WebRTC Project Authors. All rights reserved. |    2  *  Copyright 2004 The WebRTC Project Authors. All rights reserved. | 
|    3  * |    3  * | 
|    4  *  Use of this source code is governed by a BSD-style license |    4  *  Use of this source code is governed by a BSD-style license | 
|    5  *  that can be found in the LICENSE file in the root of the source |    5  *  that can be found in the LICENSE file in the root of the source | 
|    6  *  tree. An additional intellectual property rights grant can be found |    6  *  tree. An additional intellectual property rights grant can be found | 
|    7  *  in the file PATENTS.  All contributing project authors may |    7  *  in the file PATENTS.  All contributing project authors may | 
|    8  *  be found in the AUTHORS file in the root of the source tree. |    8  *  be found in the AUTHORS file in the root of the source tree. | 
|    9  */ |    9  */ | 
|   10  |   10  | 
|   11 #include "webrtc/base/event.h" |   11 #include "webrtc/base/event.h" | 
|   12  |   12  | 
|   13 #if defined(WEBRTC_WIN) |   13 #if defined(WEBRTC_WIN) | 
|   14 #include <windows.h> |   14 #include <windows.h> | 
|   15 #elif defined(WEBRTC_POSIX) |   15 #elif defined(WEBRTC_POSIX) | 
|   16 #include <pthread.h> |   16 #include <pthread.h> | 
|   17 #include <sys/time.h> |   17 #include <sys/time.h> | 
|   18 #include <time.h> |   18 #include <time.h> | 
|   19 #else |   19 #else | 
|   20 #error "Must define either WEBRTC_WIN or WEBRTC_POSIX." |   20 #error "Must define either WEBRTC_WIN or WEBRTC_POSIX." | 
|   21 #endif |   21 #endif | 
|   22  |   22  | 
|   23 #include "webrtc/base/checks.h" |   23 #include "webrtc/base/checks.h" | 
|   24  |   24  | 
|   25 namespace rtc { |   25 namespace rtc { | 
|   26  |   26  | 
|   27 #if defined(WEBRTC_WIN) |   27 #if defined(WEBRTC_WIN) | 
|   28  |   28  | 
|   29 Event::Event(bool manual_reset, bool initially_signaled) { |   29 Event::Event(bool manual_reset, bool initially_signaled) { | 
|   30   event_handle_ = ::CreateEvent(NULL,                 // Security attributes. |   30   event_handle_ = ::CreateEvent(nullptr,  // Security attributes. | 
|   31                                 manual_reset, |   31                                 manual_reset, initially_signaled, | 
|   32                                 initially_signaled, |   32                                 nullptr);  // Name. | 
|   33                                 NULL);                // Name. |  | 
|   34   RTC_CHECK(event_handle_); |   33   RTC_CHECK(event_handle_); | 
|   35 } |   34 } | 
|   36  |   35  | 
|   37 Event::~Event() { |   36 Event::~Event() { | 
|   38   CloseHandle(event_handle_); |   37   CloseHandle(event_handle_); | 
|   39 } |   38 } | 
|   40  |   39  | 
|   41 void Event::Set() { |   40 void Event::Set() { | 
|   42   SetEvent(event_handle_); |   41   SetEvent(event_handle_); | 
|   43 } |   42 } | 
|   44  |   43  | 
|   45 void Event::Reset() { |   44 void Event::Reset() { | 
|   46   ResetEvent(event_handle_); |   45   ResetEvent(event_handle_); | 
|   47 } |   46 } | 
|   48  |   47  | 
|   49 bool Event::Wait(int milliseconds) { |   48 bool Event::Wait(int milliseconds) { | 
|   50   DWORD ms = (milliseconds == kForever) ? INFINITE : milliseconds; |   49   DWORD ms = (milliseconds == kForever) ? INFINITE : milliseconds; | 
|   51   return (WaitForSingleObject(event_handle_, ms) == WAIT_OBJECT_0); |   50   return (WaitForSingleObject(event_handle_, ms) == WAIT_OBJECT_0); | 
|   52 } |   51 } | 
|   53  |   52  | 
|   54 #elif defined(WEBRTC_POSIX) |   53 #elif defined(WEBRTC_POSIX) | 
|   55  |   54  | 
|   56 Event::Event(bool manual_reset, bool initially_signaled) |   55 Event::Event(bool manual_reset, bool initially_signaled) | 
|   57     : is_manual_reset_(manual_reset), |   56     : is_manual_reset_(manual_reset), | 
|   58       event_status_(initially_signaled) { |   57       event_status_(initially_signaled) { | 
|   59   RTC_CHECK(pthread_mutex_init(&event_mutex_, NULL) == 0); |   58   RTC_CHECK(pthread_mutex_init(&event_mutex_, nullptr) == 0); | 
|   60   RTC_CHECK(pthread_cond_init(&event_cond_, NULL) == 0); |   59   RTC_CHECK(pthread_cond_init(&event_cond_, nullptr) == 0); | 
|   61 } |   60 } | 
|   62  |   61  | 
|   63 Event::~Event() { |   62 Event::~Event() { | 
|   64   pthread_mutex_destroy(&event_mutex_); |   63   pthread_mutex_destroy(&event_mutex_); | 
|   65   pthread_cond_destroy(&event_cond_); |   64   pthread_cond_destroy(&event_cond_); | 
|   66 } |   65 } | 
|   67  |   66  | 
|   68 void Event::Set() { |   67 void Event::Set() { | 
|   69   pthread_mutex_lock(&event_mutex_); |   68   pthread_mutex_lock(&event_mutex_); | 
|   70   event_status_ = true; |   69   event_status_ = true; | 
| (...skipping 15 matching lines...) Expand all  Loading... | 
|   86     // Converting from seconds and microseconds (1e-6) plus |   85     // Converting from seconds and microseconds (1e-6) plus | 
|   87     // milliseconds (1e-3) to seconds and nanoseconds (1e-9). |   86     // milliseconds (1e-3) to seconds and nanoseconds (1e-9). | 
|   88  |   87  | 
|   89 #ifdef HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE |   88 #ifdef HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE | 
|   90     // Use relative time version, which tends to be more efficient for |   89     // Use relative time version, which tends to be more efficient for | 
|   91     // pthread implementations where provided (like on Android). |   90     // pthread implementations where provided (like on Android). | 
|   92     ts.tv_sec = milliseconds / 1000; |   91     ts.tv_sec = milliseconds / 1000; | 
|   93     ts.tv_nsec = (milliseconds % 1000) * 1000000; |   92     ts.tv_nsec = (milliseconds % 1000) * 1000000; | 
|   94 #else |   93 #else | 
|   95     struct timeval tv; |   94     struct timeval tv; | 
|   96     gettimeofday(&tv, NULL); |   95     gettimeofday(&tv, nullptr); | 
|   97  |   96  | 
|   98     ts.tv_sec = tv.tv_sec + (milliseconds / 1000); |   97     ts.tv_sec = tv.tv_sec + (milliseconds / 1000); | 
|   99     ts.tv_nsec = tv.tv_usec * 1000 + (milliseconds % 1000) * 1000000; |   98     ts.tv_nsec = tv.tv_usec * 1000 + (milliseconds % 1000) * 1000000; | 
|  100  |   99  | 
|  101     // Handle overflow. |  100     // Handle overflow. | 
|  102     if (ts.tv_nsec >= 1000000000) { |  101     if (ts.tv_nsec >= 1000000000) { | 
|  103       ts.tv_sec++; |  102       ts.tv_sec++; | 
|  104       ts.tv_nsec -= 1000000000; |  103       ts.tv_nsec -= 1000000000; | 
|  105     } |  104     } | 
|  106 #endif |  105 #endif | 
| (...skipping 21 matching lines...) Expand all  Loading... | 
|  128     event_status_ = false; |  127     event_status_ = false; | 
|  129  |  128  | 
|  130   pthread_mutex_unlock(&event_mutex_); |  129   pthread_mutex_unlock(&event_mutex_); | 
|  131  |  130  | 
|  132   return (error == 0); |  131   return (error == 0); | 
|  133 } |  132 } | 
|  134  |  133  | 
|  135 #endif |  134 #endif | 
|  136  |  135  | 
|  137 }  // namespace rtc |  136 }  // namespace rtc | 
| OLD | NEW |