| 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: 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/test-fw/utest.h> |
| 23 |
#include <lsp-plug.in/ipc/Thread.h> |
| 24 |
|
| 25 |
using namespace lsp; |
| 26 |
|
| 27 |
UTEST_BEGIN("runtime.ipc", thread) |
| 28 |
class TestThread: public ipc::Thread |
| 29 |
{ |
| 30 |
private: |
| 31 |
volatile int *res; |
| 32 |
int exit_code; |
| 33 |
|
| 34 |
public: |
| 35 |
explicit TestThread() { res = NULL; exit_code = -1; } |
| 36 |
virtual ~TestThread() {} |
| 37 |
|
| 38 |
void bind(volatile int *res, int exit_code) { this->res = res; this->exit_code = exit_code; } |
| 39 |
|
| 40 |
virtual status_t run() |
| 41 |
{ |
| 42 |
while (*res == 0) |
| 43 |
Thread::sleep(100); |
| 44 |
|
| 45 |
Thread::sleep(200); |
| 46 |
*res = 2; |
| 47 |
Thread::sleep(100000000); // We need to leave sleep() on cancel() event |
| 48 |
*res = 3; |
| 49 |
|
| 50 |
return exit_code; |
| 51 |
} |
| 52 |
}; |
| 53 |
|
| 54 |
class TestRunnable: public ipc::IRunnable |
| 55 |
{ |
| 56 |
private: |
| 57 |
volatile int *res; |
| 58 |
int exit_code; |
| 59 |
|
| 60 |
public: |
| 61 |
TestRunnable(volatile int *res, int exit_code) { this->res = res; this->exit_code = exit_code; } |
| 62 |
|
| 63 |
virtual status_t run() |
| 64 |
{ |
| 65 |
while (*res == 0) |
| 66 |
ipc::Thread::sleep(100); |
| 67 |
|
| 68 |
ipc::Thread::sleep(200); |
| 69 |
*res = 2; |
| 70 |
ipc::Thread::sleep(100000000); // We need to leave sleep() on cancel() event |
| 71 |
*res = 3; |
| 72 |
|
| 73 |
return exit_code; |
| 74 |
} |
| 75 |
}; |
| 76 |
|
| 77 |
typedef struct binding_t |
| 78 |
{ |
| 79 |
volatile int *res; |
| 80 |
int exit_code; |
| 81 |
} binding_t; |
| 82 |
|
| 83 |
static status_t thread_proc(void *arg) |
| 84 |
{ |
| 85 |
binding_t *binding = reinterpret_cast<binding_t *>(arg); |
| 86 |
|
| 87 |
while (*(binding->res) == 0) |
| 88 |
ipc::Thread::sleep(100); |
| 89 |
|
| 90 |
ipc::Thread::sleep(200); |
| 91 |
*(binding->res) = 2; |
| 92 |
ipc::Thread::sleep(100000000); // We need to leave sleep() on cancel() event |
| 93 |
*(binding->res) = 3; |
| 94 |
|
| 95 |
return binding->exit_code; |
| 96 |
} |
| 97 |
|
| 98 |
UTEST_MAIN |
| 99 |
{ |
| 100 |
size_t i; |
| 101 |
volatile int res[6]; |
| 102 |
ipc::Thread *pt[6]; |
| 103 |
|
| 104 |
for (i=0; i<6; ++i) |
| 105 |
res[i] = 0; |
| 106 |
|
| 107 |
//--------------------------------------------------------------------- |
| 108 |
// Create threads |
| 109 |
printf("Creating threads...\n"); |
| 110 |
|
| 111 |
// Create custom threads |
| 112 |
TestThread t[4]; |
| 113 |
for (i=0; i<4; ++i) |
| 114 |
{ |
| 115 |
pt[i] = &t[i]; |
| 116 |
t[i].bind(&res[i], int(i+1)); |
| 117 |
} |
| 118 |
|
| 119 |
// Create thread as wrapper of runnable |
| 120 |
TestRunnable r(&res[i], int(i+1)); |
| 121 |
ipc::Thread rt(&r); |
| 122 |
pt[i++] = &rt; |
| 123 |
|
| 124 |
// Create thread as a wrapper around thread_proc |
| 125 |
binding_t binding; |
| 126 |
binding.res = &res[i]; |
| 127 |
binding.exit_code = int(i + 1); |
| 128 |
ipc::Thread bt(thread_proc, &binding); |
| 129 |
pt[i++] = &bt; |
| 130 |
|
| 131 |
//--------------------------------------------------------------------- |
| 132 |
// Launch threads |
| 133 |
printf("Launching threads...\n"); |
| 134 |
for (i=0; i<6; ++i) |
| 135 |
{ |
| 136 |
UTEST_ASSERT(pt[i]->start() == STATUS_OK); |
| 137 |
} |
| 138 |
|
| 139 |
//--------------------------------------------------------------------- |
| 140 |
// Main checking cycle |
| 141 |
printf("Sleeping...\n"); |
| 142 |
ipc::Thread::sleep(500); |
| 143 |
|
| 144 |
printf("Waking threads...\n"); |
| 145 |
for (size_t i=0; i<6; ++i) |
| 146 |
{ |
| 147 |
UTEST_ASSERT(res[i] == 0); |
| 148 |
res[i] = 1; |
| 149 |
} |
| 150 |
|
| 151 |
printf("Syncing...\n"); |
| 152 |
while (true) |
| 153 |
{ |
| 154 |
size_t n = 0; |
| 155 |
for (size_t i=0; i<6; ++i) |
| 156 |
{ |
| 157 |
UTEST_ASSERT(res[i] < 2); |
| 158 |
if (res[i] == 1) |
| 159 |
++n; |
| 160 |
} |
| 161 |
if (n == 6) |
| 162 |
break; |
| 163 |
} |
| 164 |
|
| 165 |
//--------------------------------------------------------------------- |
| 166 |
// Cancel threads |
| 167 |
printf("Cancelling threads...\n"); |
| 168 |
for (i=0; i<6; ++i) |
| 169 |
{ |
| 170 |
UTEST_ASSERT(!pt[i]->cancelled()); |
| 171 |
UTEST_ASSERT(pt[i]->cancel() == STATUS_OK); |
| 172 |
} |
| 173 |
|
| 174 |
printf("Waiting threads for termination...\n"); |
| 175 |
for (i=0; i<6; ++i) |
| 176 |
pt[i]->join(); |
| 177 |
|
| 178 |
printf("Analyzing exit status...\n"); |
| 179 |
for (i=0; i<6; ++i) |
| 180 |
{ |
| 181 |
UTEST_ASSERT(pt[i]->cancelled()); |
| 182 |
UTEST_ASSERT(size_t(pt[i]->get_result()) == (i+1)); |
| 183 |
} |
| 184 |
} |
| 185 |
UTEST_END; |
| 186 |
|
| 187 |
|