blob: 47945682a9d8d0c358ef33cb6a50e19f920a7d93 [file] [log] [blame]
Pierre Ossman687d52c2015-11-12 12:16:08 +01001/* Copyright 2015 Pierre Ossman for Cendio AB
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19#ifdef WIN32
20#include <windows.h>
Steve Kondik2b563712017-06-19 23:14:18 -070021#elif defined(__ANDROID__)
22#include <utils/Errors.h>
23#include <utils/Mutex.h>
24#include <utils/Condition.h>
Pierre Ossman687d52c2015-11-12 12:16:08 +010025#else
26#include <pthread.h>
27#endif
28
29#include <rdr/Exception.h>
30
31#include <os/Mutex.h>
32
33using namespace os;
34
35Mutex::Mutex()
36{
37#ifdef WIN32
38 systemMutex = new CRITICAL_SECTION;
39 InitializeCriticalSection((CRITICAL_SECTION*)systemMutex);
Steve Kondik2b563712017-06-19 23:14:18 -070040#elif defined(__ANDROID__)
41 systemMutex = new ::android::Mutex;
Pierre Ossman687d52c2015-11-12 12:16:08 +010042#else
43 int ret;
44
45 systemMutex = new pthread_mutex_t;
46 ret = pthread_mutex_init((pthread_mutex_t*)systemMutex, NULL);
47 if (ret != 0)
48 throw rdr::SystemException("Failed to create mutex", ret);
49#endif
50}
51
52Mutex::~Mutex()
53{
54#ifdef WIN32
55 DeleteCriticalSection((CRITICAL_SECTION*)systemMutex);
56 delete (CRITICAL_SECTION*)systemMutex;
Steve Kondik2b563712017-06-19 23:14:18 -070057#elif defined(__ANDROID__)
58 delete (::android::Mutex*)systemMutex;
Pierre Ossman687d52c2015-11-12 12:16:08 +010059#else
Pierre Ossmanca0c5f52017-09-15 15:40:47 +020060 pthread_mutex_destroy((pthread_mutex_t*)systemMutex);
Pierre Ossman687d52c2015-11-12 12:16:08 +010061 delete (pthread_mutex_t*)systemMutex;
Pierre Ossman687d52c2015-11-12 12:16:08 +010062#endif
63}
64
65void Mutex::lock()
66{
67#ifdef WIN32
68 EnterCriticalSection((CRITICAL_SECTION*)systemMutex);
Steve Kondik2b563712017-06-19 23:14:18 -070069#elif defined(__ANDROID__)
70 android::status_t ret;
71
72 ret = ((::android::Mutex*)systemMutex)->lock();
73 if (ret != android::NO_ERROR)
74 throw rdr::SystemException("Failed to lock mutex", ret);
Pierre Ossman687d52c2015-11-12 12:16:08 +010075#else
76 int ret;
77
78 ret = pthread_mutex_lock((pthread_mutex_t*)systemMutex);
79 if (ret != 0)
80 throw rdr::SystemException("Failed to lock mutex", ret);
81#endif
82}
83
84void Mutex::unlock()
85{
86#ifdef WIN32
87 LeaveCriticalSection((CRITICAL_SECTION*)systemMutex);
Steve Kondik2b563712017-06-19 23:14:18 -070088#elif defined(__ANDROID__)
89 ((::android::Mutex*)systemMutex)->unlock();
Pierre Ossman687d52c2015-11-12 12:16:08 +010090#else
91 int ret;
92
93 ret = pthread_mutex_unlock((pthread_mutex_t*)systemMutex);
94 if (ret != 0)
95 throw rdr::SystemException("Failed to unlock mutex", ret);
96#endif
97}
98
99Condition::Condition(Mutex* mutex)
100{
101 this->mutex = mutex;
102
103#ifdef WIN32
104 systemCondition = new CONDITION_VARIABLE;
105 InitializeConditionVariable((CONDITION_VARIABLE*)systemCondition);
Steve Kondik2b563712017-06-19 23:14:18 -0700106#elif defined(__ANDROID__)
107 systemCondition = new ::android::Condition;
Pierre Ossman687d52c2015-11-12 12:16:08 +0100108#else
109 int ret;
110
111 systemCondition = new pthread_cond_t;
112 ret = pthread_cond_init((pthread_cond_t*)systemCondition, NULL);
113 if (ret != 0)
114 throw rdr::SystemException("Failed to create condition variable", ret);
115#endif
116}
117
118Condition::~Condition()
119{
120#ifdef WIN32
121 delete (CONDITION_VARIABLE*)systemCondition;
Steve Kondik2b563712017-06-19 23:14:18 -0700122#elif defined(__ANDROID__)
123 delete (::android::Condition*)systemCondition;
Pierre Ossman687d52c2015-11-12 12:16:08 +0100124#else
Pierre Ossmanca0c5f52017-09-15 15:40:47 +0200125 pthread_cond_destroy((pthread_cond_t*)systemCondition);
Pierre Ossman687d52c2015-11-12 12:16:08 +0100126 delete (pthread_cond_t*)systemCondition;
Pierre Ossman687d52c2015-11-12 12:16:08 +0100127#endif
128}
129
130void Condition::wait()
131{
132#ifdef WIN32
133 BOOL ret;
134
135 ret = SleepConditionVariableCS((CONDITION_VARIABLE*)systemCondition,
136 (CRITICAL_SECTION*)mutex->systemMutex,
137 INFINITE);
138 if (!ret)
139 throw rdr::SystemException("Failed to wait on condition variable", GetLastError());
Steve Kondik2b563712017-06-19 23:14:18 -0700140#elif defined(__ANDROID__)
141 android::status_t ret;
142
143 ret = ((::android::Condition*)systemCondition)->wait(*(::android::Mutex*)mutex->systemMutex);
144 if (ret != android::NO_ERROR)
145 throw rdr::SystemException("Failed to wait on condition variable", ret);
Pierre Ossman687d52c2015-11-12 12:16:08 +0100146#else
147 int ret;
148
149 ret = pthread_cond_wait((pthread_cond_t*)systemCondition,
150 (pthread_mutex_t*)mutex->systemMutex);
151 if (ret != 0)
152 throw rdr::SystemException("Failed to wait on condition variable", ret);
153#endif
154}
155
156void Condition::signal()
157{
158#ifdef WIN32
159 WakeConditionVariable((CONDITION_VARIABLE*)systemCondition);
Steve Kondik2b563712017-06-19 23:14:18 -0700160#elif defined(__ANDROID__)
161 ((::android::Condition*)systemCondition)->signal();
Pierre Ossman687d52c2015-11-12 12:16:08 +0100162#else
163 int ret;
164
165 ret = pthread_cond_signal((pthread_cond_t*)systemCondition);
166 if (ret != 0)
167 throw rdr::SystemException("Failed to signal condition variable", ret);
168#endif
169}
170
171void Condition::broadcast()
172{
173#ifdef WIN32
174 WakeAllConditionVariable((CONDITION_VARIABLE*)systemCondition);
Steve Kondik2b563712017-06-19 23:14:18 -0700175#elif defined(__ANDROID__)
176 ((::android::Condition*)systemCondition)->broadcast();
Pierre Ossman687d52c2015-11-12 12:16:08 +0100177#else
178 int ret;
179
180 ret = pthread_cond_broadcast((pthread_cond_t*)systemCondition);
181 if (ret != 0)
182 throw rdr::SystemException("Failed to broadcast condition variable", ret);
183#endif
184}