| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#include "asterisk.h" |
| 32 |
|
| 33 |
#ifdef DEBUG_SCHEDULER |
| 34 |
#define DEBUG(a) a |
| 35 |
#else |
| 36 |
#define DEBUG(a) |
| 37 |
#endif |
| 38 |
|
| 39 |
#include <sys/time.h> |
| 40 |
|
| 41 |
#include "asterisk/sched.h" |
| 42 |
#include "asterisk/channel.h" |
| 43 |
#include "asterisk/lock.h" |
| 44 |
#include "asterisk/utils.h" |
| 45 |
#include "asterisk/heap.h" |
| 46 |
#include "asterisk/threadstorage.h" |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
#define SCHED_MAX_CACHE 128 |
| 57 |
|
| 58 |
AST_THREADSTORAGE(last_del_id); |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
struct sched_id { |
| 71 |
|
| 72 |
int id; |
| 73 |
AST_LIST_ENTRY(sched_id) list; |
| 74 |
}; |
| 75 |
|
| 76 |
struct sched { |
| 77 |
AST_LIST_ENTRY(sched) list; |
| 78 |
|
| 79 |
struct sched_id *sched_id; |
| 80 |
struct timeval when; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
unsigned int tie_breaker; |
| 89 |
int resched; |
| 90 |
int variable; |
| 91 |
const void *data; |
| 92 |
ast_sched_cb callback; |
| 93 |
ssize_t __heap_index; |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
ast_cond_t cond; |
| 99 |
|
| 100 |
unsigned int deleted:1; |
| 101 |
|
| 102 |
unsigned int rescheduled:1; |
| 103 |
}; |
| 104 |
|
| 105 |
struct sched_thread { |
| 106 |
pthread_t thread; |
| 107 |
ast_cond_t cond; |
| 108 |
unsigned int stop:1; |
| 109 |
}; |
| 110 |
|
| 111 |
struct ast_sched_context { |
| 112 |
ast_mutex_t lock; |
| 113 |
unsigned int eventcnt; |
| 114 |
unsigned int highwater; |
| 115 |
|
| 116 |
unsigned int tie_breaker; |
| 117 |
struct ast_heap *sched_heap; |
| 118 |
struct sched_thread *sched_thread; |
| 119 |
|
| 120 |
struct sched *currently_executing; |
| 121 |
|
| 122 |
pthread_t executing_thread_id; |
| 123 |
|
| 124 |
#ifdef SCHED_MAX_CACHE |
| 125 |
AST_LIST_HEAD_NOLOCK(, sched) schedc; |
| 126 |
unsigned int schedccnt; |
| 127 |
#endif |
| 128 |
|
| 129 |
AST_LIST_HEAD_NOLOCK(, sched_id) id_queue; |
| 130 |
|
| 131 |
int id_queue_size; |
| 132 |
}; |
| 133 |
|
| 134 |
static void *sched_run(void *data) |
| 135 |
{ |
| 136 |
struct ast_sched_context *con = data; |
| 137 |
|
| 138 |
while (!con->sched_thread->stop) { |
| 139 |
int ms; |
| 140 |
struct timespec ts = { |
| 141 |
.tv_sec = 0, |
| 142 |
}; |
| 143 |
|
| 144 |
ast_mutex_lock(&con->lock); |
| 145 |
|
| 146 |
if (con->sched_thread->stop) { |
| 147 |
ast_mutex_unlock(&con->lock); |
| 148 |
return NULL; |
| 149 |
} |
| 150 |
|
| 151 |
ms = ast_sched_wait(con); |
| 152 |
|
| 153 |
if (ms == -1) { |
| 154 |
ast_cond_wait(&con->sched_thread->cond, &con->lock); |
| 155 |
} else { |
| 156 |
struct timeval tv; |
| 157 |
tv = ast_tvadd(ast_tvnow(), ast_samp2tv(ms, 1000)); |
| 158 |
ts.tv_sec = tv.tv_sec; |
| 159 |
ts.tv_nsec = tv.tv_usec * 1000; |
| 160 |
ast_cond_timedwait(&con->sched_thread->cond, &con->lock, &ts); |
| 161 |
} |
| 162 |
|
| 163 |
ast_mutex_unlock(&con->lock); |
| 164 |
|
| 165 |
if (con->sched_thread->stop) { |
| 166 |
return NULL; |
| 167 |
} |
| 168 |
|
| 169 |
ast_sched_runq(con); |
| 170 |
} |
| 171 |
|
| 172 |
return NULL; |
| 173 |
} |
| 174 |
|
| 175 |
static void sched_thread_destroy(struct ast_sched_context *con) |
| 176 |
{ |
| 177 |
if (!con->sched_thread) { |
| 178 |
return; |
| 179 |
} |
| 180 |
|
| 181 |
if (con->sched_thread->thread != AST_PTHREADT_NULL) { |
| 182 |
ast_mutex_lock(&con->lock); |
| 183 |
con->sched_thread->stop = 1; |
| 184 |
ast_cond_signal(&con->sched_thread->cond); |
| 185 |
ast_mutex_unlock(&con->lock); |
| 186 |
pthread_join(con->sched_thread->thread, NULL); |
| 187 |
con->sched_thread->thread = AST_PTHREADT_NULL; |
| 188 |
} |
| 189 |
|
| 190 |
ast_cond_destroy(&con->sched_thread->cond); |
| 191 |
|
| 192 |
ast_free(con->sched_thread); |
| 193 |
|
| 194 |
con->sched_thread = NULL; |
| 195 |
} |
| 196 |
|
| 197 |
int ast_sched_start_thread(struct ast_sched_context *con) |
| 198 |
{ |
| 199 |
struct sched_thread *st; |
| 200 |
|
| 201 |
if (con->sched_thread) { |
| 202 |
ast_log(LOG_ERROR, "Thread already started on this scheduler context\n"); |
| 203 |
return -1; |
| 204 |
} |
| 205 |
|
| 206 |
if (!(st = ast_calloc(1, sizeof(*st)))) { |
| 207 |
return -1; |
| 208 |
} |
| 209 |
|
| 210 |
ast_cond_init(&st->cond, NULL); |
| 211 |
|
| 212 |
st->thread = AST_PTHREADT_NULL; |
| 213 |
|
| 214 |
con->sched_thread = st; |
| 215 |
|
| 216 |
if (ast_pthread_create_background(&st->thread, NULL, sched_run, con)) { |
| 217 |
ast_log(LOG_ERROR, "Failed to create scheduler thread\n"); |
| 218 |
sched_thread_destroy(con); |
| 219 |
return -1; |
| 220 |
} |
| 221 |
|
| 222 |
return 0; |
| 223 |
} |
| 224 |
|
| 225 |
static int sched_time_cmp(void *va, void *vb) |
| 226 |
{ |
| 227 |
struct sched *a = va; |
| 228 |
struct sched *b = vb; |
| 229 |
int cmp; |
| 230 |
|
| 231 |
cmp = ast_tvcmp(b->when, a->when); |
| 232 |
if (!cmp) { |
| 233 |
cmp = b->tie_breaker - a->tie_breaker; |
| 234 |
} |
| 235 |
return cmp; |
| 236 |
} |
| 237 |
|
| 238 |
struct ast_sched_context *ast_sched_context_create(void) |
| 239 |
{ |
| 240 |
struct ast_sched_context *tmp; |
| 241 |
|
| 242 |
if (!(tmp = ast_calloc(1, sizeof(*tmp)))) { |
| 243 |
return NULL; |
| 244 |
} |
| 245 |
|
| 246 |
ast_mutex_init(&tmp->lock); |
| 247 |
tmp->eventcnt = 1; |
| 248 |
|
| 249 |
AST_LIST_HEAD_INIT_NOLOCK(&tmp->id_queue); |
| 250 |
|
| 251 |
if (!(tmp->sched_heap = ast_heap_create(8, sched_time_cmp, |
| 252 |
offsetof(struct sched, __heap_index)))) { |
| 253 |
ast_sched_context_destroy(tmp); |
| 254 |
return NULL; |
| 255 |
} |
| 256 |
|
| 257 |
return tmp; |
| 258 |
} |
| 259 |
|
| 260 |
static void sched_free(struct sched *task) |
| 261 |
{ |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
ast_free(task->sched_id); |
| 267 |
ast_cond_destroy(&task->cond); |
| 268 |
ast_free(task); |
| 269 |
} |
| 270 |
|
| 271 |
void ast_sched_context_destroy(struct ast_sched_context *con) |
| 272 |
{ |
| 273 |
struct sched *s; |
| 274 |
struct sched_id *sid; |
| 275 |
|
| 276 |
sched_thread_destroy(con); |
| 277 |
con->sched_thread = NULL; |
| 278 |
|
| 279 |
ast_mutex_lock(&con->lock); |
| 280 |
|
| 281 |
#ifdef SCHED_MAX_CACHE |
| 282 |
while ((s = AST_LIST_REMOVE_HEAD(&con->schedc, list))) { |
| 283 |
sched_free(s); |
| 284 |
} |
| 285 |
#endif |
| 286 |
|
| 287 |
if (con->sched_heap) { |
| 288 |
while ((s = ast_heap_pop(con->sched_heap))) { |
| 289 |
sched_free(s); |
| 290 |
} |
| 291 |
ast_heap_destroy(con->sched_heap); |
| 292 |
con->sched_heap = NULL; |
| 293 |
} |
| 294 |
|
| 295 |
while ((sid = AST_LIST_REMOVE_HEAD(&con->id_queue, list))) { |
| 296 |
ast_free(sid); |
| 297 |
} |
| 298 |
|
| 299 |
ast_mutex_unlock(&con->lock); |
| 300 |
ast_mutex_destroy(&con->lock); |
| 301 |
|
| 302 |
ast_free(con); |
| 303 |
} |
| 304 |
|
| 305 |
#define ID_QUEUE_INCREMENT 16 |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
static int add_ids(struct ast_sched_context *con) |
| 313 |
{ |
| 314 |
int new_size; |
| 315 |
int original_size; |
| 316 |
int i; |
| 317 |
|
| 318 |
original_size = con->id_queue_size; |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
new_size = original_size + ID_QUEUE_INCREMENT; |
| 324 |
if (new_size < 0) { |
| 325 |
|
| 326 |
new_size = INT_MAX; |
| 327 |
} |
| 328 |
for (i = original_size; i < new_size; ++i) { |
| 329 |
struct sched_id *new_id; |
| 330 |
|
| 331 |
new_id = ast_calloc(1, sizeof(*new_id)); |
| 332 |
if (!new_id) { |
| 333 |
break; |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
new_id->id = ++con->id_queue_size; |
| 343 |
|
| 344 |
AST_LIST_INSERT_TAIL(&con->id_queue, new_id, list); |
| 345 |
} |
| 346 |
|
| 347 |
return con->id_queue_size - original_size; |
| 348 |
} |
| 349 |
|
| 350 |
static int set_sched_id(struct ast_sched_context *con, struct sched *new_sched) |
| 351 |
{ |
| 352 |
if (AST_LIST_EMPTY(&con->id_queue) && (add_ids(con) == 0)) { |
| 353 |
return -1; |
| 354 |
} |
| 355 |
|
| 356 |
new_sched->sched_id = AST_LIST_REMOVE_HEAD(&con->id_queue, list); |
| 357 |
return 0; |
| 358 |
} |
| 359 |
|
| 360 |
static void sched_release(struct ast_sched_context *con, struct sched *tmp) |
| 361 |
{ |
| 362 |
if (tmp->sched_id) { |
| 363 |
AST_LIST_INSERT_TAIL(&con->id_queue, tmp->sched_id, list); |
| 364 |
tmp->sched_id = NULL; |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
#ifdef SCHED_MAX_CACHE |
| 372 |
if (con->schedccnt < SCHED_MAX_CACHE) { |
| 373 |
AST_LIST_INSERT_HEAD(&con->schedc, tmp, list); |
| 374 |
con->schedccnt++; |
| 375 |
} else |
| 376 |
#endif |
| 377 |
sched_free(tmp); |
| 378 |
} |
| 379 |
|
| 380 |
static struct sched *sched_alloc(struct ast_sched_context *con) |
| 381 |
{ |
| 382 |
struct sched *tmp; |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
#ifdef SCHED_MAX_CACHE |
| 389 |
if ((tmp = AST_LIST_REMOVE_HEAD(&con->schedc, list))) { |
| 390 |
con->schedccnt--; |
| 391 |
} else |
| 392 |
#endif |
| 393 |
{ |
| 394 |
tmp = ast_calloc(1, sizeof(*tmp)); |
| 395 |
if (!tmp) { |
| 396 |
return NULL; |
| 397 |
} |
| 398 |
ast_cond_init(&tmp->cond, NULL); |
| 399 |
} |
| 400 |
|
| 401 |
if (set_sched_id(con, tmp)) { |
| 402 |
sched_release(con, tmp); |
| 403 |
return NULL; |
| 404 |
} |
| 405 |
|
| 406 |
return tmp; |
| 407 |
} |
| 408 |
|
| 409 |
void ast_sched_clean_by_callback(struct ast_sched_context *con, ast_sched_cb match, ast_sched_cb cleanup_cb) |
| 410 |
{ |
| 411 |
int i = 1; |
| 412 |
struct sched *current; |
| 413 |
|
| 414 |
ast_mutex_lock(&con->lock); |
| 415 |
while ((current = ast_heap_peek(con->sched_heap, i))) { |
| 416 |
if (current->callback != match) { |
| 417 |
i++; |
| 418 |
continue; |
| 419 |
} |
| 420 |
|
| 421 |
ast_heap_remove(con->sched_heap, current); |
| 422 |
|
| 423 |
cleanup_cb(current->data); |
| 424 |
sched_release(con, current); |
| 425 |
} |
| 426 |
ast_mutex_unlock(&con->lock); |
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
int ast_sched_wait(struct ast_sched_context *con) |
| 434 |
{ |
| 435 |
int ms; |
| 436 |
struct sched *s; |
| 437 |
|
| 438 |
DEBUG(ast_debug(1, "ast_sched_wait()\n")); |
| 439 |
|
| 440 |
ast_mutex_lock(&con->lock); |
| 441 |
if ((s = ast_heap_peek(con->sched_heap, 1))) { |
| 442 |
ms = ast_tvdiff_ms(s->when, ast_tvnow()); |
| 443 |
if (ms < 0) { |
| 444 |
ms = 0; |
| 445 |
} |
| 446 |
} else { |
| 447 |
ms = -1; |
| 448 |
} |
| 449 |
ast_mutex_unlock(&con->lock); |
| 450 |
|
| 451 |
return ms; |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
static void schedule(struct ast_sched_context *con, struct sched *s) |
| 461 |
{ |
| 462 |
size_t size; |
| 463 |
|
| 464 |
size = ast_heap_size(con->sched_heap); |
| 465 |
|
| 466 |
|
| 467 |
if (con->highwater <= size) { |
| 468 |
con->highwater = size + 1; |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
if (size) { |
| 473 |
++con->tie_breaker; |
| 474 |
} else { |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
con->tie_breaker = 0; |
| 480 |
} |
| 481 |
s->tie_breaker = con->tie_breaker; |
| 482 |
|
| 483 |
ast_heap_push(con->sched_heap, s); |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
static void sched_settime(struct timeval *t, int when) |
| 491 |
{ |
| 492 |
struct timeval now = ast_tvnow(); |
| 493 |
|
| 494 |
if (when < 0) { |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
ast_log(LOG_WARNING, |
| 500 |
"Bug likely: Negative time interval %d (interpreted as %u ms) requested!\n", |
| 501 |
when, (unsigned int) when); |
| 502 |
ast_assert(0); |
| 503 |
} |
| 504 |
|
| 505 |
|
| 506 |
if (ast_tvzero(*t)) |
| 507 |
*t = now; |
| 508 |
*t = ast_tvadd(*t, ast_samp2tv(when, 1000)); |
| 509 |
if (ast_tvcmp(*t, now) < 0) { |
| 510 |
*t = now; |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
int ast_sched_replace_variable(int old_id, struct ast_sched_context *con, int when, ast_sched_cb callback, const void *data, int variable) |
| 515 |
{ |
| 516 |
|
| 517 |
if (old_id > 0) { |
| 518 |
AST_SCHED_DEL(con, old_id); |
| 519 |
} |
| 520 |
return ast_sched_add_variable(con, when, callback, data, variable); |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
int ast_sched_add_variable(struct ast_sched_context *con, int when, ast_sched_cb callback, const void *data, int variable) |
| 527 |
{ |
| 528 |
struct sched *tmp; |
| 529 |
int res = -1; |
| 530 |
|
| 531 |
DEBUG(ast_debug(1, "ast_sched_add()\n")); |
| 532 |
|
| 533 |
ast_mutex_lock(&con->lock); |
| 534 |
if ((tmp = sched_alloc(con))) { |
| 535 |
con->eventcnt++; |
| 536 |
tmp->callback = callback; |
| 537 |
tmp->data = data; |
| 538 |
tmp->resched = when; |
| 539 |
tmp->variable = variable; |
| 540 |
tmp->when = ast_tv(0, 0); |
| 541 |
tmp->deleted = 0; |
| 542 |
|
| 543 |
sched_settime(&tmp->when, when); |
| 544 |
schedule(con, tmp); |
| 545 |
res = tmp->sched_id->id; |
| 546 |
} |
| 547 |
#ifdef DUMP_SCHEDULER |
| 548 |
|
| 549 |
ast_sched_dump(con); |
| 550 |
#endif |
| 551 |
if (con->sched_thread) { |
| 552 |
ast_cond_signal(&con->sched_thread->cond); |
| 553 |
} |
| 554 |
ast_mutex_unlock(&con->lock); |
| 555 |
|
| 556 |
return res; |
| 557 |
} |
| 558 |
|
| 559 |
int ast_sched_replace(int old_id, struct ast_sched_context *con, int when, ast_sched_cb callback, const void *data) |
| 560 |
{ |
| 561 |
if (old_id > -1) { |
| 562 |
AST_SCHED_DEL(con, old_id); |
| 563 |
} |
| 564 |
return ast_sched_add(con, when, callback, data); |
| 565 |
} |
| 566 |
|
| 567 |
int ast_sched_add(struct ast_sched_context *con, int when, ast_sched_cb callback, const void *data) |
| 568 |
{ |
| 569 |
return ast_sched_add_variable(con, when, callback, data, 0); |
| 570 |
} |
| 571 |
|
| 572 |
static struct sched *sched_find(struct ast_sched_context *con, int id) |
| 573 |
{ |
| 574 |
int x; |
| 575 |
size_t heap_size; |
| 576 |
|
| 577 |
heap_size = ast_heap_size(con->sched_heap); |
| 578 |
for (x = 1; x <= heap_size; x++) { |
| 579 |
struct sched *cur = ast_heap_peek(con->sched_heap, x); |
| 580 |
|
| 581 |
if (cur->sched_id->id == id) { |
| 582 |
return cur; |
| 583 |
} |
| 584 |
} |
| 585 |
|
| 586 |
return NULL; |
| 587 |
} |
| 588 |
|
| 589 |
const void *ast_sched_find_data(struct ast_sched_context *con, int id) |
| 590 |
{ |
| 591 |
struct sched *s; |
| 592 |
const void *data = NULL; |
| 593 |
|
| 594 |
ast_mutex_lock(&con->lock); |
| 595 |
|
| 596 |
s = sched_find(con, id); |
| 597 |
if (s) { |
| 598 |
data = s->data; |
| 599 |
} |
| 600 |
|
| 601 |
ast_mutex_unlock(&con->lock); |
| 602 |
|
| 603 |
return data; |
| 604 |
} |
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
int ast_sched_del(struct ast_sched_context *con, int id) |
| 615 |
{ |
| 616 |
return ast_sched_del_nonrunning(con, id) ? -1 : 0; |
| 617 |
} |
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
int ast_sched_del_nonrunning(struct ast_sched_context *con, int id) |
| 628 |
{ |
| 629 |
struct sched *s = NULL; |
| 630 |
int *last_id = ast_threadstorage_get(&last_del_id, sizeof(int)); |
| 631 |
int res = 0; |
| 632 |
|
| 633 |
DEBUG(ast_debug(1, "ast_sched_del(%d)\n", id)); |
| 634 |
|
| 635 |
if (id < 0) { |
| 636 |
return 0; |
| 637 |
} |
| 638 |
|
| 639 |
ast_mutex_lock(&con->lock); |
| 640 |
|
| 641 |
s = sched_find(con, id); |
| 642 |
if (s) { |
| 643 |
if (!ast_heap_remove(con->sched_heap, s)) { |
| 644 |
ast_log(LOG_WARNING,"sched entry %d not in the sched heap?\n", s->sched_id->id); |
| 645 |
} |
| 646 |
sched_release(con, s); |
| 647 |
} else if (con->currently_executing && (id == con->currently_executing->sched_id->id)) { |
| 648 |
if (con->executing_thread_id == pthread_self()) { |
| 649 |
|
| 650 |
|
| 651 |
ast_log(LOG_ERROR, |
| 652 |
"BUG! Trying to delete sched %d from within the callback %p. " |
| 653 |
"Ignoring so we don't deadlock\n", |
| 654 |
id, con->currently_executing->callback); |
| 655 |
ast_log_backtrace(); |
| 656 |
|
| 657 |
|
| 658 |
} else { |
| 659 |
s = con->currently_executing; |
| 660 |
s->deleted = 1; |
| 661 |
|
| 662 |
|
| 663 |
while (con->currently_executing && (id == con->currently_executing->sched_id->id)) { |
| 664 |
ast_cond_wait(&s->cond, &con->lock); |
| 665 |
} |
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
if (!s->rescheduled) { |
| 670 |
res = -2; |
| 671 |
} |
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
sched_release(con, s); |
| 676 |
s = NULL; |
| 677 |
} |
| 678 |
} |
| 679 |
|
| 680 |
#ifdef DUMP_SCHEDULER |
| 681 |
|
| 682 |
ast_sched_dump(con); |
| 683 |
#endif |
| 684 |
if (con->sched_thread) { |
| 685 |
ast_cond_signal(&con->sched_thread->cond); |
| 686 |
} |
| 687 |
ast_mutex_unlock(&con->lock); |
| 688 |
|
| 689 |
if(res == -2){ |
| 690 |
return res; |
| 691 |
} |
| 692 |
else if (!s && *last_id != id) { |
| 693 |
ast_debug(1, "Attempted to delete nonexistent schedule entry %d!\n", id); |
| 694 |
|
| 695 |
|
| 696 |
*last_id = id; |
| 697 |
return -1; |
| 698 |
} else if (!s) { |
| 699 |
return -1; |
| 700 |
} |
| 701 |
|
| 702 |
return res; |
| 703 |
} |
| 704 |
|
| 705 |
void ast_sched_report(struct ast_sched_context *con, struct ast_str **buf, struct ast_cb_names *cbnames) |
| 706 |
{ |
| 707 |
int i, x; |
| 708 |
struct sched *cur; |
| 709 |
int countlist[cbnames->numassocs + 1]; |
| 710 |
size_t heap_size; |
| 711 |
|
| 712 |
memset(countlist, 0, sizeof(countlist)); |
| 713 |
ast_str_set(buf, 0, " Highwater = %u\n schedcnt = %zu\n", con->highwater, ast_heap_size(con->sched_heap)); |
| 714 |
|
| 715 |
ast_mutex_lock(&con->lock); |
| 716 |
|
| 717 |
heap_size = ast_heap_size(con->sched_heap); |
| 718 |
for (x = 1; x <= heap_size; x++) { |
| 719 |
cur = ast_heap_peek(con->sched_heap, x); |
| 720 |
|
| 721 |
for (i = 0; i < cbnames->numassocs; i++) { |
| 722 |
if (cur->callback == cbnames->cblist[i]) { |
| 723 |
break; |
| 724 |
} |
| 725 |
} |
| 726 |
if (i < cbnames->numassocs) { |
| 727 |
countlist[i]++; |
| 728 |
} else { |
| 729 |
countlist[cbnames->numassocs]++; |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
ast_mutex_unlock(&con->lock); |
| 734 |
|
| 735 |
for (i = 0; i < cbnames->numassocs; i++) { |
| 736 |
ast_str_append(buf, 0, " %s : %d\n", cbnames->list[i], countlist[i]); |
| 737 |
} |
| 738 |
|
| 739 |
ast_str_append(buf, 0, " <unknown> : %d\n", countlist[cbnames->numassocs]); |
| 740 |
} |
| 741 |
|
| 742 |
|
| 743 |
void ast_sched_dump(struct ast_sched_context *con) |
| 744 |
{ |
| 745 |
struct sched *q; |
| 746 |
struct timeval when; |
| 747 |
int x; |
| 748 |
size_t heap_size; |
| 749 |
|
| 750 |
if (!DEBUG_ATLEAST(1)) { |
| 751 |
return; |
| 752 |
} |
| 753 |
|
| 754 |
when = ast_tvnow(); |
| 755 |
#ifdef SCHED_MAX_CACHE |
| 756 |
ast_log(LOG_DEBUG, "Asterisk Schedule Dump (%zu in Q, %u Total, %u Cache, %u high-water)\n", |
| 757 |
ast_heap_size(con->sched_heap), con->eventcnt - 1, con->schedccnt, con->highwater); |
| 758 |
#else |
| 759 |
ast_log(LOG_DEBUG, "Asterisk Schedule Dump (%zu in Q, %u Total, %u high-water)\n", |
| 760 |
ast_heap_size(con->sched_heap), con->eventcnt - 1, con->highwater); |
| 761 |
#endif |
| 762 |
|
| 763 |
ast_log(LOG_DEBUG, "=============================================================\n"); |
| 764 |
ast_log(LOG_DEBUG, "|ID Callback Data Time (sec:ms) |\n"); |
| 765 |
ast_log(LOG_DEBUG, "+-----+-----------------+-----------------+-----------------+\n"); |
| 766 |
ast_mutex_lock(&con->lock); |
| 767 |
heap_size = ast_heap_size(con->sched_heap); |
| 768 |
for (x = 1; x <= heap_size; x++) { |
| 769 |
struct timeval delta; |
| 770 |
q = ast_heap_peek(con->sched_heap, x); |
| 771 |
delta = ast_tvsub(q->when, when); |
| 772 |
ast_log(LOG_DEBUG, "|%.4d | %-15p | %-15p | %.6ld : %.6ld |\n", |
| 773 |
q->sched_id->id, |
| 774 |
q->callback, |
| 775 |
q->data, |
| 776 |
(long)delta.tv_sec, |
| 777 |
(long int)delta.tv_usec); |
| 778 |
} |
| 779 |
ast_mutex_unlock(&con->lock); |
| 780 |
ast_log(LOG_DEBUG, "=============================================================\n"); |
| 781 |
} |
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
int ast_sched_runq(struct ast_sched_context *con) |
| 787 |
{ |
| 788 |
struct sched *current; |
| 789 |
struct timeval when; |
| 790 |
int numevents; |
| 791 |
int res; |
| 792 |
|
| 793 |
DEBUG(ast_debug(1, "ast_sched_runq()\n")); |
| 794 |
|
| 795 |
ast_mutex_lock(&con->lock); |
| 796 |
|
| 797 |
when = ast_tvadd(ast_tvnow(), ast_tv(0, 1000)); |
| 798 |
for (numevents = 0; (current = ast_heap_peek(con->sched_heap, 1)); numevents++) { |
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
if (ast_tvcmp(current->when, when) != -1) { |
| 805 |
break; |
| 806 |
} |
| 807 |
|
| 808 |
current = ast_heap_pop(con->sched_heap); |
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
con->currently_executing = current; |
| 820 |
con->executing_thread_id = pthread_self(); |
| 821 |
ast_mutex_unlock(&con->lock); |
| 822 |
res = current->callback(current->data); |
| 823 |
ast_mutex_lock(&con->lock); |
| 824 |
con->currently_executing = NULL; |
| 825 |
ast_cond_signal(¤t->cond); |
| 826 |
|
| 827 |
if (current->deleted) { |
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
current->rescheduled = res ? 1 : 0; |
| 833 |
} else if (res) { |
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
sched_settime(¤t->when, current->variable ? res : current->resched); |
| 839 |
schedule(con, current); |
| 840 |
} else { |
| 841 |
|
| 842 |
sched_release(con, current); |
| 843 |
} |
| 844 |
} |
| 845 |
|
| 846 |
ast_mutex_unlock(&con->lock); |
| 847 |
|
| 848 |
return numevents; |
| 849 |
} |
| 850 |
|
| 851 |
long ast_sched_when(struct ast_sched_context *con,int id) |
| 852 |
{ |
| 853 |
struct sched *s; |
| 854 |
long secs = -1; |
| 855 |
DEBUG(ast_debug(1, "ast_sched_when()\n")); |
| 856 |
|
| 857 |
ast_mutex_lock(&con->lock); |
| 858 |
|
| 859 |
s = sched_find(con, id); |
| 860 |
if (s) { |
| 861 |
struct timeval now = ast_tvnow(); |
| 862 |
secs = s->when.tv_sec - now.tv_sec; |
| 863 |
} |
| 864 |
|
| 865 |
ast_mutex_unlock(&con->lock); |
| 866 |
|
| 867 |
return secs; |
| 868 |
} |