| 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-audio-pipewire-lib |
| 6 |
* Created on: 2 мая 2026 г. |
| 7 |
* |
| 8 |
* lsp-audio-pipewire-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-audio-pipewire-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-audio-pipewire-lib. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#ifndef LSP_PLUG_IN_AUDIO_PIPEWIRE_IMPL_MUTEX_H_ |
| 23 |
#define LSP_PLUG_IN_AUDIO_PIPEWIRE_IMPL_MUTEX_H_ |
| 24 |
|
| 25 |
#include <lsp-plug.in/audio/pipewire/mutex.h> |
| 26 |
#include <lsp-plug.in/common/alloc.h> |
| 27 |
#include <lsp-plug.in/common/types.h> |
| 28 |
|
| 29 |
#ifdef PLATFORM_POSIX |
| 30 |
#include <errno.h> |
| 31 |
#include <pthread.h> |
| 32 |
#include <sys/time.h> |
| 33 |
#else |
| 34 |
#error "Need to implement mutex for the target platform" |
| 35 |
#endif /* PLATFORM_POSIX*/ |
| 36 |
|
| 37 |
namespace lsp |
| 38 |
{ |
| 39 |
namespace audio |
| 40 |
{ |
| 41 |
namespace pipewire |
| 42 |
{ |
| 43 |
typedef struct mutex_t |
| 44 |
{ |
| 45 |
pthread_mutex_t lock; |
| 46 |
pthread_cond_t cond; |
| 47 |
} mutex_t; |
| 48 |
|
| 49 |
inline mutex_t *mutex_create() |
| 50 |
{ |
| 51 |
mutex_t *mutex = malloc_count<mutex_t>(1); |
| 52 |
if (mutex == NULL) |
| 53 |
return NULL; |
| 54 |
lsp_finally { |
| 55 |
if (mutex != NULL) |
| 56 |
free(mutex); |
| 57 |
}; |
| 58 |
|
| 59 |
pthread_mutexattr_t attr; |
| 60 |
pthread_mutexattr_init(&attr); |
| 61 |
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); |
| 62 |
pthread_mutex_init(&mutex->lock, &attr); |
| 63 |
pthread_mutexattr_destroy(&attr); |
| 64 |
|
| 65 |
pthread_condattr_t cond_attr; |
| 66 |
pthread_condattr_init(&cond_attr); |
| 67 |
pthread_cond_init(&mutex->cond, &cond_attr); |
| 68 |
pthread_condattr_destroy(&cond_attr); |
| 69 |
|
| 70 |
return release_ptr(mutex); |
| 71 |
} |
| 72 |
|
| 73 |
inline void mutex_destroy(mutex_t *mutex) |
| 74 |
{ |
| 75 |
if (mutex == NULL) |
| 76 |
return; |
| 77 |
|
| 78 |
pthread_mutex_destroy(&mutex->lock); |
| 79 |
free(mutex); |
| 80 |
} |
| 81 |
|
| 82 |
inline void mutex_lock(mutex_t *mutex) |
| 83 |
{ |
| 84 |
if (mutex != NULL) |
| 85 |
pthread_mutex_lock(&mutex->lock); |
| 86 |
} |
| 87 |
|
| 88 |
inline bool mutex_trylock(mutex_t *mutex) |
| 89 |
{ |
| 90 |
if (mutex == NULL) |
| 91 |
return true; |
| 92 |
return pthread_mutex_trylock(&mutex->lock) != EBUSY; |
| 93 |
} |
| 94 |
|
| 95 |
inline void mutex_unlock(mutex_t *mutex) |
| 96 |
{ |
| 97 |
if (mutex != NULL) |
| 98 |
pthread_mutex_unlock(&mutex->lock); |
| 99 |
} |
| 100 |
|
| 101 |
status_t mutex_wait(mutex_t *mutex, uint32_t millis) |
| 102 |
{ |
| 103 |
struct timeval now; |
| 104 |
struct timespec deadline; |
| 105 |
|
| 106 |
// Set-up the fire time |
| 107 |
gettimeofday(&now, NULL); |
| 108 |
deadline.tv_nsec = now.tv_usec * 1000 + (millis % 1000) * 1000000; |
| 109 |
deadline.tv_sec = now.tv_sec + (millis / 1000) + (deadline.tv_nsec / 1000000000); |
| 110 |
deadline.tv_nsec %= 1000000000; |
| 111 |
|
| 112 |
// Perform wait |
| 113 |
int result = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &deadline); |
| 114 |
switch (result) |
| 115 |
{ |
| 116 |
case 0: return STATUS_OK; |
| 117 |
case ETIMEDOUT: return STATUS_TIMED_OUT; |
| 118 |
case EPERM: return STATUS_BAD_STATE; |
| 119 |
default: break; |
| 120 |
} |
| 121 |
|
| 122 |
return STATUS_UNKNOWN_ERR; |
| 123 |
} |
| 124 |
|
| 125 |
bool mutex_notify(mutex_t *mutex) |
| 126 |
{ |
| 127 |
return pthread_cond_broadcast(&mutex->cond) == 0; |
| 128 |
} |
| 129 |
|
| 130 |
inline mutex_guard::mutex_guard(mutex_t *mutex) |
| 131 |
{ |
| 132 |
pMutex = mutex; |
| 133 |
mutex_lock(pMutex); |
| 134 |
} |
| 135 |
|
| 136 |
inline mutex_guard::mutex_guard(const mutex_guard & src) |
| 137 |
{ |
| 138 |
pMutex = src.pMutex; |
| 139 |
mutex_lock(pMutex); |
| 140 |
} |
| 141 |
|
| 142 |
inline mutex_guard::mutex_guard(mutex_guard && src) |
| 143 |
{ |
| 144 |
pMutex = lsp::exchange(src.pMutex, static_cast<mutex_t *>(NULL)); |
| 145 |
} |
| 146 |
|
| 147 |
inline mutex_guard::~mutex_guard() |
| 148 |
{ |
| 149 |
mutex_unlock(pMutex); |
| 150 |
pMutex = NULL; |
| 151 |
} |
| 152 |
|
| 153 |
mutex_guard & mutex_guard::operator = (const mutex_guard & src) |
| 154 |
{ |
| 155 |
mutex_lock(src.pMutex); |
| 156 |
mutex_unlock(pMutex); |
| 157 |
pMutex = src.pMutex; |
| 158 |
return *this; |
| 159 |
} |
| 160 |
|
| 161 |
inline mutex_guard & mutex_guard::operator = (mutex_guard && src) |
| 162 |
{ |
| 163 |
pMutex = lsp::exchange(src.pMutex, static_cast<mutex_t *>(NULL)); |
| 164 |
return *this; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
} /* namespace pipewire */ |
| 169 |
} /* namespace audio */ |
| 170 |
} /* namespace lsp */ |
| 171 |
|
| 172 |
#endif /* LSP_PLUG_IN_AUDIO_PIPEWIRE_IMPL_MUTEX_H_ */ |