1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
//
// MIT License
// Copyright (c) 2020 Jonathan R. Madsen
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED
// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// ---------------------------------------------------------------
// Tasking class header
// Class Description:
// Abstract base class for creating a task queue used by
// ThreadPool
// ---------------------------------------------------------------
// Author: Jonathan Madsen
// ---------------------------------------------------------------
#pragma once
#include "PTL/Globals.hh"
#include "PTL/Threading.hh"
#include "PTL/Types.hh"
#include <cstddef>
#include <memory>
#include <set>
#include <tuple>
#include <type_traits>
#include <utility>
namespace PTL
{
class VTask;
class ThreadPool;
class ThreadData;
class VUserTaskQueue
{
public:
typedef std::shared_ptr<VTask> task_pointer;
typedef std::atomic<intmax_t> AtomicInt;
typedef uintmax_t size_type;
typedef std::function<void()> function_type;
typedef std::set<ThreadId> ThreadIdSet;
public:
// Constructor - accepting the number of workers
explicit VUserTaskQueue(intmax_t nworkers = -1);
// Virtual destructors are required by abstract classes
// so add it by default, just in case
virtual ~VUserTaskQueue() = default;
public:
// Virtual function for getting a task from the queue
// parameters:
// 1. int - get from specific sub-queue
// 2. int - number of iterations
// returns:
// VTask* - a task or nullptr
virtual task_pointer GetTask(intmax_t subq = -1, intmax_t nitr = -1) = 0;
// Virtual function for inserting a task into the queue
// parameters:
// 1. VTask* - task to insert
// 2. int - sub-queue to inserting into
// return:
// int - subqueue inserted into
virtual intmax_t InsertTask(task_pointer&&, ThreadData* = nullptr,
intmax_t subq = -1) PTL_NO_SANITIZE_THREAD = 0;
// Overload this function to hold threads
virtual void Wait() = 0;
virtual intmax_t GetThreadBin() const = 0;
virtual void resize(intmax_t) = 0;
// these are used for stanard checking
virtual size_type size() const = 0;
virtual bool empty() const = 0;
virtual size_type bin_size(size_type bin) const = 0;
virtual bool bin_empty(size_type bin) const = 0;
// these are for slower checking, default to returning normal size()/empty
virtual size_type true_size() const { return size(); }
virtual bool true_empty() const { return empty(); }
// a method of executing a specific function on all threads
virtual void ExecuteOnAllThreads(ThreadPool* tp, function_type f) = 0;
virtual void ExecuteOnSpecificThreads(ThreadIdSet tid_set, ThreadPool* tp,
function_type f) = 0;
intmax_t workers() const { return m_workers; }
virtual VUserTaskQueue* clone() = 0;
protected:
intmax_t m_workers = 0;
};
} // namespace PTL
|