1 /*****************************************************************
3 | Platinum - Thread Tasks
5 | Copyright (c) 2004-2010, Plutinosoft, LLC.
7 | http://www.plutinosoft.com
9 | This program is free software; you can redistribute it and/or
10 | modify it under the terms of the GNU General Public License
11 | as published by the Free Software Foundation; either version 2
12 | of the License, or (at your option) any later version.
14 | OEMs, ISVs, VARs and other distributors that combine and
15 | distribute commercially licensed software with Platinum software
16 | and do not wish to distribute the source code for the commercially
17 | licensed software under version 2, or (at your option) any later
18 | version, of the GNU General Public License (the "GPL") must enter
19 | into a commercial license agreement with Plutinosoft, LLC.
20 | licensing@plutinosoft.com
22 | This program is distributed in the hope that it will be useful,
23 | but WITHOUT ANY WARRANTY; without even the implied warranty of
24 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 | GNU General Public License for more details.
27 | You should have received a copy of the GNU General Public License
28 | along with this program; see the file LICENSE.txt. If not, write to
29 | the Free Software Foundation, Inc.,
30 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
31 | http://www.gnu.org/licenses/gpl-2.0.html
33 ****************************************************************/
39 #ifndef _PLT_THREADTASK_H_
40 #define _PLT_THREADTASK_H_
42 /*----------------------------------------------------------------------
44 +---------------------------------------------------------------------*/
46 #include "PltTaskManager.h"
48 /*----------------------------------------------------------------------
49 | PLT_ThreadTask class
50 +---------------------------------------------------------------------*/
52 The PLT_ThreadTask class is a base class for executing a given task in a worker
53 thread. A PLT_ThreadTask is usually always associated to a PLT_TaskManager
54 which maintains a list to stop and destroy tasks when finished.
56 class PLT_ThreadTask
: public NPT_Runnable
59 friend class PLT_TaskManager
;
62 When a task is not managed by a PLT_TaskManager, the owner must call
63 this to stop and destroy it.
69 Return whether this task is in the process of stopping.
70 @param timeout number of milliseconds to wait
71 @return boolean indicating if the task is stopping
73 virtual bool IsAborting(NPT_Timeout timeout
) {
74 return NPT_SUCCEEDED(m_Abort
.WaitUntilEquals(1, timeout
));
78 Start a task by associating it with a task manager.
79 @param task_manager PLT_TaskManager pointer
80 @param delay optional time interval to wait before launching the new task
81 @param auto_destroy a flag to indicate if the task is owned by someone else
82 and thus should not destroy itself when done.
84 NPT_Result
Start(PLT_TaskManager
* task_manager
= NULL
,
85 NPT_TimeInterval
* delay
= NULL
,
86 bool auto_destroy
= true);
88 Stop the task. This is either called by a task manager or the Kill method.
89 @param blocking Whether the method should block until the task has finished.
91 NPT_Result
Stop(bool blocking
= true);
94 This method to override in derived classes is called when the task is about
97 virtual void DoInit() {}
100 This method to override in derived classes is called when the task is about
103 virtual void DoAbort() {}
106 This method to override in derived classes is the main task loop.
108 virtual void DoRun() {}
111 A PLT_ThreadTask base class is never instantiated directly.
116 The task manager will destroy the task when finished if m_AutoDestroy is
117 true otherwise the owner of this task must use the Kill method.
119 ~PLT_ThreadTask() override
;
122 NPT_Result
StartThread();
124 // NPT_Thread methods
129 PLT_TaskManager
* m_TaskManager
;
133 NPT_SharedVariable m_Started
;
134 NPT_SharedVariable m_Abort
;
135 NPT_Thread
* m_Thread
;
137 NPT_TimeInterval m_Delay
;
140 #endif /* _PLT_THREADTASK_H_ */