timer.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Copyright (c) 2008-2009 Travis Geiselbrecht
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files
  6. * (the "Software"), to deal in the Software without restriction,
  7. * including without limitation the rights to use, copy, modify, merge,
  8. * publish, distribute, sublicense, and/or sell copies of the Software,
  9. * and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #ifndef __KERNEL_TIMER_H
  24. #define __KERNEL_TIMER_H
  25. #include <list.h>
  26. #include <sys/types.h>
  27. void timer_init(void);
  28. struct timer;
  29. typedef enum handler_return (*timer_callback)(struct timer *, time_t now, void *arg);
  30. #define TIMER_MAGIC 'timr'
  31. typedef struct timer {
  32. int magic;
  33. struct list_node node;
  34. time_t scheduled_time;
  35. time_t periodic_time;
  36. timer_callback callback;
  37. void *arg;
  38. } timer_t;
  39. /* Rules for Timers:
  40. * - Timer callbacks occur from interrupt context
  41. * - Timers may be programmed or canceled from interrupt or thread context
  42. * - Timers may be canceled or reprogrammed from within their callback
  43. * - Timers currently are dispatched from a 10ms periodic tick
  44. */
  45. void timer_initialize(timer_t *);
  46. void timer_set_oneshot(timer_t *, time_t delay, timer_callback, void *arg);
  47. void timer_set_periodic(timer_t *, time_t period, timer_callback, void *arg);
  48. void timer_cancel(timer_t *);
  49. #endif