aboutsummaryrefslogtreecommitdiffstats
path: root/QtVsTools.Core/Common/Timestamp.cs
blob: 326bca347c900627cd8dd68a3cadc96ddfa44c5f (plain)
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
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

using System.Diagnostics;

namespace QtVsTools
{
    using Common;

    public class Timestamp : Concurrent<Timestamp>
    {
        static LazyFactory StaticLazy { get; } = new();

        long LastTimestamp { get; set; }
        long GetStrictMonotonicTimestamp()
        {
            lock (CriticalSection) {
                long t = Stopwatch.GetTimestamp();
                if (t <= LastTimestamp)
                    t = LastTimestamp + 1;
                return LastTimestamp = t;
            }
        }

        static Timestamp Instance => StaticLazy.Get(() =>
            Instance, () => new Timestamp());

        public static long Next()
        {
            return Instance.GetStrictMonotonicTimestamp();
        }
    }
}