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 | |
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 | |
49 | void ResourceRestrictionPolicy::setCap(int cap) |
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 | |
61 | bool ResourceRestrictionPolicy::canRun(JobPointer job) |
62 | { |
63 | QMutexLocker l(d->mutex()); |
64 | if (d->customers.size() < d->cap) { |
65 | d->customers.append(t: 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 | |
73 | void ResourceRestrictionPolicy::free(JobPointer job) |
74 | { |
75 | QMutexLocker l(d->mutex()); |
76 | int position = d->customers.indexOf(t: job); |
77 | |
78 | if (position != -1) { |
79 | d->customers.removeAt(i: 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 | |
84 | void ResourceRestrictionPolicy::release(JobPointer job) |
85 | { |
86 | free(job); |
87 | } |
88 | |
89 | void ResourceRestrictionPolicy::destructed(JobInterface *job) |
90 | { |
91 | free(job: ManagedJobPointer<JobInterface>(job)); |
92 | } |
93 | |