/// <summary> /// <para> /// Locking. /// </para> /// <para> /// Creates the values required to create an ID. /// </para> /// </summary> private (ulong Timestamp, RandomSequence6 RandomSequence) CreateValues() { // The random number generator is likely to lock, so doing this outside of our own lock is likely to increase throughput var randomSequence = CreateRandomSequence(); lock (this._lockObject) { var timestamp = this.GetTimestamp(); // If the clock has not advanced since the previous invocation if (timestamp == this.PreviousCreationTimestamp) { // If we succeed in creating another, greater value, use that if (this.TryCreateIncrementalRandomSequence(this.PreviousRandomSequence, randomSequence, out var incrementedRandomSequence)) { this.PreviousRandomSequence = incrementedRandomSequence; return(timestamp, incrementedRandomSequence); } // Otherwise, sleep until the clock has advanced else { timestamp = this.AwaitUpdatedClockValue(); } } // Update the previous timestamp this.PreviousCreationTimestamp = timestamp; this.PreviousRandomSequence = randomSequence; return(timestamp, randomSequence); } }
/// <summary> /// <para> /// Pure function. /// </para> /// <para> /// Creates a new ID based on the given values. /// </para> /// </summary> /// <param name="timestamp">The UTC timestamp in milliseconds since the epoch.</param> /// <param name="randomSequence">A random sequence whose 2 low bytes are zeros. This is checked to ensure that the caller has understood what will be used.</param> internal decimal CreateCore(ulong timestamp, RandomSequence6 randomSequence) { // 93 bits fit into 28 decimals // 96 bits: [3 unused bits] [45 time bits] [48 random bits] Span <byte> bytes = stackalloc byte[2 + 12 + 2]; // Bits: 16 padding (to treat the left half as ulong) + 96 useful + 16 padding (to treat the right half as ulong) // Populate the left half with the timestamp { // The 64-bit timestamp's 19 high bits must be zero, leaving the low 45 bits to be used if (timestamp >> 45 != 0UL) { throw new InvalidOperationException($"{nameof(DistributedId)} has run out of available time bits."); // Year 3084 } // Write the time component into the first 8 bytes (64 bits: 16 padding to write a ulong, 3 unused, 45 used) BinaryPrimitives.WriteUInt64BigEndian(bytes, timestamp); } bytes = bytes[2..]; // Disregard the left padding
/// <summary> /// <para> /// Pure function. /// </para> /// <para> /// Creates a new 48-bit random sequence based on the given previous one and new one. /// Adds new randomness while maintaining the incremental property. /// </para> /// <para> /// Returns true on success or false on overflow. /// </para> /// </summary> private bool TryCreateIncrementalRandomSequence(RandomSequence6 previousRandomSequence, RandomSequence6 newRandomSequence, out RandomSequence6 incrementedRandomSequence) { return(previousRandomSequence.TryAddRandomBits(newRandomSequence, out incrementedRandomSequence)); }
/// <summary> /// <para> /// Pure function (although the random number generator may use locking internally). /// </para> /// <para> /// Returns a new 48-bit (6-byte) random sequence. /// </para> /// </summary> private RandomSequence6 CreateRandomSequence() { return(RandomSequence6.Create()); }