├── v2 ├── src │ ├── net │ │ ├── config.h │ │ ├── Timer.cpp │ │ ├── notCopyable.h │ │ ├── CountDownLatch.h │ │ ├── TimerId.h │ │ ├── Makefile │ │ ├── FileUtil.h │ │ ├── CountDownLatch.cpp │ │ ├── Endian.h │ │ ├── Condition.cpp │ │ ├── EventLoopThread.h │ │ ├── Condition.h │ │ ├── Timestamp.cpp │ │ ├── Atomic.h │ │ ├── Thread.h │ │ ├── Acceptor.h │ │ ├── CurrentThread.cpp │ │ ├── Poller.h │ │ ├── Socket.h │ │ ├── LogFile.h │ │ └── CurrentThread.h │ ├── build.sh │ ├── clean.sh │ └── app │ │ └── http │ │ ├── Makefile │ │ ├── HttpContext.h │ │ ├── HttpResponse.cpp │ │ └── HttpServer.h ├── resources │ ├── muduo.png │ ├── muduoK.png │ ├── suika.png │ └── suikaK.png ├── README.md └── v2性能评估.md ├── v1 └── muduo++ │ ├── 10 │ ├── config.h │ ├── Timer.cpp │ ├── notCopyable.h │ ├── CountDownLatch.h │ ├── TimerId.h │ ├── FileUtil.h │ ├── CountDownLatch.cpp │ ├── Endian.h │ ├── Condition.cpp │ ├── EventLoopThread.h │ ├── Thread.h │ ├── Timestamp.cpp │ ├── Atomic.h │ ├── Acceptor.h │ ├── CurrentThread.cpp │ ├── Poller.h │ ├── Socket.h │ ├── Condition.h │ ├── LogFile.h │ └── CurrentThread.h │ ├── 11 │ ├── config.h │ ├── core │ ├── Timer.cpp │ ├── notCopyable.h │ ├── CountDownLatch.h │ ├── TimerId.h │ ├── Makefile │ ├── FileUtil.h │ ├── CountDownLatch.cpp │ ├── Endian.h │ ├── Condition.cpp │ ├── EventLoopThread.h │ ├── Thread.h │ ├── Timestamp.cpp │ ├── Atomic.h │ ├── Acceptor.h │ ├── CurrentThread.cpp │ ├── Poller.h │ ├── Socket.h │ ├── Condition.h │ ├── LogFile.h │ └── CurrentThread.h │ ├── 00 │ ├── config.h │ ├── notCopyable.h │ ├── CurrentThread.h │ ├── CurrentThread.cpp │ ├── Thread.h │ ├── Makefile │ ├── EventLoop.h │ ├── main.cpp │ └── EventLoop.cpp │ ├── 01 │ ├── config.h │ ├── Timestamp.cpp │ ├── notCopyable.h │ ├── CurrentThread.h │ ├── CurrentThread.cpp │ ├── Thread.h │ ├── Timestamp.h │ ├── main.cpp │ ├── Makefile │ ├── Poller.h │ ├── Channel.cpp │ └── EventLoop.h │ ├── 02 │ ├── config.h │ ├── Callbacks.h │ ├── Timer.cpp │ ├── notCopyable.h │ ├── CurrentThread.h │ ├── CurrentThread.cpp │ ├── TimerId.h │ ├── Thread.h │ ├── Timestamp.cpp │ ├── Atomic.h │ ├── Poller.h │ ├── main.cpp │ ├── Channel.cpp │ └── Timer.h │ ├── 03 │ ├── config.h │ ├── Callbacks.h │ ├── Timer.cpp │ ├── notCopyable.h │ ├── CurrentThread.h │ ├── CurrentThread.cpp │ ├── CountDownLatch.h │ ├── TimerId.h │ ├── CountDownLatch.cpp │ ├── main.cpp │ ├── Thread.h │ ├── Timestamp.cpp │ ├── Condition.cpp │ ├── EventLoopThread.h │ ├── Atomic.h │ ├── Poller.h │ ├── Condition.h │ ├── Channel.cpp │ └── Timer.h │ ├── 04 │ ├── config.h │ ├── Callbacks.h │ ├── Timer.cpp │ ├── notCopyable.h │ ├── CurrentThread.h │ ├── CurrentThread.cpp │ ├── CountDownLatch.h │ ├── TimerId.h │ ├── CountDownLatch.cpp │ ├── Thread.h │ ├── Timestamp.cpp │ ├── Endian.h │ ├── main.cpp │ ├── Condition.cpp │ ├── EventLoopThread.h │ ├── Atomic.h │ ├── Acceptor.h │ ├── Poller.h │ ├── Socket.h │ ├── Condition.h │ ├── Channel.cpp │ └── Timer.h │ ├── 05 │ ├── config.h │ ├── Timer.cpp │ ├── notCopyable.h │ ├── CurrentThread.h │ ├── CurrentThread.cpp │ ├── CountDownLatch.h │ ├── TimerId.h │ ├── CountDownLatch.cpp │ ├── Thread.h │ ├── Timestamp.cpp │ ├── Endian.h │ ├── Condition.cpp │ ├── EventLoopThread.h │ ├── Atomic.h │ ├── Acceptor.h │ ├── Poller.h │ ├── Socket.h │ ├── Condition.h │ ├── main.cpp │ ├── Callbacks.h │ ├── TcpServer.h │ └── Timer.h │ ├── 06 │ ├── config.h │ ├── Timer.cpp │ ├── notCopyable.h │ ├── CurrentThread.h │ ├── CurrentThread.cpp │ ├── CountDownLatch.h │ ├── TimerId.h │ ├── CountDownLatch.cpp │ ├── Thread.h │ ├── Timestamp.cpp │ ├── Endian.h │ ├── Condition.cpp │ ├── EventLoopThread.h │ ├── Atomic.h │ ├── Acceptor.h │ ├── Poller.h │ ├── Socket.h │ ├── Condition.h │ ├── Callbacks.h │ └── TcpServer.h │ ├── 07 │ ├── config.h │ ├── Timer.cpp │ ├── notCopyable.h │ ├── CurrentThread.h │ ├── CurrentThread.cpp │ ├── CountDownLatch.h │ ├── TimerId.h │ ├── CountDownLatch.cpp │ ├── Thread.h │ ├── Timestamp.cpp │ ├── Endian.h │ ├── Condition.cpp │ ├── EventLoopThread.h │ ├── Atomic.h │ ├── Acceptor.h │ ├── Poller.h │ ├── Socket.h │ └── Condition.h │ ├── 08 │ ├── config.h │ ├── Timer.cpp │ ├── notCopyable.h │ ├── CurrentThread.h │ ├── CurrentThread.cpp │ ├── CountDownLatch.h │ ├── TimerId.h │ ├── CountDownLatch.cpp │ ├── Thread.h │ ├── Timestamp.cpp │ ├── Endian.h │ ├── Condition.cpp │ ├── EventLoopThread.h │ ├── Atomic.h │ ├── Acceptor.h │ ├── Poller.h │ ├── Socket.h │ └── Condition.h │ └── 09 │ ├── config.h │ ├── Timer.cpp │ ├── notCopyable.h │ ├── CurrentThread.h │ ├── CurrentThread.cpp │ ├── CountDownLatch.h │ ├── TimerId.h │ ├── CountDownLatch.cpp │ ├── Thread.h │ ├── Timestamp.cpp │ ├── Endian.h │ ├── Condition.cpp │ ├── EventLoopThread.h │ ├── Atomic.h │ ├── Acceptor.h │ ├── Poller.h │ ├── Socket.h │ └── Condition.h ├── v3 ├── src │ ├── clear.sh │ ├── build.sh │ ├── base │ │ ├── Timer.cpp │ │ ├── notCopyable.h │ │ ├── CountDownLatch.h │ │ ├── Makefile │ │ ├── log │ │ │ ├── FileUtil.h │ │ │ └── LogFile.h │ │ ├── CountDownLatch.cpp │ │ ├── Thread.h │ │ ├── Timestamp.cpp │ │ ├── CurrentThread.cpp │ │ ├── Mutex.h │ │ ├── CurrentThread.h │ │ ├── Condition.h │ │ ├── Buffer.cpp │ │ └── Timer.h │ ├── Makefile │ ├── TcpServer.h │ ├── EventLoopThread.h │ ├── EventLoopThreadPool.h │ ├── EventLoopThread.cpp │ └── EPollPoller.h ├── resources │ ├── muduo-k.png │ ├── muduo-nk.png │ ├── buffer │ │ ├── suika-k.png │ │ └── suika-nk.png │ └── connection_pool │ │ ├── suika-v3-k.png │ │ └── suika-v3-nk.png └── README.md └── README.md /v2/src/net/config.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | -------------------------------------------------------------------------------- /v1/muduo++/00/config.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | -------------------------------------------------------------------------------- /v1/muduo++/01/config.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | -------------------------------------------------------------------------------- /v1/muduo++/02/config.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | -------------------------------------------------------------------------------- /v1/muduo++/03/config.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | -------------------------------------------------------------------------------- /v1/muduo++/04/config.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | -------------------------------------------------------------------------------- /v1/muduo++/05/config.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | -------------------------------------------------------------------------------- /v1/muduo++/06/config.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | -------------------------------------------------------------------------------- /v1/muduo++/07/config.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | -------------------------------------------------------------------------------- /v1/muduo++/08/config.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | -------------------------------------------------------------------------------- /v1/muduo++/09/config.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | -------------------------------------------------------------------------------- /v1/muduo++/10/config.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | -------------------------------------------------------------------------------- /v1/muduo++/11/config.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | -------------------------------------------------------------------------------- /v3/src/clear.sh: -------------------------------------------------------------------------------- 1 | make clean 2 | cd base 3 | make clean 4 | -------------------------------------------------------------------------------- /v2/src/build.sh: -------------------------------------------------------------------------------- 1 | cd net 2 | make 3 | cd ../app/http 4 | make 5 | -------------------------------------------------------------------------------- /v2/src/clean.sh: -------------------------------------------------------------------------------- 1 | cd net 2 | make clean 3 | 4 | cd ../app/http 5 | make clean 6 | -------------------------------------------------------------------------------- /v1/muduo++/11/core: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ChyauAng/suika/HEAD/v1/muduo++/11/core -------------------------------------------------------------------------------- /v2/resources/muduo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ChyauAng/suika/HEAD/v2/resources/muduo.png -------------------------------------------------------------------------------- /v2/resources/muduoK.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ChyauAng/suika/HEAD/v2/resources/muduoK.png -------------------------------------------------------------------------------- /v2/resources/suika.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ChyauAng/suika/HEAD/v2/resources/suika.png -------------------------------------------------------------------------------- /v2/resources/suikaK.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ChyauAng/suika/HEAD/v2/resources/suikaK.png -------------------------------------------------------------------------------- /v3/resources/muduo-k.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ChyauAng/suika/HEAD/v3/resources/muduo-k.png -------------------------------------------------------------------------------- /v3/resources/muduo-nk.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ChyauAng/suika/HEAD/v3/resources/muduo-nk.png -------------------------------------------------------------------------------- /v3/resources/buffer/suika-k.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ChyauAng/suika/HEAD/v3/resources/buffer/suika-k.png -------------------------------------------------------------------------------- /v2/README.md: -------------------------------------------------------------------------------- 1 | # suika: a web server based on muduo++ 2 | ## muduo++: a muduo-like reactor implementation 3 | 4 | -------------------------------------------------------------------------------- /v3/resources/buffer/suika-nk.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ChyauAng/suika/HEAD/v3/resources/buffer/suika-nk.png -------------------------------------------------------------------------------- /v3/resources/connection_pool/suika-v3-k.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ChyauAng/suika/HEAD/v3/resources/connection_pool/suika-v3-k.png -------------------------------------------------------------------------------- /v3/resources/connection_pool/suika-v3-nk.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ChyauAng/suika/HEAD/v3/resources/connection_pool/suika-v3-nk.png -------------------------------------------------------------------------------- /v3/src/build.sh: -------------------------------------------------------------------------------- 1 | rm -rf core 2 | 3 | ulimit -c unlimited 4 | 5 | sh clear.sh 6 | 7 | cd base 8 | make 9 | cd .. 10 | make 11 | 12 | ./HttpServer 4 13 | 14 | -------------------------------------------------------------------------------- /v1/muduo++/02/Callbacks.h: -------------------------------------------------------------------------------- 1 | #ifndef _CALLBACKS_H_ 2 | #define _CALLBACKS_H_ 3 | 4 | #include 5 | #include 6 | #include "Timestamp.h" 7 | 8 | typedef std::function TimerCallback; 9 | 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /v1/muduo++/03/Callbacks.h: -------------------------------------------------------------------------------- 1 | #ifndef _CALLBACKS_H_ 2 | #define _CALLBACKS_H_ 3 | 4 | #include 5 | #include 6 | #include "Timestamp.h" 7 | 8 | typedef std::function TimerCallback; 9 | 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /v1/muduo++/04/Callbacks.h: -------------------------------------------------------------------------------- 1 | #ifndef _CALLBACKS_H_ 2 | #define _CALLBACKS_H_ 3 | 4 | #include 5 | #include 6 | #include "Timestamp.h" 7 | 8 | typedef std::function TimerCallback; 9 | 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /v3/src/base/Timer.cpp: -------------------------------------------------------------------------------- 1 | #include "Timer.h" 2 | 3 | int Timer::s_numCreated_; 4 | 5 | void Timer::restart(Timestamp now){ 6 | if(repeat_){ 7 | expiration_ = addTime(now, interval_); 8 | } 9 | else{ 10 | expiration_ = Timestamp::invalid(); 11 | } 12 | } 13 | 14 | -------------------------------------------------------------------------------- /v1/muduo++/02/Timer.cpp: -------------------------------------------------------------------------------- 1 | #include "Timer.h" 2 | 3 | AtomicInt64 Timer::s_numCreated_; 4 | 5 | void Timer::restart(Timestamp now){ 6 | if(repeat_){ 7 | expiration_ = addTime(now, interval_); 8 | } 9 | else{ 10 | expiration_ = Timestamp::invalid(); 11 | } 12 | } 13 | 14 | -------------------------------------------------------------------------------- /v1/muduo++/03/Timer.cpp: -------------------------------------------------------------------------------- 1 | #include "Timer.h" 2 | 3 | AtomicInt64 Timer::s_numCreated_; 4 | 5 | void Timer::restart(Timestamp now){ 6 | if(repeat_){ 7 | expiration_ = addTime(now, interval_); 8 | } 9 | else{ 10 | expiration_ = Timestamp::invalid(); 11 | } 12 | } 13 | 14 | -------------------------------------------------------------------------------- /v1/muduo++/04/Timer.cpp: -------------------------------------------------------------------------------- 1 | #include "Timer.h" 2 | 3 | AtomicInt64 Timer::s_numCreated_; 4 | 5 | void Timer::restart(Timestamp now){ 6 | if(repeat_){ 7 | expiration_ = addTime(now, interval_); 8 | } 9 | else{ 10 | expiration_ = Timestamp::invalid(); 11 | } 12 | } 13 | 14 | -------------------------------------------------------------------------------- /v1/muduo++/05/Timer.cpp: -------------------------------------------------------------------------------- 1 | #include "Timer.h" 2 | 3 | AtomicInt64 Timer::s_numCreated_; 4 | 5 | void Timer::restart(Timestamp now){ 6 | if(repeat_){ 7 | expiration_ = addTime(now, interval_); 8 | } 9 | else{ 10 | expiration_ = Timestamp::invalid(); 11 | } 12 | } 13 | 14 | -------------------------------------------------------------------------------- /v1/muduo++/06/Timer.cpp: -------------------------------------------------------------------------------- 1 | #include "Timer.h" 2 | 3 | AtomicInt64 Timer::s_numCreated_; 4 | 5 | void Timer::restart(Timestamp now){ 6 | if(repeat_){ 7 | expiration_ = addTime(now, interval_); 8 | } 9 | else{ 10 | expiration_ = Timestamp::invalid(); 11 | } 12 | } 13 | 14 | -------------------------------------------------------------------------------- /v1/muduo++/07/Timer.cpp: -------------------------------------------------------------------------------- 1 | #include "Timer.h" 2 | 3 | AtomicInt64 Timer::s_numCreated_; 4 | 5 | void Timer::restart(Timestamp now){ 6 | if(repeat_){ 7 | expiration_ = addTime(now, interval_); 8 | } 9 | else{ 10 | expiration_ = Timestamp::invalid(); 11 | } 12 | } 13 | 14 | -------------------------------------------------------------------------------- /v1/muduo++/08/Timer.cpp: -------------------------------------------------------------------------------- 1 | #include "Timer.h" 2 | 3 | AtomicInt64 Timer::s_numCreated_; 4 | 5 | void Timer::restart(Timestamp now){ 6 | if(repeat_){ 7 | expiration_ = addTime(now, interval_); 8 | } 9 | else{ 10 | expiration_ = Timestamp::invalid(); 11 | } 12 | } 13 | 14 | -------------------------------------------------------------------------------- /v1/muduo++/09/Timer.cpp: -------------------------------------------------------------------------------- 1 | #include "Timer.h" 2 | 3 | AtomicInt64 Timer::s_numCreated_; 4 | 5 | void Timer::restart(Timestamp now){ 6 | if(repeat_){ 7 | expiration_ = addTime(now, interval_); 8 | } 9 | else{ 10 | expiration_ = Timestamp::invalid(); 11 | } 12 | } 13 | 14 | -------------------------------------------------------------------------------- /v1/muduo++/10/Timer.cpp: -------------------------------------------------------------------------------- 1 | #include "Timer.h" 2 | 3 | AtomicInt64 Timer::s_numCreated_; 4 | 5 | void Timer::restart(Timestamp now){ 6 | if(repeat_){ 7 | expiration_ = addTime(now, interval_); 8 | } 9 | else{ 10 | expiration_ = Timestamp::invalid(); 11 | } 12 | } 13 | 14 | -------------------------------------------------------------------------------- /v1/muduo++/11/Timer.cpp: -------------------------------------------------------------------------------- 1 | #include "Timer.h" 2 | 3 | AtomicInt64 Timer::s_numCreated_; 4 | 5 | void Timer::restart(Timestamp now){ 6 | if(repeat_){ 7 | expiration_ = addTime(now, interval_); 8 | } 9 | else{ 10 | expiration_ = Timestamp::invalid(); 11 | } 12 | } 13 | 14 | -------------------------------------------------------------------------------- /v2/src/net/Timer.cpp: -------------------------------------------------------------------------------- 1 | #include "Timer.h" 2 | 3 | AtomicInt64 Timer::s_numCreated_; 4 | 5 | void Timer::restart(Timestamp now){ 6 | if(repeat_){ 7 | expiration_ = addTime(now, interval_); 8 | } 9 | else{ 10 | expiration_ = Timestamp::invalid(); 11 | } 12 | } 13 | 14 | -------------------------------------------------------------------------------- /v3/src/base/notCopyable.h: -------------------------------------------------------------------------------- 1 | #ifndef _NOT_COPYABLE_H_ 2 | #define _NOT_COPYABLE_H_ 3 | 4 | class notCopyable{ 5 | protected: 6 | notCopyable(){}; 7 | ~notCopyable(){}; 8 | 9 | private: 10 | notCopyable(const notCopyable&); 11 | notCopyable& operator=(const notCopyable&); 12 | }; 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /v1/muduo++/01/Timestamp.cpp: -------------------------------------------------------------------------------- 1 | #include "Timestamp.h" 2 | 3 | #include 4 | #include 5 | 6 | Timestamp Timestamp::now(){ 7 | struct timeval tv; 8 | gettimeofday(&tv, NULL); 9 | int64_t seconds = tv.tv_sec; 10 | return Timestamp(seconds * kMicroSecondsPerSecond + tv.tv_usec); 11 | } 12 | -------------------------------------------------------------------------------- /v2/src/net/notCopyable.h: -------------------------------------------------------------------------------- 1 | #include"config.h" 2 | 3 | #ifndef _NOT_COPYABLE_H 4 | #define _NOT_COPYABLE_H 5 | class notCopyable{ 6 | protected: 7 | notCopyable(){}; 8 | ~notCopyable(){}; 9 | 10 | private: 11 | notCopyable(const notCopyable&); 12 | notCopyable& operator=(const notCopyable&); 13 | 14 | }; 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /v1/muduo++/00/notCopyable.h: -------------------------------------------------------------------------------- 1 | #include"config.h" 2 | 3 | #ifndef _NOT_COPYABLE_H 4 | #define _NOT_COPYABLE_H 5 | class notCopyable{ 6 | protected: 7 | notCopyable(){}; 8 | ~notCopyable(){}; 9 | 10 | private: 11 | notCopyable(const notCopyable&); 12 | notCopyable operator=(const notCopyable&); 13 | 14 | }; 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /v1/muduo++/01/notCopyable.h: -------------------------------------------------------------------------------- 1 | #include"config.h" 2 | 3 | #ifndef _NOT_COPYABLE_H 4 | #define _NOT_COPYABLE_H 5 | class notCopyable{ 6 | protected: 7 | notCopyable(){}; 8 | ~notCopyable(){}; 9 | 10 | private: 11 | notCopyable(const notCopyable&); 12 | notCopyable operator=(const notCopyable&); 13 | 14 | }; 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /v1/muduo++/02/notCopyable.h: -------------------------------------------------------------------------------- 1 | #include"config.h" 2 | 3 | #ifndef _NOT_COPYABLE_H 4 | #define _NOT_COPYABLE_H 5 | class notCopyable{ 6 | protected: 7 | notCopyable(){}; 8 | ~notCopyable(){}; 9 | 10 | private: 11 | notCopyable(const notCopyable&); 12 | notCopyable operator=(const notCopyable&); 13 | 14 | }; 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /v1/muduo++/03/notCopyable.h: -------------------------------------------------------------------------------- 1 | #include"config.h" 2 | 3 | #ifndef _NOT_COPYABLE_H 4 | #define _NOT_COPYABLE_H 5 | class notCopyable{ 6 | protected: 7 | notCopyable(){}; 8 | ~notCopyable(){}; 9 | 10 | private: 11 | notCopyable(const notCopyable&); 12 | notCopyable operator=(const notCopyable&); 13 | 14 | }; 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /v1/muduo++/04/notCopyable.h: -------------------------------------------------------------------------------- 1 | #include"config.h" 2 | 3 | #ifndef _NOT_COPYABLE_H 4 | #define _NOT_COPYABLE_H 5 | class notCopyable{ 6 | protected: 7 | notCopyable(){}; 8 | ~notCopyable(){}; 9 | 10 | private: 11 | notCopyable(const notCopyable&); 12 | notCopyable operator=(const notCopyable&); 13 | 14 | }; 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /v1/muduo++/05/notCopyable.h: -------------------------------------------------------------------------------- 1 | #include"config.h" 2 | 3 | #ifndef _NOT_COPYABLE_H 4 | #define _NOT_COPYABLE_H 5 | class notCopyable{ 6 | protected: 7 | notCopyable(){}; 8 | ~notCopyable(){}; 9 | 10 | private: 11 | notCopyable(const notCopyable&); 12 | notCopyable operator=(const notCopyable&); 13 | 14 | }; 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /v1/muduo++/06/notCopyable.h: -------------------------------------------------------------------------------- 1 | #include"config.h" 2 | 3 | #ifndef _NOT_COPYABLE_H 4 | #define _NOT_COPYABLE_H 5 | class notCopyable{ 6 | protected: 7 | notCopyable(){}; 8 | ~notCopyable(){}; 9 | 10 | private: 11 | notCopyable(const notCopyable&); 12 | notCopyable operator=(const notCopyable&); 13 | 14 | }; 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /v1/muduo++/07/notCopyable.h: -------------------------------------------------------------------------------- 1 | #include"config.h" 2 | 3 | #ifndef _NOT_COPYABLE_H 4 | #define _NOT_COPYABLE_H 5 | class notCopyable{ 6 | protected: 7 | notCopyable(){}; 8 | ~notCopyable(){}; 9 | 10 | private: 11 | notCopyable(const notCopyable&); 12 | notCopyable operator=(const notCopyable&); 13 | 14 | }; 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /v1/muduo++/08/notCopyable.h: -------------------------------------------------------------------------------- 1 | #include"config.h" 2 | 3 | #ifndef _NOT_COPYABLE_H 4 | #define _NOT_COPYABLE_H 5 | class notCopyable{ 6 | protected: 7 | notCopyable(){}; 8 | ~notCopyable(){}; 9 | 10 | private: 11 | notCopyable(const notCopyable&); 12 | notCopyable operator=(const notCopyable&); 13 | 14 | }; 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /v1/muduo++/09/notCopyable.h: -------------------------------------------------------------------------------- 1 | #include"config.h" 2 | 3 | #ifndef _NOT_COPYABLE_H 4 | #define _NOT_COPYABLE_H 5 | class notCopyable{ 6 | protected: 7 | notCopyable(){}; 8 | ~notCopyable(){}; 9 | 10 | private: 11 | notCopyable(const notCopyable&); 12 | notCopyable operator=(const notCopyable&); 13 | 14 | }; 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /v1/muduo++/10/notCopyable.h: -------------------------------------------------------------------------------- 1 | #include"config.h" 2 | 3 | #ifndef _NOT_COPYABLE_H 4 | #define _NOT_COPYABLE_H 5 | class notCopyable{ 6 | protected: 7 | notCopyable(){}; 8 | ~notCopyable(){}; 9 | 10 | private: 11 | notCopyable(const notCopyable&); 12 | notCopyable operator=(const notCopyable&); 13 | 14 | }; 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /v1/muduo++/11/notCopyable.h: -------------------------------------------------------------------------------- 1 | #include"config.h" 2 | 3 | #ifndef _NOT_COPYABLE_H 4 | #define _NOT_COPYABLE_H 5 | class notCopyable{ 6 | protected: 7 | notCopyable(){}; 8 | ~notCopyable(){}; 9 | 10 | private: 11 | notCopyable(const notCopyable&); 12 | notCopyable operator=(const notCopyable&); 13 | 14 | }; 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /v1/muduo++/00/CurrentThread.h: -------------------------------------------------------------------------------- 1 | #ifndef _CURRENT_THREAD_H 2 | #define _CURRENT_THREAD_H 3 | 4 | #include 5 | 6 | namespace CurrentThread{ 7 | 8 | extern __thread int t_cachedTid; 9 | 10 | pid_t gettid(); 11 | void cachedTid(); 12 | 13 | inline int tid(){ 14 | if(__builtin_expect(t_cachedTid == 0, 0)){ 15 | cachedTid(); 16 | } 17 | return t_cachedTid; 18 | } 19 | 20 | } 21 | 22 | #endif 23 | 24 | -------------------------------------------------------------------------------- /v1/muduo++/01/CurrentThread.h: -------------------------------------------------------------------------------- 1 | #ifndef _CURRENT_THREAD_H 2 | #define _CURRENT_THREAD_H 3 | 4 | #include 5 | 6 | namespace CurrentThread{ 7 | 8 | extern __thread int t_cachedTid; 9 | 10 | pid_t gettid(); 11 | void cachedTid(); 12 | 13 | inline int tid(){ 14 | if(__builtin_expect(t_cachedTid == 0, 0)){ 15 | cachedTid(); 16 | } 17 | return t_cachedTid; 18 | } 19 | 20 | } 21 | 22 | #endif 23 | 24 | -------------------------------------------------------------------------------- /v1/muduo++/02/CurrentThread.h: -------------------------------------------------------------------------------- 1 | #ifndef _CURRENT_THREAD_H 2 | #define _CURRENT_THREAD_H 3 | 4 | #include 5 | 6 | namespace CurrentThread{ 7 | 8 | extern __thread int t_cachedTid; 9 | 10 | pid_t gettid(); 11 | void cachedTid(); 12 | 13 | inline int tid(){ 14 | if(__builtin_expect(t_cachedTid == 0, 0)){ 15 | cachedTid(); 16 | } 17 | return t_cachedTid; 18 | } 19 | 20 | } 21 | 22 | #endif 23 | 24 | -------------------------------------------------------------------------------- /v1/muduo++/03/CurrentThread.h: -------------------------------------------------------------------------------- 1 | #ifndef _CURRENT_THREAD_H 2 | #define _CURRENT_THREAD_H 3 | 4 | #include 5 | 6 | namespace CurrentThread{ 7 | 8 | extern __thread int t_cachedTid; 9 | 10 | pid_t gettid(); 11 | void cachedTid(); 12 | 13 | inline int tid(){ 14 | if(__builtin_expect(t_cachedTid == 0, 0)){ 15 | cachedTid(); 16 | } 17 | return t_cachedTid; 18 | } 19 | 20 | } 21 | 22 | #endif 23 | 24 | -------------------------------------------------------------------------------- /v1/muduo++/04/CurrentThread.h: -------------------------------------------------------------------------------- 1 | #ifndef _CURRENT_THREAD_H 2 | #define _CURRENT_THREAD_H 3 | 4 | #include 5 | 6 | namespace CurrentThread{ 7 | 8 | extern __thread int t_cachedTid; 9 | 10 | pid_t gettid(); 11 | void cachedTid(); 12 | 13 | inline int tid(){ 14 | if(__builtin_expect(t_cachedTid == 0, 0)){ 15 | cachedTid(); 16 | } 17 | return t_cachedTid; 18 | } 19 | 20 | } 21 | 22 | #endif 23 | 24 | -------------------------------------------------------------------------------- /v1/muduo++/05/CurrentThread.h: -------------------------------------------------------------------------------- 1 | #ifndef _CURRENT_THREAD_H 2 | #define _CURRENT_THREAD_H 3 | 4 | #include 5 | 6 | namespace CurrentThread{ 7 | 8 | extern __thread int t_cachedTid; 9 | 10 | pid_t gettid(); 11 | void cachedTid(); 12 | 13 | inline int tid(){ 14 | if(__builtin_expect(t_cachedTid == 0, 0)){ 15 | cachedTid(); 16 | } 17 | return t_cachedTid; 18 | } 19 | 20 | } 21 | 22 | #endif 23 | 24 | -------------------------------------------------------------------------------- /v1/muduo++/06/CurrentThread.h: -------------------------------------------------------------------------------- 1 | #ifndef _CURRENT_THREAD_H 2 | #define _CURRENT_THREAD_H 3 | 4 | #include 5 | 6 | namespace CurrentThread{ 7 | 8 | extern __thread int t_cachedTid; 9 | 10 | pid_t gettid(); 11 | void cachedTid(); 12 | 13 | inline int tid(){ 14 | if(__builtin_expect(t_cachedTid == 0, 0)){ 15 | cachedTid(); 16 | } 17 | return t_cachedTid; 18 | } 19 | 20 | } 21 | 22 | #endif 23 | 24 | -------------------------------------------------------------------------------- /v1/muduo++/07/CurrentThread.h: -------------------------------------------------------------------------------- 1 | #ifndef _CURRENT_THREAD_H 2 | #define _CURRENT_THREAD_H 3 | 4 | #include 5 | 6 | namespace CurrentThread{ 7 | 8 | extern __thread int t_cachedTid; 9 | 10 | pid_t gettid(); 11 | void cachedTid(); 12 | 13 | inline int tid(){ 14 | if(__builtin_expect(t_cachedTid == 0, 0)){ 15 | cachedTid(); 16 | } 17 | return t_cachedTid; 18 | } 19 | 20 | } 21 | 22 | #endif 23 | 24 | -------------------------------------------------------------------------------- /v1/muduo++/08/CurrentThread.h: -------------------------------------------------------------------------------- 1 | #ifndef _CURRENT_THREAD_H 2 | #define _CURRENT_THREAD_H 3 | 4 | #include 5 | 6 | namespace CurrentThread{ 7 | 8 | extern __thread int t_cachedTid; 9 | 10 | pid_t gettid(); 11 | void cachedTid(); 12 | 13 | inline int tid(){ 14 | if(__builtin_expect(t_cachedTid == 0, 0)){ 15 | cachedTid(); 16 | } 17 | return t_cachedTid; 18 | } 19 | 20 | } 21 | 22 | #endif 23 | 24 | -------------------------------------------------------------------------------- /v1/muduo++/09/CurrentThread.h: -------------------------------------------------------------------------------- 1 | #ifndef _CURRENT_THREAD_H 2 | #define _CURRENT_THREAD_H 3 | 4 | #include 5 | 6 | namespace CurrentThread{ 7 | 8 | extern __thread int t_cachedTid; 9 | 10 | pid_t gettid(); 11 | void cachedTid(); 12 | 13 | inline int tid(){ 14 | if(__builtin_expect(t_cachedTid == 0, 0)){ 15 | cachedTid(); 16 | } 17 | return t_cachedTid; 18 | } 19 | 20 | } 21 | 22 | #endif 23 | 24 | -------------------------------------------------------------------------------- /v1/muduo++/00/CurrentThread.cpp: -------------------------------------------------------------------------------- 1 | #include"CurrentThread.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | namespace CurrentThread{ 9 | __thread int t_cachedTid = 0; 10 | 11 | pid_t gettid(){ 12 | return static_cast(syscall(SYS_gettid)); 13 | } 14 | 15 | void cachedTid(){ 16 | if(t_cachedTid == 0){ 17 | t_cachedTid = gettid(); 18 | } 19 | } 20 | 21 | 22 | } 23 | -------------------------------------------------------------------------------- /v1/muduo++/01/CurrentThread.cpp: -------------------------------------------------------------------------------- 1 | #include"CurrentThread.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | namespace CurrentThread{ 9 | __thread int t_cachedTid = 0; 10 | 11 | pid_t gettid(){ 12 | return static_cast(syscall(SYS_gettid)); 13 | } 14 | 15 | void cachedTid(){ 16 | if(t_cachedTid == 0){ 17 | t_cachedTid = gettid(); 18 | } 19 | } 20 | 21 | 22 | } 23 | -------------------------------------------------------------------------------- /v1/muduo++/02/CurrentThread.cpp: -------------------------------------------------------------------------------- 1 | #include"CurrentThread.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | namespace CurrentThread{ 9 | __thread int t_cachedTid = 0; 10 | 11 | pid_t gettid(){ 12 | return static_cast(syscall(SYS_gettid)); 13 | } 14 | 15 | void cachedTid(){ 16 | if(t_cachedTid == 0){ 17 | t_cachedTid = gettid(); 18 | } 19 | } 20 | 21 | 22 | } 23 | -------------------------------------------------------------------------------- /v1/muduo++/03/CurrentThread.cpp: -------------------------------------------------------------------------------- 1 | #include"CurrentThread.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | namespace CurrentThread{ 9 | __thread int t_cachedTid = 0; 10 | 11 | pid_t gettid(){ 12 | return static_cast(syscall(SYS_gettid)); 13 | } 14 | 15 | void cachedTid(){ 16 | if(t_cachedTid == 0){ 17 | t_cachedTid = gettid(); 18 | } 19 | } 20 | 21 | 22 | } 23 | -------------------------------------------------------------------------------- /v1/muduo++/04/CurrentThread.cpp: -------------------------------------------------------------------------------- 1 | #include"CurrentThread.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | namespace CurrentThread{ 9 | __thread int t_cachedTid = 0; 10 | 11 | pid_t gettid(){ 12 | return static_cast(syscall(SYS_gettid)); 13 | } 14 | 15 | void cachedTid(){ 16 | if(t_cachedTid == 0){ 17 | t_cachedTid = gettid(); 18 | } 19 | } 20 | 21 | 22 | } 23 | -------------------------------------------------------------------------------- /v1/muduo++/05/CurrentThread.cpp: -------------------------------------------------------------------------------- 1 | #include"CurrentThread.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | namespace CurrentThread{ 9 | __thread int t_cachedTid = 0; 10 | 11 | pid_t gettid(){ 12 | return static_cast(syscall(SYS_gettid)); 13 | } 14 | 15 | void cachedTid(){ 16 | if(t_cachedTid == 0){ 17 | t_cachedTid = gettid(); 18 | } 19 | } 20 | 21 | 22 | } 23 | -------------------------------------------------------------------------------- /v1/muduo++/06/CurrentThread.cpp: -------------------------------------------------------------------------------- 1 | #include"CurrentThread.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | namespace CurrentThread{ 9 | __thread int t_cachedTid = 0; 10 | 11 | pid_t gettid(){ 12 | return static_cast(syscall(SYS_gettid)); 13 | } 14 | 15 | void cachedTid(){ 16 | if(t_cachedTid == 0){ 17 | t_cachedTid = gettid(); 18 | } 19 | } 20 | 21 | 22 | } 23 | -------------------------------------------------------------------------------- /v1/muduo++/07/CurrentThread.cpp: -------------------------------------------------------------------------------- 1 | #include"CurrentThread.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | namespace CurrentThread{ 9 | __thread int t_cachedTid = 0; 10 | 11 | pid_t gettid(){ 12 | return static_cast(syscall(SYS_gettid)); 13 | } 14 | 15 | void cachedTid(){ 16 | if(t_cachedTid == 0){ 17 | t_cachedTid = gettid(); 18 | } 19 | } 20 | 21 | 22 | } 23 | -------------------------------------------------------------------------------- /v1/muduo++/08/CurrentThread.cpp: -------------------------------------------------------------------------------- 1 | #include"CurrentThread.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | namespace CurrentThread{ 9 | __thread int t_cachedTid = 0; 10 | 11 | pid_t gettid(){ 12 | return static_cast(syscall(SYS_gettid)); 13 | } 14 | 15 | void cachedTid(){ 16 | if(t_cachedTid == 0){ 17 | t_cachedTid = gettid(); 18 | } 19 | } 20 | 21 | 22 | } 23 | -------------------------------------------------------------------------------- /v1/muduo++/09/CurrentThread.cpp: -------------------------------------------------------------------------------- 1 | #include"CurrentThread.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | namespace CurrentThread{ 9 | __thread int t_cachedTid = 0; 10 | 11 | pid_t gettid(){ 12 | return static_cast(syscall(SYS_gettid)); 13 | } 14 | 15 | void cachedTid(){ 16 | if(t_cachedTid == 0){ 17 | t_cachedTid = gettid(); 18 | } 19 | } 20 | 21 | 22 | } 23 | -------------------------------------------------------------------------------- /v3/src/base/CountDownLatch.h: -------------------------------------------------------------------------------- 1 | #ifndef _COUNTDOWNLATCH_H_ 2 | #define _COUNTDOWNLATCH_H_ 3 | 4 | #include "Condition.h" 5 | #include "Mutex.h" 6 | #include "notCopyable.h" 7 | 8 | class CountDownLatch: public notCopyable{ 9 | public: 10 | explicit CountDownLatch(int count); 11 | 12 | void wait(); 13 | 14 | void countDown(); 15 | 16 | int getCount()const; 17 | 18 | private: 19 | int count_; 20 | mutable MutexLock mutex_; 21 | Condition condition_; 22 | }; 23 | #endif 24 | -------------------------------------------------------------------------------- /v2/src/net/CountDownLatch.h: -------------------------------------------------------------------------------- 1 | #ifndef _COUNTDOWNLATCH_H_ 2 | #define _COUNTDOWNLATCH_H_ 3 | 4 | #include "Condition.h" 5 | #include "Mutex.h" 6 | #include "notCopyable.h" 7 | 8 | class CountDownLatch: public notCopyable{ 9 | public: 10 | explicit CountDownLatch(int count); 11 | 12 | void wait(); 13 | 14 | void countDown(); 15 | 16 | int getCount()const; 17 | 18 | private: 19 | mutable MutexLock mutex_; 20 | Condition condition_; 21 | int count_; 22 | }; 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /v1/muduo++/03/CountDownLatch.h: -------------------------------------------------------------------------------- 1 | #ifndef _COUNTDOWNLATCH_H_ 2 | #define _COUNTDOWNLATCH_H_ 3 | 4 | #include "Condition.h" 5 | #include "Mutex.h" 6 | #include "notCopyable.h" 7 | 8 | class CountDownLatch: public notCopyable{ 9 | public: 10 | explicit CountDownLatch(int count); 11 | 12 | void wait(); 13 | 14 | void countDown(); 15 | 16 | int getCount()const; 17 | 18 | private: 19 | mutable MutexLock mutex_; 20 | Condition condition_; 21 | int count_; 22 | }; 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /v1/muduo++/04/CountDownLatch.h: -------------------------------------------------------------------------------- 1 | #ifndef _COUNTDOWNLATCH_H_ 2 | #define _COUNTDOWNLATCH_H_ 3 | 4 | #include "Condition.h" 5 | #include "Mutex.h" 6 | #include "notCopyable.h" 7 | 8 | class CountDownLatch: public notCopyable{ 9 | public: 10 | explicit CountDownLatch(int count); 11 | 12 | void wait(); 13 | 14 | void countDown(); 15 | 16 | int getCount()const; 17 | 18 | private: 19 | mutable MutexLock mutex_; 20 | Condition condition_; 21 | int count_; 22 | }; 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /v1/muduo++/05/CountDownLatch.h: -------------------------------------------------------------------------------- 1 | #ifndef _COUNTDOWNLATCH_H_ 2 | #define _COUNTDOWNLATCH_H_ 3 | 4 | #include "Condition.h" 5 | #include "Mutex.h" 6 | #include "notCopyable.h" 7 | 8 | class CountDownLatch: public notCopyable{ 9 | public: 10 | explicit CountDownLatch(int count); 11 | 12 | void wait(); 13 | 14 | void countDown(); 15 | 16 | int getCount()const; 17 | 18 | private: 19 | mutable MutexLock mutex_; 20 | Condition condition_; 21 | int count_; 22 | }; 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /v1/muduo++/06/CountDownLatch.h: -------------------------------------------------------------------------------- 1 | #ifndef _COUNTDOWNLATCH_H_ 2 | #define _COUNTDOWNLATCH_H_ 3 | 4 | #include "Condition.h" 5 | #include "Mutex.h" 6 | #include "notCopyable.h" 7 | 8 | class CountDownLatch: public notCopyable{ 9 | public: 10 | explicit CountDownLatch(int count); 11 | 12 | void wait(); 13 | 14 | void countDown(); 15 | 16 | int getCount()const; 17 | 18 | private: 19 | mutable MutexLock mutex_; 20 | Condition condition_; 21 | int count_; 22 | }; 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /v1/muduo++/07/CountDownLatch.h: -------------------------------------------------------------------------------- 1 | #ifndef _COUNTDOWNLATCH_H_ 2 | #define _COUNTDOWNLATCH_H_ 3 | 4 | #include "Condition.h" 5 | #include "Mutex.h" 6 | #include "notCopyable.h" 7 | 8 | class CountDownLatch: public notCopyable{ 9 | public: 10 | explicit CountDownLatch(int count); 11 | 12 | void wait(); 13 | 14 | void countDown(); 15 | 16 | int getCount()const; 17 | 18 | private: 19 | mutable MutexLock mutex_; 20 | Condition condition_; 21 | int count_; 22 | }; 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /v1/muduo++/08/CountDownLatch.h: -------------------------------------------------------------------------------- 1 | #ifndef _COUNTDOWNLATCH_H_ 2 | #define _COUNTDOWNLATCH_H_ 3 | 4 | #include "Condition.h" 5 | #include "Mutex.h" 6 | #include "notCopyable.h" 7 | 8 | class CountDownLatch: public notCopyable{ 9 | public: 10 | explicit CountDownLatch(int count); 11 | 12 | void wait(); 13 | 14 | void countDown(); 15 | 16 | int getCount()const; 17 | 18 | private: 19 | mutable MutexLock mutex_; 20 | Condition condition_; 21 | int count_; 22 | }; 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /v1/muduo++/09/CountDownLatch.h: -------------------------------------------------------------------------------- 1 | #ifndef _COUNTDOWNLATCH_H_ 2 | #define _COUNTDOWNLATCH_H_ 3 | 4 | #include "Condition.h" 5 | #include "Mutex.h" 6 | #include "notCopyable.h" 7 | 8 | class CountDownLatch: public notCopyable{ 9 | public: 10 | explicit CountDownLatch(int count); 11 | 12 | void wait(); 13 | 14 | void countDown(); 15 | 16 | int getCount()const; 17 | 18 | private: 19 | mutable MutexLock mutex_; 20 | Condition condition_; 21 | int count_; 22 | }; 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /v1/muduo++/10/CountDownLatch.h: -------------------------------------------------------------------------------- 1 | #ifndef _COUNTDOWNLATCH_H_ 2 | #define _COUNTDOWNLATCH_H_ 3 | 4 | #include "Condition.h" 5 | #include "Mutex.h" 6 | #include "notCopyable.h" 7 | 8 | class CountDownLatch: public notCopyable{ 9 | public: 10 | explicit CountDownLatch(int count); 11 | 12 | void wait(); 13 | 14 | void countDown(); 15 | 16 | int getCount()const; 17 | 18 | private: 19 | mutable MutexLock mutex_; 20 | Condition condition_; 21 | int count_; 22 | }; 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /v1/muduo++/11/CountDownLatch.h: -------------------------------------------------------------------------------- 1 | #ifndef _COUNTDOWNLATCH_H_ 2 | #define _COUNTDOWNLATCH_H_ 3 | 4 | #include "Condition.h" 5 | #include "Mutex.h" 6 | #include "notCopyable.h" 7 | 8 | class CountDownLatch: public notCopyable{ 9 | public: 10 | explicit CountDownLatch(int count); 11 | 12 | void wait(); 13 | 14 | void countDown(); 15 | 16 | int getCount()const; 17 | 18 | private: 19 | mutable MutexLock mutex_; 20 | Condition condition_; 21 | int count_; 22 | }; 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /v2/src/net/TimerId.h: -------------------------------------------------------------------------------- 1 | #ifndef _TIMERID_H_ 2 | #define _TIMERID_H_ 3 | 4 | #include 5 | 6 | class Timer; 7 | 8 | //user interface 9 | class TimerId{ 10 | public: 11 | TimerId() 12 | :timer_(NULL), 13 | sequence_(0){ 14 | 15 | } 16 | 17 | TimerId(Timer* timer, int64_t seq) 18 | :timer_(timer), 19 | sequence_(seq){ 20 | 21 | } 22 | 23 | friend class TimerQueue; 24 | 25 | private: 26 | Timer* timer_; 27 | int64_t sequence_; 28 | }; 29 | 30 | #endif 31 | 32 | -------------------------------------------------------------------------------- /v1/muduo++/02/TimerId.h: -------------------------------------------------------------------------------- 1 | #ifndef _TIMERID_H_ 2 | #define _TIMERID_H_ 3 | 4 | #include 5 | 6 | class Timer; 7 | 8 | //user interface 9 | class TimerId{ 10 | public: 11 | TimerId() 12 | :timer_(NULL), 13 | sequence_(0){ 14 | 15 | } 16 | 17 | TimerId(Timer* timer, int64_t seq) 18 | :timer_(timer), 19 | sequence_(seq){ 20 | 21 | } 22 | 23 | friend class TimerQueue; 24 | 25 | private: 26 | Timer* timer_; 27 | int64_t sequence_; 28 | }; 29 | 30 | #endif 31 | 32 | -------------------------------------------------------------------------------- /v1/muduo++/03/TimerId.h: -------------------------------------------------------------------------------- 1 | #ifndef _TIMERID_H_ 2 | #define _TIMERID_H_ 3 | 4 | #include 5 | 6 | class Timer; 7 | 8 | //user interface 9 | class TimerId{ 10 | public: 11 | TimerId() 12 | :timer_(NULL), 13 | sequence_(0){ 14 | 15 | } 16 | 17 | TimerId(Timer* timer, int64_t seq) 18 | :timer_(timer), 19 | sequence_(seq){ 20 | 21 | } 22 | 23 | friend class TimerQueue; 24 | 25 | private: 26 | Timer* timer_; 27 | int64_t sequence_; 28 | }; 29 | 30 | #endif 31 | 32 | -------------------------------------------------------------------------------- /v1/muduo++/04/TimerId.h: -------------------------------------------------------------------------------- 1 | #ifndef _TIMERID_H_ 2 | #define _TIMERID_H_ 3 | 4 | #include 5 | 6 | class Timer; 7 | 8 | //user interface 9 | class TimerId{ 10 | public: 11 | TimerId() 12 | :timer_(NULL), 13 | sequence_(0){ 14 | 15 | } 16 | 17 | TimerId(Timer* timer, int64_t seq) 18 | :timer_(timer), 19 | sequence_(seq){ 20 | 21 | } 22 | 23 | friend class TimerQueue; 24 | 25 | private: 26 | Timer* timer_; 27 | int64_t sequence_; 28 | }; 29 | 30 | #endif 31 | 32 | -------------------------------------------------------------------------------- /v1/muduo++/05/TimerId.h: -------------------------------------------------------------------------------- 1 | #ifndef _TIMERID_H_ 2 | #define _TIMERID_H_ 3 | 4 | #include 5 | 6 | class Timer; 7 | 8 | //user interface 9 | class TimerId{ 10 | public: 11 | TimerId() 12 | :timer_(NULL), 13 | sequence_(0){ 14 | 15 | } 16 | 17 | TimerId(Timer* timer, int64_t seq) 18 | :timer_(timer), 19 | sequence_(seq){ 20 | 21 | } 22 | 23 | friend class TimerQueue; 24 | 25 | private: 26 | Timer* timer_; 27 | int64_t sequence_; 28 | }; 29 | 30 | #endif 31 | 32 | -------------------------------------------------------------------------------- /v1/muduo++/06/TimerId.h: -------------------------------------------------------------------------------- 1 | #ifndef _TIMERID_H_ 2 | #define _TIMERID_H_ 3 | 4 | #include 5 | 6 | class Timer; 7 | 8 | //user interface 9 | class TimerId{ 10 | public: 11 | TimerId() 12 | :timer_(NULL), 13 | sequence_(0){ 14 | 15 | } 16 | 17 | TimerId(Timer* timer, int64_t seq) 18 | :timer_(timer), 19 | sequence_(seq){ 20 | 21 | } 22 | 23 | friend class TimerQueue; 24 | 25 | private: 26 | Timer* timer_; 27 | int64_t sequence_; 28 | }; 29 | 30 | #endif 31 | 32 | -------------------------------------------------------------------------------- /v1/muduo++/07/TimerId.h: -------------------------------------------------------------------------------- 1 | #ifndef _TIMERID_H_ 2 | #define _TIMERID_H_ 3 | 4 | #include 5 | 6 | class Timer; 7 | 8 | //user interface 9 | class TimerId{ 10 | public: 11 | TimerId() 12 | :timer_(NULL), 13 | sequence_(0){ 14 | 15 | } 16 | 17 | TimerId(Timer* timer, int64_t seq) 18 | :timer_(timer), 19 | sequence_(seq){ 20 | 21 | } 22 | 23 | friend class TimerQueue; 24 | 25 | private: 26 | Timer* timer_; 27 | int64_t sequence_; 28 | }; 29 | 30 | #endif 31 | 32 | -------------------------------------------------------------------------------- /v1/muduo++/08/TimerId.h: -------------------------------------------------------------------------------- 1 | #ifndef _TIMERID_H_ 2 | #define _TIMERID_H_ 3 | 4 | #include 5 | 6 | class Timer; 7 | 8 | //user interface 9 | class TimerId{ 10 | public: 11 | TimerId() 12 | :timer_(NULL), 13 | sequence_(0){ 14 | 15 | } 16 | 17 | TimerId(Timer* timer, int64_t seq) 18 | :timer_(timer), 19 | sequence_(seq){ 20 | 21 | } 22 | 23 | friend class TimerQueue; 24 | 25 | private: 26 | Timer* timer_; 27 | int64_t sequence_; 28 | }; 29 | 30 | #endif 31 | 32 | -------------------------------------------------------------------------------- /v1/muduo++/09/TimerId.h: -------------------------------------------------------------------------------- 1 | #ifndef _TIMERID_H_ 2 | #define _TIMERID_H_ 3 | 4 | #include 5 | 6 | class Timer; 7 | 8 | //user interface 9 | class TimerId{ 10 | public: 11 | TimerId() 12 | :timer_(NULL), 13 | sequence_(0){ 14 | 15 | } 16 | 17 | TimerId(Timer* timer, int64_t seq) 18 | :timer_(timer), 19 | sequence_(seq){ 20 | 21 | } 22 | 23 | friend class TimerQueue; 24 | 25 | private: 26 | Timer* timer_; 27 | int64_t sequence_; 28 | }; 29 | 30 | #endif 31 | 32 | -------------------------------------------------------------------------------- /v1/muduo++/10/TimerId.h: -------------------------------------------------------------------------------- 1 | #ifndef _TIMERID_H_ 2 | #define _TIMERID_H_ 3 | 4 | #include 5 | 6 | class Timer; 7 | 8 | //user interface 9 | class TimerId{ 10 | public: 11 | TimerId() 12 | :timer_(NULL), 13 | sequence_(0){ 14 | 15 | } 16 | 17 | TimerId(Timer* timer, int64_t seq) 18 | :timer_(timer), 19 | sequence_(seq){ 20 | 21 | } 22 | 23 | friend class TimerQueue; 24 | 25 | private: 26 | Timer* timer_; 27 | int64_t sequence_; 28 | }; 29 | 30 | #endif 31 | 32 | -------------------------------------------------------------------------------- /v1/muduo++/11/TimerId.h: -------------------------------------------------------------------------------- 1 | #ifndef _TIMERID_H_ 2 | #define _TIMERID_H_ 3 | 4 | #include 5 | 6 | class Timer; 7 | 8 | //user interface 9 | class TimerId{ 10 | public: 11 | TimerId() 12 | :timer_(NULL), 13 | sequence_(0){ 14 | 15 | } 16 | 17 | TimerId(Timer* timer, int64_t seq) 18 | :timer_(timer), 19 | sequence_(seq){ 20 | 21 | } 22 | 23 | friend class TimerQueue; 24 | 25 | private: 26 | Timer* timer_; 27 | int64_t sequence_; 28 | }; 29 | 30 | #endif 31 | 32 | -------------------------------------------------------------------------------- /v3/src/base/Makefile: -------------------------------------------------------------------------------- 1 | TARGET := libbase.a 2 | 3 | SOURCE := $(wildcard *.c) $(wildcard *.cpp) 4 | OBJS := $(patsubst %.c, %.o, $(patsubst %.cpp, %.o, $(SOURCE))) 5 | 6 | CC := gcc 7 | CXX := g++ 8 | 9 | LIBS := 10 | LDFLAGS := 11 | 12 | DEFINES := 13 | INCLUDE := -I . 14 | 15 | CFLAGS := -g -o3 $(DEFINES) $(INCLUDE) 16 | CXXFLAGS := $(CFLAGS) -std=c++11 -pthread -DNDEBUG 17 | 18 | .PHONY : all clean rebuild 19 | 20 | all: $(TARGET) 21 | clean: 22 | rm -f *.o 23 | rm $(TARGET) 24 | rebuild: clean all 25 | 26 | $(TARGET) : $(OBJS) 27 | ar rcs $@ $^ 28 | -------------------------------------------------------------------------------- /v2/src/net/Makefile: -------------------------------------------------------------------------------- 1 | TARGET := libmyreactor.a 2 | 3 | SOURCE := $(wildcard *.c) $(wildcard *.cpp) 4 | OBJS := $(patsubst %.c, %.o, $(patsubst %.cpp, %.o, $(SOURCE))) 5 | 6 | CC := gcc 7 | CXX := g++ 8 | 9 | LIBS := 10 | LDFLAGS := 11 | 12 | DEFINES := 13 | INCLUDE := -I . 14 | 15 | CFLAGS := -g -o3 $(DEFINES) $(INCLUDE) 16 | CXXFLAGS := $(CFLAGS) -std=c++11 -pthread -DNDEBUG 17 | 18 | .PHONY : all clean rebuild 19 | 20 | all: $(TARGET) 21 | clean: 22 | rm -f *.o 23 | rm $(TARGET) 24 | rebuild: clean all 25 | 26 | $(TARGET) : $(OBJS) 27 | ar rcs $@ $^ 28 | -------------------------------------------------------------------------------- /v1/muduo++/11/Makefile: -------------------------------------------------------------------------------- 1 | TARGET := main 2 | 3 | SOURCE := $(wildcard *.c) $(wildcard *.cpp) 4 | OBJS := $(patsubst %.c, %.o, $(patsubst %.cpp, %.o, $(SOURCE))) 5 | 6 | CC := gcc 7 | CXX := g++ 8 | 9 | LIBS := 10 | LDFLAGS := 11 | 12 | DEFINES := 13 | INCLUDE := 14 | 15 | CFLAGS := -g -Wall -o3 $(DEFINES) $(INCLUDE) 16 | CXXFLAGS := $(CFLAGS) -std=c++11 -pthread 17 | 18 | .PHONY : all clean rebuild 19 | 20 | all: $(TARGET) 21 | clean: 22 | rm -f *.o 23 | rm $(TARGET) 24 | rebuild: clean all 25 | 26 | $(TARGET) : $(OBJS) 27 | $(CXX) $(CXXFLAGS) -o $@ $(OBJS) $(LDFLAGS) $(LIBS) 28 | -------------------------------------------------------------------------------- /v1/muduo++/00/Thread.h: -------------------------------------------------------------------------------- 1 | #ifndef _THREAD_H 2 | #define _THREAD_H 3 | 4 | #include 5 | #include 6 | #include"notCopyable.h" 7 | 8 | class Thread: public notCopyable{ 9 | public: 10 | typedef std::function ThreadFunc; 11 | 12 | Thread(const ThreadFunc&); 13 | ~Thread(); 14 | 15 | void start(); 16 | int join(); 17 | 18 | bool started(); 19 | pid_t tid(); 20 | 21 | private: 22 | bool started_; 23 | bool joined_; 24 | pid_t tid_; 25 | pthread_t pthreadId_; 26 | ThreadFunc func_; 27 | 28 | }; 29 | 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /v1/muduo++/01/Thread.h: -------------------------------------------------------------------------------- 1 | #ifndef _THREAD_H 2 | #define _THREAD_H 3 | 4 | #include 5 | #include 6 | #include"notCopyable.h" 7 | 8 | class Thread: public notCopyable{ 9 | public: 10 | typedef std::function ThreadFunc; 11 | 12 | Thread(const ThreadFunc&); 13 | ~Thread(); 14 | 15 | void start(); 16 | int join(); 17 | 18 | bool started(); 19 | pid_t tid(); 20 | 21 | private: 22 | bool started_; 23 | bool joined_; 24 | pid_t tid_; 25 | pthread_t pthreadId_; 26 | ThreadFunc func_; 27 | 28 | }; 29 | 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /v1/muduo++/02/Thread.h: -------------------------------------------------------------------------------- 1 | #ifndef _THREAD_H 2 | #define _THREAD_H 3 | 4 | #include 5 | #include 6 | #include"notCopyable.h" 7 | 8 | class Thread: public notCopyable{ 9 | public: 10 | typedef std::function ThreadFunc; 11 | 12 | Thread(const ThreadFunc&); 13 | ~Thread(); 14 | 15 | void start(); 16 | int join(); 17 | 18 | bool started(); 19 | pid_t tid(); 20 | 21 | private: 22 | bool started_; 23 | bool joined_; 24 | pid_t tid_; 25 | pthread_t pthreadId_; 26 | ThreadFunc func_; 27 | 28 | }; 29 | 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /v1/muduo++/00/Makefile: -------------------------------------------------------------------------------- 1 | objects = main.o EventLoop.o Thread.o CurrentThread.o 2 | 3 | main: $(objects) 4 | g++ -std=c++11 -pthread -o main $(objects) 5 | 6 | main.o: main.cpp EventLoop.h Thread.h 7 | g++ -std=c++11 -c main.cpp 8 | 9 | EventLoop.o: EventLoop.cpp EventLoop.h notCopyable.h CurrentThread.h 10 | g++ -std=c++11 -c EventLoop.cpp 11 | 12 | Thread.o: Thread.cpp Thread.h notCopyable.h CurrentThread.h 13 | g++ -std=c++11 -pthread -c Thread.cpp 14 | 15 | CurrentThread.o: CurrentThread.cpp CurrentThread.h 16 | g++ -std=c++11 -c CurrentThread.cpp 17 | 18 | clean: 19 | rm main $(objects) 20 | 21 | -------------------------------------------------------------------------------- /v3/src/Makefile: -------------------------------------------------------------------------------- 1 | TARGET := HttpServer 2 | 3 | SOURCE := $(wildcard *.c) $(wildcard *.cpp) 4 | OBJS := $(patsubst %.c, %.o, $(patsubst %.cpp, %.o, $(SOURCE))) 5 | 6 | CC := gcc 7 | CXX := g++ 8 | 9 | LIBS := base/libbase.a 10 | LDFLAGS := 11 | 12 | DEFINES := 13 | INCLUDE := -I base/ 14 | 15 | CFLAGS := -g -o3 $(DEFINES) $(INCLUDE) 16 | CXXFLAGS := $(CFLAGS) -std=c++11 -pthread -DNDEBUG 17 | 18 | .PHONY : all clean rebuild 19 | 20 | all: $(TARGET) 21 | clean: 22 | rm -f *.o 23 | rm $(TARGET) 24 | rebuild: clean all 25 | 26 | $(TARGET) : $(OBJS) 27 | $(CXX) $(CXXFLAGS) -o $@ $(OBJS) $(LDFLAGS) $(LIBS) 28 | -------------------------------------------------------------------------------- /v1/muduo++/01/Timestamp.h: -------------------------------------------------------------------------------- 1 | #ifndef _TIMRSTAMP_H_ 2 | #define _TIMESTAMP_H_ 3 | 4 | #include 5 | 6 | class Timestamp{ 7 | public: 8 | Timestamp(): 9 | microSecondsSinceEpoch_(0){ 10 | 11 | } 12 | 13 | explicit Timestamp(int64_t microSecondsSinceEpochArg) 14 | :microSecondsSinceEpoch_(microSecondsSinceEpochArg){ 15 | 16 | } 17 | 18 | ~Timestamp(){ 19 | 20 | } 21 | 22 | static Timestamp now(); 23 | static const int kMicroSecondsPerSecond = 1000 * 1000; 24 | 25 | private: 26 | int64_t microSecondsSinceEpoch_; 27 | 28 | }; 29 | #endif 30 | -------------------------------------------------------------------------------- /v2/src/net/FileUtil.h: -------------------------------------------------------------------------------- 1 | #ifndef _FILEUTIL_H_ 2 | #define _FILEUTIL_H_ 3 | 4 | #include "notCopyable.h" 5 | #include "StringPiece.h" 6 | 7 | class AppendFile: public notCopyable{ 8 | public: 9 | explicit AppendFile(StringArg filename); 10 | ~AppendFile(); 11 | 12 | void append(const char* logline, const size_t len); 13 | void flush(); 14 | int writtenBytes() const{ 15 | return writtenBytes_; 16 | } 17 | 18 | private: 19 | size_t write(const char* logline, size_t len); 20 | 21 | FILE* fp_; 22 | char buffer_[64 * 1024]; 23 | int writtenBytes_; 24 | }; 25 | 26 | #endif 27 | 28 | 29 | -------------------------------------------------------------------------------- /v1/muduo++/10/FileUtil.h: -------------------------------------------------------------------------------- 1 | #ifndef _FILEUTIL_H_ 2 | #define _FILEUTIL_H_ 3 | 4 | #include "notCopyable.h" 5 | #include "StringPiece.h" 6 | 7 | class AppendFile: public notCopyable{ 8 | public: 9 | explicit AppendFile(StringArg filename); 10 | ~AppendFile(); 11 | 12 | void append(const char* logline, const size_t len); 13 | void flush(); 14 | int writtenBytes() const{ 15 | return writtenBytes_; 16 | } 17 | 18 | private: 19 | size_t write(const char* logline, size_t len); 20 | 21 | FILE* fp_; 22 | char buffer_[64 * 1024]; 23 | int writtenBytes_; 24 | }; 25 | 26 | #endif 27 | 28 | 29 | -------------------------------------------------------------------------------- /v1/muduo++/11/FileUtil.h: -------------------------------------------------------------------------------- 1 | #ifndef _FILEUTIL_H_ 2 | #define _FILEUTIL_H_ 3 | 4 | #include "notCopyable.h" 5 | #include "StringPiece.h" 6 | 7 | class AppendFile: public notCopyable{ 8 | public: 9 | explicit AppendFile(StringArg filename); 10 | ~AppendFile(); 11 | 12 | void append(const char* logline, const size_t len); 13 | void flush(); 14 | int writtenBytes() const{ 15 | return writtenBytes_; 16 | } 17 | 18 | private: 19 | size_t write(const char* logline, size_t len); 20 | 21 | FILE* fp_; 22 | char buffer_[64 * 1024]; 23 | int writtenBytes_; 24 | }; 25 | 26 | #endif 27 | 28 | 29 | -------------------------------------------------------------------------------- /v1/muduo++/03/CountDownLatch.cpp: -------------------------------------------------------------------------------- 1 | #include "CountDownLatch.h" 2 | 3 | CountDownLatch::CountDownLatch(int count) 4 | :mutex_(), 5 | condition_(mutex_), 6 | count_(count){ 7 | 8 | } 9 | 10 | void CountDownLatch::wait(){ 11 | MutexLockGuard lock(mutex_); 12 | while(count_ > 0){ 13 | condition_.wait(); 14 | } 15 | } 16 | 17 | void CountDownLatch::countDown(){ 18 | MutexLockGuard lock(mutex_); 19 | count_--; 20 | if(0 == count_){ 21 | condition_.notifyAll(); 22 | } 23 | } 24 | 25 | int CountDownLatch::getCount()const{ 26 | MutexLockGuard lock(mutex_); 27 | return count_; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /v1/muduo++/04/CountDownLatch.cpp: -------------------------------------------------------------------------------- 1 | #include "CountDownLatch.h" 2 | 3 | CountDownLatch::CountDownLatch(int count) 4 | :mutex_(), 5 | condition_(mutex_), 6 | count_(count){ 7 | 8 | } 9 | 10 | void CountDownLatch::wait(){ 11 | MutexLockGuard lock(mutex_); 12 | while(count_ > 0){ 13 | condition_.wait(); 14 | } 15 | } 16 | 17 | void CountDownLatch::countDown(){ 18 | MutexLockGuard lock(mutex_); 19 | count_--; 20 | if(0 == count_){ 21 | condition_.notifyAll(); 22 | } 23 | } 24 | 25 | int CountDownLatch::getCount()const{ 26 | MutexLockGuard lock(mutex_); 27 | return count_; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /v1/muduo++/05/CountDownLatch.cpp: -------------------------------------------------------------------------------- 1 | #include "CountDownLatch.h" 2 | 3 | CountDownLatch::CountDownLatch(int count) 4 | :mutex_(), 5 | condition_(mutex_), 6 | count_(count){ 7 | 8 | } 9 | 10 | void CountDownLatch::wait(){ 11 | MutexLockGuard lock(mutex_); 12 | while(count_ > 0){ 13 | condition_.wait(); 14 | } 15 | } 16 | 17 | void CountDownLatch::countDown(){ 18 | MutexLockGuard lock(mutex_); 19 | count_--; 20 | if(0 == count_){ 21 | condition_.notifyAll(); 22 | } 23 | } 24 | 25 | int CountDownLatch::getCount()const{ 26 | MutexLockGuard lock(mutex_); 27 | return count_; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /v1/muduo++/06/CountDownLatch.cpp: -------------------------------------------------------------------------------- 1 | #include "CountDownLatch.h" 2 | 3 | CountDownLatch::CountDownLatch(int count) 4 | :mutex_(), 5 | condition_(mutex_), 6 | count_(count){ 7 | 8 | } 9 | 10 | void CountDownLatch::wait(){ 11 | MutexLockGuard lock(mutex_); 12 | while(count_ > 0){ 13 | condition_.wait(); 14 | } 15 | } 16 | 17 | void CountDownLatch::countDown(){ 18 | MutexLockGuard lock(mutex_); 19 | count_--; 20 | if(0 == count_){ 21 | condition_.notifyAll(); 22 | } 23 | } 24 | 25 | int CountDownLatch::getCount()const{ 26 | MutexLockGuard lock(mutex_); 27 | return count_; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /v1/muduo++/07/CountDownLatch.cpp: -------------------------------------------------------------------------------- 1 | #include "CountDownLatch.h" 2 | 3 | CountDownLatch::CountDownLatch(int count) 4 | :mutex_(), 5 | condition_(mutex_), 6 | count_(count){ 7 | 8 | } 9 | 10 | void CountDownLatch::wait(){ 11 | MutexLockGuard lock(mutex_); 12 | while(count_ > 0){ 13 | condition_.wait(); 14 | } 15 | } 16 | 17 | void CountDownLatch::countDown(){ 18 | MutexLockGuard lock(mutex_); 19 | count_--; 20 | if(0 == count_){ 21 | condition_.notifyAll(); 22 | } 23 | } 24 | 25 | int CountDownLatch::getCount()const{ 26 | MutexLockGuard lock(mutex_); 27 | return count_; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /v1/muduo++/08/CountDownLatch.cpp: -------------------------------------------------------------------------------- 1 | #include "CountDownLatch.h" 2 | 3 | CountDownLatch::CountDownLatch(int count) 4 | :mutex_(), 5 | condition_(mutex_), 6 | count_(count){ 7 | 8 | } 9 | 10 | void CountDownLatch::wait(){ 11 | MutexLockGuard lock(mutex_); 12 | while(count_ > 0){ 13 | condition_.wait(); 14 | } 15 | } 16 | 17 | void CountDownLatch::countDown(){ 18 | MutexLockGuard lock(mutex_); 19 | count_--; 20 | if(0 == count_){ 21 | condition_.notifyAll(); 22 | } 23 | } 24 | 25 | int CountDownLatch::getCount()const{ 26 | MutexLockGuard lock(mutex_); 27 | return count_; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /v3/src/base/log/FileUtil.h: -------------------------------------------------------------------------------- 1 | #ifndef _FILEUTIL_H_ 2 | #define _FILEUTIL_H_ 3 | 4 | #include "../notCopyable.h" 5 | #include "../StringPiece.h" 6 | 7 | class AppendFile: public notCopyable{ 8 | public: 9 | explicit AppendFile(StringArg filename); 10 | ~AppendFile(); 11 | 12 | void append(const char* logline, const size_t len); 13 | void flush(); 14 | int writtenBytes() const{ 15 | return writtenBytes_; 16 | } 17 | 18 | private: 19 | size_t write(const char* logline, size_t len); 20 | 21 | FILE* fp_; 22 | char buffer_[64 * 1024]; 23 | int writtenBytes_; 24 | }; 25 | 26 | #endif 27 | 28 | 29 | -------------------------------------------------------------------------------- /v1/muduo++/00/EventLoop.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include"CurrentThread.h" 3 | #include"notCopyable.h" 4 | 5 | class EventLoop : public notCopyable{ 6 | public: 7 | EventLoop(); 8 | ~EventLoop(); 9 | 10 | void loop(); 11 | 12 | void assertInLoopThread(){ 13 | if(!isInLoopThread()){ 14 | // do some logging things 15 | abortNotInLoopThread(); 16 | } 17 | } 18 | 19 | bool isInLoopThread() const{ 20 | return threadId_ == CurrentThread::tid(); 21 | } 22 | 23 | private: 24 | void abortNotInLoopThread(); 25 | 26 | bool looping_; 27 | const pid_t threadId_; 28 | }; 29 | -------------------------------------------------------------------------------- /v3/src/base/CountDownLatch.cpp: -------------------------------------------------------------------------------- 1 | #include "CountDownLatch.h" 2 | 3 | CountDownLatch::CountDownLatch(int count) 4 | :count_(count), 5 | mutex_(), 6 | condition_(mutex_){ 7 | 8 | } 9 | 10 | void CountDownLatch::wait(){ 11 | MutexLockGuard lock(mutex_); 12 | while(count_ > 0){ // loop condition 13 | condition_.wait(); 14 | } 15 | } 16 | 17 | void CountDownLatch::countDown(){ 18 | MutexLockGuard lock(mutex_); 19 | count_--; 20 | if(0 == count_){ 21 | condition_.notifyAll(); 22 | } 23 | } 24 | 25 | int CountDownLatch::getCount()const{ 26 | MutexLockGuard lock(mutex_); 27 | return count_; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /v2/src/net/CountDownLatch.cpp: -------------------------------------------------------------------------------- 1 | #include "CountDownLatch.h" 2 | 3 | CountDownLatch::CountDownLatch(int count) 4 | :mutex_(), 5 | condition_(mutex_), 6 | count_(count){ 7 | 8 | } 9 | 10 | void CountDownLatch::wait(){ 11 | MutexLockGuard lock(mutex_); 12 | // loop condition 13 | while(count_ > 0){ 14 | condition_.wait(); 15 | } 16 | } 17 | 18 | void CountDownLatch::countDown(){ 19 | MutexLockGuard lock(mutex_); 20 | count_--; 21 | if(0 == count_){ 22 | condition_.notifyAll(); 23 | } 24 | } 25 | 26 | int CountDownLatch::getCount()const{ 27 | MutexLockGuard lock(mutex_); 28 | return count_; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /v1/muduo++/09/CountDownLatch.cpp: -------------------------------------------------------------------------------- 1 | #include "CountDownLatch.h" 2 | 3 | CountDownLatch::CountDownLatch(int count) 4 | :mutex_(), 5 | condition_(mutex_), 6 | count_(count){ 7 | 8 | } 9 | 10 | void CountDownLatch::wait(){ 11 | MutexLockGuard lock(mutex_); 12 | // loop condition 13 | while(count_ > 0){ 14 | condition_.wait(); 15 | } 16 | } 17 | 18 | void CountDownLatch::countDown(){ 19 | MutexLockGuard lock(mutex_); 20 | count_--; 21 | if(0 == count_){ 22 | condition_.notifyAll(); 23 | } 24 | } 25 | 26 | int CountDownLatch::getCount()const{ 27 | MutexLockGuard lock(mutex_); 28 | return count_; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /v1/muduo++/10/CountDownLatch.cpp: -------------------------------------------------------------------------------- 1 | #include "CountDownLatch.h" 2 | 3 | CountDownLatch::CountDownLatch(int count) 4 | :mutex_(), 5 | condition_(mutex_), 6 | count_(count){ 7 | 8 | } 9 | 10 | void CountDownLatch::wait(){ 11 | MutexLockGuard lock(mutex_); 12 | // loop condition 13 | while(count_ > 0){ 14 | condition_.wait(); 15 | } 16 | } 17 | 18 | void CountDownLatch::countDown(){ 19 | MutexLockGuard lock(mutex_); 20 | count_--; 21 | if(0 == count_){ 22 | condition_.notifyAll(); 23 | } 24 | } 25 | 26 | int CountDownLatch::getCount()const{ 27 | MutexLockGuard lock(mutex_); 28 | return count_; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /v1/muduo++/11/CountDownLatch.cpp: -------------------------------------------------------------------------------- 1 | #include "CountDownLatch.h" 2 | 3 | CountDownLatch::CountDownLatch(int count) 4 | :mutex_(), 5 | condition_(mutex_), 6 | count_(count){ 7 | 8 | } 9 | 10 | void CountDownLatch::wait(){ 11 | MutexLockGuard lock(mutex_); 12 | // loop condition 13 | while(count_ > 0){ 14 | condition_.wait(); 15 | } 16 | } 17 | 18 | void CountDownLatch::countDown(){ 19 | MutexLockGuard lock(mutex_); 20 | count_--; 21 | if(0 == count_){ 22 | condition_.notifyAll(); 23 | } 24 | } 25 | 26 | int CountDownLatch::getCount()const{ 27 | MutexLockGuard lock(mutex_); 28 | return count_; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /v3/src/TcpServer.h: -------------------------------------------------------------------------------- 1 | #ifndef TCPSERVER_H 2 | #define TCPSERVER_H 3 | 4 | #include "base/notCopyable.h" 5 | 6 | #include 7 | 8 | class Channel; 9 | class EventLoop; 10 | class EventLoopThreadPool; 11 | 12 | class TcpServer:public notCopyable{ 13 | public: 14 | TcpServer(EventLoop* loop, int threadNum, int port); 15 | 16 | ~TcpServer(); 17 | 18 | void start(); 19 | 20 | void handleNewConn(); 21 | 22 | private: 23 | int threadNum_; 24 | int port_; 25 | int listenFd_; 26 | 27 | EventLoop* loop_; 28 | 29 | Channel* acceptChannel_; 30 | const std::unique_ptr threadPool_; 31 | }; 32 | 33 | #endif 34 | 35 | -------------------------------------------------------------------------------- /v1/muduo++/03/main.cpp: -------------------------------------------------------------------------------- 1 | #include "EventLoop.h" 2 | #include "EventLoopThread.h" 3 | #include "CurrentThread.h" 4 | #include 5 | #include 6 | 7 | void runInThread() 8 | { 9 | printf("runInThread(): pid = %d, tid = %d\n", 10 | getpid(), CurrentThread::tid()); 11 | } 12 | 13 | int main() 14 | { 15 | printf("main(): pid = %d, tid = %d\n", 16 | getpid(), CurrentThread::tid()); 17 | 18 | EventLoopThread loopThread; 19 | EventLoop* loop = loopThread.startLoop(); 20 | loop->runInLoop(runInThread); 21 | sleep(1); 22 | loop->runAfter(2, runInThread); 23 | sleep(3); 24 | loop->quit(); 25 | 26 | printf("exit main().\n"); 27 | } 28 | -------------------------------------------------------------------------------- /v3/src/base/Thread.h: -------------------------------------------------------------------------------- 1 | #ifndef _THREAD_H 2 | #define _THREAD_H 3 | 4 | #include "CountDownLatch.h" 5 | #include "notCopyable.h" 6 | 7 | #include 8 | #include 9 | 10 | class Thread: public notCopyable{ 11 | public: 12 | typedef std::function ThreadFunc; 13 | 14 | explicit Thread(ThreadFunc&& func); 15 | ~Thread(); 16 | 17 | void start(); 18 | int join(); 19 | 20 | bool started() const{return started_;}; 21 | pid_t tid(); 22 | 23 | private: 24 | bool started_; 25 | bool joined_; 26 | 27 | pid_t tid_; 28 | pthread_t pthreadId_; 29 | 30 | ThreadFunc func_; 31 | 32 | CountDownLatch latch_; 33 | }; 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /v1/muduo++/03/Thread.h: -------------------------------------------------------------------------------- 1 | #ifndef _THREAD_H 2 | #define _THREAD_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include "notCopyable.h" 8 | #include "CountDownLatch.h" 9 | 10 | class Thread: public notCopyable{ 11 | public: 12 | typedef std::function ThreadFunc; 13 | 14 | explicit Thread(const ThreadFunc&); 15 | ~Thread(); 16 | 17 | void start(); 18 | int join(); 19 | 20 | bool started() const{return started_;}; 21 | pid_t tid(); 22 | 23 | private: 24 | bool started_; 25 | bool joined_; 26 | pid_t tid_; 27 | pthread_t pthreadId_; 28 | ThreadFunc func_; 29 | CountDownLatch latch_; 30 | 31 | }; 32 | 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /v1/muduo++/04/Thread.h: -------------------------------------------------------------------------------- 1 | #ifndef _THREAD_H 2 | #define _THREAD_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include "notCopyable.h" 8 | #include "CountDownLatch.h" 9 | 10 | class Thread: public notCopyable{ 11 | public: 12 | typedef std::function ThreadFunc; 13 | 14 | explicit Thread(const ThreadFunc&); 15 | ~Thread(); 16 | 17 | void start(); 18 | int join(); 19 | 20 | bool started() const{return started_;}; 21 | pid_t tid(); 22 | 23 | private: 24 | bool started_; 25 | bool joined_; 26 | pid_t tid_; 27 | pthread_t pthreadId_; 28 | ThreadFunc func_; 29 | CountDownLatch latch_; 30 | 31 | }; 32 | 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /v1/muduo++/05/Thread.h: -------------------------------------------------------------------------------- 1 | #ifndef _THREAD_H 2 | #define _THREAD_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include "notCopyable.h" 8 | #include "CountDownLatch.h" 9 | 10 | class Thread: public notCopyable{ 11 | public: 12 | typedef std::function ThreadFunc; 13 | 14 | explicit Thread(const ThreadFunc&); 15 | ~Thread(); 16 | 17 | void start(); 18 | int join(); 19 | 20 | bool started() const{return started_;}; 21 | pid_t tid(); 22 | 23 | private: 24 | bool started_; 25 | bool joined_; 26 | pid_t tid_; 27 | pthread_t pthreadId_; 28 | ThreadFunc func_; 29 | CountDownLatch latch_; 30 | 31 | }; 32 | 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /v1/muduo++/06/Thread.h: -------------------------------------------------------------------------------- 1 | #ifndef _THREAD_H 2 | #define _THREAD_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include "notCopyable.h" 8 | #include "CountDownLatch.h" 9 | 10 | class Thread: public notCopyable{ 11 | public: 12 | typedef std::function ThreadFunc; 13 | 14 | explicit Thread(const ThreadFunc&); 15 | ~Thread(); 16 | 17 | void start(); 18 | int join(); 19 | 20 | bool started() const{return started_;}; 21 | pid_t tid(); 22 | 23 | private: 24 | bool started_; 25 | bool joined_; 26 | pid_t tid_; 27 | pthread_t pthreadId_; 28 | ThreadFunc func_; 29 | CountDownLatch latch_; 30 | 31 | }; 32 | 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /v1/muduo++/07/Thread.h: -------------------------------------------------------------------------------- 1 | #ifndef _THREAD_H 2 | #define _THREAD_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include "notCopyable.h" 8 | #include "CountDownLatch.h" 9 | 10 | class Thread: public notCopyable{ 11 | public: 12 | typedef std::function ThreadFunc; 13 | 14 | explicit Thread(const ThreadFunc&); 15 | ~Thread(); 16 | 17 | void start(); 18 | int join(); 19 | 20 | bool started() const{return started_;}; 21 | pid_t tid(); 22 | 23 | private: 24 | bool started_; 25 | bool joined_; 26 | pid_t tid_; 27 | pthread_t pthreadId_; 28 | ThreadFunc func_; 29 | CountDownLatch latch_; 30 | 31 | }; 32 | 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /v1/muduo++/08/Thread.h: -------------------------------------------------------------------------------- 1 | #ifndef _THREAD_H 2 | #define _THREAD_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include "notCopyable.h" 8 | #include "CountDownLatch.h" 9 | 10 | class Thread: public notCopyable{ 11 | public: 12 | typedef std::function ThreadFunc; 13 | 14 | explicit Thread(const ThreadFunc&); 15 | ~Thread(); 16 | 17 | void start(); 18 | int join(); 19 | 20 | bool started() const{return started_;}; 21 | pid_t tid(); 22 | 23 | private: 24 | bool started_; 25 | bool joined_; 26 | pid_t tid_; 27 | pthread_t pthreadId_; 28 | ThreadFunc func_; 29 | CountDownLatch latch_; 30 | 31 | }; 32 | 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /v1/muduo++/09/Thread.h: -------------------------------------------------------------------------------- 1 | #ifndef _THREAD_H 2 | #define _THREAD_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include "notCopyable.h" 8 | #include "CountDownLatch.h" 9 | 10 | class Thread: public notCopyable{ 11 | public: 12 | typedef std::function ThreadFunc; 13 | 14 | explicit Thread(const ThreadFunc&); 15 | ~Thread(); 16 | 17 | void start(); 18 | int join(); 19 | 20 | bool started() const{return started_;}; 21 | pid_t tid(); 22 | 23 | private: 24 | bool started_; 25 | bool joined_; 26 | pid_t tid_; 27 | pthread_t pthreadId_; 28 | ThreadFunc func_; 29 | CountDownLatch latch_; 30 | 31 | }; 32 | 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /v1/muduo++/02/Timestamp.cpp: -------------------------------------------------------------------------------- 1 | #include "Timestamp.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | Timestamp Timestamp::now(){ 9 | struct timeval tv; 10 | gettimeofday(&tv, NULL); 11 | int64_t seconds = tv.tv_sec; 12 | return Timestamp(seconds * kMicroSecondsPerSecond + tv.tv_usec); 13 | } 14 | 15 | std::string Timestamp::toString() const{ 16 | char buf[32] = {0}; 17 | int64_t seconds = microSecondsSinceEpoch_ / kMicroSecondsPerSecond; 18 | int64_t microseconds = microSecondsSinceEpoch_ % kMicroSecondsPerSecond; 19 | snprintf(buf, sizeof(buf) - 1, "%" PRId64 ".%06" PRId64 "", seconds, microseconds); 20 | return buf; 21 | } 22 | -------------------------------------------------------------------------------- /v1/muduo++/03/Timestamp.cpp: -------------------------------------------------------------------------------- 1 | #include "Timestamp.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | Timestamp Timestamp::now(){ 9 | struct timeval tv; 10 | gettimeofday(&tv, NULL); 11 | int64_t seconds = tv.tv_sec; 12 | return Timestamp(seconds * kMicroSecondsPerSecond + tv.tv_usec); 13 | } 14 | 15 | std::string Timestamp::toString() const{ 16 | char buf[32] = {0}; 17 | int64_t seconds = microSecondsSinceEpoch_ / kMicroSecondsPerSecond; 18 | int64_t microseconds = microSecondsSinceEpoch_ % kMicroSecondsPerSecond; 19 | snprintf(buf, sizeof(buf) - 1, "%" PRId64 ".%06" PRId64 "", seconds, microseconds); 20 | return buf; 21 | } 22 | -------------------------------------------------------------------------------- /v1/muduo++/04/Timestamp.cpp: -------------------------------------------------------------------------------- 1 | #include "Timestamp.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | Timestamp Timestamp::now(){ 9 | struct timeval tv; 10 | gettimeofday(&tv, NULL); 11 | int64_t seconds = tv.tv_sec; 12 | return Timestamp(seconds * kMicroSecondsPerSecond + tv.tv_usec); 13 | } 14 | 15 | std::string Timestamp::toString() const{ 16 | char buf[32] = {0}; 17 | int64_t seconds = microSecondsSinceEpoch_ / kMicroSecondsPerSecond; 18 | int64_t microseconds = microSecondsSinceEpoch_ % kMicroSecondsPerSecond; 19 | snprintf(buf, sizeof(buf) - 1, "%" PRId64 ".%06" PRId64 "", seconds, microseconds); 20 | return buf; 21 | } 22 | -------------------------------------------------------------------------------- /v1/muduo++/05/Timestamp.cpp: -------------------------------------------------------------------------------- 1 | #include "Timestamp.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | Timestamp Timestamp::now(){ 9 | struct timeval tv; 10 | gettimeofday(&tv, NULL); 11 | int64_t seconds = tv.tv_sec; 12 | return Timestamp(seconds * kMicroSecondsPerSecond + tv.tv_usec); 13 | } 14 | 15 | std::string Timestamp::toString() const{ 16 | char buf[32] = {0}; 17 | int64_t seconds = microSecondsSinceEpoch_ / kMicroSecondsPerSecond; 18 | int64_t microseconds = microSecondsSinceEpoch_ % kMicroSecondsPerSecond; 19 | snprintf(buf, sizeof(buf) - 1, "%" PRId64 ".%06" PRId64 "", seconds, microseconds); 20 | return buf; 21 | } 22 | -------------------------------------------------------------------------------- /v1/muduo++/06/Timestamp.cpp: -------------------------------------------------------------------------------- 1 | #include "Timestamp.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | Timestamp Timestamp::now(){ 9 | struct timeval tv; 10 | gettimeofday(&tv, NULL); 11 | int64_t seconds = tv.tv_sec; 12 | return Timestamp(seconds * kMicroSecondsPerSecond + tv.tv_usec); 13 | } 14 | 15 | std::string Timestamp::toString() const{ 16 | char buf[32] = {0}; 17 | int64_t seconds = microSecondsSinceEpoch_ / kMicroSecondsPerSecond; 18 | int64_t microseconds = microSecondsSinceEpoch_ % kMicroSecondsPerSecond; 19 | snprintf(buf, sizeof(buf) - 1, "%" PRId64 ".%06" PRId64 "", seconds, microseconds); 20 | return buf; 21 | } 22 | -------------------------------------------------------------------------------- /v1/muduo++/07/Timestamp.cpp: -------------------------------------------------------------------------------- 1 | #include "Timestamp.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | Timestamp Timestamp::now(){ 9 | struct timeval tv; 10 | gettimeofday(&tv, NULL); 11 | int64_t seconds = tv.tv_sec; 12 | return Timestamp(seconds * kMicroSecondsPerSecond + tv.tv_usec); 13 | } 14 | 15 | std::string Timestamp::toString() const{ 16 | char buf[32] = {0}; 17 | int64_t seconds = microSecondsSinceEpoch_ / kMicroSecondsPerSecond; 18 | int64_t microseconds = microSecondsSinceEpoch_ % kMicroSecondsPerSecond; 19 | snprintf(buf, sizeof(buf) - 1, "%" PRId64 ".%06" PRId64 "", seconds, microseconds); 20 | return buf; 21 | } 22 | -------------------------------------------------------------------------------- /v1/muduo++/08/Timestamp.cpp: -------------------------------------------------------------------------------- 1 | #include "Timestamp.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | Timestamp Timestamp::now(){ 9 | struct timeval tv; 10 | gettimeofday(&tv, NULL); 11 | int64_t seconds = tv.tv_sec; 12 | return Timestamp(seconds * kMicroSecondsPerSecond + tv.tv_usec); 13 | } 14 | 15 | std::string Timestamp::toString() const{ 16 | char buf[32] = {0}; 17 | int64_t seconds = microSecondsSinceEpoch_ / kMicroSecondsPerSecond; 18 | int64_t microseconds = microSecondsSinceEpoch_ % kMicroSecondsPerSecond; 19 | snprintf(buf, sizeof(buf) - 1, "%" PRId64 ".%06" PRId64 "", seconds, microseconds); 20 | return buf; 21 | } 22 | -------------------------------------------------------------------------------- /v1/muduo++/09/Timestamp.cpp: -------------------------------------------------------------------------------- 1 | #include "Timestamp.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | Timestamp Timestamp::now(){ 9 | struct timeval tv; 10 | gettimeofday(&tv, NULL); 11 | int64_t seconds = tv.tv_sec; 12 | return Timestamp(seconds * kMicroSecondsPerSecond + tv.tv_usec); 13 | } 14 | 15 | std::string Timestamp::toString() const{ 16 | char buf[32] = {0}; 17 | int64_t seconds = microSecondsSinceEpoch_ / kMicroSecondsPerSecond; 18 | int64_t microseconds = microSecondsSinceEpoch_ % kMicroSecondsPerSecond; 19 | snprintf(buf, sizeof(buf) - 1, "%" PRId64 ".%06" PRId64 "", seconds, microseconds); 20 | return buf; 21 | } 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # suika 2 | 3 | ## v1: muduo++ 4 | * a step-by-step muduo-like web server implementation 5 | 6 | ## v2: suika 7 | * a little step beyond muduo, add several modifications on v1 8 | 9 | ## v3: suika with efficient buffer design and connection pool 10 | * performance is significantly improved over Webbench compared with [moduo](https://github.com/chenshuo/muduo), look [here](https://github.com/ChyauAng/suika/blob/master/v3/v3性能评估.md) 11 | 12 | * one eventloop per thread + thread pool 13 | * Nignx-like connection pool 14 | * more efficient buffer design: sds(simple dynamic string, Redis-like) + scatter IO 15 | * more in process...look [here](https://github.com/ChyauAng/suika/blob/master/v3/README.md) 16 | -------------------------------------------------------------------------------- /v1/muduo++/04/Endian.h: -------------------------------------------------------------------------------- 1 | #ifndef _ENDIAN_H_ 2 | #define _ENDIAN_H_ 3 | 4 | #include 5 | #include 6 | 7 | inline uint64_t hostToNetwork64(uint64_t host64){ 8 | return htobe64(host64); 9 | } 10 | 11 | inline uint32_t hostToNetwork32(uint32_t host32){ 12 | return htobe32(host32); 13 | } 14 | 15 | 16 | inline uint16_t hostToNetwork16(uint16_t host16){ 17 | return htobe16(host16); 18 | } 19 | 20 | inline uint64_t networkToHost64(uint64_t net64){ 21 | return be64toh(net64); 22 | } 23 | 24 | 25 | inline uint32_t networkToHost32(uint32_t net32){ 26 | return be32toh(net32); 27 | } 28 | 29 | 30 | inline uint16_t networkToHost16(uint16_t net16){ 31 | return be16toh(net16); 32 | } 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /v1/muduo++/05/Endian.h: -------------------------------------------------------------------------------- 1 | #ifndef _ENDIAN_H_ 2 | #define _ENDIAN_H_ 3 | 4 | #include 5 | #include 6 | 7 | inline uint64_t hostToNetwork64(uint64_t host64){ 8 | return htobe64(host64); 9 | } 10 | 11 | inline uint32_t hostToNetwork32(uint32_t host32){ 12 | return htobe32(host32); 13 | } 14 | 15 | 16 | inline uint16_t hostToNetwork16(uint16_t host16){ 17 | return htobe16(host16); 18 | } 19 | 20 | inline uint64_t networkToHost64(uint64_t net64){ 21 | return be64toh(net64); 22 | } 23 | 24 | 25 | inline uint32_t networkToHost32(uint32_t net32){ 26 | return be32toh(net32); 27 | } 28 | 29 | 30 | inline uint16_t networkToHost16(uint16_t net16){ 31 | return be16toh(net16); 32 | } 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /v1/muduo++/06/Endian.h: -------------------------------------------------------------------------------- 1 | #ifndef _ENDIAN_H_ 2 | #define _ENDIAN_H_ 3 | 4 | #include 5 | #include 6 | 7 | inline uint64_t hostToNetwork64(uint64_t host64){ 8 | return htobe64(host64); 9 | } 10 | 11 | inline uint32_t hostToNetwork32(uint32_t host32){ 12 | return htobe32(host32); 13 | } 14 | 15 | 16 | inline uint16_t hostToNetwork16(uint16_t host16){ 17 | return htobe16(host16); 18 | } 19 | 20 | inline uint64_t networkToHost64(uint64_t net64){ 21 | return be64toh(net64); 22 | } 23 | 24 | 25 | inline uint32_t networkToHost32(uint32_t net32){ 26 | return be32toh(net32); 27 | } 28 | 29 | 30 | inline uint16_t networkToHost16(uint16_t net16){ 31 | return be16toh(net16); 32 | } 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /v1/muduo++/07/Endian.h: -------------------------------------------------------------------------------- 1 | #ifndef _ENDIAN_H_ 2 | #define _ENDIAN_H_ 3 | 4 | #include 5 | #include 6 | 7 | inline uint64_t hostToNetwork64(uint64_t host64){ 8 | return htobe64(host64); 9 | } 10 | 11 | inline uint32_t hostToNetwork32(uint32_t host32){ 12 | return htobe32(host32); 13 | } 14 | 15 | 16 | inline uint16_t hostToNetwork16(uint16_t host16){ 17 | return htobe16(host16); 18 | } 19 | 20 | inline uint64_t networkToHost64(uint64_t net64){ 21 | return be64toh(net64); 22 | } 23 | 24 | 25 | inline uint32_t networkToHost32(uint32_t net32){ 26 | return be32toh(net32); 27 | } 28 | 29 | 30 | inline uint16_t networkToHost16(uint16_t net16){ 31 | return be16toh(net16); 32 | } 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /v1/muduo++/08/Endian.h: -------------------------------------------------------------------------------- 1 | #ifndef _ENDIAN_H_ 2 | #define _ENDIAN_H_ 3 | 4 | #include 5 | #include 6 | 7 | inline uint64_t hostToNetwork64(uint64_t host64){ 8 | return htobe64(host64); 9 | } 10 | 11 | inline uint32_t hostToNetwork32(uint32_t host32){ 12 | return htobe32(host32); 13 | } 14 | 15 | 16 | inline uint16_t hostToNetwork16(uint16_t host16){ 17 | return htobe16(host16); 18 | } 19 | 20 | inline uint64_t networkToHost64(uint64_t net64){ 21 | return be64toh(net64); 22 | } 23 | 24 | 25 | inline uint32_t networkToHost32(uint32_t net32){ 26 | return be32toh(net32); 27 | } 28 | 29 | 30 | inline uint16_t networkToHost16(uint16_t net16){ 31 | return be16toh(net16); 32 | } 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /v1/muduo++/09/Endian.h: -------------------------------------------------------------------------------- 1 | #ifndef _ENDIAN_H_ 2 | #define _ENDIAN_H_ 3 | 4 | #include 5 | #include 6 | 7 | inline uint64_t hostToNetwork64(uint64_t host64){ 8 | return htobe64(host64); 9 | } 10 | 11 | inline uint32_t hostToNetwork32(uint32_t host32){ 12 | return htobe32(host32); 13 | } 14 | 15 | 16 | inline uint16_t hostToNetwork16(uint16_t host16){ 17 | return htobe16(host16); 18 | } 19 | 20 | inline uint64_t networkToHost64(uint64_t net64){ 21 | return be64toh(net64); 22 | } 23 | 24 | 25 | inline uint32_t networkToHost32(uint32_t net32){ 26 | return be32toh(net32); 27 | } 28 | 29 | 30 | inline uint16_t networkToHost16(uint16_t net16){ 31 | return be16toh(net16); 32 | } 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /v1/muduo++/10/Endian.h: -------------------------------------------------------------------------------- 1 | #ifndef _ENDIAN_H_ 2 | #define _ENDIAN_H_ 3 | 4 | #include 5 | #include 6 | 7 | inline uint64_t hostToNetwork64(uint64_t host64){ 8 | return htobe64(host64); 9 | } 10 | 11 | inline uint32_t hostToNetwork32(uint32_t host32){ 12 | return htobe32(host32); 13 | } 14 | 15 | 16 | inline uint16_t hostToNetwork16(uint16_t host16){ 17 | return htobe16(host16); 18 | } 19 | 20 | inline uint64_t networkToHost64(uint64_t net64){ 21 | return be64toh(net64); 22 | } 23 | 24 | 25 | inline uint32_t networkToHost32(uint32_t net32){ 26 | return be32toh(net32); 27 | } 28 | 29 | 30 | inline uint16_t networkToHost16(uint16_t net16){ 31 | return be16toh(net16); 32 | } 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /v1/muduo++/11/Endian.h: -------------------------------------------------------------------------------- 1 | #ifndef _ENDIAN_H_ 2 | #define _ENDIAN_H_ 3 | 4 | #include 5 | #include 6 | 7 | inline uint64_t hostToNetwork64(uint64_t host64){ 8 | return htobe64(host64); 9 | } 10 | 11 | inline uint32_t hostToNetwork32(uint32_t host32){ 12 | return htobe32(host32); 13 | } 14 | 15 | 16 | inline uint16_t hostToNetwork16(uint16_t host16){ 17 | return htobe16(host16); 18 | } 19 | 20 | inline uint64_t networkToHost64(uint64_t net64){ 21 | return be64toh(net64); 22 | } 23 | 24 | 25 | inline uint32_t networkToHost32(uint32_t net32){ 26 | return be32toh(net32); 27 | } 28 | 29 | 30 | inline uint16_t networkToHost16(uint16_t net16){ 31 | return be16toh(net16); 32 | } 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /v2/src/net/Endian.h: -------------------------------------------------------------------------------- 1 | #ifndef _ENDIAN_H_ 2 | #define _ENDIAN_H_ 3 | 4 | #include 5 | #include 6 | 7 | inline uint64_t hostToNetwork64(uint64_t host64){ 8 | return htobe64(host64); 9 | } 10 | 11 | inline uint32_t hostToNetwork32(uint32_t host32){ 12 | return htobe32(host32); 13 | } 14 | 15 | 16 | inline uint16_t hostToNetwork16(uint16_t host16){ 17 | return htobe16(host16); 18 | } 19 | 20 | inline uint64_t networkToHost64(uint64_t net64){ 21 | return be64toh(net64); 22 | } 23 | 24 | 25 | inline uint32_t networkToHost32(uint32_t net32){ 26 | return be32toh(net32); 27 | } 28 | 29 | 30 | inline uint16_t networkToHost16(uint16_t net16){ 31 | return be16toh(net16); 32 | } 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /v2/src/app/http/Makefile: -------------------------------------------------------------------------------- 1 | TARGET := HttpServer_test 2 | 3 | SOURCE := $(wildcard *.c) $(wildcard *.cpp) 4 | OBJS := $(patsubst %.c, %.o, $(patsubst %.cpp, %.o, $(SOURCE))) 5 | 6 | MYREACTOR_SRC := $(wildcard ../../net/*.cpp) 7 | MYREACTOR_OBJS := $(patsubst %.cpp, %.o, $(MYREACTOR_SRC)) 8 | 9 | CC := gcc 10 | CXX := g++ 11 | 12 | LIBS := ../../net/libmyreactor.a 13 | LDFLAGS := 14 | 15 | DEFINES := 16 | INCLUDE := -I . 17 | 18 | CFLAGS := -g -o3 $(DEFINES) $(INCLUDE) 19 | CXXFLAGS := $(CFLAGS) -std=c++11 -pthread -DNDEBUG 20 | 21 | .PHONY : all clean rebuild 22 | 23 | all: $(TARGET) 24 | clean: 25 | rm -f *.o 26 | rm $(TARGET) 27 | rebuild: clean all 28 | 29 | $(TARGET) : $(OBJS) 30 | $(CXX) $(CXXFLAGS) -o $@ $(OBJS) $(LDFLAGS) $(LIBS) 31 | -------------------------------------------------------------------------------- /v1/muduo++/04/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Acceptor.h" 2 | #include "EventLoop.h" 3 | #include "InetAddress.h" 4 | #include "SocketsOps.h" 5 | #include 6 | #include 7 | #include 8 | 9 | 10 | void newConnection(int sockfd, const InetAddress& peerAddr){ 11 | printf("newConnection(): accept s new connection from %s\n", peerAddr.toIpPort().c_str()); 12 | ::write(sockfd, "How are you?\n", 13); 13 | sockets::close(sockfd); 14 | } 15 | 16 | int main(){ 17 | printf("main(): pid = %d\n", ::getpid()); 18 | InetAddress listenAddr(9981); 19 | EventLoop loop; 20 | 21 | Acceptor acceptor(&loop, listenAddr, 1); 22 | acceptor.setNewConnectionCallback(newConnection); 23 | acceptor.listen(); 24 | 25 | loop.loop(); 26 | } 27 | -------------------------------------------------------------------------------- /v1/muduo++/03/Condition.cpp: -------------------------------------------------------------------------------- 1 | #include "Condition.h" 2 | 3 | #include 4 | #include 5 | 6 | bool Condition::waitForSeconds(double seconds){ 7 | struct timespec relativetime; 8 | clock_gettime(CLOCK_MONOTONIC, &relativetime); 9 | 10 | const int64_t KNanoSecondsPerSecond = 1000000000; 11 | int64_t nanoseconds = static_cast(seconds * KNanoSecondsPerSecond); 12 | 13 | relativetime.tv_sec += static_cast((relativetime.tv_nsec + nanoseconds) / KNanoSecondsPerSecond); 14 | relativetime.tv_nsec = static_cast((relativetime.tv_nsec + nanoseconds) % KNanoSecondsPerSecond); 15 | 16 | MutexLock::UnassignGuard ug(mutex_); 17 | return ETIMEDOUT == pthread_cond_timedwait(&pcond_, mutex_.getMutex(), &relativetime); 18 | } 19 | -------------------------------------------------------------------------------- /v1/muduo++/04/Condition.cpp: -------------------------------------------------------------------------------- 1 | #include "Condition.h" 2 | 3 | #include 4 | #include 5 | 6 | bool Condition::waitForSeconds(double seconds){ 7 | struct timespec relativetime; 8 | clock_gettime(CLOCK_MONOTONIC, &relativetime); 9 | 10 | const int64_t KNanoSecondsPerSecond = 1000000000; 11 | int64_t nanoseconds = static_cast(seconds * KNanoSecondsPerSecond); 12 | 13 | relativetime.tv_sec += static_cast((relativetime.tv_nsec + nanoseconds) / KNanoSecondsPerSecond); 14 | relativetime.tv_nsec = static_cast((relativetime.tv_nsec + nanoseconds) % KNanoSecondsPerSecond); 15 | 16 | MutexLock::UnassignGuard ug(mutex_); 17 | return ETIMEDOUT == pthread_cond_timedwait(&pcond_, mutex_.getMutex(), &relativetime); 18 | } 19 | -------------------------------------------------------------------------------- /v1/muduo++/05/Condition.cpp: -------------------------------------------------------------------------------- 1 | #include "Condition.h" 2 | 3 | #include 4 | #include 5 | 6 | bool Condition::waitForSeconds(double seconds){ 7 | struct timespec relativetime; 8 | clock_gettime(CLOCK_MONOTONIC, &relativetime); 9 | 10 | const int64_t KNanoSecondsPerSecond = 1000000000; 11 | int64_t nanoseconds = static_cast(seconds * KNanoSecondsPerSecond); 12 | 13 | relativetime.tv_sec += static_cast((relativetime.tv_nsec + nanoseconds) / KNanoSecondsPerSecond); 14 | relativetime.tv_nsec = static_cast((relativetime.tv_nsec + nanoseconds) % KNanoSecondsPerSecond); 15 | 16 | MutexLock::UnassignGuard ug(mutex_); 17 | return ETIMEDOUT == pthread_cond_timedwait(&pcond_, mutex_.getMutex(), &relativetime); 18 | } 19 | -------------------------------------------------------------------------------- /v1/muduo++/06/Condition.cpp: -------------------------------------------------------------------------------- 1 | #include "Condition.h" 2 | 3 | #include 4 | #include 5 | 6 | bool Condition::waitForSeconds(double seconds){ 7 | struct timespec relativetime; 8 | clock_gettime(CLOCK_MONOTONIC, &relativetime); 9 | 10 | const int64_t KNanoSecondsPerSecond = 1000000000; 11 | int64_t nanoseconds = static_cast(seconds * KNanoSecondsPerSecond); 12 | 13 | relativetime.tv_sec += static_cast((relativetime.tv_nsec + nanoseconds) / KNanoSecondsPerSecond); 14 | relativetime.tv_nsec = static_cast((relativetime.tv_nsec + nanoseconds) % KNanoSecondsPerSecond); 15 | 16 | MutexLock::UnassignGuard ug(mutex_); 17 | return ETIMEDOUT == pthread_cond_timedwait(&pcond_, mutex_.getMutex(), &relativetime); 18 | } 19 | -------------------------------------------------------------------------------- /v1/muduo++/07/Condition.cpp: -------------------------------------------------------------------------------- 1 | #include "Condition.h" 2 | 3 | #include 4 | #include 5 | 6 | bool Condition::waitForSeconds(double seconds){ 7 | struct timespec relativetime; 8 | clock_gettime(CLOCK_MONOTONIC, &relativetime); 9 | 10 | const int64_t KNanoSecondsPerSecond = 1000000000; 11 | int64_t nanoseconds = static_cast(seconds * KNanoSecondsPerSecond); 12 | 13 | relativetime.tv_sec += static_cast((relativetime.tv_nsec + nanoseconds) / KNanoSecondsPerSecond); 14 | relativetime.tv_nsec = static_cast((relativetime.tv_nsec + nanoseconds) % KNanoSecondsPerSecond); 15 | 16 | MutexLock::UnassignGuard ug(mutex_); 17 | return ETIMEDOUT == pthread_cond_timedwait(&pcond_, mutex_.getMutex(), &relativetime); 18 | } 19 | -------------------------------------------------------------------------------- /v1/muduo++/08/Condition.cpp: -------------------------------------------------------------------------------- 1 | #include "Condition.h" 2 | 3 | #include 4 | #include 5 | 6 | bool Condition::waitForSeconds(double seconds){ 7 | struct timespec relativetime; 8 | clock_gettime(CLOCK_MONOTONIC, &relativetime); 9 | 10 | const int64_t KNanoSecondsPerSecond = 1000000000; 11 | int64_t nanoseconds = static_cast(seconds * KNanoSecondsPerSecond); 12 | 13 | relativetime.tv_sec += static_cast((relativetime.tv_nsec + nanoseconds) / KNanoSecondsPerSecond); 14 | relativetime.tv_nsec = static_cast((relativetime.tv_nsec + nanoseconds) % KNanoSecondsPerSecond); 15 | 16 | MutexLock::UnassignGuard ug(mutex_); 17 | return ETIMEDOUT == pthread_cond_timedwait(&pcond_, mutex_.getMutex(), &relativetime); 18 | } 19 | -------------------------------------------------------------------------------- /v1/muduo++/09/Condition.cpp: -------------------------------------------------------------------------------- 1 | #include "Condition.h" 2 | 3 | #include 4 | #include 5 | 6 | bool Condition::waitForSeconds(double seconds){ 7 | struct timespec relativetime; 8 | clock_gettime(CLOCK_MONOTONIC, &relativetime); 9 | 10 | const int64_t KNanoSecondsPerSecond = 1000000000; 11 | int64_t nanoseconds = static_cast(seconds * KNanoSecondsPerSecond); 12 | 13 | relativetime.tv_sec += static_cast((relativetime.tv_nsec + nanoseconds) / KNanoSecondsPerSecond); 14 | relativetime.tv_nsec = static_cast((relativetime.tv_nsec + nanoseconds) % KNanoSecondsPerSecond); 15 | 16 | MutexLock::UnassignGuard ug(mutex_); 17 | return ETIMEDOUT == pthread_cond_timedwait(&pcond_, mutex_.getMutex(), &relativetime); 18 | } 19 | -------------------------------------------------------------------------------- /v1/muduo++/10/Condition.cpp: -------------------------------------------------------------------------------- 1 | #include "Condition.h" 2 | 3 | #include 4 | #include 5 | 6 | bool Condition::waitForSeconds(double seconds){ 7 | struct timespec relativetime; 8 | clock_gettime(CLOCK_MONOTONIC, &relativetime); 9 | 10 | const int64_t KNanoSecondsPerSecond = 1000000000; 11 | int64_t nanoseconds = static_cast(seconds * KNanoSecondsPerSecond); 12 | 13 | relativetime.tv_sec += static_cast((relativetime.tv_nsec + nanoseconds) / KNanoSecondsPerSecond); 14 | relativetime.tv_nsec = static_cast((relativetime.tv_nsec + nanoseconds) % KNanoSecondsPerSecond); 15 | 16 | MutexLock::UnassignGuard ug(mutex_); 17 | return ETIMEDOUT == pthread_cond_timedwait(&pcond_, mutex_.getMutex(), &relativetime); 18 | } 19 | -------------------------------------------------------------------------------- /v1/muduo++/11/Condition.cpp: -------------------------------------------------------------------------------- 1 | #include "Condition.h" 2 | 3 | #include 4 | #include 5 | 6 | bool Condition::waitForSeconds(double seconds){ 7 | struct timespec relativetime; 8 | clock_gettime(CLOCK_MONOTONIC, &relativetime); 9 | 10 | const int64_t KNanoSecondsPerSecond = 1000000000; 11 | int64_t nanoseconds = static_cast(seconds * KNanoSecondsPerSecond); 12 | 13 | relativetime.tv_sec += static_cast((relativetime.tv_nsec + nanoseconds) / KNanoSecondsPerSecond); 14 | relativetime.tv_nsec = static_cast((relativetime.tv_nsec + nanoseconds) % KNanoSecondsPerSecond); 15 | 16 | MutexLock::UnassignGuard ug(mutex_); 17 | return ETIMEDOUT == pthread_cond_timedwait(&pcond_, mutex_.getMutex(), &relativetime); 18 | } 19 | -------------------------------------------------------------------------------- /v2/src/net/Condition.cpp: -------------------------------------------------------------------------------- 1 | #include "Condition.h" 2 | 3 | #include 4 | #include 5 | 6 | bool Condition::waitForSeconds(double seconds){ 7 | struct timespec relativetime; 8 | clock_gettime(CLOCK_MONOTONIC, &relativetime); 9 | 10 | const int64_t KNanoSecondsPerSecond = 1000000000; 11 | int64_t nanoseconds = static_cast(seconds * KNanoSecondsPerSecond); 12 | 13 | relativetime.tv_sec += static_cast((relativetime.tv_nsec + nanoseconds) / KNanoSecondsPerSecond); 14 | relativetime.tv_nsec = static_cast((relativetime.tv_nsec + nanoseconds) % KNanoSecondsPerSecond); 15 | 16 | MutexLock::UnassignGuard ug(mutex_); 17 | return ETIMEDOUT == pthread_cond_timedwait(&pcond_, mutex_.getMutex(), &relativetime); 18 | } 19 | -------------------------------------------------------------------------------- /v1/muduo++/03/EventLoopThread.h: -------------------------------------------------------------------------------- 1 | #ifndef _EVENTLOOPTHREAD_H_ 2 | #define _EVENTLOOPTHREAD_H_ 3 | 4 | #include "Condition.h" 5 | #include "Mutex.h" 6 | #include "Thread.h" 7 | #include "notCopyable.h" 8 | 9 | class EventLoop; 10 | 11 | // one event loop per thread 12 | // start an IO thread then waiting for events coming 13 | class EventLoopThread:public notCopyable{ 14 | public: 15 | typedef std::function ThreadInitCallback; 16 | EventLoopThread(); 17 | // EventLoopThread(const ThreadInitCallback& cb = ThreadInitCallback()); 18 | ~EventLoopThread(); 19 | EventLoop* startLoop(); 20 | 21 | private: 22 | void threadFunc(); 23 | 24 | EventLoop* loop_; 25 | Thread thread_; 26 | MutexLock mutex_; 27 | Condition cond_; 28 | ThreadInitCallback callback_; 29 | }; 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /v1/muduo++/04/EventLoopThread.h: -------------------------------------------------------------------------------- 1 | #ifndef _EVENTLOOPTHREAD_H_ 2 | #define _EVENTLOOPTHREAD_H_ 3 | 4 | #include "Condition.h" 5 | #include "Mutex.h" 6 | #include "Thread.h" 7 | #include "notCopyable.h" 8 | 9 | class EventLoop; 10 | 11 | // one event loop per thread 12 | // start an IO thread then waiting for events coming 13 | class EventLoopThread:public notCopyable{ 14 | public: 15 | typedef std::function ThreadInitCallback; 16 | EventLoopThread(); 17 | // EventLoopThread(const ThreadInitCallback& cb = ThreadInitCallback()); 18 | ~EventLoopThread(); 19 | EventLoop* startLoop(); 20 | 21 | private: 22 | void threadFunc(); 23 | 24 | EventLoop* loop_; 25 | Thread thread_; 26 | MutexLock mutex_; 27 | Condition cond_; 28 | ThreadInitCallback callback_; 29 | }; 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /v1/muduo++/05/EventLoopThread.h: -------------------------------------------------------------------------------- 1 | #ifndef _EVENTLOOPTHREAD_H_ 2 | #define _EVENTLOOPTHREAD_H_ 3 | 4 | #include "Condition.h" 5 | #include "Mutex.h" 6 | #include "Thread.h" 7 | #include "notCopyable.h" 8 | 9 | class EventLoop; 10 | 11 | // one event loop per thread 12 | // start an IO thread then waiting for events coming 13 | class EventLoopThread:public notCopyable{ 14 | public: 15 | typedef std::function ThreadInitCallback; 16 | EventLoopThread(); 17 | // EventLoopThread(const ThreadInitCallback& cb = ThreadInitCallback()); 18 | ~EventLoopThread(); 19 | EventLoop* startLoop(); 20 | 21 | private: 22 | void threadFunc(); 23 | 24 | EventLoop* loop_; 25 | Thread thread_; 26 | MutexLock mutex_; 27 | Condition cond_; 28 | ThreadInitCallback callback_; 29 | }; 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /v1/muduo++/06/EventLoopThread.h: -------------------------------------------------------------------------------- 1 | #ifndef _EVENTLOOPTHREAD_H_ 2 | #define _EVENTLOOPTHREAD_H_ 3 | 4 | #include "Condition.h" 5 | #include "Mutex.h" 6 | #include "Thread.h" 7 | #include "notCopyable.h" 8 | 9 | class EventLoop; 10 | 11 | // one event loop per thread 12 | // start an IO thread then waiting for events coming 13 | class EventLoopThread:public notCopyable{ 14 | public: 15 | typedef std::function ThreadInitCallback; 16 | EventLoopThread(); 17 | // EventLoopThread(const ThreadInitCallback& cb = ThreadInitCallback()); 18 | ~EventLoopThread(); 19 | EventLoop* startLoop(); 20 | 21 | private: 22 | void threadFunc(); 23 | 24 | EventLoop* loop_; 25 | Thread thread_; 26 | MutexLock mutex_; 27 | Condition cond_; 28 | ThreadInitCallback callback_; 29 | }; 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /v1/muduo++/07/EventLoopThread.h: -------------------------------------------------------------------------------- 1 | #ifndef _EVENTLOOPTHREAD_H_ 2 | #define _EVENTLOOPTHREAD_H_ 3 | 4 | #include "Condition.h" 5 | #include "Mutex.h" 6 | #include "Thread.h" 7 | #include "notCopyable.h" 8 | 9 | class EventLoop; 10 | 11 | // one event loop per thread 12 | // start an IO thread then waiting for events coming 13 | class EventLoopThread:public notCopyable{ 14 | public: 15 | typedef std::function ThreadInitCallback; 16 | // EventLoopThread(); 17 | EventLoopThread(const ThreadInitCallback& cb = ThreadInitCallback()); 18 | ~EventLoopThread(); 19 | EventLoop* startLoop(); 20 | 21 | private: 22 | void threadFunc(); 23 | 24 | EventLoop* loop_; 25 | Thread thread_; 26 | MutexLock mutex_; 27 | Condition cond_; 28 | ThreadInitCallback callback_; 29 | }; 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /v1/muduo++/08/EventLoopThread.h: -------------------------------------------------------------------------------- 1 | #ifndef _EVENTLOOPTHREAD_H_ 2 | #define _EVENTLOOPTHREAD_H_ 3 | 4 | #include "Condition.h" 5 | #include "Mutex.h" 6 | #include "Thread.h" 7 | #include "notCopyable.h" 8 | 9 | class EventLoop; 10 | 11 | // one event loop per thread 12 | // start an IO thread then waiting for events coming 13 | class EventLoopThread:public notCopyable{ 14 | public: 15 | typedef std::function ThreadInitCallback; 16 | // EventLoopThread(); 17 | EventLoopThread(const ThreadInitCallback& cb = ThreadInitCallback()); 18 | ~EventLoopThread(); 19 | EventLoop* startLoop(); 20 | 21 | private: 22 | void threadFunc(); 23 | 24 | EventLoop* loop_; 25 | Thread thread_; 26 | MutexLock mutex_; 27 | Condition cond_; 28 | ThreadInitCallback callback_; 29 | }; 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /v1/muduo++/02/Atomic.h: -------------------------------------------------------------------------------- 1 | #ifndef _ATOMIC_H_ 2 | #define _ATOMIC_H_ 3 | 4 | #include "notCopyable.h" 5 | #include 6 | 7 | template 8 | class AtomicIntegerT: public notCopyable{ 9 | public: 10 | AtomicIntegerT() 11 | :value_(0){ 12 | 13 | } 14 | 15 | T get(){ 16 | // gcc atomic operation 17 | return __sync_val_compare_and_swap(&value_, 0, 0); 18 | } 19 | 20 | T getAndAdd(T x){ 21 | return __sync_fetch_and_add(&value_, x); 22 | } 23 | 24 | T addAndGet(T x){ 25 | return getAndAdd(x) + x; 26 | } 27 | 28 | 29 | T incrementAndGet(){ 30 | return addAndGet(1); 31 | } 32 | 33 | private: 34 | volatile T value_; 35 | }; 36 | 37 | typedef AtomicIntegerT AtomicInt32; 38 | typedef AtomicIntegerT AtomicInt64; 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /v1/muduo++/03/Atomic.h: -------------------------------------------------------------------------------- 1 | #ifndef _ATOMIC_H_ 2 | #define _ATOMIC_H_ 3 | 4 | #include "notCopyable.h" 5 | #include 6 | 7 | template 8 | class AtomicIntegerT: public notCopyable{ 9 | public: 10 | AtomicIntegerT() 11 | :value_(0){ 12 | 13 | } 14 | 15 | T get(){ 16 | // gcc atomic operation 17 | return __sync_val_compare_and_swap(&value_, 0, 0); 18 | } 19 | 20 | T getAndAdd(T x){ 21 | return __sync_fetch_and_add(&value_, x); 22 | } 23 | 24 | T addAndGet(T x){ 25 | return getAndAdd(x) + x; 26 | } 27 | 28 | 29 | T incrementAndGet(){ 30 | return addAndGet(1); 31 | } 32 | 33 | private: 34 | volatile T value_; 35 | }; 36 | 37 | typedef AtomicIntegerT AtomicInt32; 38 | typedef AtomicIntegerT AtomicInt64; 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /v1/muduo++/04/Atomic.h: -------------------------------------------------------------------------------- 1 | #ifndef _ATOMIC_H_ 2 | #define _ATOMIC_H_ 3 | 4 | #include "notCopyable.h" 5 | #include 6 | 7 | template 8 | class AtomicIntegerT: public notCopyable{ 9 | public: 10 | AtomicIntegerT() 11 | :value_(0){ 12 | 13 | } 14 | 15 | T get(){ 16 | // gcc atomic operation 17 | return __sync_val_compare_and_swap(&value_, 0, 0); 18 | } 19 | 20 | T getAndAdd(T x){ 21 | return __sync_fetch_and_add(&value_, x); 22 | } 23 | 24 | T addAndGet(T x){ 25 | return getAndAdd(x) + x; 26 | } 27 | 28 | 29 | T incrementAndGet(){ 30 | return addAndGet(1); 31 | } 32 | 33 | private: 34 | volatile T value_; 35 | }; 36 | 37 | typedef AtomicIntegerT AtomicInt32; 38 | typedef AtomicIntegerT AtomicInt64; 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /v1/muduo++/01/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "EventLoop.h" 5 | #include "Channel.h" 6 | 7 | EventLoop* g_loop; 8 | 9 | void timeout(){ 10 | printf("Timeout!\n"); 11 | g_loop->quit(); 12 | } 13 | 14 | int main(){ 15 | EventLoop loop; 16 | g_loop = &loop; 17 | 18 | int timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC); 19 | Channel channel(&loop, timerfd); 20 | channel.setReadCallback(timeout); 21 | channel.enableReading(); 22 | 23 | struct itimerspec howlong; 24 | bzero(&howlong, sizeof howlong); 25 | howlong.it_value.tv_sec = 5; 26 | timerfd_settime(timerfd, 0, &howlong, NULL); 27 | 28 | loop.loop(); 29 | 30 | // close the timer file descriptor 31 | //close(timerfd); 32 | 33 | return 0; 34 | } 35 | 36 | -------------------------------------------------------------------------------- /v1/muduo++/09/EventLoopThread.h: -------------------------------------------------------------------------------- 1 | #ifndef _EVENTLOOPTHREAD_H_ 2 | #define _EVENTLOOPTHREAD_H_ 3 | 4 | #include "Condition.h" 5 | #include "Mutex.h" 6 | #include "Thread.h" 7 | #include "notCopyable.h" 8 | 9 | class EventLoop; 10 | 11 | // one event loop per thread 12 | // start an IO thread then waiting for events coming 13 | class EventLoopThread:public notCopyable{ 14 | public: 15 | typedef std::function ThreadInitCallback; 16 | // EventLoopThread(); 17 | EventLoopThread(const ThreadInitCallback& cb = ThreadInitCallback()); 18 | ~EventLoopThread(); 19 | EventLoop* startLoop(); 20 | 21 | private: 22 | void threadFunc(); 23 | 24 | EventLoop* loop_; 25 | bool existing_; 26 | Thread thread_; 27 | MutexLock mutex_; 28 | Condition cond_; 29 | ThreadInitCallback callback_; 30 | }; 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /v1/muduo++/10/EventLoopThread.h: -------------------------------------------------------------------------------- 1 | #ifndef _EVENTLOOPTHREAD_H_ 2 | #define _EVENTLOOPTHREAD_H_ 3 | 4 | #include "Condition.h" 5 | #include "Mutex.h" 6 | #include "Thread.h" 7 | #include "notCopyable.h" 8 | 9 | class EventLoop; 10 | 11 | // one event loop per thread 12 | // start an IO thread then waiting for events coming 13 | class EventLoopThread:public notCopyable{ 14 | public: 15 | typedef std::function ThreadInitCallback; 16 | // EventLoopThread(); 17 | EventLoopThread(const ThreadInitCallback& cb = ThreadInitCallback()); 18 | ~EventLoopThread(); 19 | EventLoop* startLoop(); 20 | 21 | private: 22 | void threadFunc(); 23 | 24 | EventLoop* loop_; 25 | bool existing_; 26 | Thread thread_; 27 | MutexLock mutex_; 28 | Condition cond_; 29 | ThreadInitCallback callback_; 30 | }; 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /v1/muduo++/11/EventLoopThread.h: -------------------------------------------------------------------------------- 1 | #ifndef _EVENTLOOPTHREAD_H_ 2 | #define _EVENTLOOPTHREAD_H_ 3 | 4 | #include "Condition.h" 5 | #include "Mutex.h" 6 | #include "Thread.h" 7 | #include "notCopyable.h" 8 | 9 | class EventLoop; 10 | 11 | // one event loop per thread 12 | // start an IO thread then waiting for events coming 13 | class EventLoopThread:public notCopyable{ 14 | public: 15 | typedef std::function ThreadInitCallback; 16 | // EventLoopThread(); 17 | EventLoopThread(const ThreadInitCallback& cb = ThreadInitCallback()); 18 | ~EventLoopThread(); 19 | EventLoop* startLoop(); 20 | 21 | private: 22 | void threadFunc(); 23 | 24 | EventLoop* loop_; 25 | bool existing_; 26 | Thread thread_; 27 | MutexLock mutex_; 28 | Condition cond_; 29 | ThreadInitCallback callback_; 30 | }; 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /v2/src/net/EventLoopThread.h: -------------------------------------------------------------------------------- 1 | #ifndef _EVENTLOOPTHREAD_H_ 2 | #define _EVENTLOOPTHREAD_H_ 3 | 4 | #include "Condition.h" 5 | #include "Mutex.h" 6 | #include "Thread.h" 7 | #include "notCopyable.h" 8 | 9 | class EventLoop; 10 | 11 | // one event loop per thread 12 | // start an IO thread then waiting for events coming 13 | class EventLoopThread:public notCopyable{ 14 | public: 15 | typedef std::function ThreadInitCallback; 16 | // EventLoopThread(); 17 | EventLoopThread(const ThreadInitCallback& cb = ThreadInitCallback()); 18 | ~EventLoopThread(); 19 | EventLoop* startLoop(); 20 | 21 | private: 22 | void threadFunc(); 23 | 24 | EventLoop* loop_; 25 | bool existing_; 26 | Thread thread_; 27 | MutexLock mutex_; 28 | Condition cond_; 29 | ThreadInitCallback callback_; 30 | }; 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /v1/muduo++/10/Thread.h: -------------------------------------------------------------------------------- 1 | #ifndef _THREAD_H 2 | #define _THREAD_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include "notCopyable.h" 8 | #include "CountDownLatch.h" 9 | 10 | class Thread: public notCopyable{ 11 | public: 12 | typedef std::function ThreadFunc; 13 | 14 | explicit Thread(const ThreadFunc& func, const std::string& name = std::string("")); 15 | ~Thread(); 16 | 17 | void start(); 18 | int join(); 19 | 20 | bool started() const{return started_;}; 21 | pid_t tid(); 22 | const std::string& name() const{ 23 | return name_; 24 | } 25 | 26 | private: 27 | bool started_; 28 | bool joined_; 29 | pid_t tid_; 30 | pthread_t pthreadId_; 31 | ThreadFunc func_; 32 | CountDownLatch latch_; 33 | std::string name_; 34 | 35 | }; 36 | 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /v3/src/EventLoopThread.h: -------------------------------------------------------------------------------- 1 | #ifndef _EVENTLOOPTHREAD_H_ 2 | #define _EVENTLOOPTHREAD_H_ 3 | 4 | #include "base/Condition.h" 5 | #include "base/Mutex.h" 6 | #include "base/notCopyable.h" 7 | #include "base/Thread.h" 8 | 9 | class EventLoop; 10 | 11 | // one event loop per thread 12 | // start an IO thread then waiting for events coming 13 | class EventLoopThread:public notCopyable{ 14 | public: 15 | typedef std::function ThreadInitCallback; 16 | EventLoopThread(const ThreadInitCallback& cb = ThreadInitCallback()); 17 | ~EventLoopThread(); 18 | EventLoop* startLoop(); 19 | 20 | private: 21 | void threadFunc(); 22 | 23 | bool existing_; 24 | 25 | EventLoop* loop_; 26 | ThreadInitCallback callback_; 27 | 28 | MutexLock mutex_; 29 | 30 | Condition cond_; 31 | 32 | Thread thread_; 33 | 34 | }; 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /v1/muduo++/11/Thread.h: -------------------------------------------------------------------------------- 1 | #ifndef _THREAD_H 2 | #define _THREAD_H 3 | 4 | #include 5 | #include 6 | #include "notCopyable.h" 7 | #include "CountDownLatch.h" 8 | #include 9 | 10 | class Thread: public notCopyable{ 11 | public: 12 | typedef std::function ThreadFunc; 13 | 14 | explicit Thread(const ThreadFunc& func, const std::string& name = std::string("Anonymous")); 15 | ~Thread(); 16 | 17 | void start(); 18 | int join(); 19 | 20 | bool started() const{return started_;}; 21 | pid_t tid(); 22 | const std::string& name() const{ 23 | return name_; 24 | } 25 | 26 | private: 27 | bool started_; 28 | bool joined_; 29 | pid_t tid_; 30 | pthread_t pthreadId_; 31 | ThreadFunc func_; 32 | CountDownLatch latch_; 33 | std::string name_; 34 | 35 | }; 36 | 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /v2/src/net/Condition.h: -------------------------------------------------------------------------------- 1 | #ifndef _CONDITION_H_ 2 | #define _CONDITION_H_ 3 | 4 | #include "Mutex.h" 5 | #include "notCopyable.h" 6 | #include 7 | 8 | 9 | class Condition: public notCopyable{ 10 | public: 11 | explicit Condition(MutexLock& mutex) 12 | :mutex_(mutex){ 13 | pthread_cond_init(&pcond_, NULL); 14 | } 15 | 16 | ~Condition(){ 17 | pthread_cond_destroy(&pcond_); 18 | } 19 | 20 | void wait(){ 21 | MutexLock::UnassignGuard ug(mutex_); 22 | pthread_cond_wait(&pcond_, mutex_.getMutex()); 23 | } 24 | 25 | bool waitForSeconds(double seconds); 26 | 27 | void notify(){ 28 | pthread_cond_signal(&pcond_); 29 | } 30 | 31 | void notifyAll(){ 32 | pthread_cond_broadcast(&pcond_); 33 | } 34 | 35 | private: 36 | MutexLock& mutex_; 37 | pthread_cond_t pcond_; 38 | }; 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /v2/v2性能评估.md: -------------------------------------------------------------------------------- 1 | ## 评估环境 2 | * OS:Ubuntu 16.10 3 | * 内存:8G 4 | * CPU:I5-6300HQ 5 | 6 | ## 评估工具 7 | * [WebBench](https://github.com/linyacool/WebServer/tree/master/WebBench) 8 | 9 | 10 | ## [评估方法](https://github.com/linyacool/WebServer/blob/master/%E6%B5%8B%E8%AF%95%E5%8F%8A%E6%94%B9%E8%BF%9B.md) 11 | 12 | 13 | ## 评估结果 14 | 测试截图放在最后 15 | 16 | | 服务器 | 短连接QPS | 长连接QPS | 17 | | - | :-: | -: | 18 | | suika | 48845| 153570 | 19 | | muduo | 46374 | 118139 | 20 | 21 | 22 | ## 评估结果截图 23 | * suika短连接 24 | ![shortMyServer](https://github.com/ChyauAng/suika/blob/master/v2/resources/suika.png) 25 | * muduo短连接 26 | ![shortMuduo](https://github.com/ChyauAng/suika/blob/master/v2/resources/muduo.png) 27 | * suika长连接 28 | ![keepMyServer](https://github.com/ChyauAng/suika/blob/master/v2/resources/suikaK.png) 29 | * muduo长连接 30 | ![keepMuduo](https://github.com/ChyauAng/suika/blob/master/v2/resources/muduoK.png) 31 | -------------------------------------------------------------------------------- /v1/muduo++/00/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include"EventLoop.h" 6 | #include"Thread.h" 7 | 8 | 9 | void threadFunc(){ 10 | printf("threadFunc(): pid:%d, tid:%d\n", getpid(), CurrentThread::tid()); 11 | 12 | EventLoop loop; 13 | 14 | loop.loop(); 15 | } 16 | 17 | int main(){ 18 | printf("main(): pid:%d, tid:%d\n", getpid(), CurrentThread::tid()); 19 | 20 | EventLoop loop; 21 | 22 | Thread thread(threadFunc); 23 | thread.start(); 24 | 25 | loop.loop(); 26 | pthread_exit(NULL); 27 | return 0; 28 | } 29 | 30 | 31 | /* 32 | EventLoop* g_loop; 33 | 34 | void threadFunc(){ 35 | g_loop->loop(); 36 | } 37 | 38 | int main(){ 39 | EventLoop loop; 40 | g_loop = &loop; 41 | Thread t(threadFunc); 42 | t.start(); 43 | t.join(); 44 | 45 | return 0; 46 | } 47 | */ 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /v3/src/base/Timestamp.cpp: -------------------------------------------------------------------------------- 1 | #include "Timestamp.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | Timestamp Timestamp::now(){ 9 | struct timeval tv; 10 | gettimeofday(&tv, NULL); 11 | int64_t seconds = tv.tv_sec; 12 | return Timestamp(seconds * kMicroSecondsPerSecond + tv.tv_usec); 13 | } 14 | 15 | std::string Timestamp::toFormattedString() const{ 16 | char buf[64] = {0}; 17 | time_t seconds = static_cast(microSecondsSinceEpoch_ / kMicroSecondsPerSecond); 18 | struct tm tm_time; 19 | gmtime_r(&seconds, &tm_time); 20 | int microseconds = static_cast(microSecondsSinceEpoch_ % kMicroSecondsPerSecond); 21 | snprintf(buf, sizeof(buf), "%4d%02d%02d %02d:%02d:%02d.%06d", tm_time.tm_year + 1900, tm_time.tm_mon + 1, tm_time.tm_mday, tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec, microseconds); 22 | return buf; 23 | 24 | } 25 | -------------------------------------------------------------------------------- /v1/muduo++/10/Timestamp.cpp: -------------------------------------------------------------------------------- 1 | #include "Timestamp.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | Timestamp Timestamp::now(){ 9 | struct timeval tv; 10 | gettimeofday(&tv, NULL); 11 | int64_t seconds = tv.tv_sec; 12 | return Timestamp(seconds * kMicroSecondsPerSecond + tv.tv_usec); 13 | } 14 | 15 | std::string Timestamp::toFormattedString() const{ 16 | char buf[64] = {0}; 17 | time_t seconds = static_cast(microSecondsSinceEpoch_ / kMicroSecondsPerSecond); 18 | struct tm tm_time; 19 | gmtime_r(&seconds, &tm_time); 20 | 21 | int microseconds = static_cast(microSecondsSinceEpoch_ % kMicroSecondsPerSecond); 22 | snprintf(buf, sizeof(buf), "%4d%02d%02d %02d:%02d:%02d.%06d", tm_time.tm_year + 1900, tm_time.tm_mon + 1, tm_time.tm_mday, tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec, microseconds); 23 | 24 | return buf; 25 | 26 | } 27 | -------------------------------------------------------------------------------- /v1/muduo++/11/Timestamp.cpp: -------------------------------------------------------------------------------- 1 | #include "Timestamp.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | Timestamp Timestamp::now(){ 9 | struct timeval tv; 10 | gettimeofday(&tv, NULL); 11 | int64_t seconds = tv.tv_sec; 12 | return Timestamp(seconds * kMicroSecondsPerSecond + tv.tv_usec); 13 | } 14 | 15 | std::string Timestamp::toFormattedString() const{ 16 | char buf[64] = {0}; 17 | time_t seconds = static_cast(microSecondsSinceEpoch_ / kMicroSecondsPerSecond); 18 | struct tm tm_time; 19 | gmtime_r(&seconds, &tm_time); 20 | 21 | int microseconds = static_cast(microSecondsSinceEpoch_ % kMicroSecondsPerSecond); 22 | snprintf(buf, sizeof(buf), "%4d%02d%02d %02d:%02d:%02d.%06d", tm_time.tm_year + 1900, tm_time.tm_mon + 1, tm_time.tm_mday, tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec, microseconds); 23 | 24 | return buf; 25 | 26 | } 27 | -------------------------------------------------------------------------------- /v2/src/net/Timestamp.cpp: -------------------------------------------------------------------------------- 1 | #include "Timestamp.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | Timestamp Timestamp::now(){ 9 | struct timeval tv; 10 | gettimeofday(&tv, NULL); 11 | int64_t seconds = tv.tv_sec; 12 | return Timestamp(seconds * kMicroSecondsPerSecond + tv.tv_usec); 13 | } 14 | 15 | std::string Timestamp::toFormattedString() const{ 16 | char buf[64] = {0}; 17 | time_t seconds = static_cast(microSecondsSinceEpoch_ / kMicroSecondsPerSecond); 18 | struct tm tm_time; 19 | gmtime_r(&seconds, &tm_time); 20 | 21 | int microseconds = static_cast(microSecondsSinceEpoch_ % kMicroSecondsPerSecond); 22 | snprintf(buf, sizeof(buf), "%4d%02d%02d %02d:%02d:%02d.%06d", tm_time.tm_year + 1900, tm_time.tm_mon + 1, tm_time.tm_mday, tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec, microseconds); 23 | 24 | return buf; 25 | 26 | } 27 | -------------------------------------------------------------------------------- /v1/muduo++/00/EventLoop.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include"EventLoop.h" 6 | 7 | using namespace std; 8 | 9 | __thread EventLoop* t_loopInThisThread = 0; 10 | 11 | EventLoop::EventLoop() 12 | :looping_(false), 13 | threadId_(CurrentThread::tid()){ 14 | // do some logging things 15 | if(t_loopInThisThread){ 16 | // do some logging things 17 | } 18 | else{ 19 | t_loopInThisThread = this; 20 | } 21 | } 22 | 23 | EventLoop::~EventLoop(){ 24 | assert(!looping_); 25 | t_loopInThisThread = NULL; 26 | } 27 | 28 | void EventLoop::loop(){ 29 | assert(!looping_); 30 | assertInLoopThread(); 31 | looping_ = true; 32 | 33 | poll(NULL, 0, 5 * 1000); 34 | 35 | // do some loging things 36 | looping_ = false; 37 | } 38 | 39 | void EventLoop::abortNotInLoopThread(){ 40 | // do some logging things 41 | // LOG_FATAL level 42 | } 43 | -------------------------------------------------------------------------------- /v2/src/net/Atomic.h: -------------------------------------------------------------------------------- 1 | #ifndef _ATOMIC_H_ 2 | #define _ATOMIC_H_ 3 | 4 | #include "notCopyable.h" 5 | #include 6 | 7 | template 8 | class AtomicIntegerT: public notCopyable{ 9 | public: 10 | AtomicIntegerT() 11 | :value_(0){ 12 | 13 | } 14 | 15 | T get(){ 16 | // gcc atomic operation 17 | return __sync_val_compare_and_swap(&value_, 0, 0); 18 | } 19 | 20 | T getAndAdd(T x){ 21 | return __sync_fetch_and_add(&value_, x); 22 | } 23 | 24 | T addAndGet(T x){ 25 | return getAndAdd(x) + x; 26 | } 27 | 28 | 29 | T incrementAndGet(){ 30 | return addAndGet(1); 31 | } 32 | 33 | T getAndSet(T newValue){ 34 | return __sync_lock_test_and_set(&value_, newValue); 35 | } 36 | 37 | private: 38 | volatile T value_; 39 | }; 40 | 41 | typedef AtomicIntegerT AtomicInt32; 42 | typedef AtomicIntegerT AtomicInt64; 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /v1/muduo++/01/Makefile: -------------------------------------------------------------------------------- 1 | objects = main.o EventLoop.o Poller.o Channel.o Timestamp.o Thread.o CurrentThread.o 2 | 3 | main: $(objects) 4 | g++ -std=c++11 -pthread -o main $(objects) 5 | 6 | main.o: main.cpp EventLoop.h Thread.h 7 | g++ -std=c++11 -c main.cpp 8 | 9 | EventLoop.o: EventLoop.cpp EventLoop.h notCopyable.h CurrentThread.h Poller.h Channel.h 10 | g++ -std=c++11 -c EventLoop.cpp 11 | 12 | Poller.o: Poller.cpp Poller.h notCopyable.h Timestamp.h EventLoop.h Channel.h 13 | g++ -std=c++11 -c Poller.cpp 14 | 15 | Channel.o: Channel.cpp Channel.h notCopyable.h EventLoop.h 16 | g++ -std=c++11 -c Channel.cpp 17 | 18 | Timestamp.o: Timestamp.cpp Timestamp.h 19 | g++ -std=c++11 -c Timestamp.cpp 20 | 21 | Thread.o: Thread.cpp Thread.h notCopyable.h CurrentThread.h 22 | g++ -std=c++11 -pthread -c Thread.cpp 23 | 24 | CurrentThread.o: CurrentThread.cpp CurrentThread.h 25 | g++ -std=c++11 -c CurrentThread.cpp 26 | 27 | clean: 28 | rm main $(objects) 29 | 30 | -------------------------------------------------------------------------------- /v1/muduo++/05/Atomic.h: -------------------------------------------------------------------------------- 1 | #ifndef _ATOMIC_H_ 2 | #define _ATOMIC_H_ 3 | 4 | #include "notCopyable.h" 5 | #include 6 | 7 | template 8 | class AtomicIntegerT: public notCopyable{ 9 | public: 10 | AtomicIntegerT() 11 | :value_(0){ 12 | 13 | } 14 | 15 | T get(){ 16 | // gcc atomic operation 17 | return __sync_val_compare_and_swap(&value_, 0, 0); 18 | } 19 | 20 | T getAndAdd(T x){ 21 | return __sync_fetch_and_add(&value_, x); 22 | } 23 | 24 | T addAndGet(T x){ 25 | return getAndAdd(x) + x; 26 | } 27 | 28 | 29 | T incrementAndGet(){ 30 | return addAndGet(1); 31 | } 32 | 33 | T getAndSet(T newValue){ 34 | return __sync_lock_test_and_set(&value_, newValue); 35 | } 36 | 37 | private: 38 | volatile T value_; 39 | }; 40 | 41 | typedef AtomicIntegerT AtomicInt32; 42 | typedef AtomicIntegerT AtomicInt64; 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /v1/muduo++/06/Atomic.h: -------------------------------------------------------------------------------- 1 | #ifndef _ATOMIC_H_ 2 | #define _ATOMIC_H_ 3 | 4 | #include "notCopyable.h" 5 | #include 6 | 7 | template 8 | class AtomicIntegerT: public notCopyable{ 9 | public: 10 | AtomicIntegerT() 11 | :value_(0){ 12 | 13 | } 14 | 15 | T get(){ 16 | // gcc atomic operation 17 | return __sync_val_compare_and_swap(&value_, 0, 0); 18 | } 19 | 20 | T getAndAdd(T x){ 21 | return __sync_fetch_and_add(&value_, x); 22 | } 23 | 24 | T addAndGet(T x){ 25 | return getAndAdd(x) + x; 26 | } 27 | 28 | 29 | T incrementAndGet(){ 30 | return addAndGet(1); 31 | } 32 | 33 | T getAndSet(T newValue){ 34 | return __sync_lock_test_and_set(&value_, newValue); 35 | } 36 | 37 | private: 38 | volatile T value_; 39 | }; 40 | 41 | typedef AtomicIntegerT AtomicInt32; 42 | typedef AtomicIntegerT AtomicInt64; 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /v1/muduo++/07/Atomic.h: -------------------------------------------------------------------------------- 1 | #ifndef _ATOMIC_H_ 2 | #define _ATOMIC_H_ 3 | 4 | #include "notCopyable.h" 5 | #include 6 | 7 | template 8 | class AtomicIntegerT: public notCopyable{ 9 | public: 10 | AtomicIntegerT() 11 | :value_(0){ 12 | 13 | } 14 | 15 | T get(){ 16 | // gcc atomic operation 17 | return __sync_val_compare_and_swap(&value_, 0, 0); 18 | } 19 | 20 | T getAndAdd(T x){ 21 | return __sync_fetch_and_add(&value_, x); 22 | } 23 | 24 | T addAndGet(T x){ 25 | return getAndAdd(x) + x; 26 | } 27 | 28 | 29 | T incrementAndGet(){ 30 | return addAndGet(1); 31 | } 32 | 33 | T getAndSet(T newValue){ 34 | return __sync_lock_test_and_set(&value_, newValue); 35 | } 36 | 37 | private: 38 | volatile T value_; 39 | }; 40 | 41 | typedef AtomicIntegerT AtomicInt32; 42 | typedef AtomicIntegerT AtomicInt64; 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /v1/muduo++/08/Atomic.h: -------------------------------------------------------------------------------- 1 | #ifndef _ATOMIC_H_ 2 | #define _ATOMIC_H_ 3 | 4 | #include "notCopyable.h" 5 | #include 6 | 7 | template 8 | class AtomicIntegerT: public notCopyable{ 9 | public: 10 | AtomicIntegerT() 11 | :value_(0){ 12 | 13 | } 14 | 15 | T get(){ 16 | // gcc atomic operation 17 | return __sync_val_compare_and_swap(&value_, 0, 0); 18 | } 19 | 20 | T getAndAdd(T x){ 21 | return __sync_fetch_and_add(&value_, x); 22 | } 23 | 24 | T addAndGet(T x){ 25 | return getAndAdd(x) + x; 26 | } 27 | 28 | 29 | T incrementAndGet(){ 30 | return addAndGet(1); 31 | } 32 | 33 | T getAndSet(T newValue){ 34 | return __sync_lock_test_and_set(&value_, newValue); 35 | } 36 | 37 | private: 38 | volatile T value_; 39 | }; 40 | 41 | typedef AtomicIntegerT AtomicInt32; 42 | typedef AtomicIntegerT AtomicInt64; 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /v1/muduo++/09/Atomic.h: -------------------------------------------------------------------------------- 1 | #ifndef _ATOMIC_H_ 2 | #define _ATOMIC_H_ 3 | 4 | #include "notCopyable.h" 5 | #include 6 | 7 | template 8 | class AtomicIntegerT: public notCopyable{ 9 | public: 10 | AtomicIntegerT() 11 | :value_(0){ 12 | 13 | } 14 | 15 | T get(){ 16 | // gcc atomic operation 17 | return __sync_val_compare_and_swap(&value_, 0, 0); 18 | } 19 | 20 | T getAndAdd(T x){ 21 | return __sync_fetch_and_add(&value_, x); 22 | } 23 | 24 | T addAndGet(T x){ 25 | return getAndAdd(x) + x; 26 | } 27 | 28 | 29 | T incrementAndGet(){ 30 | return addAndGet(1); 31 | } 32 | 33 | T getAndSet(T newValue){ 34 | return __sync_lock_test_and_set(&value_, newValue); 35 | } 36 | 37 | private: 38 | volatile T value_; 39 | }; 40 | 41 | typedef AtomicIntegerT AtomicInt32; 42 | typedef AtomicIntegerT AtomicInt64; 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /v1/muduo++/10/Atomic.h: -------------------------------------------------------------------------------- 1 | #ifndef _ATOMIC_H_ 2 | #define _ATOMIC_H_ 3 | 4 | #include "notCopyable.h" 5 | #include 6 | 7 | template 8 | class AtomicIntegerT: public notCopyable{ 9 | public: 10 | AtomicIntegerT() 11 | :value_(0){ 12 | 13 | } 14 | 15 | T get(){ 16 | // gcc atomic operation 17 | return __sync_val_compare_and_swap(&value_, 0, 0); 18 | } 19 | 20 | T getAndAdd(T x){ 21 | return __sync_fetch_and_add(&value_, x); 22 | } 23 | 24 | T addAndGet(T x){ 25 | return getAndAdd(x) + x; 26 | } 27 | 28 | 29 | T incrementAndGet(){ 30 | return addAndGet(1); 31 | } 32 | 33 | T getAndSet(T newValue){ 34 | return __sync_lock_test_and_set(&value_, newValue); 35 | } 36 | 37 | private: 38 | volatile T value_; 39 | }; 40 | 41 | typedef AtomicIntegerT AtomicInt32; 42 | typedef AtomicIntegerT AtomicInt64; 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /v1/muduo++/11/Atomic.h: -------------------------------------------------------------------------------- 1 | #ifndef _ATOMIC_H_ 2 | #define _ATOMIC_H_ 3 | 4 | #include "notCopyable.h" 5 | #include 6 | 7 | template 8 | class AtomicIntegerT: public notCopyable{ 9 | public: 10 | AtomicIntegerT() 11 | :value_(0){ 12 | 13 | } 14 | 15 | T get(){ 16 | // gcc atomic operation 17 | return __sync_val_compare_and_swap(&value_, 0, 0); 18 | } 19 | 20 | T getAndAdd(T x){ 21 | return __sync_fetch_and_add(&value_, x); 22 | } 23 | 24 | T addAndGet(T x){ 25 | return getAndAdd(x) + x; 26 | } 27 | 28 | 29 | T incrementAndGet(){ 30 | return addAndGet(1); 31 | } 32 | 33 | T getAndSet(T newValue){ 34 | return __sync_lock_test_and_set(&value_, newValue); 35 | } 36 | 37 | private: 38 | volatile T value_; 39 | }; 40 | 41 | typedef AtomicIntegerT AtomicInt32; 42 | typedef AtomicIntegerT AtomicInt64; 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /v2/src/net/Thread.h: -------------------------------------------------------------------------------- 1 | #ifndef _THREAD_H 2 | #define _THREAD_H 3 | 4 | #include 5 | #include 6 | #include "notCopyable.h" 7 | #include "CountDownLatch.h" 8 | #include 9 | 10 | class Thread: public notCopyable{ 11 | public: 12 | typedef std::function ThreadFunc; 13 | 14 | // explicit Thread(const ThreadFunc& func, const std::string& name = std::string("Anonymous")); 15 | explicit Thread(ThreadFunc&& func, const std::string& name = std::string("Anonymous")); 16 | ~Thread(); 17 | 18 | void start(); 19 | int join(); 20 | 21 | bool started() const{return started_;}; 22 | pid_t tid(); 23 | const std::string& name() const{ 24 | return name_; 25 | } 26 | 27 | private: 28 | bool started_; 29 | bool joined_; 30 | pid_t tid_; 31 | pthread_t pthreadId_; 32 | ThreadFunc func_; 33 | CountDownLatch latch_; 34 | std::string name_; 35 | 36 | }; 37 | 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /v1/muduo++/01/Poller.h: -------------------------------------------------------------------------------- 1 | #ifndef _POLLER_H_ 2 | #define _POLLER_H_ 3 | 4 | #include "notCopyable.h" 5 | #include "Timestamp.h" 6 | #include "EventLoop.h" 7 | #include 8 | #include 9 | 10 | struct pollfd; 11 | 12 | class Channel; 13 | 14 | class Poller: public notCopyable{ 15 | public: 16 | typedef std::vector ChannelList; 17 | 18 | Poller(EventLoop* loop); 19 | ~Poller(); 20 | 21 | Timestamp polll(int timeoutMs, ChannelList* activeChannels); 22 | 23 | void updateChannel(Channel* channel); 24 | 25 | void assertInLoopThread(){ 26 | ownerLoop_->assertInLoopThread(); 27 | } 28 | 29 | private: 30 | void fillActiveChannels(int numEvents, ChannelList* activeChannel) const; 31 | 32 | typedef std::vector PollFdList; 33 | typedef std::map ChannelMap; 34 | 35 | EventLoop* ownerLoop_; 36 | PollFdList pollfds_; 37 | ChannelMap channels_; 38 | 39 | }; 40 | 41 | #endif 42 | 43 | -------------------------------------------------------------------------------- /v1/muduo++/04/Acceptor.h: -------------------------------------------------------------------------------- 1 | #ifndef _ACCEPTOR_H_ 2 | #define _ACEPTOR_H_ 3 | 4 | #include 5 | 6 | #include "notCopyable.h" 7 | #include "Channel.h" 8 | #include "Socket.h" 9 | 10 | class EventLoop; 11 | class InetAddress; 12 | 13 | class Acceptor: public notCopyable{ 14 | public: 15 | typedef std::function NewConnectionCallback; 16 | 17 | Acceptor(EventLoop* loop, const InetAddress& listenAddr, bool reuseport); 18 | ~Acceptor(); 19 | 20 | void setNewConnectionCallback(const NewConnectionCallback& cb){ 21 | newConnectionCallback_ = cb; 22 | } 23 | 24 | bool listenning() const{ 25 | return listenning_; 26 | } 27 | 28 | void listen(); 29 | 30 | private: 31 | void handleRead(); 32 | 33 | EventLoop* loop_; 34 | Socket acceptSocket_; 35 | Channel acceptChannel_; 36 | NewConnectionCallback newConnectionCallback_; 37 | bool listenning_; 38 | int idleFd_; 39 | }; 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /v1/muduo++/05/Acceptor.h: -------------------------------------------------------------------------------- 1 | #ifndef _ACCEPTOR_H_ 2 | #define _ACEPTOR_H_ 3 | 4 | #include 5 | 6 | #include "notCopyable.h" 7 | #include "Channel.h" 8 | #include "Socket.h" 9 | 10 | class EventLoop; 11 | class InetAddress; 12 | 13 | class Acceptor: public notCopyable{ 14 | public: 15 | typedef std::function NewConnectionCallback; 16 | 17 | Acceptor(EventLoop* loop, const InetAddress& listenAddr, bool reuseport); 18 | ~Acceptor(); 19 | 20 | void setNewConnectionCallback(const NewConnectionCallback& cb){ 21 | newConnectionCallback_ = cb; 22 | } 23 | 24 | bool listenning() const{ 25 | return listenning_; 26 | } 27 | 28 | void listen(); 29 | 30 | private: 31 | void handleRead(); 32 | 33 | EventLoop* loop_; 34 | Socket acceptSocket_; 35 | Channel acceptChannel_; 36 | NewConnectionCallback newConnectionCallback_; 37 | bool listenning_; 38 | int idleFd_; 39 | }; 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /v1/muduo++/06/Acceptor.h: -------------------------------------------------------------------------------- 1 | #ifndef _ACCEPTOR_H_ 2 | #define _ACEPTOR_H_ 3 | 4 | #include 5 | 6 | #include "notCopyable.h" 7 | #include "Channel.h" 8 | #include "Socket.h" 9 | 10 | class EventLoop; 11 | class InetAddress; 12 | 13 | class Acceptor: public notCopyable{ 14 | public: 15 | typedef std::function NewConnectionCallback; 16 | 17 | Acceptor(EventLoop* loop, const InetAddress& listenAddr, bool reuseport); 18 | ~Acceptor(); 19 | 20 | void setNewConnectionCallback(const NewConnectionCallback& cb){ 21 | newConnectionCallback_ = cb; 22 | } 23 | 24 | bool listenning() const{ 25 | return listenning_; 26 | } 27 | 28 | void listen(); 29 | 30 | private: 31 | void handleRead(); 32 | 33 | EventLoop* loop_; 34 | Socket acceptSocket_; 35 | Channel acceptChannel_; 36 | NewConnectionCallback newConnectionCallback_; 37 | bool listenning_; 38 | int idleFd_; 39 | }; 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /v1/muduo++/07/Acceptor.h: -------------------------------------------------------------------------------- 1 | #ifndef _ACCEPTOR_H_ 2 | #define _ACEPTOR_H_ 3 | 4 | #include 5 | 6 | #include "notCopyable.h" 7 | #include "Channel.h" 8 | #include "Socket.h" 9 | 10 | class EventLoop; 11 | class InetAddress; 12 | 13 | class Acceptor: public notCopyable{ 14 | public: 15 | typedef std::function NewConnectionCallback; 16 | 17 | Acceptor(EventLoop* loop, const InetAddress& listenAddr, bool reuseport); 18 | ~Acceptor(); 19 | 20 | void setNewConnectionCallback(const NewConnectionCallback& cb){ 21 | newConnectionCallback_ = cb; 22 | } 23 | 24 | bool listenning() const{ 25 | return listenning_; 26 | } 27 | 28 | void listen(); 29 | 30 | private: 31 | void handleRead(); 32 | 33 | EventLoop* loop_; 34 | Socket acceptSocket_; 35 | Channel acceptChannel_; 36 | NewConnectionCallback newConnectionCallback_; 37 | bool listenning_; 38 | int idleFd_; 39 | }; 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /v1/muduo++/08/Acceptor.h: -------------------------------------------------------------------------------- 1 | #ifndef _ACCEPTOR_H_ 2 | #define _ACEPTOR_H_ 3 | 4 | #include 5 | 6 | #include "notCopyable.h" 7 | #include "Channel.h" 8 | #include "Socket.h" 9 | 10 | class EventLoop; 11 | class InetAddress; 12 | 13 | class Acceptor: public notCopyable{ 14 | public: 15 | typedef std::function NewConnectionCallback; 16 | 17 | Acceptor(EventLoop* loop, const InetAddress& listenAddr, bool reuseport); 18 | ~Acceptor(); 19 | 20 | void setNewConnectionCallback(const NewConnectionCallback& cb){ 21 | newConnectionCallback_ = cb; 22 | } 23 | 24 | bool listenning() const{ 25 | return listenning_; 26 | } 27 | 28 | void listen(); 29 | 30 | private: 31 | void handleRead(); 32 | 33 | EventLoop* loop_; 34 | Socket acceptSocket_; 35 | Channel acceptChannel_; 36 | NewConnectionCallback newConnectionCallback_; 37 | bool listenning_; 38 | int idleFd_; 39 | }; 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /v1/muduo++/09/Acceptor.h: -------------------------------------------------------------------------------- 1 | #ifndef _ACCEPTOR_H_ 2 | #define _ACEPTOR_H_ 3 | 4 | #include 5 | 6 | #include "notCopyable.h" 7 | #include "Channel.h" 8 | #include "Socket.h" 9 | 10 | class EventLoop; 11 | class InetAddress; 12 | 13 | class Acceptor: public notCopyable{ 14 | public: 15 | typedef std::function NewConnectionCallback; 16 | 17 | Acceptor(EventLoop* loop, const InetAddress& listenAddr, bool reuseport); 18 | ~Acceptor(); 19 | 20 | void setNewConnectionCallback(const NewConnectionCallback& cb){ 21 | newConnectionCallback_ = cb; 22 | } 23 | 24 | bool listenning() const{ 25 | return listenning_; 26 | } 27 | 28 | void listen(); 29 | 30 | private: 31 | void handleRead(); 32 | 33 | EventLoop* loop_; 34 | Socket acceptSocket_; 35 | Channel acceptChannel_; 36 | NewConnectionCallback newConnectionCallback_; 37 | bool listenning_; 38 | int idleFd_; 39 | }; 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /v1/muduo++/10/Acceptor.h: -------------------------------------------------------------------------------- 1 | #ifndef _ACCEPTOR_H_ 2 | #define _ACEPTOR_H_ 3 | 4 | #include 5 | 6 | #include "notCopyable.h" 7 | #include "Channel.h" 8 | #include "Socket.h" 9 | 10 | class EventLoop; 11 | class InetAddress; 12 | 13 | class Acceptor: public notCopyable{ 14 | public: 15 | typedef std::function NewConnectionCallback; 16 | 17 | Acceptor(EventLoop* loop, const InetAddress& listenAddr, bool reuseport); 18 | ~Acceptor(); 19 | 20 | void setNewConnectionCallback(const NewConnectionCallback& cb){ 21 | newConnectionCallback_ = cb; 22 | } 23 | 24 | bool listenning() const{ 25 | return listenning_; 26 | } 27 | 28 | void listen(); 29 | 30 | private: 31 | void handleRead(); 32 | 33 | EventLoop* loop_; 34 | Socket acceptSocket_; 35 | Channel acceptChannel_; 36 | NewConnectionCallback newConnectionCallback_; 37 | bool listenning_; 38 | int idleFd_; 39 | }; 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /v1/muduo++/11/Acceptor.h: -------------------------------------------------------------------------------- 1 | #ifndef _ACCEPTOR_H_ 2 | #define _ACEPTOR_H_ 3 | 4 | #include 5 | 6 | #include "notCopyable.h" 7 | #include "Channel.h" 8 | #include "Socket.h" 9 | 10 | class EventLoop; 11 | class InetAddress; 12 | 13 | class Acceptor: public notCopyable{ 14 | public: 15 | typedef std::function NewConnectionCallback; 16 | 17 | Acceptor(EventLoop* loop, const InetAddress& listenAddr, bool reuseport); 18 | ~Acceptor(); 19 | 20 | void setNewConnectionCallback(const NewConnectionCallback& cb){ 21 | newConnectionCallback_ = cb; 22 | } 23 | 24 | bool listenning() const{ 25 | return listenning_; 26 | } 27 | 28 | void listen(); 29 | 30 | private: 31 | void handleRead(); 32 | 33 | EventLoop* loop_; 34 | Socket acceptSocket_; 35 | Channel acceptChannel_; 36 | NewConnectionCallback newConnectionCallback_; 37 | bool listenning_; 38 | int idleFd_; 39 | }; 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /v2/src/net/Acceptor.h: -------------------------------------------------------------------------------- 1 | #ifndef _ACCEPTOR_H_ 2 | #define _ACEPTOR_H_ 3 | 4 | #include 5 | 6 | #include "notCopyable.h" 7 | #include "Channel.h" 8 | #include "Socket.h" 9 | 10 | class EventLoop; 11 | class InetAddress; 12 | 13 | class Acceptor: public notCopyable{ 14 | public: 15 | typedef std::function NewConnectionCallback; 16 | 17 | Acceptor(EventLoop* loop, const InetAddress& listenAddr, bool reuseport); 18 | ~Acceptor(); 19 | 20 | void setNewConnectionCallback(const NewConnectionCallback& cb){ 21 | newConnectionCallback_ = cb; 22 | } 23 | 24 | bool listenning() const{ 25 | return listenning_; 26 | } 27 | 28 | void listen(); 29 | 30 | private: 31 | void handleRead(); 32 | 33 | EventLoop* loop_; 34 | Socket acceptSocket_; 35 | Channel acceptChannel_; 36 | NewConnectionCallback newConnectionCallback_; 37 | bool listenning_; 38 | int idleFd_; 39 | }; 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /v2/src/net/CurrentThread.cpp: -------------------------------------------------------------------------------- 1 | #include "CurrentThread.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | namespace CurrentThread{ 10 | __thread int t_cachedTid = 0; 11 | __thread char t_tidString[32]; 12 | __thread int t_tidStringLength = 6; 13 | __thread int t_cachedPid = 0; 14 | __thread const char* t_threadName = "Anonymous"; 15 | 16 | pid_t gettid(){ 17 | return static_cast(syscall(SYS_gettid)); 18 | } 19 | 20 | void cachedTid(){ 21 | if(t_cachedTid == 0){ 22 | t_cachedTid = gettid(); 23 | t_tidStringLength = snprintf(t_tidString, sizeof t_tidString, "%5d ", t_cachedTid); 24 | } 25 | } 26 | 27 | pid_t getpid(){ 28 | return static_cast(syscall(SYS_getpid)); 29 | } 30 | 31 | void cachedPid(){ 32 | if(t_cachedPid == 0){ 33 | t_cachedPid = getpid(); 34 | } 35 | } 36 | 37 | 38 | } 39 | -------------------------------------------------------------------------------- /v1/muduo++/01/Channel.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "EventLoop.h" 3 | #include "Channel.h" 4 | 5 | const int Channel::kNoneEvent = 0; 6 | const int Channel::kReadEvent = POLLIN |POLLPRI; 7 | const int Channel::kWriteEvent = POLLOUT; 8 | 9 | Channel::Channel(EventLoop* loop, int fd) 10 | :loop_(loop), 11 | fd_(fd), 12 | events_(0), 13 | revents_(0), 14 | index_(-1){ 15 | 16 | } 17 | void Channel::update(){ 18 | loop_->updateChannel(this); 19 | } 20 | 21 | void Channel::handleEvent(){ 22 | if(revents_ & POLLNVAL){ 23 | // LOG_WARN 24 | } 25 | 26 | if(revents_ & (POLLERR | POLLNVAL)){ 27 | if(errorCallback_){ 28 | errorCallback_(); 29 | } 30 | } 31 | 32 | if(revents_ & (POLLIN | POLLPRI | POLLRDHUP)){ 33 | if(readCallback_){ 34 | readCallback_(); 35 | } 36 | } 37 | 38 | if(revents_ & (POLLOUT)){ 39 | if(writeCallback_){ 40 | writeCallback_(); 41 | } 42 | } 43 | } 44 | 45 | 46 | -------------------------------------------------------------------------------- /v1/muduo++/06/Poller.h: -------------------------------------------------------------------------------- 1 | #ifndef _POLLER_H_ 2 | #define _POLLER_H_ 3 | 4 | #include "notCopyable.h" 5 | #include "Timestamp.h" 6 | #include "EventLoop.h" 7 | #include 8 | #include 9 | 10 | struct pollfd; 11 | 12 | class Channel; 13 | 14 | class Poller: public notCopyable{ 15 | public: 16 | typedef std::vector ChannelList; 17 | 18 | Poller(EventLoop* loop); 19 | ~Poller(); 20 | 21 | Timestamp poll(int timeoutMs, ChannelList* activeChannels); 22 | 23 | void updateChannel(Channel* channel); 24 | void removeChannel(Channel* channel); 25 | 26 | void assertInLoopThread(){ 27 | ownerLoop_->assertInLoopThread(); 28 | } 29 | 30 | private: 31 | void fillActiveChannels(int numEvents, ChannelList* activeChannel) const; 32 | 33 | typedef std::vector PollFdList; 34 | typedef std::map ChannelMap; 35 | 36 | EventLoop* ownerLoop_; 37 | PollFdList pollfds_; 38 | ChannelMap channels_; 39 | 40 | }; 41 | 42 | #endif 43 | 44 | -------------------------------------------------------------------------------- /v1/muduo++/07/Poller.h: -------------------------------------------------------------------------------- 1 | #ifndef _POLLER_H_ 2 | #define _POLLER_H_ 3 | 4 | #include "notCopyable.h" 5 | #include "Timestamp.h" 6 | #include "EventLoop.h" 7 | #include 8 | #include 9 | 10 | struct pollfd; 11 | 12 | class Channel; 13 | 14 | class Poller: public notCopyable{ 15 | public: 16 | typedef std::vector ChannelList; 17 | 18 | Poller(EventLoop* loop); 19 | ~Poller(); 20 | 21 | Timestamp poll(int timeoutMs, ChannelList* activeChannels); 22 | 23 | void updateChannel(Channel* channel); 24 | void removeChannel(Channel* channel); 25 | 26 | void assertInLoopThread(){ 27 | ownerLoop_->assertInLoopThread(); 28 | } 29 | 30 | private: 31 | void fillActiveChannels(int numEvents, ChannelList* activeChannel) const; 32 | 33 | typedef std::vector PollFdList; 34 | typedef std::map ChannelMap; 35 | 36 | EventLoop* ownerLoop_; 37 | PollFdList pollfds_; 38 | ChannelMap channels_; 39 | 40 | }; 41 | 42 | #endif 43 | 44 | -------------------------------------------------------------------------------- /v1/muduo++/08/Poller.h: -------------------------------------------------------------------------------- 1 | #ifndef _POLLER_H_ 2 | #define _POLLER_H_ 3 | 4 | #include "notCopyable.h" 5 | #include "Timestamp.h" 6 | #include "EventLoop.h" 7 | #include 8 | #include 9 | 10 | struct pollfd; 11 | 12 | class Channel; 13 | 14 | class Poller: public notCopyable{ 15 | public: 16 | typedef std::vector ChannelList; 17 | 18 | Poller(EventLoop* loop); 19 | ~Poller(); 20 | 21 | Timestamp poll(int timeoutMs, ChannelList* activeChannels); 22 | 23 | void updateChannel(Channel* channel); 24 | void removeChannel(Channel* channel); 25 | 26 | void assertInLoopThread(){ 27 | ownerLoop_->assertInLoopThread(); 28 | } 29 | 30 | private: 31 | void fillActiveChannels(int numEvents, ChannelList* activeChannel) const; 32 | 33 | typedef std::vector PollFdList; 34 | typedef std::map ChannelMap; 35 | 36 | EventLoop* ownerLoop_; 37 | PollFdList pollfds_; 38 | ChannelMap channels_; 39 | 40 | }; 41 | 42 | #endif 43 | 44 | -------------------------------------------------------------------------------- /v1/muduo++/10/CurrentThread.cpp: -------------------------------------------------------------------------------- 1 | #include "CurrentThread.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | namespace CurrentThread{ 10 | __thread int t_cachedTid = 0; 11 | __thread char t_tidString[32]; 12 | __thread int t_tidStringLength = 6; 13 | __thread int t_cachedPid = 0; 14 | __thread const char* t_threadName = "Anonymous"; 15 | 16 | pid_t gettid(){ 17 | return static_cast(syscall(SYS_gettid)); 18 | } 19 | 20 | void cachedTid(){ 21 | if(t_cachedTid == 0){ 22 | t_cachedTid = gettid(); 23 | t_tidStringLength = snprintf(t_tidString, sizeof t_tidString, "%5d ", t_cachedTid); 24 | } 25 | } 26 | 27 | pid_t getpid(){ 28 | return static_cast(syscall(SYS_getpid)); 29 | } 30 | 31 | void cachedPid(){ 32 | if(t_cachedPid == 0){ 33 | t_cachedPid = getpid(); 34 | } 35 | } 36 | 37 | 38 | } 39 | -------------------------------------------------------------------------------- /v1/muduo++/11/CurrentThread.cpp: -------------------------------------------------------------------------------- 1 | #include "CurrentThread.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | namespace CurrentThread{ 10 | __thread int t_cachedTid = 0; 11 | __thread char t_tidString[32]; 12 | __thread int t_tidStringLength = 6; 13 | __thread int t_cachedPid = 0; 14 | __thread const char* t_threadName = "Anonymous"; 15 | 16 | pid_t gettid(){ 17 | return static_cast(syscall(SYS_gettid)); 18 | } 19 | 20 | void cachedTid(){ 21 | if(t_cachedTid == 0){ 22 | t_cachedTid = gettid(); 23 | t_tidStringLength = snprintf(t_tidString, sizeof t_tidString, "%5d ", t_cachedTid); 24 | } 25 | } 26 | 27 | pid_t getpid(){ 28 | return static_cast(syscall(SYS_getpid)); 29 | } 30 | 31 | void cachedPid(){ 32 | if(t_cachedPid == 0){ 33 | t_cachedPid = getpid(); 34 | } 35 | } 36 | 37 | 38 | } 39 | -------------------------------------------------------------------------------- /v3/src/base/CurrentThread.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "CurrentThread.h" 8 | 9 | namespace CurrentThread{ 10 | __thread int t_cachedTid = 0; 11 | __thread char t_tidString[32]; 12 | __thread int t_tidStringLength = 6; 13 | __thread int t_cachedPid = 0; 14 | __thread const char* t_threadName = "Anonymous"; 15 | 16 | pid_t gettid(){ 17 | return static_cast(syscall(SYS_gettid)); 18 | } 19 | 20 | void cachedTid(){ 21 | if(t_cachedTid == 0){ 22 | t_cachedTid = gettid(); 23 | t_tidStringLength = snprintf(t_tidString, sizeof t_tidString, "%5d ", t_cachedTid); 24 | } 25 | } 26 | 27 | pid_t getpid(){ 28 | return static_cast(syscall(SYS_getpid)); 29 | } 30 | 31 | void cachedPid(){ 32 | if(t_cachedPid == 0){ 33 | t_cachedPid = getpid(); 34 | } 35 | } 36 | 37 | 38 | } 39 | -------------------------------------------------------------------------------- /v1/muduo++/02/Poller.h: -------------------------------------------------------------------------------- 1 | #ifndef _POLLER_H_ 2 | #define _POLLER_H_ 3 | 4 | #include "notCopyable.h" 5 | #include "Timestamp.h" 6 | #include "EventLoop.h" 7 | #include 8 | #include 9 | 10 | struct pollfd; 11 | 12 | class Channel; 13 | 14 | class Poller: public notCopyable{ 15 | public: 16 | typedef std::vector ChannelList; 17 | 18 | Poller(EventLoop* loop); 19 | ~Poller(); 20 | 21 | Timestamp polll(int timeoutMs, ChannelList* activeChannels); 22 | 23 | void updateChannel(Channel* channel); 24 | void removeChannel(Channel* channel); 25 | 26 | void assertInLoopThread(){ 27 | ownerLoop_->assertInLoopThread(); 28 | } 29 | 30 | private: 31 | void fillActiveChannels(int numEvents, ChannelList* activeChannel) const; 32 | 33 | typedef std::vector PollFdList; 34 | typedef std::map ChannelMap; 35 | 36 | EventLoop* ownerLoop_; 37 | PollFdList pollfds_; 38 | ChannelMap channels_; 39 | 40 | }; 41 | 42 | #endif 43 | 44 | -------------------------------------------------------------------------------- /v1/muduo++/03/Poller.h: -------------------------------------------------------------------------------- 1 | #ifndef _POLLER_H_ 2 | #define _POLLER_H_ 3 | 4 | #include "notCopyable.h" 5 | #include "Timestamp.h" 6 | #include "EventLoop.h" 7 | #include 8 | #include 9 | 10 | struct pollfd; 11 | 12 | class Channel; 13 | 14 | class Poller: public notCopyable{ 15 | public: 16 | typedef std::vector ChannelList; 17 | 18 | Poller(EventLoop* loop); 19 | ~Poller(); 20 | 21 | Timestamp polll(int timeoutMs, ChannelList* activeChannels); 22 | 23 | void updateChannel(Channel* channel); 24 | void removeChannel(Channel* channel); 25 | 26 | void assertInLoopThread(){ 27 | ownerLoop_->assertInLoopThread(); 28 | } 29 | 30 | private: 31 | void fillActiveChannels(int numEvents, ChannelList* activeChannel) const; 32 | 33 | typedef std::vector PollFdList; 34 | typedef std::map ChannelMap; 35 | 36 | EventLoop* ownerLoop_; 37 | PollFdList pollfds_; 38 | ChannelMap channels_; 39 | 40 | }; 41 | 42 | #endif 43 | 44 | -------------------------------------------------------------------------------- /v1/muduo++/04/Poller.h: -------------------------------------------------------------------------------- 1 | #ifndef _POLLER_H_ 2 | #define _POLLER_H_ 3 | 4 | #include "notCopyable.h" 5 | #include "Timestamp.h" 6 | #include "EventLoop.h" 7 | #include 8 | #include 9 | 10 | struct pollfd; 11 | 12 | class Channel; 13 | 14 | class Poller: public notCopyable{ 15 | public: 16 | typedef std::vector ChannelList; 17 | 18 | Poller(EventLoop* loop); 19 | ~Poller(); 20 | 21 | Timestamp polll(int timeoutMs, ChannelList* activeChannels); 22 | 23 | void updateChannel(Channel* channel); 24 | void removeChannel(Channel* channel); 25 | 26 | void assertInLoopThread(){ 27 | ownerLoop_->assertInLoopThread(); 28 | } 29 | 30 | private: 31 | void fillActiveChannels(int numEvents, ChannelList* activeChannel) const; 32 | 33 | typedef std::vector PollFdList; 34 | typedef std::map ChannelMap; 35 | 36 | EventLoop* ownerLoop_; 37 | PollFdList pollfds_; 38 | ChannelMap channels_; 39 | 40 | }; 41 | 42 | #endif 43 | 44 | -------------------------------------------------------------------------------- /v1/muduo++/05/Poller.h: -------------------------------------------------------------------------------- 1 | #ifndef _POLLER_H_ 2 | #define _POLLER_H_ 3 | 4 | #include "notCopyable.h" 5 | #include "Timestamp.h" 6 | #include "EventLoop.h" 7 | #include 8 | #include 9 | 10 | struct pollfd; 11 | 12 | class Channel; 13 | 14 | class Poller: public notCopyable{ 15 | public: 16 | typedef std::vector ChannelList; 17 | 18 | Poller(EventLoop* loop); 19 | ~Poller(); 20 | 21 | Timestamp polll(int timeoutMs, ChannelList* activeChannels); 22 | 23 | void updateChannel(Channel* channel); 24 | void removeChannel(Channel* channel); 25 | 26 | void assertInLoopThread(){ 27 | ownerLoop_->assertInLoopThread(); 28 | } 29 | 30 | private: 31 | void fillActiveChannels(int numEvents, ChannelList* activeChannel) const; 32 | 33 | typedef std::vector PollFdList; 34 | typedef std::map ChannelMap; 35 | 36 | EventLoop* ownerLoop_; 37 | PollFdList pollfds_; 38 | ChannelMap channels_; 39 | 40 | }; 41 | 42 | #endif 43 | 44 | -------------------------------------------------------------------------------- /v1/muduo++/09/Poller.h: -------------------------------------------------------------------------------- 1 | #ifndef _POLLER_H_ 2 | #define _POLLER_H_ 3 | 4 | #include "notCopyable.h" 5 | #include "Timestamp.h" 6 | #include "EventLoop.h" 7 | #include 8 | #include 9 | 10 | struct pollfd; 11 | 12 | class Channel; 13 | 14 | // fd关注的事件类型注册于Channel中 15 | class Poller: public notCopyable{ 16 | public: 17 | typedef std::vector ChannelList; 18 | 19 | Poller(EventLoop* loop); 20 | ~Poller(); 21 | 22 | Timestamp poll(int timeoutMs, ChannelList* activeChannels); 23 | 24 | void updateChannel(Channel* channel); 25 | void removeChannel(Channel* channel); 26 | 27 | void assertInLoopThread(){ 28 | ownerLoop_->assertInLoopThread(); 29 | } 30 | 31 | private: 32 | void fillActiveChannels(int numEvents, ChannelList* activeChannel) const; 33 | 34 | typedef std::vector PollFdList; 35 | typedef std::map ChannelMap; 36 | 37 | EventLoop* ownerLoop_; 38 | PollFdList pollfds_; 39 | ChannelMap channels_; 40 | 41 | }; 42 | 43 | #endif 44 | 45 | -------------------------------------------------------------------------------- /v1/muduo++/10/Poller.h: -------------------------------------------------------------------------------- 1 | #ifndef _POLLER_H_ 2 | #define _POLLER_H_ 3 | 4 | #include "notCopyable.h" 5 | #include "Timestamp.h" 6 | #include "EventLoop.h" 7 | #include 8 | #include 9 | 10 | struct pollfd; 11 | 12 | class Channel; 13 | 14 | // fd关注的事件类型注册于Channel中 15 | class Poller: public notCopyable{ 16 | public: 17 | typedef std::vector ChannelList; 18 | 19 | Poller(EventLoop* loop); 20 | ~Poller(); 21 | 22 | Timestamp poll(int timeoutMs, ChannelList* activeChannels); 23 | 24 | void updateChannel(Channel* channel); 25 | void removeChannel(Channel* channel); 26 | 27 | void assertInLoopThread(){ 28 | ownerLoop_->assertInLoopThread(); 29 | } 30 | 31 | private: 32 | void fillActiveChannels(int numEvents, ChannelList* activeChannel) const; 33 | 34 | typedef std::vector PollFdList; 35 | typedef std::map ChannelMap; 36 | 37 | EventLoop* ownerLoop_; 38 | PollFdList pollfds_; 39 | ChannelMap channels_; 40 | 41 | }; 42 | 43 | #endif 44 | 45 | -------------------------------------------------------------------------------- /v1/muduo++/11/Poller.h: -------------------------------------------------------------------------------- 1 | #ifndef _POLLER_H_ 2 | #define _POLLER_H_ 3 | 4 | #include "notCopyable.h" 5 | #include "Timestamp.h" 6 | #include "EventLoop.h" 7 | #include 8 | #include 9 | 10 | struct pollfd; 11 | 12 | class Channel; 13 | 14 | // fd关注的事件类型注册于Channel中 15 | class Poller: public notCopyable{ 16 | public: 17 | typedef std::vector ChannelList; 18 | 19 | Poller(EventLoop* loop); 20 | ~Poller(); 21 | 22 | Timestamp poll(int timeoutMs, ChannelList* activeChannels); 23 | 24 | void updateChannel(Channel* channel); 25 | void removeChannel(Channel* channel); 26 | 27 | void assertInLoopThread(){ 28 | ownerLoop_->assertInLoopThread(); 29 | } 30 | 31 | private: 32 | void fillActiveChannels(int numEvents, ChannelList* activeChannel) const; 33 | 34 | typedef std::vector PollFdList; 35 | typedef std::map ChannelMap; 36 | 37 | EventLoop* ownerLoop_; 38 | PollFdList pollfds_; 39 | ChannelMap channels_; 40 | 41 | }; 42 | 43 | #endif 44 | 45 | -------------------------------------------------------------------------------- /v2/src/net/Poller.h: -------------------------------------------------------------------------------- 1 | #ifndef _POLLER_H_ 2 | #define _POLLER_H_ 3 | 4 | #include "notCopyable.h" 5 | #include "Timestamp.h" 6 | #include "EventLoop.h" 7 | #include 8 | #include 9 | 10 | struct pollfd; 11 | 12 | class Channel; 13 | 14 | // fd关注的事件类型注册于Channel中 15 | class Poller: public notCopyable{ 16 | public: 17 | typedef std::vector ChannelList; 18 | 19 | Poller(EventLoop* loop); 20 | ~Poller(); 21 | 22 | Timestamp poll(int timeoutMs, ChannelList* activeChannels); 23 | 24 | void updateChannel(Channel* channel); 25 | void removeChannel(Channel* channel); 26 | 27 | void assertInLoopThread(){ 28 | ownerLoop_->assertInLoopThread(); 29 | } 30 | 31 | private: 32 | void fillActiveChannels(int numEvents, ChannelList* activeChannel) const; 33 | 34 | typedef std::vector PollFdList; 35 | typedef std::map ChannelMap; 36 | 37 | EventLoop* ownerLoop_; 38 | PollFdList pollfds_; 39 | ChannelMap channels_; 40 | 41 | }; 42 | 43 | #endif 44 | 45 | -------------------------------------------------------------------------------- /v1/muduo++/04/Socket.h: -------------------------------------------------------------------------------- 1 | #ifndef _SOCKET_H_ 2 | #define _SOCKET_H_ 3 | 4 | #include "notCopyable.h" 5 | 6 | // http://www.cse.scu.edu/~dclark/am_256_graph_theory/linux_2_6_stack/structtcp__info.html 7 | struct tcp_info; 8 | 9 | class InetAddress; 10 | 11 | class Socket: public notCopyable{ 12 | public: 13 | explicit Socket(int sockfd) 14 | :sockfd_(sockfd){ 15 | } 16 | 17 | ~Socket(); 18 | 19 | int fd() const{ 20 | return sockfd_; 21 | } 22 | 23 | bool getTcpInfo(struct tcp_info*) const; 24 | bool getTcpInfoString(char* buf, int len) const; 25 | 26 | // abort operations 27 | void bindAddress(const InetAddress& localaddr); 28 | void listen(); 29 | 30 | int accept(InetAddress* peeraddr); 31 | 32 | void shutdownWrite(); 33 | 34 | // Set sockfd operations 35 | void setTcpNoDelay(bool on); // Nagle's algorithm 36 | void setReuseAddr(bool on); 37 | void setReusePort(bool on); 38 | void setKeepAlive(bool on); 39 | 40 | private: 41 | const int sockfd_; 42 | }; 43 | #endif 44 | -------------------------------------------------------------------------------- /v1/muduo++/05/Socket.h: -------------------------------------------------------------------------------- 1 | #ifndef _SOCKET_H_ 2 | #define _SOCKET_H_ 3 | 4 | #include "notCopyable.h" 5 | 6 | // http://www.cse.scu.edu/~dclark/am_256_graph_theory/linux_2_6_stack/structtcp__info.html 7 | struct tcp_info; 8 | 9 | class InetAddress; 10 | 11 | class Socket: public notCopyable{ 12 | public: 13 | explicit Socket(int sockfd) 14 | :sockfd_(sockfd){ 15 | } 16 | 17 | ~Socket(); 18 | 19 | int fd() const{ 20 | return sockfd_; 21 | } 22 | 23 | bool getTcpInfo(struct tcp_info*) const; 24 | bool getTcpInfoString(char* buf, int len) const; 25 | 26 | // abort operations 27 | void bindAddress(const InetAddress& localaddr); 28 | void listen(); 29 | 30 | int accept(InetAddress* peeraddr); 31 | 32 | void shutdownWrite(); 33 | 34 | // Set sockfd operations 35 | void setTcpNoDelay(bool on); // Nagle's algorithm 36 | void setReuseAddr(bool on); 37 | void setReusePort(bool on); 38 | void setKeepAlive(bool on); 39 | 40 | private: 41 | const int sockfd_; 42 | }; 43 | #endif 44 | -------------------------------------------------------------------------------- /v1/muduo++/06/Socket.h: -------------------------------------------------------------------------------- 1 | #ifndef _SOCKET_H_ 2 | #define _SOCKET_H_ 3 | 4 | #include "notCopyable.h" 5 | 6 | // http://www.cse.scu.edu/~dclark/am_256_graph_theory/linux_2_6_stack/structtcp__info.html 7 | struct tcp_info; 8 | 9 | class InetAddress; 10 | 11 | class Socket: public notCopyable{ 12 | public: 13 | explicit Socket(int sockfd) 14 | :sockfd_(sockfd){ 15 | } 16 | 17 | ~Socket(); 18 | 19 | int fd() const{ 20 | return sockfd_; 21 | } 22 | 23 | bool getTcpInfo(struct tcp_info*) const; 24 | bool getTcpInfoString(char* buf, int len) const; 25 | 26 | // abort operations 27 | void bindAddress(const InetAddress& localaddr); 28 | void listen(); 29 | 30 | int accept(InetAddress* peeraddr); 31 | 32 | void shutdownWrite(); 33 | 34 | // Set sockfd operations 35 | void setTcpNoDelay(bool on); // Nagle's algorithm 36 | void setReuseAddr(bool on); 37 | void setReusePort(bool on); 38 | void setKeepAlive(bool on); 39 | 40 | private: 41 | const int sockfd_; 42 | }; 43 | #endif 44 | -------------------------------------------------------------------------------- /v1/muduo++/07/Socket.h: -------------------------------------------------------------------------------- 1 | #ifndef _SOCKET_H_ 2 | #define _SOCKET_H_ 3 | 4 | #include "notCopyable.h" 5 | 6 | // http://www.cse.scu.edu/~dclark/am_256_graph_theory/linux_2_6_stack/structtcp__info.html 7 | struct tcp_info; 8 | 9 | class InetAddress; 10 | 11 | class Socket: public notCopyable{ 12 | public: 13 | explicit Socket(int sockfd) 14 | :sockfd_(sockfd){ 15 | } 16 | 17 | ~Socket(); 18 | 19 | int fd() const{ 20 | return sockfd_; 21 | } 22 | 23 | bool getTcpInfo(struct tcp_info*) const; 24 | bool getTcpInfoString(char* buf, int len) const; 25 | 26 | // abort operations 27 | void bindAddress(const InetAddress& localaddr); 28 | void listen(); 29 | 30 | int accept(InetAddress* peeraddr); 31 | 32 | void shutdownWrite(); 33 | 34 | // Set sockfd operations 35 | void setTcpNoDelay(bool on); // Nagle's algorithm 36 | void setReuseAddr(bool on); 37 | void setReusePort(bool on); 38 | void setKeepAlive(bool on); 39 | 40 | private: 41 | const int sockfd_; 42 | }; 43 | #endif 44 | -------------------------------------------------------------------------------- /v1/muduo++/08/Socket.h: -------------------------------------------------------------------------------- 1 | #ifndef _SOCKET_H_ 2 | #define _SOCKET_H_ 3 | 4 | #include "notCopyable.h" 5 | 6 | // http://www.cse.scu.edu/~dclark/am_256_graph_theory/linux_2_6_stack/structtcp__info.html 7 | struct tcp_info; 8 | 9 | class InetAddress; 10 | 11 | class Socket: public notCopyable{ 12 | public: 13 | explicit Socket(int sockfd) 14 | :sockfd_(sockfd){ 15 | } 16 | 17 | ~Socket(); 18 | 19 | int fd() const{ 20 | return sockfd_; 21 | } 22 | 23 | bool getTcpInfo(struct tcp_info*) const; 24 | bool getTcpInfoString(char* buf, int len) const; 25 | 26 | // abort operations 27 | void bindAddress(const InetAddress& localaddr); 28 | void listen(); 29 | 30 | int accept(InetAddress* peeraddr); 31 | 32 | void shutdownWrite(); 33 | 34 | // Set sockfd operations 35 | void setTcpNoDelay(bool on); // Nagle's algorithm 36 | void setReuseAddr(bool on); 37 | void setReusePort(bool on); 38 | void setKeepAlive(bool on); 39 | 40 | private: 41 | const int sockfd_; 42 | }; 43 | #endif 44 | -------------------------------------------------------------------------------- /v1/muduo++/09/Socket.h: -------------------------------------------------------------------------------- 1 | #ifndef _SOCKET_H_ 2 | #define _SOCKET_H_ 3 | 4 | #include "notCopyable.h" 5 | 6 | // http://www.cse.scu.edu/~dclark/am_256_graph_theory/linux_2_6_stack/structtcp__info.html 7 | struct tcp_info; 8 | 9 | class InetAddress; 10 | 11 | class Socket: public notCopyable{ 12 | public: 13 | explicit Socket(int sockfd) 14 | :sockfd_(sockfd){ 15 | } 16 | 17 | ~Socket(); 18 | 19 | int fd() const{ 20 | return sockfd_; 21 | } 22 | 23 | bool getTcpInfo(struct tcp_info*) const; 24 | bool getTcpInfoString(char* buf, int len) const; 25 | 26 | // abort operations 27 | void bindAddress(const InetAddress& localaddr); 28 | void listen(); 29 | 30 | int accept(InetAddress* peeraddr); 31 | 32 | void shutdownWrite(); 33 | 34 | // Set sockfd operations 35 | void setTcpNoDelay(bool on); // Nagle's algorithm 36 | void setReuseAddr(bool on); 37 | void setReusePort(bool on); 38 | void setKeepAlive(bool on); 39 | 40 | private: 41 | const int sockfd_; 42 | }; 43 | #endif 44 | -------------------------------------------------------------------------------- /v1/muduo++/10/Socket.h: -------------------------------------------------------------------------------- 1 | #ifndef _SOCKET_H_ 2 | #define _SOCKET_H_ 3 | 4 | #include "notCopyable.h" 5 | 6 | // http://www.cse.scu.edu/~dclark/am_256_graph_theory/linux_2_6_stack/structtcp__info.html 7 | struct tcp_info; 8 | 9 | class InetAddress; 10 | 11 | class Socket: public notCopyable{ 12 | public: 13 | explicit Socket(int sockfd) 14 | :sockfd_(sockfd){ 15 | } 16 | 17 | ~Socket(); 18 | 19 | int fd() const{ 20 | return sockfd_; 21 | } 22 | 23 | bool getTcpInfo(struct tcp_info*) const; 24 | bool getTcpInfoString(char* buf, int len) const; 25 | 26 | // abort operations 27 | void bindAddress(const InetAddress& localaddr); 28 | void listen(); 29 | 30 | int accept(InetAddress* peeraddr); 31 | 32 | void shutdownWrite(); 33 | 34 | // Set sockfd operations 35 | void setTcpNoDelay(bool on); // Nagle's algorithm 36 | void setReuseAddr(bool on); 37 | void setReusePort(bool on); 38 | void setKeepAlive(bool on); 39 | 40 | private: 41 | const int sockfd_; 42 | }; 43 | #endif 44 | -------------------------------------------------------------------------------- /v1/muduo++/11/Socket.h: -------------------------------------------------------------------------------- 1 | #ifndef _SOCKET_H_ 2 | #define _SOCKET_H_ 3 | 4 | #include "notCopyable.h" 5 | 6 | // http://www.cse.scu.edu/~dclark/am_256_graph_theory/linux_2_6_stack/structtcp__info.html 7 | struct tcp_info; 8 | 9 | class InetAddress; 10 | 11 | class Socket: public notCopyable{ 12 | public: 13 | explicit Socket(int sockfd) 14 | :sockfd_(sockfd){ 15 | } 16 | 17 | ~Socket(); 18 | 19 | int fd() const{ 20 | return sockfd_; 21 | } 22 | 23 | bool getTcpInfo(struct tcp_info*) const; 24 | bool getTcpInfoString(char* buf, int len) const; 25 | 26 | // abort operations 27 | void bindAddress(const InetAddress& localaddr); 28 | void listen(); 29 | 30 | int accept(InetAddress* peeraddr); 31 | 32 | void shutdownWrite(); 33 | 34 | // Set sockfd operations 35 | void setTcpNoDelay(bool on); // Nagle's algorithm 36 | void setReuseAddr(bool on); 37 | void setReusePort(bool on); 38 | void setKeepAlive(bool on); 39 | 40 | private: 41 | const int sockfd_; 42 | }; 43 | #endif 44 | -------------------------------------------------------------------------------- /v2/src/net/Socket.h: -------------------------------------------------------------------------------- 1 | #ifndef _SOCKET_H_ 2 | #define _SOCKET_H_ 3 | 4 | #include "notCopyable.h" 5 | 6 | // http://www.cse.scu.edu/~dclark/am_256_graph_theory/linux_2_6_stack/structtcp__info.html 7 | struct tcp_info; 8 | 9 | class InetAddress; 10 | 11 | class Socket: public notCopyable{ 12 | public: 13 | explicit Socket(int sockfd) 14 | :sockfd_(sockfd){ 15 | } 16 | 17 | ~Socket(); 18 | 19 | int fd() const{ 20 | return sockfd_; 21 | } 22 | 23 | bool getTcpInfo(struct tcp_info*) const; 24 | bool getTcpInfoString(char* buf, int len) const; 25 | 26 | // abort operations 27 | void bindAddress(const InetAddress& localaddr); 28 | void listen(); 29 | 30 | int accept(InetAddress* peeraddr); 31 | 32 | void shutdownWrite(); 33 | 34 | // Set sockfd operations 35 | void setTcpNoDelay(bool on); // Nagle's algorithm 36 | void setReuseAddr(bool on); 37 | void setReusePort(bool on); 38 | void setKeepAlive(bool on); 39 | 40 | private: 41 | const int sockfd_; 42 | }; 43 | #endif 44 | -------------------------------------------------------------------------------- /v1/muduo++/01/EventLoop.h: -------------------------------------------------------------------------------- 1 | #ifndef _EVENTLOOP_H_ 2 | #define _EVENTLOOP_H_ 3 | 4 | 5 | #include 6 | #include 7 | #include 8 | #include "CurrentThread.h" 9 | #include "notCopyable.h" 10 | 11 | 12 | class Poller; 13 | class Channel; 14 | 15 | class EventLoop : public notCopyable{ 16 | public: 17 | EventLoop(); 18 | ~EventLoop(); 19 | 20 | void loop(); 21 | 22 | void assertInLoopThread(){ 23 | if(!isInLoopThread()){ 24 | // do some logging things 25 | abortNotInLoopThread(); 26 | } 27 | } 28 | 29 | void quit(); 30 | 31 | void updateChannel(Channel* channel); 32 | 33 | bool isInLoopThread() const{ 34 | return threadId_ == CurrentThread::tid(); 35 | } 36 | 37 | private: 38 | void abortNotInLoopThread(); 39 | 40 | typedef std::vector ChannelList; 41 | 42 | bool looping_; 43 | bool quit_; 44 | 45 | const pid_t threadId_; 46 | 47 | std::unique_ptr poller_; 48 | ChannelList activeChannels_; 49 | }; 50 | 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /v3/src/base/Mutex.h: -------------------------------------------------------------------------------- 1 | #ifndef _MUTEX_H_ 2 | #define _MUTEX_H_ 3 | 4 | #include 5 | #include 6 | 7 | #include "CurrentThread.h" 8 | #include "notCopyable.h" 9 | 10 | 11 | class MutexLock: public notCopyable{ 12 | public: 13 | MutexLock(){ 14 | pthread_mutex_init(&mutex_, NULL); 15 | } 16 | 17 | ~MutexLock(){ 18 | pthread_mutex_destroy(&mutex_); 19 | } 20 | 21 | void lock(){ 22 | pthread_mutex_lock(&mutex_); 23 | } 24 | 25 | void unlock(){ 26 | pthread_mutex_unlock(&mutex_); 27 | } 28 | 29 | pthread_mutex_t* getMutex(){ 30 | return &mutex_; 31 | } 32 | 33 | private: 34 | friend class Condition; 35 | 36 | pthread_mutex_t mutex_; 37 | }; 38 | 39 | // RAII stack object 40 | class MutexLockGuard: public notCopyable{ 41 | public: 42 | explicit MutexLockGuard(MutexLock& mutex) 43 | :mutex_(mutex){ 44 | mutex_.lock(); 45 | } 46 | 47 | ~MutexLockGuard(){ 48 | mutex_.unlock(); 49 | } 50 | 51 | private: 52 | MutexLock& mutex_; 53 | }; 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /v3/src/EventLoopThreadPool.h: -------------------------------------------------------------------------------- 1 | #ifndef _EVENTLOOPTHREADPOOL_H_ 2 | #define _EVENTLOOPTHREADPOOL_H_ 3 | 4 | #include 5 | #include 6 | 7 | #include "base/notCopyable.h" 8 | 9 | class EventLoop; 10 | class EventLoopThread; 11 | class EventLoopThreadPool; 12 | 13 | // stack variable 14 | class EventLoopThreadPool: public notCopyable{ 15 | public: 16 | typedef std::function ThreadInitCallback; 17 | 18 | EventLoopThreadPool(EventLoop* loop); 19 | ~EventLoopThreadPool(); 20 | 21 | void setThreadNum(int numThreads){ 22 | numThreads_ = numThreads; 23 | } 24 | 25 | void start(const ThreadInitCallback& cb = ThreadInitCallback()); 26 | 27 | EventLoop* getNextLoop(); 28 | std::vector getAllLoops(); 29 | 30 | bool started() const{ 31 | return started_; 32 | } 33 | 34 | private: 35 | bool started_; 36 | 37 | int numThreads_; 38 | int next_; 39 | 40 | EventLoop* baseLoop_; 41 | 42 | std::vector threads_; 43 | std::vector loops_; 44 | }; 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /v1/muduo++/03/Condition.h: -------------------------------------------------------------------------------- 1 | #ifndef _CONDITION_H_ 2 | #define _CONDITION_H_ 3 | 4 | #include "Mutex.h" 5 | #include "notCopyable.h" 6 | #include 7 | 8 | #define MCHECK(ret) ({ __typeof__ (ret) errnum = (ret); \ 9 | if (__builtin_expect(errnum != 0, 0)) \ 10 | __assert_perror_fail (errnum, __FILE__, __LINE__, __func__);}) 11 | 12 | 13 | class Condition: public notCopyable{ 14 | public: 15 | explicit Condition(MutexLock& mutex) 16 | :mutex_(mutex){ 17 | pthread_cond_init(&pcond_, NULL); 18 | } 19 | 20 | ~Condition(){ 21 | pthread_cond_destroy(&pcond_); 22 | } 23 | 24 | void wait(){ 25 | MutexLock::UnassignGuard ug(mutex_); 26 | MCHECK(pthread_cond_wait(&pcond_, mutex_.getMutex())); 27 | } 28 | 29 | bool waitForSeconds(double seconds); 30 | 31 | void notify(){ 32 | pthread_cond_signal(&pcond_); 33 | } 34 | 35 | void notifyAll(){ 36 | pthread_cond_broadcast(&pcond_); 37 | } 38 | 39 | private: 40 | MutexLock& mutex_; 41 | pthread_cond_t pcond_; 42 | }; 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /v1/muduo++/04/Condition.h: -------------------------------------------------------------------------------- 1 | #ifndef _CONDITION_H_ 2 | #define _CONDITION_H_ 3 | 4 | #include "Mutex.h" 5 | #include "notCopyable.h" 6 | #include 7 | 8 | #define MCHECK(ret) ({ __typeof__ (ret) errnum = (ret); \ 9 | if (__builtin_expect(errnum != 0, 0)) \ 10 | __assert_perror_fail (errnum, __FILE__, __LINE__, __func__);}) 11 | 12 | 13 | class Condition: public notCopyable{ 14 | public: 15 | explicit Condition(MutexLock& mutex) 16 | :mutex_(mutex){ 17 | pthread_cond_init(&pcond_, NULL); 18 | } 19 | 20 | ~Condition(){ 21 | pthread_cond_destroy(&pcond_); 22 | } 23 | 24 | void wait(){ 25 | MutexLock::UnassignGuard ug(mutex_); 26 | MCHECK(pthread_cond_wait(&pcond_, mutex_.getMutex())); 27 | } 28 | 29 | bool waitForSeconds(double seconds); 30 | 31 | void notify(){ 32 | pthread_cond_signal(&pcond_); 33 | } 34 | 35 | void notifyAll(){ 36 | pthread_cond_broadcast(&pcond_); 37 | } 38 | 39 | private: 40 | MutexLock& mutex_; 41 | pthread_cond_t pcond_; 42 | }; 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /v1/muduo++/05/Condition.h: -------------------------------------------------------------------------------- 1 | #ifndef _CONDITION_H_ 2 | #define _CONDITION_H_ 3 | 4 | #include "Mutex.h" 5 | #include "notCopyable.h" 6 | #include 7 | 8 | #define MCHECK(ret) ({ __typeof__ (ret) errnum = (ret); \ 9 | if (__builtin_expect(errnum != 0, 0)) \ 10 | __assert_perror_fail (errnum, __FILE__, __LINE__, __func__);}) 11 | 12 | 13 | class Condition: public notCopyable{ 14 | public: 15 | explicit Condition(MutexLock& mutex) 16 | :mutex_(mutex){ 17 | pthread_cond_init(&pcond_, NULL); 18 | } 19 | 20 | ~Condition(){ 21 | pthread_cond_destroy(&pcond_); 22 | } 23 | 24 | void wait(){ 25 | MutexLock::UnassignGuard ug(mutex_); 26 | MCHECK(pthread_cond_wait(&pcond_, mutex_.getMutex())); 27 | } 28 | 29 | bool waitForSeconds(double seconds); 30 | 31 | void notify(){ 32 | pthread_cond_signal(&pcond_); 33 | } 34 | 35 | void notifyAll(){ 36 | pthread_cond_broadcast(&pcond_); 37 | } 38 | 39 | private: 40 | MutexLock& mutex_; 41 | pthread_cond_t pcond_; 42 | }; 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /v1/muduo++/06/Condition.h: -------------------------------------------------------------------------------- 1 | #ifndef _CONDITION_H_ 2 | #define _CONDITION_H_ 3 | 4 | #include "Mutex.h" 5 | #include "notCopyable.h" 6 | #include 7 | 8 | #define MCHECK(ret) ({ __typeof__ (ret) errnum = (ret); \ 9 | if (__builtin_expect(errnum != 0, 0)) \ 10 | __assert_perror_fail (errnum, __FILE__, __LINE__, __func__);}) 11 | 12 | 13 | class Condition: public notCopyable{ 14 | public: 15 | explicit Condition(MutexLock& mutex) 16 | :mutex_(mutex){ 17 | pthread_cond_init(&pcond_, NULL); 18 | } 19 | 20 | ~Condition(){ 21 | pthread_cond_destroy(&pcond_); 22 | } 23 | 24 | void wait(){ 25 | MutexLock::UnassignGuard ug(mutex_); 26 | MCHECK(pthread_cond_wait(&pcond_, mutex_.getMutex())); 27 | } 28 | 29 | bool waitForSeconds(double seconds); 30 | 31 | void notify(){ 32 | pthread_cond_signal(&pcond_); 33 | } 34 | 35 | void notifyAll(){ 36 | pthread_cond_broadcast(&pcond_); 37 | } 38 | 39 | private: 40 | MutexLock& mutex_; 41 | pthread_cond_t pcond_; 42 | }; 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /v1/muduo++/07/Condition.h: -------------------------------------------------------------------------------- 1 | #ifndef _CONDITION_H_ 2 | #define _CONDITION_H_ 3 | 4 | #include "Mutex.h" 5 | #include "notCopyable.h" 6 | #include 7 | 8 | #define MCHECK(ret) ({ __typeof__ (ret) errnum = (ret); \ 9 | if (__builtin_expect(errnum != 0, 0)) \ 10 | __assert_perror_fail (errnum, __FILE__, __LINE__, __func__);}) 11 | 12 | 13 | class Condition: public notCopyable{ 14 | public: 15 | explicit Condition(MutexLock& mutex) 16 | :mutex_(mutex){ 17 | pthread_cond_init(&pcond_, NULL); 18 | } 19 | 20 | ~Condition(){ 21 | pthread_cond_destroy(&pcond_); 22 | } 23 | 24 | void wait(){ 25 | MutexLock::UnassignGuard ug(mutex_); 26 | MCHECK(pthread_cond_wait(&pcond_, mutex_.getMutex())); 27 | } 28 | 29 | bool waitForSeconds(double seconds); 30 | 31 | void notify(){ 32 | pthread_cond_signal(&pcond_); 33 | } 34 | 35 | void notifyAll(){ 36 | pthread_cond_broadcast(&pcond_); 37 | } 38 | 39 | private: 40 | MutexLock& mutex_; 41 | pthread_cond_t pcond_; 42 | }; 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /v1/muduo++/08/Condition.h: -------------------------------------------------------------------------------- 1 | #ifndef _CONDITION_H_ 2 | #define _CONDITION_H_ 3 | 4 | #include "Mutex.h" 5 | #include "notCopyable.h" 6 | #include 7 | 8 | #define MCHECK(ret) ({ __typeof__ (ret) errnum = (ret); \ 9 | if (__builtin_expect(errnum != 0, 0)) \ 10 | __assert_perror_fail (errnum, __FILE__, __LINE__, __func__);}) 11 | 12 | 13 | class Condition: public notCopyable{ 14 | public: 15 | explicit Condition(MutexLock& mutex) 16 | :mutex_(mutex){ 17 | pthread_cond_init(&pcond_, NULL); 18 | } 19 | 20 | ~Condition(){ 21 | pthread_cond_destroy(&pcond_); 22 | } 23 | 24 | void wait(){ 25 | MutexLock::UnassignGuard ug(mutex_); 26 | MCHECK(pthread_cond_wait(&pcond_, mutex_.getMutex())); 27 | } 28 | 29 | bool waitForSeconds(double seconds); 30 | 31 | void notify(){ 32 | pthread_cond_signal(&pcond_); 33 | } 34 | 35 | void notifyAll(){ 36 | pthread_cond_broadcast(&pcond_); 37 | } 38 | 39 | private: 40 | MutexLock& mutex_; 41 | pthread_cond_t pcond_; 42 | }; 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /v1/muduo++/09/Condition.h: -------------------------------------------------------------------------------- 1 | #ifndef _CONDITION_H_ 2 | #define _CONDITION_H_ 3 | 4 | #include "Mutex.h" 5 | #include "notCopyable.h" 6 | #include 7 | 8 | #define MCHECK(ret) ({ __typeof__ (ret) errnum = (ret); \ 9 | if (__builtin_expect(errnum != 0, 0)) \ 10 | __assert_perror_fail (errnum, __FILE__, __LINE__, __func__);}) 11 | 12 | 13 | class Condition: public notCopyable{ 14 | public: 15 | explicit Condition(MutexLock& mutex) 16 | :mutex_(mutex){ 17 | pthread_cond_init(&pcond_, NULL); 18 | } 19 | 20 | ~Condition(){ 21 | pthread_cond_destroy(&pcond_); 22 | } 23 | 24 | void wait(){ 25 | MutexLock::UnassignGuard ug(mutex_); 26 | MCHECK(pthread_cond_wait(&pcond_, mutex_.getMutex())); 27 | } 28 | 29 | bool waitForSeconds(double seconds); 30 | 31 | void notify(){ 32 | pthread_cond_signal(&pcond_); 33 | } 34 | 35 | void notifyAll(){ 36 | pthread_cond_broadcast(&pcond_); 37 | } 38 | 39 | private: 40 | MutexLock& mutex_; 41 | pthread_cond_t pcond_; 42 | }; 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /v1/muduo++/10/Condition.h: -------------------------------------------------------------------------------- 1 | #ifndef _CONDITION_H_ 2 | #define _CONDITION_H_ 3 | 4 | #include "Mutex.h" 5 | #include "notCopyable.h" 6 | #include 7 | 8 | #define MCHECK(ret) ({ __typeof__ (ret) errnum = (ret); \ 9 | if (__builtin_expect(errnum != 0, 0)) \ 10 | __assert_perror_fail (errnum, __FILE__, __LINE__, __func__);}) 11 | 12 | 13 | class Condition: public notCopyable{ 14 | public: 15 | explicit Condition(MutexLock& mutex) 16 | :mutex_(mutex){ 17 | pthread_cond_init(&pcond_, NULL); 18 | } 19 | 20 | ~Condition(){ 21 | pthread_cond_destroy(&pcond_); 22 | } 23 | 24 | void wait(){ 25 | MutexLock::UnassignGuard ug(mutex_); 26 | MCHECK(pthread_cond_wait(&pcond_, mutex_.getMutex())); 27 | } 28 | 29 | bool waitForSeconds(double seconds); 30 | 31 | void notify(){ 32 | pthread_cond_signal(&pcond_); 33 | } 34 | 35 | void notifyAll(){ 36 | pthread_cond_broadcast(&pcond_); 37 | } 38 | 39 | private: 40 | MutexLock& mutex_; 41 | pthread_cond_t pcond_; 42 | }; 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /v1/muduo++/11/Condition.h: -------------------------------------------------------------------------------- 1 | #ifndef _CONDITION_H_ 2 | #define _CONDITION_H_ 3 | 4 | #include "Mutex.h" 5 | #include "notCopyable.h" 6 | #include 7 | 8 | #define MCHECK(ret) ({ __typeof__ (ret) errnum = (ret); \ 9 | if (__builtin_expect(errnum != 0, 0)) \ 10 | __assert_perror_fail (errnum, __FILE__, __LINE__, __func__);}) 11 | 12 | 13 | class Condition: public notCopyable{ 14 | public: 15 | explicit Condition(MutexLock& mutex) 16 | :mutex_(mutex){ 17 | pthread_cond_init(&pcond_, NULL); 18 | } 19 | 20 | ~Condition(){ 21 | pthread_cond_destroy(&pcond_); 22 | } 23 | 24 | void wait(){ 25 | MutexLock::UnassignGuard ug(mutex_); 26 | MCHECK(pthread_cond_wait(&pcond_, mutex_.getMutex())); 27 | } 28 | 29 | bool waitForSeconds(double seconds); 30 | 31 | void notify(){ 32 | pthread_cond_signal(&pcond_); 33 | } 34 | 35 | void notifyAll(){ 36 | pthread_cond_broadcast(&pcond_); 37 | } 38 | 39 | private: 40 | MutexLock& mutex_; 41 | pthread_cond_t pcond_; 42 | }; 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /v2/src/net/LogFile.h: -------------------------------------------------------------------------------- 1 | #ifndef _LOGFILE_H_ 2 | #define _LOGFILE_H_ 3 | 4 | #include "Mutex.h" 5 | #include "notCopyable.h" 6 | 7 | #include 8 | #include 9 | 10 | class AppendFile; 11 | 12 | class LogFile: public notCopyable{ 13 | public: 14 | LogFile(const std::string& basename, int rollSize, bool threadSafe = true, int flushInterval = 3, int checkEvenyN = 2014); 15 | ~LogFile(); 16 | 17 | void append(const char* logFile, int len); 18 | void flush(); 19 | bool rollFile(); 20 | 21 | private: 22 | void append_unlocked(const char* logline, int len); 23 | static std::string getLogFileName(const std::string basename, time_t* now); 24 | 25 | const std::string basename_; 26 | const int rollSize_; 27 | const int flushInterval_; 28 | const int checkEveryN_; 29 | 30 | int count_; 31 | 32 | const std::unique_ptr mutex_; 33 | std::unique_ptr file_; 34 | 35 | time_t startOfPeriod_; 36 | time_t lastRoll_; 37 | time_t lastFlush_; 38 | const static int kRollPerSeconds_ = 60 * 60 * 24; 39 | }; 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /v1/muduo++/10/LogFile.h: -------------------------------------------------------------------------------- 1 | #ifndef _LOGFILE_H_ 2 | #define _LOGFILE_H_ 3 | 4 | #include "Mutex.h" 5 | #include "notCopyable.h" 6 | 7 | #include 8 | #include 9 | 10 | class AppendFile; 11 | 12 | class LogFile: public notCopyable{ 13 | public: 14 | LogFile(const std::string& basename, int rollSize, bool threadSafe = true, int flushInterval = 3, int checkEvenyN = 2014); 15 | ~LogFile(); 16 | 17 | void append(const char* logFile, int len); 18 | void flush(); 19 | bool rollFile(); 20 | 21 | private: 22 | void append_unlocked(const char* logline, int len); 23 | static std::string getLogFileName(const std::string basename, time_t* now); 24 | 25 | const std::string basename_; 26 | const int rollSize_; 27 | const int flushInterval_; 28 | const int checkEveryN_; 29 | 30 | int count_; 31 | 32 | const std::unique_ptr mutex_; 33 | std::unique_ptr file_; 34 | 35 | time_t startOfPeriod_; 36 | time_t lastRoll_; 37 | time_t lastFlush_; 38 | const static int kRollPerSeconds_ = 60 * 60 * 24; 39 | }; 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /v1/muduo++/11/LogFile.h: -------------------------------------------------------------------------------- 1 | #ifndef _LOGFILE_H_ 2 | #define _LOGFILE_H_ 3 | 4 | #include "Mutex.h" 5 | #include "notCopyable.h" 6 | 7 | #include 8 | #include 9 | 10 | class AppendFile; 11 | 12 | class LogFile: public notCopyable{ 13 | public: 14 | LogFile(const std::string& basename, int rollSize, bool threadSafe = true, int flushInterval = 3, int checkEvenyN = 2014); 15 | ~LogFile(); 16 | 17 | void append(const char* logFile, int len); 18 | void flush(); 19 | bool rollFile(); 20 | 21 | private: 22 | void append_unlocked(const char* logline, int len); 23 | static std::string getLogFileName(const std::string basename, time_t* now); 24 | 25 | const std::string basename_; 26 | const int rollSize_; 27 | const int flushInterval_; 28 | const int checkEveryN_; 29 | 30 | int count_; 31 | 32 | const std::unique_ptr mutex_; 33 | std::unique_ptr file_; 34 | 35 | time_t startOfPeriod_; 36 | time_t lastRoll_; 37 | time_t lastFlush_; 38 | const static int kRollPerSeconds_ = 60 * 60 * 24; 39 | }; 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /v3/src/base/log/LogFile.h: -------------------------------------------------------------------------------- 1 | #ifndef _LOGFILE_H_ 2 | #define _LOGFILE_H_ 3 | 4 | #include "../Mutex.h" 5 | #include "../notCopyable.h" 6 | 7 | #include 8 | #include 9 | 10 | class AppendFile; 11 | 12 | class LogFile: public notCopyable{ 13 | public: 14 | LogFile(const std::string& basename, int rollSize, bool threadSafe = true, int flushInterval = 3, int checkEvenyN = 2014); 15 | ~LogFile(); 16 | 17 | void append(const char* logFile, int len); 18 | void flush(); 19 | bool rollFile(); 20 | 21 | private: 22 | void append_unlocked(const char* logline, int len); 23 | static std::string getLogFileName(const std::string basename, time_t* now); 24 | 25 | const std::string basename_; 26 | const int rollSize_; 27 | const int flushInterval_; 28 | const int checkEveryN_; 29 | 30 | int count_; 31 | 32 | const std::unique_ptr mutex_; 33 | std::unique_ptr file_; 34 | 35 | time_t startOfPeriod_; 36 | time_t lastRoll_; 37 | time_t lastFlush_; 38 | const static int kRollPerSeconds_ = 60 * 60 * 24; 39 | }; 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /v1/muduo++/05/main.cpp: -------------------------------------------------------------------------------- 1 | #include "TcpConnection.h" 2 | #include "InetAddress.h" 3 | #include "EventLoop.h" 4 | #include "TcpServer.h" 5 | #include "Callbacks.h" 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | void onConnection(const TcpConnectionPtr& conn){ 12 | if(conn->connected()){ 13 | printf("onConnection(): new connection [%s] from %s\n", conn->name().c_str(), conn->peerAddress().toIpPort().c_str()); 14 | } 15 | else{ 16 | printf("on connection(): conenction [%s] is down\n", conn->name().c_str()); 17 | } 18 | } 19 | 20 | void onMessage(const TcpConnectionPtr& conn, const char* data, ssize_t n){ 21 | printf("onMessage(): recived from connection [%s] with %zd byte data\n", conn->name().c_str(), n); 22 | } 23 | 24 | int main(){ 25 | printf("main(): pid = %d\n", getpid()); 26 | 27 | InetAddress listenAddr(9981); 28 | EventLoop loop; 29 | TcpServer server(&loop, listenAddr, std::string("one")); 30 | server.setConnectionCallback(onConnection); 31 | server.setMessageCallback(onMessage); 32 | server.start(); 33 | 34 | loop.loop(); 35 | } 36 | -------------------------------------------------------------------------------- /v3/src/base/CurrentThread.h: -------------------------------------------------------------------------------- 1 | #ifndef _CURRENT_THREAD_H_ 2 | #define _CURRENT_THREAD_H_ 3 | 4 | #include 5 | 6 | namespace CurrentThread{ 7 | extern __thread int t_cachedTid; 8 | extern __thread char t_tidString[32]; 9 | extern __thread int t_tidStringLength; 10 | extern __thread int t_cachedPid; 11 | extern __thread const char* t_threadName; 12 | 13 | pid_t gettid(); 14 | void cachedTid(); 15 | 16 | pid_t getpid(); 17 | void cachedPid(); 18 | 19 | inline int tid(){ 20 | if(__builtin_expect(t_cachedTid == 0, 0)){ 21 | cachedTid(); 22 | } 23 | return t_cachedTid; 24 | } 25 | 26 | inline int pid(){ 27 | if(__builtin_expect(t_cachedPid == 0, 0)){ 28 | cachedPid(); 29 | } 30 | return t_cachedPid; 31 | } 32 | 33 | inline const char* tidString(){ 34 | return t_tidString; 35 | } 36 | 37 | inline int tidStringLength(){ 38 | return t_tidStringLength; 39 | } 40 | 41 | inline const char* name(){ 42 | return t_threadName; 43 | } 44 | } 45 | 46 | #endif 47 | 48 | -------------------------------------------------------------------------------- /v1/muduo++/02/main.cpp: -------------------------------------------------------------------------------- 1 | #include "EventLoop.h" 2 | #include "CurrentThread.h" 3 | #include "Timestamp.h" 4 | 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | int cnt = 0; 11 | EventLoop* g_loop; 12 | 13 | void printTid() 14 | { 15 | printf("pid = %d, tid = %d\n", getpid(), CurrentThread::tid()); 16 | printf("now %s\n", Timestamp::now().toString().c_str()); 17 | } 18 | 19 | void print(const char* msg) 20 | { 21 | printf("msg %s %s\n", Timestamp::now().toString().c_str(), msg); 22 | if (++cnt == 20) 23 | { 24 | g_loop->quit(); 25 | } 26 | } 27 | 28 | int main() 29 | { 30 | printTid(); 31 | EventLoop loop; 32 | g_loop = &loop; 33 | 34 | print("main"); 35 | loop.runAfter(1, std::bind(print, "once1")); 36 | printf("I am here"); 37 | loop.runAfter(1.5, std::bind(print, "once1.5")); 38 | loop.runAfter(2.5, std::bind(print, "once2.5")); 39 | loop.runAfter(3.5, std::bind(print, "once3.5")); 40 | loop.runEvery(2, std::bind(print, "every2")); 41 | loop.runEvery(3, std::bind(print, "every3")); 42 | 43 | loop.loop(); 44 | print("main loop exits"); 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /v1/muduo++/05/Callbacks.h: -------------------------------------------------------------------------------- 1 | #ifndef _CALLBACKS_H_ 2 | #define _CALLBACKS_H_ 3 | 4 | #include 5 | #include 6 | #include "Timestamp.h" 7 | 8 | typedef std::function TimerCallback; 9 | 10 | // The only one that uses shared_ptr 11 | class TcpConnection; 12 | typedef std::shared_ptr TcpConnectionPtr; 13 | 14 | typedef std::function ConnectionCallback; 15 | typedef std::function CloseCallback; 16 | // implementation in TcpConnetcion.cc in muduo project 17 | void defaultConnectionCallback(const TcpConnectionPtr& conn); 18 | 19 | typedef std::function MessageCallback; 20 | void defaultMessageCallback(const TcpConnectionPtr& conn, const char* buf, int n); 21 | 22 | // get_pointer as it be named 23 | template 24 | T* get_pointer(T* p){ 25 | return p; 26 | } 27 | 28 | template 29 | T* get_pointer(std::unique_ptr const& p){ 30 | return p.get(); 31 | } 32 | 33 | template 34 | T* get_pointer(std::shared_ptr const& p){ 35 | return p.get(); 36 | } 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /v1/muduo++/10/CurrentThread.h: -------------------------------------------------------------------------------- 1 | #ifndef _CURRENT_THREAD_H 2 | #define _CURRENT_THREAD_H 3 | 4 | #include 5 | 6 | namespace CurrentThread{ 7 | 8 | extern __thread int t_cachedTid; 9 | extern __thread char t_tidString[32]; 10 | extern __thread int t_tidStringLength; 11 | extern __thread int t_cachedPid; 12 | extern __thread const char* t_threadName; 13 | 14 | pid_t gettid(); 15 | void cachedTid(); 16 | 17 | pid_t getpid(); 18 | void cachedPid(); 19 | 20 | inline int tid(){ 21 | if(__builtin_expect(t_cachedTid == 0, 0)){ 22 | cachedTid(); 23 | } 24 | return t_cachedTid; 25 | } 26 | 27 | inline int pid(){ 28 | if(__builtin_expect(t_cachedPid == 0, 0)){ 29 | cachedPid(); 30 | } 31 | return t_cachedPid; 32 | } 33 | 34 | inline const char* tidString(){ 35 | return t_tidString; 36 | } 37 | 38 | inline int tidStringLength(){ 39 | return t_tidStringLength; 40 | } 41 | 42 | inline const char* name(){ 43 | return t_threadName; 44 | } 45 | 46 | 47 | } 48 | 49 | #endif 50 | 51 | -------------------------------------------------------------------------------- /v1/muduo++/11/CurrentThread.h: -------------------------------------------------------------------------------- 1 | #ifndef _CURRENT_THREAD_H 2 | #define _CURRENT_THREAD_H 3 | 4 | #include 5 | 6 | namespace CurrentThread{ 7 | 8 | extern __thread int t_cachedTid; 9 | extern __thread char t_tidString[32]; 10 | extern __thread int t_tidStringLength; 11 | extern __thread int t_cachedPid; 12 | extern __thread const char* t_threadName; 13 | 14 | pid_t gettid(); 15 | void cachedTid(); 16 | 17 | pid_t getpid(); 18 | void cachedPid(); 19 | 20 | inline int tid(){ 21 | if(__builtin_expect(t_cachedTid == 0, 0)){ 22 | cachedTid(); 23 | } 24 | return t_cachedTid; 25 | } 26 | 27 | inline int pid(){ 28 | if(__builtin_expect(t_cachedPid == 0, 0)){ 29 | cachedPid(); 30 | } 31 | return t_cachedPid; 32 | } 33 | 34 | inline const char* tidString(){ 35 | return t_tidString; 36 | } 37 | 38 | inline int tidStringLength(){ 39 | return t_tidStringLength; 40 | } 41 | 42 | inline const char* name(){ 43 | return t_threadName; 44 | } 45 | 46 | 47 | } 48 | 49 | #endif 50 | 51 | -------------------------------------------------------------------------------- /v2/src/net/CurrentThread.h: -------------------------------------------------------------------------------- 1 | #ifndef _CURRENT_THREAD_H 2 | #define _CURRENT_THREAD_H 3 | 4 | #include 5 | 6 | namespace CurrentThread{ 7 | 8 | extern __thread int t_cachedTid; 9 | extern __thread char t_tidString[32]; 10 | extern __thread int t_tidStringLength; 11 | extern __thread int t_cachedPid; 12 | extern __thread const char* t_threadName; 13 | 14 | pid_t gettid(); 15 | void cachedTid(); 16 | 17 | pid_t getpid(); 18 | void cachedPid(); 19 | 20 | inline int tid(){ 21 | if(__builtin_expect(t_cachedTid == 0, 0)){ 22 | cachedTid(); 23 | } 24 | return t_cachedTid; 25 | } 26 | 27 | inline int pid(){ 28 | if(__builtin_expect(t_cachedPid == 0, 0)){ 29 | cachedPid(); 30 | } 31 | return t_cachedPid; 32 | } 33 | 34 | inline const char* tidString(){ 35 | return t_tidString; 36 | } 37 | 38 | inline int tidStringLength(){ 39 | return t_tidStringLength; 40 | } 41 | 42 | inline const char* name(){ 43 | return t_threadName; 44 | } 45 | 46 | 47 | } 48 | 49 | #endif 50 | 51 | -------------------------------------------------------------------------------- /v2/src/app/http/HttpContext.h: -------------------------------------------------------------------------------- 1 | #ifndef HTTPCONTEXT_H 2 | #define HTTPCONTEXT_H 3 | 4 | #include "HttpRequest.h" 5 | 6 | class Buffer; 7 | 8 | class HttpContext{ 9 | public: 10 | enum HttpRequestParseState{ 11 | kExpectRequestLine, 12 | kExpectHeaders, 13 | kExpectBody, 14 | kGotAll 15 | }; 16 | 17 | HttpContext() 18 | :state_(kExpectRequestLine){ 19 | 20 | } 21 | 22 | // use default c-ctor && operator= as data members are well-defined 23 | 24 | bool parseRequest(Buffer* buf, Timestamp receiveTime); 25 | 26 | bool isGotAll() const{ 27 | return state_ == kGotAll; 28 | } 29 | 30 | void reset(){ 31 | state_ = kExpectRequestLine; 32 | HttpRequest dummy; 33 | request_.swap(dummy); 34 | } 35 | 36 | const HttpRequest& getRequest() const{ 37 | return request_; 38 | } 39 | HttpRequest& getRequest(){ 40 | return request_; 41 | } 42 | 43 | private: 44 | bool processRequestLine(const char* begin, const char* end); 45 | 46 | HttpRequestParseState state_; 47 | HttpRequest request_; 48 | }; 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /v1/muduo++/06/Callbacks.h: -------------------------------------------------------------------------------- 1 | #ifndef _CALLBACKS_H_ 2 | #define _CALLBACKS_H_ 3 | 4 | #include 5 | #include 6 | #include "Timestamp.h" 7 | #include "Buffer.h" 8 | 9 | typedef std::function TimerCallback; 10 | 11 | // The only one that uses shared_ptr 12 | class TcpConnection; 13 | typedef std::shared_ptr TcpConnectionPtr; 14 | 15 | typedef std::function ConnectionCallback; 16 | typedef std::function CloseCallback; 17 | // implementation in TcpConnetcion.cc in muduo project 18 | void defaultConnectionCallback(const TcpConnectionPtr& conn); 19 | 20 | typedef std::function MessageCallback; 21 | void defaultMessageCallback(const TcpConnectionPtr& conn, Buffer*, int n); 22 | 23 | // get_pointer as it be named 24 | template 25 | T* get_pointer(T* p){ 26 | return p; 27 | } 28 | 29 | template 30 | T* get_pointer(std::unique_ptr const& p){ 31 | return p.get(); 32 | } 33 | 34 | template 35 | T* get_pointer(std::shared_ptr const& p){ 36 | return p.get(); 37 | } 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /v3/src/base/Condition.h: -------------------------------------------------------------------------------- 1 | #ifndef _CONDITION_H_ 2 | #define _CONDITION_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include "notCopyable.h" 9 | #include "Mutex.h" 10 | 11 | 12 | class Condition: public notCopyable{ 13 | public: 14 | explicit Condition(MutexLock& mutex) 15 | :mutex_(mutex){ 16 | pthread_cond_init(&pcond_, NULL); 17 | } 18 | 19 | ~Condition(){ 20 | pthread_cond_destroy(&pcond_); 21 | } 22 | 23 | void wait(){ 24 | pthread_cond_wait(&pcond_, mutex_.getMutex()); 25 | } 26 | 27 | bool waitForSeconds(int seconds){ 28 | struct timespec relativetime; 29 | clock_gettime(CLOCK_MONOTONIC, &relativetime); 30 | relativetime.tv_sec += static_cast(seconds); 31 | return ETIMEDOUT == pthread_cond_timedwait(&pcond_, mutex_.getMutex(), &relativetime); 32 | } 33 | 34 | void notify(){ 35 | pthread_cond_signal(&pcond_); 36 | } 37 | 38 | void notifyAll(){ 39 | pthread_cond_broadcast(&pcond_); 40 | } 41 | 42 | private: 43 | MutexLock& mutex_; 44 | pthread_cond_t pcond_; 45 | }; 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /v2/src/app/http/HttpResponse.cpp: -------------------------------------------------------------------------------- 1 | #include "HttpResponse.h" 2 | #include "../../net/Buffer.h" 3 | 4 | #include 5 | 6 | // take http response message format as reference 7 | void HttpResponse::appendToBuffer(Buffer* output) const{ 8 | StringPiece crlf("\r\n"); 9 | char buf[32]; 10 | snprintf(buf, sizeof buf, "HTTP/1.1 %d", statusCode_); 11 | output->append(StringPiece(buf)); 12 | output->append(statusMessage_); 13 | output->append(crlf); 14 | 15 | if(closeConnection_){ 16 | output->append(StringPiece("Connection: close\r\n")); 17 | } 18 | else{ 19 | snprintf(buf, sizeof buf, "Content-Length: %zd\r\n", static_cast(body_.size())); 20 | output->append(StringPiece(buf)); 21 | output->append(StringPiece("Connection: Keep-Alive\r\n")); 22 | } 23 | 24 | for(std::map::const_iterator it = headers_.begin(); it != headers_.end(); it++){ 25 | output->append(it->first); 26 | output->append(StringPiece(": ")); 27 | output->append(it->second); 28 | output->append(crlf); 29 | } 30 | 31 | output->append(crlf); 32 | output->append(body_); 33 | } 34 | -------------------------------------------------------------------------------- /v3/README.md: -------------------------------------------------------------------------------- 1 | ## suika v3特性概述 2 | 3 | ### (已完成) 4 | 5 | * 基于对象编程,暴露一个具体类,以注册回调函数实现控制反转。 6 | 避免面向对象方式暴露一个抽象基类,用户需以继承方式承担虚指针与虚表开销,以及面对多重继承、可扩展性不足等问题。 7 | * 事件驱动架构(one event loop per thread) + Thread Pool。 8 | * 主eventloop线程 + I/O worker eventloop线程 + 基于阻塞队列的任务队列计算线程 + 四缓冲异步日志线程。 9 | 单进程多线程设计,提升并发性。发挥多核性能,降低延迟。 10 | 11 | * 内存分配 12 | * 对资源使用预分配策略,进行池化处理,设计连接池(参考Nginx连接池设计),避免因频繁内存分配影响性能。 13 | * 数据拷贝 14 | * Buffer的特别设计,参照Redis的SDS字符串设计,使用Buffer chain,并对其使用scatter I/O,减少系统调用(read(), size(), realloc()等)次数。 15 | * 使用C++ 11的移动语义,避免拷贝。 16 | * 使用epoll LT,较selet与poll减少一次由用户空间向内核空间的拷贝。 17 | 18 | * 上下文切换 19 | * 分工明确的多线程设计 + EventLoop::runInLoop() + EventLoop::queueInLoop()避免线程上下文切换开销。 20 | * 锁竞争 21 | * 通过逻辑与设计(master + workers + others线程)减少锁的使用,且仅使用NPTL的互斥锁mutex。 22 | 对于需要磁盘写入的日志线程,采取异步形式,减少对计算及I/O线程的影响。 23 | * 使用Copy-on-Write等手法尽可能的缩小临界区(EventLoop::doPendingFunctors()等)。 24 | 25 | 26 | ### (待完成) 27 | 28 | * 多阶段请求异步处理。(类Nginx实现) 29 | * 内存池的使用。(类Nginx实现) 30 | * 使用更多针对性能优化的数据结构。(类Redis实现) 31 | 32 | * 模块化设计。(类Nginx实现) 33 | * 网络库与应用逻辑分离设计,提高可重用性。 34 | 35 | 36 | ### (已放弃方案) 37 | * Epoll ET的使用。(性能原因,见[v3性能评估.md](https://github.com/ChyauAng/suika/blob/master/v3/v3性能评估.md)) 38 | -------------------------------------------------------------------------------- /v2/src/app/http/HttpServer.h: -------------------------------------------------------------------------------- 1 | #ifndef HTTPSERVER_H 2 | #define HTTPSERVER_H 3 | 4 | #include "../../net/TcpServer.h" 5 | 6 | #include 7 | 8 | class HttpRequest; 9 | class HttpResponse; 10 | 11 | class HttpServer: public notCopyable{ 12 | public: 13 | typedef std::function HttpCallback; 14 | 15 | HttpServer(EventLoop* loop, const InetAddress& listenAddr, const std::string& name); 16 | ~HttpServer(); 17 | 18 | EventLoop* getLoop() const{ 19 | return server_.getLoop(); 20 | } 21 | 22 | void setHttpCallback(const HttpCallback& cb){ 23 | httpCallback_ = cb; 24 | } 25 | 26 | void setThreadNum(int numThreads){ 27 | server_.setThreadNum(numThreads); 28 | } 29 | 30 | void start(); 31 | 32 | 33 | private: 34 | TcpServer server_; 35 | HttpCallback httpCallback_; 36 | 37 | void onConnection(const TcpConnectionPtr& conn); 38 | void onMessage(const TcpConnectionPtr& conn, Buffer* buf, Timestamp receiveTime); 39 | void onRequest(const TcpConnectionPtr&, const HttpRequest&); 40 | 41 | HttpServer(const HttpServer&); 42 | HttpServer& operator=(const HttpServer&); 43 | 44 | }; 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /v3/src/base/Buffer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "Buffer.h" 5 | #include "SocketsOps.h" 6 | 7 | template 8 | inline To implicit_cast(From const& f){ 9 | return f; 10 | } 11 | 12 | const char Buffer::kCRLF[] = "\r\n"; 13 | 14 | const size_t Buffer::KInitialSize; 15 | 16 | ssize_t Buffer::readFd(int fd, int* savedErrno){ 17 | char extrabuf[65536]; // when buffer space is not enough, write in the extrabuf first, then write to the buffer, to save an ioctl()/FIONREAD call cost 18 | struct iovec vec[2]; 19 | const size_t writable = writableBytes(); 20 | vec[0].iov_base = buf_ + writerIndex_; 21 | vec[0].iov_len = writable; 22 | vec[1].iov_base = extrabuf; 23 | vec[1].iov_len = 65536; 24 | const int iovcnt = (writable < sizeof extrabuf)? 2 : 1; 25 | const ssize_t n = sockets::readv(fd, vec, iovcnt); // multi-buffer read operation 26 | if(n < 0){ 27 | *savedErrno = errno; 28 | } 29 | else if(implicit_cast(n) <= writable){ 30 | writerIndex_ += n; 31 | } 32 | else{ 33 | writerIndex_ = len_; 34 | append(extrabuf, n - writable); 35 | } 36 | return n; 37 | } 38 | 39 | 40 | -------------------------------------------------------------------------------- /v3/src/base/Timer.h: -------------------------------------------------------------------------------- 1 | #ifndef _TIMER_H_ 2 | #define _TIMER_H_ 3 | 4 | #include "Callbacks.h" 5 | #include "notCopyable.h" 6 | #include "Timestamp.h" 7 | 8 | 9 | class Timer: public notCopyable{ 10 | public: 11 | Timer(TimerCallback&& cb, Timestamp when, double interval) 12 | :repeat_(interval > 0.0), 13 | callback_(std::move(cb)), 14 | expiration_(when), 15 | interval_(interval), 16 | sequence_(__sync_fetch_and_add(&s_numCreated_, 1) + 1){ 17 | 18 | } 19 | // callback 20 | void run() const{ 21 | callback_(); 22 | } 23 | 24 | Timestamp expiration() const{ 25 | return expiration_; 26 | } 27 | bool repeat() const{ 28 | return repeat_; 29 | } 30 | int64_t sequence() const{ 31 | return sequence_; 32 | } 33 | static int64_t numCreated(){ 34 | return __sync_val_compare_and_swap(&s_numCreated_, 0, 0); 35 | } 36 | 37 | void restart(Timestamp now); 38 | 39 | private: 40 | const bool repeat_; 41 | 42 | const int sequence_; 43 | 44 | const TimerCallback callback_; 45 | Timestamp expiration_; 46 | const double interval_; 47 | 48 | static int s_numCreated_; 49 | }; 50 | 51 | #endif 52 | 53 | -------------------------------------------------------------------------------- /v1/muduo++/02/Channel.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "EventLoop.h" 3 | #include "Channel.h" 4 | #include 5 | 6 | const int Channel::kNoneEvent = 0; 7 | const int Channel::kReadEvent = POLLIN |POLLPRI; 8 | const int Channel::kWriteEvent = POLLOUT; 9 | 10 | Channel::Channel(EventLoop* loop, int fd) 11 | :loop_(loop), 12 | fd_(fd), 13 | events_(0), 14 | revents_(0), 15 | index_(-1), 16 | addedToLoop_(false){ 17 | 18 | } 19 | 20 | void Channel::update(){ 21 | addedToLoop_ = true; 22 | loop_->updateChannel(this); 23 | } 24 | 25 | void Channel::remove(){ 26 | assert(isNoneEvent()); 27 | addedToLoop_ = false; 28 | loop_->removeChannel(this); 29 | } 30 | 31 | void Channel::handleEvent(){ 32 | if(revents_ & POLLNVAL){ 33 | // LOG_WARN 34 | } 35 | 36 | if(revents_ & (POLLERR | POLLNVAL)){ 37 | if(errorCallback_){ 38 | errorCallback_(); 39 | } 40 | } 41 | 42 | if(revents_ & (POLLIN | POLLPRI | POLLRDHUP)){ 43 | if(readCallback_){ 44 | readCallback_(); 45 | } 46 | } 47 | 48 | if(revents_ & (POLLOUT)){ 49 | if(writeCallback_){ 50 | writeCallback_(); 51 | } 52 | } 53 | } 54 | 55 | -------------------------------------------------------------------------------- /v1/muduo++/03/Channel.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "EventLoop.h" 3 | #include "Channel.h" 4 | #include 5 | 6 | const int Channel::kNoneEvent = 0; 7 | const int Channel::kReadEvent = POLLIN |POLLPRI; 8 | const int Channel::kWriteEvent = POLLOUT; 9 | 10 | Channel::Channel(EventLoop* loop, int fd) 11 | :loop_(loop), 12 | fd_(fd), 13 | events_(0), 14 | revents_(0), 15 | index_(-1), 16 | addedToLoop_(false){ 17 | 18 | } 19 | 20 | void Channel::update(){ 21 | addedToLoop_ = true; 22 | loop_->updateChannel(this); 23 | } 24 | 25 | void Channel::remove(){ 26 | assert(isNoneEvent()); 27 | addedToLoop_ = false; 28 | loop_->removeChannel(this); 29 | } 30 | 31 | void Channel::handleEvent(){ 32 | if(revents_ & POLLNVAL){ 33 | // LOG_WARN 34 | } 35 | 36 | if(revents_ & (POLLERR | POLLNVAL)){ 37 | if(errorCallback_){ 38 | errorCallback_(); 39 | } 40 | } 41 | 42 | if(revents_ & (POLLIN | POLLPRI | POLLRDHUP)){ 43 | if(readCallback_){ 44 | readCallback_(); 45 | } 46 | } 47 | 48 | if(revents_ & (POLLOUT)){ 49 | if(writeCallback_){ 50 | writeCallback_(); 51 | } 52 | } 53 | } 54 | 55 | -------------------------------------------------------------------------------- /v1/muduo++/04/Channel.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "EventLoop.h" 3 | #include "Channel.h" 4 | #include 5 | 6 | const int Channel::kNoneEvent = 0; 7 | const int Channel::kReadEvent = POLLIN |POLLPRI; 8 | const int Channel::kWriteEvent = POLLOUT; 9 | 10 | Channel::Channel(EventLoop* loop, int fd) 11 | :loop_(loop), 12 | fd_(fd), 13 | events_(0), 14 | revents_(0), 15 | index_(-1), 16 | addedToLoop_(false){ 17 | 18 | } 19 | 20 | void Channel::update(){ 21 | addedToLoop_ = true; 22 | loop_->updateChannel(this); 23 | } 24 | 25 | void Channel::remove(){ 26 | assert(isNoneEvent()); 27 | addedToLoop_ = false; 28 | loop_->removeChannel(this); 29 | } 30 | 31 | void Channel::handleEvent(){ 32 | if(revents_ & POLLNVAL){ 33 | // LOG_WARN 34 | } 35 | 36 | if(revents_ & (POLLERR | POLLNVAL)){ 37 | if(errorCallback_){ 38 | errorCallback_(); 39 | } 40 | } 41 | 42 | if(revents_ & (POLLIN | POLLPRI | POLLRDHUP)){ 43 | if(readCallback_){ 44 | readCallback_(); 45 | } 46 | } 47 | 48 | if(revents_ & (POLLOUT)){ 49 | if(writeCallback_){ 50 | writeCallback_(); 51 | } 52 | } 53 | } 54 | 55 | -------------------------------------------------------------------------------- /v3/src/EventLoopThread.cpp: -------------------------------------------------------------------------------- 1 | #include "EventLoopThread.h" 2 | #include "EventLoop.h" 3 | #include 4 | #include 5 | #include 6 | 7 | EventLoopThread::EventLoopThread(const ThreadInitCallback& cb) 8 | :existing_(false), 9 | loop_(NULL), 10 | callback_(cb), 11 | mutex_(), 12 | cond_(mutex_), 13 | thread_(std::bind(&EventLoopThread::threadFunc, this)){ 14 | 15 | } 16 | 17 | EventLoopThread::~EventLoopThread(){ 18 | existing_ = true; 19 | if(loop_ != NULL){ 20 | loop_->quit(); // not 100% thread safe 21 | thread_.join(); 22 | } 23 | } 24 | 25 | EventLoop* EventLoopThread::startLoop(){ 26 | assert(!thread_.started()); 27 | thread_.start(); 28 | { 29 | MutexLockGuard lock(mutex_); 30 | while(loop_ == NULL){ 31 | cond_.wait(); 32 | 33 | } 34 | } 35 | return loop_; 36 | } 37 | 38 | void EventLoopThread::threadFunc(){ 39 | EventLoop loop; 40 | if(callback_){ 41 | callback_(&loop); 42 | } 43 | { 44 | MutexLockGuard lock(mutex_); 45 | loop_= &loop; 46 | cond_.notify(); 47 | } 48 | loop.initContextPool(); 49 | loop.loop(); 50 | loop_ = NULL; 51 | } 52 | 53 | -------------------------------------------------------------------------------- /v1/muduo++/05/TcpServer.h: -------------------------------------------------------------------------------- 1 | #ifndef _TCPSERVER_H_ 2 | #define _TCPSERVER_H_ 3 | 4 | #include "Atomic.h" 5 | #include "TcpConnection.h" 6 | #include "notCopyable.h" 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | class Acceptor; 13 | class EventLoop; 14 | 15 | class TcpServer: public notCopyable{ 16 | public: 17 | TcpServer(EventLoop* loop, const InetAddress& listenAddr, const std::string& nameArg); 18 | ~TcpServer(); 19 | 20 | void start(); 21 | 22 | void setConnectionCallback(const ConnectionCallback& cb){ 23 | connectionCallback_ = cb; 24 | } 25 | 26 | void setMessageCallback(const MessageCallback& cb){ 27 | messageCallback_ = cb; 28 | } 29 | 30 | private: 31 | void newConnection(int sockfd, const InetAddress& peerAddr); 32 | void removeConnection(const TcpConnectionPtr& conn); 33 | typedef std::map ConnectionMap; 34 | 35 | EventLoop* loop_; 36 | const std::string name_; 37 | std::unique_ptr acceptor_; 38 | ConnectionCallback connectionCallback_; 39 | MessageCallback messageCallback_; 40 | AtomicInt32 started_; 41 | int nextConnId_; 42 | ConnectionMap connections_; 43 | }; 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /v1/muduo++/06/TcpServer.h: -------------------------------------------------------------------------------- 1 | #ifndef _TCPSERVER_H_ 2 | #define _TCPSERVER_H_ 3 | 4 | #include "Atomic.h" 5 | #include "TcpConnection.h" 6 | #include "notCopyable.h" 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | class Acceptor; 13 | class EventLoop; 14 | 15 | class TcpServer: public notCopyable{ 16 | public: 17 | TcpServer(EventLoop* loop, const InetAddress& listenAddr, const std::string& nameArg); 18 | ~TcpServer(); 19 | 20 | void start(); 21 | 22 | void setConnectionCallback(const ConnectionCallback& cb){ 23 | connectionCallback_ = cb; 24 | } 25 | 26 | void setMessageCallback(const MessageCallback& cb){ 27 | messageCallback_ = cb; 28 | } 29 | 30 | private: 31 | void newConnection(int sockfd, const InetAddress& peerAddr); 32 | void removeConnection(const TcpConnectionPtr& conn); 33 | typedef std::map ConnectionMap; 34 | 35 | EventLoop* loop_; 36 | const std::string name_; 37 | std::unique_ptr acceptor_; 38 | ConnectionCallback connectionCallback_; 39 | MessageCallback messageCallback_; 40 | AtomicInt32 started_; 41 | int nextConnId_; 42 | ConnectionMap connections_; 43 | }; 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /v3/src/EPollPoller.h: -------------------------------------------------------------------------------- 1 | #ifndef _EPOLLPOLLER_H_ 2 | #define _EPOLLPOLLER_H_ 3 | 4 | #include 5 | #include 6 | 7 | #include "EventLoop.h" 8 | #include "base/notCopyable.h" 9 | 10 | class Channel; 11 | 12 | class EPollPoller: public notCopyable{ 13 | public: 14 | typedef std::vector ChannelList; 15 | 16 | EPollPoller(EventLoop* loop); 17 | ~EPollPoller(); 18 | 19 | void poll(int timeoutMs, ChannelList* activeChannels); 20 | void updateChannel(Channel* channel); 21 | void removeChannel(Channel* channel); 22 | 23 | bool hasChannel(Channel* channel) const; 24 | 25 | void assertInLoopThread() const{ 26 | ownerLoop_->assertInLoopThread(); 27 | } 28 | 29 | private: 30 | typedef std::map ChannelMap; 31 | typedef std::vector EventList; 32 | 33 | void fillActiveChannels(int numEvents, ChannelList* activeChannels) const; 34 | void update(int operation, Channel* channel); 35 | 36 | static const int kInitEventListSize = 16; 37 | static const char* operationToString(int op); 38 | 39 | int epollfd_; 40 | 41 | EventLoop* ownerLoop_; 42 | 43 | EventList events_; 44 | 45 | ChannelMap channels_; 46 | }; 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /v1/muduo++/02/Timer.h: -------------------------------------------------------------------------------- 1 | #ifndef _TIMER_H_ 2 | #define _TIMER_H_ 3 | 4 | #include "notCopyable.h" 5 | #include "Timestamp.h" 6 | #include "Atomic.h" 7 | #include "Callbacks.h" 8 | 9 | 10 | class Timer: public notCopyable{ 11 | public: 12 | Timer(const TimerCallback& cb, Timestamp when, double interval) 13 | :callback_(cb), 14 | expiration_(when), 15 | interval_(interval), 16 | repeat_(interval > 0.0), 17 | sequence_(s_numCreated_.incrementAndGet()){ 18 | 19 | } 20 | 21 | // callback 22 | void run() const{ 23 | callback_(); 24 | } 25 | 26 | // get functions 27 | Timestamp expiration() const{ 28 | return expiration_; 29 | } 30 | bool repeat() const{ 31 | return repeat_; 32 | } 33 | int64_t sequence() const{ 34 | return sequence_; 35 | } 36 | static int64_t numCreated(){ 37 | return s_numCreated_.get(); 38 | } 39 | 40 | 41 | void restart(Timestamp now); 42 | 43 | 44 | private: 45 | const TimerCallback callback_; 46 | Timestamp expiration_; 47 | const double interval_; 48 | const bool repeat_; 49 | const int64_t sequence_; 50 | 51 | static AtomicInt64 s_numCreated_; 52 | }; 53 | 54 | #endif 55 | 56 | -------------------------------------------------------------------------------- /v1/muduo++/03/Timer.h: -------------------------------------------------------------------------------- 1 | #ifndef _TIMER_H_ 2 | #define _TIMER_H_ 3 | 4 | #include "notCopyable.h" 5 | #include "Timestamp.h" 6 | #include "Atomic.h" 7 | #include "Callbacks.h" 8 | 9 | 10 | class Timer: public notCopyable{ 11 | public: 12 | Timer(const TimerCallback& cb, Timestamp when, double interval) 13 | :callback_(cb), 14 | expiration_(when), 15 | interval_(interval), 16 | repeat_(interval > 0.0), 17 | sequence_(s_numCreated_.incrementAndGet()){ 18 | 19 | } 20 | 21 | // callback 22 | void run() const{ 23 | callback_(); 24 | } 25 | 26 | // get functions 27 | Timestamp expiration() const{ 28 | return expiration_; 29 | } 30 | bool repeat() const{ 31 | return repeat_; 32 | } 33 | int64_t sequence() const{ 34 | return sequence_; 35 | } 36 | static int64_t numCreated(){ 37 | return s_numCreated_.get(); 38 | } 39 | 40 | 41 | void restart(Timestamp now); 42 | 43 | 44 | private: 45 | const TimerCallback callback_; 46 | Timestamp expiration_; 47 | const double interval_; 48 | const bool repeat_; 49 | const int64_t sequence_; 50 | 51 | static AtomicInt64 s_numCreated_; 52 | }; 53 | 54 | #endif 55 | 56 | -------------------------------------------------------------------------------- /v1/muduo++/04/Timer.h: -------------------------------------------------------------------------------- 1 | #ifndef _TIMER_H_ 2 | #define _TIMER_H_ 3 | 4 | #include "notCopyable.h" 5 | #include "Timestamp.h" 6 | #include "Atomic.h" 7 | #include "Callbacks.h" 8 | 9 | 10 | class Timer: public notCopyable{ 11 | public: 12 | Timer(const TimerCallback& cb, Timestamp when, double interval) 13 | :callback_(cb), 14 | expiration_(when), 15 | interval_(interval), 16 | repeat_(interval > 0.0), 17 | sequence_(s_numCreated_.incrementAndGet()){ 18 | 19 | } 20 | 21 | // callback 22 | void run() const{ 23 | callback_(); 24 | } 25 | 26 | // get functions 27 | Timestamp expiration() const{ 28 | return expiration_; 29 | } 30 | bool repeat() const{ 31 | return repeat_; 32 | } 33 | int64_t sequence() const{ 34 | return sequence_; 35 | } 36 | static int64_t numCreated(){ 37 | return s_numCreated_.get(); 38 | } 39 | 40 | 41 | void restart(Timestamp now); 42 | 43 | 44 | private: 45 | const TimerCallback callback_; 46 | Timestamp expiration_; 47 | const double interval_; 48 | const bool repeat_; 49 | const int64_t sequence_; 50 | 51 | static AtomicInt64 s_numCreated_; 52 | }; 53 | 54 | #endif 55 | 56 | -------------------------------------------------------------------------------- /v1/muduo++/05/Timer.h: -------------------------------------------------------------------------------- 1 | #ifndef _TIMER_H_ 2 | #define _TIMER_H_ 3 | 4 | #include "notCopyable.h" 5 | #include "Timestamp.h" 6 | #include "Atomic.h" 7 | #include "Callbacks.h" 8 | 9 | 10 | class Timer: public notCopyable{ 11 | public: 12 | Timer(const TimerCallback& cb, Timestamp when, double interval) 13 | :callback_(cb), 14 | expiration_(when), 15 | interval_(interval), 16 | repeat_(interval > 0.0), 17 | sequence_(s_numCreated_.incrementAndGet()){ 18 | 19 | } 20 | 21 | // callback 22 | void run() const{ 23 | callback_(); 24 | } 25 | 26 | // get functions 27 | Timestamp expiration() const{ 28 | return expiration_; 29 | } 30 | bool repeat() const{ 31 | return repeat_; 32 | } 33 | int64_t sequence() const{ 34 | return sequence_; 35 | } 36 | static int64_t numCreated(){ 37 | return s_numCreated_.get(); 38 | } 39 | 40 | 41 | void restart(Timestamp now); 42 | 43 | 44 | private: 45 | const TimerCallback callback_; 46 | Timestamp expiration_; 47 | const double interval_; 48 | const bool repeat_; 49 | const int64_t sequence_; 50 | 51 | static AtomicInt64 s_numCreated_; 52 | }; 53 | 54 | #endif 55 | 56 | --------------------------------------------------------------------------------