ThreadWeaver

resourcerestrictionpolicy.cpp
1 /* -*- C++ -*-
2  This file implements the ResourceRestrictionPolicy class.
3 
4  SPDX-FileCopyrightText: 2004-2013 Mirko Boehm <[email protected]>
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 
19 using namespace ThreadWeaver;
20 
21 class Q_DECL_HIDDEN ResourceRestrictionPolicy::Private
22 {
23 public:
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 
38 ResourceRestrictionPolicy::ResourceRestrictionPolicy(int cap)
39  : QueuePolicy()
40  , d(new Private(cap))
41 {
42 }
43 
44 ResourceRestrictionPolicy::~ResourceRestrictionPolicy()
45 {
46  delete d;
47 }
48 
50 {
51  QMutexLocker l(d->mutex());
52  d->cap = cap;
53 }
54 
55 int 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 
85 {
86  free(job);
87 }
88 
90 {
91  free(ManagedJobPointer<JobInterface>(job));
92 }
T * data() const const
void setCap(int newCap)
Cap the number of simultaneously executing jobs.
void free(JobPointer) override
free() is called after the job has been executed.
QueuePolicy is an interface for customizations of the queueing behaviour of jobs.
Definition: queuepolicy.h:38
bool canRun(JobPointer) override
canRun() is called before the job is executed.
void release(JobPointer) override
release() is called if canRun() returned true, but the job has not been executed for external reasons...
void destructed(JobInterface *job) override
destructing() is called when a Job that has this queue policy assigned gets destructed.
ResourceRestrictionPolicy is used to limit the number of concurrent accesses to the same resource.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Feb 7 2023 04:09:38 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.