forked from chenshuo/muduo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MacOSX.diff
854 lines (774 loc) · 23 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
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
diff --git a/CMakeLists.txt b/CMakeLists.txt
index d18840f..8c9f075 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -11,14 +11,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
@@ -27,16 +28,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..a76404f 100644
--- a/muduo/base/CMakeLists.txt
+++ b/muduo/base/CMakeLists.txt
@@ -16,10 +16,10 @@ 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)
+target_link_libraries(muduo_base_cpp11 pthread)
set_target_properties(muduo_base_cpp11 PROPERTIES COMPILE_FLAGS "-std=c++0x")
install(TARGETS muduo_base DESTINATION lib)
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 999b0e5..ad9da8a 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(StringArg filename)
diff --git a/muduo/base/Logging.cc b/muduo/base/Logging.cc
index 341d627..9376007 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 9d64780..cddcac1 100644
--- a/muduo/base/Thread.cc
+++ b/muduo/base/Thread.cc
@@ -15,10 +15,12 @@
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
-#include <sys/prctl.h>
#include <sys/syscall.h>
#include <sys/types.h>
+#ifndef __MACH__
+#include <sys/prctl.h>
#include <linux/unistd.h>
+#endif
namespace muduo
{
@@ -35,10 +37,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()
{
@@ -88,7 +97,9 @@ struct ThreadData
}
muduo::CurrentThread::t_threadName = name_.empty() ? "muduoThread" : name_.c_str();
+#ifndef __MACH__
::prctl(PR_SET_NAME, muduo::CurrentThread::t_threadName);
+#endif
try
{
func_();
diff --git a/muduo/base/TimeZone.cc b/muduo/base/TimeZone.cc
index 37959d9..f95beb3 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>
@@ -285,7 +285,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/BlockingQueue_test.cc b/muduo/base/tests/BlockingQueue_test.cc
index c392773..6977578 100644
--- a/muduo/base/tests/BlockingQueue_test.cc
+++ b/muduo/base/tests/BlockingQueue_test.cc
@@ -80,9 +80,6 @@ class Test
void testMove()
{
#ifdef __GXX_EXPERIMENTAL_CXX0X__
-
-// std::unique_ptr requires gcc 4.4 or later
-#if __GNUC_PREREQ (4,4)
muduo::BlockingQueue<std::unique_ptr<int>> queue;
queue.put(std::unique_ptr<int>(new int(42)));
std::unique_ptr<int> x = queue.take();
@@ -92,8 +89,6 @@ void testMove()
std::unique_ptr<int> y = queue.take();
printf("took %d\n", *y);
#endif
-
-#endif
}
int main()
diff --git a/muduo/base/tests/GzipFile_test.cc b/muduo/base/tests/GzipFile_test.cc
index 6dc0d4d..b051ca8 100644
--- a/muduo/base/tests/GzipFile_test.cc
+++ b/muduo/base/tests/GzipFile_test.cc
@@ -2,6 +2,8 @@
#include <muduo/base/Logging.h>
+#include <errno.h>
+
int main()
{
const char* filename = "/tmp/gzipfile_test.gz";
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 0127c48..9ea16ed 100644
--- a/muduo/net/CMakeLists.txt
+++ b/muduo/net/CMakeLists.txt
@@ -16,7 +16,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 f5e6624..62fbd6f 100644
--- a/muduo/net/Channel.cc
+++ b/muduo/net/Channel.cc
@@ -102,6 +102,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);
@@ -135,8 +138,10 @@ string Channel::eventsToString(int fd, int ev)
oss << "OUT ";
if (ev & POLLHUP)
oss << "HUP ";
+#ifdef POLLRDHUP
if (ev & POLLRDHUP)
oss << "RDHUP ";
+#endif
if (ev & POLLERR)
oss << "ERR ";
if (ev & 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 7346838..19fde05 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
@@ -95,7 +87,8 @@ EventLoop::~EventLoop()
<< " destructs in thread " << CurrentThread::tid();
wakeupChannel_->disableAll();
wakeupChannel_->remove();
- ::close(wakeupFd_);
+ ::close(wakeupFd_[0]);
+ ::close(wakeupFd_[1]);
t_loopInThisThread = NULL;
}
@@ -110,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();
@@ -273,7 +267,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";
@@ -283,7 +277,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 5741961..b03dd67 100644
--- a/muduo/net/EventLoop.h
+++ b/muduo/net/EventLoop.h
@@ -156,7 +156,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 394870a..05bb5de 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 {
@@ -83,10 +83,15 @@ bool InetAddress::resolve(StringArg 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.c_str(), &hent, t_resolveBuffer, sizeof t_resolveBuffer, &he, &herrno);
+#else
+ he = gethostbyname(hostname.c_str());
+ 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/Socket.cc b/muduo/net/Socket.cc
index 111d87d..4e0efa7 100644
--- a/muduo/net/Socket.cc
+++ b/muduo/net/Socket.cc
@@ -27,13 +27,18 @@ Socket::~Socket()
bool Socket::getTcpInfo(struct tcp_info* tcpi) const
{
+#ifndef __MACH__
socklen_t len = sizeof(*tcpi);
bzero(tcpi, len);
return ::getsockopt(sockfd_, SOL_TCP, TCP_INFO, tcpi, &len) == 0;
+#else
+ return false;
+#endif
}
bool Socket::getTcpInfoString(char* buf, int len) const
{
+#ifndef __MACH__
struct tcp_info tcpi;
bool ok = getTcpInfo(&tcpi);
if (ok)
@@ -56,6 +61,9 @@ bool Socket::getTcpInfoString(char* buf, int len) const
tcpi.tcpi_total_retrans); // Total retransmits for entire connection
}
return ok;
+#else
+ return false;
+#endif
}
void Socket::bindAddress(const InetAddress& addr)
diff --git a/muduo/net/SocketsOps.cc b/muduo/net/SocketsOps.cc
index 188c3cb..1e5f268 100644
--- a/muduo/net/SocketsOps.cc
+++ b/muduo/net/SocketsOps.cc
@@ -17,18 +17,26 @@
#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;
#if VALGRIND || defined (NO_ACCEPT4)
+namespace net
+{
+namespace sockets
+{
+
void setNonBlockAndCloseOnExec(int sockfd)
{
// non-block
@@ -45,6 +53,9 @@ void setNonBlockAndCloseOnExec(int sockfd)
(void)ret;
}
+
+}
+}
#endif
}
@@ -71,7 +82,6 @@ struct sockaddr_in* sockets::sockaddr_in_cast(struct sockaddr* addr)
int sockets::createNonblockingOrDie()
{
-#if VALGRIND
int sockfd = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sockfd < 0)
{
@@ -79,13 +89,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;
}
@@ -110,13 +113,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 || defined (NO_ACCEPT4)
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 a07b1fe..efc779a 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 0f199e5..7f4813a 100644
--- a/muduo/net/TimerQueue.cc
+++ b/muduo/net/TimerQueue.cc
@@ -19,8 +19,6 @@
#include <boost/bind.hpp>
-#include <sys/timerfd.h>
-
namespace muduo
{
namespace net
@@ -28,57 +26,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);
}
}
@@ -91,22 +47,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()
{
- timerfdChannel_.disableAll();
- timerfdChannel_.remove();
- ::close(timerfd_);
// do not remove channel, since we're in EventLoop::dtor();
for (TimerList::iterator it = timers_.begin();
it != timers_.end(); ++it)
@@ -146,11 +93,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());
}
}
@@ -174,11 +129,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);
@@ -242,11 +196,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
}
diff --git a/examples/protobuf/rpcbalancer/balancer_raw.cc b/examples/protobuf/rpcbalancer/balancer_raw.cc
index 9c2e1db..c30b19d 100644
--- a/examples/protobuf/rpcbalancer/balancer_raw.cc
+++ b/examples/protobuf/rpcbalancer/balancer_raw.cc
@@ -12,7 +12,7 @@
#include <boost/bind.hpp>
#include <boost/ptr_container/ptr_vector.hpp>
-#include <endian.h>
+#include <machine/endian.h>
#include <stdio.h>
using namespace muduo;