guodongxiaren commented on code in PR #1888: URL: https://github.com/apache/incubator-brpc/pull/1888#discussion_r948035908
########## src/brpc/event_dispatcher_kqueue.cpp: ########## @@ -0,0 +1,223 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + + +#include <sys/types.h> +#include <sys/event.h> +#include <sys/time.h> + +namespace brpc { + +EventDispatcher::EventDispatcher() + : _epfd(-1) + , _stop(false) + , _tid(0) + , _consumer_thread_attr(BTHREAD_ATTR_NORMAL) +{ + _epfd = kqueue(); + if (_epfd < 0) { + PLOG(FATAL) << "Fail to create kqueue"; + return; + } + CHECK_EQ(0, butil::make_close_on_exec(_epfd)); + + _wakeup_fds[0] = -1; + _wakeup_fds[1] = -1; + if (pipe(_wakeup_fds) != 0) { + PLOG(FATAL) << "Fail to create pipe"; + return; + } +} + +EventDispatcher::~EventDispatcher() { + Stop(); + Join(); + if (_epfd >= 0) { + close(_epfd); + _epfd = -1; + } + if (_wakeup_fds[0] > 0) { + close(_wakeup_fds[0]); + close(_wakeup_fds[1]); + } +} + +int EventDispatcher::Start(const bthread_attr_t* consumer_thread_attr) { + if (_epfd < 0) { + LOG(FATAL) << "kqueue was not created"; + return -1; + } + + if (_tid != 0) { + LOG(FATAL) << "Already started this dispatcher(" << this + << ") in bthread=" << _tid; + return -1; + } + + // Set _consumer_thread_attr before creating epoll/kqueue thread to make sure + // everyting seems sane to the thread. + _consumer_thread_attr = (consumer_thread_attr ? + *consumer_thread_attr : BTHREAD_ATTR_NORMAL); + + //_consumer_thread_attr is used in StartInputEvent(), assign flag NEVER_QUIT to it will cause new bthread + // that created by epoll_wait() never to quit. + _epoll_thread_attr = _consumer_thread_attr | BTHREAD_NEVER_QUIT; Review Comment: 发现这个变量只在单个成员函数中调用到,所以我从头文件中删了,改成cpp函数内局部变量了。避免命名问题。 -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
