123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- /*
- * Copyright (c) 2017 Richard Braun.
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- *
- * Low resolution timer system.
- */
- #ifndef KERN_TIMER_H
- #define KERN_TIMER_H
- #include <stdint.h>
- #include <kern/hlist.h>
- #include <kern/init.h>
- #include <kern/work.h>
- // Scheduling flags.
- #define TIMER_DETACHED 0x1 // Timer completion isn't synchronized.
- #define TIMER_INTR 0x2 // Handler is run from interrupt context.
- #define TIMER_HIGH_PRIO 0x4 // Handler is run in high priority thread.
- struct timer;
- // Type for timer functions.
- typedef void (*timer_fn_t) (struct timer *);
- /*
- * Locking keys :
- * (c) cpu_data
- * (a) atomic
- *
- * (*) The cpu member is used to determine which lock serializes access to
- * the structure. It must be accessed atomically, but updated while the
- * timer is locked.
- */
- struct timer
- {
- union
- {
- struct hlist_node node; // (c)
- struct work work;
- };
- uint64_t ticks; // (c)
- timer_fn_t fn;
- unsigned int cpu; // (c,a,*)
- unsigned short state; // (c)
- unsigned short flags; // (c)
- struct thread *joiner; // (c)
- };
- /*
- * Return the absolute expiration time of the timer, in ticks.
- *
- * This function may not be called while another thread is scheduling the
- * timer.
- */
- static inline uint64_t
- timer_get_time (const struct timer *timer)
- {
- return (timer->ticks);
- }
- /*
- * Initialize a timer.
- *
- * Timers that are reponsible for releasing their own resources must
- * be detached.
- */
- void timer_init (struct timer *timer, timer_fn_t fn, int flags);
- /*
- * Schedule a timer.
- *
- * The time of expiration is an absolute time in ticks.
- *
- * Timers may safely be rescheduled after completion. Periodic timers are
- * implemented by rescheduling from the handler.
- *
- * If the timer has been canceled, this function does nothing. A
- * canceled timer must be reinitialized before being scheduled again.
- *
- * This function may safely be called in interrupt context.
- */
- void timer_schedule (struct timer *timer, uint64_t ticks);
- /*
- * Cancel a timer.
- *
- * The given timer must not be detached.
- *
- * If the timer has already expired, this function waits until the timer
- * function completes, or returns immediately if the function has already
- * completed.
- *
- * This function may safely be called from the timer handler, but not on
- * the current timer. Canceling a timer from the handler is achieved by
- * simply not rescheduling it.
- */
- void timer_cancel (struct timer *timer);
- /*
- * Report a periodic event on the current processor.
- *
- * Interrupts and preemption must be disabled when calling this function.
- */
- void timer_report_periodic_event (void);
- /*
- * This init operation provides :
- * - timer initialization and scheduling
- */
- INIT_OP_DECLARE (timer_bootstrap);
- #endif
|