| 216 | |
| 217 | |
| 218 | bool CThreadScheduler::DoAddTask(CThreadTask* task, bool overwrite) |
| 219 | { |
| 220 | // GetTick is too lowres, so we just use a counter to ensure that |
| 221 | // the sorted order will match the order in which the tasks were added. |
| 222 | static unsigned taskAge = 0; |
| 223 | |
| 224 | // Get the map for this task type, implicitly creating it as needed. |
| 225 | CDescMap& map = m_taskDescs[task->GetType()]; |
| 226 | |
| 227 | CDescMap::value_type entry(task->GetDesc(), task); |
| 228 | if (map.insert(entry).second) { |
| 229 | AddDebugLogLineN(logThreads, "Task scheduled: " + task->GetType() + " - " + task->GetDesc()); |
| 230 | m_tasks.push_back(CEntryPair(task, taskAge++)); |
| 231 | m_tasksDirty = true; |
| 232 | } else if (overwrite) { |
| 233 | AddDebugLogLineN(logThreads, "Task overwritten: " + task->GetType() + " - " + task->GetDesc()); |
| 234 | |
| 235 | CThreadTask* existingTask = map[task->GetDesc()]; |
| 236 | if (m_currentTask == existingTask) { |
| 237 | // The duplicate is already being executed, abort it. |
| 238 | m_currentTask->m_abort = true; |
| 239 | } else { |
| 240 | // Task not yet started, simply remove and delete. |
| 241 | wxCHECK2(map.erase(existingTask->GetDesc()), /* Do nothing. */); |
| 242 | delete existingTask; |
| 243 | } |
| 244 | |
| 245 | m_tasks.push_back(CEntryPair(task, taskAge++)); |
| 246 | map[task->GetDesc()] = task; |
| 247 | m_tasksDirty = true; |
| 248 | } else { |
| 249 | AddDebugLogLineN(logThreads, "Duplicate task, discarding: " + task->GetType() + " - " + task->GetDesc()); |
| 250 | delete task; |
| 251 | return false; |
| 252 | } |
| 253 | |
| 254 | if (s_running) { |
| 255 | CreateSchedulerThread(); |
| 256 | } |
| 257 | |
| 258 | return true; |
| 259 | } |
| 260 | |
| 261 | |
| 262 | void* CThreadScheduler::Entry() |