| 1 | /* -*- C++ -*- |
| 2 | This file declares 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 | #ifndef RESOURCE_RESTRICTION_POLICY_H |
| 12 | #define RESOURCE_RESTRICTION_POLICY_H |
| 13 | |
| 14 | #include <QtGlobal> |
| 15 | |
| 16 | #include "jobpointer.h" |
| 17 | #include "queuepolicy.h" |
| 18 | |
| 19 | namespace ThreadWeaver |
| 20 | { |
| 21 | class JobInterface; |
| 22 | |
| 23 | /*! |
| 24 | * \class ThreadWeaver::ResourceRestrictionPolicy |
| 25 | * \inheaderfile ThreadWeaver/ResourceRestrictionPolicy |
| 26 | * \inmodule ThreadWeaver |
| 27 | * |
| 28 | * \brief ResourceRestrictionPolicy is used to limit the number of concurrent accesses to the same resource. |
| 29 | * |
| 30 | * If a set of Jobs accesses a resource that can be overloaded, this may degrade application performance. For |
| 31 | * example, loading too many files from the hard disc at the same time may lead to longer load times. |
| 32 | * ResourceRestrictionPolicy can be used to cap the number of accesses. Resource restriction policies are |
| 33 | * shared between the affected jobs. All jobs that share a resource restriction policy have to acquire |
| 34 | * permission from the policy before they can run. In this way, resource restrictions can be compared to |
| 35 | * semaphores, only that they require no locking at the thread level. |
| 36 | * The example uses a resource restriction to limit the number of images files that are loaded from |
| 37 | * the disk at the same time. |
| 38 | */ |
| 39 | class THREADWEAVER_EXPORT ResourceRestrictionPolicy : public QueuePolicy |
| 40 | { |
| 41 | public: |
| 42 | /*! |
| 43 | */ |
| 44 | explicit ResourceRestrictionPolicy(int cap = 0); |
| 45 | ~ResourceRestrictionPolicy() override; |
| 46 | |
| 47 | /*! \brief Cap the number of simultaneously executing jobs. |
| 48 | * Capping the amount of jobs will make sure that at max the number of jobs executing at any time is |
| 49 | * limited to the capped amount. Note that immediately after setting the amount of running jobs may be |
| 50 | * higher than the set amount. This setting only limits the starting of new jobs. |
| 51 | * \a newCap the new cap to limit the amount of parallel jobs. |
| 52 | */ |
| 53 | void setCap(int newCap); |
| 54 | /*! |
| 55 | */ |
| 56 | int cap() const; |
| 57 | bool canRun(JobPointer) override; |
| 58 | void free(JobPointer) override; |
| 59 | void release(JobPointer) override; |
| 60 | void destructed(JobInterface *job) override; |
| 61 | |
| 62 | private: |
| 63 | class Private; |
| 64 | Private *const d; |
| 65 | }; |
| 66 | |
| 67 | } |
| 68 | |
| 69 | #endif // RESOURCE_RESTRICTION_POLICY_H |
| 70 | |