File indexing completed on 2025-01-05 04:37:12

0001 /*
0002     SPDX-FileCopyrightText: 2005 Joris Guisson <joris.guisson@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #include "taskmanager.h"
0007 #include "dht.h"
0008 #include "nodelookup.h"
0009 #include <QtAlgorithms>
0010 #include <util/log.h>
0011 
0012 using namespace bt;
0013 
0014 namespace dht
0015 {
0016 TaskManager::TaskManager(const DHT *dh_table)
0017     : dh_table(dh_table)
0018     , num_active(0)
0019 {
0020 }
0021 
0022 TaskManager::~TaskManager()
0023 {
0024 }
0025 
0026 void TaskManager::addTask(Task *task)
0027 {
0028     connect(task, &Task::finished, this, &TaskManager::taskFinished);
0029     if (task->isQueued())
0030         queued.append(QPointer<Task>(task));
0031     else
0032         num_active++;
0033 }
0034 
0035 void TaskManager::taskFinished(Task *task)
0036 {
0037     if (!task->isQueued() && num_active > 0)
0038         num_active--;
0039     task->deleteLater();
0040 
0041     while (dh_table->canStartTask() && !queued.isEmpty()) {
0042         QPointer<Task> t = queued.takeFirst();
0043         if (t) {
0044             Out(SYS_DHT | LOG_NOTICE) << "DHT: starting queued task" << endl;
0045             t.data()->start();
0046             num_active++;
0047         }
0048     }
0049 }
0050 
0051 }