Mercurial > projects > hoofbaby
comparison deps/Platinum/Source/Core/PltTaskManager.cpp @ 0:3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
author | fraserofthenight |
---|---|
date | Mon, 06 Jul 2009 08:06:28 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3425707ddbf6 |
---|---|
1 /***************************************************************** | |
2 | | |
3 | Platinum - Task Manager | |
4 | | |
5 | Copyright (c) 2004-2008, 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 | | |
21 | This program is distributed in the hope that it will be useful, | |
22 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
23 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
24 | GNU General Public License for more details. | |
25 | | |
26 | You should have received a copy of the GNU General Public License | |
27 | along with this program; see the file LICENSE.txt. If not, write to | |
28 | the Free Software Foundation, Inc., | |
29 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
30 | http://www.gnu.org/licenses/gpl-2.0.html | |
31 | | |
32 ****************************************************************/ | |
33 | |
34 /*---------------------------------------------------------------------- | |
35 | includes | |
36 +---------------------------------------------------------------------*/ | |
37 #include "PltTaskManager.h" | |
38 #include "PltThreadTask.h" | |
39 | |
40 NPT_SET_LOCAL_LOGGER("platinum.core.taskmanager") | |
41 | |
42 /*---------------------------------------------------------------------- | |
43 | PLT_TaskManager::PLT_TaskManager | |
44 +---------------------------------------------------------------------*/ | |
45 PLT_TaskManager::PLT_TaskManager(NPT_Cardinal max_items /* = 0 */) : | |
46 m_Queue(max_items?new NPT_Queue<int>(max_items):NULL) | |
47 { | |
48 } | |
49 | |
50 /*---------------------------------------------------------------------- | |
51 | PLT_TaskManager::~PLT_TaskManager | |
52 +---------------------------------------------------------------------*/ | |
53 PLT_TaskManager::~PLT_TaskManager() | |
54 { | |
55 StopAllTasks(); | |
56 } | |
57 | |
58 /*---------------------------------------------------------------------- | |
59 | PLT_TaskManager::StartTask | |
60 +---------------------------------------------------------------------*/ | |
61 NPT_Result | |
62 PLT_TaskManager::StartTask(PLT_ThreadTask* task, | |
63 NPT_TimeInterval* delay /* = NULL*/, | |
64 bool auto_destroy /* = true */) | |
65 { | |
66 return task->Start(this, delay, auto_destroy); | |
67 } | |
68 | |
69 /*---------------------------------------------------------------------- | |
70 | PLT_TaskManager::StopAllTasks | |
71 +---------------------------------------------------------------------*/ | |
72 NPT_Result | |
73 PLT_TaskManager::StopAllTasks() | |
74 { | |
75 // unblock the queue if any | |
76 if (m_Queue) { | |
77 NPT_Queue<int>* queue = m_Queue; | |
78 m_Queue = NULL; | |
79 delete queue; | |
80 } | |
81 | |
82 // stop all tasks first but don't block | |
83 // otherwise when RemoveTask is called by PLT_ThreadTask::Run | |
84 // it will deadlock with m_TasksLock | |
85 { | |
86 NPT_AutoLock lock(m_TasksLock); | |
87 NPT_List<PLT_ThreadTask*>::Iterator task = m_Tasks.GetFirstItem(); | |
88 while (task) { | |
89 (*task)->Stop(false); | |
90 ++task; | |
91 } | |
92 } | |
93 | |
94 // then wait for list to become empty | |
95 // as tasks remove themselves from the list | |
96 while (1) { | |
97 { | |
98 NPT_AutoLock lock(m_TasksLock); | |
99 if (m_Tasks.GetItemCount() == 0) | |
100 return NPT_SUCCESS; | |
101 } | |
102 | |
103 NPT_System::Sleep(NPT_TimeInterval(0, 10000)); | |
104 } | |
105 | |
106 return NPT_SUCCESS; | |
107 } | |
108 | |
109 /*---------------------------------------------------------------------- | |
110 | PLT_TaskManager::AddTask | |
111 +---------------------------------------------------------------------*/ | |
112 NPT_Result | |
113 PLT_TaskManager::AddTask(PLT_ThreadTask* task) | |
114 { | |
115 if (m_Queue) { | |
116 NPT_CHECK_SEVERE(m_Queue->Push(new int)); | |
117 } | |
118 | |
119 { | |
120 NPT_AutoLock lock(m_TasksLock); | |
121 return m_Tasks.Add(task); | |
122 } | |
123 } | |
124 | |
125 /*---------------------------------------------------------------------- | |
126 | PLT_TaskManager::RemoveTask | |
127 +---------------------------------------------------------------------*/ | |
128 // called by a PLT_ThreadTask::Run when done | |
129 NPT_Result | |
130 PLT_TaskManager::RemoveTask(PLT_ThreadTask* task) | |
131 { | |
132 if (m_Queue) { | |
133 int* val = NULL; | |
134 if (NPT_SUCCEEDED(m_Queue->Pop(val))) | |
135 delete val; | |
136 } | |
137 | |
138 { | |
139 NPT_AutoLock lock(m_TasksLock); | |
140 m_Tasks.Remove(task); | |
141 } | |
142 | |
143 // cleanup task only if auto-destroy flag was set | |
144 if (task->m_AutoDestroy) delete task; | |
145 | |
146 return NPT_SUCCESS; | |
147 } |