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

#include <vespa/log/log.h>
LOG_SETUP(".vespalib.shutdownguard");

namespace vespalib {

void
ShutdownGuard::run()
{
    while (_dieAtTime > steady_clock::now() && !_cancel.load(std::memory_order_relaxed)) {
        std::this_thread::sleep_for(5ms);
    }
    if (_dieAtTime <= steady_clock::now()) {
        LOG(warning, "ShutdownGuard is now forcing an exit of the process.");
        _exit(EXIT_FAILURE);
    }
}

ShutdownGuard::ShutdownGuard(duration millis)
  : _thread(),
    _dieAtTime(steady_clock::now() + millis)
{
    _thread = std::thread(&ShutdownGuard::run, this);
}

ShutdownGuard::~ShutdownGuard()
{
    _cancel = true;
    _thread.join();
}

}