/* * CallWeaver -- An open source telephony toolkit. * * Copyright (C) 1999 - 2005, Digium, Inc. * * Mark Spencer * * See http://www.callweaver.org for more information about * the CallWeaver project. Please do not directly contact * any of the maintainers of this project for assistance; * the project provides a web site, mailing lists and IRC * channels for your use. * * This program is free software, distributed under the terms of * the GNU General Public License Version 2. See the LICENSE file * at the top of the source tree. */ /*! \file * \brief Scheduler Routines (derived from cheops) */ #ifndef _CALLWEAVER_SCHED_H #define _CALLWEAVER_SCHED_H #if defined(__cplusplus) || defined(c_plusplus) extern "C" { #endif /*! Max num of schedule structs */ /*! * The max number of schedule structs to keep around * for use. Undefine to disable schedule structure * caching. (Only disable this on very low memory * machines) */ #define SCHED_MAX_CACHE 128 struct sched_context; /*! New schedule context */ /* ! * \param nthreads number of service threads to start * Create a scheduling context * Returns a malloc'd sched_context structure, NULL on failure */ extern CW_API_PUBLIC struct sched_context *sched_context_create(int nthreads); /*! destroys a schedule context */ /*! * \param c Context to free * Destroys (free's) the given sched_context structure * Returns 0 on success, -1 on failure */ extern CW_API_PUBLIC void sched_context_destroy(struct sched_context *c); /*! callback for a cheops scheduler */ /*! * A cheops scheduler callback takes a pointer with callback data and * returns a 0 if it should not be run again, or non-zero if it should be * rescheduled to run again */ typedef int (*cw_sched_cb)(void *data); #define CW_SCHED_CB(a) ((cw_sched_cb)(a)) /*!Adds a scheduled event */ /*! * \param con scheduler context to add * \param when how many milliseconds to wait for event to occur * \param callback function to call when the amount of time expires * \param data data to pass to the callback * \param variable If true, the result value of callback function will be * used for rescheduling * Schedule an event to take place at some point in the future. callback * will be called with data as the argument, when milliseconds into the * future (approximately) * If callback returns 0, no further events will be re-scheduled * Returns a schedule item ID on success, -1 on failure */ extern CW_API_PUBLIC int cw_sched_add_variable(struct sched_context *con, int when, cw_sched_cb callback, void *data, int variable); #define cw_sched_add(con, when, callback, data) cw_sched_add_variable(con, when, callback, data, 0) /*! Deletes a scheduled event */ /*! * \param con scheduling context to delete item from * \param id ID of the scheduled item to delete * Remove this event from being run. A procedure should not remove its * own event, but return 0 instead. * Returns 0 on success, -1 on failure */ extern CW_API_PUBLIC int cw_sched_del(struct sched_context *con, int id); /*! Atomically modifies a scheduled event or adds it if the ID did not exist. * \param con,id See cw_sched_del * \param ... See cw_sched_add * Returns a new schedule item ID on success, -1 on failure */ extern CW_API_PUBLIC int cw_sched_modify_variable(struct sched_context *con, int id, int when, cw_sched_cb callback, void *data, int variable); #define cw_sched_modify(con, id, when, callback, data) cw_sched_modify_variable(con, id, when, callback, data, 0) /*!Returns the number of seconds before an event takes place */ /*! * \param con Context to use * \param id Id to dump */ extern CW_API_PUBLIC long cw_sched_when(struct sched_context *con,int id); #if defined(__cplusplus) || defined(c_plusplus) } #endif #endif /* _CALLWEAVER_SCHED_H */