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 /library/cpp/threading/future/core | |
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
Diffstat (limited to 'library/cpp/threading/future/core')
-rw-r--r-- | library/cpp/threading/future/core/future-inl.h | 22 | ||||
-rw-r--r-- | library/cpp/threading/future/core/future.h | 22 |
2 files changed, 44 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); |