You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

uSnowWorkerM2.pas 1.7 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. unit uSnowWorkerM2;
  2. interface
  3. uses System.SysUtils, uSnowWorkerM1, uIdGeneratorOptions, System.SyncObjs;
  4. /// <summary>
  5. /// ѩ㷨
  6. /// </summary>
  7. type
  8. TSnowWorkerM2 = class(TSnowWorkerM1)
  9. public
  10. function NextId(): Int64;
  11. constructor Create(options: TIdGeneratorOptions); overload;
  12. end;
  13. implementation
  14. { TSnowWorkerM2 }
  15. function IncX(var x: Integer): Integer; inline;
  16. begin
  17. Result := x;
  18. Inc(x);
  19. end;
  20. constructor TSnowWorkerM2.Create(options: TIdGeneratorOptions);
  21. begin
  22. inherited Create(options);
  23. end;
  24. function TSnowWorkerM2.NextId(): Int64;
  25. var
  26. CurrentTimeTick: Int64;
  27. begin
  28. SyncLock.Enter;
  29. try
  30. CurrentTimeTick := GetCurrentTimeTick();
  31. if (FLastTimeTick = CurrentTimeTick) then
  32. begin
  33. // if (IncX(FCurrentSeqNumber) > FMaxSeqNumber) then
  34. // begin
  35. // FCurrentSeqNumber := FMinSeqNumber;
  36. // CurrentTimeTick := GetNextTimeTick();
  37. // end;
  38. if ((FCurrentSeqNumber) > FMaxSeqNumber) then
  39. begin
  40. FCurrentSeqNumber := FMinSeqNumber;
  41. CurrentTimeTick := GetNextTimeTick();
  42. end
  43. else
  44. begin
  45. Inc(FCurrentSeqNumber);
  46. end;
  47. end
  48. else
  49. begin
  50. FCurrentSeqNumber := FMinSeqNumber;
  51. end;
  52. if (CurrentTimeTick < FLastTimeTick) then
  53. begin
  54. raise Exception.Create(Format('Time error for %d milliseconds', [FLastTimeTick - CurrentTimeTick]));
  55. end;
  56. FLastTimeTick := CurrentTimeTick;
  57. Result := ((CurrentTimeTick shl FTimestampShift) + (Int64(FWorkerId) shl FSeqBitLength) +
  58. Cardinal(FCurrentSeqNumber));
  59. finally
  60. SyncLock.Leave;
  61. end;
  62. end;
  63. end.