Skip to content

Commit 1ad7f69

Browse files
loveyacperokochayang
authored andcommitted
delete useless code: event loop group and config parser
1 parent 7e27471 commit 1ad7f69

File tree

11 files changed

+79
-383
lines changed

11 files changed

+79
-383
lines changed

net/Application.cc

Lines changed: 58 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
#include <cstring>
44
#include <cstdio>
55

6+
#include <memory>
7+
#include <mutex>
8+
#include <condition_variable>
9+
610
#include "util/Util.h"
711
#include "Application.h"
812
#include "AnanasLogo.h"
913
#include "Socket.h"
10-
#include "EventLoopGroup.h"
1114
#include "AnanasDebug.h"
1215

1316
static void SignalHandler(int num) {
@@ -24,11 +27,6 @@ static void InitSignal() {
2427
// ignore sigpipe
2528
sig.sa_handler = SIG_IGN;
2629
sigaction(SIGPIPE, &sig, NULL);
27-
28-
#ifdef ANANAS_LOGO
29-
// logo
30-
printf("%s\n", ananas::internal::logo);
31-
#endif
3230
}
3331

3432

@@ -44,12 +42,14 @@ Application& Application::Instance() {
4442

4543
void Application::SetNumOfWorker(size_t num) {
4644
assert (state_ == State::eS_None);
47-
workerGroup_->SetNumOfEventLoop(num);
45+
assert (num <= 512);
46+
47+
numLoop_ = num;
4848
}
4949

5050
size_t Application::NumOfWorker() const {
5151
// plus one : the baseLoop
52-
return 1 + workerGroup_->Size();
52+
return 1 + numLoop_;
5353
}
5454

5555
void Application::Run(int ac, char* av[]) {
@@ -71,25 +71,23 @@ void Application::Run(int ac, char* av[]) {
7171
}
7272
}
7373

74-
state_ = State::eS_Started;
75-
workerGroup_->Start();
76-
74+
// start loops in thread pool
75+
_StartWorkers();
7776
BaseLoop()->Run();
7877

79-
baseGroup_->Wait();
80-
printf("Stopped BaseEventLoopGroup ...\n");
78+
printf("Stopped BaseEventLoop...\n");
8179

82-
workerGroup_->Wait();
83-
printf("Stopped WorkerEventLoopGroup...\n");
80+
pool_.JoinAll();
81+
loops_.clear();
82+
numLoop_ = 0;
83+
printf("Stopped WorkerEventLoops...\n");
8484
}
8585

8686
void Application::Exit() {
8787
if (state_ == State::eS_Stopped)
8888
return;
8989

9090
state_ = State::eS_Stopped;
91-
baseGroup_->Stop();
92-
workerGroup_->Stop();
9391
}
9492

9593
bool Application::IsExit() const {
@@ -184,20 +182,55 @@ void Application::Connect(const char* ip,
184182

185183

186184
EventLoop* Application::Next() {
187-
//assert (BaseLoop()->IsInSameLoop());
188-
auto loop = workerGroup_->Next();
189-
if (loop)
190-
return loop;
185+
if (state_ != State::eS_Started)
186+
return BaseLoop();
187+
188+
if (loops_.empty())
189+
return BaseLoop();
191190

192-
return BaseLoop();
191+
auto& loop = loops_[currentLoop_++ % loops_.size()];
192+
return loop.get();
193+
}
194+
195+
void Application::_StartWorkers() {
196+
// only called by main thread
197+
assert (state_ == State::eS_None);
198+
199+
std::mutex mutex;
200+
std::condition_variable cond;
201+
202+
pool_.SetNumOfThreads(numLoop_);
203+
for (size_t i = 0; i < numLoop_; ++i) {
204+
pool_.Execute([this, &mutex, &cond]() {
205+
EventLoop* loop(new EventLoop);
206+
207+
{
208+
std::unique_lock<std::mutex> guard(mutex);
209+
loops_.push_back(std::unique_ptr<EventLoop>(loop));
210+
if (loops_.size() == numLoop_)
211+
cond.notify_one();
212+
}
213+
214+
loop->Run();
215+
});
216+
}
217+
218+
std::unique_lock<std::mutex> guard(mutex);
219+
cond.wait(guard, [this] () {
220+
return loops_.size() == numLoop_;
221+
});
222+
223+
state_ = State::eS_Started;
193224
}
194225

195226
Application::Application() :
196-
baseGroup_(new internal::EventLoopGroup(0)),
197-
base_(baseGroup_.get()),
198-
workerGroup_(new internal::EventLoopGroup(0)),
199227
state_ {State::eS_None} {
200228
InitSignal();
229+
230+
// logo
231+
fprintf(stdout, "%s", "\033[1;36;40m");
232+
printf("%s\n", ananas::internal::logo);
233+
fprintf(stdout, "%s", "\033[0m");
201234
}
202235

203236
void Application::_DefaultBindCallback(bool succ, const SocketAddr& listenAddr) {

net/Application.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,10 @@
1010
#include "Typedefs.h"
1111
#include "Poller.h"
1212
#include "ananas/util/Timer.h"
13+
#include "ananas/util/ThreadPool.h"
1314

1415
///@brief Namespace ananas
1516
namespace ananas {
16-
17-
namespace internal {
18-
class EventLoopGroup;
19-
}
20-
2117
/// @file Application.h
2218

2319
///@brief Abstract for a process.
@@ -137,13 +133,16 @@ class Application {
137133
private:
138134
Application();
139135

140-
// baseGroup_ is empty, just a placeholder container for base_.
141-
std::unique_ptr<internal::EventLoopGroup> baseGroup_;
136+
void _StartWorkers();
142137

143-
// The default loop for accept/connect, or as worker if workerGroup_ is empty
138+
// The default loop for accept/connect, or as worker if empty worker pool
144139
EventLoop base_;
145140

146-
std::unique_ptr<internal::EventLoopGroup> workerGroup_;
141+
// worker thread pool
142+
ThreadPool pool_;
143+
std::vector<std::unique_ptr<EventLoop>> loops_;
144+
size_t numLoop_ {0};
145+
mutable std::atomic<size_t> currentLoop_ {0};
147146

148147
enum class State {
149148
eS_None,

net/EventLoop.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include <thread>
44

55
#include "EventLoop.h"
6-
#include "EventLoopGroup.h"
6+
#include "Application.h"
77

88
#include "Acceptor.h"
99
#include "Connection.h"
@@ -34,8 +34,7 @@ void EventLoop::SetMaxOpenFd(rlim_t maxfdPlus1) {
3434
s_maxOpenFdPlus1 = maxfdPlus1;
3535
}
3636

37-
EventLoop::EventLoop(internal::EventLoopGroup* group) :
38-
group_(group) {
37+
EventLoop::EventLoop() {
3938
assert (!g_thisLoop && "There must be only one EventLoop per thread");
4039
g_thisLoop = this;
4140

@@ -204,12 +203,14 @@ bool EventLoop::Cancel(TimerId id) {
204203
}
205204

206205
void EventLoop::Run() {
206+
assert (this->InThisLoop());
207+
207208
const DurationMs kDefaultPollTime(10);
208209
const DurationMs kMinPollTime(1);
209210

210211
Register(internal::eET_Read, notifier_);
211212

212-
while (!group_->IsStopped()) {
213+
while (!Application::Instance().IsExit()) {
213214
auto timeout = std::min(kDefaultPollTime, timers_.NearestTimer());
214215
timeout = std::max(kMinPollTime, timeout);
215216

net/EventLoop.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ struct SocketAddr;
2020

2121
namespace internal {
2222
class Connector;
23-
class EventLoopGroup;
2423
}
2524

2625
///@brief EventLoop class
@@ -29,9 +28,7 @@ class EventLoopGroup;
2928
class EventLoop : public Scheduler {
3029
public:
3130
///@brief Constructor
32-
///@param group The group belong to
33-
explicit
34-
EventLoop(internal::EventLoopGroup* group);
31+
EventLoop();
3532
~EventLoop();
3633

3734
EventLoop(const EventLoop& ) = delete;
@@ -118,7 +115,7 @@ class EventLoop : public Scheduler {
118115

119116
///@brief Run application
120117
///
121-
/// It's a infinite loop, until belonging EventLoopGroup stopped
118+
/// It's a infinite loop, until Application stopped
122119
void Run();
123120

124121
bool Register(int events, std::shared_ptr<internal::Channel> src);
@@ -155,7 +152,6 @@ class EventLoop : public Scheduler {
155152
private:
156153
bool _Loop(DurationMs timeout);
157154

158-
internal::EventLoopGroup* group_;
159155
std::unique_ptr<internal::Poller> poller_;
160156

161157
std::shared_ptr<internal::PipeChannel> notifier_;

net/EventLoopGroup.cc

Lines changed: 0 additions & 83 deletions
This file was deleted.

net/EventLoopGroup.h

Lines changed: 0 additions & 62 deletions
This file was deleted.

0 commit comments

Comments
 (0)