| 1 |
/* |
| 2 |
* Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2020 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 |
#include <lsp-plug.in/ipc/Mutex.h> |
| 25 |
|
| 26 |
using namespace lsp; |
| 27 |
|
| 28 |
UTEST_BEGIN("runtime.ipc", mutex) |
| 29 |
|
| 30 |
status_t execute(int *shared, ipc::Mutex *mutex) |
| 31 |
{ |
| 32 |
UTEST_ASSERT(mutex->lock()); |
| 33 |
int value = ++(*shared); |
| 34 |
|
| 35 |
for (size_t i=0; i<3; ++i) |
| 36 |
UTEST_ASSERT(mutex->lock()); |
| 37 |
|
| 38 |
ipc::Thread::sleep((rand() % 1000) + 100); |
| 39 |
|
| 40 |
for (size_t i=0; i<3; ++i) |
| 41 |
UTEST_ASSERT(mutex->unlock()); |
| 42 |
|
| 43 |
UTEST_ASSERT(value == (*shared)++); |
| 44 |
UTEST_ASSERT(mutex->unlock()); |
| 45 |
|
| 46 |
return 1; |
| 47 |
} |
| 48 |
|
| 49 |
class TestThread: public ipc::Thread |
| 50 |
{ |
| 51 |
private: |
| 52 |
test_type_t *test; |
| 53 |
ipc::Mutex *mutex; |
| 54 |
int *shared; |
| 55 |
|
| 56 |
public: |
| 57 |
explicit TestThread() { test = NULL; shared = NULL; mutex = NULL; } |
| 58 |
virtual ~TestThread() {} |
| 59 |
|
| 60 |
void bind(test_type_t *test, int *shared, ipc::Mutex *mutex) |
| 61 |
{ |
| 62 |
this->test = test; |
| 63 |
this->shared = shared; |
| 64 |
this->mutex = mutex; |
| 65 |
} |
| 66 |
|
| 67 |
virtual status_t run() |
| 68 |
{ |
| 69 |
return test->execute(shared, mutex); |
| 70 |
} |
| 71 |
}; |
| 72 |
|
| 73 |
|
| 74 |
UTEST_MAIN |
| 75 |
{ |
| 76 |
TestThread t[4]; |
| 77 |
ipc::Mutex mutex; |
| 78 |
int shared = 0; |
| 79 |
|
| 80 |
printf("Starting threads...\n"); |
| 81 |
for (size_t i=0; i<4; ++i) |
| 82 |
{ |
| 83 |
t[i].bind(this, &shared, &mutex); |
| 84 |
UTEST_ASSERT(t[i].start() == STATUS_OK); |
| 85 |
} |
| 86 |
|
| 87 |
printf("Waiting threads for termination...\n"); |
| 88 |
for (size_t i=0; i<4; ++i) |
| 89 |
t[i].join(); |
| 90 |
|
| 91 |
printf("Analyzing exit status...\n"); |
| 92 |
for (int i=0; i<4; ++i) |
| 93 |
{ |
| 94 |
UTEST_ASSERT(!t[i].cancelled()); |
| 95 |
UTEST_ASSERT(t[i].get_result() > 0); |
| 96 |
} |
| 97 |
|
| 98 |
UTEST_ASSERT(shared == 8); |
| 99 |
} |
| 100 |
UTEST_END; |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|