1717#include < atomic>
1818#include < span>
1919
20- #if defined(UUID_TIME_GENERATOR) || defined(UUID_SYSTEM_GENERATOR)
2120#ifdef _WIN32
22- #ifndef WIN32_LEAN_AND_MEAN
23- #define WIN32_LEAN_AND_MEAN
24- #endif
25- #ifndef NOMINMAX
26- #define NOMINMAX
27- #endif
2821
2922#ifdef UUID_SYSTEM_GENERATOR
3023#include < objbase.h>
3124#endif
3225
33- #include < windows.h>
34- #include < intrin.h>
26+ #ifdef UUID_TIME_GENERATOR
3527#include < iphlpapi.h>
3628#pragma comment(lib, "IPHLPAPI.lib")
29+ #endif
3730
3831#elif defined(__linux__) || defined(__unix__)
3932
4740#include < CoreFoundation/CFUUID.h>
4841#endif
4942
50- #endif
5143#endif
5244
5345namespace uuids
@@ -278,10 +270,6 @@ namespace uuids
278270 size_t m_blockByteIndex;
279271 size_t m_byteCount;
280272 };
281-
282- static std::mt19937 clock_gen (std::random_device{}());
283- static std::uniform_int_distribution<short > clock_dis{ -32768 , 32767 };
284- static std::atomic_short clock_sequence = clock_dis(clock_gen);
285273 }
286274
287275 // --------------------------------------------------------------------------------------------------------------------------
@@ -847,11 +835,15 @@ namespace uuids
847835 return ns / 100 ;
848836 }
849837
850- public:
851- uuid_time_generator ()
838+ static unsigned short get_clock_sequence ()
852839 {
840+ static std::mt19937 clock_gen (std::random_device{}());
841+ static std::uniform_int_distribution<unsigned short > clock_dis;
842+ static std::atomic_ushort clock_sequence = clock_dis (clock_gen);
843+ return clock_sequence++;
853844 }
854845
846+ public:
855847 uuid operator ()()
856848 {
857849 if (get_mac_address ())
@@ -860,25 +852,22 @@ namespace uuids
860852
861853 auto tm = get_time_intervals ();
862854
863- short clock_seq = detail::clock_sequence++;
864-
865- clock_seq &= 0x3FFF ;
855+ auto clock_seq = get_clock_sequence ();
866856
867857 auto ptm = reinterpret_cast <uuids::uuid::value_type*>(&tm);
868- ptm[0 ] &= 0x0F ;
869858
870859 memcpy (&data[0 ], ptm + 4 , 4 );
871860 memcpy (&data[4 ], ptm + 2 , 2 );
872861 memcpy (&data[6 ], ptm, 2 );
873862
874- memcpy (&data[8 ], reinterpret_cast <uuids::uuid::value_type*>( &clock_seq) , 2 );
863+ memcpy (&data[8 ], &clock_seq, 2 );
875864
876865 // variant must be 0b10xxxxxx
877866 data[8 ] &= 0xBF ;
878867 data[8 ] |= 0x80 ;
879868
880869 // version must be 0b0001xxxx
881- data[6 ] &= 0x5F ;
870+ data[6 ] &= 0x1F ;
882871 data[6 ] |= 0x10 ;
883872
884873 memcpy (&data[10 ], &device_address.value ()[0 ], 6 );
0 commit comments