Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
VTask.hh
Go to the documentation of this file.
1//
2// MIT License
3// Copyright (c) 2020 Jonathan R. Madsen
4// Permission is hereby granted, free of charge, to any person obtaining a copy
5// of this software and associated documentation files (the "Software"), to deal
6// in the Software without restriction, including without limitation the rights
7// to use, copy, modify, merge, publish, distribute, sublicense, and
8// copies of the Software, and to permit persons to whom the Software is
9// furnished to do so, subject to the following conditions:
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED
12// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
13// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
15// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
16// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
17// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18//
19// Tasking class header file
20//
21// Class Description:
22//
23// This file creates an abstract base class for the thread-pool tasking
24// system
25//
26// ---------------------------------------------------------------
27// Author: Jonathan Madsen (Feb 13th 2018)
28// ---------------------------------------------------------------
29
30#pragma once
31
32#include "PTL/AutoLock.hh"
33#include "PTL/TaskAllocator.hh"
34#include "PTL/Threading.hh"
35
36#include <atomic>
37#include <cstddef>
38#include <cstdint>
39#include <functional>
40#include <future>
41#include <string>
42#include <thread>
43#include <tuple>
44#include <utility>
45
46namespace PTL
47{
48class VTaskGroup;
49class ThreadPool;
50
51//======================================================================================//
52
53/// \brief VTask is the abstract class stored in thread_pool
54class VTask
55{
56public:
57 typedef std::thread::id tid_type;
58 typedef size_t size_type;
60 typedef std::atomic_uintmax_t count_t;
61 typedef VTask* iterator;
62 typedef const VTask* const_iterator;
63 typedef std::function<void()> void_func_t;
64
65public:
66 VTask();
67 explicit VTask(VTaskGroup* task_group);
68 explicit VTask(ThreadPool* pool);
69 virtual ~VTask();
70
71public:
72 // execution operator
73 virtual void operator()() = 0;
74
75public:
76 // used by thread_pool
77 void operator--();
78 virtual bool is_native_task() const;
79 virtual ThreadPool* pool() const;
80 VTaskGroup* group() const { return m_group; }
81
82public:
83 // used by task tree
84 iterator begin() { return this; }
85 iterator end() { return this + 1; }
86
87 const_iterator begin() const { return this; }
88 const_iterator end() const { return this + 1; }
89
90 const_iterator cbegin() const { return this; }
91 const_iterator cend() const { return this + 1; }
92
93 intmax_t& depth() { return m_depth; }
94 const intmax_t& depth() const { return m_depth; }
95
96protected:
97 static tid_type this_tid() { return std::this_thread::get_id(); }
98
99protected:
100 intmax_t m_depth;
104};
105
106//======================================================================================//
107
108} // namespace PTL
VTask is the abstract class stored in thread_pool.
Definition: VTask.hh:55
iterator begin()
Definition: VTask.hh:84
virtual bool is_native_task() const
Definition: VTask.cc:95
std::atomic_uintmax_t count_t
Definition: VTask.hh:60
VTaskGroup * m_group
Definition: VTask.hh:101
void_func_t m_func
Definition: VTask.hh:103
iterator end()
Definition: VTask.hh:85
const intmax_t & depth() const
Definition: VTask.hh:94
ThreadPool * m_pool
Definition: VTask.hh:102
const_iterator cend() const
Definition: VTask.hh:91
std::function< void()> void_func_t
Definition: VTask.hh:63
std::thread::id tid_type
Definition: VTask.hh:57
virtual ThreadPool * pool() const
Definition: VTask.cc:103
const_iterator end() const
Definition: VTask.hh:88
static tid_type this_tid()
Definition: VTask.hh:97
VTask this_type
Definition: VTask.hh:59
intmax_t & depth()
Definition: VTask.hh:93
virtual ~VTask()
Definition: VTask.cc:64
VTask()
Definition: VTask.cc:40
VTaskGroup * group() const
Definition: VTask.hh:80
const_iterator cbegin() const
Definition: VTask.hh:90
const_iterator begin() const
Definition: VTask.hh:87
VTask * iterator
Definition: VTask.hh:61
size_t size_type
Definition: VTask.hh:58
void operator--()
Definition: VTask.cc:69
virtual void operator()()=0
const VTask * const_iterator
Definition: VTask.hh:62
intmax_t m_depth
Definition: VTask.hh:100
Definition: AutoLock.hh:254