Debian Codegraph @

modules/lsp-runtime-lib/src/main/ipc/NativeExecutor.cpp

raw
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: 27 янв. 2016 г.
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/common/debug.h>
23 #include <lsp-plug.in/ipc/NativeExecutor.h>
24
25 namespace lsp
26 {
27 namespace ipc
28 {
29 static constexpr size_t POLL_INTERVAL = 20;
30
31 NativeExecutor::NativeExecutor():
32 hThread(execute, this)
33 {
34 // Initialize list
35 pHead = NULL;
36 pTail = NULL;
37 atomic_init(nLock);
38 }
39
40 NativeExecutor::~NativeExecutor()
41 {
42 }
43
44 status_t NativeExecutor::start()
45 {
46 return hThread.start();
47 }
48
49 bool NativeExecutor::submit(ITask *task)
50 {
51 lsp_trace("submit task=%p", task);
52
53 // Update task state to SUBMITTED
54 if (!change_task_state(task, ITask::TS_IDLE, ITask::TS_SUBMITTED))
55 return false;
56
57 // Try to acquire critical section
58 if (!atomic_trylock(nLock))
59 {
60 set_task_state(task, ITask::TS_IDLE);
61 return false;
62 }
63 lsp_finally { atomic_unlock(nLock); };
64
65 // Critical section acquired, bind new task
66 // Check that queue is empty
67 if (pTail != NULL)
68 link_task(pTail, task);
69 else
70 pHead = task;
71 pTail = task;
72
73 return true;
74 }
75
76 void NativeExecutor::shutdown()
77 {
78 lsp_trace("start shutdown");
79
80 // Wait until the queue is empty
81 while (true)
82 {
83 // Try to acquire critical section
84 if (atomic_trylock(nLock))
85 {
86 // Check that queue is empty
87 if (pHead == NULL)
88 break;
89 // Release critical section
90 atomic_unlock(nLock);
91 }
92
93 ipc::Thread::sleep(POLL_INTERVAL);
94 }
95
96 // Now there are no pending tasks, terminate thread
97 hThread.cancel();
98 hThread.join();
99
100 lsp_trace("shutdown complete");
101 }
102
103 void NativeExecutor::run()
104 {
105 while (!ipc::Thread::is_cancelled())
106 {
107 // Sleep until critical section is acquired
108 while (!atomic_trylock(nLock))
109 {
110 if (ipc::Thread::sleep(POLL_INTERVAL) == STATUS_CANCELLED)
111 return;
112 }
113
114 // Try to get task
115 ITask *task = pHead;
116 if (task == NULL)
117 {
118 // Release critical section
119 atomic_unlock(nLock);
120
121 // Wait for a while
122 if (ipc::Thread::sleep(POLL_INTERVAL) == STATUS_CANCELLED)
123 return;
124 }
125 else
126 {
127 // Remove task from queue
128 pHead = unlink_task(pHead);
129 if (pHead == NULL)
130 pTail = NULL;
131
132 // Release critical section
133 atomic_unlock(nLock);
134
135 // Execute task
136 #ifdef LSP_TRACE
137 lsp_trace("executing task %p", task);
138 const system::time_millis_t start = system::get_time_millis();
139 #endif /* LSP_TRACE */
140
141 run_task(task);
142
143 #ifdef LSP_TRACE
144 const system::time_millis_t end = system::get_time_millis();
145 lsp_trace("executed task %p with code %d, time=%d ms",
146 task, int(task->code()), int(end - start));
147 #endif /* LSP_TRACE */
148 }
149 }
150 }
151
152 status_t NativeExecutor::execute(void *params)
153 {
154 NativeExecutor *self = reinterpret_cast<NativeExecutor *>(params);
155 self->run();
156 return STATUS_OK;
157 }
158
159 } /* namespace ipc */
160 } /* namespace lsp */