Pierre Ossman | 687d52c | 2015-11-12 12:16:08 +0100 | [diff] [blame] | 1 | /* 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 Kondik | 2b56371 | 2017-06-19 23:14:18 -0700 | [diff] [blame] | 21 | #elif defined(__ANDROID__) |
| 22 | #include <utils/Errors.h> |
| 23 | #include <utils/Mutex.h> |
| 24 | #include <utils/Condition.h> |
Pierre Ossman | 687d52c | 2015-11-12 12:16:08 +0100 | [diff] [blame] | 25 | #else |
| 26 | #include <pthread.h> |
| 27 | #endif |
| 28 | |
| 29 | #include <rdr/Exception.h> |
| 30 | |
| 31 | #include <os/Mutex.h> |
| 32 | |
| 33 | using namespace os; |
| 34 | |
| 35 | Mutex::Mutex() |
| 36 | { |
| 37 | #ifdef WIN32 |
| 38 | systemMutex = new CRITICAL_SECTION; |
| 39 | InitializeCriticalSection((CRITICAL_SECTION*)systemMutex); |
Steve Kondik | 2b56371 | 2017-06-19 23:14:18 -0700 | [diff] [blame] | 40 | #elif defined(__ANDROID__) |
| 41 | systemMutex = new ::android::Mutex; |
Pierre Ossman | 687d52c | 2015-11-12 12:16:08 +0100 | [diff] [blame] | 42 | #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 | |
| 52 | Mutex::~Mutex() |
| 53 | { |
| 54 | #ifdef WIN32 |
| 55 | DeleteCriticalSection((CRITICAL_SECTION*)systemMutex); |
| 56 | delete (CRITICAL_SECTION*)systemMutex; |
Steve Kondik | 2b56371 | 2017-06-19 23:14:18 -0700 | [diff] [blame] | 57 | #elif defined(__ANDROID__) |
| 58 | delete (::android::Mutex*)systemMutex; |
Pierre Ossman | 687d52c | 2015-11-12 12:16:08 +0100 | [diff] [blame] | 59 | #else |
Pierre Ossman | ca0c5f5 | 2017-09-15 15:40:47 +0200 | [diff] [blame] | 60 | pthread_mutex_destroy((pthread_mutex_t*)systemMutex); |
Pierre Ossman | 687d52c | 2015-11-12 12:16:08 +0100 | [diff] [blame] | 61 | delete (pthread_mutex_t*)systemMutex; |
Pierre Ossman | 687d52c | 2015-11-12 12:16:08 +0100 | [diff] [blame] | 62 | #endif |
| 63 | } |
| 64 | |
| 65 | void Mutex::lock() |
| 66 | { |
| 67 | #ifdef WIN32 |
| 68 | EnterCriticalSection((CRITICAL_SECTION*)systemMutex); |
Steve Kondik | 2b56371 | 2017-06-19 23:14:18 -0700 | [diff] [blame] | 69 | #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 Ossman | 687d52c | 2015-11-12 12:16:08 +0100 | [diff] [blame] | 75 | #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 | |
| 84 | void Mutex::unlock() |
| 85 | { |
| 86 | #ifdef WIN32 |
| 87 | LeaveCriticalSection((CRITICAL_SECTION*)systemMutex); |
Steve Kondik | 2b56371 | 2017-06-19 23:14:18 -0700 | [diff] [blame] | 88 | #elif defined(__ANDROID__) |
| 89 | ((::android::Mutex*)systemMutex)->unlock(); |
Pierre Ossman | 687d52c | 2015-11-12 12:16:08 +0100 | [diff] [blame] | 90 | #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 | |
| 99 | Condition::Condition(Mutex* mutex) |
| 100 | { |
| 101 | this->mutex = mutex; |
| 102 | |
| 103 | #ifdef WIN32 |
| 104 | systemCondition = new CONDITION_VARIABLE; |
| 105 | InitializeConditionVariable((CONDITION_VARIABLE*)systemCondition); |
Steve Kondik | 2b56371 | 2017-06-19 23:14:18 -0700 | [diff] [blame] | 106 | #elif defined(__ANDROID__) |
| 107 | systemCondition = new ::android::Condition; |
Pierre Ossman | 687d52c | 2015-11-12 12:16:08 +0100 | [diff] [blame] | 108 | #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 | |
| 118 | Condition::~Condition() |
| 119 | { |
| 120 | #ifdef WIN32 |
| 121 | delete (CONDITION_VARIABLE*)systemCondition; |
Steve Kondik | 2b56371 | 2017-06-19 23:14:18 -0700 | [diff] [blame] | 122 | #elif defined(__ANDROID__) |
| 123 | delete (::android::Condition*)systemCondition; |
Pierre Ossman | 687d52c | 2015-11-12 12:16:08 +0100 | [diff] [blame] | 124 | #else |
Pierre Ossman | ca0c5f5 | 2017-09-15 15:40:47 +0200 | [diff] [blame] | 125 | pthread_cond_destroy((pthread_cond_t*)systemCondition); |
Pierre Ossman | 687d52c | 2015-11-12 12:16:08 +0100 | [diff] [blame] | 126 | delete (pthread_cond_t*)systemCondition; |
Pierre Ossman | 687d52c | 2015-11-12 12:16:08 +0100 | [diff] [blame] | 127 | #endif |
| 128 | } |
| 129 | |
| 130 | void 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 Kondik | 2b56371 | 2017-06-19 23:14:18 -0700 | [diff] [blame] | 140 | #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 Ossman | 687d52c | 2015-11-12 12:16:08 +0100 | [diff] [blame] | 146 | #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 | |
| 156 | void Condition::signal() |
| 157 | { |
| 158 | #ifdef WIN32 |
| 159 | WakeConditionVariable((CONDITION_VARIABLE*)systemCondition); |
Steve Kondik | 2b56371 | 2017-06-19 23:14:18 -0700 | [diff] [blame] | 160 | #elif defined(__ANDROID__) |
| 161 | ((::android::Condition*)systemCondition)->signal(); |
Pierre Ossman | 687d52c | 2015-11-12 12:16:08 +0100 | [diff] [blame] | 162 | #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 | |
| 171 | void Condition::broadcast() |
| 172 | { |
| 173 | #ifdef WIN32 |
| 174 | WakeAllConditionVariable((CONDITION_VARIABLE*)systemCondition); |
Steve Kondik | 2b56371 | 2017-06-19 23:14:18 -0700 | [diff] [blame] | 175 | #elif defined(__ANDROID__) |
| 176 | ((::android::Condition*)systemCondition)->broadcast(); |
Pierre Ossman | 687d52c | 2015-11-12 12:16:08 +0100 | [diff] [blame] | 177 | #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 | } |