sctp_callout.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*-
  2. * SPDX-License-Identifier: BSD-3-Clause
  3. *
  4. * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
  5. * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
  6. * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the project nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
  21. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. */
  32. #if defined(__FreeBSD__) && !defined(__Userspace__)
  33. #include <sys/cdefs.h>
  34. __FBSDID("$FreeBSD$");
  35. #endif
  36. #ifndef _NETINET_SCTP_CALLOUT_
  37. #define _NETINET_SCTP_CALLOUT_
  38. /*
  39. * NOTE: the following MACROS are required for locking the callout
  40. * queue along with a lock/mutex in the OS specific headers and
  41. * implementation files::
  42. * - SCTP_TIMERQ_LOCK()
  43. * - SCTP_TIMERQ_UNLOCK()
  44. * - SCTP_TIMERQ_LOCK_INIT()
  45. * - SCTP_TIMERQ_LOCK_DESTROY()
  46. */
  47. #define _SCTP_NEEDS_CALLOUT_ 1
  48. #define SCTP_TICKS_PER_FASTTIMO 20 /* called about every 20ms */
  49. #if defined(__Userspace__)
  50. #if defined(_WIN32)
  51. #define SCTP_TIMERQ_LOCK() EnterCriticalSection(&SCTP_BASE_VAR(timer_mtx))
  52. #define SCTP_TIMERQ_UNLOCK() LeaveCriticalSection(&SCTP_BASE_VAR(timer_mtx))
  53. #define SCTP_TIMERQ_LOCK_INIT() InitializeCriticalSection(&SCTP_BASE_VAR(timer_mtx))
  54. #define SCTP_TIMERQ_LOCK_DESTROY() DeleteCriticalSection(&SCTP_BASE_VAR(timer_mtx))
  55. #else
  56. #ifdef INVARIANTS
  57. #define SCTP_TIMERQ_LOCK() KASSERT(pthread_mutex_lock(&SCTP_BASE_VAR(timer_mtx)) == 0, ("%s: timer_mtx already locked", __func__))
  58. #define SCTP_TIMERQ_UNLOCK() KASSERT(pthread_mutex_unlock(&SCTP_BASE_VAR(timer_mtx)) == 0, ("%s: timer_mtx not locked", __func__))
  59. #else
  60. #define SCTP_TIMERQ_LOCK() (void)pthread_mutex_lock(&SCTP_BASE_VAR(timer_mtx))
  61. #define SCTP_TIMERQ_UNLOCK() (void)pthread_mutex_unlock(&SCTP_BASE_VAR(timer_mtx))
  62. #endif
  63. #define SCTP_TIMERQ_LOCK_INIT() (void)pthread_mutex_init(&SCTP_BASE_VAR(timer_mtx), &SCTP_BASE_VAR(mtx_attr))
  64. #define SCTP_TIMERQ_LOCK_DESTROY() (void)pthread_mutex_destroy(&SCTP_BASE_VAR(timer_mtx))
  65. #endif
  66. #endif
  67. uint32_t sctp_get_tick_count(void);
  68. TAILQ_HEAD(calloutlist, sctp_callout);
  69. struct sctp_callout {
  70. TAILQ_ENTRY(sctp_callout) tqe;
  71. uint32_t c_time; /* ticks to the event */
  72. void *c_arg; /* function argument */
  73. void (*c_func)(void *); /* function to call */
  74. int c_flags; /* state of this entry */
  75. };
  76. typedef struct sctp_callout sctp_os_timer_t;
  77. #define SCTP_CALLOUT_ACTIVE 0x0002 /* callout is currently active */
  78. #define SCTP_CALLOUT_PENDING 0x0004 /* callout is waiting for timeout */
  79. void sctp_os_timer_init(sctp_os_timer_t *tmr);
  80. /* Returns 1 if pending timer was rescheduled, 0 otherwise. */
  81. int sctp_os_timer_start(sctp_os_timer_t *, uint32_t, void (*)(void *), void *);
  82. /* Returns 1 if pending timer was stopped, 0 otherwise. */
  83. int sctp_os_timer_stop(sctp_os_timer_t *);
  84. void sctp_handle_tick(uint32_t);
  85. #define SCTP_OS_TIMER_INIT sctp_os_timer_init
  86. /*
  87. * NOTE: The next two shouldn't be called directly outside of sctp_timer_start()
  88. * and sctp_timer_stop(), since they don't handle incrementing/decrementing
  89. * relevant reference counts.
  90. */
  91. #define SCTP_OS_TIMER_START sctp_os_timer_start
  92. #define SCTP_OS_TIMER_STOP sctp_os_timer_stop
  93. /* MT FIXME: Is the following correct? */
  94. #define SCTP_OS_TIMER_STOP_DRAIN SCTP_OS_TIMER_STOP
  95. #define SCTP_OS_TIMER_PENDING(tmr) ((tmr)->c_flags & SCTP_CALLOUT_PENDING)
  96. #define SCTP_OS_TIMER_ACTIVE(tmr) ((tmr)->c_flags & SCTP_CALLOUT_ACTIVE)
  97. #define SCTP_OS_TIMER_DEACTIVATE(tmr) ((tmr)->c_flags &= ~SCTP_CALLOUT_ACTIVE)
  98. #if defined(__Userspace__)
  99. void sctp_start_timer_thread(void);
  100. void sctp_stop_timer_thread(void);
  101. #endif
  102. #if defined(__APPLE__) && !defined(__Userspace__)
  103. void sctp_timeout(void *);
  104. #endif
  105. #endif