2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
20 * \brief Scheduler Routines (derived from cheops)
23 #ifndef _ASTERISK_SCHED_H
24 #define _ASTERISK_SCHED_H
26 #if defined(__cplusplus) || defined(c_plusplus)
30 /*! \brief Max num of schedule structs
31 * \note The max number of schedule structs to keep around
32 * for use. Undefine to disable schedule structure
33 * caching. (Only disable this on very low memory
36 #define SCHED_MAX_CACHE 128
38 #define AST_SCHED_DEL(sched, id) \
41 while (id > -1 && ast_sched_del(sched, id) && _count++ < 10) \
44 ast_log(LOG_WARNING, "Unable to cancel schedule ID %d. This is probably a bug (%s: %s, line %d).\n", id, __FILE__, __PRETTY_FUNCTION__, __LINE__); \
48 #define AST_SCHED_REPLACE_VARIABLE(id, sched, when, callback, data, variable) \
51 while (id > -1 && ast_sched_del(sched, id) && _count++ < 10) \
54 ast_log(LOG_WARNING, "Unable to cancel schedule ID %d. This is probably a bug (%s: %s, line %d).\n", id, __FILE__, __PRETTY_FUNCTION__, __LINE__); \
55 id = ast_sched_add_variable(sched, when, callback, data, variable); \
58 #define AST_SCHED_REPLACE(id, sched, when, callback, data) \
59 AST_SCHED_REPLACE_VARIABLE(id, sched, when, callback, data, 0)
63 /*! \brief New schedule context
64 * \note Create a scheduling context
65 * \return Returns a malloc'd sched_context structure, NULL on failure
67 struct sched_context *sched_context_create(void);
69 /*! \brief destroys a schedule context
70 * Destroys (free's) the given sched_context structure
71 * \param c Context to free
72 * \return Returns 0 on success, -1 on failure
74 void sched_context_destroy(struct sched_context *c);
76 /*! \brief callback for a cheops scheduler
77 * A cheops scheduler callback takes a pointer with callback data and
78 * \return returns a 0 if it should not be run again, or non-zero if it should be
79 * rescheduled to run again
81 typedef int (*ast_sched_cb)(const void *data);
82 #define AST_SCHED_CB(a) ((ast_sched_cb)(a))
84 /*! \brief Adds a scheduled event
85 * Schedule an event to take place at some point in the future. callback
86 * will be called with data as the argument, when milliseconds into the
87 * future (approximately)
88 * If callback returns 0, no further events will be re-scheduled
89 * \param con Scheduler context to add
90 * \param when how many milliseconds to wait for event to occur
91 * \param callback function to call when the amount of time expires
92 * \param data data to pass to the callback
93 * \return Returns a schedule item ID on success, -1 on failure
95 int ast_sched_add(struct sched_context *con, int when, ast_sched_cb callback, const void *data);
98 * \brief replace a scheduler entry
100 * This deletes the scheduler entry for old_id if it exists, and then
101 * calls ast_sched_add to create a new entry. A negative old_id will
105 * \retval otherwise, returns scheduled item ID
107 int ast_sched_replace(int old_id, struct sched_context *con, int when, ast_sched_cb callback, const void *data);
109 /*!Adds a scheduled event with rescheduling support
110 * \param con Scheduler context to add
111 * \param when how many milliseconds to wait for event to occur
112 * \param callback function to call when the amount of time expires
113 * \param data data to pass to the callback
114 * \param variable If true, the result value of callback function will be
115 * used for rescheduling
116 * Schedule an event to take place at some point in the future. Callback
117 * will be called with data as the argument, when milliseconds into the
118 * future (approximately)
119 * If callback returns 0, no further events will be re-scheduled
120 * \return Returns a schedule item ID on success, -1 on failure
122 int ast_sched_add_variable(struct sched_context *con, int when, ast_sched_cb callback, const void *data, int variable);
125 * \brief replace a scheduler entry
127 * This deletes the scheduler entry for old_id if it exists, and then
128 * calls ast_sched_add to create a new entry. A negative old_id will
132 * \retval otherwise, returns scheduled item ID
134 int ast_sched_replace_variable(int old_id, struct sched_context *con, int when, ast_sched_cb callback, const void *data, int variable);
136 /*! \brief Deletes a scheduled event
137 * Remove this event from being run. A procedure should not remove its
138 * own event, but return 0 instead.
139 * \param con scheduling context to delete item from
140 * \param id ID of the scheduled item to delete
141 * \return Returns 0 on success, -1 on failure
143 int ast_sched_del(struct sched_context *con, int id);
145 /*! \brief Determines number of seconds until the next outstanding event to take place
146 * Determine the number of seconds until the next outstanding event
147 * should take place, and return the number of milliseconds until
148 * it needs to be run. This value is perfect for passing to the poll
150 * \param con context to act upon
151 * \return Returns "-1" if there is nothing there are no scheduled events
152 * (and thus the poll should not timeout)
154 int ast_sched_wait(struct sched_context *con);
156 /*! \brief Runs the queue
157 * \param con Scheduling context to run
158 * Run the queue, executing all callbacks which need to be performed
160 * \param con context to act upon
161 * \return Returns the number of events processed.
163 int ast_sched_runq(struct sched_context *con);
165 /*! \brief Dumps the scheduler contents
166 * Debugging: Dump the contents of the scheduler to stderr
167 * \param con Context to dump
169 void ast_sched_dump(const struct sched_context *con);
171 /*! \brief Returns the number of seconds before an event takes place
172 * \param con Context to use
173 * \param id Id to dump
175 long ast_sched_when(struct sched_context *con,int id);
178 * \brief Convenience macro for objects and reference (add)
181 #define ast_sched_add_object(obj,con,when,callback) ast_sched_add((con),(when),(callback), ASTOBJ_REF((obj)))
184 * \brief Convenience macro for objects and reference (del)
187 #define ast_sched_del_object(obj,destructor,con,id) do { \
189 ast_sched_del((con),(id)); \
191 ASTOBJ_UNREF((obj),(destructor)); \
195 #if defined(__cplusplus) || defined(c_plusplus)
199 #endif /* _ASTERISK_SCHED_H */