libabigail
Loading...
Searching...
No Matches
abg-workers.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2// -*- Mode: C++ -*-
3//
4// Copyright (C) 2013-2026 Red Hat, Inc.
5//
6// Author: Dodji Seketeli
7
8/// @file
9///
10/// This file declares an interface for the worker threads (or thread
11/// pool) design pattern. It aims at performing a set of tasks in
12/// parallel, using the multi-threading capabilities of the underlying
13/// processor(s).
14///
15
16#ifndef __ABG_WORKERS_H__
17#define __ABG_WORKERS_H__
18
19#include <functional>
20#include <memory>
21#include <vector>
22
23#include "abg-cxx-compat.h"
24
25using std::shared_ptr;
26
27namespace abigail
28{
29
30/// The namespace of the worker threads (or thread pool)
31/// implementation of libabigail. This was modelled after the article
32/// https://en.wikipedia.org/wiki/Thread_pool.
33namespace workers
34{
35
37
39
40/// This represents a task to be performed.
41///
42/// Each instance of this type represents a task that can be performed
43/// concurrently to other instance of the same type.
44///
45/// An instance of @ref task is meant to be performed by a worker
46/// (thread). A set of tasks can be stored in a @ref queue.
47class task
48{
49public:
50 virtual void
51 perform() = 0;
52
53 virtual ~task(){};
54}; // end class task.
55
56typedef shared_ptr<task> task_sptr;
57
58/// The template of a task to be performed.
59///
60/// The function to be performed by the task, its return type and its
61/// list of parameters are parameters of the template.
62///
63/// @tparam Fn the function to be executed by the instantiation.
64///
65/// @tparam RetType the return type of the @p function type @p Fn.
66///
67/// @tparam Args the set of arugments of function @p Fn.
68template<typename Fn, typename RetType, typename... Args>
69class simple_task : public task
70{
71 std::function<RetType(Args...)> fn_;
72 RetType ret_val_;
73 std::tuple<Args...> args_;
74
75 simple_task() = delete;
76
77public:
78
79 simple_task(Fn&& fn, Args... args)
80 : fn_(fn), args_(args...)
81 {
82 }
83
84 RetType
85 get_return_value()
86 {
87 return ret_val_;
88 }
89
90 virtual void
91 perform()
92 {
93 ret_val_ = abg_compat::apply(fn_, args_);
94 }
95}; // end class simple_task.
96
97/// This is a specialization of the @ref simple_task class template
98/// for which the function to be performed returns a void type.
99///
100/// It represents the template of a task to be performed.
101///
102/// The function to be performed by the task with a void return type
103/// and its list of parameters are parameters of the template.
104///
105/// @tparam Fn the function to be executed by the instantiation.
106///
107/// @tparam Args the set of arugments of function @p Fn.
108template<typename Fn, typename... Args>
109class simple_task<Fn, void, Args...> : public task
110{
111 std::function<void(Args...)> fn_;
112 std::tuple<Args...> args_;
113
114 simple_task() = delete;
115
116public:
117
118 simple_task(Fn&& fn, Args... args)
119 : fn_(fn), args_(args...)
120 {
121 }
122
123 virtual void
124 perform()
125 {
126 abg_compat::apply(fn_, args_);
127 }
128}; // end class simple_task.
129
130/// A type alias for shared_ptr<simple_task<Fn, RetType, Args...>>.
131template<typename Fn, typename RetType, typename... Args>
132using simple_task_sptr = shared_ptr<simple_task<Fn, RetType, Args...>>;
133
134/// This represents a queue of tasks to be performed.
135///
136/// Tasks are performed by a number of worker threads.
137///
138/// When a task is inserted into a @ref queue, the task is said to be
139/// "scheduled for execution".
140///
141/// This is because there are worker threads waiting for tasks to be
142/// added to the queue. When a task is added to the queue, a worker
143/// thread picks it up, executes it, notifies interested listeners
144/// when the @ref task's execution is completed, and waits for another
145/// task to be added to the queue.
146///
147/// Of course, several worker threads can execute tasks concurrently.
148class queue
149{
150public:
151 struct priv;
152
153 /// A convenience typedef for a vector of @ref task_sptr
154 typedef std::vector<task_sptr> tasks_type;
155
156private:
157 std::unique_ptr<priv> p_;
158
159public:
160 struct task_done_notify;
161 queue();
162 queue(unsigned number_of_workers);
163 queue(unsigned number_of_workers,
164 task_done_notify& notifier);
165 size_t get_size() const;
166 bool schedule_task(const task_sptr&);
167 bool schedule_tasks(const tasks_type&);
168 bool stage_task(const task_sptr&);
172 ~queue();
173}; // end class queue
174
175/// This functor is to notify listeners that a given task scheduled
176/// for execution has been fully executed.
178{
179 virtual void
180 operator()(const task_sptr& task_done);
181};
182} // end namespace workers
183} // end namespace abigail
184#endif // __ABG_WORKERS_H__
This represents a queue of tasks to be performed.
~queue()
Destructor for the queue type.
tasks_type & get_completed_tasks() const
Getter of the vector of tasks that got performed.
void wait_for_workers_to_complete()
Suspends the current thread until all worker threads finish performing the tasks they are executing.
void schedule_staged_tasks()
Schedule the tasks that have been previously staged by stage_task().
bool stage_task(const task_sptr &)
Stages a task to be scheduled later.
std::vector< task_sptr > tasks_type
A convenience typedef for a vector of task_sptr.
size_t get_size() const
Getter of the size of the queue. This gives the number of task still present in the queue.
bool schedule_tasks(const tasks_type &)
Submit a vector of tasks to the queue of tasks to be performed.
queue()
Default constructor of the queue type.
bool schedule_task(const task_sptr &)
Submit a task to the queue of tasks to be performed.
The template of a task to be performed.
Definition abg-workers.h:70
This represents a task to be performed.
Definition abg-workers.h:48
size_t get_number_of_threads()
size_t get_number_of_available_threads()
shared_ptr< simple_task< Fn, RetType, Args... > > simple_task_sptr
A type alias for shared_ptr<simple_task<Fn, RetType, Args...>>.
Toplevel namespace for libabigail.
This functor is to notify listeners that a given task scheduled for execution has been fully executed...
virtual void operator()(const task_sptr &task_done)
The default function invocation operator of the queue type.