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 /*! Max num of schedule structs */
32 * The max number of schedule structs to keep around
33 * for use. Undefine to disable schedule structure
34 * caching. (Only disable this on very low memory
37 #define SCHED_MAX_CACHE 128
41 /*! New schedule context */
43 * Create a scheduling context
44 * Returns a malloc'd sched_context structure, NULL on failure
46 extern struct sched_context *sched_context_create(void);
48 /*! destroys a schedule context */
50 * \param c Context to free
51 * Destroys (free's) the given sched_context structure
52 * Returns 0 on success, -1 on failure
54 void sched_context_destroy(struct sched_context *c);
56 /*! callback for a cheops scheduler */
58 * A cheops scheduler callback takes a pointer with callback data and
59 * returns a 0 if it should not be run again, or non-zero if it should be
60 * rescheduled to run again
62 typedef int (*ast_sched_cb)(void *data);
63 #define AST_SCHED_CB(a) ((ast_sched_cb)(a))
65 /*!Adds a scheduled event */
67 * \param con Schduler context to add
68 * \param when how many milliseconds to wait for event to occur
69 * \param callback function to call when the amount of time expires
70 * \param data data to pass to the callback
71 * Schedule an event to take place at some point in the future. callback
72 * will be called with data as the argument, when milliseconds into the
73 * future (approximately)
74 * If callback returns 0, no further events will be re-scheduled
75 * Returns a schedule item ID on success, -1 on failure
77 extern int ast_sched_add(struct sched_context *con, int when, ast_sched_cb callback, void *data);
79 /*!Adds a scheduled event */
81 * \param con Schduler context to add
82 * \param when how many milliseconds to wait for event to occur
83 * \param callback function to call when the amount of time expires
84 * \param data data to pass to the callback
85 * \param variable If true, the result value of callback function will be
86 * used for rescheduling
87 * Schedule an event to take place at some point in the future. callback
88 * will be called with data as the argument, when milliseconds into the
89 * future (approximately)
90 * If callback returns 0, no further events will be re-scheduled
91 * Returns a schedule item ID on success, -1 on failure
93 extern int ast_sched_add_variable(struct sched_context *con, int when, ast_sched_cb callback, void *data, int variable);
95 /*! Deletes a scheduled event */
97 * \param con scheduling context to delete item from
98 * \param id ID of the scheduled item to delete
99 * Remove this event from being run. A procedure should not remove its
100 * own event, but return 0 instead.
101 * Returns 0 on success, -1 on failure
103 extern int ast_sched_del(struct sched_context *con, int id);
105 /*! Determines number of seconds until the next outstanding event to take place */
107 * \param con context to act upon
108 * Determine the number of seconds until the next outstanding event
109 * should take place, and return the number of milliseconds until
110 * it needs to be run. This value is perfect for passing to the poll
111 * call. Returns "-1" if there is nothing there are no scheduled events
112 * (and thus the poll should not timeout)
114 extern int ast_sched_wait(struct sched_context *con);
116 /*! Runs the queue */
118 * \param con Scheduling context to run
119 * Run the queue, executing all callbacks which need to be performed
120 * at this time. Returns the number of events processed.
122 extern int ast_sched_runq(struct sched_context *con);
124 /*!Dumps the scheduler contents */
126 * \param con Context to dump
127 * Debugging: Dump the contents of the scheduler to stderr
129 extern void ast_sched_dump(const struct sched_context *con);
131 /*!Returns the number of seconds before an event takes place */
133 * \param con Context to use
134 * \param id Id to dump
136 extern long ast_sched_when(struct sched_context *con,int id);
139 *! Convenience macro for objects and reference (add)
142 #define ast_sched_add_object(obj,con,when,callback) ast_sched_add((con),(when),(callback), ASTOBJ_REF((obj)))
145 *! Convenience macro for objects and reference (del)
148 #define ast_sched_del_object(obj,destructor,con,id) do { \
150 ast_sched_del((con),(id)); \
152 ASTOBJ_UNREF((obj),(destructor)); \
156 #if defined(__cplusplus) || defined(c_plusplus)
160 #endif /* _ASTERISK_SCHED_H */