| 435 | } |
| 436 | |
| 437 | void Stream::Wait(void (*on_writable)(StreamId, void*, int), void* arg, |
| 438 | const timespec* due_time, bool new_thread, bthread_id_t *join_id) { |
| 439 | WritableMeta *wm = new WritableMeta; |
| 440 | wm->on_writable = on_writable; |
| 441 | wm->id = id(); |
| 442 | wm->arg = arg; |
| 443 | wm->new_thread = new_thread; |
| 444 | wm->has_timer = false; |
| 445 | bthread_id_t wait_id; |
| 446 | const int rc = bthread_id_create(&wait_id, wm, TriggerOnWritable); |
| 447 | if (rc != 0) { |
| 448 | CHECK(false) << "Fail to create bthread_id, " << berror(rc); |
| 449 | wm->error_code = rc; |
| 450 | RunOnWritable(wm); |
| 451 | return; |
| 452 | } |
| 453 | if (join_id) { |
| 454 | *join_id = wait_id; |
| 455 | } |
| 456 | CHECK_EQ(0, bthread_id_lock(wait_id, NULL)); |
| 457 | if (due_time != NULL) { |
| 458 | wm->has_timer = true; |
| 459 | const int rc = bthread_timer_add(&wm->timer, *due_time, |
| 460 | OnTimedOut, |
| 461 | reinterpret_cast<void*>(wait_id.value)); |
| 462 | if (rc != 0) { |
| 463 | LOG(ERROR) << "Fail to add timer, " << berror(rc); |
| 464 | CHECK_EQ(0, TriggerOnWritable(wait_id, wm, rc)); |
| 465 | } |
| 466 | } |
| 467 | bthread_mutex_lock(&_congestion_control_mutex); |
| 468 | if (_cur_buf_size <= 0 |
| 469 | || _produced < _remote_consumed + _cur_buf_size) { |
| 470 | bthread_mutex_unlock(&_congestion_control_mutex); |
| 471 | CHECK_EQ(0, TriggerOnWritable(wait_id, wm, 0)); |
| 472 | return; |
| 473 | } else { |
| 474 | bthread_id_list_add(&_writable_wait_list, wait_id); |
| 475 | bthread_mutex_unlock(&_congestion_control_mutex); |
| 476 | } |
| 477 | CHECK_EQ(0, bthread_id_unlock(wait_id)); |
| 478 | } |
| 479 | |
| 480 | void Stream::Wait(void (*on_writable)(StreamId, void *, int), void *arg, |
| 481 | const timespec* due_time) { |
no test coverage detected