aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/common/scheduletaskcallback.h
blob: 2e65cda66b59f750dc0116badef944aa5d6f1c99 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include "vespa/vespalib/util/idestructorcallback.h"
#include <vespa/vespalib/util/executor.h>

namespace search {

/**
 * Class that schedules a task when instance is destroyed. Typically a
 * shared pointer to an instance is passed around to multiple worker
 * threads that performs portions of a larger task before dropping the
 * shared pointer, triggering the callback when all worker threads
 * have completed.
 */
class ScheduleTaskCallback : public vespalib::IDestructorCallback
{
    vespalib::Executor &_executor;
    vespalib::Executor::Task::UP _task;
public:
    ScheduleTaskCallback(vespalib::Executor &executor,
                         vespalib::Executor::Task::UP task) noexcept
        : _executor(executor),
          _task(std::move(task))
    {}
    ~ScheduleTaskCallback() override {
        _executor.execute(std::move(_task));
    }
};

} // namespace search