1 /*- 2 * Copyright (c) 2012 Michael Tuexen 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 */ 27 28 #include <sys/types.h> 29 #if !defined (__Userspace_os_Windows) 30 #include <sys/wait.h> 31 #include <unistd.h> 32 #include <pthread.h> 33 #endif 34 #include <stdlib.h> 35 #include <string.h> 36 #include <stdio.h> 37 #include <errno.h> 38 #include <netinet/sctp_pcb.h> 39 #include <netinet/sctp_sysctl.h> 40 #include "netinet/sctp_callout.h" 41 42 /* This is the polling time of callqueue in milliseconds 43 * 10ms seems to work well. 1ms was giving erratic behavior 44 */ 45 #define TIMEOUT_INTERVAL 10 46 47 extern int ticks; 48 49 void * 50 user_sctp_timer_iterate(void *arg) 51 { 52 sctp_os_timer_t *c; 53 void (*c_func)(void *); 54 void *c_arg; 55 sctp_os_timer_t *sctp_os_timer_next; 56 /* 57 * The MSEC_TO_TICKS conversion depends on hz. The to_ticks in 58 * sctp_os_timer_start also depends on hz. E.g. if hz=1000 then 59 * for multiple INIT the to_ticks is 2000, 4000, 8000, 16000, 32000, 60000 60 * and further to_ticks level off at 60000 i.e. 60 seconds. 61 * If hz=100 then for multiple INIT the to_ticks are 200, 400, 800 and so-on. 62 */ 63 for (;;) { 64 #if defined (__Userspace_os_Windows) 65 Sleep(TIMEOUT_INTERVAL); 66 #else 67 struct timeval timeout; 68 69 timeout.tv_sec = 0; 70 timeout.tv_usec = 1000 * TIMEOUT_INTERVAL; 71 select(0, NULL, NULL, NULL, &timeout); 72 #endif 73 if (SCTP_BASE_VAR(timer_thread_should_exit)) { 74 break; 75 } 76 SCTP_TIMERQ_LOCK(); 77 /* update our tick count */ 78 ticks += MSEC_TO_TICKS(TIMEOUT_INTERVAL); 79 c = TAILQ_FIRST(&SCTP_BASE_INFO(callqueue)); 80 while (c) { 81 if (c->c_time <= ticks) { 82 sctp_os_timer_next = TAILQ_NEXT(c, tqe); 83 TAILQ_REMOVE(&SCTP_BASE_INFO(callqueue), c, tqe); 84 c_func = c->c_func; 85 c_arg = c->c_arg; 86 c->c_flags &= ~SCTP_CALLOUT_PENDING; 87 SCTP_TIMERQ_UNLOCK(); 88 c_func(c_arg); 89 SCTP_TIMERQ_LOCK(); 90 c = sctp_os_timer_next; 91 } else { 92 c = TAILQ_NEXT(c, tqe); 93 } 94 } 95 SCTP_TIMERQ_UNLOCK(); 96 } 97 return (NULL); 98 } 99 100 void 101 sctp_start_timer(void) 102 { 103 /* 104 * No need to do SCTP_TIMERQ_LOCK_INIT(); 105 * here, it is being done in sctp_pcb_init() 106 */ 107 #if defined (__Userspace_os_Windows) 108 if ((SCTP_BASE_VAR(timer_thread) = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)user_sctp_timer_iterate, NULL, 0, NULL)) == NULL) { 109 SCTP_PRINTF("ERROR; Creating ithread failed\n"); 110 } 111 #else 112 int rc; 113 114 rc = pthread_create(&SCTP_BASE_VAR(timer_thread), NULL, user_sctp_timer_iterate, NULL); 115 if (rc) { 116 SCTP_PRINTF("ERROR; return code from pthread_create() is %d\n", rc); 117 } 118 #endif 119 } 120