aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/messagebus/misc
diff options
context:
space:
mode:
authornga <nga@yandex-team.ru>2022-02-10 16:48:09 +0300
committerDaniil Cherednik <dcherednik@yandex-team.ru>2022-02-10 16:48:09 +0300
commitc2a1af049e9deca890e9923abe64fe6c59060348 (patch)
treeb222e5ac2e2e98872661c51ccceee5da0d291e13 /library/cpp/messagebus/misc
parent1f553f46fb4f3c5eec631352cdd900a0709016af (diff)
downloadydb-c2a1af049e9deca890e9923abe64fe6c59060348.tar.gz
Restoring authorship annotation for <nga@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/messagebus/misc')
-rw-r--r--library/cpp/messagebus/misc/atomic_box.h46
-rw-r--r--library/cpp/messagebus/misc/test_sync.h134
-rw-r--r--library/cpp/messagebus/misc/weak_ptr.h166
-rw-r--r--library/cpp/messagebus/misc/weak_ptr_ut.cpp72
4 files changed, 209 insertions, 209 deletions
diff --git a/library/cpp/messagebus/misc/atomic_box.h b/library/cpp/messagebus/misc/atomic_box.h
index a7e83b70ab..401621f933 100644
--- a/library/cpp/messagebus/misc/atomic_box.h
+++ b/library/cpp/messagebus/misc/atomic_box.h
@@ -1,34 +1,34 @@
-#pragma once
-
+#pragma once
+
#include <util/system/atomic.h>
-// TAtomic with human interface
-template <typename T>
-class TAtomicBox {
-private:
- union {
- TAtomic Value;
- // when T is enum, it is convenient to inspect its content in gdb
- T ValueForDebugger;
- };
-
+// TAtomic with human interface
+template <typename T>
+class TAtomicBox {
+private:
+ union {
+ TAtomic Value;
+ // when T is enum, it is convenient to inspect its content in gdb
+ T ValueForDebugger;
+ };
+
static_assert(sizeof(T) <= sizeof(TAtomic), "expect sizeof(T) <= sizeof(TAtomic)");
-public:
+public:
TAtomicBox(T value = T())
: Value(value)
{
}
-
- void Set(T value) {
+
+ void Set(T value) {
AtomicSet(Value, (TAtomic)value);
- }
-
- T Get() const {
+ }
+
+ T Get() const {
return (T)AtomicGet(Value);
- }
-
- bool CompareAndSet(T expected, T set) {
+ }
+
+ bool CompareAndSet(T expected, T set) {
return AtomicCas(&Value, (TAtomicBase)set, (TAtomicBase)expected);
- }
-};
+ }
+};
diff --git a/library/cpp/messagebus/misc/test_sync.h b/library/cpp/messagebus/misc/test_sync.h
index 8e5aa212aa..be3f4f20b8 100644
--- a/library/cpp/messagebus/misc/test_sync.h
+++ b/library/cpp/messagebus/misc/test_sync.h
@@ -1,75 +1,75 @@
-#pragma once
-
+#pragma once
+
#include <util/system/condvar.h>
-#include <util/system/mutex.h>
-
-class TTestSync {
-private:
- unsigned Current;
-
- TMutex Mutex;
- TCondVar CondVar;
-
-public:
- TTestSync()
- : Current(0)
+#include <util/system/mutex.h>
+
+class TTestSync {
+private:
+ unsigned Current;
+
+ TMutex Mutex;
+ TCondVar CondVar;
+
+public:
+ TTestSync()
+ : Current(0)
{
}
-
- void Inc() {
- TGuard<TMutex> guard(Mutex);
-
- DoInc();
- CondVar.BroadCast();
- }
-
- unsigned Get() {
- TGuard<TMutex> guard(Mutex);
-
- return Current;
- }
-
- void WaitFor(unsigned n) {
- TGuard<TMutex> guard(Mutex);
-
+
+ void Inc() {
+ TGuard<TMutex> guard(Mutex);
+
+ DoInc();
+ CondVar.BroadCast();
+ }
+
+ unsigned Get() {
+ TGuard<TMutex> guard(Mutex);
+
+ return Current;
+ }
+
+ void WaitFor(unsigned n) {
+ TGuard<TMutex> guard(Mutex);
+
Y_VERIFY(Current <= n, "too late, waiting for %d, already %d", n, Current);
-
- while (n > Current) {
- CondVar.WaitI(Mutex);
- }
- }
-
- void WaitForAndIncrement(unsigned n) {
- TGuard<TMutex> guard(Mutex);
-
+
+ while (n > Current) {
+ CondVar.WaitI(Mutex);
+ }
+ }
+
+ void WaitForAndIncrement(unsigned n) {
+ TGuard<TMutex> guard(Mutex);
+
Y_VERIFY(Current <= n, "too late, waiting for %d, already %d", n, Current);
-
- while (n > Current) {
- CondVar.WaitI(Mutex);
- }
-
- DoInc();
- CondVar.BroadCast();
- }
-
- void CheckAndIncrement(unsigned n) {
- TGuard<TMutex> guard(Mutex);
-
+
+ while (n > Current) {
+ CondVar.WaitI(Mutex);
+ }
+
+ DoInc();
+ CondVar.BroadCast();
+ }
+
+ void CheckAndIncrement(unsigned n) {
+ TGuard<TMutex> guard(Mutex);
+
Y_VERIFY(Current == n, "must be %d, currently %d", n, Current);
-
- DoInc();
- CondVar.BroadCast();
- }
-
- void Check(unsigned n) {
- TGuard<TMutex> guard(Mutex);
-
+
+ DoInc();
+ CondVar.BroadCast();
+ }
+
+ void Check(unsigned n) {
+ TGuard<TMutex> guard(Mutex);
+
Y_VERIFY(Current == n, "must be %d, currently %d", n, Current);
- }
-
-private:
- void DoInc() {
- unsigned r = ++Current;
+ }
+
+private:
+ void DoInc() {
+ unsigned r = ++Current;
Y_UNUSED(r);
- }
-};
+ }
+};
diff --git a/library/cpp/messagebus/misc/weak_ptr.h b/library/cpp/messagebus/misc/weak_ptr.h
index 46b6496d86..70fdeb0e2a 100644
--- a/library/cpp/messagebus/misc/weak_ptr.h
+++ b/library/cpp/messagebus/misc/weak_ptr.h
@@ -1,99 +1,99 @@
-#pragma once
-
-#include <util/generic/ptr.h>
-#include <util/system/mutex.h>
-
-template <typename T>
-struct TWeakPtr;
-
-template <typename TSelf>
-struct TWeakRefCounted {
+#pragma once
+
+#include <util/generic/ptr.h>
+#include <util/system/mutex.h>
+
+template <typename T>
+struct TWeakPtr;
+
+template <typename TSelf>
+struct TWeakRefCounted {
template <typename>
friend struct TWeakPtr;
-private:
+private:
struct TRef: public TAtomicRefCount<TRef> {
- TMutex Mutex;
- TSelf* Outer;
-
+ TMutex Mutex;
+ TSelf* Outer;
+
TRef(TSelf* outer)
: Outer(outer)
{
}
-
- void Release() {
- TGuard<TMutex> g(Mutex);
+
+ void Release() {
+ TGuard<TMutex> g(Mutex);
Y_ASSERT(!!Outer);
Outer = nullptr;
- }
-
- TIntrusivePtr<TSelf> Get() {
- TGuard<TMutex> g(Mutex);
+ }
+
+ TIntrusivePtr<TSelf> Get() {
+ TGuard<TMutex> g(Mutex);
Y_ASSERT(!Outer || Outer->RefCount() > 0);
- return Outer;
- }
- };
-
- TAtomicCounter Counter;
- TIntrusivePtr<TRef> RefPtr;
-
-public:
- TWeakRefCounted()
- : RefPtr(new TRef(static_cast<TSelf*>(this)))
+ return Outer;
+ }
+ };
+
+ TAtomicCounter Counter;
+ TIntrusivePtr<TRef> RefPtr;
+
+public:
+ TWeakRefCounted()
+ : RefPtr(new TRef(static_cast<TSelf*>(this)))
{
}
-
- void Ref() {
- Counter.Inc();
- }
-
- void UnRef() {
- if (Counter.Dec() == 0) {
- RefPtr->Release();
-
- // drop is to prevent dtor from reading it
- RefPtr.Drop();
-
- delete static_cast<TSelf*>(this);
- }
- }
-
- void DecRef() {
- Counter.Dec();
- }
-
- unsigned RefCount() const {
- return Counter.Val();
- }
-};
-
-template <typename T>
-struct TWeakPtr {
-private:
- typedef TIntrusivePtr<typename T::TRef> TRefPtr;
- TRefPtr RefPtr;
-
-public:
+
+ void Ref() {
+ Counter.Inc();
+ }
+
+ void UnRef() {
+ if (Counter.Dec() == 0) {
+ RefPtr->Release();
+
+ // drop is to prevent dtor from reading it
+ RefPtr.Drop();
+
+ delete static_cast<TSelf*>(this);
+ }
+ }
+
+ void DecRef() {
+ Counter.Dec();
+ }
+
+ unsigned RefCount() const {
+ return Counter.Val();
+ }
+};
+
+template <typename T>
+struct TWeakPtr {
+private:
+ typedef TIntrusivePtr<typename T::TRef> TRefPtr;
+ TRefPtr RefPtr;
+
+public:
TWeakPtr() {
}
-
- TWeakPtr(T* t) {
- if (!!t) {
- RefPtr = t->RefPtr;
- }
- }
-
- TWeakPtr(TIntrusivePtr<T> t) {
- if (!!t) {
- RefPtr = t->RefPtr;
- }
- }
-
- TIntrusivePtr<T> Get() {
- if (!RefPtr) {
+
+ TWeakPtr(T* t) {
+ if (!!t) {
+ RefPtr = t->RefPtr;
+ }
+ }
+
+ TWeakPtr(TIntrusivePtr<T> t) {
+ if (!!t) {
+ RefPtr = t->RefPtr;
+ }
+ }
+
+ TIntrusivePtr<T> Get() {
+ if (!RefPtr) {
return nullptr;
- } else {
- return RefPtr->Get();
- }
- }
-};
+ } else {
+ return RefPtr->Get();
+ }
+ }
+};
diff --git a/library/cpp/messagebus/misc/weak_ptr_ut.cpp b/library/cpp/messagebus/misc/weak_ptr_ut.cpp
index 63d253e128..5a325278db 100644
--- a/library/cpp/messagebus/misc/weak_ptr_ut.cpp
+++ b/library/cpp/messagebus/misc/weak_ptr_ut.cpp
@@ -1,11 +1,11 @@
#include <library/cpp/testing/unittest/registar.h>
-
-#include "weak_ptr.h"
-
+
+#include "weak_ptr.h"
+
Y_UNIT_TEST_SUITE(TWeakPtrTest) {
- struct TWeakPtrTester: public TWeakRefCounted<TWeakPtrTester> {
- int* const CounterPtr;
-
+ struct TWeakPtrTester: public TWeakRefCounted<TWeakPtrTester> {
+ int* const CounterPtr;
+
TWeakPtrTester(int* counterPtr)
: CounterPtr(counterPtr)
{
@@ -13,34 +13,34 @@ Y_UNIT_TEST_SUITE(TWeakPtrTest) {
~TWeakPtrTester() {
++*CounterPtr;
}
- };
-
+ };
+
Y_UNIT_TEST(Simple) {
- int destroyCount = 0;
-
- TIntrusivePtr<TWeakPtrTester> p(new TWeakPtrTester(&destroyCount));
-
- UNIT_ASSERT(!!p);
- UNIT_ASSERT_VALUES_EQUAL(1u, p->RefCount());
-
- TWeakPtr<TWeakPtrTester> p2(p);
-
- UNIT_ASSERT_VALUES_EQUAL(1u, p->RefCount());
-
- {
- TIntrusivePtr<TWeakPtrTester> p3 = p2.Get();
- UNIT_ASSERT(!!p3);
- UNIT_ASSERT_VALUES_EQUAL(2u, p->RefCount());
- }
-
- p.Drop();
- UNIT_ASSERT_VALUES_EQUAL(1, destroyCount);
-
- {
- TIntrusivePtr<TWeakPtrTester> p3 = p2.Get();
- UNIT_ASSERT(!p3);
- }
-
- UNIT_ASSERT_VALUES_EQUAL(1, destroyCount);
- }
-}
+ int destroyCount = 0;
+
+ TIntrusivePtr<TWeakPtrTester> p(new TWeakPtrTester(&destroyCount));
+
+ UNIT_ASSERT(!!p);
+ UNIT_ASSERT_VALUES_EQUAL(1u, p->RefCount());
+
+ TWeakPtr<TWeakPtrTester> p2(p);
+
+ UNIT_ASSERT_VALUES_EQUAL(1u, p->RefCount());
+
+ {
+ TIntrusivePtr<TWeakPtrTester> p3 = p2.Get();
+ UNIT_ASSERT(!!p3);
+ UNIT_ASSERT_VALUES_EQUAL(2u, p->RefCount());
+ }
+
+ p.Drop();
+ UNIT_ASSERT_VALUES_EQUAL(1, destroyCount);
+
+ {
+ TIntrusivePtr<TWeakPtrTester> p3 = p2.Get();
+ UNIT_ASSERT(!p3);
+ }
+
+ UNIT_ASSERT_VALUES_EQUAL(1, destroyCount);
+ }
+}