blob: 830d7c76e7c169effa51453b64a847e90f47e919 (
plain) (
blame)
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
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once
#include "runnable.h"
#include <vector>
namespace vespalib {
/**
* Interface used to separate the ownership and deployment of a
* collection of threads cooperating to perform a partitioned
* operation in parallel.
**/
struct ThreadBundle {
/**
* The size of the thread bundle is defined to be the maximum
* number of runnables that can be performed in parallel by the
* run function.
*
* @return size of this thread bundle
**/
virtual size_t size() const = 0;
/**
* Performs all the given runnables in parallel and waits for
* their completion. This function cannot be called with more
* targets than the size of this bundle.
**/
virtual void run(Runnable* const* targets, size_t cnt) = 0;
// convenience run wrapper
void run(const std::vector<Runnable*> &targets) {
run(targets.data(), targets.size());
}
// convenience run wrapper
void run(const std::vector<Runnable::UP> &targets) {
static_assert(sizeof(Runnable::UP) == sizeof(Runnable*));
run(reinterpret_cast<Runnable* const*>(targets.data()), targets.size());
}
template <typename T>
static constexpr bool is_runnable_ptr() {
return (std::is_same_v<T,Runnable*> || std::is_same_v<T,Runnable::UP>);
}
// convenience run wrapper
template <typename Item>
std::enable_if_t<!is_runnable_ptr<Item>(),void> run(std::vector<Item> &items) {
std::vector<Runnable*> targets;
targets.reserve(items.size());
for (auto &item: items) {
targets.push_back(resolve(item));
}
run(targets);
}
/**
* Empty virtual destructor to enable subclassing.
**/
virtual ~ThreadBundle() {}
// a thread bundle that can only run things in the current thread.
static ThreadBundle &trivial();
private:
Runnable *resolve(Runnable &target) { return ⌖ }
template <typename T>
Runnable *resolve(const std::unique_ptr<T> &target) { return target.get(); }
};
} // namespace vespalib
|