ThreadWeaver

resourcerestrictionpolicy.cpp
1/* -*- C++ -*-
2 This file implements the ResourceRestrictionPolicy class.
3
4 SPDX-FileCopyrightText: 2004-2013 Mirko Boehm <mirko@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7
8 $Id: Job.h 32 2005-08-17 08:38:01Z mirko $
9*/
10
11#include "resourcerestrictionpolicy.h"
12
13#include <QList>
14#include <QMutex>
15
16#include "debuggingaids.h"
17#include "managedjobpointer.h"
18
19using namespace ThreadWeaver;
20
21class Q_DECL_HIDDEN ResourceRestrictionPolicy::Private
22{
23public:
24 Private(int theCap)
25 : cap(theCap)
26 {
27 }
28 QMutex *mutex()
29 {
30 return &mutex_;
31 }
32
33 int cap;
34 QList<JobPointer> customers;
35 QMutex mutex_;
36};
37
38ResourceRestrictionPolicy::ResourceRestrictionPolicy(int cap)
39 : QueuePolicy()
40 , d(new Private(cap))
41{
42}
43
44ResourceRestrictionPolicy::~ResourceRestrictionPolicy()
45{
46 delete d;
47}
48
50{
51 QMutexLocker l(d->mutex());
52 d->cap = cap;
53}
54
55int ResourceRestrictionPolicy::cap() const
56{
57 QMutexLocker l(d->mutex());
58 return d->cap;
59}
60
62{
63 QMutexLocker l(d->mutex());
64 if (d->customers.size() < d->cap) {
65 d->customers.append(job);
66 TWDEBUG(4, "ResourceRestrictionPolicy::canRun: job %p added, %i customers (cap %i).\n", (void *)job.data(), d->customers.count(), d->cap);
67 return true;
68 } else {
69 return false;
70 }
71}
72
74{
75 QMutexLocker l(d->mutex());
76 int position = d->customers.indexOf(job);
77
78 if (position != -1) {
79 d->customers.removeAt(position);
80 TWDEBUG(4, "ResourceRestrictionPolicy::free: job %p completed, %i customers (cap %i).\n", (void *)job.data(), d->customers.count(), d->cap);
81 }
82}
83
88
90{
91 free(ManagedJobPointer<JobInterface>(job));
92}
QueuePolicy is an interface for customizations of the queueing behaviour of jobs.
Definition queuepolicy.h:39
ResourceRestrictionPolicy is used to limit the number of concurrent accesses to the same resource.
void release(JobPointer) override
release() is called if canRun() returned true, but the job has not been executed for external reasons...
bool canRun(JobPointer) override
canRun() is called before the job is executed.
void destructed(JobInterface *job) override
destructing() is called when a Job that has this queue policy assigned gets destructed.
void setCap(int newCap)
Cap the number of simultaneously executing jobs.
void free(JobPointer) override
free() is called after the job has been executed.
T * data() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:34:40 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.