aboutsummaryrefslogtreecommitdiffstats
path: root/staging_vespalib/src/vespa/vespalib/util/shutdownguard.h
blob: 2eeb06e1a0367fa6d18375c6e8cfbb25e4f3d0ab (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include <vespa/fastos/thread.h>

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 : public FastOS_Runnable
{
    enum { STACK_SIZE = (1u << 16) };

    static uint64_t getTimeInMillis() {
        struct timeval mytime;
        gettimeofday(&mytime, 0);
        uint64_t mult = 1000;
        return (mytime.tv_sec * mult) + (mytime.tv_usec / mult);
    }

    FastOS_ThreadPool _pool;
    volatile uint64_t _dieAtTime;

    void Run(FastOS_ThreadInterface *, void *) override;

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

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

};

} // namespace vespalib