aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/util/shutdownguard.h
blob: 32190f08d1c5513b59af8fa23096da8c88a7d553 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include <vespa/vespalib/util/time.h>
#include <thread>
#include <atomic>

namespace vespalib {

/**
 * Class to ensure that the current process finishes within a given time.
 * Construct with the number of milliseconds before triggering _exit();
 * destruct the ShutdownGuard object to dismiss the automatic process
 * termination.
 * A separate shutdown thread will perform the actual _exit() call.
 **/
class ShutdownGuard
{
    std::thread _thread;
    steady_time _dieAtTime;
    std::atomic<bool> _cancel;

    void run();

public:
    /**
     * Construct a shutdown guard with a given lifetime.
     * @arg millis the number of milliseconds before process automatically exits
     **/
    ShutdownGuard(duration millis);

    /**
     * Destructor that dismisses the guard and collects the shutdown thread.
     **/
    ~ShutdownGuard();

};

} // namespace vespalib