diff options
author | ilnurkh <ilnurkh@yandex-team.com> | 2024-07-26 15:19:25 +0300 |
---|---|---|
committer | ilnurkh <ilnurkh@yandex-team.com> | 2024-07-26 15:31:14 +0300 |
commit | 696e048df66bace916e830df8fc37349098d22cf (patch) | |
tree | f23871493d3a95d3a2b96ea58e7591118277fbd2 | |
parent | 43c64f9b535d8e793a95647872127d2843ac0f01 (diff) | |
download | ydb-696e048df66bace916e830df8fc37349098d22cf.tar.gz |
TFuture add IsReady method
IsReady returns true if exception or value was set.
allows to check readiness without locking cheŃker-thread
NOTE: returns true even if value was extracted from promise
good replace for HasValue() || HasException()
b2d64093e25cef5a350dfebe784c449203a5b383
-rw-r--r-- | library/cpp/threading/future/core/future-inl.h | 22 | ||||
-rw-r--r-- | library/cpp/threading/future/core/future.h | 22 | ||||
-rw-r--r-- | library/cpp/threading/future/future_ut.cpp | 4 |
3 files changed, 48 insertions, 0 deletions
diff --git a/library/cpp/threading/future/core/future-inl.h b/library/cpp/threading/future/core/future-inl.h index df6b7c21b2..142ea92567 100644 --- a/library/cpp/threading/future/core/future-inl.h +++ b/library/cpp/threading/future/core/future-inl.h @@ -116,6 +116,9 @@ namespace NThreading { bool HasException() const { return AtomicGet(State) == ExceptionSet; } + bool IsReady() const { + return AtomicGet(State) != NotReady; + } const T& GetValue(TDuration timeout = TDuration::Zero()) const { AccessValue(timeout, ValueRead); @@ -297,6 +300,9 @@ namespace NThreading { bool HasException() const { return AtomicGet(State) == ExceptionSet; } + bool IsReady() const { + return AtomicGet(State) != NotReady; + } void GetValue(TDuration timeout = TDuration::Zero()) const { TAtomicBase state = AtomicGet(State); @@ -583,6 +589,10 @@ namespace NThreading { inline bool TFuture<T>::HasException() const { return State && State->HasException(); } + template <typename T> + inline bool TFuture<T>::IsReady() const { + return State && State->IsReady(); + } template <typename T> inline void TFuture<T>::Wait() const { @@ -688,6 +698,9 @@ namespace NThreading { inline bool TFuture<void>::HasException() const { return State && State->HasException(); } + inline bool TFuture<void>::IsReady() const { + return State && State->IsReady(); + } inline void TFuture<void>::Wait() const { EnsureInitialized(); @@ -824,6 +837,11 @@ namespace NThreading { } template <typename T> + inline bool TPromise<T>::IsReady() const { + return State && State->IsReady(); + } + + template <typename T> inline void TPromise<T>::SetException(const TString& e) { EnsureInitialized(); State->SetException(std::make_exception_ptr(yexception() << e)); @@ -904,6 +922,10 @@ namespace NThreading { return State && State->HasException(); } + inline bool TPromise<void>::IsReady() const { + return State && State->IsReady(); + } + inline void TPromise<void>::SetException(const TString& e) { EnsureInitialized(); State->SetException(std::make_exception_ptr(yexception() << e)); diff --git a/library/cpp/threading/future/core/future.h b/library/cpp/threading/future/core/future.h index 598336282a..1e76a04d3f 100644 --- a/library/cpp/threading/future/core/future.h +++ b/library/cpp/threading/future/core/future.h @@ -98,6 +98,12 @@ namespace NThreading { void TryRethrow() const; bool HasException() const; + // returns true if exception or value was set. + // allows to check readiness without locking cheker-thread + // NOTE: returns true even if value was extracted from promise + // good replace for HasValue() || HasException() + bool IsReady() const; + void Wait() const; bool Wait(TDuration timeout) const; bool Wait(TInstant deadline) const; @@ -153,6 +159,11 @@ namespace NThreading { void TryRethrow() const; bool HasException() const; + // returns true if exception or value was set. + // allows to check readiness without locking cheker-thread + // good replace for HasValue() || HasException() + bool IsReady() const; + void Wait() const; bool Wait(TDuration timeout) const; bool Wait(TInstant deadline) const; @@ -216,6 +227,12 @@ namespace NThreading { void TryRethrow() const; bool HasException() const; + + // returns true if exception or value was set. + // allows to check readiness without locking cheker-thread + // NOTE: returns true even if value was extracted from promise + // good replace for HasValue() || HasException() + bool IsReady() const; void SetException(const TString& e); void SetException(std::exception_ptr e); bool TrySetException(std::exception_ptr e); @@ -256,6 +273,11 @@ namespace NThreading { void TryRethrow() const; bool HasException() const; + + // returns true if exception or value was set. + // allows to check readiness without locking cheker-thread + // good replace for HasValue() || HasException() + bool IsReady() const; void SetException(const TString& e); void SetException(std::exception_ptr e); bool TrySetException(std::exception_ptr e); diff --git a/library/cpp/threading/future/future_ut.cpp b/library/cpp/threading/future/future_ut.cpp index 26f8fa9e9c..ceb07cae93 100644 --- a/library/cpp/threading/future/future_ut.cpp +++ b/library/cpp/threading/future/future_ut.cpp @@ -105,6 +105,7 @@ namespace { future = MakeFuture(345); UNIT_ASSERT(future.HasValue()); + UNIT_ASSERT(future.IsReady()); UNIT_ASSERT_EQUAL(future.GetValue(), 345); } @@ -115,6 +116,7 @@ namespace { TFuture<void> future = promise.GetFuture(); UNIT_ASSERT(future.HasValue()); + UNIT_ASSERT(future.IsReady()); future = MakeFuture(); UNIT_ASSERT(future.HasValue()); @@ -523,6 +525,7 @@ namespace { { auto future1 = MakeErrorFuture<void>(std::make_exception_ptr(TFutureException())); UNIT_ASSERT(future1.HasException()); + UNIT_ASSERT(future1.IsReady()); UNIT_CHECK_GENERATED_EXCEPTION(future1.GetValue(), TFutureException); auto future2 = MakeErrorFuture<int>(std::make_exception_ptr(TFutureException())); @@ -563,6 +566,7 @@ namespace { promise2.SetException("foo-exception"); wait.Wait(); UNIT_ASSERT(future2.HasException()); + UNIT_ASSERT(!future1.IsReady()); UNIT_ASSERT(!future1.HasValue() && !future1.HasException()); } |