GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — muduo/backport.diff at master · feirendada/muduo Expand file tree/ Copy pathbackport.diffMore file actions 551 lines (499 loc) · 14.9 KB / Copy pathbackport.diffFile metadata and controls551 lines (499 loc) · 14.9 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 diff --git a/CMakeLists.txt b/CMakeLists.txt --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,19 +9,19 @@ endif() set(CXX_FLAGS -g # -DVALGRIND - # -DMUDUO_STD_STRING + -DMUDUO_STD_STRING -D_FILE_OFFSET_BITS=64 -Wall -Wextra -Werror -Wconversion -Wno-unused-parameter - -Wold-style-cast + # -Wold-style-cast -Woverloaded-virtual -Wpointer-arith - -Wshadow + # -Wshadow -Wwrite-strings - -march=native + -march=nocona # -MMD # -std=c++0x -rdynamic @@ -31,7 +31,7 @@ if(CMAKE_BUILD_BITS EQUAL 32) endif() string(REPLACE ";" " " CMAKE_CXX_FLAGS "${CXX_FLAGS}") -set(CMAKE_CXX_COMPILER "g++") +set(CMAKE_CXX_COMPILER "g++4") #set(CMAKE_CXX_COMPILER "icpc") set(CMAKE_CXX_FLAGS_DEBUG "-O0") set(CMAKE_CXX_FLAGS_RELEASE "-O2 -finline-limit=1000 -DNDEBUG") diff --git a/examples/idleconnection/CMakeLists.txt b/examples/idleconnection/CMakeLists.txt --- a/examples/idleconnection/CMakeLists.txt +++ b/examples/idleconnection/CMakeLists.txt @@ -1,4 +1,4 @@ -add_executable(idleconnection_echo echo.cc main.cc) +add_executable(idleconnection_echo EXCLUDE_FROM_ALL echo.cc main.cc) target_link_libraries(idleconnection_echo muduo_net) add_executable(idleconnection_echo2 sortedlist.cc) diff --git a/muduo/base/Timestamp.cc b/muduo/base/Timestamp.cc --- a/muduo/base/Timestamp.cc +++ b/muduo/base/Timestamp.cc @@ -4,7 +4,7 @@ #include <stdio.h> #define __STDC_FORMAT_MACROS #include <inttypes.h> -#undef __STDC_FORMAT_MACROS +//#undef __STDC_FORMAT_MACROS #include <boost/static_assert.hpp> diff --git a/muduo/base/tests/CMakeLists.txt b/muduo/base/tests/CMakeLists.txt --- a/muduo/base/tests/CMakeLists.txt +++ b/muduo/base/tests/CMakeLists.txt @@ -10,7 +10,7 @@ target_link_libraries(blockingqueue_test muduo_base) add_executable(blockingqueue_bench BlockingQueue_bench.cc) target_link_libraries(blockingqueue_bench muduo_base) -add_executable(boundedblockingqueue_test BoundedBlockingQueue_test.cc) +add_executable(boundedblockingqueue_test EXCLUDE_FROM_ALL BoundedBlockingQueue_test.cc) target_link_libraries(boundedblockingqueue_test muduo_base) add_executable(date_unittest Date_unittest.cc) diff --git a/muduo/net/Channel.cc b/muduo/net/Channel.cc --- a/muduo/net/Channel.cc +++ b/muduo/net/Channel.cc @@ -21,6 +21,10 @@ const int Channel::kNoneEvent = 0; const int Channel::kReadEvent = POLLIN | POLLPRI; const int Channel::kWriteEvent = POLLOUT; +#ifndef POLLRDHUP +#define POLLRDHUP 0x2000 +#endif + Channel::Channel(EventLoop* loop, int fd__) : loop_(loop), fd_(fd__), diff --git a/muduo/net/Endian.h b/muduo/net/Endian.h --- a/muduo/net/Endian.h +++ b/muduo/net/Endian.h @@ -13,6 +13,7 @@ #include <stdint.h> #include <endian.h> +#include <byteswap.h> namespace muduo { @@ -23,48 +24,38 @@ namespace sockets // the inline assembler code makes type blur, // so we disable warnings for a while. -#if __GNUC_MINOR__ >= 6 -#pragma GCC diagnostic push -#endif -#pragma GCC diagnostic ignored "-Wconversion" -#pragma GCC diagnostic ignored "-Wold-style-cast" +#if __BYTE_ORDER == __LITTLE_ENDIAN inline uint64_t hostToNetwork64(uint64_t host64) { - return htobe64(host64); + return bswap_64(host64); } inline uint32_t hostToNetwork32(uint32_t host32) { - return htobe32(host32); + return bswap_32(host32); } inline uint16_t hostToNetwork16(uint16_t host16) { - return htobe16(host16); + return bswap_16(host16); } inline uint64_t networkToHost64(uint64_t net64) { - return be64toh(net64); + return bswap_64(net64); } inline uint32_t networkToHost32(uint32_t net32) { - return be32toh(net32); + return bswap_32(net32); } inline uint16_t networkToHost16(uint16_t net16) { - return be16toh(net16); + return bswap_16(net16); } -#if __GNUC_MINOR__ >= 6 -#pragma GCC diagnostic pop -#else -#pragma GCC diagnostic error "-Wconversion" -#pragma GCC diagnostic error "-Wold-style-cast" #endif - } } } diff --git a/muduo/net/EventLoop.cc b/muduo/net/EventLoop.cc --- a/muduo/net/EventLoop.cc +++ b/muduo/net/EventLoop.cc @@ -18,7 +18,8 @@ #include <boost/bind.hpp> #include <signal.h> -#include <sys/eventfd.h> +#include <sys/types.h> +#include <sys/socket.h> using namespace muduo; using namespace muduo::net; @@ -29,18 +30,6 @@ __thread EventLoop* t_loopInThisThread = 0; const int kPollTimeMs = 10000; -int createEventfd() -{ - int evtfd = ::eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); - if (evtfd < 0) - { - LOG_SYSERR << "Failed in eventfd"; - abort(); - } - return evtfd; -} - -#pragma GCC diagnostic ignored "-Wold-style-cast" class IgnoreSigPipe { public: @@ -50,7 +39,6 @@ class IgnoreSigPipe LOG_TRACE << "Ignore SIGPIPE"; } }; -#pragma GCC diagnostic error "-Wold-style-cast" IgnoreSigPipe initObj; } @@ -69,11 +57,17 @@ EventLoop::EventLoop() threadId_(CurrentThread::tid()), poller_(Poller::newDefaultPoller(this)), timerQueue_(new TimerQueue(this)), - wakeupFd_(createEventfd()), - wakeupChannel_(new Channel(this, wakeupFd_)), currentActiveChannel_(NULL) { LOG_DEBUG << "EventLoop created " << this << " in thread " << threadId_; + if (::socketpair(AF_UNIX, SOCK_STREAM, 0, wakeupFd_) < 0) + { + LOG_SYSFATAL << "Failed in socketpair"; + } + sockets::setNonBlockAndCloseOnExec(wakeupFd_[0]); + sockets::setNonBlockAndCloseOnExec(wakeupFd_[1]); + wakeupChannel_.reset(new Channel(this, wakeupFd_[0])); + if (t_loopInThisThread) { LOG_FATAL << "Another EventLoop " << t_loopInThisThread @@ -93,7 +87,8 @@ EventLoop::~EventLoop() { LOG_DEBUG << "EventLoop " << this << " of thread " << threadId_ << " destructs in thread " << CurrentThread::tid(); - ::close(wakeupFd_); + ::close(wakeupFd_[0]); + ::close(wakeupFd_[1]); t_loopInThisThread = NULL; } @@ -108,12 +103,13 @@ void EventLoop::loop() while (!quit_) { activeChannels_.clear(); - pollReturnTime_ = poller_->poll(kPollTimeMs, &activeChannels_); + pollReturnTime_ = poller_->poll(timerQueue_->getTimeout(), &activeChannels_); ++iteration_; if (Logger::logLevel() <= Logger::TRACE) { printActiveChannels(); } + timerQueue_->processTimers(); // TODO sort channel by priority eventHandling_ = true; for (ChannelList::iterator it = activeChannels_.begin(); @@ -215,22 +211,19 @@ void EventLoop::abortNotInLoopThread() void EventLoop::wakeup() { - uint64_t one = 1; - ssize_t n = sockets::write(wakeupFd_, &one, sizeof one); + char one = '1'; + ssize_t n = sockets::write(wakeupFd_[1], &one, sizeof one); if (n != sizeof one) { - LOG_ERROR << "EventLoop::wakeup() writes " << n << " bytes instead of 8"; + LOG_ERROR << "EventLoop::wakeup() writes " << n << " bytes instead of 1"; } } void EventLoop::handleRead() { - uint64_t one = 1; - ssize_t n = sockets::read(wakeupFd_, &one, sizeof one); - if (n != sizeof one) - { - LOG_ERROR << "EventLoop::handleRead() reads " << n << " bytes instead of 8"; - } + char buf[4096] = { 0 }; + ssize_t n = sockets::read(wakeupFd_[0], buf, sizeof buf); + (void)n; } void EventLoop::doPendingFunctors() diff --git a/muduo/net/EventLoop.h b/muduo/net/EventLoop.h --- a/muduo/net/EventLoop.h +++ b/muduo/net/EventLoop.h @@ -130,7 +130,7 @@ class EventLoop : boost::noncopyable Timestamp pollReturnTime_; boost::scoped_ptr<Poller> poller_; boost::scoped_ptr<TimerQueue> timerQueue_; - int wakeupFd_; + int wakeupFd_[2]; // unlike in TimerQueue, which is an internal class, // we don't expose Channel to client. boost::scoped_ptr<Channel> wakeupChannel_; diff --git a/muduo/net/InetAddress.cc b/muduo/net/InetAddress.cc --- a/muduo/net/InetAddress.cc +++ b/muduo/net/InetAddress.cc @@ -17,9 +17,7 @@ #include <boost/static_assert.hpp> // INADDR_ANY use (type)value casting. -#pragma GCC diagnostic ignored "-Wold-style-cast" static const in_addr_t kInaddrAny = INADDR_ANY; -#pragma GCC diagnostic error "-Wold-style-cast" // /* Structure describing an Internet socket address. */ // struct sockaddr_in { diff --git a/muduo/net/SocketsOps.cc b/muduo/net/SocketsOps.cc --- a/muduo/net/SocketsOps.cc +++ b/muduo/net/SocketsOps.cc @@ -37,7 +37,9 @@ SA* sockaddr_cast(struct sockaddr_in* addr) return static_cast<SA*>(implicit_cast<void*>(addr)); } -void setNonBlockAndCloseOnExec(int sockfd) +} + +void sockets::setNonBlockAndCloseOnExec(int sockfd) { // non-block int flags = ::fcntl(sockfd, F_GETFL, 0); @@ -54,12 +56,9 @@ void setNonBlockAndCloseOnExec(int sockfd) (void)ret; } -} - int sockets::createNonblockingOrDie() { // socket -#if VALGRIND int sockfd = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (sockfd < 0) { @@ -67,13 +66,6 @@ int sockets::createNonblockingOrDie() } setNonBlockAndCloseOnExec(sockfd); -#else - int sockfd = ::socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, IPPROTO_TCP); - if (sockfd < 0) - { - LOG_SYSFATAL << "sockets::createNonblockingOrDie"; - } -#endif return sockfd; } @@ -98,13 +90,8 @@ void sockets::listenOrDie(int sockfd) int sockets::accept(int sockfd, struct sockaddr_in* addr) { socklen_t addrlen = static_cast<socklen_t>(sizeof *addr); -#if VALGRIND int connfd = ::accept(sockfd, sockaddr_cast(addr), &addrlen); setNonBlockAndCloseOnExec(connfd); -#else - int connfd = ::accept4(sockfd, sockaddr_cast(addr), - &addrlen, SOCK_NONBLOCK | SOCK_CLOEXEC); -#endif if (connfd < 0) { int savedErrno = errno; diff --git a/muduo/net/SocketsOps.h b/muduo/net/SocketsOps.h --- a/muduo/net/SocketsOps.h +++ b/muduo/net/SocketsOps.h @@ -25,6 +25,8 @@ namespace sockets /// abort if any error. int createNonblockingOrDie(); +void setNonBlockAndCloseOnExec(int sockfd); + int connect(int sockfd, const struct sockaddr_in& addr); void bindOrDie(int sockfd, const struct sockaddr_in& addr); void listenOrDie(int sockfd); diff --git a/muduo/net/TimerQueue.cc b/muduo/net/TimerQueue.cc --- a/muduo/net/TimerQueue.cc +++ b/muduo/net/TimerQueue.cc @@ -16,8 +16,6 @@ #include <boost/bind.hpp> -#include <sys/timerfd.h> - namespace muduo { namespace net @@ -25,57 +23,15 @@ namespace net namespace detail { -int createTimerfd() -{ - int timerfd = ::timerfd_create(CLOCK_MONOTONIC, - TFD_NONBLOCK | TFD_CLOEXEC); - if (timerfd < 0) - { - LOG_SYSFATAL << "Failed in timerfd_create"; - } - return timerfd; -} - -struct timespec howMuchTimeFromNow(Timestamp when) +int howMuchTimeFromNow(Timestamp when) { int64_t microseconds = when.microSecondsSinceEpoch() - Timestamp::now().microSecondsSinceEpoch(); - if (microseconds < 100) - { - microseconds = 100; - } - struct timespec ts; - ts.tv_sec = static_cast<time_t>( - microseconds / Timestamp::kMicroSecondsPerSecond); - ts.tv_nsec = static_cast<long>( - (microseconds % Timestamp::kMicroSecondsPerSecond) * 1000); - return ts; -} - -void readTimerfd(int timerfd, Timestamp now) -{ - uint64_t howmany; - ssize_t n = ::read(timerfd, &howmany, sizeof howmany); - LOG_TRACE << "TimerQueue::handleRead() " << howmany << " at " << now.toString(); - if (n != sizeof howmany) - { - LOG_ERROR << "TimerQueue::handleRead() reads " << n << " bytes instead of 8"; - } -} - -void resetTimerfd(int timerfd, Timestamp expiration) -{ - // wake up loop by timerfd_settime() - struct itimerspec newValue; - struct itimerspec oldValue; - bzero(&newValue, sizeof newValue); - bzero(&oldValue, sizeof oldValue); - newValue.it_value = howMuchTimeFromNow(expiration); - int ret = ::timerfd_settime(timerfd, 0, &newValue, &oldValue); - if (ret) + if (microseconds < 1000) { - LOG_SYSERR << "timerfd_settime()"; + microseconds = 1000; } + return static_cast<int>(microseconds / 1000); } } @@ -88,20 +44,13 @@ using namespace muduo::net::detail; TimerQueue::TimerQueue(EventLoop* loop) : loop_(loop), - timerfd_(createTimerfd()), - timerfdChannel_(loop, timerfd_), timers_(), callingExpiredTimers_(false) { - timerfdChannel_.setReadCallback( - boost::bind(&TimerQueue::handleRead, this)); - // we are always reading the timerfd, we disarm it with timerfd_settime. - timerfdChannel_.enableReading(); } TimerQueue::~TimerQueue() { - ::close(timerfd_); // do not remove channel, since we're in EventLoop::dtor(); for (TimerList::iterator it = timers_.begin(); it != timers_.end(); ++it) @@ -129,11 +78,19 @@ void TimerQueue::cancel(TimerId timerId) void TimerQueue::addTimerInLoop(Timer* timer) { loop_->assertInLoopThread(); - bool earliestChanged = insert(timer); + insert(timer); +} - if (earliestChanged) +int TimerQueue::getTimeout() const +{ + loop_->assertInLoopThread(); + if (timers_.empty()) + { + return 10000; + } + else { - resetTimerfd(timerfd_, timer->expiration()); + return howMuchTimeFromNow(timers_.begin()->second->expiration()); } } @@ -157,11 +114,10 @@ void TimerQueue::cancelInLoop(TimerId timerId) assert(timers_.size() == activeTimers_.size()); } -void TimerQueue::handleRead() +void TimerQueue::processTimers() { loop_->assertInLoopThread(); Timestamp now(Timestamp::now()); - readTimerfd(timerfd_, now); std::vector<Entry> expired = getExpired(now); @@ -225,11 +181,6 @@ void TimerQueue::reset(const std::vector<Entry>& expired, Timestamp now) { nextExpire = timers_.begin()->second->expiration(); } - - if (nextExpire.valid()) - { - resetTimerfd(timerfd_, nextExpire); - } } bool TimerQueue::insert(Timer* timer) diff --git a/muduo/net/TimerQueue.h b/muduo/net/TimerQueue.h --- a/muduo/net/TimerQueue.h +++ b/muduo/net/TimerQueue.h @@ -51,6 +51,9 @@ class TimerQueue : boost::noncopyable void cancel(TimerId timerId); + int getTimeout() const; + void processTimers(); + private: // FIXME: use unique_ptr<Timer> instead of raw pointers. @@ -61,8 +64,6 @@ class TimerQueue : boost::noncopyable void addTimerInLoop(Timer* timer); void cancelInLoop(TimerId timerId); - // called when timerfd alarms - void handleRead(); // move out all expired timers std::vector<Entry> getExpired(Timestamp now); void reset(const std::vector<Entry>& expired, Timestamp now); @@ -70,9 +71,6 @@ class TimerQueue : boost::noncopyable bool insert(Timer* timer); EventLoop* loop_; - const int timerfd_; - Channel timerfdChannel_; - // Timer list sorted by expiration TimerList timers_; // for cancel() diff --git a/muduo/net/poller/EPollPoller.cc b/muduo/net/poller/EPollPoller.cc --- a/muduo/net/poller/EPollPoller.cc +++ b/muduo/net/poller/EPollPoller.cc @@ -26,7 +26,6 @@ using namespace muduo::net; BOOST_STATIC_ASSERT(EPOLLIN == POLLIN); BOOST_STATIC_ASSERT(EPOLLPRI == POLLPRI); BOOST_STATIC_ASSERT(EPOLLOUT == POLLOUT); -BOOST_STATIC_ASSERT(EPOLLRDHUP == POLLRDHUP); BOOST_STATIC_ASSERT(EPOLLERR == POLLERR); BOOST_STATIC_ASSERT(EPOLLHUP == POLLHUP); @@ -39,7 +38,7 @@ const int kDeleted = 2; EPollPoller::EPollPoller(EventLoop* loop) : Poller(loop), - epollfd_(::epoll_create1(EPOLL_CLOEXEC)), + epollfd_(::epoll_create(32)), events_(kInitEventListSize) { if (epollfd_ < 0) |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |