1 /*****************************************************************
2 |
3 |   Platinum - Thread Tasks
4 |
5 | Copyright (c) 2004-2010, Plutinosoft, LLC.
6 | All rights reserved.
7 | http://www.plutinosoft.com
8 |
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.
13 |
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
21 |
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.
26 |
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
32 |
33 ****************************************************************/
34 
35 /** @file
36  Runnable Task
37  */
38 
39 #ifndef _PLT_THREADTASK_H_
40 #define _PLT_THREADTASK_H_
41 
42 /*----------------------------------------------------------------------
43 |   includes
44 +---------------------------------------------------------------------*/
45 #include "Neptune.h"
46 #include "PltTaskManager.h"
47 
48 /*----------------------------------------------------------------------
49 |   PLT_ThreadTask class
50 +---------------------------------------------------------------------*/
51 /**
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.
55  */
56 class PLT_ThreadTask : public NPT_Runnable
57 {
58 public:
59     friend class PLT_TaskManager;
60 
61     /**
62      When a task is not managed by a PLT_TaskManager, the owner must call
63      this to stop and destroy it.
64      */
65     NPT_Result Kill();
66 
67 protected:
68     /**
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
72      */
IsAborting(NPT_Timeout timeout)73     virtual bool IsAborting(NPT_Timeout timeout) {
74         return NPT_SUCCEEDED(m_Abort.WaitUntilEquals(1, timeout));
75     }
76 
77     /**
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.
83      */
84     NPT_Result Start(PLT_TaskManager*  task_manager = NULL,
85                      NPT_TimeInterval* delay = NULL,
86                      bool              auto_destroy = true);
87     /**
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.
90      */
91     NPT_Result Stop(bool blocking = true);
92 
93     /**
94      This method to override in derived classes is called when the task is about
95      to start.
96      */
DoInit()97     virtual void DoInit()    {}
98 
99     /**
100      This method to override in derived classes is called when the task is about
101      to stop.
102      */
DoAbort()103     virtual void DoAbort()   {}
104 
105     /**
106      This method to override in derived classes is the main task loop.
107      */
DoRun()108     virtual void DoRun()     {}
109 
110     /**
111      A PLT_ThreadTask base class is never instantiated directly.
112      */
113     PLT_ThreadTask();
114 
115     /**
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.
118      */
119     virtual ~PLT_ThreadTask();
120 
121 private:
122     NPT_Result StartThread();
123 
124     // NPT_Thread methods
125     void Run();
126 
127 protected:
128     // members
129     PLT_TaskManager*    m_TaskManager;
130 
131 private:
132     // members
133     NPT_SharedVariable  m_Started;
134     NPT_SharedVariable  m_Abort;
135     NPT_Thread*         m_Thread;
136     bool                m_AutoDestroy;
137     NPT_TimeInterval    m_Delay;
138 };
139 
140 #endif /* _PLT_THREADTASK_H_ */
141