File indexing completed on 2024-04-28 04:01:23

0001 /* -*- C++ -*-
0002     This file declares the ResourceRestrictionPolicy class.
0003 
0004     SPDX-FileCopyrightText: 2004-2013 Mirko Boehm <mirko@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 
0008     $Id: Job.h 32 2005-08-17 08:38:01Z mirko $
0009 */
0010 
0011 #ifndef RESOURCE_RESTRICTION_POLICY_H
0012 #define RESOURCE_RESTRICTION_POLICY_H
0013 
0014 #include <QtGlobal>
0015 
0016 #include "jobpointer.h"
0017 #include "queuepolicy.h"
0018 
0019 namespace ThreadWeaver
0020 {
0021 class JobInterface;
0022 
0023 /** @brief ResourceRestrictionPolicy is used to limit the number of concurrent accesses to the same resource.
0024  *
0025  *  If a set of Jobs accesses a resource that can be overloaded, this may degrade application performance. For
0026  *  example, loading too many files from the hard disc at the same time may lead to longer load times.
0027  *  ResourceRestrictionPolicy can be used to cap the number of accesses. Resource restriction policies are
0028  *  shared between the affected jobs. All jobs that share a resource restriction policy have to acquire
0029  *  permission from the policy before they can run. In this way, resource restrictions can be compared to
0030  *  semaphores, only that they require no locking at the thread level.
0031  *  The  example uses a resource restriction to limit the number of images files that are loaded from
0032  *  the disk at the same time.
0033  */
0034 
0035 class THREADWEAVER_EXPORT ResourceRestrictionPolicy : public QueuePolicy
0036 {
0037 public:
0038     explicit ResourceRestrictionPolicy(int cap = 0);
0039     ~ResourceRestrictionPolicy() override;
0040 
0041     /** @brief Cap the number of simultaneously executing jobs.
0042      *  Capping the amount of jobs will make sure that at max the number of jobs executing at any time is
0043      *  limited to the capped amount. Note that immediately after setting the amount of running jobs may be
0044      *  higher than the set amount. This setting only limits the starting of new jobs.
0045      *  @param newCap the new cap to limit the amount of parallel jobs.
0046      */
0047     void setCap(int newCap);
0048     int cap() const;
0049     bool canRun(JobPointer) override;
0050     void free(JobPointer) override;
0051     void release(JobPointer) override;
0052     void destructed(JobInterface *job) override;
0053 
0054 private:
0055     class Private;
0056     Private *const d;
0057 };
0058 
0059 }
0060 
0061 #endif // RESOURCE_RESTRICTION_POLICY_H