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

#include "idestructorcallback.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 IDestructorCallback
{
    vespalib::Executor &_executor;
    vespalib::Executor::Task::UP _task;
public:
    ScheduleTaskCallback(vespalib::Executor &executor,
                         vespalib::Executor::Task::UP task)
        : _executor(executor),
          _task(std::move(task))
    {
    }
    virtual ~ScheduleTaskCallback() {
        _executor.execute(std::move(_task));
    }
};

} // namespace search