| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#include "asterisk.h" |
| 21 |
|
| 22 |
#include "asterisk/threadpool.h" |
| 23 |
#include "asterisk/taskprocessor.h" |
| 24 |
#include "asterisk/astobj2.h" |
| 25 |
#include "asterisk/utils.h" |
| 26 |
|
| 27 |
|
| 28 |
#define THREAD_BUCKETS 89 |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
struct ast_threadpool { |
| 37 |
|
| 38 |
struct ast_threadpool_listener *listener; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
struct ao2_container *active_threads; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
struct ao2_container *idle_threads; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
struct ao2_container *zombie_threads; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
struct ast_taskprocessor *tps; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
struct ast_taskprocessor *control_tps; |
| 97 |
|
| 98 |
int shutting_down; |
| 99 |
|
| 100 |
struct ast_threadpool_options options; |
| 101 |
}; |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
struct ast_threadpool_listener { |
| 111 |
|
| 112 |
const struct ast_threadpool_listener_callbacks *callbacks; |
| 113 |
|
| 114 |
void *user_data; |
| 115 |
}; |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
enum worker_state { |
| 121 |
|
| 122 |
ALIVE, |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
ZOMBIE, |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
DEAD, |
| 138 |
}; |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
struct worker_thread { |
| 144 |
|
| 145 |
int id; |
| 146 |
|
| 147 |
ast_cond_t cond; |
| 148 |
|
| 149 |
ast_mutex_t lock; |
| 150 |
|
| 151 |
pthread_t thread; |
| 152 |
|
| 153 |
struct ast_threadpool *pool; |
| 154 |
|
| 155 |
enum worker_state state; |
| 156 |
|
| 157 |
int wake_up; |
| 158 |
|
| 159 |
struct ast_threadpool_options options; |
| 160 |
}; |
| 161 |
|
| 162 |
|
| 163 |
static int worker_thread_hash(const void *obj, int flags); |
| 164 |
static int worker_thread_cmp(void *obj, void *arg, int flags); |
| 165 |
static void worker_thread_destroy(void *obj); |
| 166 |
static void worker_active(struct worker_thread *worker); |
| 167 |
static void *worker_start(void *arg); |
| 168 |
static struct worker_thread *worker_thread_alloc(struct ast_threadpool *pool); |
| 169 |
static int worker_thread_start(struct worker_thread *worker); |
| 170 |
static int worker_idle(struct worker_thread *worker); |
| 171 |
static int worker_set_state(struct worker_thread *worker, enum worker_state state); |
| 172 |
static void worker_shutdown(struct worker_thread *worker); |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
static void threadpool_send_state_changed(struct ast_threadpool *pool) |
| 181 |
{ |
| 182 |
int active_size = ao2_container_count(pool->active_threads); |
| 183 |
int idle_size = ao2_container_count(pool->idle_threads); |
| 184 |
|
| 185 |
if (pool->listener && pool->listener->callbacks->state_changed) { |
| 186 |
pool->listener->callbacks->state_changed(pool, pool->listener, active_size, idle_size); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
struct thread_worker_pair { |
| 194 |
|
| 195 |
struct ast_threadpool *pool; |
| 196 |
|
| 197 |
struct worker_thread *worker; |
| 198 |
}; |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
static void thread_worker_pair_free(struct thread_worker_pair *pair) |
| 204 |
{ |
| 205 |
ao2_ref(pair->worker, -1); |
| 206 |
ast_free(pair); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
static struct thread_worker_pair *thread_worker_pair_alloc(struct ast_threadpool *pool, |
| 215 |
struct worker_thread *worker) |
| 216 |
{ |
| 217 |
struct thread_worker_pair *pair = ast_malloc(sizeof(*pair)); |
| 218 |
if (!pair) { |
| 219 |
return NULL; |
| 220 |
} |
| 221 |
pair->pool = pool; |
| 222 |
ao2_ref(worker, +1); |
| 223 |
pair->worker = worker; |
| 224 |
|
| 225 |
return pair; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
static int queued_active_thread_idle(void *data) |
| 236 |
{ |
| 237 |
struct thread_worker_pair *pair = data; |
| 238 |
|
| 239 |
ao2_link(pair->pool->idle_threads, pair->worker); |
| 240 |
ao2_unlink(pair->pool->active_threads, pair->worker); |
| 241 |
|
| 242 |
threadpool_send_state_changed(pair->pool); |
| 243 |
|
| 244 |
thread_worker_pair_free(pair); |
| 245 |
return 0; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
static void threadpool_active_thread_idle(struct ast_threadpool *pool, |
| 257 |
struct worker_thread *worker) |
| 258 |
{ |
| 259 |
struct thread_worker_pair *pair; |
| 260 |
SCOPED_AO2LOCK(lock, pool); |
| 261 |
|
| 262 |
if (pool->shutting_down) { |
| 263 |
return; |
| 264 |
} |
| 265 |
|
| 266 |
pair = thread_worker_pair_alloc(pool, worker); |
| 267 |
if (!pair) { |
| 268 |
return; |
| 269 |
} |
| 270 |
|
| 271 |
if (ast_taskprocessor_push(pool->control_tps, queued_active_thread_idle, pair)) { |
| 272 |
thread_worker_pair_free(pair); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
static int queued_zombie_thread_dead(void *data) |
| 285 |
{ |
| 286 |
struct thread_worker_pair *pair = data; |
| 287 |
|
| 288 |
ao2_unlink(pair->pool->zombie_threads, pair->worker); |
| 289 |
threadpool_send_state_changed(pair->pool); |
| 290 |
|
| 291 |
thread_worker_pair_free(pair); |
| 292 |
return 0; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
static void threadpool_zombie_thread_dead(struct ast_threadpool *pool, |
| 302 |
struct worker_thread *worker) |
| 303 |
{ |
| 304 |
struct thread_worker_pair *pair; |
| 305 |
SCOPED_AO2LOCK(lock, pool); |
| 306 |
|
| 307 |
if (pool->shutting_down) { |
| 308 |
return; |
| 309 |
} |
| 310 |
|
| 311 |
pair = thread_worker_pair_alloc(pool, worker); |
| 312 |
if (!pair) { |
| 313 |
return; |
| 314 |
} |
| 315 |
|
| 316 |
if (ast_taskprocessor_push(pool->control_tps, queued_zombie_thread_dead, pair)) { |
| 317 |
thread_worker_pair_free(pair); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
static int queued_idle_thread_dead(void *data) |
| 322 |
{ |
| 323 |
struct thread_worker_pair *pair = data; |
| 324 |
|
| 325 |
ao2_unlink(pair->pool->idle_threads, pair->worker); |
| 326 |
threadpool_send_state_changed(pair->pool); |
| 327 |
|
| 328 |
thread_worker_pair_free(pair); |
| 329 |
return 0; |
| 330 |
} |
| 331 |
|
| 332 |
static void threadpool_idle_thread_dead(struct ast_threadpool *pool, |
| 333 |
struct worker_thread *worker) |
| 334 |
{ |
| 335 |
struct thread_worker_pair *pair; |
| 336 |
SCOPED_AO2LOCK(lock, pool); |
| 337 |
|
| 338 |
if (pool->shutting_down) { |
| 339 |
return; |
| 340 |
} |
| 341 |
|
| 342 |
pair = thread_worker_pair_alloc(pool, worker); |
| 343 |
if (!pair) { |
| 344 |
return; |
| 345 |
} |
| 346 |
|
| 347 |
if (ast_taskprocessor_push(pool->control_tps, queued_idle_thread_dead, pair)) { |
| 348 |
thread_worker_pair_free(pair); |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
static int threadpool_execute(struct ast_threadpool *pool) |
| 363 |
{ |
| 364 |
ao2_lock(pool); |
| 365 |
if (!pool->shutting_down) { |
| 366 |
ao2_unlock(pool); |
| 367 |
return ast_taskprocessor_execute(pool->tps); |
| 368 |
} |
| 369 |
ao2_unlock(pool); |
| 370 |
return 0; |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
static void threadpool_destructor(void *obj) |
| 387 |
{ |
| 388 |
struct ast_threadpool *pool = obj; |
| 389 |
ao2_cleanup(pool->listener); |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
static struct ast_threadpool *threadpool_alloc(const char *name, const struct ast_threadpool_options *options) |
| 405 |
{ |
| 406 |
RAII_VAR(struct ast_threadpool *, pool, NULL, ao2_cleanup); |
| 407 |
struct ast_str *control_tps_name; |
| 408 |
|
| 409 |
pool = ao2_alloc(sizeof(*pool), threadpool_destructor); |
| 410 |
control_tps_name = ast_str_create(64); |
| 411 |
if (!pool || !control_tps_name) { |
| 412 |
ast_free(control_tps_name); |
| 413 |
return NULL; |
| 414 |
} |
| 415 |
|
| 416 |
ast_str_set(&control_tps_name, 0, "%s/pool-control", name); |
| 417 |
|
| 418 |
pool->control_tps = ast_taskprocessor_get(ast_str_buffer(control_tps_name), TPS_REF_DEFAULT); |
| 419 |
ast_free(control_tps_name); |
| 420 |
if (!pool->control_tps) { |
| 421 |
return NULL; |
| 422 |
} |
| 423 |
pool->active_threads = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, |
| 424 |
THREAD_BUCKETS, worker_thread_hash, NULL, worker_thread_cmp); |
| 425 |
if (!pool->active_threads) { |
| 426 |
return NULL; |
| 427 |
} |
| 428 |
pool->idle_threads = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, |
| 429 |
THREAD_BUCKETS, worker_thread_hash, NULL, worker_thread_cmp); |
| 430 |
if (!pool->idle_threads) { |
| 431 |
return NULL; |
| 432 |
} |
| 433 |
pool->zombie_threads = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, |
| 434 |
THREAD_BUCKETS, worker_thread_hash, NULL, worker_thread_cmp); |
| 435 |
if (!pool->zombie_threads) { |
| 436 |
return NULL; |
| 437 |
} |
| 438 |
pool->options = *options; |
| 439 |
|
| 440 |
ao2_ref(pool, +1); |
| 441 |
return pool; |
| 442 |
} |
| 443 |
|
| 444 |
static int threadpool_tps_start(struct ast_taskprocessor_listener *listener) |
| 445 |
{ |
| 446 |
return 0; |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
struct task_pushed_data { |
| 453 |
|
| 454 |
struct ast_threadpool *pool; |
| 455 |
|
| 456 |
int was_empty; |
| 457 |
}; |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
static struct task_pushed_data *task_pushed_data_alloc(struct ast_threadpool *pool, |
| 467 |
int was_empty) |
| 468 |
{ |
| 469 |
struct task_pushed_data *tpd = ast_malloc(sizeof(*tpd)); |
| 470 |
|
| 471 |
if (!tpd) { |
| 472 |
return NULL; |
| 473 |
} |
| 474 |
tpd->pool = pool; |
| 475 |
tpd->was_empty = was_empty; |
| 476 |
return tpd; |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
static int activate_thread(void *obj, void *arg, int flags) |
| 493 |
{ |
| 494 |
struct worker_thread *worker = obj; |
| 495 |
struct ast_threadpool *pool = arg; |
| 496 |
|
| 497 |
if (!ao2_link(pool->active_threads, worker)) { |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
ast_log(LOG_WARNING, "Failed to activate thread %d. Remaining idle\n", |
| 502 |
worker->id); |
| 503 |
return 0; |
| 504 |
} |
| 505 |
|
| 506 |
if (worker_set_state(worker, ALIVE)) { |
| 507 |
ast_debug(1, "Failed to activate thread %d. It is dead\n", |
| 508 |
worker->id); |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
ao2_unlink(pool->active_threads, worker); |
| 513 |
} |
| 514 |
|
| 515 |
return CMP_MATCH; |
| 516 |
} |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
static void grow(struct ast_threadpool *pool, int delta) |
| 526 |
{ |
| 527 |
int i; |
| 528 |
|
| 529 |
int current_size = ao2_container_count(pool->active_threads) + |
| 530 |
ao2_container_count(pool->idle_threads); |
| 531 |
|
| 532 |
if (pool->options.max_size && current_size + delta > pool->options.max_size) { |
| 533 |
delta = pool->options.max_size - current_size; |
| 534 |
} |
| 535 |
|
| 536 |
ast_debug(3, "Increasing threadpool %s's size by %d\n", |
| 537 |
ast_taskprocessor_name(pool->tps), delta); |
| 538 |
|
| 539 |
for (i = 0; i < delta; ++i) { |
| 540 |
struct worker_thread *worker = worker_thread_alloc(pool); |
| 541 |
if (!worker) { |
| 542 |
return; |
| 543 |
} |
| 544 |
if (ao2_link(pool->idle_threads, worker)) { |
| 545 |
if (worker_thread_start(worker)) { |
| 546 |
ast_log(LOG_ERROR, "Unable to start worker thread %d. Destroying.\n", worker->id); |
| 547 |
ao2_unlink(pool->active_threads, worker); |
| 548 |
} |
| 549 |
} else { |
| 550 |
ast_log(LOG_WARNING, "Failed to activate worker thread %d. Destroying.\n", worker->id); |
| 551 |
} |
| 552 |
ao2_ref(worker, -1); |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
static int queued_task_pushed(void *data) |
| 566 |
{ |
| 567 |
struct task_pushed_data *tpd = data; |
| 568 |
struct ast_threadpool *pool = tpd->pool; |
| 569 |
int was_empty = tpd->was_empty; |
| 570 |
unsigned int existing_active; |
| 571 |
|
| 572 |
ast_free(tpd); |
| 573 |
|
| 574 |
if (pool->listener && pool->listener->callbacks->task_pushed) { |
| 575 |
pool->listener->callbacks->task_pushed(pool, pool->listener, was_empty); |
| 576 |
} |
| 577 |
|
| 578 |
existing_active = ao2_container_count(pool->active_threads); |
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
ao2_callback(pool->idle_threads, OBJ_UNLINK | OBJ_NOLOCK | OBJ_NODATA, |
| 585 |
activate_thread, pool); |
| 586 |
|
| 587 |
|
| 588 |
if (ao2_container_count(pool->active_threads) == existing_active) { |
| 589 |
if (!pool->options.auto_increment) { |
| 590 |
return 0; |
| 591 |
} |
| 592 |
grow(pool, pool->options.auto_increment); |
| 593 |
|
| 594 |
ao2_callback(pool->idle_threads, OBJ_UNLINK | OBJ_NOLOCK | OBJ_NODATA, |
| 595 |
activate_thread, pool); |
| 596 |
} |
| 597 |
|
| 598 |
threadpool_send_state_changed(pool); |
| 599 |
return 0; |
| 600 |
} |
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
static void threadpool_tps_task_pushed(struct ast_taskprocessor_listener *listener, int was_empty) |
| 612 |
{ |
| 613 |
struct ast_threadpool *pool = ast_taskprocessor_listener_get_user_data(listener); |
| 614 |
struct task_pushed_data *tpd; |
| 615 |
SCOPED_AO2LOCK(lock, pool); |
| 616 |
|
| 617 |
if (pool->shutting_down) { |
| 618 |
return; |
| 619 |
} |
| 620 |
|
| 621 |
tpd = task_pushed_data_alloc(pool, was_empty); |
| 622 |
if (!tpd) { |
| 623 |
return; |
| 624 |
} |
| 625 |
|
| 626 |
if (ast_taskprocessor_push(pool->control_tps, queued_task_pushed, tpd)) { |
| 627 |
ast_free(tpd); |
| 628 |
} |
| 629 |
} |
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
static int queued_emptied(void *data) |
| 639 |
{ |
| 640 |
struct ast_threadpool *pool = data; |
| 641 |
|
| 642 |
|
| 643 |
pool->listener->callbacks->emptied(pool, pool->listener); |
| 644 |
return 0; |
| 645 |
} |
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
static void threadpool_tps_emptied(struct ast_taskprocessor_listener *listener) |
| 655 |
{ |
| 656 |
struct ast_threadpool *pool = ast_taskprocessor_listener_get_user_data(listener); |
| 657 |
SCOPED_AO2LOCK(lock, pool); |
| 658 |
|
| 659 |
if (pool->shutting_down) { |
| 660 |
return; |
| 661 |
} |
| 662 |
|
| 663 |
if (pool->listener && pool->listener->callbacks->emptied) { |
| 664 |
if (ast_taskprocessor_push(pool->control_tps, queued_emptied, pool)) { |
| 665 |
|
| 666 |
} |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
static void threadpool_tps_shutdown(struct ast_taskprocessor_listener *listener) |
| 680 |
{ |
| 681 |
struct ast_threadpool *pool = ast_taskprocessor_listener_get_user_data(listener); |
| 682 |
|
| 683 |
if (pool->listener && pool->listener->callbacks->shutdown) { |
| 684 |
pool->listener->callbacks->shutdown(pool->listener); |
| 685 |
} |
| 686 |
ao2_cleanup(pool->active_threads); |
| 687 |
ao2_cleanup(pool->idle_threads); |
| 688 |
ao2_cleanup(pool->zombie_threads); |
| 689 |
ao2_cleanup(pool); |
| 690 |
} |
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
static struct ast_taskprocessor_listener_callbacks threadpool_tps_listener_callbacks = { |
| 696 |
.start = threadpool_tps_start, |
| 697 |
.task_pushed = threadpool_tps_task_pushed, |
| 698 |
.emptied = threadpool_tps_emptied, |
| 699 |
.shutdown = threadpool_tps_shutdown, |
| 700 |
}; |
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
static int kill_threads(void *obj, void *arg, int flags) |
| 715 |
{ |
| 716 |
int *num_to_kill = arg; |
| 717 |
|
| 718 |
if (*num_to_kill > 0) { |
| 719 |
--(*num_to_kill); |
| 720 |
return CMP_MATCH; |
| 721 |
} else { |
| 722 |
return CMP_STOP; |
| 723 |
} |
| 724 |
} |
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
static int zombify_threads(void *obj, void *arg, void *data, int flags) |
| 746 |
{ |
| 747 |
struct worker_thread *worker = obj; |
| 748 |
struct ast_threadpool *pool = arg; |
| 749 |
int *num_to_zombify = data; |
| 750 |
|
| 751 |
if ((*num_to_zombify)-- > 0) { |
| 752 |
if (!ao2_link(pool->zombie_threads, worker)) { |
| 753 |
ast_log(LOG_WARNING, "Failed to zombify active thread %d. Thread will remain active\n", worker->id); |
| 754 |
return 0; |
| 755 |
} |
| 756 |
worker_set_state(worker, ZOMBIE); |
| 757 |
return CMP_MATCH; |
| 758 |
} else { |
| 759 |
return CMP_STOP; |
| 760 |
} |
| 761 |
} |
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
static void shrink(struct ast_threadpool *pool, int delta) |
| 776 |
{ |
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
int idle_threads = ao2_container_count(pool->idle_threads); |
| 783 |
int idle_threads_to_kill = MIN(delta, idle_threads); |
| 784 |
int active_threads_to_zombify = delta - idle_threads_to_kill; |
| 785 |
|
| 786 |
ast_debug(3, "Destroying %d idle threads in threadpool %s\n", idle_threads_to_kill, |
| 787 |
ast_taskprocessor_name(pool->tps)); |
| 788 |
|
| 789 |
ao2_callback(pool->idle_threads, OBJ_UNLINK | OBJ_NOLOCK | OBJ_NODATA | OBJ_MULTIPLE, |
| 790 |
kill_threads, &idle_threads_to_kill); |
| 791 |
|
| 792 |
ast_debug(3, "Destroying %d active threads in threadpool %s\n", active_threads_to_zombify, |
| 793 |
ast_taskprocessor_name(pool->tps)); |
| 794 |
|
| 795 |
ao2_callback_data(pool->active_threads, OBJ_UNLINK | OBJ_NOLOCK | OBJ_NODATA | OBJ_MULTIPLE, |
| 796 |
zombify_threads, pool, &active_threads_to_zombify); |
| 797 |
} |
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
struct set_size_data { |
| 803 |
|
| 804 |
struct ast_threadpool *pool; |
| 805 |
|
| 806 |
unsigned int size; |
| 807 |
}; |
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
static struct set_size_data *set_size_data_alloc(struct ast_threadpool *pool, |
| 815 |
unsigned int size) |
| 816 |
{ |
| 817 |
struct set_size_data *ssd = ast_malloc(sizeof(*ssd)); |
| 818 |
if (!ssd) { |
| 819 |
return NULL; |
| 820 |
} |
| 821 |
|
| 822 |
ssd->pool = pool; |
| 823 |
ssd->size = size; |
| 824 |
return ssd; |
| 825 |
} |
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
static int queued_set_size(void *data) |
| 839 |
{ |
| 840 |
struct set_size_data *ssd = data; |
| 841 |
struct ast_threadpool *pool = ssd->pool; |
| 842 |
unsigned int num_threads = ssd->size; |
| 843 |
|
| 844 |
|
| 845 |
unsigned int current_size = ao2_container_count(pool->active_threads) + |
| 846 |
ao2_container_count(pool->idle_threads); |
| 847 |
|
| 848 |
ast_free(ssd); |
| 849 |
|
| 850 |
if (current_size == num_threads) { |
| 851 |
ast_debug(3, "Not changing threadpool size since new size %u is the same as current %u\n", |
| 852 |
num_threads, current_size); |
| 853 |
return 0; |
| 854 |
} |
| 855 |
|
| 856 |
if (current_size < num_threads) { |
| 857 |
ao2_callback(pool->idle_threads, OBJ_UNLINK | OBJ_NOLOCK | OBJ_NODATA | OBJ_MULTIPLE, |
| 858 |
activate_thread, pool); |
| 859 |
|
| 860 |
|
| 861 |
current_size = ao2_container_count(pool->active_threads) + |
| 862 |
ao2_container_count(pool->idle_threads); |
| 863 |
grow(pool, num_threads - current_size); |
| 864 |
ao2_callback(pool->idle_threads, OBJ_UNLINK | OBJ_NOLOCK | OBJ_NODATA | OBJ_MULTIPLE, |
| 865 |
activate_thread, pool); |
| 866 |
} else { |
| 867 |
shrink(pool, current_size - num_threads); |
| 868 |
} |
| 869 |
|
| 870 |
threadpool_send_state_changed(pool); |
| 871 |
return 0; |
| 872 |
} |
| 873 |
|
| 874 |
void ast_threadpool_set_size(struct ast_threadpool *pool, unsigned int size) |
| 875 |
{ |
| 876 |
struct set_size_data *ssd; |
| 877 |
SCOPED_AO2LOCK(lock, pool); |
| 878 |
|
| 879 |
if (pool->shutting_down) { |
| 880 |
return; |
| 881 |
} |
| 882 |
|
| 883 |
ssd = set_size_data_alloc(pool, size); |
| 884 |
if (!ssd) { |
| 885 |
return; |
| 886 |
} |
| 887 |
|
| 888 |
if (ast_taskprocessor_push(pool->control_tps, queued_set_size, ssd)) { |
| 889 |
ast_free(ssd); |
| 890 |
} |
| 891 |
} |
| 892 |
|
| 893 |
struct ast_threadpool_listener *ast_threadpool_listener_alloc( |
| 894 |
const struct ast_threadpool_listener_callbacks *callbacks, void *user_data) |
| 895 |
{ |
| 896 |
struct ast_threadpool_listener *listener = ao2_alloc(sizeof(*listener), NULL); |
| 897 |
if (!listener) { |
| 898 |
return NULL; |
| 899 |
} |
| 900 |
listener->callbacks = callbacks; |
| 901 |
listener->user_data = user_data; |
| 902 |
return listener; |
| 903 |
} |
| 904 |
|
| 905 |
void *ast_threadpool_listener_get_user_data(const struct ast_threadpool_listener *listener) |
| 906 |
{ |
| 907 |
return listener->user_data; |
| 908 |
} |
| 909 |
|
| 910 |
struct pool_options_pair { |
| 911 |
struct ast_threadpool *pool; |
| 912 |
struct ast_threadpool_options options; |
| 913 |
}; |
| 914 |
|
| 915 |
struct ast_threadpool *ast_threadpool_create(const char *name, |
| 916 |
struct ast_threadpool_listener *listener, |
| 917 |
const struct ast_threadpool_options *options) |
| 918 |
{ |
| 919 |
struct ast_taskprocessor *tps; |
| 920 |
RAII_VAR(struct ast_taskprocessor_listener *, tps_listener, NULL, ao2_cleanup); |
| 921 |
RAII_VAR(struct ast_threadpool *, pool, NULL, ao2_cleanup); |
| 922 |
char *fullname; |
| 923 |
|
| 924 |
pool = threadpool_alloc(name, options); |
| 925 |
if (!pool) { |
| 926 |
return NULL; |
| 927 |
} |
| 928 |
|
| 929 |
tps_listener = ast_taskprocessor_listener_alloc(&threadpool_tps_listener_callbacks, pool); |
| 930 |
if (!tps_listener) { |
| 931 |
return NULL; |
| 932 |
} |
| 933 |
|
| 934 |
if (options->version != AST_THREADPOOL_OPTIONS_VERSION) { |
| 935 |
ast_log(LOG_WARNING, "Incompatible version of threadpool options in use.\n"); |
| 936 |
return NULL; |
| 937 |
} |
| 938 |
|
| 939 |
fullname = ast_alloca(strlen(name) + strlen("/pool") + 1); |
| 940 |
sprintf(fullname, "%s/pool", name); |
| 941 |
tps = ast_taskprocessor_create_with_listener(fullname, tps_listener); |
| 942 |
if (!tps) { |
| 943 |
return NULL; |
| 944 |
} |
| 945 |
|
| 946 |
pool->tps = tps; |
| 947 |
if (listener) { |
| 948 |
ao2_ref(listener, +1); |
| 949 |
pool->listener = listener; |
| 950 |
} |
| 951 |
ast_threadpool_set_size(pool, pool->options.initial_size); |
| 952 |
ao2_ref(pool, +1); |
| 953 |
return pool; |
| 954 |
} |
| 955 |
|
| 956 |
#undef ast_threadpool_push |
| 957 |
#define ast_threadpool_push_internal(pool, task, data) \ |
| 958 |
__ast_threadpool_push(pool, task, data, __FILE__, __LINE__, __PRETTY_FUNCTION__) |
| 959 |
int ast_threadpool_push(struct ast_threadpool *pool, int (*task)(void *data), void *data); |
| 960 |
|
| 961 |
int __ast_threadpool_push(struct ast_threadpool *pool, int (*task)(void *data), void *data, |
| 962 |
const char *file, int line, const char *function) |
| 963 |
{ |
| 964 |
SCOPED_AO2LOCK(lock, pool); |
| 965 |
if (!pool->shutting_down) { |
| 966 |
return __ast_taskprocessor_push(pool->tps, task, data, file, line, function); |
| 967 |
} |
| 968 |
return -1; |
| 969 |
} |
| 970 |
|
| 971 |
|
| 972 |
int ast_threadpool_push(struct ast_threadpool *pool, int (*task)(void *data), void *data) |
| 973 |
{ |
| 974 |
return __ast_threadpool_push(pool, task, data, NULL, 0, NULL); |
| 975 |
} |
| 976 |
|
| 977 |
void ast_threadpool_shutdown(struct ast_threadpool *pool) |
| 978 |
{ |
| 979 |
if (!pool) { |
| 980 |
return; |
| 981 |
} |
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
ao2_lock(pool); |
| 986 |
pool->shutting_down = 1; |
| 987 |
ao2_unlock(pool); |
| 988 |
ast_taskprocessor_unreference(pool->control_tps); |
| 989 |
ast_taskprocessor_unreference(pool->tps); |
| 990 |
} |
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
static int worker_id_counter; |
| 997 |
|
| 998 |
static int worker_thread_hash(const void *obj, int flags) |
| 999 |
{ |
| 1000 |
const struct worker_thread *worker = obj; |
| 1001 |
|
| 1002 |
return worker->id; |
| 1003 |
} |
| 1004 |
|
| 1005 |
static int worker_thread_cmp(void *obj, void *arg, int flags) |
| 1006 |
{ |
| 1007 |
struct worker_thread *worker1 = obj; |
| 1008 |
struct worker_thread *worker2 = arg; |
| 1009 |
|
| 1010 |
return worker1->id == worker2->id ? CMP_MATCH : 0; |
| 1011 |
} |
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
static void worker_shutdown(struct worker_thread *worker) |
| 1022 |
{ |
| 1023 |
worker_set_state(worker, DEAD); |
| 1024 |
if (worker->thread != AST_PTHREADT_NULL) { |
| 1025 |
pthread_join(worker->thread, NULL); |
| 1026 |
worker->thread = AST_PTHREADT_NULL; |
| 1027 |
} |
| 1028 |
} |
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
static void worker_thread_destroy(void *obj) |
| 1038 |
{ |
| 1039 |
struct worker_thread *worker = obj; |
| 1040 |
ast_debug(3, "Destroying worker thread %d\n", worker->id); |
| 1041 |
worker_shutdown(worker); |
| 1042 |
ast_mutex_destroy(&worker->lock); |
| 1043 |
ast_cond_destroy(&worker->cond); |
| 1044 |
} |
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
static void *worker_start(void *arg) |
| 1056 |
{ |
| 1057 |
struct worker_thread *worker = arg; |
| 1058 |
enum worker_state saved_state; |
| 1059 |
|
| 1060 |
if (worker->options.thread_start) { |
| 1061 |
worker->options.thread_start(); |
| 1062 |
} |
| 1063 |
|
| 1064 |
ast_mutex_lock(&worker->lock); |
| 1065 |
while (worker_idle(worker)) { |
| 1066 |
ast_mutex_unlock(&worker->lock); |
| 1067 |
worker_active(worker); |
| 1068 |
ast_mutex_lock(&worker->lock); |
| 1069 |
if (worker->state != ALIVE) { |
| 1070 |
break; |
| 1071 |
} |
| 1072 |
threadpool_active_thread_idle(worker->pool, worker); |
| 1073 |
} |
| 1074 |
saved_state = worker->state; |
| 1075 |
ast_mutex_unlock(&worker->lock); |
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
if (saved_state == ZOMBIE) { |
| 1086 |
threadpool_zombie_thread_dead(worker->pool, worker); |
| 1087 |
} |
| 1088 |
|
| 1089 |
if (worker->options.thread_end) { |
| 1090 |
worker->options.thread_end(); |
| 1091 |
} |
| 1092 |
return NULL; |
| 1093 |
} |
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
static struct worker_thread *worker_thread_alloc(struct ast_threadpool *pool) |
| 1105 |
{ |
| 1106 |
struct worker_thread *worker = ao2_alloc(sizeof(*worker), worker_thread_destroy); |
| 1107 |
if (!worker) { |
| 1108 |
return NULL; |
| 1109 |
} |
| 1110 |
worker->id = ast_atomic_fetchadd_int(&worker_id_counter, 1); |
| 1111 |
ast_mutex_init(&worker->lock); |
| 1112 |
ast_cond_init(&worker->cond, NULL); |
| 1113 |
worker->pool = pool; |
| 1114 |
worker->thread = AST_PTHREADT_NULL; |
| 1115 |
worker->state = ALIVE; |
| 1116 |
worker->options = pool->options; |
| 1117 |
return worker; |
| 1118 |
} |
| 1119 |
|
| 1120 |
static int worker_thread_start(struct worker_thread *worker) |
| 1121 |
{ |
| 1122 |
return ast_pthread_create(&worker->thread, NULL, worker_start, worker); |
| 1123 |
} |
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
static void worker_active(struct worker_thread *worker) |
| 1136 |
{ |
| 1137 |
int alive; |
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
do { |
| 1148 |
alive = threadpool_execute(worker->pool); |
| 1149 |
} while (alive); |
| 1150 |
} |
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
static int worker_idle(struct worker_thread *worker) |
| 1165 |
{ |
| 1166 |
struct timeval start = ast_tvnow(); |
| 1167 |
struct timespec end = { |
| 1168 |
.tv_sec = start.tv_sec + worker->options.idle_timeout, |
| 1169 |
.tv_nsec = start.tv_usec * 1000, |
| 1170 |
}; |
| 1171 |
while (!worker->wake_up) { |
| 1172 |
if (worker->options.idle_timeout <= 0) { |
| 1173 |
ast_cond_wait(&worker->cond, &worker->lock); |
| 1174 |
} else if (ast_cond_timedwait(&worker->cond, &worker->lock, &end) == ETIMEDOUT) { |
| 1175 |
break; |
| 1176 |
} |
| 1177 |
} |
| 1178 |
|
| 1179 |
if (!worker->wake_up) { |
| 1180 |
ast_debug(1, "Worker thread idle timeout reached. Dying.\n"); |
| 1181 |
threadpool_idle_thread_dead(worker->pool, worker); |
| 1182 |
worker->state = DEAD; |
| 1183 |
} |
| 1184 |
worker->wake_up = 0; |
| 1185 |
return worker->state == ALIVE; |
| 1186 |
} |
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
static int worker_set_state(struct worker_thread *worker, enum worker_state state) |
| 1198 |
{ |
| 1199 |
SCOPED_MUTEX(lock, &worker->lock); |
| 1200 |
|
| 1201 |
switch (state) { |
| 1202 |
case ALIVE: |
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
if (worker->state == DEAD) { |
| 1207 |
return -1; |
| 1208 |
} |
| 1209 |
ast_assert(worker->state != ZOMBIE); |
| 1210 |
break; |
| 1211 |
case DEAD: |
| 1212 |
break; |
| 1213 |
case ZOMBIE: |
| 1214 |
ast_assert(worker->state != DEAD); |
| 1215 |
break; |
| 1216 |
} |
| 1217 |
|
| 1218 |
worker->state = state; |
| 1219 |
worker->wake_up = 1; |
| 1220 |
ast_cond_signal(&worker->cond); |
| 1221 |
|
| 1222 |
return 0; |
| 1223 |
} |
| 1224 |
|
| 1225 |
struct serializer { |
| 1226 |
|
| 1227 |
struct ast_threadpool *pool; |
| 1228 |
|
| 1229 |
struct ast_serializer_shutdown_group *shutdown_group; |
| 1230 |
}; |
| 1231 |
|
| 1232 |
static void serializer_dtor(void *obj) |
| 1233 |
{ |
| 1234 |
struct serializer *ser = obj; |
| 1235 |
|
| 1236 |
ao2_cleanup(ser->pool); |
| 1237 |
ser->pool = NULL; |
| 1238 |
ao2_cleanup(ser->shutdown_group); |
| 1239 |
ser->shutdown_group = NULL; |
| 1240 |
} |
| 1241 |
|
| 1242 |
static struct serializer *serializer_create(struct ast_threadpool *pool, |
| 1243 |
struct ast_serializer_shutdown_group *shutdown_group) |
| 1244 |
{ |
| 1245 |
struct serializer *ser; |
| 1246 |
|
| 1247 |
ser = ao2_alloc_options(sizeof(*ser), serializer_dtor, AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 1248 |
if (!ser) { |
| 1249 |
return NULL; |
| 1250 |
} |
| 1251 |
ao2_ref(pool, +1); |
| 1252 |
ser->pool = pool; |
| 1253 |
ser->shutdown_group = ao2_bump(shutdown_group); |
| 1254 |
return ser; |
| 1255 |
} |
| 1256 |
|
| 1257 |
AST_THREADSTORAGE_RAW(current_serializer); |
| 1258 |
|
| 1259 |
static int execute_tasks(void *data) |
| 1260 |
{ |
| 1261 |
struct ast_taskprocessor *tps = data; |
| 1262 |
|
| 1263 |
ast_threadstorage_set_ptr(¤t_serializer, tps); |
| 1264 |
while (ast_taskprocessor_execute(tps)) { |
| 1265 |
|
| 1266 |
} |
| 1267 |
ast_threadstorage_set_ptr(¤t_serializer, NULL); |
| 1268 |
|
| 1269 |
ast_taskprocessor_unreference(tps); |
| 1270 |
return 0; |
| 1271 |
} |
| 1272 |
|
| 1273 |
static void serializer_task_pushed(struct ast_taskprocessor_listener *listener, int was_empty) |
| 1274 |
{ |
| 1275 |
if (was_empty) { |
| 1276 |
struct serializer *ser = ast_taskprocessor_listener_get_user_data(listener); |
| 1277 |
struct ast_taskprocessor *tps = ast_taskprocessor_listener_get_tps(listener); |
| 1278 |
|
| 1279 |
if (ast_threadpool_push(ser->pool, execute_tasks, tps)) { |
| 1280 |
ast_taskprocessor_unreference(tps); |
| 1281 |
} |
| 1282 |
} |
| 1283 |
} |
| 1284 |
|
| 1285 |
static int serializer_start(struct ast_taskprocessor_listener *listener) |
| 1286 |
{ |
| 1287 |
|
| 1288 |
return 0; |
| 1289 |
} |
| 1290 |
|
| 1291 |
static void serializer_shutdown(struct ast_taskprocessor_listener *listener) |
| 1292 |
{ |
| 1293 |
struct serializer *ser = ast_taskprocessor_listener_get_user_data(listener); |
| 1294 |
|
| 1295 |
if (ser->shutdown_group) { |
| 1296 |
ast_serializer_shutdown_group_dec(ser->shutdown_group); |
| 1297 |
} |
| 1298 |
ao2_cleanup(ser); |
| 1299 |
} |
| 1300 |
|
| 1301 |
static struct ast_taskprocessor_listener_callbacks serializer_tps_listener_callbacks = { |
| 1302 |
.task_pushed = serializer_task_pushed, |
| 1303 |
.start = serializer_start, |
| 1304 |
.shutdown = serializer_shutdown, |
| 1305 |
}; |
| 1306 |
|
| 1307 |
struct ast_taskprocessor *ast_threadpool_serializer_get_current(void) |
| 1308 |
{ |
| 1309 |
return ast_threadstorage_get_ptr(¤t_serializer); |
| 1310 |
} |
| 1311 |
|
| 1312 |
struct ast_taskprocessor *ast_threadpool_serializer_group(const char *name, |
| 1313 |
struct ast_threadpool *pool, struct ast_serializer_shutdown_group *shutdown_group) |
| 1314 |
{ |
| 1315 |
struct serializer *ser; |
| 1316 |
struct ast_taskprocessor_listener *listener; |
| 1317 |
struct ast_taskprocessor *tps; |
| 1318 |
|
| 1319 |
ser = serializer_create(pool, shutdown_group); |
| 1320 |
if (!ser) { |
| 1321 |
return NULL; |
| 1322 |
} |
| 1323 |
|
| 1324 |
listener = ast_taskprocessor_listener_alloc(&serializer_tps_listener_callbacks, ser); |
| 1325 |
if (!listener) { |
| 1326 |
ao2_ref(ser, -1); |
| 1327 |
return NULL; |
| 1328 |
} |
| 1329 |
|
| 1330 |
tps = ast_taskprocessor_create_with_listener(name, listener); |
| 1331 |
if (!tps) { |
| 1332 |
|
| 1333 |
ao2_ref(ser, -1); |
| 1334 |
} else if (shutdown_group) { |
| 1335 |
ast_serializer_shutdown_group_inc(shutdown_group); |
| 1336 |
} |
| 1337 |
|
| 1338 |
ao2_ref(listener, -1); |
| 1339 |
return tps; |
| 1340 |
} |
| 1341 |
|
| 1342 |
struct ast_taskprocessor *ast_threadpool_serializer(const char *name, struct ast_threadpool *pool) |
| 1343 |
{ |
| 1344 |
return ast_threadpool_serializer_group(name, pool, NULL); |
| 1345 |
} |
| 1346 |
|
| 1347 |
long ast_threadpool_queue_size(struct ast_threadpool *pool) |
| 1348 |
{ |
| 1349 |
return ast_taskprocessor_size(pool->tps); |
| 1350 |
} |