1.patch 2.2 KB
Newer Older
羽飞's avatar
羽飞 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
diff --git a/src/observer/CMakeLists.txt b/src/observer/CMakeLists.txt
index 1e156f2..37f5f86 100644
--- a/src/observer/CMakeLists.txt
+++ b/src/observer/CMakeLists.txt
@@ -24,7 +24,7 @@ FOREACH (F ${ALL_SRC})
 
 ENDFOREACH (F)
 
-SET(LIBRARIES common pthread dl event jsoncpp)
+SET(LIBRARIES common pthread dl event_pthreads event jsoncpp)
 
 # 指定目标文件位置
 SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/../../bin)
diff --git a/src/observer/main.cpp b/src/observer/main.cpp
index 4fff6da..112255e 100644
--- a/src/observer/main.cpp
+++ b/src/observer/main.cpp
@@ -137,8 +137,6 @@ void *quit_thread_func(void *_signum)
   LOG_INFO("Receive signal: %ld", signum);
   if (g_server) {
     g_server->shutdown();
-    delete g_server;
-    g_server = nullptr;
   }
   return nullptr;
 }
@@ -169,4 +167,6 @@ int main(int argc, char **argv)
   LOG_INFO("Server stopped");
 
   cleanup();
+
+  delete g_server;
 }
diff --git a/src/observer/net/server.cpp b/src/observer/net/server.cpp
index e87ad0d..2c6cef6 100644
--- a/src/observer/net/server.cpp
+++ b/src/observer/net/server.cpp
@@ -26,6 +26,7 @@ See the Mulan PSL v2 for more details. */
 #include <sys/types.h>
 #include <sys/un.h>
 #include <unistd.h>
+#include <event2/thread.h>
 
 #include "common/lang/mutex.h"
 #include "common/log/log.h"
@@ -411,6 +412,7 @@ int Server::start_unix_socket_server()
 
 int Server::serve()
 {
+  evthread_use_pthreads();
   event_base_ = event_base_new();
   if (event_base_ == nullptr) {
     LOG_ERROR("Failed to create event base, %s.", strerror(errno));
@@ -425,19 +427,6 @@ int Server::serve()
 
   event_base_dispatch(event_base_);
 
-  return 0;
-}
-
-void Server::shutdown()
-{
-  LOG_INFO("Server shutting down");
-
-  // cleanup
-  struct timeval exit_time;
-  gettimeofday(&exit_time, nullptr);
-  exit_time.tv_sec += 10;
-  event_base_loopexit(event_base_, &exit_time);
-
   if (listen_ev_ != nullptr) {
     event_del(listen_ev_);
     event_free(listen_ev_);
@@ -451,4 +440,16 @@ void Server::shutdown()
 
   started_ = false;
   LOG_INFO("Server quit");
+  return 0;
+}
+
+void Server::shutdown()
+{
+  LOG_INFO("Server shutting down");
+
+  // cleanup
+  if (event_base_ != nullptr && started_) {
+    started_ = false;
+    event_base_loopexit(event_base_, nullptr);
+  }
 }