11/*
2- * Copyright (C) 2024 Linux Studio Plugins Project <https://lsp-plug.in/>
3- * (C) 2024 Vladimir Sadovnikov <sadko4u@gmail.com>
2+ * Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/>
3+ * (C) 2025 Vladimir Sadovnikov <sadko4u@gmail.com>
44 *
55 * This file is part of lsp-runtime-lib
66 * Created on: 27 янв. 2016 г.
2323#define LSP_PLUG_IN_IPC_ITASK_H_
2424
2525#include < lsp-plug.in/runtime/version.h>
26+ #include < lsp-plug.in/common/atomic.h>
2627#include < lsp-plug.in/common/status.h>
2728#include < lsp-plug.in/ipc/IRunnable.h>
2829
@@ -50,7 +51,7 @@ namespace lsp
5051 ITask *pNext; // Pointer to next task queue
5152 ipc::IExecutor *pExecutor; // Nested executor if present
5253 int nCode; // Execution code
53- volatile task_state_t nState; // Task state
54+ int nState; // Task state
5455
5556 protected:
5657 // Executor service
@@ -62,67 +63,65 @@ namespace lsp
6263 ITask ();
6364 ITask (const ITask &) = delete ;
6465 ITask (ITask &&) = delete ;
65- virtual ~ITask ();
66+ virtual ~ITask () override ;
6667
6768 ITask & operator = (const ITask &) = delete ;
6869 ITask & operator = (ITask &&) = delete ;
6970
7071 public:
71- virtual status_t run ();
72+ virtual status_t run () override ;
7273
7374 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+
7481 /* * Check that task status is idle
7582 *
7683 * @return true if task status is idle
7784 */
78- inline bool idle () const { return nState == TS_IDLE; };
85+ inline bool idle () const { return state () == TS_IDLE; }
7986
8087 /* * Check that task status is submitted
8188 *
8289 * @return true if task status is submitted
8390 */
84- inline bool submitted () const { return nState == TS_SUBMITTED; };
91+ inline bool submitted () const { return state () == TS_SUBMITTED; }
8592
8693 /* * Check that task status is running
8794 *
8895 * @return true if task status is running
8996 */
90- inline bool running () const { return nState == TS_RUNNING; };
97+ inline bool running () const { return state () == TS_RUNNING; }
9198
9299 /* * Check that task status is completed
93100 *
94101 * @return true if task status is completed
95102 */
96- inline bool completed () const { return nState == TS_COMPLETED; };
103+ inline bool completed () const { return state () == TS_COMPLETED; }
97104
98105 /* * Check that execution was successful
99106 *
100107 * @return true if execution was successful;
101108 */
102- inline bool successful () const { return successful (nCode); };
109+ inline bool successful () const { return successful (nCode); }
103110
104111 /* * Get last execution code
105112 *
106113 * @return last execution code
107114 */
108- inline int code () const { return nCode; };
109-
110- /* * Get current state of task
111- *
112- * @return current task state
113- */
114- inline task_state_t state () const {return nState; };
115+ inline int code () const { return nCode; }
115116
116- /* * Reset task state
117+ /* *
118+ * Reset task state. The state can be reset only if task is in completed state.
117119 *
118- * @return task state
120+ * @return true if task has been reset
119121 */
120122 inline bool reset ()
121123 {
122- if (nState != TS_COMPLETED)
123- return false ;
124- nState = TS_IDLE;
125- return true ;
124+ return atomic_cas (&nState, TS_COMPLETED, TS_IDLE);
126125 }
127126 };
128127
0 commit comments