Files
nearby/cpp/platform/cancelable_alarm.cc
T
Alexey Polyudov ae1c427b99 nearby: snapshot of cl/313536507
Signed-off-by: Alexey Polyudov <apolyudov@google.com>
Change-Id: I8936b527079074d5c3af5531b9245063767cc4a7
2020-05-28 00:03:22 -07:00

40 lines
1.0 KiB
C++

#include "platform/cancelable_alarm.h"
#include "platform/api/platform.h"
#include "platform/api/scheduled_executor.h"
#include "platform/synchronized.h"
namespace location {
namespace nearby {
namespace {
using Platform = platform::ImplementationPlatform;
}
CancelableAlarm::CancelableAlarm(const std::string &name,
Ptr<Runnable> runnable,
std::int64_t delay_millis,
Ptr<ScheduledExecutor> scheduled_executor)
: name_(name),
lock_(Platform::createLock()),
cancelable_(scheduled_executor->schedule(runnable, delay_millis)) {}
CancelableAlarm::~CancelableAlarm() { cancelable_.destroy(); }
bool CancelableAlarm::cancel() {
Synchronized s(lock_.get());
if (cancelable_.isNull()) {
// TODO(tracyzhou): Add logging
return false;
}
bool canceled = cancelable_->cancel();
// TODO(tracyzhou): Add logging
cancelable_.destroy();
return canceled;
}
} // namespace nearby
} // namespace location