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

#include "executor_thread_service.h"
#include <vespa/vespalib/util/lambdatask.h>
#include <vespa/vespalib/util/gate.h>

using vespalib::makeLambdaTask;
using vespalib::Executor;
using vespalib::Gate;
using vespalib::Runnable;
using vespalib::ThreadExecutor;
using vespalib::SyncableThreadExecutor;

namespace proton {

namespace {

void
sampleThreadId(std::thread::id *threadId)
{
    *threadId = std::this_thread::get_id();
}

std::thread::id
getThreadId(ThreadExecutor &executor)
{
    std::thread::id id;
    vespalib::Gate gate;
    executor.execute(makeLambdaTask([threadId = &id, &gate] {
        sampleThreadId(threadId);
        gate.countDown();
    }));
    gate.await();
    return id;
}

void
runRunnable(Runnable *runnable, Gate *gate)
{
    runnable->run();
    gate->countDown();
}

} // namespace

ExecutorThreadService::ExecutorThreadService(ThreadExecutor &executor)
    : _executor(executor),
      _threadId(getThreadId(executor))
{
}

ExecutorThreadService::~ExecutorThreadService()  = default;

void
ExecutorThreadService::run(Runnable &runnable)
{
    if (isCurrentThread()) {
        runnable.run();
    } else {
        Gate gate;
        _executor.execute(makeLambdaTask([runnablePtr=&runnable, gatePtr=&gate] { runRunnable(runnablePtr, gatePtr); }));
        gate.await();
    }
}

bool
ExecutorThreadService::isCurrentThread() const
{
    return (_threadId == std::this_thread::get_id());
}

vespalib::ExecutorStats ExecutorThreadService::getStats() {
    return _executor.getStats();
}

void ExecutorThreadService::setTaskLimit(uint32_t taskLimit) {
    _executor.setTaskLimit(taskLimit);
}

uint32_t ExecutorThreadService::getTaskLimit() const {
    return _executor.getTaskLimit();
}

void
ExecutorThreadService::wakeup() {
    _executor.wakeup();
}

SyncableExecutorThreadService::SyncableExecutorThreadService(SyncableThreadExecutor &executor)
    : _executor(executor),
      _threadId(getThreadId(executor))
{
}

SyncableExecutorThreadService::~SyncableExecutorThreadService()  = default;

void
SyncableExecutorThreadService::run(Runnable &runnable)
{
    if (isCurrentThread()) {
        runnable.run();
    } else {
        Gate gate;
        _executor.execute(makeLambdaTask([runnablePtr=&runnable, gatePtr=&gate] { runRunnable(runnablePtr, gatePtr); }));
        gate.await();
    }
}

bool
SyncableExecutorThreadService::isCurrentThread() const
{
    return (_threadId == std::this_thread::get_id());
}

vespalib::ExecutorStats
SyncableExecutorThreadService::getStats() {
    return _executor.getStats();
}

void
SyncableExecutorThreadService::setTaskLimit(uint32_t taskLimit) {
    _executor.setTaskLimit(taskLimit);
}

uint32_t
SyncableExecutorThreadService::getTaskLimit() const {
    return _executor.getTaskLimit();
}

void
SyncableExecutorThreadService::wakeup() {
    _executor.wakeup();
}

} // namespace proton