aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/utils/rand_guid.cpp
blob: d89eefbd3b87c4c60d1ae2ce7bc4fa38f7433628 (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
#include "rand_guid.h"

#include <util/system/datetime.h>
#include <util/system/getpid.h>
#include <util/system/unaligned_mem.h>
#include <util/generic/guid.h>

namespace NYql {

TAtomic TRandGuid::Counter = 0;

TRandGuid::TRandGuid() {
    ResetSeed();
}

void TRandGuid::ResetSeed() {
    new (&Rnd_) TMersenne<ui64>(GetCycleCount() + MicroSeconds() + GetPID());
}

TString TRandGuid::GenGuid() {
    TGUID ret = {};
    WriteUnaligned<ui64>(ret.dw, GetRnd().GenRand());
    ret.dw[2] = (ui32)GetRnd().GenRand();
    ret.dw[3] = AtomicIncrement(Counter);

    return GetGuidAsString(ret);
}

ui64 TRandGuid::GenNumber() {
    return GetRnd().GenRand();
}
}