| 794 | } |
| 795 | |
| 796 | void StreamWait(StreamId stream_id, const timespec *due_time, |
| 797 | void (*on_writable)(StreamId, void*, int), void *arg) { |
| 798 | SocketUniquePtr ptr; |
| 799 | if (Socket::Address(stream_id, &ptr) != 0) { |
| 800 | Stream::WritableMeta* wm = new Stream::WritableMeta; |
| 801 | wm->id = stream_id; |
| 802 | wm->arg= arg; |
| 803 | wm->has_timer = false; |
| 804 | wm->on_writable = on_writable; |
| 805 | wm->error_code = EINVAL; |
| 806 | const bthread_attr_t* attr = |
| 807 | FLAGS_usercode_in_pthread ? &BTHREAD_ATTR_PTHREAD |
| 808 | : &BTHREAD_ATTR_NORMAL; |
| 809 | bthread_t tid; |
| 810 | if (bthread_start_background(&tid, attr, Stream::RunOnWritable, wm) != 0) { |
| 811 | PLOG(FATAL) << "Fail to start bthread"; |
| 812 | Stream::RunOnWritable(wm); |
| 813 | } |
| 814 | return; |
| 815 | } |
| 816 | Stream* s = (Stream*)ptr->conn(); |
| 817 | return s->Wait(on_writable, arg, due_time); |
| 818 | } |
| 819 | |
| 820 | int StreamWait(StreamId stream_id, const timespec* due_time) { |
| 821 | SocketUniquePtr ptr; |