diff --git a/base/include/timer.h b/base/include/timer.h index 596c4140c39510fdb0a5874711e6bfc7a03b7ead..9d04d7bc62d22501fe944189235f23d4a7cacdd2 100644 --- a/base/include/timer.h +++ b/base/include/timer.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -29,20 +29,23 @@ namespace OHOS { namespace Utils { -/* - * Notice: - * 1. Timer should be set up(via Setup()) before use, and shutdown(via Shutdown()) before its deconstruction. - * - * 2. Timer should be set up first and then shutdown. - * Avoid delegating them to different threads since it may cause multithreading problem. - * - * 3. Set up Timer again would not reset this Timer, but return `TIMER_ERR_INVALID_VALUE`. - * If a reset operation is required, shut Timer down first and then set it up. +/** + * @brief This is a timer manager. * - * 4. Parameter in Shutdown() determines whether the thread in Timer would be detach or join. - * (True(default) --> join; False --> detach). - * + Detach operation would cause possible multithreading problems, thus is not recommended. - * If a detach operation is required, availability of related objects used in `thread_` should be guaranteed. + * After "Timer" started, users can register several timed events, which can be + * continuous or once, to it. Some points need to be noticed:\n + * 1. Timer should be set up(via Setup()) before use, and shutdown + * (via Shutdown()) before its deconstruction.\n + * 2. Timer should be set up first and then shutdown. Avoid delegating them to + * different threads since it may cause multithreading problem.\n + * 3. Set up Timer again would not reset this Timer, but return + * `TIMER_ERR_INVALID_VALUE`. If a reset operation is required, shut Timer down + * first and then set it up.\n + * 4. Parameter in Shutdown() determines whether the thread in Timer would be + * detach or join(True(default) --> join; False --> detach). Detach operation + * would cause possible multithreading problems, thus is not recommended. If a + * detach operation is required, availability of related objects used in + * `thread_` should be guaranteed. */ class Timer { public: @@ -51,28 +54,61 @@ public: using TimerListCallback = std::function; public: - /* - * if performance-sensitive, change "timeout" larger before Setup - * default-value(1000ms), performance-estimate: occupy fixed-100us in every default-value(1000ms) - * timeout: range [-1, INT32MAX], but [-1,0] is not recommended - * -1: wait for ever(until event-trigger); - * 0: no wait, occupy too much cpu time; - * others: wait(until event-trigger) + /** + * @brief Construct Timer. + * + * If performance-sensitive, change "timeoutMs" larger before Setup. + * "timeoutMs" default-value(1000ms), performance-estimate: occupy + * fixed-100us in every default-value(1000ms). + * + * @param name Name for Timer. It is used as the name of thread in Timer. + * @param timeoutMs Time for waiting timer events. It is an integer in + * [-1, INT32MAX], but -1 and 0 is not recommended. -1 means waiting + * forever(until event-trigger). 0 means no waiting, which occupies too + * much cpu time. others means waiting(until event-trigger). */ explicit Timer(const std::string& name, int timeoutMs = 1000); virtual ~Timer() {} + /** + * @brief Set up "Timer". + * + * Do not set up repeatly before shutdown. + */ virtual uint32_t Setup(); - /* - * useJoin true: use std::thread::join(default) - * false: use std::thread::detach(not recommended) - * if timeoutMs = -1 and no valid event-trigger in epoll_wait: - * use std::thread::detach inside to avoid deadloop + /** + * @brief Shut down "Timer". + * + * There are two modes to shut the "Timer" down: blocking and unblocking. + * Blocking mode will shut "Timer" down until all running events in "Timer" + * finished. If "timeoutMs" is set as -1, use unblocking mode to avoid + * deadloop. + * + * @param useJoin Shutdown mode. true means blocking. false means + * unblocking(not recommended). */ virtual void Shutdown(bool useJoin = true); + /** + * @brief Regist timed events. + * + * @param callback callback function of a timed event. + * @param interval interval time(ms) of a timed event. + * @param once continuity of a timed event. true means discontinuous. false + * means continuous. Default is false. + * @return return ID of a timed event. You can use it as parameter of + * Unregister(). + * @see Unregister + */ uint32_t Register(const TimerCallback& callback, uint32_t interval /* ms */, bool once = false); + /** + * @brief Delete a timed events. + * + * @param timerId ID of the timed event need to be deleted. Users can get + * it by Register(). + * @see Register + */ void Unregister(uint32_t timerId); private: