RDK Documentation (Open Sourced RDK Components)
ConditionVariable.cpp
1 /*
2  * If not stated otherwise in this file or this component's Licenses.txt file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2016 RDK Management
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18 */
19 
20 
21 
22 /**
23 * @defgroup hdmicec
24 * @{
25 * @defgroup osal
26 * @{
27 **/
28 
29 
30 #include <pthread.h>
31 #include <stdlib.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <sys/time.h>
35 
37 #include "osal/Condition.hpp"
38 #include "osal/Mutex.hpp"
39 #include "osal/Util.hpp"
40 
41 CCEC_OSAL_BEGIN_NAMESPACE
42 
43 ConditionVariable::ConditionVariable() : cond(NULL), mutex(NULL), nativeHandle(NULL)
44 {
45  cond = new Condition(false);
46  mutex = new Mutex();
47  nativeHandle = Malloc(sizeof(pthread_cond_t));
48  pthread_cond_init( (pthread_cond_t *) nativeHandle, NULL );
49 }
50 
52 {
53  if (nativeHandle != NULL) {
54  pthread_cond_destroy((pthread_cond_t *) nativeHandle);
55  Free(nativeHandle);
56  }
57  delete mutex;
58  delete cond;
59 }
60 
62 {
63  mutex->lock();
64  cond->set();
65  mutex->unlock();
66 }
67 
69 {
70  mutex->lock();
71  cond->reset();
72  mutex->unlock();
73 }
74 
76 {
77  mutex->lock();
78  bool set = cond->isSet();
79  mutex->unlock();
80  return set;
81 }
82 
84 {
85  wait(0);
86 }
87 
88 long ConditionVariable::wait(long timeout)
89 {
90  long timeLeft = 1;
91  mutex->lock();
92  if (!cond->isSet()) {
93  int ret = 0;
94  if (timeout == 0) {
95  ret = pthread_cond_wait((pthread_cond_t *)nativeHandle,
96  (pthread_mutex_t *)mutex->getNativeHandle());
97  if (ret < 0) {
98  /* @TODO Throw Exception */
99  }
100  }
101  else {
102  struct timeval curTime;
103  memset(&curTime, 0, sizeof(curTime));
104  gettimeofday(&curTime, NULL);
105 
106  struct timespec wakeTime;
107  memset(&wakeTime, 0, sizeof(wakeTime));
108  wakeTime.tv_nsec = curTime.tv_usec * 1000 + (timeout % 1000) * 1000000;
109  wakeTime.tv_sec = curTime.tv_sec + (timeout / 1000);
110  if (wakeTime.tv_nsec > 1000000000)
111  {
112  wakeTime.tv_nsec -= 1000000000;
113  wakeTime.tv_sec++;
114  }
115 
116  ret = pthread_cond_timedwait((pthread_cond_t *)nativeHandle,
117  (pthread_mutex_t *)mutex->getNativeHandle(),
118  &wakeTime);
119 
120  if ((ret != 0) && !cond->isSet() && ret == ETIMEDOUT) {
121  timeLeft = 0;
122  }
123  else {
124  /* @TODO: Handle wait interruption. Return actual left over time */
125  timeLeft = 1;
126  }
127 
128  }
129  }
130  mutex->unlock();
131 
132  return timeLeft;
133 }
134 
135 void ConditionVariable::notify(void)
136 {
137  mutex->lock();
138  cond->set();
139  pthread_cond_signal((pthread_cond_t *)nativeHandle);
140  mutex->unlock();
141 }
142 
143 void ConditionVariable::notifyAll(void)
144 {
145  mutex->lock();
146  cond->set();
147  pthread_cond_broadcast((pthread_cond_t *)nativeHandle);
148  mutex->unlock();
149 }
150 
151 void * ConditionVariable::getNativeHandle(void)
152 {
153  return nativeHandle;
154 }
155 
156 CCEC_OSAL_END_NAMESPACE
157 
158 
159 /** @} */
160 /** @} */
CCEC_OSAL::Condition::isSet
virtual bool isSet(void)
Check the state of the object. Returns the state of the object, which will be either true/false.
Definition: Condition.hpp:103
CCEC_OSAL::Mutex::lock
void lock(void)
Locks the given mutex.
Definition: Mutex.cpp:73
CCEC_OSAL::ConditionVariable::wait
void wait(void)
Wait until the conditional variable is signalled. Causes the current thread to wait until it is signa...
Definition: ConditionVariable.cpp:83
CCEC_OSAL::ConditionVariable::~ConditionVariable
~ConditionVariable(void)
Destructor. Destroys the ConditionVariable object.
Definition: ConditionVariable.cpp:51
CCEC_OSAL::Mutex::getNativeHandle
void * getNativeHandle(void)
Retrieves handle to the underlying native mutex implementation.
Definition: Mutex.cpp:83
CCEC_OSAL::Condition::set
virtual void set(void)
Set the state of the object. Sets the state of the object, which will be either true/false.
Definition: Condition.hpp:93
Condition.hpp
This file defines interface of Condition class.
CCEC_OSAL::Condition::reset
virtual void reset(void)
Resets the state of the object. Reset the state of object to default, which is set while creating the...
Definition: Condition.hpp:113
CCEC_OSAL::ConditionVariable::set
void set(void)
sets the condition. Sets the condition assosiated with the ConditionVariable object.
Definition: ConditionVariable.cpp:61
ConditionVariable.hpp
This file defines interface of ConditionVariable class.
CCEC_OSAL::ConditionVariable::reset
void reset(void)
resets the condition. Resets the condition assosiated with the ConditionVariable object to default (f...
Definition: ConditionVariable.cpp:68
CCEC_OSAL::Thread::nativeHandle
void * nativeHandle
Destructor.
Definition: Thread.hpp:150
CCEC_OSAL::ConditionVariable::isSet
bool isSet(void)
Checks the status of the condition. Returns the status of the condition associated....
Definition: ConditionVariable.cpp:75
CCEC_OSAL::Mutex::unlock
void unlock(void)
Unlocks the given mutex.
Definition: Mutex.cpp:78
Mutex.hpp
This file defines interface of Mutex class.