lease.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #pragma once
  2. #include "defs.h"
  3. namespace NActors {
  4. // Value representing specific worker's permission for exclusive use of CPU till specific deadline
  5. struct TLease {
  6. // Lower WorkerBits store current fast worker id
  7. // All other higher bits store expiration (hard preemption) timestamp
  8. using TValue = ui64;
  9. TValue Value;
  10. static constexpr ui64 WorkerIdMask = ui64((1ull << WorkerBits) - 1);
  11. static constexpr ui64 ExpireTsMask = ~WorkerIdMask;
  12. explicit constexpr TLease(ui64 value)
  13. : Value(value)
  14. {}
  15. constexpr TLease(TWorkerId workerId, ui64 expireTs)
  16. : Value((workerId & WorkerIdMask) | (expireTs & ExpireTsMask))
  17. {}
  18. TWorkerId GetWorkerId() const {
  19. return Value & WorkerIdMask;
  20. }
  21. TLease NeverExpire() const {
  22. return TLease(Value | ExpireTsMask);
  23. }
  24. bool IsNeverExpiring() const {
  25. return (Value & ExpireTsMask) == ExpireTsMask;
  26. }
  27. ui64 GetExpireTs() const {
  28. // Do not truncate worker id
  29. // NOTE: it decrease accuracy, but improves performance
  30. return Value;
  31. }
  32. ui64 GetPreciseExpireTs() const {
  33. return Value & ExpireTsMask;
  34. }
  35. operator TValue() const {
  36. return Value;
  37. }
  38. };
  39. // Special expire timestamp values
  40. static constexpr ui64 NeverExpire = ui64(-1);
  41. // Special hard-preemption-in-progress lease
  42. static constexpr TLease HardPreemptionLease = TLease(TLease::WorkerIdMask, NeverExpire);
  43. }