aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/util/executor.h
blob: fe209edc4168f14ddf235b9ee9c8d123b7a70fd0 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include <memory>

namespace vespalib {

/**
 * Interface for componets that can benefit from regular wakeup calls.
 */
class IWakeup {
public:
    virtual ~IWakeup() = default;
    /**
     * In case you have a lazy executor that naps inbetween.
     **/
    virtual void wakeup() = 0;
};

/**
 * An executor decouples the execution of a task from the request of
 * executing that task. Also, tasks are typically executed
 * concurrently in multiple threads.
 **/
class Executor : public IWakeup
{
public:
    /**
     * A task that can be executed by an executor.
     **/
    struct Task {
        using UP = std::unique_ptr<Task>;
        virtual void run() = 0;
        virtual ~Task() = default;
    };

    enum class OptimizeFor {LATENCY, THROUGHPUT, ADAPTIVE};

    /**
     * Execute the given task using one of the internal threads some
     * time in the future. The task may also be rejected in which case
     * it will be returned from this function. A task will be rejected
     * if the executor has been shut down or if the task limit for
     * this executor has been reached.
     *
     * @return the task if rejected, UP(0) if accepted
     * @param task the task to execute
     **/
    virtual Task::UP execute(Task::UP task) = 0;

    virtual ~Executor() = default;
};

} // namespace vespalib