--- parser3/src/main/pa_random.C 2023/08/15 19:27:48 1.12 +++ parser3/src/main/pa_random.C 2024/11/04 03:53:25 1.16 @@ -1,8 +1,8 @@ /** @file Parser: random related functions. - Copyright (c) 2001-2020 Art. Lebedev Studio (http://www.artlebedev.com) - Author: Alexandr Petrosian (http://paf.design.ru) + Copyright (c) 2001-2024 Art. Lebedev Studio (http://www.artlebedev.com) + Authors: Konstantin Morshnev , Alexandr Petrosian */ // includes @@ -12,7 +12,7 @@ #include "pa_exception.h" #include "pa_threads.h" -volatile const char * IDENT_PA_RANDOM_C="$Id: pa_random.C,v 1.12 2023/08/15 19:27:48 moko Exp $" IDENT_PA_RANDOM_H; +volatile const char * IDENT_PA_RANDOM_C="$Id: pa_random.C,v 1.16 2024/11/04 03:53:25 moko Exp $" IDENT_PA_RANDOM_H; #ifdef _MSC_VER #include @@ -45,11 +45,6 @@ public: } } random_provider; -typedef struct timeval { - long tv_sec; - long tv_usec; -} timeval; - int gettimeofday(struct timeval * tp, void *); #else @@ -234,6 +229,22 @@ char *get_uuid7_cstr(bool lower, bool so // 48 bit big-endian unsigned number of Unix epoch timestamp in milliseconds uint64_t unix_ts_ms = (uint64_t)tv.tv_sec * 1000 + (uint64_t)tv.tv_usec / 1000; + + // 12 bit monotonicity counter + static int seq = 0; + static uint64_t ms_previous=0; + + if(unix_ts_ms == ms_previous){ + seq++; + unix_ts_ms += seq >> 12; + } else { + seq = 0; + ms_previous = unix_ts_ms; + } + + uuid[7] = (unsigned char) (seq); + uuid[6] = (unsigned char) (seq >> 8); + uuid[5] = (unsigned char) (unix_ts_ms); unix_ts_ms >>= 8; uuid[4] = (unsigned char) (unix_ts_ms); unix_ts_ms >>= 8; uuid[3] = (unsigned char) (unix_ts_ms); unix_ts_ms >>= 8; @@ -241,11 +252,6 @@ char *get_uuid7_cstr(bool lower, bool so uuid[1] = (unsigned char) (unix_ts_ms); unix_ts_ms >>= 8; uuid[0] = (unsigned char) (unix_ts_ms); - // Use increased clock precision as left-most random bits - uint64_t usec = tv.tv_usec; - uuid[7] = (unsigned char) (usec); usec >>= 8; - uuid[6] = (unsigned char) (usec); - // Set magic numbers for a "version 7" UUID, see // https://www.ietf.org/archive/id/draft-ietf-uuidrev-rfc4122bis-00.html#name-uuid-version-7 uuid[6] = (uuid[6] & 0x0f) | 0x70; /* 4 bit version [0111] */