| 427 | } |
| 428 | |
| 429 | int bthread_setconcurrency(int num) { |
| 430 | if (num < BTHREAD_MIN_CONCURRENCY || num > BTHREAD_MAX_CONCURRENCY) { |
| 431 | LOG(ERROR) << "Invalid concurrency=" << num; |
| 432 | return EINVAL; |
| 433 | } |
| 434 | if (bthread::FLAGS_bthread_min_concurrency > 0) { |
| 435 | if (num < bthread::FLAGS_bthread_min_concurrency) { |
| 436 | return EINVAL; |
| 437 | } |
| 438 | if (bthread::never_set_bthread_concurrency) { |
| 439 | bthread::never_set_bthread_concurrency = false; |
| 440 | } |
| 441 | bthread::FLAGS_bthread_concurrency = num; |
| 442 | return 0; |
| 443 | } |
| 444 | bthread::TaskControl* c = bthread::get_task_control(); |
| 445 | if (c != NULL) { |
| 446 | if (num < c->concurrency()) { |
| 447 | return EPERM; |
| 448 | } else if (num == c->concurrency()) { |
| 449 | return 0; |
| 450 | } |
| 451 | } |
| 452 | BAIDU_SCOPED_LOCK(bthread::g_task_control_mutex); |
| 453 | c = bthread::get_task_control(); |
| 454 | if (c == NULL) { |
| 455 | if (bthread::never_set_bthread_concurrency) { |
| 456 | bthread::never_set_bthread_concurrency = false; |
| 457 | bthread::FLAGS_bthread_concurrency = num; |
| 458 | } else if (num > bthread::FLAGS_bthread_concurrency) { |
| 459 | bthread::FLAGS_bthread_concurrency = num; |
| 460 | } |
| 461 | return 0; |
| 462 | } |
| 463 | if (bthread::FLAGS_bthread_concurrency != c->concurrency()) { |
| 464 | LOG(ERROR) << "CHECK failed: bthread_concurrency=" |
| 465 | << bthread::FLAGS_bthread_concurrency |
| 466 | << " != tc_concurrency=" << c->concurrency(); |
| 467 | bthread::FLAGS_bthread_concurrency = c->concurrency(); |
| 468 | } |
| 469 | if (num > bthread::FLAGS_bthread_concurrency) { |
| 470 | // Create more workers if needed. |
| 471 | auto added = bthread::add_workers_for_each_tag(num - bthread::FLAGS_bthread_concurrency); |
| 472 | bthread::FLAGS_bthread_concurrency += added; |
| 473 | } |
| 474 | return (num == bthread::FLAGS_bthread_concurrency ? 0 : EPERM); |
| 475 | } |
| 476 | |
| 477 | int bthread_getconcurrency_by_tag(bthread_tag_t tag) { |
| 478 | BAIDU_SCOPED_LOCK(bthread::g_task_control_mutex); |