yanglimingcn commented on code in PR #2560: URL: https://github.com/apache/brpc/pull/2560#discussion_r1846434647
########## test/brpc_event_dispatcher_unittest.cpp: ########## @@ -267,3 +390,130 @@ TEST_F(EventDispatcherTest, dispatch_tasks) { ASSERT_EQ(NCLIENT, info.free_item_num - old_info.free_item_num); #endif } + +// Unique identifier of a EventPipe. +// Users shall store EventFDId instead of EventPipe and call EventPipe::Address() +// to convert the identifier to an unique_ptr at each access. Whenever a +// unique_ptr is not destructed, the enclosed EventPipe will not be recycled. +typedef brpc::VRefId EventPipeId; + +const brpc::VRefId INVALID_EVENT_PIPE_ID = brpc::INVALID_VREF_ID; + +class EventPipe; +typedef brpc::VersionedRefWithIdUniquePtr<EventPipe> EventPipeUniquePtr; + + +class EventPipe : public brpc::VersionedRefWithId<EventPipe> { +public: + explicit EventPipe(Forbidden f) + : brpc::VersionedRefWithId<EventPipe>(f) + , _pipe_fds{-1, -1} + , _input_event_count(0) + {} + + int Notify() { + char c = 0; + if (write(_pipe_fds[1], &c, 1) != 1) { + PLOG(ERROR) << "Fail to write to _pipe_fds[1]"; + return -1; + } + return 0; + } + +private: +friend class VersionedRefWithId<EventPipe>; +friend class brpc::IOEvent<EventPipe>; + + int OnCreated() { + if (pipe(_pipe_fds)) { + PLOG(FATAL) << "Fail to create _pipe_fds"; + return -1; + } + if (_io_event.Init((void*)id()) != 0) { + LOG(ERROR) << "Fail to init IOEvent"; + return -1; + } + _io_event.set_bthread_tag(bthread_self_tag()); + if (_io_event.AddConsumer(_pipe_fds[0]) != 0) { + PLOG(ERROR) << "Fail to add SocketId=" << id() + << " into EventDispatcher"; + return -1; + } + + + _input_event_count = 0; + return 0; + } + + void BeforeRecycled() { + brpc::GetGlobalEventDispatcher(_pipe_fds[0], bthread_self_tag()) Review Comment: 这块可以使用_io_event.RemoveConsumer(_pipe_fds[0])吧。 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@brpc.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@brpc.apache.org For additional commands, e-mail: dev-h...@brpc.apache.org