aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/vespa/storageframework/defaultimplementation/thread/threadimpl.h
blob: 68ed63ea17cc2d66228ef0e57f8f2f9a0d2309ef (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include <vespa/storageframework/generic/thread/thread.h>
#include <vespa/vespalib/util/cpu_usage.h>
#include <array>
#include <atomic>
#include <optional>

namespace storage::framework::defaultimplementation {

struct ThreadPoolImpl;

class ThreadImpl final : public Thread
{
    /**
     * Internal data race free implementation of tick data that maps to and
     * from ThreadTickData. We hide the atomicity of this since atomic vars
     * are not CopyConstructible and thus would impose unnecessary limitations
     * on code using it.
     */
    struct AtomicThreadTickData {
        AtomicThreadTickData() noexcept
            : _lastTickType(),
              _lastTick(vespalib::steady_time(vespalib::duration::zero())),
              _maxProcessingTimeSeen(),
              _maxWaitTimeSeen()
        {}
        std::atomic<CycleType> _lastTickType;
        std::atomic<vespalib::steady_time> _lastTick;
        std::atomic<vespalib::duration> _maxProcessingTimeSeen;
        std::atomic<vespalib::duration> _maxWaitTimeSeen;
        // struct stores and loads are both data race free with relaxed
        // memory semantics. This means it's possible to observe stale/partial
        // state in a case with concurrent readers/writers.
        ThreadTickData loadRelaxed() const noexcept;
        void storeRelaxed(const ThreadTickData& newState) noexcept;
    };

    ThreadPoolImpl& _pool;
    Runnable& _runnable;
    ThreadProperties _properties;
    std::array<AtomicThreadTickData, 3> _tickData;
    std::atomic<uint32_t> _tickDataPtr;
    std::atomic<bool> _interrupted;
    bool _joined;
    std::thread _thread;
    std::optional<vespalib::CpuUsage::Category> _cpu_category;

    void run();

public:
    ThreadImpl(ThreadPoolImpl&, Runnable&, vespalib::stringref id, vespalib::duration waitTime,
               vespalib::duration maxProcessTime, int ticksBeforeWait,
               std::optional<vespalib::CpuUsage::Category> cpu_category);
    ~ThreadImpl() override;

    bool interrupted() const override;
    bool joined() const override;
    void interrupt() override;
    void join() override;

    vespalib::string get_live_thread_stack_trace() const override;

    void registerTick(CycleType, vespalib::steady_time) override;
    void registerTick(CycleType cycleType) override;
    vespalib::duration getWaitTime() const override {
        return _properties.getWaitTime();
    }
    int getTicksBeforeWait() const override {
        return _properties.getTicksBeforeWait();
    }

    void setTickData(const ThreadTickData&);
    ThreadTickData getTickData() const override;
    const ThreadProperties& getProperties() const override { return _properties; }
};

}