1 /*
2  * Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
3  *           (C) 2020 Vladimir Sadovnikov <sadko4u@gmail.com>
4  *
5  * This file is part of lsp-plugins
6  * Created on: 27 янв. 2016 г.
7  *
8  * lsp-plugins 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-plugins 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-plugins. If not, see <https://www.gnu.org/licenses/>.
20  */
21 
22 #ifndef CORE_IPC_ITASK_H_
23 #define CORE_IPC_ITASK_H_
24 
25 #include <core/status.h>
26 #include <core/ipc/IRunnable.h>
27 
28 namespace lsp
29 {
30     namespace ipc
31     {
32         class ITask: public IRunnable
33         {
34             public:
35                 enum task_state_t
36                 {
37                     TS_IDLE,
38                     TS_SUBMITTED,
39                     TS_RUNNING,
40                     TS_COMPLETED
41                 };
42 
43             protected:
44                 // Task linking
45                 ITask      *pNext;
46                 int         nCode;
47 
48                 // Task state
49                 volatile task_state_t    nState;
50 
51                 // Executor service
52                 friend class IExecutor;
53 
successful(int code)54                 static inline bool successful(int code)     { return code == STATUS_OK; };
55 
56             public:
57                 ITask();
58                 virtual ~ITask();
59 
60             public:
61                 virtual status_t run();
62 
63             public:
64                 /** Check that task status is idle
65                  *
66                  * @return true if task status is idle
67                  */
idle()68                 inline bool idle() const        { return nState == TS_IDLE;         };
69 
70                 /** Check that task status is submitted
71                  *
72                  * @return true if task status is submitted
73                  */
submitted()74                 inline bool submitted() const   { return nState == TS_SUBMITTED;    };
75 
76                 /** Check that task status is running
77                  *
78                  * @return true if task status is running
79                  */
running()80                 inline bool running() const     { return nState == TS_RUNNING;      };
81 
82                 /** Check that task status is completed
83                  *
84                  * @return true if task status is completed
85                  */
completed()86                 inline bool completed() const   { return nState == TS_COMPLETED;    };
87 
88                 /** Check that execution was successful
89                  *
90                  * @return true if execution was successful;
91                  */
successful()92                 inline bool successful() const  { return successful(nCode);         };
93 
94                 /** Get last execution code
95                  *
96                  * @return last execution code
97                  */
code()98                 inline int code() const         { return nCode;                     };
99 
100                 /** Get current state of task
101                  *
102                  * @return current task state
103                  */
state()104                 inline task_state_t state() const {return nState;                   };
105 
106                 /** Reset task state from COMPLETED to IDLE
107                  *
108                  * @return task state
109                  */
reset()110                 inline bool reset()
111                 {
112                     if (nState != TS_COMPLETED)
113                         return false;
114                     nState      = TS_IDLE;
115                     return true;
116                 }
117         };
118 
119     } /* namespace ipc */
120 
121 } /* namespace lsp */
122 
123 #endif /* CORE_IPC_ITASK_H_ */
124