| 1 |
/* |
| 2 |
* Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2025 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 24 апр. 2024 г. |
| 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/test-fw/utest.h> |
| 23 |
#include <lsp-plug.in/common/atomic.h> |
| 24 |
#include <lsp-plug.in/ipc/Thread.h> |
| 25 |
#include <lsp-plug.in/ipc/Mutex.h> |
| 26 |
#include <lsp-plug.in/ipc/SharedMutex.h> |
| 27 |
|
| 28 |
#include <stdarg.h> |
| 29 |
|
| 30 |
using namespace lsp; |
| 31 |
|
| 32 |
UTEST_BEGIN("runtime.ipc", shmutex) |
| 33 |
|
| 34 |
typedef struct context_t |
| 35 |
{ |
| 36 |
ipc::Mutex lock; |
| 37 |
LSPString data; |
| 38 |
uatomic_t sequence_latch; |
| 39 |
size_t errors; |
| 40 |
|
| 41 |
void log(status_t code, const char *event, status_t expected) |
| 42 |
{ |
| 43 |
lock.lock(); |
| 44 |
lsp_finally { lock.unlock(); }; |
| 45 |
|
| 46 |
data.append_ascii(event); |
| 47 |
data.append('='); |
| 48 |
if (code == expected) |
| 49 |
data.append_ascii("true"); |
| 50 |
else |
| 51 |
{ |
| 52 |
data.fmt_append_ascii("false(code=%d)", int(code)); |
| 53 |
++errors; |
| 54 |
} |
| 55 |
data.append(';'); |
| 56 |
} |
| 57 |
} context_t; |
| 58 |
|
| 59 |
static status_t thread_func1(void *arg) |
| 60 |
{ |
| 61 |
ipc::SharedMutex mutex; |
| 62 |
context_t *ctx = static_cast<context_t *>(arg); |
| 63 |
|
| 64 |
wait_latch(ctx->sequence_latch, 0); |
| 65 |
ctx->log(mutex.open("test-lsp.lock"), "open1", STATUS_OK); |
| 66 |
atomic_add(&ctx->sequence_latch, 1); // 0 -> 1 |
| 67 |
|
| 68 |
// Sleep 500 ms and lock the mutex |
| 69 |
wait_latch(ctx->sequence_latch, 3); |
| 70 |
ipc::Thread::sleep(500); |
| 71 |
ctx->log(mutex.lock(), "SYNC1.lock", STATUS_OK); |
| 72 |
atomic_add(&ctx->sequence_latch, 1); // 3 -> 6 |
| 73 |
|
| 74 |
// Sleep 200 ms and unlock the mutex |
| 75 |
wait_latch(ctx->sequence_latch, 7); |
| 76 |
ipc::Thread::sleep(200); |
| 77 |
ctx->log(mutex.unlock(), "SYNC2.unlock", STATUS_OK); |
| 78 |
atomic_add(&ctx->sequence_latch, 1); // 7 -> 9 |
| 79 |
|
| 80 |
// Close the mutex |
| 81 |
wait_latch(ctx->sequence_latch, 10); |
| 82 |
ctx->log(mutex.close(), "close1", STATUS_OK); |
| 83 |
atomic_add(&ctx->sequence_latch, 1); // 10 -> 11 |
| 84 |
|
| 85 |
return STATUS_OK; |
| 86 |
} |
| 87 |
|
| 88 |
static status_t thread_func2(void *arg) |
| 89 |
{ |
| 90 |
ipc::SharedMutex mutex; |
| 91 |
context_t *ctx = static_cast<context_t *>(arg); |
| 92 |
|
| 93 |
wait_latch(ctx->sequence_latch, 1); |
| 94 |
ctx->log(mutex.open("test-lsp.lock"), "open2", STATUS_OK); |
| 95 |
atomic_add(&ctx->sequence_latch, 1); // 1 -> 2 |
| 96 |
|
| 97 |
// Lock mutex immediately, sleep 500 ms and unlock it |
| 98 |
wait_latch(ctx->sequence_latch, 3); |
| 99 |
ctx->log(mutex.lock(), "lock2", STATUS_OK); |
| 100 |
ipc::Thread::sleep(500); |
| 101 |
ctx->log(mutex.unlock(), "SYNC1.unlock", STATUS_OK); |
| 102 |
atomic_add(&ctx->sequence_latch, 1); // 3 -> 6 |
| 103 |
|
| 104 |
// Call mutltiple times lock, succeed on last call |
| 105 |
wait_latch(ctx->sequence_latch, 6); |
| 106 |
ctx->log(mutex.try_lock(), "trylock2", STATUS_RETRY); |
| 107 |
ctx->log(mutex.lock(500), "timedlock2", STATUS_TIMED_OUT); |
| 108 |
atomic_add(&ctx->sequence_latch, 1); // 6 -> 7 |
| 109 |
|
| 110 |
// Succeed on timed mutex lock |
| 111 |
wait_latch(ctx->sequence_latch, 7); |
| 112 |
ctx->log(mutex.lock(800), "SYNC2.lock", STATUS_OK); |
| 113 |
atomic_add(&ctx->sequence_latch, 1); // 7 -> 9 |
| 114 |
|
| 115 |
// Sleep 200 milliseconds and unlock the mutex |
| 116 |
wait_latch(ctx->sequence_latch, 9); |
| 117 |
ipc::Thread::sleep(200); |
| 118 |
ctx->log(mutex.unlock(), "SYNC3.unlock", STATUS_OK); |
| 119 |
|
| 120 |
// Close the mutex |
| 121 |
wait_latch(ctx->sequence_latch, 11); |
| 122 |
ctx->log(mutex.close(), "close2", STATUS_OK); |
| 123 |
atomic_add(&ctx->sequence_latch, 1); // 11 -> 12 |
| 124 |
|
| 125 |
return STATUS_OK; |
| 126 |
} |
| 127 |
|
| 128 |
static void wait_latch(uatomic_t & latch, uatomic_t value) |
| 129 |
{ |
| 130 |
while (atomic_load(&latch) != value) |
| 131 |
ipc::Thread::yield(); |
| 132 |
} |
| 133 |
|
| 134 |
void test_simple() |
| 135 |
{ |
| 136 |
ipc::SharedMutex mutex; |
| 137 |
|
| 138 |
printf("Testing simple single-threaded mutex locks\n"); |
| 139 |
|
| 140 |
UTEST_ASSERT(mutex.lock() == STATUS_CLOSED); |
| 141 |
UTEST_ASSERT(mutex.lock(200) == STATUS_CLOSED); |
| 142 |
UTEST_ASSERT(mutex.try_lock() == STATUS_CLOSED); |
| 143 |
UTEST_ASSERT(mutex.unlock() == STATUS_CLOSED); |
| 144 |
UTEST_ASSERT(mutex.close() == STATUS_OK); |
| 145 |
|
| 146 |
UTEST_ASSERT(mutex.open("test-lsp.lock") == STATUS_OK); |
| 147 |
UTEST_ASSERT(mutex.unlock() == STATUS_BAD_STATE); |
| 148 |
|
| 149 |
UTEST_ASSERT(mutex.lock() == STATUS_OK); |
| 150 |
UTEST_ASSERT(mutex.lock() == STATUS_LOCKED); |
| 151 |
UTEST_ASSERT(mutex.unlock() == STATUS_OK); |
| 152 |
UTEST_ASSERT(mutex.unlock() == STATUS_BAD_STATE); |
| 153 |
|
| 154 |
UTEST_ASSERT(mutex.lock(200) == STATUS_OK); |
| 155 |
UTEST_ASSERT(mutex.lock(200) == STATUS_LOCKED); |
| 156 |
UTEST_ASSERT(mutex.unlock() == STATUS_OK); |
| 157 |
UTEST_ASSERT(mutex.unlock() == STATUS_BAD_STATE); |
| 158 |
|
| 159 |
UTEST_ASSERT(mutex.try_lock() == STATUS_OK); |
| 160 |
UTEST_ASSERT(mutex.try_lock() == STATUS_LOCKED); |
| 161 |
UTEST_ASSERT(mutex.unlock() == STATUS_OK); |
| 162 |
UTEST_ASSERT(mutex.unlock() == STATUS_BAD_STATE); |
| 163 |
|
| 164 |
UTEST_ASSERT(mutex.close() == STATUS_OK); |
| 165 |
} |
| 166 |
|
| 167 |
void test_multithreaded() |
| 168 |
{ |
| 169 |
ipc::SharedMutex mutex; |
| 170 |
context_t ctx; |
| 171 |
atomic_store(&ctx.sequence_latch, uatomic_t(0)); |
| 172 |
ctx.errors = 0; |
| 173 |
|
| 174 |
printf("Testing simple multi-threaded mutex locks\n"); |
| 175 |
|
| 176 |
ctx.log(mutex.open("test-lsp.lock"), "open", STATUS_OK); |
| 177 |
ctx.log(mutex.lock(), "lock", STATUS_OK); |
| 178 |
|
| 179 |
ipc::Thread t1(thread_func1, &ctx); |
| 180 |
ipc::Thread t2(thread_func2, &ctx); |
| 181 |
|
| 182 |
ctx.log(STATUS_OK, "start", STATUS_OK); |
| 183 |
t1.start(); |
| 184 |
t2.start(); |
| 185 |
wait_latch(ctx.sequence_latch, 2); |
| 186 |
atomic_add(&ctx.sequence_latch, 1); // 2 -> 3 |
| 187 |
|
| 188 |
// Sleep 200 ms and unlock the mutex |
| 189 |
ctx.log(STATUS_OK, "sleep", STATUS_OK); |
| 190 |
ipc::Thread::sleep(200); |
| 191 |
ctx.log(STATUS_OK, "unlock", STATUS_OK); |
| 192 |
UTEST_ASSERT(mutex.unlock() == STATUS_OK); |
| 193 |
atomic_add(&ctx.sequence_latch, 1); // 3 -> 6 |
| 194 |
|
| 195 |
// Lock the mutex, unlock it and return result |
| 196 |
wait_latch(ctx.sequence_latch, 9); |
| 197 |
ctx.log(mutex.lock(), "SYNC3.lock", STATUS_OK); |
| 198 |
ctx.log(mutex.unlock(), "unlock", STATUS_OK); |
| 199 |
|
| 200 |
ctx.log(mutex.close(), "close", STATUS_OK); |
| 201 |
atomic_add(&ctx.sequence_latch, 1); // 9 -> 10 |
| 202 |
|
| 203 |
// Do final comparison |
| 204 |
wait_latch(ctx.sequence_latch, 12); |
| 205 |
|
| 206 |
printf("Result sequence: %s\n", ctx.data.get_ascii()); |
| 207 |
UTEST_ASSERT(ctx.errors == 0); |
| 208 |
} |
| 209 |
|
| 210 |
UTEST_MAIN |
| 211 |
{ |
| 212 |
test_simple(); |
| 213 |
test_multithreaded(); |
| 214 |
} |
| 215 |
UTEST_END; |
| 216 |
|
| 217 |
|
| 218 |
|