#include <stdio.h>#include <stdlib.h>#include <sys/time.h>#include <unistd.h>#include <asterisk/sched.h>#include <asterisk/logger.h>#include <asterisk/channel.h>Go to the source code of this file.
Data Structures | |
| struct | sched |
| struct | sched_context |
Defines | |
| #define | DEBUG(a) |
| #define | SOONER(a, b) |
Functions | |
| sched_context * | sched_context_create (void) |
| New schedule context. | |
| void | sched_context_destroy (struct sched_context *con) |
| destroys a schedule context | |
| int | ast_sched_wait (struct sched_context *con) |
| Determines number of seconds until the next outstanding event to take place. | |
| int | ast_sched_add (struct sched_context *con, int when, ast_sched_cb callback, void *data) |
| Adds a scheduled event. | |
| int | ast_sched_del (struct sched_context *con, int id) |
| Deletes a scheduled event. | |
| void | ast_sched_dump (struct sched_context *con) |
| Dumps the scheduler contents. | |
| int | ast_sched_runq (struct sched_context *con) |
| Runs the queue. | |
|
|
|
|
|
Value: Definition at line 29 of file sched.c. Referenced by ast_sched_runq(). |
|
||||||||||||||||||||
|
Adds a scheduled event.
Definition at line 220 of file sched.c. References ast_log(), ast_sched_cb, DEBUG, sched_context::eventcnt, LOG_DEBUG, and LOG_NOTICE.
00221 {
00222 /*
00223 * Schedule callback(data) to happen when ms into the future
00224 */
00225 struct sched *tmp;
00226 DEBUG(ast_log(LOG_DEBUG, "ast_sched_add()\n"));
00227 if (!when) {
00228 ast_log(LOG_NOTICE, "Scheduled event in 0 ms?");
00229 return -1;
00230 }
00231 if ((tmp = sched_alloc(con))) {
00232 tmp->id = con->eventcnt++;
00233 tmp->callback = callback;
00234 tmp->data = data;
00235 tmp->resched = when;
00236 tmp->when.tv_sec = 0;
00237 tmp->when.tv_usec = 0;
00238 if (sched_settime(&tmp->when, when)) {
00239 sched_release(con, tmp);
00240 return -1;
00241 } else
00242 schedule(con, tmp);
00243 } else
00244 return -1;
00245 return tmp->id;
00246 }
|
|
||||||||||||
|
Deletes a scheduled event.
Definition at line 248 of file sched.c. References ast_log(), CRASH, DEBUG, LOG_DEBUG, LOG_NOTICE, sched::next, sched_context::schedcnt, and sched_context::schedq. Referenced by ast_closestream().
00249 {
00250 /*
00251 * Delete the schedule entry with number
00252 * "id". It's nearly impossible that there
00253 * would be two or more in the list with that
00254 * id.
00255 */
00256 struct sched *last=NULL, *s;
00257 DEBUG(ast_log(LOG_DEBUG, "ast_sched_del()\n"));
00258 s = con->schedq;
00259 while(s) {
00260 if (s->id == id) {
00261 if (last)
00262 last->next = s->next;
00263 else
00264 con->schedq = s->next;
00265 con->schedcnt--;
00266 sched_release(con, s);
00267 return 0;
00268 }
00269 last = s;
00270 s = s->next;
00271 }
00272 ast_log(LOG_NOTICE, "Attempted to delete non-existant schedule entry %d!\n", id);
00273 #ifdef DO_CRASH
00274 CRASH;
00275 #endif
00276 return -1;
00277 }
|
|
|
Dumps the scheduler contents.
Definition at line 279 of file sched.c. References ast_log(), sched_context::eventcnt, LOG_DEBUG, sched_context::schedq, and sched::when.
00280 {
00281 /*
00282 * Dump the contents of the scheduler to
00283 * stderr
00284 */
00285 struct sched *q;
00286 struct timeval tv;
00287 time_t s, ms;
00288 gettimeofday(&tv, NULL);
00289 #ifdef SCHED_MAX_CACHE
00290 ast_log(LOG_DEBUG, "Asterisk Schedule Dump (%d in Q, %d Total, %d Cache)\n",
00291 con-> schedcnt, con->eventcnt - 1, con->schedccnt);
00292 #else
00293 ast_log(LOG_DEBUG, "Asterisk Schedule Dump (%d in Q, %d Total)\n",
00294 con-> schedcnt, con->eventcnt - 1);
00295 #endif
00296
00297 ast_log(LOG_DEBUG, "=================================================\n");
00298 ast_log(LOG_DEBUG, "|ID Callback Data Time (sec:ms) |\n");
00299 ast_log(LOG_DEBUG, "+-----+-----------+-----------+-----------------+\n");
00300 q = con->schedq;
00301 while(q) {
00302 s = q->when.tv_sec - tv.tv_sec;
00303 ms = q->when.tv_usec - tv.tv_usec;
00304 if (ms < 0) {
00305 ms += 1000000;
00306 s--;
00307 }
00308 ast_log(LOG_DEBUG, "|%.4d | %p | %p | %.6ld : %.6ld |\n",
00309 q->id,
00310 q->callback,
00311 q->data,
00312 (long)s,
00313 (long)ms);
00314 q=q->next;
00315 }
00316 ast_log(LOG_DEBUG, "=================================================\n");
00317
00318 }
|
|
|
Runs the queue.
Definition at line 320 of file sched.c. References ast_log(), DEBUG, LOG_DEBUG, LOG_NOTICE, sched::next, sched_context::schedcnt, sched_context::schedq, SOONER, and sched::when. Referenced by ast_waitstream(), ast_waitstream_fr(), and ast_waitstream_full().
00321 {
00322 /*
00323 * Launch all events which need to be run at this time.
00324 */
00325 struct sched *current;
00326 struct timeval tv;
00327 int x=0;
00328 DEBUG(ast_log(LOG_DEBUG, "ast_sched_runq()\n"));
00329
00330 for(;;) {
00331 if (!con->schedq)
00332 break;
00333 if (gettimeofday(&tv, NULL)) {
00334 /* This should never happen */
00335 ast_log(LOG_NOTICE, "gettimeofday() failed!\n");
00336 return 0;
00337 }
00338 /* We only care about millisecond accuracy anyway, so this will
00339 help us get more than one event at one time if they are very
00340 close together. */
00341 tv.tv_usec += 1000;
00342 if (SOONER(con->schedq->when, tv)) {
00343 current = con->schedq;
00344 con->schedq = con->schedq->next;
00345 con->schedcnt--;
00346
00347 /*
00348 * At this point, the schedule queue is still intact. We
00349 * have removed the first event and the rest is still there,
00350 * so it's permissible for the callback to add new events, but
00351 * trying to delete itself won't work because it isn't in
00352 * the schedule queue. If that's what it wants to do, it
00353 * should return 0.
00354 */
00355 if (current->callback(current->data)) {
00356 /*
00357 * If they return non-zero, we should schedule them to be
00358 * run again.
00359 */
00360 if (sched_settime(¤t->when, current->resched)) {
00361 sched_release(con, current);
00362 } else
00363 schedule(con, current);
00364 } else {
00365 /* No longer needed, so release it */
00366 sched_release(con, current);
00367 }
00368 x++;
00369 } else
00370 break;
00371 }
00372 return x;
00373 }
|
|
|
Determines number of seconds until the next outstanding event to take place.
Definition at line 132 of file sched.c. References ast_log(), DEBUG, LOG_DEBUG, sched_context::schedq, and sched::when. Referenced by ast_waitstream(), ast_waitstream_fr(), and ast_waitstream_full().
00133 {
00134 /*
00135 * Return the number of milliseconds
00136 * until the next scheduled event
00137 */
00138 struct timeval tv;
00139 int ms;
00140 DEBUG(ast_log(LOG_DEBUG, "ast_sched_wait()\n"));
00141 if (!con->schedq)
00142 return -1;
00143 if (gettimeofday(&tv, NULL) < 0) {
00144 /* This should never happen */
00145 return 0;
00146 };
00147 ms = (con->schedq->when.tv_sec - tv.tv_sec) * 1000;
00148 ms += (con->schedq->when.tv_usec - tv.tv_usec) / 1000;
00149 if (ms < 0)
00150 ms = 0;
00151 return ms;
00152
00153 }
|
|
|
New schedule context.
Definition at line 58 of file sched.c. References malloc. Referenced by ast_channel_alloc().
00059 {
00060 struct sched_context *tmp;
00061 tmp = malloc(sizeof(struct sched_context));
00062 if (tmp) {
00063 tmp->eventcnt = 1;
00064 tmp->schedcnt = 0;
00065 tmp->schedq = NULL;
00066 #ifdef SCHED_MAX_CACHE
00067 tmp->schedc = NULL;
00068 tmp->schedccnt = 0;
00069 #endif
00070 }
00071 return tmp;
00072 }
|
|
|
destroys a schedule context
Definition at line 74 of file sched.c. References free, sched::next, and sched_context::schedq. Referenced by ast_hangup().
00075 {
00076 struct sched *s, *sl;
00077 #ifdef SCHED_MAX_CACHE
00078 /* Eliminate the cache */
00079 s = con->schedc;
00080 while(s) {
00081 sl = s;
00082 s = s->next;
00083 free(sl);
00084 }
00085 #endif
00086 /* And the queue */
00087 s = con->schedq;
00088 while(s) {
00089 sl = s;
00090 s = s->next;
00091 free(sl);
00092 }
00093 /* And the context */
00094 free(con);
00095 }
|
1.3.4