| 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 |
#ifndef LSP_PLUG_IN_IPC_MUTEX_H_ |
| 23 |
#define LSP_PLUG_IN_IPC_MUTEX_H_ |
| 24 |
|
| 25 |
#include <lsp-plug.in/runtime/version.h> |
| 26 |
#include <lsp-plug.in/common/types.h> |
| 27 |
#include <lsp-plug.in/common/atomic.h> |
| 28 |
|
| 29 |
#if defined(PLATFORM_WINDOWS) |
| 30 |
// Nothing |
| 31 |
#elif defined(PLATFORM_LINUX) |
| 32 |
#include <linux/futex.h> |
| 33 |
#include <sys/syscall.h> |
| 34 |
#include <unistd.h> |
| 35 |
#include <pthread.h> |
| 36 |
#include <sched.h> |
| 37 |
#include <errno.h> |
| 38 |
#else |
| 39 |
#include <pthread.h> |
| 40 |
#include <sched.h> |
| 41 |
#include <errno.h> |
| 42 |
#endif |
| 43 |
|
| 44 |
namespace lsp |
| 45 |
{ |
| 46 |
namespace ipc |
| 47 |
{ |
| 48 |
#if defined(PLATFORM_WINDOWS) |
| 49 |
|
| 50 |
namespace detail |
| 51 |
{ |
| 52 |
struct CRITICAL_SECTION; |
| 53 |
} /* namespace detail */ |
| 54 |
|
| 55 |
/** |
| 56 |
* Recursive mutex for Windows platform |
| 57 |
*/ |
| 58 |
class Mutex |
| 59 |
{ |
| 60 |
private: |
| 61 |
mutable detail::CRITICAL_SECTION *hMutex; // Mutex object |
| 62 |
|
| 63 |
public: |
| 64 |
explicit Mutex(); |
| 65 |
Mutex(const Mutex &) = delete; |
| 66 |
Mutex(Mutex &&) = delete; |
| 67 |
~Mutex(); |
| 68 |
Mutex & operator = (const Mutex &) = delete; |
| 69 |
Mutex & operator = (Mutex &&) = delete; |
| 70 |
|
| 71 |
/** Wait until mutex is unlocked and lock it |
| 72 |
* |
| 73 |
*/ |
| 74 |
bool lock() const; |
| 75 |
|
| 76 |
/** Try to lock mutex and return status of operation |
| 77 |
* |
| 78 |
* @return non-zero value if mutex was locked |
| 79 |
*/ |
| 80 |
bool try_lock() const; |
| 81 |
|
| 82 |
/** Unlock mutex |
| 83 |
* |
| 84 |
*/ |
| 85 |
bool unlock() const; |
| 86 |
}; |
| 87 |
#elif defined(PLATFORM_LINUX) |
| 88 |
/** |
| 89 |
* Fast recursive mutex implementation for Linux using Futex primitive |
| 90 |
*/ |
| 91 |
class Mutex |
| 92 |
{ |
| 93 |
private: |
| 94 |
mutable int nLock; // 1 = unlocked, 0 = locked |
| 95 |
mutable volatile pthread_t nThreadId; // Locked thread identifier |
| 96 |
mutable ssize_t nLocks; // Number of locks by current thread |
| 97 |
|
| 98 |
public: |
| 99 |
explicit Mutex() |
| 100 |
{ |
| 101 |
nLock = 1; |
| 102 |
nThreadId = -1; |
| 103 |
nLocks = 0; |
| 104 |
} |
| 105 |
|
| 106 |
Mutex(const Mutex &) = delete; |
| 107 |
Mutex(Mutex &&) = delete; |
| 108 |
~Mutex() = default; |
| 109 |
|
| 110 |
Mutex & operator = (const Mutex &) = delete; |
| 111 |
Mutex & operator = (Mutex &&) = delete; |
| 112 |
|
| 113 |
|
| 114 |
/** Wait until mutex is unlocked and lock it |
| 115 |
* |
| 116 |
*/ |
| 117 |
bool lock() const; |
| 118 |
|
| 119 |
/** Try to lock mutex and return status of operation |
| 120 |
* |
| 121 |
* @return non-zero value if mutex was locked |
| 122 |
*/ |
| 123 |
bool try_lock() const; |
| 124 |
|
| 125 |
/** Unlock mutex |
| 126 |
* |
| 127 |
*/ |
| 128 |
bool unlock() const; |
| 129 |
}; |
| 130 |
#else |
| 131 |
/** |
| 132 |
* Recursive mutex implementation using pthread |
| 133 |
*/ |
| 134 |
class Mutex |
| 135 |
{ |
| 136 |
private: |
| 137 |
mutable pthread_mutex_t sMutex; |
| 138 |
|
| 139 |
public: |
| 140 |
explicit Mutex(); |
| 141 |
Mutex(const Mutex &) = delete; |
| 142 |
Mutex(Mutex &&) = delete; |
| 143 |
~Mutex(); |
| 144 |
Mutex & operator = (const Mutex &) = delete; |
| 145 |
Mutex & operator = (Mutex &&) = delete; |
| 146 |
|
| 147 |
/** Wait until mutex is unlocked and lock it |
| 148 |
* |
| 149 |
*/ |
| 150 |
inline bool lock() const |
| 151 |
{ |
| 152 |
while (true) |
| 153 |
{ |
| 154 |
switch (pthread_mutex_lock(&sMutex)) |
| 155 |
{ |
| 156 |
case 0: return true; |
| 157 |
case EBUSY: |
| 158 |
sched_yield(); |
| 159 |
break; |
| 160 |
default: return false; |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
/** Try to lock mutex and return status of operation |
| 166 |
* |
| 167 |
* @return non-zero value if mutex was locked |
| 168 |
*/ |
| 169 |
inline bool try_lock() const |
| 170 |
{ |
| 171 |
return pthread_mutex_trylock(&sMutex) == 0; |
| 172 |
} |
| 173 |
|
| 174 |
/** Unlock mutex |
| 175 |
* |
| 176 |
*/ |
| 177 |
inline bool unlock() const |
| 178 |
{ |
| 179 |
return pthread_mutex_unlock(&sMutex) == 0; |
| 180 |
} |
| 181 |
}; |
| 182 |
#endif |
| 183 |
|
| 184 |
} /* namespace ipc */ |
| 185 |
} /* namespace lsp */ |
| 186 |
|
| 187 |
#endif /* LSP_PLUG_IN_IPC_MUTEX_H_ */ |