| 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 |
#ifndef LSP_PLUG_IN_IPC_ITASK_H_ |
| 23 |
#define LSP_PLUG_IN_IPC_ITASK_H_ |
| 24 |
|
| 25 |
#include <lsp-plug.in/runtime/version.h> |
| 26 |
#include <lsp-plug.in/common/atomic.h> |
| 27 |
#include <lsp-plug.in/common/status.h> |
| 28 |
#include <lsp-plug.in/ipc/IRunnable.h> |
| 29 |
|
| 30 |
namespace lsp |
| 31 |
{ |
| 32 |
namespace ipc |
| 33 |
{ |
| 34 |
class IExecutor; |
| 35 |
|
| 36 |
/** |
| 37 |
* Task interface. Task can be submitted to executor object for asynchronous execution. |
| 38 |
*/ |
| 39 |
class ITask: public IRunnable |
| 40 |
{ |
| 41 |
public: |
| 42 |
enum task_state_t |
| 43 |
{ |
| 44 |
TS_IDLE, |
| 45 |
TS_SUBMITTED, |
| 46 |
TS_RUNNING, |
| 47 |
TS_COMPLETED |
| 48 |
}; |
| 49 |
|
| 50 |
protected: |
| 51 |
ITask *pNext; // Pointer to next task queue |
| 52 |
ipc::IExecutor *pExecutor; // Nested executor if present |
| 53 |
int nCode; // Execution code |
| 54 |
int nState; // Task state |
| 55 |
|
| 56 |
protected: |
| 57 |
// Executor service |
| 58 |
friend class IExecutor; |
| 59 |
|
| 60 |
static inline bool successful(int code) { return code == STATUS_OK; }; |
| 61 |
|
| 62 |
public: |
| 63 |
ITask(); |
| 64 |
ITask(const ITask &) = delete; |
| 65 |
ITask(ITask &&) = delete; |
| 66 |
virtual ~ITask() override; |
| 67 |
|
| 68 |
ITask & operator = (const ITask &) = delete; |
| 69 |
ITask & operator = (ITask &&) = delete; |
| 70 |
|
| 71 |
public: |
| 72 |
virtual status_t run() override; |
| 73 |
|
| 74 |
public: |
| 75 |
/** Get current state of task |
| 76 |
* |
| 77 |
* @return current task state |
| 78 |
*/ |
| 79 |
inline task_state_t state() const { return task_state_t(atomic_load(&nState)); } |
| 80 |
|
| 81 |
/** Check that task status is idle |
| 82 |
* |
| 83 |
* @return true if task status is idle |
| 84 |
*/ |
| 85 |
inline bool idle() const { return state() == TS_IDLE; } |
| 86 |
|
| 87 |
/** Check that task status is submitted |
| 88 |
* |
| 89 |
* @return true if task status is submitted |
| 90 |
*/ |
| 91 |
inline bool submitted() const { return state() == TS_SUBMITTED; } |
| 92 |
|
| 93 |
/** Check that task status is running |
| 94 |
* |
| 95 |
* @return true if task status is running |
| 96 |
*/ |
| 97 |
inline bool running() const { return state() == TS_RUNNING; } |
| 98 |
|
| 99 |
/** Check that task status is completed |
| 100 |
* |
| 101 |
* @return true if task status is completed |
| 102 |
*/ |
| 103 |
inline bool completed() const { return state() == TS_COMPLETED; } |
| 104 |
|
| 105 |
/** Check that execution was successful |
| 106 |
* |
| 107 |
* @return true if execution was successful; |
| 108 |
*/ |
| 109 |
inline bool successful() const { return successful(nCode); } |
| 110 |
|
| 111 |
/** Get last execution code |
| 112 |
* |
| 113 |
* @return last execution code |
| 114 |
*/ |
| 115 |
inline int code() const { return nCode; } |
| 116 |
|
| 117 |
/** |
| 118 |
* Reset task state. The state can be reset only if task is in completed state. |
| 119 |
* |
| 120 |
* @return true if task has been reset |
| 121 |
*/ |
| 122 |
inline bool reset() |
| 123 |
{ |
| 124 |
return atomic_cas(&nState, TS_COMPLETED, TS_IDLE); |
| 125 |
} |
| 126 |
}; |
| 127 |
|
| 128 |
} /* namespace ipc */ |
| 129 |
} /* namespace lsp */ |
| 130 |
|
| 131 |
#endif /* LSP_PLUG_IN_IPC_ITASK_H_ */ |