aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/actors/core/lease.h
blob: 650ae7b1224feabbb844fd9ec071ba5858f4f806 (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
47
48
49
50
51
52
53
54
55
56
#pragma once

#include "defs.h"

namespace NActors {
    // Value representing specific worker's permission for exclusive use of CPU till specific deadline
    struct TLease {
        // Lower WorkerBits store current fast worker id
        // All other higher bits store expiration (hard preemption) timestamp
        using TValue = ui64;
        TValue Value;

        static constexpr ui64 WorkerIdMask = ui64((1ull << WorkerBits) - 1);
        static constexpr ui64 ExpireTsMask = ~WorkerIdMask;

        explicit constexpr TLease(ui64 value)
            : Value(value)
        {}

        constexpr TLease(TWorkerId workerId, ui64 expireTs)
            : Value((workerId & WorkerIdMask) | (expireTs & ExpireTsMask))
        {}

        TWorkerId GetWorkerId() const {
            return Value & WorkerIdMask;
        }

        TLease NeverExpire() const {
            return TLease(Value | ExpireTsMask);
        }

        bool IsNeverExpiring() const {
            return (Value & ExpireTsMask) == ExpireTsMask;
        }

        ui64 GetExpireTs() const {
            // Do not truncate worker id
            // NOTE: it decrease accuracy, but improves performance
            return Value;
        }

        ui64 GetPreciseExpireTs() const {
            return Value & ExpireTsMask;
        }

        operator TValue() const {
            return Value;
        }
    };

    // Special expire timestamp values
    static constexpr ui64 NeverExpire = ui64(-1);

    // Special hard-preemption-in-progress lease
    static constexpr TLease HardPreemptionLease = TLease(TLease::WorkerIdMask, NeverExpire);
}