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

0001 /* -*- C++ -*-
0002     This file declares the QueuePolicy class.
0003 
0004     SPDX-FileCopyrightText: 2004, 2005, 2006 Mirko Boehm <mirko@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 
0008     $Id: DebuggingAids.h 30 2005-08-16 16:16:04Z mirko $
0009 */
0010 
0011 #ifndef QUEUEPOLICY_H
0012 #define QUEUEPOLICY_H
0013 
0014 #include "jobpointer.h"
0015 #include "threadweaver_export.h"
0016 
0017 namespace ThreadWeaver
0018 {
0019 class JobInterface;
0020 
0021 /** @brief QueuePolicy is an interface for customizations of the queueing behaviour of jobs.
0022  *
0023  *  A job can have a number of queue policies assigned. In that case, the job is only
0024  *  executed when the method canRun() of all assigned policies return true. For every call to
0025  *  canRun() that returns true, it is guaranteed that the method free() or the method release()
0026  *  is called. Calling free() means the job has been executed, while calling release() means
0027  *  the job was not executed for external reasons, and will be tried later on.
0028  *
0029  *  As an example, dependencies can be implemented using a QueuePolicy: canRun() returns true
0030  *  when the job has no unresolved dependencies. free() and release() are empty.
0031  *
0032  *  A job can have multiple queue policies assigned, and will only be executed if all of them
0033  *  return true from canRun() within the same execution attempt. Jobs only keep a reference to the
0034  *  QueuePolicy. Therefore, the same policy object can be assigned to multiple jobs and this way
0035  *  control the way all those jobs are executed. Jobs never assume ownership of their assigned queue
0036  *  policies.
0037  */
0038 class THREADWEAVER_EXPORT QueuePolicy
0039 {
0040 public:
0041     virtual ~QueuePolicy()
0042     {
0043     }
0044 
0045     /** @brief canRun() is called before the job is executed.
0046      *  The job will only be executed if canRun() returns true.
0047      */
0048     virtual bool canRun(JobPointer) = 0;
0049 
0050     /** @brief free() is called after the job has been executed.
0051      *  It is guaranteed that free is called only after canRun()
0052      *  returned true at an earlier time.
0053      */
0054     virtual void free(JobPointer) = 0;
0055 
0056     /** @brief release() is called if canRun() returned true, but the job has not been executed for external reasons.
0057      *
0058      *  For example, a second QueuePolicy could have returned false from canRun() for the same job.
0059      */
0060     virtual void release(JobPointer) = 0;
0061 
0062     /** @brief destructing() is called when a Job that has this queue policy assigned gets destructed.
0063      */
0064     virtual void destructed(JobInterface *job) = 0;
0065 };
0066 
0067 }
0068 
0069 #endif