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;
94 /* Calculate deadline ASAP, unit of measure = 100ns. */
95 GetSystemTimePreciseAsFileTime(&ft);
96 deadline.LowPart = ft.dwLowDateTime;
97 deadline.HighPart = ft.dwHighDateTime;
98 deadline.QuadPart += 10 * us;
100 ap = calloc(1, sizeof(*ap));
102 RTE_LOG(ERR, EAL, "Cannot allocate alarm entry\n");
107 timer = CreateWaitableTimer(NULL, FALSE, NULL);
109 RTE_LOG_WIN32_ERR("CreateWaitableTimer()");
118 /* Waitable timer must be set in the same thread that will
119 * do an alertable wait for the alarm to trigger, that is,
120 * in the interrupt thread.
122 if (rte_thread_is_intr()) {
123 /* Directly schedule callback execution. */
124 ret = alarm_set(ap, deadline);
126 RTE_LOG(ERR, EAL, "Cannot setup alarm\n");
130 /* Dispatch a task to set alarm into the interrupt thread.
131 * Execute it synchronously, because it can fail.
133 struct alarm_task task = {
135 .deadline = deadline,
138 ret = intr_thread_exec_sync(alarm_task_exec, &task);
140 RTE_LOG(ERR, EAL, "Cannot setup alarm in interrupt thread\n");
149 rte_spinlock_lock(&alarm_lock);
150 LIST_INSERT_HEAD(&alarm_list, ap, next);
151 rte_spinlock_unlock(&alarm_lock);
162 rte_eal_trace_alarm_set(us, cb_fn, cb_arg, ret);
167 alarm_matches(const struct alarm_entry *ap,
168 rte_eal_alarm_callback cb_fn, void *cb_arg)
170 bool any_arg = cb_arg == (void *)(-1);
171 return (ap->cb_fn == cb_fn) && (any_arg || ap->cb_arg == cb_arg);
175 rte_eal_alarm_cancel(rte_eal_alarm_callback cb_fn, void *cb_arg)
177 struct alarm_entry *ap;
186 rte_spinlock_lock(&alarm_lock);
188 LIST_FOREACH(ap, &alarm_list, next) {
189 if (!alarm_matches(ap, cb_fn, cb_arg))
193 if (atomic_compare_exchange_strong(
194 &ap->state, &state, ALARM_CANCELLED)) {
195 alarm_remove_unsafe(ap);
197 } else if (state == ALARM_TRIGGERED)
201 rte_spinlock_unlock(&alarm_lock);
204 rte_eal_trace_alarm_cancel(cb_fn, cb_arg, removed);
209 void (*func)(void *arg);
211 rte_spinlock_t lock; /* unlocked at task completion */
215 intr_thread_entry(void *arg)
217 struct intr_task *task = arg;
218 task->func(task->arg);
219 rte_spinlock_unlock(&task->lock);
223 intr_thread_exec_sync(void (*func)(void *arg), void *arg)
225 struct intr_task task;
230 rte_spinlock_init(&task.lock);
232 /* Make timers more precise by synchronizing in userspace. */
233 rte_spinlock_lock(&task.lock);
234 ret = eal_intr_thread_schedule(intr_thread_entry, &task);
236 RTE_LOG(ERR, EAL, "Cannot schedule task to interrupt thread\n");
240 /* Wait for the task to complete. */
241 rte_spinlock_lock(&task.lock);