forked from chenshuo/muduo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MacOSX.diff
671 lines (612 loc) · 18 KB
/
MacOSX.diff
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 2c8880a..3c8c83f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -9,13 +9,14 @@ endif()
set(CXX_FLAGS
-g
# -DVALGRIND
- # -DMUDUO_STD_STRING
+ -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
@@ -24,17 +25,16 @@ 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_COMPILER "icpc")
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/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 36d3b83..69a9661 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)
install(TARGETS muduo_base DESTINATION lib)
file(GLOB HEADERS "*.h")
diff --git a/muduo/base/Condition.cc b/muduo/base/Condition.cc
index 75bae96..11f291a 100644
--- a/muduo/base/Condition.cc
+++ b/muduo/base/Condition.cc
@@ -6,12 +6,20 @@
#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
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/LogFile.cc b/muduo/base/LogFile.cc
index f06ffb1..d22c797 100644
--- a/muduo/base/LogFile.cc
+++ b/muduo/base/LogFile.cc
@@ -60,8 +60,11 @@ class LogFile::File : boost::noncopyable
size_t write(const char* logline, size_t len)
{
-#undef fwrite_unlocked
- return ::fwrite_unlocked(logline, 1, len, fp_);
+#ifdef fwrite_unlocked
+ return fwrite_unlocked(logline, 1, len, fp_);
+#else
+ return fwrite(logline, 1, len, fp_);
+#endif
}
FILE* fp_;
diff --git a/muduo/base/Logging.cc b/muduo/base/Logging.cc
index f710072..8e74f77 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 afbec18..91fe0a7 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 232841a..6e68677 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 d1df80a..7b31926 100644
--- a/muduo/net/Channel.cc
+++ b/muduo/net/Channel.cc
@@ -93,6 +93,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);
@@ -116,8 +119,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 0ca95e9..fa085bd 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 __GNUC_MINOR__ >= 6
#pragma GCC diagnostic pop
#else
-#pragma GCC diagnostic error "-Wconversion"
-#pragma GCC diagnostic error "-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 51dc13d..b8728ea 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();
@@ -216,7 +210,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";
@@ -226,7 +220,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 9d0085b..8420b22 100644
--- 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
index 368b837..f59be46 100644
--- 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
index 24a4433..266f508 100644
--- a/muduo/net/SocketsOps.cc
+++ b/muduo/net/SocketsOps.cc
@@ -17,6 +17,9 @@
#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;
@@ -59,7 +62,6 @@ void setNonBlockAndCloseOnExec(int sockfd)
int sockets::createNonblockingOrDie()
{
// socket
-#if VALGRIND
int sockfd = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sockfd < 0)
{
@@ -67,13 +69,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 +93,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/TimerQueue.cc b/muduo/net/TimerQueue.cc
index 7a95119..707ed31 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)
@@ -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
index 3608928..e2f1788 100644
--- 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/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
}