1 /* 2 * Copyright (c) 2010, Sangoma Technologies 3 * Moises Silva <moy@sangoma.com> 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 * * Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 13 * * 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 * 17 * * Neither the name of the original author; nor the names of any contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 26 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 27 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 28 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 29 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 31 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #ifndef __FTDM_SCHED_H__ 36 #define __FTDM_SCHED_H__ 37 38 #include "freetdm.h" 39 40 #ifdef __cplusplus 41 extern "C" { 42 #endif 43 44 #define FTDM_MICROSECONDS_PER_SECOND 1000000 45 46 typedef struct ftdm_sched ftdm_sched_t; 47 typedef void (*ftdm_sched_callback_t)(void *data); 48 typedef uint64_t ftdm_timer_id_t; 49 50 /*! \brief Create a new scheduling context */ 51 FT_DECLARE(ftdm_status_t) ftdm_sched_create(ftdm_sched_t **sched, const char *name); 52 53 /*! \brief Run the schedule to find timers that are expired and run its callbacks */ 54 FT_DECLARE(ftdm_status_t) ftdm_sched_run(ftdm_sched_t *sched); 55 56 /*! \brief Run the schedule in its own thread. Callbacks will be called in a core thread. You *must* not block there! */ 57 FT_DECLARE(ftdm_status_t) ftdm_sched_free_run(ftdm_sched_t *sched); 58 59 /*! 60 * \brief Schedule a new timer 61 * \param sched The scheduling context (required) 62 * \param name Timer name, typically unique but is not required to be unique, any null terminated string is fine (required) 63 * \param callback The callback to call upon timer expiration (required) 64 * \param data Optional data to pass to the callback 65 * \param timer Timer id pointer to store the id of the newly created timer. It can be null 66 * if you do not need to know the id, but you need this if you want to be able 67 * to cancel the timer with ftdm_sched_cancel_timer 68 */ 69 FT_DECLARE(ftdm_status_t) ftdm_sched_timer(ftdm_sched_t *sched, const char *name, 70 int ms, ftdm_sched_callback_t callback, void *data, ftdm_timer_id_t *timer); 71 72 /*! 73 * \brief Cancel the timer 74 * Note that there is a race between cancelling and triggering a timer. 75 * By the time you call this function the timer may be about to be triggered. 76 * This is specially true with timers in free run schedule. 77 * \param sched The scheduling context (required) 78 * \param timer The timer to cancel (required) 79 */ 80 FT_DECLARE(ftdm_status_t) ftdm_sched_cancel_timer(ftdm_sched_t *sched, ftdm_timer_id_t timer); 81 82 /*! \brief Destroy the context and all of the scheduled timers in it */ 83 FT_DECLARE(ftdm_status_t) ftdm_sched_destroy(ftdm_sched_t **sched); 84 85 /*! 86 * \brief Calculate the time to the next timer and return it 87 * \param sched The sched context 88 * \param timeto The pointer to store the next timer time in milliseconds 89 */ 90 FT_DECLARE(ftdm_status_t) ftdm_sched_get_time_to_next_timer(const ftdm_sched_t *sched, int32_t *timeto); 91 92 /*! \brief Global initialization, called just once, this is called by FreeTDM core, other users MUST not call it */ 93 FT_DECLARE(ftdm_status_t) ftdm_sched_global_init(void); 94 95 /*! \brief Checks if the main scheduling thread is running */ 96 FT_DECLARE(ftdm_bool_t) ftdm_free_sched_running(void); 97 98 /*! \brief Stop the main scheduling thread (if running) */ 99 FT_DECLARE(ftdm_bool_t) ftdm_free_sched_stop(void); 100 101 #ifdef __cplusplus 102 } 103 #endif 104 105 #endif 106 107 /* For Emacs: 108 * Local Variables: 109 * mode:c 110 * indent-tabs-mode:t 111 * tab-width:4 112 * c-basic-offset:4 113 * End: 114 * For VIM: 115 * vim:set softtabstop=4 shiftwidth=4 tabstop=4: 116 */