KRunner

runnerjobs.cpp
1 /*
2  SPDX-FileCopyrightText: 2007, 2009 Ryan P. Bitanga <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "runnerjobs_p.h"
8 
9 #include <QMutexLocker>
10 #include <QTimer>
11 
12 #include "krunner_debug.h"
13 #include "querymatch.h"
14 #include "runnermanager.h"
15 
16 using ThreadWeaver::Job;
18 
19 namespace Plasma
20 {
21 #if KRUNNER_BUILD_DEPRECATED_SINCE(5, 81)
22 DelayedRunnerPolicy::DelayedRunnerPolicy()
23  : QueuePolicy()
24 {
25 }
26 
27 DelayedRunnerPolicy::~DelayedRunnerPolicy()
28 {
29 }
30 
31 DelayedRunnerPolicy &DelayedRunnerPolicy::instance()
32 {
33  static DelayedRunnerPolicy policy;
34  return policy;
35 }
36 
37 bool DelayedRunnerPolicy::canRun(ThreadWeaver::JobPointer job)
38 {
39  QSharedPointer<FindMatchesJob> aJob(job.dynamicCast<FindMatchesJob>());
40  if (QTimer *t = aJob->delayTimer()) {
41  // If the timer is active, the required delay has not been reached
42  return !t->isActive(); // DATA RACE! (with QTimer start/stop from runnermanager.cpp)
43  }
44 
45  return true;
46 }
47 
48 void DelayedRunnerPolicy::free(ThreadWeaver::JobPointer job)
49 {
50  Q_UNUSED(job)
51 }
52 
53 void DelayedRunnerPolicy::release(ThreadWeaver::JobPointer job)
54 {
55  free(job);
56 }
57 
58 void DelayedRunnerPolicy::destructed(ThreadWeaver::JobInterface *job)
59 {
60  Q_UNUSED(job)
61 }
62 #endif
63 
64 DefaultRunnerPolicy::DefaultRunnerPolicy()
65  : QueuePolicy()
66  , m_cap(2)
67 {
68 }
69 
70 DefaultRunnerPolicy::~DefaultRunnerPolicy()
71 {
72 }
73 
74 DefaultRunnerPolicy &DefaultRunnerPolicy::instance()
75 {
76  static DefaultRunnerPolicy policy;
77  return policy;
78 }
79 
80 bool DefaultRunnerPolicy::canRun(ThreadWeaver::JobPointer job)
81 {
82  Plasma::AbstractRunner *runner = job.dynamicCast<FindMatchesJob>()->runner();
83  QMutexLocker l(&m_mutex);
84 
85  if (m_runCounts[runner->name()] > m_cap) {
86  return false;
87  } else {
88  ++m_runCounts[runner->name()];
89  return true;
90  }
91 }
92 
93 void DefaultRunnerPolicy::free(ThreadWeaver::JobPointer job)
94 {
95  Plasma::AbstractRunner *runner = job.dynamicCast<FindMatchesJob>()->runner();
96  QMutexLocker l(&m_mutex);
97 
98  --m_runCounts[runner->name()];
99 }
100 
101 void DefaultRunnerPolicy::release(ThreadWeaver::JobPointer job)
102 {
103  free(job);
104 }
105 
106 void DefaultRunnerPolicy::destructed(ThreadWeaver::JobInterface *job)
107 {
108  Q_UNUSED(job)
109 }
110 
111 ////////////////////
112 // Jobs
113 ////////////////////
114 
115 FindMatchesJob::FindMatchesJob(Plasma::AbstractRunner *runner, Plasma::RunnerContext *context, QObject *)
116  : ThreadWeaver::Job()
117  , m_context(*context, nullptr)
118  , m_runner(runner)
119 {
120  QMutexLocker l(mutex());
121  Q_UNUSED(l);
122 #if KRUNNER_BUILD_DEPRECATED_SINCE(5, 81)
123  if (runner->speed() == Plasma::AbstractRunner::SlowSpeed) {
124  assignQueuePolicy(&DelayedRunnerPolicy::instance());
125  } else {
126  assignQueuePolicy(&DefaultRunnerPolicy::instance());
127  }
128 #else
129  assignQueuePolicy(&DefaultRunnerPolicy::instance());
130 #endif
131 }
132 
133 FindMatchesJob::~FindMatchesJob()
134 {
135 }
136 
137 void FindMatchesJob::run(ThreadWeaver::JobPointer self, ThreadWeaver::Thread *)
138 {
139  if (m_context.isValid()) {
140  m_runner->match(m_context);
141  }
142  Q_EMIT done(self);
143 }
144 
145 int FindMatchesJob::priority() const
146 {
147  return m_runner->priority();
148 }
149 
150 Plasma::AbstractRunner *FindMatchesJob::runner() const
151 {
152  return m_runner;
153 }
154 
155 DelayedJobCleaner::DelayedJobCleaner(const QSet<QSharedPointer<FindMatchesJob>> &jobs, const QSet<AbstractRunner *> &runners)
156  : QObject(Queue::instance())
157  , m_weaver(Queue::instance())
158  , m_jobs(jobs)
159  , m_runners(runners)
160 {
161  connect(m_weaver, &ThreadWeaver::QueueSignals::finished, this, &DelayedJobCleaner::checkIfFinished);
162 
163  for (auto it = m_jobs.constBegin(); it != m_jobs.constEnd(); ++it) {
164  connect(it->data(), &FindMatchesJob::done, this, &DelayedJobCleaner::jobDone);
165  }
166 }
167 
168 DelayedJobCleaner::~DelayedJobCleaner()
169 {
170  qDeleteAll(m_runners);
171 }
172 
173 void DelayedJobCleaner::jobDone(ThreadWeaver::JobPointer job)
174 {
175  auto runJob = job.dynamicCast<FindMatchesJob>();
176 
177  if (!runJob) {
178  return;
179  }
180 
181  m_jobs.remove(runJob);
182 
183  if (m_jobs.isEmpty()) {
184  deleteLater();
185  }
186 }
187 
188 void DelayedJobCleaner::checkIfFinished()
189 {
190  if (m_weaver->isIdle()) {
191  m_jobs.clear();
192  deleteLater();
193  }
194 }
195 
196 } // Plasma namespace
197 
198 #include "moc_runnerjobs_p.cpp"
virtual void release(quint64 objid)
An abstract base class for Plasma Runner plugins.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Speed speed() const
The nominal speed of the runner.
The RunnerContext class provides information related to a search, including the search term,...
Definition: runnercontext.h:31
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sat Dec 2 2023 03:51:00 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.