Merge changes from team/russell/issue_9520
[asterisk-bristuff.git] / include / asterisk / sched.h
blob829a1b0f6dfff70499fd604412267e52192b6bcc
1 /*
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.
19 /*! \file
20 * \brief Scheduler Routines (derived from cheops)
23 #ifndef _ASTERISK_SCHED_H
24 #define _ASTERISK_SCHED_H
26 #if defined(__cplusplus) || defined(c_plusplus)
27 extern "C" {
28 #endif
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
34 * machines)
36 #define SCHED_MAX_CACHE 128
38 /*! \brief a loop construct to ensure that
39 * the scheduled task get deleted. The idea is that
40 * if we loop attempting to remove the scheduled task,
41 * then whatever callback had been running will complete
42 * and reinsert the task into the scheduler.
44 * Note that this is NOT always appropriate. This should
45 * only be used for tasks whose callback may return non-zero
46 * to indicate that the task needs to be rescheduled with the
47 * SAME id as previously.
49 * Some scheduler callbacks instead may reschedule the task themselves,
50 * thus removing the previous task id from the queue. If the task is rescheduled
51 * in this manner, then the id for the task will be different than before
52 * and so it makes no sense to use this macro. Note that if using the scheduler
53 * in this manner, it is perfectly acceptable for ast_sched_del to fail, and this
54 * macro should NOT be used.
56 #define AST_SCHED_DEL(sched, id) \
57 ({ \
58 int _count = 0; \
59 int _sched_res = -1; \
60 while (id > -1 && (_sched_res = ast_sched_del(sched, id)) && ++_count < 10) \
61 usleep(1); \
62 if (_count == 10 && option_debug > 2) { \
63 ast_log(LOG_DEBUG, "Unable to cancel schedule ID %d.\n", id); \
64 } \
65 id = -1; \
66 (_sched_res); \
69 struct sched_context;
71 /*! \brief New schedule context
72 * \note Create a scheduling context
73 * \return Returns a malloc'd sched_context structure, NULL on failure
75 struct sched_context *sched_context_create(void);
77 /*! \brief destroys a schedule context
78 * Destroys (free's) the given sched_context structure
79 * \param c Context to free
80 * \return Returns 0 on success, -1 on failure
82 void sched_context_destroy(struct sched_context *c);
84 /*! \brief callback for a cheops scheduler
85 * A cheops scheduler callback takes a pointer with callback data and
86 * \return returns a 0 if it should not be run again, or non-zero if it should be
87 * rescheduled to run again
89 typedef int (*ast_sched_cb)(const void *data);
90 #define AST_SCHED_CB(a) ((ast_sched_cb)(a))
92 /*! \brief Adds a scheduled event
93 * Schedule an event to take place at some point in the future. callback
94 * will be called with data as the argument, when milliseconds into the
95 * future (approximately)
96 * If callback returns 0, no further events will be re-scheduled
97 * \param con Scheduler context to add
98 * \param when how many milliseconds to wait for event to occur
99 * \param callback function to call when the amount of time expires
100 * \param data data to pass to the callback
101 * \return Returns a schedule item ID on success, -1 on failure
103 int ast_sched_add(struct sched_context *con, int when, ast_sched_cb callback, const void *data);
105 /*!Adds a scheduled event with rescheduling support
106 * \param con Scheduler context to add
107 * \param when how many milliseconds to wait for event to occur
108 * \param callback function to call when the amount of time expires
109 * \param data data to pass to the callback
110 * \param variable If true, the result value of callback function will be
111 * used for rescheduling
112 * Schedule an event to take place at some point in the future. Callback
113 * will be called with data as the argument, when milliseconds into the
114 * future (approximately)
115 * If callback returns 0, no further events will be re-scheduled
116 * \return Returns a schedule item ID on success, -1 on failure
118 int ast_sched_add_variable(struct sched_context *con, int when, ast_sched_cb callback, const void *data, int variable);
120 /*! \brief Deletes a scheduled event
121 * Remove this event from being run. A procedure should not remove its
122 * own event, but return 0 instead.
123 * \param con scheduling context to delete item from
124 * \param id ID of the scheduled item to delete
125 * \return Returns 0 on success, -1 on failure
127 int ast_sched_del(struct sched_context *con, int id);
129 /*! \brief Determines number of seconds until the next outstanding event to take place
130 * Determine the number of seconds until the next outstanding event
131 * should take place, and return the number of milliseconds until
132 * it needs to be run. This value is perfect for passing to the poll
133 * call.
134 * \param con context to act upon
135 * \return Returns "-1" if there is nothing there are no scheduled events
136 * (and thus the poll should not timeout)
138 int ast_sched_wait(struct sched_context *con);
140 /*! \brief Runs the queue
141 * \param con Scheduling context to run
142 * Run the queue, executing all callbacks which need to be performed
143 * at this time.
144 * \param con context to act upon
145 * \return Returns the number of events processed.
147 int ast_sched_runq(struct sched_context *con);
149 /*! \brief Dumps the scheduler contents
150 * Debugging: Dump the contents of the scheduler to stderr
151 * \param con Context to dump
153 void ast_sched_dump(const struct sched_context *con);
155 /*! \brief Returns the number of seconds before an event takes place
156 * \param con Context to use
157 * \param id Id to dump
159 long ast_sched_when(struct sched_context *con,int id);
162 * \brief Convenience macro for objects and reference (add)
165 #define ast_sched_add_object(obj,con,when,callback) ast_sched_add((con),(when),(callback), ASTOBJ_REF((obj)))
168 * \brief Convenience macro for objects and reference (del)
171 #define ast_sched_del_object(obj,destructor,con,id) do { \
172 if ((id) > -1) { \
173 ast_sched_del((con),(id)); \
174 (id) = -1; \
175 ASTOBJ_UNREF((obj),(destructor)); \
177 } while(0)
179 #if defined(__cplusplus) || defined(c_plusplus)
181 #endif
183 #endif /* _ASTERISK_SCHED_H */