Skip to content

Commit 8e1e3b5

Browse files
jasnelltargos
authored andcommitted
src: replace InspectorTimer with TimerWrap utility
Signed-off-by: James M Snell <[email protected]> PR-URL: #34186 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent a3a4d2c commit 8e1e3b5

File tree

1 file changed

+8
-84
lines changed

1 file changed

+8
-84
lines changed

src/inspector_agent.cc

+8-84
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "node_process.h"
1616
#include "node_url.h"
1717
#include "util-inl.h"
18+
#include "timer_wrap.h"
1819
#include "v8-inspector.h"
1920
#include "v8-platform.h"
2021

@@ -326,86 +327,6 @@ class ChannelImpl final : public v8_inspector::V8Inspector::Channel,
326327
bool retaining_context_;
327328
};
328329

329-
class InspectorTimer {
330-
public:
331-
InspectorTimer(Environment* env,
332-
double interval_s,
333-
V8InspectorClient::TimerCallback callback,
334-
void* data) : env_(env),
335-
callback_(callback),
336-
data_(data) {
337-
uv_timer_init(env->event_loop(), &timer_);
338-
int64_t interval_ms = 1000 * interval_s;
339-
uv_timer_start(&timer_, OnTimer, interval_ms, interval_ms);
340-
timer_.data = this;
341-
}
342-
343-
InspectorTimer(const InspectorTimer&) = delete;
344-
345-
void Stop() {
346-
if (timer_.data == nullptr) return;
347-
348-
timer_.data = nullptr;
349-
uv_timer_stop(&timer_);
350-
env_->CloseHandle(reinterpret_cast<uv_handle_t*>(&timer_), TimerClosedCb);
351-
}
352-
353-
inline Environment* env() const { return env_; }
354-
355-
private:
356-
static void OnTimer(uv_timer_t* uvtimer) {
357-
InspectorTimer* timer = node::ContainerOf(&InspectorTimer::timer_, uvtimer);
358-
timer->callback_(timer->data_);
359-
}
360-
361-
static void TimerClosedCb(uv_handle_t* uvtimer) {
362-
std::unique_ptr<InspectorTimer> timer(
363-
node::ContainerOf(&InspectorTimer::timer_,
364-
reinterpret_cast<uv_timer_t*>(uvtimer)));
365-
// Unique_ptr goes out of scope here and pointer is deleted.
366-
}
367-
368-
~InspectorTimer() = default;
369-
370-
Environment* env_;
371-
uv_timer_t timer_;
372-
V8InspectorClient::TimerCallback callback_;
373-
void* data_;
374-
375-
friend std::unique_ptr<InspectorTimer>::deleter_type;
376-
};
377-
378-
class InspectorTimerHandle {
379-
public:
380-
InspectorTimerHandle(Environment* env, double interval_s,
381-
V8InspectorClient::TimerCallback callback, void* data) {
382-
timer_ = new InspectorTimer(env, interval_s, callback, data);
383-
384-
env->AddCleanupHook(CleanupHook, this);
385-
}
386-
387-
InspectorTimerHandle(const InspectorTimerHandle&) = delete;
388-
389-
~InspectorTimerHandle() {
390-
Stop();
391-
}
392-
393-
private:
394-
void Stop() {
395-
if (timer_ != nullptr) {
396-
timer_->env()->RemoveCleanupHook(CleanupHook, this);
397-
timer_->Stop();
398-
}
399-
timer_ = nullptr;
400-
}
401-
402-
static void CleanupHook(void* data) {
403-
static_cast<InspectorTimerHandle*>(data)->Stop();
404-
}
405-
406-
InspectorTimer* timer_;
407-
};
408-
409330
class SameThreadInspectorSession : public InspectorSession {
410331
public:
411332
SameThreadInspectorSession(
@@ -602,9 +523,12 @@ class NodeInspectorClient : public V8InspectorClient {
602523
void startRepeatingTimer(double interval_s,
603524
TimerCallback callback,
604525
void* data) override {
605-
timers_.emplace(std::piecewise_construct, std::make_tuple(data),
606-
std::make_tuple(env_, interval_s, callback,
607-
data));
526+
auto result =
527+
timers_.emplace(std::piecewise_construct, std::make_tuple(data),
528+
std::make_tuple(env_, callback, data));
529+
CHECK(result.second);
530+
uint64_t interval = 1000 * interval_s;
531+
result.first->second.Update(interval, interval);
608532
}
609533

610534
void cancelTimer(void* data) override {
@@ -724,7 +648,7 @@ class NodeInspectorClient : public V8InspectorClient {
724648
bool running_nested_loop_ = false;
725649
std::unique_ptr<V8Inspector> client_;
726650
// Note: ~ChannelImpl may access timers_ so timers_ has to come first.
727-
std::unordered_map<void*, InspectorTimerHandle> timers_;
651+
std::unordered_map<void*, TimerWrapHandle> timers_;
728652
std::unordered_map<int, std::unique_ptr<ChannelImpl>> channels_;
729653
int next_session_id_ = 1;
730654
bool waiting_for_resume_ = false;

0 commit comments

Comments
 (0)