blob: 65f9a45ba27659e1b38282fc4bdfa8ef0fd56bf3 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3 *
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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
ajm@google.comb5c49ff2011-08-01 17:04:04 +000011#include "condition_variable_posix.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
13#if defined(WEBRTC_LINUX)
14#include <ctime>
15#else
16#include <sys/time.h>
17#endif
18
19#include <errno.h>
20
ajm@google.comb5c49ff2011-08-01 17:04:04 +000021#include "critical_section_posix.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000022
23namespace webrtc {
ajm@google.comb5c49ff2011-08-01 17:04:04 +000024ConditionVariableWrapper* ConditionVariablePosix::Create()
niklase@google.com470e71d2011-07-07 08:21:25 +000025{
ajm@google.comb5c49ff2011-08-01 17:04:04 +000026 ConditionVariablePosix* ptr = new ConditionVariablePosix;
niklase@google.com470e71d2011-07-07 08:21:25 +000027 if (!ptr)
28 {
29 return NULL;
30 }
31
32 const int error = ptr->Construct();
33 if (error)
34 {
35 delete ptr;
36 return NULL;
37 }
38
39 return ptr;
40}
41
ajm@google.comb5c49ff2011-08-01 17:04:04 +000042ConditionVariablePosix::ConditionVariablePosix()
niklase@google.com470e71d2011-07-07 08:21:25 +000043{
44}
45
ajm@google.comb5c49ff2011-08-01 17:04:04 +000046int ConditionVariablePosix::Construct()
niklase@google.com470e71d2011-07-07 08:21:25 +000047{
niklase@google.com470e71d2011-07-07 08:21:25 +000048#ifdef WEBRTC_CLOCK_TYPE_REALTIME
kma@webrtc.org3c1f96f2012-09-06 16:20:03 +000049 pthread_cond_init(&_cond, NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +000050#else
kma@webrtc.org3c1f96f2012-09-06 16:20:03 +000051 int result = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000052 pthread_condattr_t condAttr;
53 result = pthread_condattr_init(&condAttr);
54 if (result != 0)
55 {
56 return -1;
57 }
58 result = pthread_condattr_setclock(&condAttr, CLOCK_MONOTONIC);
59 if (result != 0)
60 {
61 return -1;
62 }
63 result = pthread_cond_init(&_cond, &condAttr);
64 if (result != 0)
65 {
66 return -1;
67 }
68 result = pthread_condattr_destroy(&condAttr);
69 if (result != 0)
70 {
71 return -1;
72 }
73#endif
74 return 0;
75}
76
ajm@google.comb5c49ff2011-08-01 17:04:04 +000077ConditionVariablePosix::~ConditionVariablePosix()
niklase@google.com470e71d2011-07-07 08:21:25 +000078{
79 pthread_cond_destroy(&_cond);
80}
81
ajm@google.comb5c49ff2011-08-01 17:04:04 +000082void ConditionVariablePosix::SleepCS(CriticalSectionWrapper& critSect)
niklase@google.com470e71d2011-07-07 08:21:25 +000083{
ajm@google.comb5c49ff2011-08-01 17:04:04 +000084 CriticalSectionPosix* cs = reinterpret_cast<CriticalSectionPosix*>(
niklase@google.com470e71d2011-07-07 08:21:25 +000085 &critSect);
86 pthread_cond_wait(&_cond, &cs->_mutex);
87}
88
89
90bool
ajm@google.comb5c49ff2011-08-01 17:04:04 +000091ConditionVariablePosix::SleepCS(
niklase@google.com470e71d2011-07-07 08:21:25 +000092 CriticalSectionWrapper& critSect,
93 unsigned long maxTimeInMS)
94{
95 const unsigned long INFINITE = 0xFFFFFFFF;
96
97 const int MILLISECONDS_PER_SECOND = 1000;
98#ifndef WEBRTC_LINUX
99 const int MICROSECONDS_PER_MILLISECOND = 1000;
100#endif
101 const int NANOSECONDS_PER_SECOND = 1000000000;
102 const int NANOSECONDS_PER_MILLISECOND = 1000000;
103
ajm@google.comb5c49ff2011-08-01 17:04:04 +0000104 CriticalSectionPosix* cs = reinterpret_cast<CriticalSectionPosix*>(
niklase@google.com470e71d2011-07-07 08:21:25 +0000105 &critSect);
106
107 if (maxTimeInMS != INFINITE)
108 {
109 timespec ts;
110#ifndef WEBRTC_MAC
111#ifdef WEBRTC_CLOCK_TYPE_REALTIME
112 clock_gettime(CLOCK_REALTIME, &ts);
113#else
114 clock_gettime(CLOCK_MONOTONIC, &ts);
115#endif
116#else
117 struct timeval tv;
118 gettimeofday(&tv, 0);
119 ts.tv_sec = tv.tv_sec;
120 ts.tv_nsec = tv.tv_usec * MICROSECONDS_PER_MILLISECOND;
121#endif
122
123 ts.tv_sec += maxTimeInMS / MILLISECONDS_PER_SECOND;
124 ts.tv_nsec += (maxTimeInMS - ((maxTimeInMS / MILLISECONDS_PER_SECOND)*
125 MILLISECONDS_PER_SECOND)) * NANOSECONDS_PER_MILLISECOND;
126
127 if (ts.tv_nsec >= NANOSECONDS_PER_SECOND)
128 {
129 ts.tv_sec += ts.tv_nsec / NANOSECONDS_PER_SECOND;
130 ts.tv_nsec %= NANOSECONDS_PER_SECOND;
131 }
132 const int res = pthread_cond_timedwait(&_cond, &cs->_mutex, &ts);
133 return (res == ETIMEDOUT) ? false : true;
134 }
135 else
136 {
137 pthread_cond_wait(&_cond, &cs->_mutex);
138 return true;
139 }
140}
141
ajm@google.comb5c49ff2011-08-01 17:04:04 +0000142void ConditionVariablePosix::Wake()
niklase@google.com470e71d2011-07-07 08:21:25 +0000143{
144 pthread_cond_signal(&_cond);
145}
146
ajm@google.comb5c49ff2011-08-01 17:04:04 +0000147void ConditionVariablePosix::WakeAll()
niklase@google.com470e71d2011-07-07 08:21:25 +0000148{
149 pthread_cond_broadcast(&_cond);
150}
151} // namespace webrtc