Fri Aug 24 02:22:17 2007

Asterisk developer's documentation


sched.h

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  * \brief Scheduler Routines (derived from cheops)
00021  */
00022 
00023 #ifndef _ASTERISK_SCHED_H
00024 #define _ASTERISK_SCHED_H
00025 
00026 #if defined(__cplusplus) || defined(c_plusplus)
00027 extern "C" {
00028 #endif
00029 
00030 /*! \brief Max num of schedule structs
00031  * \note The max number of schedule structs to keep around
00032  * for use.  Undefine to disable schedule structure
00033  * caching. (Only disable this on very low memory
00034  * machines)
00035  */
00036 #define SCHED_MAX_CACHE 128
00037 
00038 struct sched_context;
00039 
00040 /*! \brief New schedule context
00041  * \note Create a scheduling context
00042  * \return Returns a malloc'd sched_context structure, NULL on failure
00043  */
00044 struct sched_context *sched_context_create(void);
00045 
00046 /*! \brief destroys a schedule context
00047  * Destroys (free's) the given sched_context structure
00048  * \param c Context to free
00049  * \return Returns 0 on success, -1 on failure
00050  */
00051 void sched_context_destroy(struct sched_context *c);
00052 
00053 /*! \brief callback for a cheops scheduler
00054  * A cheops scheduler callback takes a pointer with callback data and
00055  * \return returns a 0 if it should not be run again, or non-zero if it should be
00056  * rescheduled to run again
00057  */
00058 typedef int (*ast_sched_cb)(void *data);
00059 #define AST_SCHED_CB(a) ((ast_sched_cb)(a))
00060 
00061 /*! \brief Adds a scheduled event
00062  * Schedule an event to take place at some point in the future.  callback
00063  * will be called with data as the argument, when milliseconds into the
00064  * future (approximately)
00065  * If callback returns 0, no further events will be re-scheduled
00066  * \param con Scheduler context to add
00067  * \param when how many milliseconds to wait for event to occur
00068  * \param callback function to call when the amount of time expires
00069  * \param data data to pass to the callback
00070  * \return Returns a schedule item ID on success, -1 on failure
00071  */
00072 int ast_sched_add(struct sched_context *con, int when, ast_sched_cb callback, void *data);
00073 
00074 /*!Adds a scheduled event with rescheduling support
00075  * \param con Scheduler context to add
00076  * \param when how many milliseconds to wait for event to occur
00077  * \param callback function to call when the amount of time expires
00078  * \param data data to pass to the callback
00079  * \param variable If true, the result value of callback function will be
00080  *       used for rescheduling
00081  * Schedule an event to take place at some point in the future.  Callback
00082  * will be called with data as the argument, when milliseconds into the
00083  * future (approximately)
00084  * If callback returns 0, no further events will be re-scheduled
00085  * \return Returns a schedule item ID on success, -1 on failure
00086  */
00087 int ast_sched_add_variable(struct sched_context *con, int when, ast_sched_cb callback, void *data, int variable);
00088 
00089 /*! \brief Deletes a scheduled event
00090  * Remove this event from being run.  A procedure should not remove its
00091  * own event, but return 0 instead.
00092  * \param con scheduling context to delete item from
00093  * \param id ID of the scheduled item to delete
00094  * \return Returns 0 on success, -1 on failure
00095  */
00096 int ast_sched_del(struct sched_context *con, int id);
00097 
00098 /*! \brief Determines number of seconds until the next outstanding event to take place
00099  * Determine the number of seconds until the next outstanding event
00100  * should take place, and return the number of milliseconds until
00101  * it needs to be run.  This value is perfect for passing to the poll
00102  * call.
00103  * \param con context to act upon
00104  * \return Returns "-1" if there is nothing there are no scheduled events
00105  * (and thus the poll should not timeout)
00106  */
00107 int ast_sched_wait(struct sched_context *con);
00108 
00109 /*! \brief Runs the queue
00110  * \param con Scheduling context to run
00111  * Run the queue, executing all callbacks which need to be performed
00112  * at this time.
00113  * \param con context to act upon
00114  * \return Returns the number of events processed.
00115  */
00116 int ast_sched_runq(struct sched_context *con);
00117 
00118 /*! \brief Dumps the scheduler contents
00119  * Debugging: Dump the contents of the scheduler to stderr
00120  * \param con Context to dump
00121  */
00122 void ast_sched_dump(const struct sched_context *con);
00123 
00124 /*! \brief Returns the number of seconds before an event takes place
00125  * \param con Context to use
00126  * \param id Id to dump
00127  */
00128 long ast_sched_when(struct sched_context *con,int id);
00129 
00130 /*!
00131  * \brief Convenience macro for objects and reference (add)
00132  *
00133  */
00134 #define ast_sched_add_object(obj,con,when,callback) ast_sched_add((con),(when),(callback), ASTOBJ_REF((obj)))
00135 
00136 /*!
00137  * \brief Convenience macro for objects and reference (del)
00138  *
00139  */
00140 #define ast_sched_del_object(obj,destructor,con,id) do { \
00141    if ((id) > -1) { \
00142       ast_sched_del((con),(id)); \
00143       (id) = -1; \
00144       ASTOBJ_UNREF((obj),(destructor)); \
00145    } \
00146 } while(0)
00147 
00148 #if defined(__cplusplus) || defined(c_plusplus)
00149 }
00150 #endif
00151 
00152 #endif /* _ASTERISK_SCHED_H */

Generated on Fri Aug 24 02:22:17 2007 for Asterisk - the Open Source PBX by  doxygen 1.5.1