0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-24 03:07:54 +01:00
nodejs/src/timer_wrap.h
Darshan Sen 2d368da19f
src: prevent extra copies of TimerWrap::TimerCb
I noticed that we were taking `TimerCb` as a `const&` and then copying
that into the member. This is completely fine when the constructor is
called with an lvalue. However, when called with an rvalue, we can allow
the `std::function` to be moved into the member instead of falling back
to a copy, so I changed the constructors to take in universal
references. Also, `std::function` constructors can take in multiple
arguments, so I further modified the constructors to use variadic
templates.

Signed-off-by: Darshan Sen <darshan.sen@postman.com>

PR-URL: https://github.com/nodejs/node/pull/40665
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2021-11-12 21:47:42 +00:00

86 lines
1.8 KiB
C++

#ifndef SRC_TIMER_WRAP_H_
#define SRC_TIMER_WRAP_H_
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
#include "memory_tracker.h"
#include "env.h"
#include "uv.h"
#include <functional>
namespace node {
// Utility class that makes working with libuv timers a bit easier.
class TimerWrap final : public MemoryRetainer {
public:
using TimerCb = std::function<void()>;
template <typename... Args>
explicit inline TimerWrap(Environment* env, Args&&... args);
TimerWrap(const TimerWrap&) = delete;
inline Environment* env() const { return env_; }
// Stop calling the timer callback.
void Stop();
// Render the timer unusable and delete this object.
void Close();
// Starts / Restarts the Timer
void Update(uint64_t interval, uint64_t repeat = 0);
void Ref();
void Unref();
SET_NO_MEMORY_INFO()
SET_MEMORY_INFO_NAME(TimerWrap)
SET_SELF_SIZE(TimerWrap)
private:
static void TimerClosedCb(uv_handle_t* handle);
static void OnTimeout(uv_timer_t* timer);
~TimerWrap() = default;
Environment* env_;
TimerCb fn_;
uv_timer_t timer_;
friend std::unique_ptr<TimerWrap>::deleter_type;
};
class TimerWrapHandle : public MemoryRetainer {
public:
template <typename... Args>
explicit inline TimerWrapHandle(Environment* env, Args&&... args);
TimerWrapHandle(const TimerWrapHandle&) = delete;
~TimerWrapHandle() { Close(); }
void Update(uint64_t interval, uint64_t repeat = 0);
void Ref();
void Unref();
void Stop();
void Close();
void MemoryInfo(node::MemoryTracker* tracker) const override;
SET_MEMORY_INFO_NAME(TimerWrapHandle)
SET_SELF_SIZE(TimerWrapHandle)
private:
static void CleanupHook(void* data);
TimerWrap* timer_;
};
} // namespace node
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
#endif // SRC_TIMER_WRAP_H_