| 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/_private.h" |
| 23 |
#include "asterisk/taskpool.h" |
| 24 |
#include "asterisk/taskprocessor.h" |
| 25 |
#include "asterisk/astobj2.h" |
| 26 |
#include "asterisk/serializer_shutdown_group.h" |
| 27 |
#include "asterisk/utils.h" |
| 28 |
#include "asterisk/time.h" |
| 29 |
#include "asterisk/sched.h" |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
struct taskpool_taskprocessor { |
| 35 |
|
| 36 |
struct ast_taskprocessor *taskprocessor; |
| 37 |
|
| 38 |
struct timeval last_pushed; |
| 39 |
}; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
struct taskpool_taskprocessors { |
| 45 |
|
| 46 |
AST_VECTOR(, struct taskpool_taskprocessor *) taskprocessors; |
| 47 |
|
| 48 |
unsigned int taskprocessor_num; |
| 49 |
}; |
| 50 |
|
| 51 |
typedef void (*taskpool_selector)(struct ast_taskpool *pool, struct taskpool_taskprocessors *taskprocessors, |
| 52 |
struct taskpool_taskprocessor **taskprocessor, unsigned int *growth_threshold_reached); |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
struct ast_taskpool { |
| 63 |
|
| 64 |
struct taskpool_taskprocessors static_taskprocessors; |
| 65 |
|
| 66 |
struct taskpool_taskprocessors dynamic_taskprocessors; |
| 67 |
|
| 68 |
int shutting_down; |
| 69 |
|
| 70 |
struct ast_taskpool_options options; |
| 71 |
|
| 72 |
int shrink_sched_id; |
| 73 |
|
| 74 |
taskpool_selector selector; |
| 75 |
|
| 76 |
char name[0]; |
| 77 |
}; |
| 78 |
|
| 79 |
|
| 80 |
#define TASKPOOL_GROW_THRESHOLD (AST_TASKPROCESSOR_HIGH_WATER_LEVEL * 5) / 10 |
| 81 |
|
| 82 |
|
| 83 |
static struct ast_sched_context *sched; |
| 84 |
|
| 85 |
|
| 86 |
AST_THREADSTORAGE_RAW(current_taskpool_pool); |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
static struct ast_taskpool *ast_taskpool_get_current(void) |
| 93 |
{ |
| 94 |
return ast_threadstorage_get_ptr(¤t_taskpool_pool); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
static int taskpool_taskprocessor_stop(void *data) |
| 102 |
{ |
| 103 |
struct ast_taskpool *pool = ast_taskpool_get_current(); |
| 104 |
|
| 105 |
|
| 106 |
if (pool->options.thread_end) { |
| 107 |
pool->options.thread_end(); |
| 108 |
} |
| 109 |
|
| 110 |
ao2_cleanup(pool); |
| 111 |
|
| 112 |
return 0; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
static void taskpool_taskprocessor_dtor(void *obj) |
| 117 |
{ |
| 118 |
struct taskpool_taskprocessor *taskprocessor = obj; |
| 119 |
|
| 120 |
if (taskprocessor->taskprocessor && ast_taskprocessor_push(taskprocessor->taskprocessor, taskpool_taskprocessor_stop, NULL)) { |
| 121 |
|
| 122 |
} |
| 123 |
|
| 124 |
ast_taskprocessor_unreference(taskprocessor->taskprocessor); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
static int taskpool_taskprocessor_start(void *data) |
| 132 |
{ |
| 133 |
struct ast_taskpool *pool = data; |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
ast_threadstorage_set_ptr(¤t_taskpool_pool, pool); |
| 139 |
|
| 140 |
|
| 141 |
if (pool->options.thread_start) { |
| 142 |
pool->options.thread_start(); |
| 143 |
} |
| 144 |
|
| 145 |
return 0; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
static struct taskpool_taskprocessor *taskpool_taskprocessor_alloc(struct ast_taskpool *pool, char type) |
| 153 |
{ |
| 154 |
struct taskpool_taskprocessor *taskprocessor; |
| 155 |
char tps_name[AST_TASKPROCESSOR_MAX_NAME + 1]; |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
taskprocessor = ao2_alloc_options(sizeof(*taskprocessor), taskpool_taskprocessor_dtor, AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 161 |
if (!taskprocessor) { |
| 162 |
return NULL; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
ast_taskprocessor_build_name(tps_name, sizeof(tps_name), "taskpool/%c:%s", type, pool->name); |
| 167 |
|
| 168 |
taskprocessor->taskprocessor = ast_taskprocessor_get(tps_name, TPS_REF_DEFAULT); |
| 169 |
if (!taskprocessor->taskprocessor) { |
| 170 |
ao2_ref(taskprocessor, -1); |
| 171 |
return NULL; |
| 172 |
} |
| 173 |
|
| 174 |
taskprocessor->last_pushed = ast_tvnow(); |
| 175 |
|
| 176 |
if (ast_taskprocessor_push(taskprocessor->taskprocessor, taskpool_taskprocessor_start, ao2_bump(pool))) { |
| 177 |
ao2_ref(pool, -1); |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
ast_taskprocessor_unreference(taskprocessor->taskprocessor); |
| 182 |
taskprocessor->taskprocessor = NULL; |
| 183 |
return NULL; |
| 184 |
} |
| 185 |
|
| 186 |
return taskprocessor; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
static int taskpool_taskprocessors_init(struct taskpool_taskprocessors *taskprocessors, unsigned int size) |
| 194 |
{ |
| 195 |
if (AST_VECTOR_INIT(&taskprocessors->taskprocessors, size)) { |
| 196 |
return -1; |
| 197 |
} |
| 198 |
|
| 199 |
return 0; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
static void taskpool_taskprocessors_cleanup(struct taskpool_taskprocessors *taskprocessors) |
| 207 |
{ |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
AST_VECTOR_CALLBACK_VOID(&taskprocessors->taskprocessors, ao2_cleanup); |
| 214 |
AST_VECTOR_FREE(&taskprocessors->taskprocessors); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
#define TASKPROCESSOR_IS_IDLE(tps, timeout) (ast_tvdiff_ms(ast_tvnow(), tps->last_pushed) > (timeout)) |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
static int taskpool_dynamic_pool_shrink(const void *data) |
| 227 |
{ |
| 228 |
struct ast_taskpool *pool = (struct ast_taskpool *)data; |
| 229 |
int num_removed; |
| 230 |
|
| 231 |
ao2_lock(pool); |
| 232 |
|
| 233 |
|
| 234 |
if (pool->shutting_down) { |
| 235 |
ao2_unlock(pool); |
| 236 |
ao2_ref(pool, -1); |
| 237 |
return 0; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
num_removed = AST_VECTOR_REMOVE_ALL_CMP_UNORDERED(&pool->dynamic_taskprocessors.taskprocessors, pool->options.idle_timeout * 1000, |
| 242 |
TASKPROCESSOR_IS_IDLE, ao2_cleanup); |
| 243 |
if (num_removed) { |
| 244 |
|
| 245 |
if (pool->dynamic_taskprocessors.taskprocessor_num >= AST_VECTOR_SIZE(&pool->dynamic_taskprocessors.taskprocessors)) { |
| 246 |
pool->dynamic_taskprocessors.taskprocessor_num = 0; |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
ao2_unlock(pool); |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
return pool->options.idle_timeout * 1000; |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
static void taskpool_sequential_selector(struct ast_taskpool *pool, struct taskpool_taskprocessors *taskprocessors, |
| 263 |
struct taskpool_taskprocessor **taskprocessor, unsigned int *growth_threshold_reached) |
| 264 |
{ |
| 265 |
unsigned int taskprocessor_num = taskprocessors->taskprocessor_num; |
| 266 |
|
| 267 |
if (!AST_VECTOR_SIZE(&taskprocessors->taskprocessors)) { |
| 268 |
*growth_threshold_reached = 1; |
| 269 |
return; |
| 270 |
} |
| 271 |
|
| 272 |
taskprocessors->taskprocessor_num++; |
| 273 |
if (taskprocessors->taskprocessor_num == AST_VECTOR_SIZE(&taskprocessors->taskprocessors)) { |
| 274 |
taskprocessors->taskprocessor_num = 0; |
| 275 |
} |
| 276 |
|
| 277 |
*taskprocessor = AST_VECTOR_GET(&taskprocessors->taskprocessors, taskprocessor_num); |
| 278 |
|
| 279 |
|
| 280 |
*growth_threshold_reached = (ast_taskprocessor_size((*taskprocessor)->taskprocessor) >= pool->options.growth_threshold) ? 1 : 0; |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
static void taskpool_least_full_selector(struct ast_taskpool *pool, struct taskpool_taskprocessors *taskprocessors, |
| 288 |
struct taskpool_taskprocessor **taskprocessor, unsigned int *growth_threshold_reached) |
| 289 |
{ |
| 290 |
struct taskpool_taskprocessor *least_full = NULL; |
| 291 |
unsigned int i; |
| 292 |
|
| 293 |
if (!AST_VECTOR_SIZE(&taskprocessors->taskprocessors)) { |
| 294 |
*growth_threshold_reached = 1; |
| 295 |
return; |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
*growth_threshold_reached = 0; |
| 300 |
|
| 301 |
for (i = 0; i < AST_VECTOR_SIZE(&taskprocessors->taskprocessors); i++) { |
| 302 |
struct taskpool_taskprocessor *tp = AST_VECTOR_GET(&taskprocessors->taskprocessors, i); |
| 303 |
|
| 304 |
|
| 305 |
if (!ast_taskprocessor_size(tp->taskprocessor)) { |
| 306 |
*taskprocessor = tp; |
| 307 |
return; |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
if (ast_taskprocessor_size(tp->taskprocessor) >= pool->options.growth_threshold) { |
| 312 |
*growth_threshold_reached = 1; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
if (!least_full || ast_taskprocessor_size(tp->taskprocessor) < ast_taskprocessor_size(least_full->taskprocessor)) { |
| 317 |
least_full = tp; |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
*taskprocessor = least_full; |
| 322 |
} |
| 323 |
|
| 324 |
struct ast_taskpool *ast_taskpool_create(const char *name, |
| 325 |
const struct ast_taskpool_options *options) |
| 326 |
{ |
| 327 |
struct ast_taskpool *pool; |
| 328 |
|
| 329 |
|
| 330 |
if (options->version != AST_TASKPOOL_OPTIONS_VERSION) { |
| 331 |
return NULL; |
| 332 |
} |
| 333 |
|
| 334 |
pool = ao2_alloc(sizeof(*pool) + strlen(name) + 1, NULL); |
| 335 |
if (!pool) { |
| 336 |
return NULL; |
| 337 |
} |
| 338 |
|
| 339 |
strcpy(pool->name, name); |
| 340 |
memcpy(&pool->options, options, sizeof(pool->options)); |
| 341 |
pool->shrink_sched_id = -1; |
| 342 |
|
| 343 |
|
| 344 |
if (options->initial_size < options->minimum_size) { |
| 345 |
pool->options.initial_size = options->minimum_size; |
| 346 |
ast_log(LOG_WARNING, "Taskpool '%s' has an initial size of %d, which is less than the minimum size of %d. Adjusting to %d.\n", |
| 347 |
name, options->initial_size, options->minimum_size, options->minimum_size); |
| 348 |
} |
| 349 |
|
| 350 |
if (options->max_size && pool->options.initial_size > options->max_size) { |
| 351 |
pool->options.max_size = pool->options.initial_size; |
| 352 |
ast_log(LOG_WARNING, "Taskpool '%s' has a max size of %d, which is less than the initial size of %d. Adjusting to %d.\n", |
| 353 |
name, options->max_size, pool->options.initial_size, pool->options.initial_size); |
| 354 |
} |
| 355 |
|
| 356 |
if (!options->auto_increment) { |
| 357 |
if (!pool->options.minimum_size) { |
| 358 |
pool->options.minimum_size = 1; |
| 359 |
ast_log(LOG_WARNING, "Taskpool '%s' has a minimum size of 0, which is not valid without auto increment. Adjusting to 1.\n", name); |
| 360 |
} |
| 361 |
if (!pool->options.max_size) { |
| 362 |
pool->options.max_size = pool->options.minimum_size; |
| 363 |
ast_log(LOG_WARNING, "Taskpool '%s' has a max size of 0, which is not valid without auto increment. Adjusting to %d.\n", name, pool->options.minimum_size); |
| 364 |
} |
| 365 |
if (pool->options.minimum_size != pool->options.max_size) { |
| 366 |
pool->options.minimum_size = pool->options.max_size; |
| 367 |
pool->options.initial_size = pool->options.max_size; |
| 368 |
ast_log(LOG_WARNING, "Taskpool '%s' has a minimum size of %d, while max size is %d. Adjusting all sizes to %d due to lack of auto increment.\n", |
| 369 |
name, options->minimum_size, pool->options.max_size, pool->options.max_size); |
| 370 |
} |
| 371 |
} else if (!options->growth_threshold) { |
| 372 |
pool->options.growth_threshold = TASKPOOL_GROW_THRESHOLD; |
| 373 |
} |
| 374 |
|
| 375 |
if (options->selector == AST_TASKPOOL_SELECTOR_DEFAULT || options->selector == AST_TASKPOOL_SELECTOR_LEAST_FULL) { |
| 376 |
pool->selector = taskpool_least_full_selector; |
| 377 |
} else if (options->selector == AST_TASKPOOL_SELECTOR_SEQUENTIAL) { |
| 378 |
pool->selector = taskpool_sequential_selector; |
| 379 |
} else { |
| 380 |
ast_log(LOG_WARNING, "Taskpool '%s' has an invalid selector of %d. Adjusting to default selector.\n", |
| 381 |
name, options->selector); |
| 382 |
pool->selector = taskpool_least_full_selector; |
| 383 |
} |
| 384 |
|
| 385 |
if (taskpool_taskprocessors_init(&pool->static_taskprocessors, pool->options.minimum_size)) { |
| 386 |
ao2_ref(pool, -1); |
| 387 |
return NULL; |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
for (int i = 0; i < pool->options.minimum_size; i++) { |
| 392 |
struct taskpool_taskprocessor *taskprocessor; |
| 393 |
|
| 394 |
taskprocessor = taskpool_taskprocessor_alloc(pool, 's'); |
| 395 |
if (!taskprocessor) { |
| 396 |
|
| 397 |
ast_taskpool_shutdown(pool); |
| 398 |
return NULL; |
| 399 |
} |
| 400 |
|
| 401 |
if (AST_VECTOR_APPEND(&pool->static_taskprocessors.taskprocessors, taskprocessor)) { |
| 402 |
ao2_ref(taskprocessor, -1); |
| 403 |
|
| 404 |
ast_taskpool_shutdown(pool); |
| 405 |
return NULL; |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
if (taskpool_taskprocessors_init(&pool->dynamic_taskprocessors, |
| 410 |
pool->options.initial_size - pool->options.minimum_size)) { |
| 411 |
ast_taskpool_shutdown(pool); |
| 412 |
return NULL; |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
for (int i = 0; i < (pool->options.initial_size - pool->options.minimum_size); i++) { |
| 417 |
struct taskpool_taskprocessor *taskprocessor; |
| 418 |
|
| 419 |
taskprocessor = taskpool_taskprocessor_alloc(pool, 'd'); |
| 420 |
if (!taskprocessor) { |
| 421 |
|
| 422 |
ast_taskpool_shutdown(pool); |
| 423 |
return NULL; |
| 424 |
} |
| 425 |
|
| 426 |
if (AST_VECTOR_APPEND(&pool->dynamic_taskprocessors.taskprocessors, taskprocessor)) { |
| 427 |
ao2_ref(taskprocessor, -1); |
| 428 |
|
| 429 |
ast_taskpool_shutdown(pool); |
| 430 |
return NULL; |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
if (options->idle_timeout && options->auto_increment) { |
| 439 |
pool->shrink_sched_id = ast_sched_add(sched, options->idle_timeout * 1000, taskpool_dynamic_pool_shrink, ao2_bump(pool)); |
| 440 |
if (pool->shrink_sched_id < 0) { |
| 441 |
ao2_ref(pool, -1); |
| 442 |
|
| 443 |
ast_taskpool_shutdown(pool); |
| 444 |
return NULL; |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
return pool; |
| 449 |
} |
| 450 |
|
| 451 |
size_t ast_taskpool_taskprocessors_count(struct ast_taskpool *pool) |
| 452 |
{ |
| 453 |
size_t count; |
| 454 |
|
| 455 |
ao2_lock(pool); |
| 456 |
count = AST_VECTOR_SIZE(&pool->static_taskprocessors.taskprocessors) + AST_VECTOR_SIZE(&pool->dynamic_taskprocessors.taskprocessors); |
| 457 |
ao2_unlock(pool); |
| 458 |
|
| 459 |
return count; |
| 460 |
} |
| 461 |
|
| 462 |
#define TASKPOOL_QUEUE_SIZE_ADD(tps, size) (size += ast_taskprocessor_size(tps->taskprocessor)) |
| 463 |
|
| 464 |
long ast_taskpool_queue_size(struct ast_taskpool *pool) |
| 465 |
{ |
| 466 |
long queue_size = 0; |
| 467 |
|
| 468 |
ao2_lock(pool); |
| 469 |
AST_VECTOR_CALLBACK_VOID(&pool->static_taskprocessors.taskprocessors, TASKPOOL_QUEUE_SIZE_ADD, queue_size); |
| 470 |
AST_VECTOR_CALLBACK_VOID(&pool->dynamic_taskprocessors.taskprocessors, TASKPOOL_QUEUE_SIZE_ADD, queue_size); |
| 471 |
ao2_unlock(pool); |
| 472 |
|
| 473 |
return queue_size; |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
static void taskpool_dynamic_pool_grow(struct ast_taskpool *pool, struct taskpool_taskprocessor **taskprocessor) |
| 480 |
{ |
| 481 |
unsigned int num_to_add = pool->options.auto_increment; |
| 482 |
int i; |
| 483 |
|
| 484 |
if (!num_to_add) { |
| 485 |
return; |
| 486 |
} |
| 487 |
|
| 488 |
|
| 489 |
if (pool->options.max_size) { |
| 490 |
unsigned int current_size = AST_VECTOR_SIZE(&pool->dynamic_taskprocessors.taskprocessors) + AST_VECTOR_SIZE(&pool->static_taskprocessors.taskprocessors); |
| 491 |
|
| 492 |
if (current_size + num_to_add > pool->options.max_size) { |
| 493 |
num_to_add = pool->options.max_size - current_size; |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
for (i = 0; i < num_to_add; i++) { |
| 498 |
struct taskpool_taskprocessor *new_taskprocessor; |
| 499 |
|
| 500 |
new_taskprocessor = taskpool_taskprocessor_alloc(pool, 'd'); |
| 501 |
if (!new_taskprocessor) { |
| 502 |
return; |
| 503 |
} |
| 504 |
|
| 505 |
if (AST_VECTOR_APPEND(&pool->dynamic_taskprocessors.taskprocessors, new_taskprocessor)) { |
| 506 |
ao2_ref(new_taskprocessor, -1); |
| 507 |
return; |
| 508 |
} |
| 509 |
|
| 510 |
if (i == 0) { |
| 511 |
|
| 512 |
*taskprocessor = new_taskprocessor; |
| 513 |
|
| 514 |
pool->dynamic_taskprocessors.taskprocessor_num = 0; |
| 515 |
} else if (i == 1) { |
| 516 |
|
| 517 |
pool->dynamic_taskprocessors.taskprocessor_num = AST_VECTOR_SIZE(&pool->dynamic_taskprocessors.taskprocessors) - 1; |
| 518 |
} |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
#undef ast_taskpool_push |
| 523 |
#define ast_taskpool_push_internal(pool, task, data) \ |
| 524 |
__ast_taskpool_push(pool, task, data, __FILE__, __LINE__, __PRETTY_FUNCTION__) |
| 525 |
int ast_taskpool_push(struct ast_taskpool *pool, int (*task)(void *data), void *data); |
| 526 |
|
| 527 |
int __ast_taskpool_push(struct ast_taskpool *pool, int (*task)(void *data), void *data, |
| 528 |
const char *file, int line, const char *function) |
| 529 |
{ |
| 530 |
RAII_VAR(struct taskpool_taskprocessor *, taskprocessor, NULL, ao2_cleanup); |
| 531 |
|
| 532 |
|
| 533 |
ao2_lock(pool); |
| 534 |
if (!pool->shutting_down) { |
| 535 |
unsigned int growth_threshold_reached = 0; |
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
pool->selector(pool, &pool->static_taskprocessors, &taskprocessor, &growth_threshold_reached); |
| 543 |
if (pool->options.auto_increment && growth_threshold_reached) { |
| 544 |
|
| 545 |
pool->selector(pool, &pool->dynamic_taskprocessors, &taskprocessor, &growth_threshold_reached); |
| 546 |
if (growth_threshold_reached) { |
| 547 |
|
| 548 |
taskpool_dynamic_pool_grow(pool, &taskprocessor); |
| 549 |
} |
| 550 |
|
| 551 |
|
| 552 |
if (taskprocessor) { |
| 553 |
taskprocessor->last_pushed = ast_tvnow(); |
| 554 |
} |
| 555 |
} |
| 556 |
ao2_bump(taskprocessor); |
| 557 |
} |
| 558 |
ao2_unlock(pool); |
| 559 |
|
| 560 |
if (!taskprocessor) { |
| 561 |
return -1; |
| 562 |
} |
| 563 |
|
| 564 |
if (__ast_taskprocessor_push(taskprocessor->taskprocessor, task, data, file, line, function)) { |
| 565 |
return -1; |
| 566 |
} |
| 567 |
|
| 568 |
return 0; |
| 569 |
} |
| 570 |
|
| 571 |
|
| 572 |
int ast_taskpool_push(struct ast_taskpool *pool, int (*task)(void *data), void *data) |
| 573 |
{ |
| 574 |
return __ast_taskpool_push(pool, task, data, NULL, 0, NULL); |
| 575 |
} |
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
struct taskpool_sync_task { |
| 581 |
ast_mutex_t lock; |
| 582 |
ast_cond_t cond; |
| 583 |
int complete; |
| 584 |
int fail; |
| 585 |
int (*task)(void *); |
| 586 |
void *task_data; |
| 587 |
}; |
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
static int taskpool_sync_task_init(struct taskpool_sync_task *sync_task, int (*task)(void *), void *data) |
| 593 |
{ |
| 594 |
ast_mutex_init(&sync_task->lock); |
| 595 |
ast_cond_init(&sync_task->cond, NULL); |
| 596 |
sync_task->complete = 0; |
| 597 |
sync_task->fail = 0; |
| 598 |
sync_task->task = task; |
| 599 |
sync_task->task_data = data; |
| 600 |
return 0; |
| 601 |
} |
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
static void taskpool_sync_task_cleanup(struct taskpool_sync_task *sync_task) |
| 607 |
{ |
| 608 |
ast_mutex_destroy(&sync_task->lock); |
| 609 |
ast_cond_destroy(&sync_task->cond); |
| 610 |
} |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
static int taskpool_sync_task(void *data) |
| 616 |
{ |
| 617 |
struct taskpool_sync_task *sync_task = data; |
| 618 |
int ret; |
| 619 |
|
| 620 |
sync_task->fail = sync_task->task(sync_task->task_data); |
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
ast_mutex_lock(&sync_task->lock); |
| 628 |
sync_task->complete = 1; |
| 629 |
ast_cond_signal(&sync_task->cond); |
| 630 |
ret = sync_task->fail; |
| 631 |
ast_mutex_unlock(&sync_task->lock); |
| 632 |
return ret; |
| 633 |
} |
| 634 |
|
| 635 |
int __ast_taskpool_push_wait(struct ast_taskpool *pool, int (*task)(void *data), void *data, |
| 636 |
const char *file, int line, const char *function) |
| 637 |
{ |
| 638 |
struct taskpool_sync_task sync_task; |
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
if (ast_taskpool_get_current()) { |
| 644 |
return task(data); |
| 645 |
} |
| 646 |
|
| 647 |
if (taskpool_sync_task_init(&sync_task, task, data)) { |
| 648 |
return -1; |
| 649 |
} |
| 650 |
|
| 651 |
if (__ast_taskpool_push(pool, taskpool_sync_task, &sync_task, file, line, function)) { |
| 652 |
taskpool_sync_task_cleanup(&sync_task); |
| 653 |
return -1; |
| 654 |
} |
| 655 |
|
| 656 |
ast_mutex_lock(&sync_task.lock); |
| 657 |
while (!sync_task.complete) { |
| 658 |
ast_cond_wait(&sync_task.cond, &sync_task.lock); |
| 659 |
} |
| 660 |
ast_mutex_unlock(&sync_task.lock); |
| 661 |
|
| 662 |
taskpool_sync_task_cleanup(&sync_task); |
| 663 |
return sync_task.fail; |
| 664 |
} |
| 665 |
|
| 666 |
|
| 667 |
#undef ast_taskpool_push_wait |
| 668 |
int ast_taskpool_push_wait(struct ast_taskpool *pool, int (*task)(void *data), void *data); |
| 669 |
|
| 670 |
int ast_taskpool_push_wait(struct ast_taskpool *pool, int (*task)(void *data), void *data) |
| 671 |
{ |
| 672 |
return __ast_taskpool_push_wait(pool, task, data, NULL, 0, NULL); |
| 673 |
} |
| 674 |
|
| 675 |
void ast_taskpool_shutdown(struct ast_taskpool *pool) |
| 676 |
{ |
| 677 |
if (!pool) { |
| 678 |
return; |
| 679 |
} |
| 680 |
|
| 681 |
|
| 682 |
ao2_lock(pool); |
| 683 |
pool->shutting_down = 1; |
| 684 |
ao2_unlock(pool); |
| 685 |
|
| 686 |
|
| 687 |
AST_SCHED_DEL_UNREF(sched, pool->shrink_sched_id, ao2_ref(pool, -1)); |
| 688 |
|
| 689 |
|
| 690 |
taskpool_taskprocessors_cleanup(&pool->static_taskprocessors); |
| 691 |
taskpool_taskprocessors_cleanup(&pool->dynamic_taskprocessors); |
| 692 |
|
| 693 |
ao2_ref(pool, -1); |
| 694 |
} |
| 695 |
|
| 696 |
enum serializer_suspension { |
| 697 |
SERIALIZER_UNSUSPENDED = 0, |
| 698 |
SERIALIZER_SUSPENDING, |
| 699 |
SERIALIZER_SUSPENDED, |
| 700 |
}; |
| 701 |
|
| 702 |
struct serializer { |
| 703 |
|
| 704 |
struct ast_taskpool *pool; |
| 705 |
|
| 706 |
struct ast_serializer_shutdown_group *shutdown_group; |
| 707 |
|
| 708 |
ast_cond_t cond; |
| 709 |
|
| 710 |
enum serializer_suspension suspended; |
| 711 |
}; |
| 712 |
|
| 713 |
static void serializer_dtor(void *obj) |
| 714 |
{ |
| 715 |
struct serializer *ser = obj; |
| 716 |
|
| 717 |
ao2_cleanup(ser->pool); |
| 718 |
ao2_cleanup(ser->shutdown_group); |
| 719 |
ast_cond_destroy(&ser->cond); |
| 720 |
} |
| 721 |
|
| 722 |
static struct serializer *serializer_create(struct ast_taskpool *pool, |
| 723 |
struct ast_serializer_shutdown_group *shutdown_group) |
| 724 |
{ |
| 725 |
struct serializer *ser; |
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
ser = ao2_alloc(sizeof(*ser), serializer_dtor); |
| 731 |
if (!ser) { |
| 732 |
return NULL; |
| 733 |
} |
| 734 |
ser->pool = ao2_bump(pool); |
| 735 |
ser->shutdown_group = ao2_bump(shutdown_group); |
| 736 |
ast_cond_init(&ser->cond, NULL); |
| 737 |
return ser; |
| 738 |
} |
| 739 |
|
| 740 |
AST_THREADSTORAGE_RAW(current_taskpool_serializer); |
| 741 |
|
| 742 |
static int execute_tasks(void *data) |
| 743 |
{ |
| 744 |
struct ast_taskpool *pool = ast_taskpool_get_current(); |
| 745 |
struct ast_taskprocessor *tps = data; |
| 746 |
struct ast_taskprocessor_listener *listener = ast_taskprocessor_listener(tps); |
| 747 |
struct serializer *ser = ast_taskprocessor_listener_get_user_data(listener); |
| 748 |
size_t remaining, requeue = 0; |
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
ao2_lock(ser); |
| 758 |
|
| 759 |
ast_threadstorage_set_ptr(¤t_taskpool_serializer, tps); |
| 760 |
for (remaining = ast_taskprocessor_size(tps); remaining > 0; remaining--) { |
| 761 |
requeue = ast_taskprocessor_execute(tps); |
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
if (ser->suspended == SERIALIZER_SUSPENDED) { |
| 768 |
requeue = 0; |
| 769 |
break; |
| 770 |
} |
| 771 |
} |
| 772 |
ast_threadstorage_set_ptr(¤t_taskpool_serializer, NULL); |
| 773 |
|
| 774 |
ao2_unlock(ser); |
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
if (requeue) { |
| 780 |
|
| 781 |
if (ast_taskpool_push(pool, execute_tasks, tps)) { |
| 782 |
ast_taskprocessor_unreference(tps); |
| 783 |
} |
| 784 |
} else { |
| 785 |
ast_taskprocessor_unreference(tps); |
| 786 |
} |
| 787 |
|
| 788 |
return 0; |
| 789 |
} |
| 790 |
|
| 791 |
static void serializer_task_pushed(struct ast_taskprocessor_listener *listener, int was_empty) |
| 792 |
{ |
| 793 |
if (was_empty) { |
| 794 |
struct serializer *ser = ast_taskprocessor_listener_get_user_data(listener); |
| 795 |
struct ast_taskprocessor *tps = ast_taskprocessor_listener_get_tps(listener); |
| 796 |
|
| 797 |
if (ast_taskpool_push(ser->pool, execute_tasks, tps)) { |
| 798 |
ast_taskprocessor_unreference(tps); |
| 799 |
} |
| 800 |
} |
| 801 |
} |
| 802 |
|
| 803 |
static int serializer_start(struct ast_taskprocessor_listener *listener) |
| 804 |
{ |
| 805 |
|
| 806 |
return 0; |
| 807 |
} |
| 808 |
|
| 809 |
static void serializer_shutdown(struct ast_taskprocessor_listener *listener) |
| 810 |
{ |
| 811 |
struct serializer *ser = ast_taskprocessor_listener_get_user_data(listener); |
| 812 |
|
| 813 |
if (ser->shutdown_group) { |
| 814 |
ast_serializer_shutdown_group_dec(ser->shutdown_group); |
| 815 |
} |
| 816 |
ao2_cleanup(ser); |
| 817 |
} |
| 818 |
|
| 819 |
static struct ast_taskprocessor_listener_callbacks serializer_tps_listener_callbacks = { |
| 820 |
.task_pushed = serializer_task_pushed, |
| 821 |
.start = serializer_start, |
| 822 |
.shutdown = serializer_shutdown, |
| 823 |
}; |
| 824 |
|
| 825 |
struct ast_taskprocessor *ast_taskpool_serializer_get_current(void) |
| 826 |
{ |
| 827 |
return ast_threadstorage_get_ptr(¤t_taskpool_serializer); |
| 828 |
} |
| 829 |
|
| 830 |
struct ast_taskprocessor *ast_taskpool_serializer_group(const char *name, |
| 831 |
struct ast_taskpool *pool, struct ast_serializer_shutdown_group *shutdown_group) |
| 832 |
{ |
| 833 |
struct serializer *ser; |
| 834 |
struct ast_taskprocessor_listener *listener; |
| 835 |
struct ast_taskprocessor *tps; |
| 836 |
|
| 837 |
ser = serializer_create(pool, shutdown_group); |
| 838 |
if (!ser) { |
| 839 |
return NULL; |
| 840 |
} |
| 841 |
|
| 842 |
listener = ast_taskprocessor_listener_alloc(&serializer_tps_listener_callbacks, ser); |
| 843 |
if (!listener) { |
| 844 |
ao2_ref(ser, -1); |
| 845 |
return NULL; |
| 846 |
} |
| 847 |
|
| 848 |
tps = ast_taskprocessor_create_with_listener(name, listener); |
| 849 |
if (!tps) { |
| 850 |
|
| 851 |
ao2_ref(ser, -1); |
| 852 |
} else if (shutdown_group) { |
| 853 |
ast_serializer_shutdown_group_inc(shutdown_group); |
| 854 |
} |
| 855 |
|
| 856 |
ao2_ref(listener, -1); |
| 857 |
return tps; |
| 858 |
} |
| 859 |
|
| 860 |
struct ast_taskprocessor *ast_taskpool_serializer(const char *name, struct ast_taskpool *pool) |
| 861 |
{ |
| 862 |
return ast_taskpool_serializer_group(name, pool, NULL); |
| 863 |
} |
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
static int taskpool_serializer_empty_task(void *data) |
| 869 |
{ |
| 870 |
return 0; |
| 871 |
} |
| 872 |
|
| 873 |
|
| 874 |
#undef ast_taskpool_serializer_push_wait |
| 875 |
int ast_taskpool_serializer_push_wait(struct ast_taskprocessor *serializer, int (*task)(void *data), void *data); |
| 876 |
|
| 877 |
int ast_taskpool_serializer_push_wait(struct ast_taskprocessor *serializer, int (*task)(void *data), void *data) |
| 878 |
{ |
| 879 |
return __ast_taskpool_serializer_push_wait(serializer, task, data, NULL, 0, NULL); |
| 880 |
} |
| 881 |
|
| 882 |
int __ast_taskpool_serializer_push_wait(struct ast_taskprocessor *serializer, int (*task)(void *data), void *data, |
| 883 |
const char *file, int line, const char *function) |
| 884 |
{ |
| 885 |
struct ast_taskprocessor_listener *listener = ast_taskprocessor_listener(serializer); |
| 886 |
struct serializer *ser = ast_taskprocessor_listener_get_user_data(listener); |
| 887 |
struct ast_taskprocessor *prior_serializer; |
| 888 |
struct taskpool_sync_task sync_task; |
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
if (!ast_taskpool_get_current()) { |
| 893 |
if (taskpool_sync_task_init(&sync_task, task, data)) { |
| 894 |
return -1; |
| 895 |
} |
| 896 |
|
| 897 |
if (__ast_taskprocessor_push(serializer, taskpool_sync_task, &sync_task, file, line, function)) { |
| 898 |
taskpool_sync_task_cleanup(&sync_task); |
| 899 |
return -1; |
| 900 |
} |
| 901 |
|
| 902 |
ast_mutex_lock(&sync_task.lock); |
| 903 |
while (!sync_task.complete) { |
| 904 |
ast_cond_wait(&sync_task.cond, &sync_task.lock); |
| 905 |
} |
| 906 |
ast_mutex_unlock(&sync_task.lock); |
| 907 |
|
| 908 |
taskpool_sync_task_cleanup(&sync_task); |
| 909 |
return sync_task.fail; |
| 910 |
} |
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
prior_serializer = ast_taskpool_serializer_get_current(); |
| 916 |
|
| 917 |
ao2_lock(ser); |
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
if (!ast_taskprocessor_size(serializer) || prior_serializer == serializer) { |
| 926 |
ast_threadstorage_set_ptr(¤t_taskpool_serializer, serializer); |
| 927 |
sync_task.fail = task(data); |
| 928 |
ao2_unlock(ser); |
| 929 |
ast_threadstorage_set_ptr(¤t_taskpool_serializer, prior_serializer); |
| 930 |
return sync_task.fail; |
| 931 |
} |
| 932 |
|
| 933 |
if (taskpool_sync_task_init(&sync_task, task, data)) { |
| 934 |
ao2_unlock(ser); |
| 935 |
return -1; |
| 936 |
} |
| 937 |
|
| 938 |
|
| 939 |
if (__ast_taskprocessor_push(serializer, taskpool_sync_task, &sync_task, file, line, function)) { |
| 940 |
taskpool_sync_task_cleanup(&sync_task); |
| 941 |
ao2_unlock(ser); |
| 942 |
return -1; |
| 943 |
} |
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
if (ast_taskprocessor_push(serializer, taskpool_serializer_empty_task, NULL)) { |
| 949 |
taskpool_sync_task_cleanup(&sync_task); |
| 950 |
ao2_unlock(ser); |
| 951 |
return -1; |
| 952 |
} |
| 953 |
|
| 954 |
|
| 955 |
ast_threadstorage_set_ptr(¤t_taskpool_serializer, serializer); |
| 956 |
while (!sync_task.complete) { |
| 957 |
|
| 958 |
while (ser->suspended == SERIALIZER_SUSPENDED) { |
| 959 |
ast_cond_wait(&ser->cond, ao2_object_get_lockaddr(ser)); |
| 960 |
} |
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
ast_taskprocessor_execute(serializer); |
| 966 |
} |
| 967 |
taskpool_sync_task_cleanup(&sync_task); |
| 968 |
ao2_unlock(ser); |
| 969 |
|
| 970 |
ast_threadstorage_set_ptr(¤t_taskpool_serializer, prior_serializer); |
| 971 |
|
| 972 |
return sync_task.fail; |
| 973 |
} |
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
static int taskpool_serializer_suspend_task(void *data) |
| 979 |
{ |
| 980 |
struct ast_taskprocessor *serializer = data; |
| 981 |
struct ast_taskprocessor_listener *listener = ast_taskprocessor_listener(serializer); |
| 982 |
struct serializer *ser = ast_taskprocessor_listener_get_user_data(listener); |
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
if (ast_taskprocessor_push(serializer, taskpool_serializer_empty_task, NULL)) { |
| 990 |
return -1; |
| 991 |
} |
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
ser->suspended = SERIALIZER_SUSPENDED; |
| 997 |
|
| 998 |
return 0; |
| 999 |
} |
| 1000 |
|
| 1001 |
int ast_taskpool_serializer_suspend(struct ast_taskprocessor *serializer) |
| 1002 |
{ |
| 1003 |
struct ast_taskprocessor_listener *listener = ast_taskprocessor_listener(serializer); |
| 1004 |
struct serializer *ser = ast_taskprocessor_listener_get_user_data(listener); |
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
if (ast_taskpool_get_current()) { |
| 1017 |
return -1; |
| 1018 |
} |
| 1019 |
|
| 1020 |
ao2_lock(ser); |
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
if (ser->suspended != SERIALIZER_UNSUSPENDED) { |
| 1026 |
ao2_unlock(ser); |
| 1027 |
return 0; |
| 1028 |
} |
| 1029 |
|
| 1030 |
ser->suspended = SERIALIZER_SUSPENDING; |
| 1031 |
|
| 1032 |
ao2_unlock(ser); |
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
if (ast_taskpool_serializer_push_wait(serializer, taskpool_serializer_suspend_task, serializer)) { |
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
ao2_lock(ser); |
| 1042 |
ser->suspended = SERIALIZER_UNSUSPENDED; |
| 1043 |
ao2_unlock(ser); |
| 1044 |
return -1; |
| 1045 |
} |
| 1046 |
|
| 1047 |
return 0; |
| 1048 |
} |
| 1049 |
|
| 1050 |
int ast_taskpool_serializer_unsuspend(struct ast_taskprocessor *serializer) |
| 1051 |
{ |
| 1052 |
struct ast_taskprocessor_listener *listener = ast_taskprocessor_listener(serializer); |
| 1053 |
struct serializer *ser = ast_taskprocessor_listener_get_user_data(listener); |
| 1054 |
|
| 1055 |
if (ast_taskpool_get_current()) { |
| 1056 |
return -1; |
| 1057 |
} |
| 1058 |
|
| 1059 |
ao2_lock(ser); |
| 1060 |
|
| 1061 |
if (ser->suspended != SERIALIZER_SUSPENDED) { |
| 1062 |
ao2_unlock(ser); |
| 1063 |
return 0; |
| 1064 |
} |
| 1065 |
|
| 1066 |
ser->suspended = SERIALIZER_UNSUSPENDED; |
| 1067 |
|
| 1068 |
|
| 1069 |
ast_cond_broadcast(&ser->cond); |
| 1070 |
|
| 1071 |
|
| 1072 |
if (ast_taskpool_push(ser->pool, execute_tasks, ao2_bump(serializer))) { |
| 1073 |
ast_taskprocessor_unreference(serializer); |
| 1074 |
} |
| 1075 |
|
| 1076 |
ao2_unlock(ser); |
| 1077 |
|
| 1078 |
return 0; |
| 1079 |
} |
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
static void taskpool_shutdown(void) |
| 1086 |
{ |
| 1087 |
if (sched) { |
| 1088 |
ast_sched_context_destroy(sched); |
| 1089 |
sched = NULL; |
| 1090 |
} |
| 1091 |
} |
| 1092 |
|
| 1093 |
int ast_taskpool_init(void) |
| 1094 |
{ |
| 1095 |
sched = ast_sched_context_create(); |
| 1096 |
if (!sched) { |
| 1097 |
return -1; |
| 1098 |
} |
| 1099 |
|
| 1100 |
if (ast_sched_start_thread(sched)) { |
| 1101 |
return -1; |
| 1102 |
} |
| 1103 |
|
| 1104 |
ast_register_cleanup(taskpool_shutdown); |
| 1105 |
|
| 1106 |
return 0; |
| 1107 |
} |