aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/vespa/storageframework/generic/thread/thread.h
blob: 9fab2be06d119c38da572c824315103dcd5ee4c2 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/**
 * \class storage::framework::Thread
 * \ingroup thread
 *
 * \brief A wrapper for a thread class.
 *
 * This thread class exist to hide the actual implementation of threads used,
 * and to give some extra information about the threads. This is in turned used
 * by monitoring, to be able to see data about the threads running. One such
 * monitoring tool is the deadlock detector.
 */
#pragma once

#include "runnable.h"
#include "thread_properties.h"
#include <condition_variable>

namespace storage::framework {

/** Data kept on each thread due to the registerTick functionality. */
struct ThreadTickData {
    CycleType _lastTickType;
    vespalib::steady_time _lastTick;
    vespalib::duration _maxProcessingTimeSeen;
    vespalib::duration _maxWaitTimeSeen;
};

class Thread : public ThreadHandle {
    vespalib::string _id;

public:
    using UP = std::unique_ptr<Thread>;

    explicit Thread(vespalib::stringref id) : _id(id) {}
    ~Thread() override = default;

    [[nodiscard]] virtual const vespalib::string& getId() const { return _id; }

    /** Check whether thread have been interrupted or not. */
    [[nodiscard]] bool interrupted() const override = 0;
    /** Check whether thread have been joined or not. */
    [[nodiscard]] virtual bool joined() const  = 0;

    /**
     * Call this function to set interrupt flag, such that later calls to
     * interrupt returns true. If called on already interrupted thread it is a
     * noop.
     */
    virtual void interrupt() = 0;
    /**
     * Call this function to wait until thread has finished processing. If
     * called after thread has already finished, it is a noop.
     */
    virtual void join() = 0;

    virtual ThreadTickData getTickData() const = 0;
    virtual const ThreadProperties& getProperties() const = 0;

    virtual vespalib::string get_live_thread_stack_trace() const = 0;

    /**
     * Utility function to interrupt and join a thread, possibly broadcasting
     * through a monitor after the signalling face.
     */
    void interruptAndJoin();

    void interruptAndJoin(std::condition_variable &cv);
};

}