udf_counter.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #pragma once
  2. #include "udf_types.h"
  3. #include "udf_type_size_check.h"
  4. #include <library/cpp/deprecated/atomic/atomic.h>
  5. namespace NYql {
  6. namespace NUdf {
  7. class TCounter {
  8. public:
  9. TCounter(i64* ptr = nullptr)
  10. : Ptr_(ptr)
  11. {}
  12. void Inc() {
  13. if (Ptr_) {
  14. AtomicIncrement(AsAtomic());
  15. }
  16. }
  17. void Dec() {
  18. if (Ptr_) {
  19. AtomicDecrement(AsAtomic());
  20. }
  21. }
  22. void Add(i64 delta) {
  23. if (Ptr_) {
  24. AtomicAdd(AsAtomic(), delta);
  25. }
  26. }
  27. void Sub(i64 delta) {
  28. if (Ptr_) {
  29. AtomicSub(AsAtomic(), delta);
  30. }
  31. }
  32. void Set(i64 value) {
  33. if (Ptr_) {
  34. AtomicSet(AsAtomic(), value);
  35. }
  36. }
  37. private:
  38. TAtomic& AsAtomic() {
  39. return *reinterpret_cast<TAtomic*>(Ptr_);
  40. }
  41. private:
  42. i64* Ptr_;
  43. };
  44. UDF_ASSERT_TYPE_SIZE(TCounter, 8);
  45. class IScopedProbeHost {
  46. public:
  47. virtual ~IScopedProbeHost() = default;
  48. virtual void Acquire(void* cookie) = 0;
  49. virtual void Release(void* cookie) = 0;
  50. };
  51. UDF_ASSERT_TYPE_SIZE(IScopedProbeHost, 8);
  52. class TScopedProbe {
  53. public:
  54. TScopedProbe(IScopedProbeHost* host = nullptr, void* cookie = nullptr)
  55. : Host_(host ? host : &NullHost_)
  56. , Cookie_(cookie)
  57. {}
  58. void Acquire() {
  59. Host_->Acquire(Cookie_);
  60. }
  61. void Release() {
  62. Host_->Release(Cookie_);
  63. }
  64. private:
  65. class TNullHost : public IScopedProbeHost {
  66. public:
  67. void Acquire(void* cookie) override {
  68. Y_UNUSED(cookie);
  69. }
  70. void Release(void* cookie) override {
  71. Y_UNUSED(cookie);
  72. }
  73. };
  74. IScopedProbeHost* Host_;
  75. void* Cookie_;
  76. static TNullHost NullHost_;
  77. };
  78. UDF_ASSERT_TYPE_SIZE(TScopedProbe, 16);
  79. class ICountersProvider {
  80. public:
  81. virtual ~ICountersProvider() = default;
  82. virtual TCounter GetCounter(const TStringRef& module, const TStringRef& name, bool deriv) = 0;
  83. virtual TScopedProbe GetScopedProbe(const TStringRef& module, const TStringRef& name) = 0;
  84. };
  85. UDF_ASSERT_TYPE_SIZE(ICountersProvider, 8);
  86. } // namspace NUdf
  87. } // namspace NYql