Asterisk - The Open Source Telephony Project  GIT-master-a1fa8df
sched.h
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2010, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  * Russell Bryant <russell@digium.com>
8  *
9  * See http://www.asterisk.org for more information about
10  * the Asterisk project. Please do not directly contact
11  * any of the maintainers of this project for assistance;
12  * the project provides a web site, mailing lists and IRC
13  * channels for your use.
14  *
15  * This program is free software, distributed under the terms of
16  * the GNU General Public License Version 2. See the LICENSE file
17  * at the top of the source tree.
18  */
19 
20 /*! \file
21  * \brief Scheduler Routines (derived from cheops)
22  */
23 
24 #ifndef _ASTERISK_SCHED_H
25 #define _ASTERISK_SCHED_H
26 
27 #if defined(__cplusplus) || defined(c_plusplus)
28 extern "C" {
29 #endif
30 
31 /*!
32  * \brief Remove a scheduler entry
33  *
34  * This is a loop construct to ensure that
35  * the scheduled task get deleted. The idea is that
36  * if we loop attempting to remove the scheduled task,
37  * then whatever callback had been running will complete
38  * and reinsert the task into the scheduler.
39  *
40  * Since macro expansion essentially works like pass-by-name
41  * parameter passing, this macro will still work correctly even
42  * if the id of the task to delete changes. This holds as long as
43  * the name of the id which could change is passed to the macro
44  * and not a copy of the value of the id.
45  */
46 #define AST_SCHED_DEL(sched, id) \
47  ({ \
48  int _count = 0; \
49  int _sched_res = -1; \
50  while (id > -1 && (_sched_res = ast_sched_del(sched, id)) && ++_count < 10) \
51  usleep(1); \
52  if (_count == 10) { \
53  ast_debug(3, "Unable to cancel schedule ID %d.\n", id); \
54  } \
55  id = -1; \
56  (_sched_res); \
57  })
58 
59 #define AST_SCHED_DEL_ACCESSOR(sched, obj, getter, setter) \
60  ({ \
61  int _count = 0; \
62  int _sched_res = -1; \
63  while (getter(obj) > -1 && (_sched_res = ast_sched_del(sched, getter(obj))) && ++_count < 10) \
64  usleep(1); \
65  if (_count == 10) { \
66  ast_debug(3, "Unable to cancel schedule ID %d.\n", getter(obj)); \
67  } \
68  setter(obj, -1); \
69  (_sched_res); \
70  })
71 
72 /*!
73  * \brief schedule task to get deleted and call unref function
74  *
75  * Only calls unref function if the delete succeeded.
76  *
77  * \sa AST_SCHED_DEL
78  * \since 1.6.1
79  */
80 #define AST_SCHED_DEL_UNREF(sched, id, refcall) \
81  do { \
82  int _count = 0, _id; \
83  while ((_id = id) > -1 && ast_sched_del(sched, _id) && ++_count < 10) { \
84  usleep(1); \
85  } \
86  if (_count == 10) { \
87  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__); \
88  } else if (_id > -1) { \
89  refcall; \
90  id = -1; \
91  } \
92  } while (0);
93 
94 /*!
95  * \brief schedule task to get deleted releasing the lock between attempts
96  * \since 1.6.1
97  */
98 #define AST_SCHED_DEL_SPINLOCK(sched, id, lock) \
99  ({ \
100  int _count = 0; \
101  int _sched_res = -1; \
102  while (id > -1 && (_sched_res = ast_sched_del(sched, id)) && ++_count < 10) { \
103  ast_mutex_unlock(lock); \
104  usleep(1); \
105  ast_mutex_lock(lock); \
106  } \
107  if (_count == 10) { \
108  ast_debug(3, "Unable to cancel schedule ID %d.\n", id); \
109  } \
110  id = -1; \
111  (_sched_res); \
112  })
113 
114 #define AST_SCHED_REPLACE_VARIABLE(id, sched, when, callback, data, variable) \
115  do { \
116  int _count = 0; \
117  while (id > -1 && ast_sched_del(sched, id) && ++_count < 10) { \
118  usleep(1); \
119  } \
120  if (_count == 10) \
121  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__); \
122  id = ast_sched_add_variable(sched, when, callback, data, variable); \
123  } while (0);
124 
125 #define AST_SCHED_REPLACE(id, sched, when, callback, data) \
126  AST_SCHED_REPLACE_VARIABLE(id, sched, when, callback, data, 0)
127 
128 /*!
129  * \note Not currently used in the source?
130  * \since 1.6.1
131  */
132 #define AST_SCHED_REPLACE_VARIABLE_UNREF(id, sched, when, callback, data, variable, unrefcall, addfailcall, refcall) \
133  do { \
134  int _count = 0, _res=1; \
135  void *_data = (void *)ast_sched_find_data(sched, id); \
136  while (id > -1 && (_res = ast_sched_del(sched, id) && _count++ < 10)) { \
137  usleep(1); \
138  } \
139  if (!_res && _data && _data != data) \
140  unrefcall; /* should ref _data! */ \
141  if (_count == 10) \
142  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__); \
143  if (_data != data) \
144  refcall; \
145  id = ast_sched_add_variable(sched, when, callback, data, variable); \
146  if (id == -1) \
147  addfailcall; \
148  } while (0);
149 
150 #define AST_SCHED_REPLACE_UNREF(id, sched, when, callback, data, unrefcall, addfailcall, refcall) \
151  AST_SCHED_REPLACE_VARIABLE_UNREF(id, sched, when, callback, data, 0, unrefcall, addfailcall, refcall)
152 
153 /*!
154  * \brief Create a scheduler context
155  *
156  * \return Returns a malloc'd sched_context structure, NULL on failure
157  */
159 
160 /*!
161  * \brief destroys a schedule context
162  *
163  * \param c Context to free
164  */
166 
167 /*!
168  * \brief scheduler callback
169  *
170  * A scheduler callback takes a pointer with callback data and
171  *
172  * \retval 0 if the callback should not be rescheduled
173  * \retval non-zero if the callback should be scheduled again
174  */
175 typedef int (*ast_sched_cb)(const void *data);
176 #define AST_SCHED_CB(a) ((ast_sched_cb)(a))
177 
178 /*!
179  * \brief Clean all scheduled events with matching callback.
180  *
181  * \param con Scheduler Context
182  * \param match Callback to match
183  * \param cleanup_cb Callback to run
184  *
185  * \note The return of cleanup_cb is ignored. No events are rescheduled.
186  */
188 
189 struct ast_cb_names {
191  char *list[10];
193 };
194 
195 /*!
196  * \brief Show statics on what it is in the schedule queue
197  * \param con Schedule context to check
198  * \param buf dynamic string to store report
199  * \param cbnames to check against
200  * \since 1.6.1
201  */
202 void ast_sched_report(struct ast_sched_context *con, struct ast_str **buf, struct ast_cb_names *cbnames);
203 
204 /*!
205  * \brief Adds a scheduled event
206  *
207  * Schedule an event to take place at some point in the future. callback
208  * will be called with data as the argument, when milliseconds into the
209  * future (approximately)
210  *
211  * If callback returns 0, no further events will be re-scheduled
212  *
213  * \param con Scheduler context to add
214  * \param when how many milliseconds to wait for event to occur
215  * \param callback function to call when the amount of time expires
216  * \param data data to pass to the callback
217  *
218  * \return Returns a schedule item ID on success, -1 on failure
219  */
220 int ast_sched_add(struct ast_sched_context *con, int when, ast_sched_cb callback, const void *data) attribute_warn_unused_result;
221 
222 /*!
223  * \brief replace a scheduler entry
224  * \deprecated You should use the AST_SCHED_REPLACE() macro instead.
225  *
226  * This deletes the scheduler entry for old_id if it exists, and then
227  * calls ast_sched_add to create a new entry. A negative old_id will
228  * be ignored.
229  *
230  * \retval -1 failure
231  * \retval otherwise, returns scheduled item ID
232  */
233 int ast_sched_replace(int old_id, struct ast_sched_context *con, int when, ast_sched_cb callback, const void *data) attribute_warn_unused_result;
234 
235 /*!
236  * \brief Adds a scheduled event with rescheduling support
237  *
238  * \param con Scheduler context to add
239  * \param when how many milliseconds to wait for event to occur
240  * \param callback function to call when the amount of time expires
241  * \param data data to pass to the callback
242  * \param variable If true, the result value of callback function will be
243  * used for rescheduling
244  *
245  * Schedule an event to take place at some point in the future. Callback
246  * will be called with data as the argument, when milliseconds into the
247  * future (approximately)
248  *
249  * If callback returns 0, no further events will be re-scheduled
250  *
251  * \return Returns a schedule item ID on success, -1 on failure
252  */
253 int ast_sched_add_variable(struct ast_sched_context *con, int when, ast_sched_cb callback, const void *data, int variable) attribute_warn_unused_result;
254 
255 /*!
256  * \brief replace a scheduler entry
257  * \deprecated You should use the AST_SCHED_REPLACE_VARIABLE() macro instead.
258  *
259  * This deletes the scheduler entry for old_id if it exists, and then
260  * calls ast_sched_add to create a new entry. A negative old_id will
261  * be ignored.
262  *
263  * \retval -1 failure
264  * \retval otherwise, returns scheduled item ID
265  */
266 int ast_sched_replace_variable(int old_id, struct ast_sched_context *con, int when, ast_sched_cb callback, const void *data, int variable) attribute_warn_unused_result;
267 
268 /*!
269  * \brief Find a sched structure and return the data field associated with it.
270  *
271  * \param con scheduling context in which to search fro the matching id
272  * \param id ID of the scheduled item to find
273  * \return the data field from the matching sched struct if found; else return NULL if not found.
274  *
275  * \since 1.6.1
276  */
277 const void *ast_sched_find_data(struct ast_sched_context *con, int id);
278 
279 /*!
280  * \brief Deletes a scheduled event
281  *
282  * Remove this event from being run. A procedure should not remove its own
283  * event, but return 0 instead. In most cases, you should not call this
284  * routine directly, but use the AST_SCHED_DEL() macro instead (especially if
285  * you don't intend to do something different when it returns failure).
286  *
287  * \param con scheduling context to delete item from
288  * \param id ID of the scheduled item to delete
289  *
290  * \return Returns 0 on success, -1 on failure
291  */
293 
294 /*!
295  * \brief Determines number of seconds until the next outstanding event to take place
296  *
297  * Determine the number of seconds until the next outstanding event
298  * should take place, and return the number of milliseconds until
299  * it needs to be run. This value is perfect for passing to the poll
300  * call.
301  *
302  * \param con context to act upon
303  *
304  * \return Returns "-1" if there is nothing there are no scheduled events
305  * (and thus the poll should not timeout)
306  */
308 
309 /*!
310  * \brief Runs the queue
311  *
312  * Run the queue, executing all callbacks which need to be performed
313  * at this time.
314  *
315  * \param con Scheduling context to run
316  * \param con context to act upon
317  *
318  * \return Returns the number of events processed.
319  */
320 int ast_sched_runq(struct ast_sched_context *con);
321 
322 /*!
323  * \brief Dumps the scheduler contents
324  *
325  * Debugging: Dump the contents of the scheduler to stderr
326  *
327  * \param con Context to dump
328  */
329 void ast_sched_dump(struct ast_sched_context *con);
330 
331 /*!
332  * \brief Returns the number of seconds before an event takes place
333  *
334  * \param con Context to use
335  * \param id Id to dump
336  */
337 long ast_sched_when(struct ast_sched_context *con,int id);
338 
339 /*!
340  * \brief Convenience macro for objects and reference (add)
341  *
342  */
343 #define ast_sched_add_object(obj,con,when,callback) ast_sched_add((con),(when),(callback), ASTOBJ_REF((obj)))
344 
345 /*!
346  * \brief Convenience macro for objects and reference (del)
347  *
348  */
349 #define ast_sched_del_object(obj,destructor,con,id) do { \
350  if ((id) > -1) { \
351  ast_sched_del((con),(id)); \
352  (id) = -1; \
353  ASTOBJ_UNREF((obj),(destructor)); \
354  } \
355 } while(0)
356 
357 /*!
358  * \brief Start a thread for processing scheduler entries
359  *
360  * \param con the scheduler context this thread will manage
361  *
362  * \retval 0 success
363  * \retval non-zero failure
364  */
366 
367 #if defined(__cplusplus) || defined(c_plusplus)
368 }
369 #endif
370 
371 #endif /* _ASTERISK_SCHED_H */
char * list[10]
Definition: sched.h:191
int ast_sched_start_thread(struct ast_sched_context *con)
Start a thread for processing scheduler entries.
Definition: sched.c:195
void ast_sched_clean_by_callback(struct ast_sched_context *con, ast_sched_cb match, ast_sched_cb cleanup_cb)
Clean all scheduled events with matching callback.
Definition: sched.c:407
int ast_sched_runq(struct ast_sched_context *con)
Runs the queue.
Definition: sched.c:755
ast_sched_cb cblist[10]
Definition: sched.h:192
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
int ast_sched_add_variable(struct ast_sched_context *con, int when, ast_sched_cb callback, const void *data, int variable) attribute_warn_unused_result
Adds a scheduled event with rescheduling support.
Definition: sched.c:524
static struct test_val c
static int match(struct ast_sockaddr *addr, unsigned short callno, unsigned short dcallno, const struct chan_iax2_pvt *cur, int check_dcallno)
Definition: chan_iax2.c:2316
static int cleanup_cb(void *obj, void *arg, int flags)
Definition: res_stasis.c:309
int numassocs
Definition: sched.h:190
struct ast_sched_context * ast_sched_context_create(void)
Create a scheduler context.
Definition: sched.c:236
void ast_sched_report(struct ast_sched_context *con, struct ast_str **buf, struct ast_cb_names *cbnames)
Show statics on what it is in the schedule queue.
Definition: sched.c:674
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:584
long ast_sched_when(struct ast_sched_context *con, int id)
Returns the number of seconds before an event takes place.
Definition: sched.c:814
#define attribute_warn_unused_result
Definition: compiler.h:71
int ast_sched_del(struct ast_sched_context *con, int id) attribute_warn_unused_result
Deletes a scheduled event.
Definition: sched.c:610
const void * ast_sched_find_data(struct ast_sched_context *con, int id)
Find a sched structure and return the data field associated with it.
Definition: sched.c:587
int(* ast_sched_cb)(const void *data)
scheduler callback
Definition: sched.h:175
int ast_sched_add(struct ast_sched_context *con, int when, ast_sched_cb callback, const void *data) attribute_warn_unused_result
Adds a scheduled event.
Definition: sched.c:565
int ast_sched_replace_variable(int old_id, struct ast_sched_context *con, int when, ast_sched_cb callback, const void *data, int variable) attribute_warn_unused_result
replace a scheduler entry
Definition: sched.c:512
int ast_sched_replace(int old_id, struct ast_sched_context *con, int when, ast_sched_cb callback, const void *data) attribute_warn_unused_result
replace a scheduler entry
Definition: sched.c:557
void ast_sched_dump(struct ast_sched_context *con)
Dumps the scheduler contents.
Definition: sched.c:712
int ast_sched_wait(struct ast_sched_context *con) attribute_warn_unused_result
Determines number of seconds until the next outstanding event to take place.
Definition: sched.c:431
void ast_sched_context_destroy(struct ast_sched_context *c)
destroys a schedule context
Definition: sched.c:269