timeout.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* $OpenBSD: timeout.h,v 1.25 2014/12/22 04:43:38 dlg Exp $ */
  2. /*
  3. * Copyright (c) 2000-2001 Artur Grabowski <art@openbsd.org>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. The name of the author may not be used to endorse or promote products
  13. * derived from this software without specific prior written permission.
  14. *
  15. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  16. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  18. * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  21. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  22. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  23. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  24. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #ifndef _SYS_TIMEOUT_H_
  27. #define _SYS_TIMEOUT_H_
  28. /*
  29. * Interface for handling time driven events in the kernel.
  30. *
  31. * The basic component of this API is the struct timeout. The user should not
  32. * touch the internals of this structure, but it's the users responsibility
  33. * to allocate and deallocate timeouts.
  34. *
  35. * The functions used to manipulate timeouts are:
  36. * - timeout_set(timeout, function, argument)
  37. * Initializes a timeout struct to call the function with the argument.
  38. * A timeout only needs to be initialized once.
  39. * - timeout_add(timeout, ticks)
  40. * Schedule this timeout to run in "ticks" ticks (there are hz ticks in
  41. * one second). You may not touch the timeout with timeout_set once the
  42. * timeout is scheduled. A second call to timeout_add with an already
  43. * scheduled timeout will cause the old timeout to be canceled and the
  44. * new will be scheduled.
  45. * - timeout_del(timeout)
  46. * Remove the timeout from the timeout queue. It's legal to remove
  47. * a timeout that has already happened.
  48. *
  49. * These functions may be called in interrupt context (anything below splhigh).
  50. */
  51. struct circq {
  52. struct circq *next; /* next element */
  53. struct circq *prev; /* previous element */
  54. };
  55. struct timeout {
  56. struct circq to_list; /* timeout queue, don't move */
  57. void (*to_func)(void *); /* function to call */
  58. void *to_arg; /* function argument */
  59. int to_time; /* ticks on event */
  60. int to_flags; /* misc flags */
  61. };
  62. /*
  63. * flags in the to_flags field.
  64. */
  65. #define TIMEOUT_ONQUEUE 2 /* timeout is on the todo queue */
  66. #define TIMEOUT_INITIALIZED 4 /* timeout is initialized */
  67. #define TIMEOUT_TRIGGERED 8 /* timeout is running or ran */
  68. #ifdef _KERNEL
  69. /*
  70. * special macros
  71. *
  72. * timeout_pending(to) - is this timeout already scheduled to run?
  73. * timeout_initialized(to) - is this timeout initialized?
  74. */
  75. #define timeout_pending(to) ((to)->to_flags & TIMEOUT_ONQUEUE)
  76. #define timeout_initialized(to) ((to)->to_flags & TIMEOUT_INITIALIZED)
  77. #define timeout_triggered(to) ((to)->to_flags & TIMEOUT_TRIGGERED)
  78. #define TIMEOUT_INITIALIZER(_f, _a) \
  79. { { NULL, NULL }, (_f), (_a), 0, TIMEOUT_INITIALIZED }
  80. struct bintime;
  81. void timeout_set(struct timeout *, void (*)(void *), void *);
  82. int timeout_add(struct timeout *, int);
  83. int timeout_add_tv(struct timeout *, const struct timeval *);
  84. int timeout_add_ts(struct timeout *, const struct timespec *);
  85. int timeout_add_bt(struct timeout *, const struct bintime *);
  86. int timeout_add_sec(struct timeout *, int);
  87. int timeout_add_msec(struct timeout *, int);
  88. int timeout_add_usec(struct timeout *, int);
  89. int timeout_add_nsec(struct timeout *, int);
  90. int timeout_del(struct timeout *);
  91. void timeout_startup(void);
  92. void timeout_adjust_ticks(int);
  93. /*
  94. * called once every hardclock. returns non-zero if we need to schedule a
  95. * softclock.
  96. */
  97. int timeout_hardclock_update(void);
  98. #endif /* _KERNEL */
  99. #endif /* _SYS_TIMEOUT_H_ */