michael@0: /*- michael@0: * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved. michael@0: * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved. michael@0: * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions are met: michael@0: * michael@0: * a) Redistributions of source code must retain the above copyright notice, michael@0: * this list of conditions and the following disclaimer. michael@0: * michael@0: * b) Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in michael@0: * the documentation and/or other materials provided with the distribution. michael@0: * michael@0: * c) Neither the name of Cisco Systems, Inc. nor the names of its michael@0: * contributors may be used to endorse or promote products derived michael@0: * from this software without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, michael@0: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE michael@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE michael@0: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR michael@0: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF michael@0: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS michael@0: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN michael@0: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) michael@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF michael@0: * THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: michael@0: #if defined(__Userspace__) michael@0: #include michael@0: #if !defined (__Userspace_os_Windows) michael@0: #include michael@0: #include michael@0: #include michael@0: #endif michael@0: #if defined(__Userspace_os_NaCl) michael@0: #include michael@0: #endif michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #else michael@0: #include michael@0: #include michael@0: #include michael@0: #endif michael@0: michael@0: /* michael@0: * Callout/Timer routines for OS that doesn't have them michael@0: */ michael@0: #if defined(__APPLE__) || defined(__Userspace__) michael@0: int ticks = 0; michael@0: #else michael@0: extern int ticks; michael@0: #endif michael@0: michael@0: /* michael@0: * SCTP_TIMERQ_LOCK protects: michael@0: * - SCTP_BASE_INFO(callqueue) michael@0: * - sctp_os_timer_current: current timer in process michael@0: * - sctp_os_timer_next: next timer to check michael@0: */ michael@0: static sctp_os_timer_t *sctp_os_timer_current = NULL; michael@0: static sctp_os_timer_t *sctp_os_timer_next = NULL; michael@0: michael@0: void michael@0: sctp_os_timer_init(sctp_os_timer_t *c) michael@0: { michael@0: bzero(c, sizeof(*c)); michael@0: } michael@0: michael@0: void michael@0: sctp_os_timer_start(sctp_os_timer_t *c, int to_ticks, void (*ftn) (void *), michael@0: void *arg) michael@0: { michael@0: /* paranoia */ michael@0: if ((c == NULL) || (ftn == NULL)) michael@0: return; michael@0: michael@0: SCTP_TIMERQ_LOCK(); michael@0: /* check to see if we're rescheduling a timer */ michael@0: if (c->c_flags & SCTP_CALLOUT_PENDING) { michael@0: if (c == sctp_os_timer_next) { michael@0: sctp_os_timer_next = TAILQ_NEXT(c, tqe); michael@0: } michael@0: TAILQ_REMOVE(&SCTP_BASE_INFO(callqueue), c, tqe); michael@0: /* michael@0: * part of the normal "stop a pending callout" process michael@0: * is to clear the CALLOUT_ACTIVE and CALLOUT_PENDING michael@0: * flags. We don't bother since we are setting these michael@0: * below and we still hold the lock. michael@0: */ michael@0: } michael@0: michael@0: /* michael@0: * We could unlock/splx here and lock/spl at the TAILQ_INSERT_TAIL, michael@0: * but there's no point since doing this setup doesn't take much time. michael@0: */ michael@0: if (to_ticks <= 0) michael@0: to_ticks = 1; michael@0: michael@0: c->c_arg = arg; michael@0: c->c_flags = (SCTP_CALLOUT_ACTIVE | SCTP_CALLOUT_PENDING); michael@0: c->c_func = ftn; michael@0: c->c_time = ticks + to_ticks; michael@0: TAILQ_INSERT_TAIL(&SCTP_BASE_INFO(callqueue), c, tqe); michael@0: SCTP_TIMERQ_UNLOCK(); michael@0: } michael@0: michael@0: int michael@0: sctp_os_timer_stop(sctp_os_timer_t *c) michael@0: { michael@0: SCTP_TIMERQ_LOCK(); michael@0: /* michael@0: * Don't attempt to delete a callout that's not on the queue. michael@0: */ michael@0: if (!(c->c_flags & SCTP_CALLOUT_PENDING)) { michael@0: c->c_flags &= ~SCTP_CALLOUT_ACTIVE; michael@0: SCTP_TIMERQ_UNLOCK(); michael@0: return (0); michael@0: } michael@0: c->c_flags &= ~(SCTP_CALLOUT_ACTIVE | SCTP_CALLOUT_PENDING); michael@0: if (c == sctp_os_timer_next) { michael@0: sctp_os_timer_next = TAILQ_NEXT(c, tqe); michael@0: } michael@0: TAILQ_REMOVE(&SCTP_BASE_INFO(callqueue), c, tqe); michael@0: SCTP_TIMERQ_UNLOCK(); michael@0: return (1); michael@0: } michael@0: michael@0: static void michael@0: sctp_handle_tick(int delta) michael@0: { michael@0: sctp_os_timer_t *c; michael@0: void (*c_func)(void *); michael@0: void *c_arg; michael@0: michael@0: SCTP_TIMERQ_LOCK(); michael@0: /* update our tick count */ michael@0: ticks += delta; michael@0: c = TAILQ_FIRST(&SCTP_BASE_INFO(callqueue)); michael@0: while (c) { michael@0: if (c->c_time <= ticks) { michael@0: sctp_os_timer_next = TAILQ_NEXT(c, tqe); michael@0: TAILQ_REMOVE(&SCTP_BASE_INFO(callqueue), c, tqe); michael@0: c_func = c->c_func; michael@0: c_arg = c->c_arg; michael@0: c->c_flags &= ~SCTP_CALLOUT_PENDING; michael@0: sctp_os_timer_current = c; michael@0: SCTP_TIMERQ_UNLOCK(); michael@0: c_func(c_arg); michael@0: SCTP_TIMERQ_LOCK(); michael@0: sctp_os_timer_current = NULL; michael@0: c = sctp_os_timer_next; michael@0: } else { michael@0: c = TAILQ_NEXT(c, tqe); michael@0: } michael@0: } michael@0: sctp_os_timer_next = NULL; michael@0: SCTP_TIMERQ_UNLOCK(); michael@0: } michael@0: michael@0: #if defined(__APPLE__) michael@0: void michael@0: sctp_timeout(void *arg SCTP_UNUSED) michael@0: { michael@0: sctp_handle_tick(SCTP_BASE_VAR(sctp_main_timer_ticks)); michael@0: sctp_start_main_timer(); michael@0: } michael@0: #endif michael@0: michael@0: #if defined(__Userspace__) michael@0: #define TIMEOUT_INTERVAL 10 michael@0: michael@0: void * michael@0: user_sctp_timer_iterate(void *arg) michael@0: { michael@0: for (;;) { michael@0: #if defined (__Userspace_os_Windows) michael@0: Sleep(TIMEOUT_INTERVAL); michael@0: #else michael@0: struct timeval timeout; michael@0: michael@0: timeout.tv_sec = 0; michael@0: timeout.tv_usec = 1000 * TIMEOUT_INTERVAL; michael@0: select(0, NULL, NULL, NULL, &timeout); michael@0: #endif michael@0: if (SCTP_BASE_VAR(timer_thread_should_exit)) { michael@0: break; michael@0: } michael@0: sctp_handle_tick(MSEC_TO_TICKS(TIMEOUT_INTERVAL)); michael@0: } michael@0: return (NULL); michael@0: } michael@0: michael@0: void michael@0: sctp_start_timer(void) michael@0: { michael@0: /* michael@0: * No need to do SCTP_TIMERQ_LOCK_INIT(); michael@0: * here, it is being done in sctp_pcb_init() michael@0: */ michael@0: #if defined (__Userspace_os_Windows) michael@0: if ((SCTP_BASE_VAR(timer_thread) = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)user_sctp_timer_iterate, NULL, 0, NULL)) == NULL) { michael@0: SCTP_PRINTF("ERROR; Creating ithread failed\n"); michael@0: } michael@0: #else michael@0: int rc; michael@0: michael@0: rc = pthread_create(&SCTP_BASE_VAR(timer_thread), NULL, user_sctp_timer_iterate, NULL); michael@0: if (rc) { michael@0: SCTP_PRINTF("ERROR; return code from pthread_create() is %d\n", rc); michael@0: } michael@0: #endif michael@0: } michael@0: michael@0: #endif