Skip to content

Commit 09921ff

Browse files
chore: Use unique_ptr for WorkerThread#worker (apache#703)
Co-authored-by: Twice <[email protected]>
1 parent 4657769 commit 09921ff

File tree

3 files changed

+11
-14
lines changed

3 files changed

+11
-14
lines changed

src/server.cc

+5-9
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,18 @@ Server::Server(Engine::Storage *storage, Config *config) :
5454
cluster_ = std::unique_ptr<Cluster>(new Cluster(this, config_->binds, config_->port));
5555

5656
for (int i = 0; i < config->workers; i++) {
57-
auto worker = new Worker(this, config);
57+
auto worker = Util::MakeUnique<Worker>(this, config);
5858
// multiple workers can't listen to the same unix socket, so
5959
// listen unix socket only from a single worker - the first one
6060
if (!config->unixsocket.empty() && i == 0) {
6161
Status s = worker->ListenUnixSocket(config->unixsocket, config->unixsocketperm, config->backlog);
6262
if (!s.IsOK()) {
6363
LOG(ERROR) << "[server] Failed to listen on unix socket: "<< config->unixsocket
6464
<< ", encounter error: " << s.Msg();
65-
delete worker;
6665
exit(1);
6766
}
6867
}
69-
worker_threads_.emplace_back(new WorkerThread(worker));
68+
worker_threads_.emplace_back(Util::MakeUnique<WorkerThread>(std::move(worker)));
7069
}
7170
AdjustOpenFilesLimit();
7271
slow_log_.SetMaxEntries(config->slowlog_max_len);
@@ -79,9 +78,6 @@ Server::Server(Engine::Storage *storage, Config *config) :
7978
}
8079

8180
Server::~Server() {
82-
for (const auto &worker_thread : worker_threads_) {
83-
delete worker_thread;
84-
}
8581
for (const auto &iter : conn_ctxs_) {
8682
delete iter.first;
8783
}
@@ -121,7 +117,7 @@ Status Server::Start() {
121117
}
122118
}
123119

124-
for (const auto worker : worker_threads_) {
120+
for (const auto& worker : worker_threads_) {
125121
worker->Start();
126122
}
127123
task_runner_.Start();
@@ -175,7 +171,7 @@ Status Server::Start() {
175171
void Server::Stop() {
176172
stop_ = true;
177173
if (replication_thread_) replication_thread_->Stop();
178-
for (const auto worker : worker_threads_) {
174+
for (const auto& worker : worker_threads_) {
179175
worker->Stop();
180176
}
181177
DisconnectSlaves();
@@ -184,7 +180,7 @@ void Server::Stop() {
184180
}
185181

186182
void Server::Join() {
187-
for (const auto worker : worker_threads_) {
183+
for (const auto& worker : worker_threads_) {
188184
worker->Join();
189185
}
190186
task_runner_.Join();

src/server.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ class Server {
261261
std::thread cron_thread_;
262262
std::thread compaction_checker_thread_;
263263
TaskRunner task_runner_;
264-
std::vector<WorkerThread *> worker_threads_;
264+
std::vector<std::unique_ptr<WorkerThread>> worker_threads_;
265265
std::unique_ptr<ReplicationThread> replication_thread_;
266266
};
267267

src/worker.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <memory>
2727
#include <thread>
2828
#include <string>
29+
#include <utility>
2930
#include <vector>
3031
#include <event2/buffer.h>
3132
#include <event2/bufferevent.h>
@@ -89,16 +90,16 @@ class Worker {
8990

9091
class WorkerThread {
9192
public:
92-
explicit WorkerThread(Worker *worker) : worker_(worker) {}
93-
~WorkerThread() { delete worker_; }
93+
explicit WorkerThread(std::unique_ptr<Worker> worker) : worker_(std::move(worker)) {}
94+
~WorkerThread() = default;
9495
WorkerThread(const WorkerThread&) = delete;
9596
WorkerThread(WorkerThread&&) = delete;
96-
Worker *GetWorker() { return worker_; }
97+
Worker *GetWorker() { return worker_.get(); }
9798
void Start();
9899
void Stop();
99100
void Join();
100101

101102
private:
102103
std::thread t_;
103-
Worker *worker_;
104+
std::unique_ptr<Worker> worker_;
104105
};

0 commit comments

Comments
 (0)