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