1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2020 Dmitry Kozlyuk
9 #include <rte_spinlock.h>
11 #include <rte_eal_trace.h>
13 #include "eal_windows.h"
22 LIST_ENTRY(alarm_entry) next;
23 rte_eal_alarm_callback cb_fn;
29 static LIST_HEAD(alarm_list, alarm_entry) alarm_list = LIST_HEAD_INITIALIZER();
31 static rte_spinlock_t alarm_lock = RTE_SPINLOCK_INITIALIZER;
33 static int intr_thread_exec_sync(void (*func)(void *arg), void *arg);
36 alarm_remove_unsafe(struct alarm_entry *ap)
38 LIST_REMOVE(ap, next);
39 CloseHandle(ap->timer);
44 alarm_callback(void *arg, DWORD low __rte_unused, DWORD high __rte_unused)
46 struct alarm_entry *ap = arg;
47 unsigned int state = ALARM_ARMED;
49 if (!atomic_compare_exchange_strong(
50 &ap->state, &state, ALARM_TRIGGERED))
53 ap->cb_fn(ap->cb_arg);
55 rte_spinlock_lock(&alarm_lock);
56 alarm_remove_unsafe(ap);
57 rte_spinlock_unlock(&alarm_lock);
61 alarm_set(struct alarm_entry *entry, LARGE_INTEGER deadline)
63 BOOL ret = SetWaitableTimer(
64 entry->timer, &deadline, 0, alarm_callback, entry, FALSE);
66 RTE_LOG_WIN32_ERR("SetWaitableTimer");
73 struct alarm_entry *entry;
74 LARGE_INTEGER deadline;
79 alarm_task_exec(void *arg)
81 struct alarm_task *task = arg;
82 task->ret = alarm_set(task->entry, task->deadline);
86 rte_eal_alarm_set(uint64_t us, rte_eal_alarm_callback cb_fn, void *cb_arg)
88 struct alarm_entry *ap;
91 LARGE_INTEGER deadline;
95 RTE_LOG(ERR, EAL, "NULL callback\n");
100 /* Calculate deadline ASAP, unit of measure = 100ns. */
101 GetSystemTimePreciseAsFileTime(&ft);
102 deadline.LowPart = ft.dwLowDateTime;
103 deadline.HighPart = ft.dwHighDateTime;
104 deadline.QuadPart += 10 * us;
106 ap = calloc(1, sizeof(*ap));
108 RTE_LOG(ERR, EAL, "Cannot allocate alarm entry\n");
113 timer = CreateWaitableTimer(NULL, FALSE, NULL);
115 RTE_LOG_WIN32_ERR("CreateWaitableTimer()");
124 /* Waitable timer must be set in the same thread that will
125 * do an alertable wait for the alarm to trigger, that is,
126 * in the interrupt thread.
128 if (rte_thread_is_intr()) {
129 /* Directly schedule callback execution. */
130 ret = alarm_set(ap, deadline);
132 RTE_LOG(ERR, EAL, "Cannot setup alarm\n");
136 /* Dispatch a task to set alarm into the interrupt thread.
137 * Execute it synchronously, because it can fail.
139 struct alarm_task task = {
141 .deadline = deadline,
144 ret = intr_thread_exec_sync(alarm_task_exec, &task);
146 RTE_LOG(ERR, EAL, "Cannot setup alarm in interrupt thread\n");
155 rte_spinlock_lock(&alarm_lock);
156 LIST_INSERT_HEAD(&alarm_list, ap, next);
157 rte_spinlock_unlock(&alarm_lock);
168 rte_eal_trace_alarm_set(us, cb_fn, cb_arg, ret);
173 alarm_matches(const struct alarm_entry *ap,
174 rte_eal_alarm_callback cb_fn, void *cb_arg)
176 bool any_arg = cb_arg == (void *)(-1);
177 return (ap->cb_fn == cb_fn) && (any_arg || ap->cb_arg == cb_arg);
181 rte_eal_alarm_cancel(rte_eal_alarm_callback cb_fn, void *cb_arg)
183 struct alarm_entry *ap;
191 RTE_LOG(ERR, EAL, "NULL callback\n");
198 rte_spinlock_lock(&alarm_lock);
200 LIST_FOREACH(ap, &alarm_list, next) {
201 if (!alarm_matches(ap, cb_fn, cb_arg))
205 if (atomic_compare_exchange_strong(
206 &ap->state, &state, ALARM_CANCELLED)) {
207 alarm_remove_unsafe(ap);
209 } else if (state == ALARM_TRIGGERED)
213 rte_spinlock_unlock(&alarm_lock);
216 rte_eal_trace_alarm_cancel(cb_fn, cb_arg, removed);
221 void (*func)(void *arg);
223 rte_spinlock_t lock; /* unlocked at task completion */
227 intr_thread_entry(void *arg)
229 struct intr_task *task = arg;
230 task->func(task->arg);
231 rte_spinlock_unlock(&task->lock);
235 intr_thread_exec_sync(void (*func)(void *arg), void *arg)
237 struct intr_task task;
242 rte_spinlock_init(&task.lock);
244 /* Make timers more precise by synchronizing in userspace. */
245 rte_spinlock_lock(&task.lock);
246 ret = eal_intr_thread_schedule(intr_thread_entry, &task);
248 RTE_LOG(ERR, EAL, "Cannot schedule task to interrupt thread\n");
252 /* Wait for the task to complete. */
253 rte_spinlock_lock(&task.lock);