Debian Codegraph @

modules/lsp-runtime-lib/src/test/utest/ipc/condition.cpp

raw
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-runtime-lib
6 * Created on: 4 янв. 2026 г.
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/Condition.h>
25
26 using namespace lsp;
27
28 UTEST_BEGIN("runtime.ipc", condition)
29
30 UTEST_TIMELIMIT(10)
31
32 status_t execute(int id, int *shared, ipc::Condition *condition)
33 {
34 // Wait for the condition
35 {
36 UTEST_ASSERT(condition->lock());
37 lsp_finally { condition->unlock(); };
38
39 while (*shared != id)
40 condition->wait();
41 }
42
43 printf("Thread %d has been awaken\n", id);
44
45 // Do some sleep
46 ipc::Thread::sleep(500);
47
48 // Trigger condition
49 {
50 UTEST_ASSERT(condition->lock());
51 lsp_finally { condition->unlock(); };
52
53 ++(*shared);
54 UTEST_ASSERT(condition->notify_all());
55 }
56
57 return 1;
58 }
59
60 class TestThread: public ipc::Thread
61 {
62 private:
63 test_type_t *test;
64 ipc::Condition *condition;
65 int *shared;
66 int id;
67
68 public:
69 explicit TestThread()
70 {
71 test = NULL;
72 shared = NULL;
73 id = 0;
74 condition = NULL;
75 }
76 virtual ~TestThread() {}
77
78 void bind(test_type_t *test, int *shared, int id, ipc::Condition *condition)
79 {
80 this->test = test;
81 this->shared = shared;
82 this->id = id;
83 this->condition = condition;
84 }
85
86 virtual status_t run()
87 {
88 return test->execute(id, shared, condition);
89 }
90 };
91
92
93 UTEST_MAIN
94 {
95 TestThread t[4];
96 ipc::Condition condition;
97 int shared = 0;
98
99 printf("Starting threads...\n");
100 for (size_t i=0; i<4; ++i)
101 {
102 t[i].bind(this, &shared, int(i+1), &condition);
103 UTEST_ASSERT(t[i].start() == STATUS_OK);
104 }
105
106 // Do some sleep
107 printf("Performing sleep\n");
108 ipc::Thread::sleep(1000);
109
110 printf("Signalling first thread to wake up\n");
111 {
112 UTEST_ASSERT(condition.lock());
113 lsp_finally { condition.unlock(); };
114 ++shared;
115 UTEST_ASSERT(condition.notify_all());
116 }
117
118 printf("Waiting threads for termination...\n");
119 for (size_t i=0; i<4; ++i)
120 t[i].join();
121
122 printf("Analyzing exit status...\n");
123 for (int i=0; i<4; ++i)
124 {
125 UTEST_ASSERT(!t[i].cancelled());
126 UTEST_ASSERT(t[i].get_result() == 1);
127 }
128
129 UTEST_ASSERT(shared == 5);
130 }
131 UTEST_END;
132
133
134