代码拉取完成,页面将自动刷新
diff --git a/CMakeLists.txt b/CMakeLists.txt
index a3066c5..4d887e5 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 2.6)
-project(muduo CXX)
+project(muduo C CXX)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Debug")
@@ -9,14 +9,15 @@ endif()
set(CXX_FLAGS
-g
# -DVALGRIND
- # -DMUDUO_STD_STRING
- -DCHECK_PTHREAD_RETURN_VALUE
+ # -DCHECK_PTHREAD_RETURN_VALUE
+ -DMUDUO_STD_STRING
-D_FILE_OFFSET_BITS=64
-Wall
-Wextra
- -Werror
+ # -Werror
-Wconversion
-Wno-unused-parameter
+ -Wno-sign-conversion
-Wold-style-cast
-Woverloaded-virtual
-Wpointer-arith
@@ -25,16 +26,15 @@ set(CXX_FLAGS
-march=native
# -MMD
# -std=c++0x
- -rdynamic
)
if(CMAKE_BUILD_BITS EQUAL 32)
list(APPEND CXX_FLAGS "-m32")
endif()
string(REPLACE ";" " " CMAKE_CXX_FLAGS "${CXX_FLAGS}")
-set(CMAKE_CXX_COMPILER "g++")
+set(CMAKE_CXX_COMPILER "clang++")
set(CMAKE_CXX_FLAGS_DEBUG "-O0")
-set(CMAKE_CXX_FLAGS_RELEASE "-O2 -finline-limit=1000 -DNDEBUG")
+set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG")
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
diff --git a/examples/roundtrip/roundtrip_udp.cc b/examples/roundtrip/roundtrip_udp.cc
index 5f171b8..d612570 100644
--- a/examples/roundtrip/roundtrip_udp.cc
+++ b/examples/roundtrip/roundtrip_udp.cc
@@ -17,7 +17,12 @@ const size_t frameLen = 2*sizeof(int64_t);
int createNonblockingUDP()
{
+#ifndef __MACH__
int sockfd = ::socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC, IPPROTO_UDP);
+#else
+ int sockfd = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
+ sockets::setNonBlockAndCloseOnExec(sockfd);
+#endif
if (sockfd < 0)
{
LOG_SYSFATAL << "::socket";
diff --git a/examples/socks4a/tcprelay.cc b/examples/socks4a/tcprelay.cc
index a4c6ec9..09a6a3a 100644
--- a/examples/socks4a/tcprelay.cc
+++ b/examples/socks4a/tcprelay.cc
@@ -1,6 +1,5 @@
#include "tunnel.h"
-#include <malloc.h>
#include <stdio.h>
#include <sys/resource.h>
@@ -43,7 +42,6 @@ void onServerMessage(const TcpConnectionPtr& conn, Buffer* buf, Timestamp)
void memstat()
{
- malloc_stats();
}
int main(int argc, char* argv[])
diff --git a/muduo/base/CMakeLists.txt b/muduo/base/CMakeLists.txt
index 6e250d4..2822c4f 100644
--- a/muduo/base/CMakeLists.txt
+++ b/muduo/base/CMakeLists.txt
@@ -16,7 +16,7 @@ set(base_SRCS
)
add_library(muduo_base ${base_SRCS})
-target_link_libraries(muduo_base pthread rt)
+target_link_libraries(muduo_base pthread)
add_library(muduo_base_cpp11 ${base_SRCS})
target_link_libraries(muduo_base_cpp11 pthread rt)
diff --git a/muduo/base/Condition.cc b/muduo/base/Condition.cc
index f10ace3..73a1715 100644
--- a/muduo/base/Condition.cc
+++ b/muduo/base/Condition.cc
@@ -6,13 +6,21 @@
#include <muduo/base/Condition.h>
#include <errno.h>
+#include <sys/time.h>
// returns true if time out, false otherwise.
bool muduo::Condition::waitForSeconds(int seconds)
{
struct timespec abstime;
+#ifdef CLOCK_REALTIME
// FIXME: use CLOCK_MONOTONIC or CLOCK_MONOTONIC_RAW to prevent time rewind.
clock_gettime(CLOCK_REALTIME, &abstime);
+#else // Mac OS X
+ struct timeval tv;
+ gettimeofday(&tv, NULL);
+ abstime.tv_sec = tv.tv_sec;
+ abstime.tv_nsec = tv.tv_usec * 1000;
+#endif
abstime.tv_sec += seconds;
MutexLock::UnassignGuard ug(mutex_);
return ETIMEDOUT == pthread_cond_timedwait(&pcond_, mutex_.getPthreadMutex(), &abstime);
diff --git a/muduo/base/FileUtil.cc b/muduo/base/FileUtil.cc
index 4d338d8..a4c2515 100644
--- a/muduo/base/FileUtil.cc
+++ b/muduo/base/FileUtil.cc
@@ -64,8 +64,12 @@ void FileUtil::AppendFile::flush()
size_t FileUtil::AppendFile::write(const char* logline, size_t len)
{
+#ifdef fwrite_unlocked
// #undef fwrite_unlocked
return ::fwrite_unlocked(logline, 1, len, fp_);
+#else
+ return ::fwrite(logline, 1, len, fp_);
+#endif
}
FileUtil::ReadSmallFile::ReadSmallFile(StringPiece filename)
diff --git a/muduo/base/Logging.cc b/muduo/base/Logging.cc
index d6b9a2a..21fa5b6 100644
--- a/muduo/base/Logging.cc
+++ b/muduo/base/Logging.cc
@@ -36,7 +36,12 @@ __thread time_t t_lastSecond;
const char* strerror_tl(int savedErrno)
{
+#ifndef __MACH__
return strerror_r(savedErrno, t_errnobuf, sizeof t_errnobuf);
+#else
+ strerror_r(savedErrno, t_errnobuf, sizeof t_errnobuf);
+ return t_errnobuf;
+#endif
}
Logger::LogLevel initLogLevel()
diff --git a/muduo/base/Thread.cc b/muduo/base/Thread.cc
index b5e7ed7..1274bea 100644
--- a/muduo/base/Thread.cc
+++ b/muduo/base/Thread.cc
@@ -17,7 +17,9 @@
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>
+#ifndef __MACH__
#include <linux/unistd.h>
+#endif
namespace muduo
{
@@ -33,10 +35,17 @@ namespace CurrentThread
namespace detail
{
+#ifdef __MACH__
+pid_t gettid()
+{
+ return pthread_mach_thread_np(pthread_self());
+}
+#else
pid_t gettid()
{
return static_cast<pid_t>(::syscall(SYS_gettid));
}
+#endif
void afterFork()
{
diff --git a/muduo/base/TimeZone.cc b/muduo/base/TimeZone.cc
index 5fd9e28..008b931 100644
--- a/muduo/base/TimeZone.cc
+++ b/muduo/base/TimeZone.cc
@@ -8,7 +8,7 @@
#include <vector>
//#define _BSD_SOURCE
-#include <endian.h>
+#include <muduo/net/Endian.h>
#include <stdint.h>
#include <stdio.h>
@@ -278,7 +278,7 @@ struct tm TimeZone::toLocalTime(time_t seconds) const
::gmtime_r(&localSeconds, &localTime); // FIXME: fromUtcTime
localTime.tm_isdst = local->isDst;
localTime.tm_gmtoff = local->gmtOffset;
- localTime.tm_zone = &data.abbreviation[local->arrbIdx];
+ localTime.tm_zone = const_cast<char*>(&data.abbreviation[local->arrbIdx]);
}
return localTime;
diff --git a/muduo/base/tests/AsyncLogging_test.cc b/muduo/base/tests/AsyncLogging_test.cc
index bd9fe59..e510fd4 100644
--- a/muduo/base/tests/AsyncLogging_test.cc
+++ b/muduo/base/tests/AsyncLogging_test.cc
@@ -4,6 +4,9 @@
#include <stdio.h>
#include <sys/resource.h>
+#ifdef __MACH__
+#include <libgen.h> // basename()
+#endif
int kRollSize = 500*1000*1000;
diff --git a/muduo/base/tests/LogFile_test.cc b/muduo/base/tests/LogFile_test.cc
index e77d68d..d27d65e 100644
--- a/muduo/base/tests/LogFile_test.cc
+++ b/muduo/base/tests/LogFile_test.cc
@@ -1,5 +1,8 @@
#include <muduo/base/LogFile.h>
#include <muduo/base/Logging.h>
+#ifdef __MACH__
+#include <libgen.h> // basename()
+#endif
boost::scoped_ptr<muduo::LogFile> g_logFile;
diff --git a/muduo/net/CMakeLists.txt b/muduo/net/CMakeLists.txt
index 9889643..b5fcd44 100644
--- a/muduo/net/CMakeLists.txt
+++ b/muduo/net/CMakeLists.txt
@@ -9,7 +9,6 @@ set(net_SRCS
InetAddress.cc
Poller.cc
poller/DefaultPoller.cc
- poller/EPollPoller.cc
poller/PollPoller.cc
Socket.cc
SocketsOps.cc
diff --git a/muduo/net/Channel.cc b/muduo/net/Channel.cc
index 20ba82a..cfcea14 100644
--- a/muduo/net/Channel.cc
+++ b/muduo/net/Channel.cc
@@ -94,6 +94,9 @@ void Channel::handleEventWithGuard(Timestamp receiveTime)
{
if (errorCallback_) errorCallback_();
}
+#ifndef POLLRDHUP
+ const int POLLRDHUP = 0;
+#endif
if (revents_ & (POLLIN | POLLPRI | POLLRDHUP))
{
if (readCallback_) readCallback_(receiveTime);
@@ -117,8 +120,10 @@ string Channel::reventsToString() const
oss << "OUT ";
if (revents_ & POLLHUP)
oss << "HUP ";
+#ifdef POLLRDHUP
if (revents_ & POLLRDHUP)
oss << "RDHUP ";
+#endif
if (revents_ & POLLERR)
oss << "ERR ";
if (revents_ & POLLNVAL)
diff --git a/muduo/net/Endian.h b/muduo/net/Endian.h
index b277503..851e449 100644
--- a/muduo/net/Endian.h
+++ b/muduo/net/Endian.h
@@ -12,7 +12,28 @@
#define MUDUO_NET_ENDIAN_H
#include <stdint.h>
+
+#ifdef __MACH__
+#include <libkern/OSByteOrder.h>
+
+#define htobe16(x) OSSwapHostToBigInt16(x)
+#define htole16(x) OSSwapHostToLittleInt16(x)
+#define be16toh(x) OSSwapBigToHostInt16(x)
+#define le16toh(x) OSSwapLittleToHostInt16(x)
+
+#define htobe32(x) OSSwapHostToBigInt32(x)
+#define htole32(x) OSSwapHostToLittleInt32(x)
+#define be32toh(x) OSSwapBigToHostInt32(x)
+#define le32toh(x) OSSwapLittleToHostInt32(x)
+
+#define htobe64(x) OSSwapHostToBigInt64(x)
+#define htole64(x) OSSwapHostToLittleInt64(x)
+#define be64toh(x) OSSwapBigToHostInt64(x)
+#define le64toh(x) OSSwapLittleToHostInt64(x)
+#else
#include <endian.h>
+#endif
+
namespace muduo
{
@@ -60,8 +81,8 @@ inline uint16_t networkToHost16(uint16_t net16)
#if defined(__clang__) || __GNUC_MINOR__ >= 6
#pragma GCC diagnostic pop
#else
-#pragma GCC diagnostic warning "-Wconversion"
-#pragma GCC diagnostic warning "-Wold-style-cast"
+//#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
index 49e76bf..e18e97e 100644
--- 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,15 @@ 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";
+ }
+ wakeupChannel_.reset(new Channel(this, wakeupFd_[0]));
+
if (t_loopInThisThread)
{
LOG_FATAL << "Another EventLoop " << t_loopInThisThread
@@ -93,7 +85,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 +101,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();
@@ -264,7 +258,7 @@ void EventLoop::abortNotInLoopThread()
void EventLoop::wakeup()
{
uint64_t one = 1;
- ssize_t n = sockets::write(wakeupFd_, &one, sizeof one);
+ ssize_t n = sockets::write(wakeupFd_[1], &one, sizeof one);
if (n != sizeof one)
{
LOG_ERROR << "EventLoop::wakeup() writes " << n << " bytes instead of 8";
@@ -274,7 +268,7 @@ void EventLoop::wakeup()
void EventLoop::handleRead()
{
uint64_t one = 1;
- ssize_t n = sockets::read(wakeupFd_, &one, sizeof one);
+ ssize_t n = sockets::read(wakeupFd_[0], &one, sizeof one);
if (n != sizeof one)
{
LOG_ERROR << "EventLoop::handleRead() reads " << n << " bytes instead of 8";
diff --git a/muduo/net/EventLoop.h b/muduo/net/EventLoop.h
index 2776828..0145b13 100644
--- a/muduo/net/EventLoop.h
+++ b/muduo/net/EventLoop.h
@@ -145,7 +145,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
index 24db014..13352cc 100644
--- a/muduo/net/InetAddress.cc
+++ b/muduo/net/InetAddress.cc
@@ -19,10 +19,10 @@
#include <boost/static_assert.hpp>
// INADDR_ANY use (type)value casting.
-#pragma GCC diagnostic ignored "-Wold-style-cast"
+// #pragma GCC diagnostic ignored "-Wold-style-cast"
static const in_addr_t kInaddrAny = INADDR_ANY;
static const in_addr_t kInaddrLoopback = INADDR_LOOPBACK;
-#pragma GCC diagnostic error "-Wold-style-cast"
+// #pragma GCC diagnostic error "-Wold-style-cast"
// /* Structure describing an Internet socket address. */
// struct sockaddr_in {
@@ -78,10 +78,15 @@ bool InetAddress::resolve(const char* hostname, InetAddress* out)
assert(out != NULL);
struct hostent hent;
struct hostent* he = NULL;
- int herrno = 0;
bzero(&hent, sizeof(hent));
+#ifndef __MACH__
+ int herrno = 0;
int ret = gethostbyname_r(hostname, &hent, t_resolveBuffer, sizeof t_resolveBuffer, &he, &herrno);
+#else
+ he = gethostbyname(hostname);
+ int ret = 0;
+#endif
if (ret == 0 && he != NULL)
{
assert(he->h_addrtype == AF_INET && he->h_length == sizeof(uint32_t));
diff --git a/muduo/net/SocketsOps.cc b/muduo/net/SocketsOps.cc
index 7839754..73a021b 100644
--- a/muduo/net/SocketsOps.cc
+++ b/muduo/net/SocketsOps.cc
@@ -17,12 +17,15 @@
#include <stdio.h> // snprintf
#include <strings.h> // bzero
#include <sys/socket.h>
+#ifdef __MACH__
+#include <sys/uio.h> // readv
+#endif
#include <unistd.h>
using namespace muduo;
using namespace muduo::net;
-namespace
+namespace muduo
{
typedef struct sockaddr SA;
@@ -37,7 +40,12 @@ SA* sockaddr_cast(struct sockaddr_in* addr)
return static_cast<SA*>(implicit_cast<void*>(addr));
}
-#if VALGRIND
+#if VALGRIND || __MACH__
+namespace net
+{
+namespace sockets
+{
+
void setNonBlockAndCloseOnExec(int sockfd)
{
// non-block
@@ -54,13 +62,15 @@ void setNonBlockAndCloseOnExec(int sockfd)
(void)ret;
}
+
+}
+}
#endif
}
int sockets::createNonblockingOrDie()
{
-#if VALGRIND
int sockfd = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sockfd < 0)
{
@@ -68,13 +78,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;
}
@@ -99,13 +102,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
index fd2db91..ee2c78d 100644
--- a/muduo/net/SocketsOps.h
+++ b/muduo/net/SocketsOps.h
@@ -24,6 +24,9 @@ namespace sockets
/// Creates a non-blocking socket file descriptor,
/// abort if any error.
int createNonblockingOrDie();
+#ifdef __MACH__
+void setNonBlockAndCloseOnExec(int sockfd);
+#endif
int connect(int sockfd, const struct sockaddr_in& addr);
void bindOrDie(int sockfd, const struct sockaddr_in& addr);
diff --git a/muduo/net/TimerQueue.cc b/muduo/net/TimerQueue.cc
index 9259c01..f6d241d 100644
--- 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)
@@ -141,11 +90,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());
}
}
@@ -169,11 +126,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);
@@ -237,11 +193,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
index 0cfb02f..d882b71 100644
--- a/muduo/net/TimerQueue.h
+++ b/muduo/net/TimerQueue.h
@@ -56,6 +56,9 @@ class TimerQueue : boost::noncopyable
void cancel(TimerId timerId);
+ int getTimeout() const;
+ void processTimers();
+
private:
// FIXME: use unique_ptr<Timer> instead of raw pointers.
@@ -66,8 +69,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);
@@ -75,9 +76,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/DefaultPoller.cc b/muduo/net/poller/DefaultPoller.cc
index f42f5a4..a6a3133 100644
--- a/muduo/net/poller/DefaultPoller.cc
+++ b/muduo/net/poller/DefaultPoller.cc
@@ -16,6 +16,9 @@ using namespace muduo::net;
Poller* Poller::newDefaultPoller(EventLoop* loop)
{
+#ifdef __MACH__
+ return new PollPoller(loop);
+#else
if (::getenv("MUDUO_USE_POLL"))
{
return new PollPoller(loop);
@@ -24,4 +27,5 @@ Poller* Poller::newDefaultPoller(EventLoop* loop)
{
return new EPollPoller(loop);
}
+#endif
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。