| 1 |
/* |
| 2 |
* Copyright (C) 2024 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2024 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 25 февр. 2019 г. |
| 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/Mutex.h> |
| 23 |
#include <errno.h> |
| 24 |
|
| 25 |
#ifdef PLATFORM_WINDOWS |
| 26 |
#include <windows.h> |
| 27 |
#include <synchapi.h> |
| 28 |
#endif /* PLATFORM_WINDOWS */ |
| 29 |
|
| 30 |
namespace lsp |
| 31 |
{ |
| 32 |
namespace ipc |
| 33 |
{ |
| 34 |
#if defined(PLATFORM_WINDOWS) |
| 35 |
|
| 36 |
namespace detail |
| 37 |
{ |
| 38 |
struct CRITICAL_SECTION: public ::CRITICAL_SECTION |
| 39 |
{ |
| 40 |
}; |
| 41 |
} /* namespace detail */ |
| 42 |
|
| 43 |
Mutex::Mutex() |
| 44 |
{ |
| 45 |
hMutex = new detail::CRITICAL_SECTION; |
| 46 |
if (hMutex != NULL) |
| 47 |
InitializeCriticalSectionAndSpinCount(hMutex, 0x10); |
| 48 |
} |
| 49 |
|
| 50 |
Mutex::~Mutex() |
| 51 |
{ |
| 52 |
if (hMutex != NULL) |
| 53 |
{ |
| 54 |
DeleteCriticalSection(hMutex); |
| 55 |
delete hMutex; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
bool Mutex::lock() const |
| 60 |
{ |
| 61 |
EnterCriticalSection(hMutex); |
| 62 |
return true; |
| 63 |
} |
| 64 |
|
| 65 |
bool Mutex::try_lock() const |
| 66 |
{ |
| 67 |
return TryEnterCriticalSection(hMutex); |
| 68 |
} |
| 69 |
|
| 70 |
bool Mutex::unlock() const |
| 71 |
{ |
| 72 |
HANDLE thread = reinterpret_cast<HANDLE>(GetCurrentThreadId()); |
| 73 |
if (hMutex->OwningThread != thread) |
| 74 |
return false; |
| 75 |
if (hMutex->RecursionCount <= 0) |
| 76 |
return false; |
| 77 |
|
| 78 |
LeaveCriticalSection(hMutex); |
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
#elif defined(PLATFORM_LINUX) |
| 83 |
static inline int futex( |
| 84 |
int *addr, |
| 85 |
int futex_op, |
| 86 |
int val, |
| 87 |
const struct timespec *timeout, |
| 88 |
volatile int *addr2, |
| 89 |
int val3) |
| 90 |
{ |
| 91 |
return syscall(SYS_futex, addr, futex_op, val, timeout, addr2, val3); |
| 92 |
} |
| 93 |
|
| 94 |
bool Mutex::lock() const |
| 95 |
{ |
| 96 |
int res; |
| 97 |
|
| 98 |
// Check that we already own the mutex |
| 99 |
pthread_t tid = pthread_self(); |
| 100 |
if (nThreadId == tid) |
| 101 |
{ |
| 102 |
++nLocks; |
| 103 |
return true; |
| 104 |
} |
| 105 |
|
| 106 |
// Perform the wait until another thread releases the mutex |
| 107 |
while (!atomic_cas(&nLock, 1, 0)) |
| 108 |
{ |
| 109 |
// Issue wait |
| 110 |
res = futex( |
| 111 |
&nLock, // addr: address |
| 112 |
FUTEX_WAIT, // futex_op: WAIT for the notification related to the address |
| 113 |
0, // val: value to test for |
| 114 |
NULL, // timeout: infinite wait |
| 115 |
NULL, // addr2: unused |
| 116 |
0); // val3: unused |
| 117 |
if ((res == ENOSYS) || (res == EAGAIN)) |
| 118 |
sched_yield(); |
| 119 |
} |
| 120 |
|
| 121 |
// Update lock state |
| 122 |
nThreadId = tid; |
| 123 |
++nLocks; |
| 124 |
|
| 125 |
return true; |
| 126 |
} |
| 127 |
|
| 128 |
bool Mutex::try_lock() const |
| 129 |
{ |
| 130 |
// Check that we already own the mutex |
| 131 |
pthread_t tid = pthread_self(); |
| 132 |
if (nThreadId == tid) |
| 133 |
{ |
| 134 |
++nLocks; |
| 135 |
return true; |
| 136 |
} |
| 137 |
|
| 138 |
if (!atomic_cas(&nLock, 1, 0)) |
| 139 |
return false; |
| 140 |
|
| 141 |
// Update lock state |
| 142 |
nThreadId = tid; |
| 143 |
++nLocks; |
| 144 |
|
| 145 |
return true; |
| 146 |
} |
| 147 |
|
| 148 |
bool Mutex::unlock() const |
| 149 |
{ |
| 150 |
if (nThreadId != pthread_self()) |
| 151 |
return false; |
| 152 |
|
| 153 |
// Update the lock state |
| 154 |
if (--nLocks) |
| 155 |
return true; |
| 156 |
nThreadId = -1; |
| 157 |
|
| 158 |
if (atomic_cas(&nLock, 0, 1)) |
| 159 |
{ |
| 160 |
futex( |
| 161 |
&nLock, // addr: address |
| 162 |
FUTEX_WAKE, // futex_op: WAKEUP listeners at the specified address |
| 163 |
1, // val: Number of listeners to wake up |
| 164 |
NULL, // timeout: unused |
| 165 |
0, // addr2: unused |
| 166 |
0); // val3: unused |
| 167 |
} |
| 168 |
|
| 169 |
return true; |
| 170 |
} |
| 171 |
|
| 172 |
#else |
| 173 |
Mutex::Mutex() |
| 174 |
{ |
| 175 |
pthread_mutexattr_t attr; |
| 176 |
pthread_mutexattr_init(&attr); |
| 177 |
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); |
| 178 |
pthread_mutex_init(&sMutex, &attr); |
| 179 |
pthread_mutexattr_destroy(&attr); |
| 180 |
} |
| 181 |
|
| 182 |
Mutex::~Mutex() |
| 183 |
{ |
| 184 |
pthread_mutex_destroy(&sMutex); |
| 185 |
} |
| 186 |
#endif /* PLATFORM_LINUX */ |
| 187 |
|
| 188 |
} /* namespace ipc */ |
| 189 |
} /* namespace lsp */ |