| 1 |
/* |
| 2 |
* Copyright (C) 2024 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2024 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 |
#ifndef LSP_PLUG_IN_IPC_IEXECUTOR_H_ |
| 23 |
#define LSP_PLUG_IN_IPC_IEXECUTOR_H_ |
| 24 |
|
| 25 |
#include <lsp-plug.in/runtime/version.h> |
| 26 |
#include <lsp-plug.in/common/types.h> |
| 27 |
#include <lsp-plug.in/ipc/ITask.h> |
| 28 |
|
| 29 |
namespace lsp |
| 30 |
{ |
| 31 |
namespace ipc |
| 32 |
{ |
| 33 |
class IExecutor |
| 34 |
{ |
| 35 |
protected: |
| 36 |
static inline void set_task_state(ITask *task, ITask::task_state_t state) |
| 37 |
{ |
| 38 |
atomic_store(&task->nState, state); |
| 39 |
} |
| 40 |
|
| 41 |
static inline bool change_task_state(ITask *task, ITask::task_state_t old_state, ITask::task_state_t new_state) |
| 42 |
{ |
| 43 |
return atomic_cas(&task->nState, old_state, new_state); |
| 44 |
} |
| 45 |
|
| 46 |
static inline void link_task(ITask *tail, ITask *link) |
| 47 |
{ |
| 48 |
tail->pNext = link; |
| 49 |
link->pNext = NULL; |
| 50 |
} |
| 51 |
|
| 52 |
static inline ITask *unlink_task(ITask *task) |
| 53 |
{ |
| 54 |
ITask *next = task->pNext; |
| 55 |
task->pNext = NULL; |
| 56 |
return next; |
| 57 |
} |
| 58 |
|
| 59 |
static inline void set_next_task(ITask *tail, ITask *next) |
| 60 |
{ |
| 61 |
tail->pNext = next; |
| 62 |
} |
| 63 |
|
| 64 |
static inline ITask *get_next_task(ITask *task) |
| 65 |
{ |
| 66 |
return task->pNext; |
| 67 |
} |
| 68 |
|
| 69 |
static inline IExecutor *get_executor(ITask *task) |
| 70 |
{ |
| 71 |
return task->pExecutor; |
| 72 |
} |
| 73 |
|
| 74 |
static inline void set_executor(ITask *task, IExecutor *executor) |
| 75 |
{ |
| 76 |
task->pExecutor = executor; |
| 77 |
} |
| 78 |
|
| 79 |
void run_task(ITask *task); |
| 80 |
|
| 81 |
public: |
| 82 |
IExecutor(); |
| 83 |
IExecutor(const IExecutor &) = delete; |
| 84 |
IExecutor(IExecutor &&) = delete; |
| 85 |
virtual ~IExecutor(); |
| 86 |
|
| 87 |
IExecutor &operator = (const IExecutor & src) = delete; |
| 88 |
IExecutor &operator = (IExecutor && src) = delete; |
| 89 |
|
| 90 |
protected: |
| 91 |
virtual void task_finished(ITask *task); |
| 92 |
|
| 93 |
public: |
| 94 |
/** Submit task for execution |
| 95 |
* |
| 96 |
* @param task task to execute |
| 97 |
* @return true if task was submitted |
| 98 |
*/ |
| 99 |
virtual bool submit(ITask *task); |
| 100 |
|
| 101 |
/** Shutdown executor service |
| 102 |
* The method must return only when all tasks |
| 103 |
* have been completed or terminated |
| 104 |
* |
| 105 |
*/ |
| 106 |
virtual void shutdown(); |
| 107 |
}; |
| 108 |
|
| 109 |
} /* namespace ipc */ |
| 110 |
} /* namespace lsp */ |
| 111 |
|
| 112 |
#endif /* LSP_PLUG_IN_IPC_IEXECUTOR_H_ */ |