aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/vespa/searchcore/proton/server/executor_thread_service.h
blob: 7104aeea3f35ecdd589fa79dcc059a696f79d2c5 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include <vespa/searchcorespi/index/i_thread_service.h>
#include <vespa/vespalib/util/threadexecutor.h>
#include <thread>

namespace proton {

/**
 * Implementation of IThreadService using an underlying thread stack executor
 * with a single thread.
 */
class ExecutorThreadService : public searchcorespi::index::IThreadService
{
private:
    vespalib::ThreadExecutor &_executor;
    std::thread::id _threadId;

public:
    ExecutorThreadService(vespalib::ThreadExecutor &executor);
    ~ExecutorThreadService();

    vespalib::ExecutorStats getStats() override;

    vespalib::Executor::Task::UP execute(vespalib::Executor::Task::UP task) override {
        return _executor.execute(std::move(task));
    }
    void run(vespalib::Runnable &runnable) override;

    bool isCurrentThread() const override;
    size_t getNumThreads() const override { return _executor.getNumThreads(); }

    void setTaskLimit(uint32_t taskLimit) override;
    uint32_t getTaskLimit() const override;
    void wakeup() override;
};

class SyncableExecutorThreadService : public searchcorespi::index::ISyncableThreadService
{
private:
    vespalib::SyncableThreadExecutor &_executor;
    std::thread::id _threadId;

public:
    SyncableExecutorThreadService(vespalib::SyncableThreadExecutor &executor);
    ~SyncableExecutorThreadService();

    vespalib::ExecutorStats getStats() override;

    vespalib::Executor::Task::UP execute(vespalib::Executor::Task::UP task) override {
        return _executor.execute(std::move(task));
    }
    void run(vespalib::Runnable &runnable) override;
    vespalib::Syncable &sync() override {
        _executor.sync();
        return *this;
    }

    bool isCurrentThread() const override;
    size_t getNumThreads() const override { return _executor.getNumThreads(); }

    void setTaskLimit(uint32_t taskLimit) override;
    uint32_t getTaskLimit() const override;
    void wakeup() override;
};

} // namespace proton