mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-15 07:06:11 -04:00
Signed-off-by: Alexey Polyudov <apolyudov@google.com> Change-Id: I4bf367877a986910bb03e1c54aa972b4bcd59942
109 lines
2.8 KiB
C++
109 lines
2.8 KiB
C++
#ifndef PLATFORM_V2_PUBLIC_SETTABLE_FUTURE_H_
|
|
#define PLATFORM_V2_PUBLIC_SETTABLE_FUTURE_H_
|
|
|
|
#include <utility>
|
|
|
|
#include "platform_v2/public/condition_variable.h"
|
|
#include "platform_v2/public/mutex.h"
|
|
#include "platform_v2/public/mutex_lock.h"
|
|
#include "platform_v2/public/system_clock.h"
|
|
|
|
namespace location {
|
|
namespace nearby {
|
|
|
|
template <typename T>
|
|
class SettableFuture : public api::SettableFuture<T> {
|
|
public:
|
|
SettableFuture() = default;
|
|
~SettableFuture() override = default;
|
|
|
|
bool Set(T value) override {
|
|
MutexLock lock(&mutex_);
|
|
if (!done_) {
|
|
value_ = std::move(value);
|
|
done_ = true;
|
|
exception_ = {Exception::kSuccess};
|
|
completed_.Notify();
|
|
InvokeAllLocked();
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void AddListener(Runnable runnable, api::Executor* executor) override {
|
|
MutexLock lock(&mutex_);
|
|
if (done_) {
|
|
executor->Execute(std::move(runnable));
|
|
} else {
|
|
listeners_.emplace_back(std::make_pair(executor, std::move(runnable)));
|
|
}
|
|
}
|
|
|
|
bool SetException(Exception exception) override {
|
|
MutexLock lock(&mutex_);
|
|
return SetExceptionLocked(exception);
|
|
}
|
|
|
|
ExceptionOr<T> Get() override {
|
|
MutexLock lock(&mutex_);
|
|
while (!done_) {
|
|
completed_.Wait();
|
|
}
|
|
return exception_.value != Exception::kSuccess
|
|
? ExceptionOr<T>{exception_.value}
|
|
: ExceptionOr<T>{value_};
|
|
}
|
|
|
|
ExceptionOr<T> Get(absl::Duration timeout) override {
|
|
MutexLock lock(&mutex_);
|
|
while (!done_) {
|
|
absl::Time start_time = SystemClock::ElapsedRealtime();
|
|
if (completed_.Wait(timeout).Raised(Exception::kTimeout)) {
|
|
SetExceptionLocked({Exception::kTimeout});
|
|
break;
|
|
}
|
|
absl::Duration spent = SystemClock::ElapsedRealtime() - start_time;
|
|
if (spent < timeout) {
|
|
timeout -= spent;
|
|
} else if (!done_) {
|
|
SetExceptionLocked({Exception::kTimeout});
|
|
break;
|
|
}
|
|
}
|
|
return exception_.value != Exception::kSuccess
|
|
? ExceptionOr<T>{exception_.value}
|
|
: ExceptionOr<T>{value_};
|
|
}
|
|
|
|
private:
|
|
bool SetExceptionLocked(Exception exception) {
|
|
if (!done_) {
|
|
exception_ = exception.value != Exception::kSuccess
|
|
? exception
|
|
: Exception{Exception::kFailed};
|
|
done_ = true;
|
|
completed_.Notify();
|
|
InvokeAllLocked();
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void InvokeAllLocked() {
|
|
for (auto& item : listeners_) {
|
|
item.first->Execute(std::move(item.second));
|
|
}
|
|
listeners_.clear();
|
|
}
|
|
|
|
Mutex mutex_;
|
|
ConditionVariable completed_{&mutex_};
|
|
std::vector<std::pair<api::Executor*, std::function<void()>>> listeners_;
|
|
bool done_{false};
|
|
T value_;
|
|
Exception exception_{Exception::kFailed};
|
|
};
|
|
|
|
} // namespace nearby
|
|
} // namespace location
|
|
|
|
#endif // PLATFORM_V2_PUBLIC_SETTABLE_FUTURE_H_
|