Description
std::uint64_t SetTimer (std::chrono::steady_clock::time_point at,
std::chrono::nanoseconds interval,
Function<void (std::uint64_t )>&& cb) {
// This is ugly. But since we have to start a fiber each time user's `cb` is
// called, we must share it.
//
// We also take measures not to call user's callback before the previous call
// has returned. Otherwise we'll likely crash user's (presumably poor) code.
struct UserCallback {
void Run (std::uint64_t tid) {
if (!running.exchange (true , std::memory_order_acq_rel)) {
cb (tid);
}
running.store (false , std::memory_order_relaxed);
// Otherwise this call is lost. This can happen if user's code runs too
// slowly. For the moment we left the behavior as unspecified.
}
Function<void (std::uint64_t )> cb;
std::atomic<bool > running{};
};
auto ucb = std::make_shared<UserCallback>();
ucb->cb = std::move (cb);
auto sg = detail::NearestSchedulingGroup ();
auto timer_id = sg->CreateTimer (at, interval, [ucb](auto tid) mutable {
internal::StartFiberDetached ([ucb, tid] { ucb->cb (tid); });
});
sg->EnableTimer (timer_id);
return timer_id;
}
没有看懂
UserCallback的作用,
Run函数完全没有调用啊,
running也就显得乏力了
Reactions are currently unavailable
You can’t perform that action at this time.
flare/flare/fiber/timer.cc
Lines 53 to 83 in 17b70d1
没有看懂
UserCallback的作用,Run函数完全没有调用啊,running也就显得乏力了