rand_guid.cpp 659 B

1234567891011121314151617181920212223242526272829303132
  1. #include "rand_guid.h"
  2. #include <util/system/datetime.h>
  3. #include <util/system/getpid.h>
  4. #include <util/system/unaligned_mem.h>
  5. #include <util/generic/guid.h>
  6. namespace NYql {
  7. TAtomic TRandGuid::Counter = 0;
  8. TRandGuid::TRandGuid() {
  9. ResetSeed();
  10. }
  11. void TRandGuid::ResetSeed() {
  12. new (&Rnd_) TMersenne<ui64>(GetCycleCount() + MicroSeconds() + GetPID());
  13. }
  14. TString TRandGuid::GenGuid() {
  15. TGUID ret = {};
  16. WriteUnaligned<ui64>(ret.dw, GetRnd().GenRand());
  17. ret.dw[2] = (ui32)GetRnd().GenRand();
  18. ret.dw[3] = AtomicIncrement(Counter);
  19. return GetGuidAsString(ret);
  20. }
  21. ui64 TRandGuid::GenNumber() {
  22. return GetRnd().GenRand();
  23. }
  24. }