aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/util/simple_thread_bundle.h
blob: 9a5ae9dcf6f28196e52300358dba2d520cc40ab8 (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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "count_down_latch.h"
#include "thread.h"
#include "runnable.h"
#include "thread_bundle.h"

namespace vespalib {

namespace fixed_thread_bundle {

/**
 * collection of work to be done by a single call to the thread bundle
 * run function, and latch to count down when each part is done. The
 * same data structure will be reused for all calls to run in order to
 * support static wiring of signal paths and execution hooks.
 **/
struct Work {
    Runnable* const* targets;
    size_t cnt;
    CountDownLatch *latch;
    Work() : targets(nullptr), cnt(0), latch(nullptr) {}
};

/**
 * the subset of work to be done by a single thread.
 **/
struct Part {
    const Work &work;
    size_t offset;
    Part(const Work &w, size_t o) : work(w), offset(o) {}
    bool valid() const noexcept { return (offset < work.cnt); }
    void perform() {
        if (valid()) {
            work.targets[offset]->run();
        }
        work.latch->countDown();
    }
};

/**
 * countable signal path between threads.
 **/
struct Signal {
    bool valid;
    size_t generation;
    std::unique_ptr<std::mutex> monitor;
    std::unique_ptr<std::condition_variable> cond;
    Signal() noexcept;
    Signal(Signal &&) noexcept = default;
    ~Signal();
    size_t wait(size_t &localGen) const {
        std::unique_lock guard(*monitor);
        while (localGen == generation) {
            cond->wait(guard);
        }
        size_t diff = (generation - localGen);
        localGen = generation;
        return (valid ? diff : 0);
    }
    void send() {
        std::lock_guard guard(*monitor);
        ++generation;
        cond->notify_one();
    }
    void broadcast() {
        std::lock_guard guard(*monitor);
        ++generation;
        cond->notify_all();
    }
    void cancel() {
        std::lock_guard guard(*monitor);
        ++generation;
        valid = false;
        cond->notify_all();
    }
};

} // namespace vespalib::fixed_thread_bundle

/**
 * A ThreadBundle implementation employing a fixed set of internal
 * threads. The internal Pool class can be used to recycle bundles.
 **/
class SimpleThreadBundle : public ThreadBundle
{
public:
    using Work = fixed_thread_bundle::Work;
    using Signal = fixed_thread_bundle::Signal;
    using init_fun_t = Runnable::init_fun_t;

    using UP = std::unique_ptr<SimpleThreadBundle>;
    enum Strategy { USE_SIGNAL_LIST, USE_SIGNAL_TREE, USE_BROADCAST };

    class Pool
    {
    private:
        std::mutex _lock;
        size_t     _bundleSize;
        init_fun_t _init_fun;
        std::vector<SimpleThreadBundle*> _bundles;

    public:
        class Guard {
        public:
            explicit Guard(Pool & pool) : _bundle(pool.obtain()), _pool(pool) {}
            Guard(const Guard &) = delete;
            Guard & operator =(const Guard &) = delete;
            ~Guard() { _pool.release(std::move(_bundle)); }
            SimpleThreadBundle & bundle() { return *_bundle; }
        private:
            SimpleThreadBundle::UP  _bundle;
            Pool                   &_pool;
        };
        Pool(size_t bundleSize, init_fun_t init_fun);
        explicit Pool(size_t bundleSize) : Pool(bundleSize, Runnable::default_init_function) {}
        ~Pool();
        Guard getBundle() { return Guard(*this); }
        //TODO Make private
        SimpleThreadBundle::UP obtain();
        void release(SimpleThreadBundle::UP bundle);
    };

private:
    struct Worker : Runnable {
        using UP = std::unique_ptr<Worker>;
        std::thread thread;
        Signal &signal;
        Runnable::UP hook;
        Worker(Signal &s, init_fun_t init_fun, Runnable::UP h);
        void run() override;
    };

    Work                    _work;
    std::vector<Signal>     _signals;
    std::vector<Worker::UP> _workers;
    Runnable::UP            _hook;

public:
    SimpleThreadBundle(size_t size, init_fun_t init_fun, Strategy strategy);
    SimpleThreadBundle(size_t size, Strategy strategy)
      : SimpleThreadBundle(size, Runnable::default_init_function, strategy) {}
    explicit SimpleThreadBundle(size_t size)
            : SimpleThreadBundle(size, USE_SIGNAL_LIST) {}
    ~SimpleThreadBundle() override;
    size_t size() const override;
    using ThreadBundle::run;
    void run(Runnable* const* targets, size_t cnt) override;
};

} // namespace vespalib