00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "asterisk.h"
00027
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 253631 $")
00029
00030 #ifdef DEBUG_SCHEDULER
00031 #define DEBUG(a) do { \
00032 if (option_debug) \
00033 DEBUG_M(a) \
00034 } while (0)
00035 #else
00036 #define DEBUG(a)
00037 #endif
00038
00039 #include <stdio.h>
00040 #include <stdlib.h>
00041 #include <sys/time.h>
00042 #include <unistd.h>
00043 #include <string.h>
00044
00045 #include "asterisk/sched.h"
00046 #include "asterisk/logger.h"
00047 #include "asterisk/channel.h"
00048 #include "asterisk/lock.h"
00049 #include "asterisk/utils.h"
00050 #include "asterisk/linkedlists.h"
00051 #include "asterisk/options.h"
00052
00053 struct sched {
00054 AST_LIST_ENTRY(sched) list;
00055 int id;
00056 struct timeval when;
00057 int resched;
00058 int variable;
00059 const void *data;
00060 ast_sched_cb callback;
00061 };
00062
00063 struct sched_context {
00064 ast_mutex_t lock;
00065 unsigned int eventcnt;
00066 unsigned int schedcnt;
00067 AST_LIST_HEAD_NOLOCK(, sched) schedq;
00068
00069 #ifdef SCHED_MAX_CACHE
00070 AST_LIST_HEAD_NOLOCK(, sched) schedc;
00071 unsigned int schedccnt;
00072 #endif
00073 };
00074
00075 struct sched_context *sched_context_create(void)
00076 {
00077 struct sched_context *tmp;
00078
00079 if (!(tmp = ast_calloc(1, sizeof(*tmp))))
00080 return NULL;
00081
00082 ast_mutex_init(&tmp->lock);
00083 tmp->eventcnt = 1;
00084
00085 return tmp;
00086 }
00087
00088 void sched_context_destroy(struct sched_context *con)
00089 {
00090 struct sched *s;
00091
00092 ast_mutex_lock(&con->lock);
00093
00094 #ifdef SCHED_MAX_CACHE
00095
00096 while ((s = AST_LIST_REMOVE_HEAD(&con->schedc, list)))
00097 free(s);
00098 #endif
00099
00100
00101 while ((s = AST_LIST_REMOVE_HEAD(&con->schedq, list)))
00102 free(s);
00103
00104
00105 ast_mutex_unlock(&con->lock);
00106 ast_mutex_destroy(&con->lock);
00107 free(con);
00108 }
00109
00110 static struct sched *sched_alloc(struct sched_context *con)
00111 {
00112 struct sched *tmp;
00113
00114
00115
00116
00117
00118 #ifdef SCHED_MAX_CACHE
00119 if ((tmp = AST_LIST_REMOVE_HEAD(&con->schedc, list)))
00120 con->schedccnt--;
00121 else
00122 #endif
00123 tmp = ast_calloc(1, sizeof(*tmp));
00124
00125 return tmp;
00126 }
00127
00128 static void sched_release(struct sched_context *con, struct sched *tmp)
00129 {
00130
00131
00132
00133
00134
00135 #ifdef SCHED_MAX_CACHE
00136 if (con->schedccnt < SCHED_MAX_CACHE) {
00137 AST_LIST_INSERT_HEAD(&con->schedc, tmp, list);
00138 con->schedccnt++;
00139 } else
00140 #endif
00141 free(tmp);
00142 }
00143
00144
00145
00146
00147
00148 int ast_sched_wait(struct sched_context *con)
00149 {
00150 int ms;
00151
00152 DEBUG(ast_log(LOG_DEBUG, "ast_sched_wait()\n"));
00153
00154 ast_mutex_lock(&con->lock);
00155 if (AST_LIST_EMPTY(&con->schedq)) {
00156 ms = -1;
00157 } else {
00158 ms = ast_tvdiff_ms(AST_LIST_FIRST(&con->schedq)->when, ast_tvnow());
00159 if (ms < 0)
00160 ms = 0;
00161 }
00162 ast_mutex_unlock(&con->lock);
00163
00164 return ms;
00165 }
00166
00167
00168
00169
00170
00171
00172
00173 static void schedule(struct sched_context *con, struct sched *s)
00174 {
00175
00176 struct sched *cur = NULL;
00177
00178 AST_LIST_TRAVERSE_SAFE_BEGIN(&con->schedq, cur, list) {
00179 if (ast_tvcmp(s->when, cur->when) == -1) {
00180 AST_LIST_INSERT_BEFORE_CURRENT(&con->schedq, s, list);
00181 break;
00182 }
00183 }
00184 AST_LIST_TRAVERSE_SAFE_END
00185 if (!cur)
00186 AST_LIST_INSERT_TAIL(&con->schedq, s, list);
00187
00188 con->schedcnt++;
00189 }
00190
00191
00192
00193
00194
00195 static int sched_settime(struct timeval *tv, int when)
00196 {
00197 struct timeval now = ast_tvnow();
00198
00199
00200 if (ast_tvzero(*tv))
00201 *tv = now;
00202 *tv = ast_tvadd(*tv, ast_samp2tv(when, 1000));
00203 if (ast_tvcmp(*tv, now) < 0) {
00204 *tv = now;
00205 }
00206 return 0;
00207 }
00208
00209
00210
00211
00212
00213 int ast_sched_add_variable(struct sched_context *con, int when, ast_sched_cb callback, const void *data, int variable)
00214 {
00215 struct sched *tmp;
00216 int res = -1;
00217
00218 DEBUG(ast_log(LOG_DEBUG, "ast_sched_add()\n"));
00219
00220 ast_mutex_lock(&con->lock);
00221 if ((tmp = sched_alloc(con))) {
00222 tmp->id = con->eventcnt++;
00223 tmp->callback = callback;
00224 tmp->data = data;
00225 tmp->resched = when;
00226 tmp->variable = variable;
00227 tmp->when = ast_tv(0, 0);
00228 if (sched_settime(&tmp->when, when)) {
00229 sched_release(con, tmp);
00230 } else {
00231 schedule(con, tmp);
00232 res = tmp->id;
00233 }
00234 }
00235 #ifdef DUMP_SCHEDULER
00236
00237 if (option_debug)
00238 ast_sched_dump(con);
00239 #endif
00240 ast_mutex_unlock(&con->lock);
00241
00242 return res;
00243 }
00244
00245 int ast_sched_add(struct sched_context *con, int when, ast_sched_cb callback, const void *data)
00246 {
00247 return ast_sched_add_variable(con, when, callback, data, 0);
00248 }
00249
00250
00251
00252
00253
00254
00255
00256 #ifndef AST_DEVMODE
00257 int ast_sched_del(struct sched_context *con, int id)
00258 #else
00259 int _ast_sched_del(struct sched_context *con, int id, const char *file, int line, const char *function)
00260 #endif
00261 {
00262 struct sched *s;
00263
00264 DEBUG(ast_log(LOG_DEBUG, "ast_sched_del()\n"));
00265
00266 ast_mutex_lock(&con->lock);
00267 AST_LIST_TRAVERSE_SAFE_BEGIN(&con->schedq, s, list) {
00268 if (s->id == id) {
00269 AST_LIST_REMOVE_CURRENT(&con->schedq, list);
00270 con->schedcnt--;
00271 sched_release(con, s);
00272 break;
00273 }
00274 }
00275 AST_LIST_TRAVERSE_SAFE_END
00276
00277 #ifdef DUMP_SCHEDULER
00278
00279 if (option_debug)
00280 ast_sched_dump(con);
00281 #endif
00282 ast_mutex_unlock(&con->lock);
00283
00284 if (!s) {
00285 if (option_debug)
00286 ast_log(LOG_DEBUG, "Attempted to delete nonexistent schedule entry %d!\n", id);
00287 #ifndef AST_DEVMODE
00288 ast_assert(s != NULL);
00289 #else
00290 _ast_assert(0, "s != NULL", file, line, function);
00291 #endif
00292 return -1;
00293 }
00294
00295 return 0;
00296 }
00297
00298
00299 void ast_sched_dump(const struct sched_context *con)
00300 {
00301 struct sched *q;
00302 struct timeval tv = ast_tvnow();
00303 #ifdef SCHED_MAX_CACHE
00304 ast_log(LOG_DEBUG, "Asterisk Schedule Dump (%d in Q, %d Total, %d Cache)\n", con->schedcnt, con->eventcnt - 1, con->schedccnt);
00305 #else
00306 ast_log(LOG_DEBUG, "Asterisk Schedule Dump (%d in Q, %d Total)\n", con->schedcnt, con->eventcnt - 1);
00307 #endif
00308
00309 ast_log(LOG_DEBUG, "=============================================================\n");
00310 ast_log(LOG_DEBUG, "|ID Callback Data Time (sec:ms) |\n");
00311 ast_log(LOG_DEBUG, "+-----+-----------------+-----------------+-----------------+\n");
00312 AST_LIST_TRAVERSE(&con->schedq, q, list) {
00313 struct timeval delta = ast_tvsub(q->when, tv);
00314
00315 ast_log(LOG_DEBUG, "|%.4d | %-15p | %-15p | %.6ld : %.6ld |\n",
00316 q->id,
00317 q->callback,
00318 q->data,
00319 (long) delta.tv_sec,
00320 (long int)delta.tv_usec);
00321 }
00322 ast_log(LOG_DEBUG, "=============================================================\n");
00323
00324 }
00325
00326
00327
00328
00329 int ast_sched_runq(struct sched_context *con)
00330 {
00331 struct sched *current;
00332 struct timeval tv;
00333 int numevents;
00334 int res;
00335
00336 DEBUG(ast_log(LOG_DEBUG, "ast_sched_runq()\n"));
00337
00338 ast_mutex_lock(&con->lock);
00339
00340 for (numevents = 0; !AST_LIST_EMPTY(&con->schedq); numevents++) {
00341
00342
00343
00344
00345
00346 tv = ast_tvadd(ast_tvnow(), ast_tv(0, 1000));
00347 if (ast_tvcmp(AST_LIST_FIRST(&con->schedq)->when, tv) != -1)
00348 break;
00349
00350 current = AST_LIST_REMOVE_HEAD(&con->schedq, list);
00351 con->schedcnt--;
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362 ast_mutex_unlock(&con->lock);
00363 res = current->callback(current->data);
00364 ast_mutex_lock(&con->lock);
00365
00366 if (res) {
00367
00368
00369
00370
00371 if (sched_settime(¤t->when, current->variable? res : current->resched)) {
00372 sched_release(con, current);
00373 } else
00374 schedule(con, current);
00375 } else {
00376
00377 sched_release(con, current);
00378 }
00379 }
00380
00381 ast_mutex_unlock(&con->lock);
00382
00383 return numevents;
00384 }
00385
00386 long ast_sched_when(struct sched_context *con,int id)
00387 {
00388 struct sched *s;
00389 long secs = -1;
00390 DEBUG(ast_log(LOG_DEBUG, "ast_sched_when()\n"));
00391
00392 ast_mutex_lock(&con->lock);
00393 AST_LIST_TRAVERSE(&con->schedq, s, list) {
00394 if (s->id == id)
00395 break;
00396 }
00397 if (s) {
00398 struct timeval now = ast_tvnow();
00399 secs = s->when.tv_sec - now.tv_sec;
00400 }
00401 ast_mutex_unlock(&con->lock);
00402
00403 return secs;
00404 }