| 1 |
/* |
| 2 |
* Copyright (C) 2026 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2026 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 4 янв. 2026 г. |
| 7 |
* |
| 8 |
* lsp-runtime-lib is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* lsp-runtime-lib is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <lsp-plug.in/ipc/Condition.h> |
| 23 |
|
| 24 |
#ifdef PLATFORM_WINDOWS |
| 25 |
#include <windows.h> |
| 26 |
#include <synchapi.h> |
| 27 |
#else |
| 28 |
#include <errno.h> |
| 29 |
#include <sys/time.h> |
| 30 |
#include <sched.h> |
| 31 |
#endif /* PLATFORM_WINDOWS */ |
| 32 |
|
| 33 |
namespace lsp |
| 34 |
{ |
| 35 |
namespace ipc |
| 36 |
{ |
| 37 |
#if defined(PLATFORM_WINDOWS) |
| 38 |
|
| 39 |
namespace detail |
| 40 |
{ |
| 41 |
struct condition_t |
| 42 |
{ |
| 43 |
::CRITICAL_SECTION sMutex; |
| 44 |
::CONDITION_VARIABLE sCondition; |
| 45 |
}; |
| 46 |
} /* namespace detail */ |
| 47 |
|
| 48 |
Condition::Condition() |
| 49 |
{ |
| 50 |
pState = new detail::condition_t; |
| 51 |
if (pState != NULL) |
| 52 |
{ |
| 53 |
InitializeCriticalSectionAndSpinCount(&pState->sMutex, 0x10); |
| 54 |
InitializeConditionVariable(&pState->sCondition); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
Condition::~Condition() |
| 59 |
{ |
| 60 |
if (pState != NULL) |
| 61 |
{ |
| 62 |
DeleteCriticalSection(&pState->sMutex); |
| 63 |
delete pState; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
bool Condition::lock() const |
| 68 |
{ |
| 69 |
if (pState == NULL) |
| 70 |
return false; |
| 71 |
EnterCriticalSection(&pState->sMutex); |
| 72 |
return true; |
| 73 |
} |
| 74 |
|
| 75 |
bool Condition::try_lock() const |
| 76 |
{ |
| 77 |
return (pState != NULL) ? TryEnterCriticalSection(&pState->sMutex) : false; |
| 78 |
} |
| 79 |
|
| 80 |
bool Condition::unlock() const |
| 81 |
{ |
| 82 |
if (pState == NULL) |
| 83 |
return false; |
| 84 |
|
| 85 |
HANDLE thread = reinterpret_cast<HANDLE>(GetCurrentThreadId()); |
| 86 |
if (pState->sMutex.OwningThread != thread) |
| 87 |
return false; |
| 88 |
if (pState->sMutex.RecursionCount <= 0) |
| 89 |
return false; |
| 90 |
|
| 91 |
LeaveCriticalSection(&pState->sMutex); |
| 92 |
return true; |
| 93 |
} |
| 94 |
|
| 95 |
bool Condition::notify() const |
| 96 |
{ |
| 97 |
if (pState == NULL) |
| 98 |
return false; |
| 99 |
WakeConditionVariable(&pState->sCondition); |
| 100 |
return true; |
| 101 |
} |
| 102 |
|
| 103 |
bool Condition::notify_all() const |
| 104 |
{ |
| 105 |
if (pState == NULL) |
| 106 |
return false; |
| 107 |
WakeAllConditionVariable(&pState->sCondition); |
| 108 |
return true; |
| 109 |
} |
| 110 |
|
| 111 |
status_t Condition::wait() |
| 112 |
{ |
| 113 |
if (pState == NULL) |
| 114 |
return STATUS_BAD_STATE; |
| 115 |
|
| 116 |
if (SleepConditionVariableCS(&pState->sCondition, &pState->sMutex, INFINITE)) |
| 117 |
return STATUS_OK; |
| 118 |
|
| 119 |
const DWORD result = GetLastError(); |
| 120 |
return (result == ERROR_TIMEOUT) ? STATUS_TIMED_OUT: STATUS_UNKNOWN_ERR; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
status_t Condition::wait(system::time_millis_t millis) |
| 125 |
{ |
| 126 |
if (pState == NULL) |
| 127 |
return STATUS_BAD_STATE; |
| 128 |
|
| 129 |
do |
| 130 |
{ |
| 131 |
const DWORD timeout = (millis >= INFINITE) ? INFINITE - 1 : DWORD(millis); |
| 132 |
if (SleepConditionVariableCS(&pState->sCondition, &pState->sMutex, DWORD(millis))) |
| 133 |
return STATUS_OK; |
| 134 |
|
| 135 |
const DWORD result = GetLastError(); |
| 136 |
if (result != ERROR_TIMEOUT) |
| 137 |
return STATUS_UNKNOWN_ERR; |
| 138 |
|
| 139 |
millis -= timeout; |
| 140 |
} while (millis > 0); |
| 141 |
|
| 142 |
return STATUS_TIMEOUT; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
#else |
| 147 |
Condition::Condition() |
| 148 |
{ |
| 149 |
pthread_mutexattr_t attr; |
| 150 |
pthread_mutexattr_init(&attr); |
| 151 |
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); |
| 152 |
pthread_mutex_init(&sMutex, &attr); |
| 153 |
pthread_mutexattr_destroy(&attr); |
| 154 |
|
| 155 |
pthread_condattr_t cond_attr; |
| 156 |
pthread_condattr_init(&cond_attr); |
| 157 |
pthread_cond_init(&sCondition, &cond_attr); |
| 158 |
pthread_condattr_destroy(&cond_attr); |
| 159 |
} |
| 160 |
|
| 161 |
Condition::~Condition() |
| 162 |
{ |
| 163 |
pthread_cond_destroy(&sCondition); |
| 164 |
pthread_mutex_destroy(&sMutex); |
| 165 |
} |
| 166 |
|
| 167 |
bool Condition::lock() const |
| 168 |
{ |
| 169 |
while (true) |
| 170 |
{ |
| 171 |
switch (pthread_mutex_lock(&sMutex)) |
| 172 |
{ |
| 173 |
case 0: return true; |
| 174 |
case EBUSY: |
| 175 |
sched_yield(); |
| 176 |
break; |
| 177 |
default: return false; |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
status_t Condition::wait(system::time_millis_t millis) |
| 183 |
{ |
| 184 |
struct timeval now; |
| 185 |
struct timespec deadline; |
| 186 |
|
| 187 |
// Set-up the fire time |
| 188 |
gettimeofday(&now, NULL); |
| 189 |
deadline.tv_nsec = now.tv_usec * 1000 + (millis % 1000) * 1000000; |
| 190 |
deadline.tv_sec = now.tv_sec + (millis / 1000) + (deadline.tv_nsec / 1000000000); |
| 191 |
deadline.tv_nsec %= 1000000000; |
| 192 |
|
| 193 |
// Perform wait |
| 194 |
int result = pthread_cond_timedwait(&sCondition, &sMutex, &deadline); |
| 195 |
switch (result) |
| 196 |
{ |
| 197 |
case 0: return STATUS_OK; |
| 198 |
case ETIMEDOUT: return STATUS_TIMED_OUT; |
| 199 |
case EPERM: return STATUS_BAD_STATE; |
| 200 |
default: break; |
| 201 |
} |
| 202 |
|
| 203 |
return STATUS_UNKNOWN_ERR; |
| 204 |
} |
| 205 |
|
| 206 |
status_t Condition::wait() |
| 207 |
{ |
| 208 |
int result = pthread_cond_wait(&sCondition, &sMutex); |
| 209 |
switch (result) |
| 210 |
{ |
| 211 |
case 0: return STATUS_OK; |
| 212 |
case ETIMEDOUT: return STATUS_TIMED_OUT; |
| 213 |
case EPERM: return STATUS_BAD_STATE; |
| 214 |
default: break; |
| 215 |
} |
| 216 |
|
| 217 |
return STATUS_UNKNOWN_ERR; |
| 218 |
} |
| 219 |
|
| 220 |
#endif /* PLATFORM_LINUX */ |
| 221 |
|
| 222 |
} /* namespace ipc */ |
| 223 |
} /* namespace lsp */ |
| 224 |
|
| 225 |
|
| 226 |
|