blob: b3b348cb5ebd004979f321c0be1925da2a704c5b [file] [log] [blame]
#ifndef PRODUCTION_SUSHID_SAFEPOWER_AGENT_BMC_SCHEDULER_BMC_H_
#define PRODUCTION_SUSHID_SAFEPOWER_AGENT_BMC_SCHEDULER_BMC_H_
#include <chrono> // NOLINT(build/c++11)
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <unordered_map>
#include "scheduler_interface.h"
#include "absl/base/attributes.h"
#include "absl/base/thread_annotations.h"
#include "absl/functional/any_invocable.h"
#include "absl/status/status.h"
#include "absl/synchronization/mutex.h"
#include "absl/time/time.h"
// NOLINTBEGIN(readability/boost): this file is only used for test in google3
#include "boost/asio.hpp"
#include "boost/asio/io_context.hpp"
#include "boost/asio/io_service.hpp"
#include "boost/asio/steady_timer.hpp"
#include "boost/date_time/posix_time/posix_time_types.hpp"
#include "boost/system/detail/error_code.hpp"
// NOLINTEND(readability/boost)
namespace safepower_agent {
class SchedulerBMC : public SchedulerInterface,
public std::enable_shared_from_this<SchedulerBMC> {
public:
SchedulerBMC() = delete;
explicit SchedulerBMC(
boost::asio::io_context& io_in ABSL_ATTRIBUTE_LIFETIME_BOUND);
~SchedulerBMC() override;
absl::Status PeriodicCall(absl::AnyInvocable<void()> fn,
absl::Duration interval,
std::string_view name) override;
absl::Status DelayCall(absl::AnyInvocable<void() &&> fn, absl::Duration delay,
std::string_view name) override;
absl::Status CancelCall(std::string_view name) override;
ABSL_LOCKS_EXCLUDED(lock_)
absl::Status CancelAll() override ABSL_LOCKS_EXCLUDED(lock_);
absl::Status Shutdown() override ABSL_LOCKS_EXCLUDED(lock_);
private:
void CallbackPeriodic(std::string task_name,
std::unique_ptr<boost::asio::steady_timer> timer,
std::chrono::milliseconds wait_duration,
std::shared_ptr<absl::AnyInvocable<void()>> fn)
ABSL_LOCKS_EXCLUDED(lock_);
void Print() ABSL_EXCLUSIVE_LOCKS_REQUIRED(lock_);
boost::asio::io_context& io_;
boost::asio::executor_work_guard<boost::asio::io_context::executor_type>
work_guard_;
// TODO: b/349393089 switch to absl::flat_hash_map, when asan error is fixed
std::unordered_map<std::string, boost::asio::steady_timer*> timer_by_name_
ABSL_GUARDED_BY(lock_);
absl::Mutex lock_;
bool shutdown_called_;
std::optional<std::string> current_task_;
};
} // namespace safepower_agent
#endif // PRODUCTION_SUSHID_SAFEPOWER_AGENT_BMC_SCHEDULER_BMC_H_