Pārlūkot izejas kodu

filter-out *_test.c

ithewei 4 gadi atpakaļ
vecāks
revīzija
7c339c47e2
6 mainītis faili ar 59 papildinājumiem un 62 dzēšanām
  1. 2 2
      CMakeLists.txt
  2. 4 3
      Makefile.in
  3. 1 0
      cmake/utils.cmake
  4. 0 2
      evpp/EventLoop.h
  5. 49 45
      evpp/EventLoopThreadPool.h
  6. 3 10
      evpp/EventLoopThreadPool_test.cpp

+ 2 - 2
CMakeLists.txt

@@ -1,6 +1,6 @@
-cmake_minimum_required(VERSION 3.0)
+cmake_minimum_required(VERSION 3.6)
 
-project(hv VERSION 1.20.8)
+project(hv VERSION 1.0.0)
 
 option(BUILD_SHARED "build shared library" ON)
 option(BUILD_STATIC "build static library" ON)

+ 4 - 3
Makefile.in

@@ -193,10 +193,11 @@ endif
 endif
 endif
 
-override SRCS += $(foreach dir, $(SRCDIRS), $(wildcard $(dir)/*.c $(dir)/*.cc $(dir)/*.cpp))
-ifeq ($(SRCS), )
-	SRCS = $(wildcard *.c *.cc *.cpp)
+ALL_SRCS += $(foreach dir, $(SRCDIRS), $(wildcard $(dir)/*.c $(dir)/*.cc $(dir)/*.cpp))
+ifeq ($(ALL_SRCS), )
+	ALL_SRCS = $(wildcard *.c *.cc *.cpp)
 endif
+override SRCS += $(filter-out %_test.c %_test.cc %_test.cpp, $(ALL_SRCS))
 # OBJS += $(patsubst %.c, %.o, $(SRCS))
 # OBJS += $(patsubst %.cc, %.o, $(SRCS))
 # OBJS += $(patsubst %.cpp, %.o, $(SRCS))

+ 1 - 0
cmake/utils.cmake

@@ -29,5 +29,6 @@ macro(list_source_directories srcs)
         aux_source_directory(${dir} tmp)
     endforeach()
     set(${srcs} ${tmp})
+    list(FILTER ${srcs} EXCLUDE REGEX ".*_test\\.c")
 endmacro()
 

+ 0 - 2
evpp/EventLoop.h

@@ -160,7 +160,6 @@ public:
 private:
     static void onTimer(htimer_t* htimer) {
         EventLoop* loop = (EventLoop*)hevent_userdata(htimer);
-        hloop_t* hloop = (hloop_t*)hevent_loop(htimer);
 
         TimerID timerID = hevent_id(htimer);
         TimerCallback cb = NULL;
@@ -192,7 +191,6 @@ private:
 
     static void onCustomEvent(hevent_t* hev) {
         EventLoop* loop = (EventLoop*)hevent_userdata(hev);
-        hloop_t* hloop = (hloop_t*)hevent_loop(hev);
 
         loop->mutex_.lock();
         EventPtr ev = loop->customEvents.front();

+ 49 - 45
evpp/EventLoopThreadPool.h

@@ -1,23 +1,15 @@
 #ifndef HV_EVENT_LOOP_THREAD_POOL_HPP_
 #define HV_EVENT_LOOP_THREAD_POOL_HPP_
 
-#include <thread>
-
 #include "EventLoopThread.h"
 
 namespace hv {
 
 class EventLoopThreadPool : public Status {
 public:
-    EventLoopThreadPool(EventLoopPtr master_loop = NULL,
-                        int worker_threads = std::thread::hardware_concurrency()) {
+    EventLoopThreadPool(int thread_num = std::thread::hardware_concurrency()) {
         setStatus(kInitializing);
-        if (master_loop) {
-            master_loop_ = master_loop;
-        } else {
-            master_loop_.reset(new EventLoop);
-        }
-        thread_num_ = worker_threads;
+        thread_num_ = thread_num;
         next_loop_idx_ = 0;
         setStatus(kInitialized);
     }
@@ -27,28 +19,37 @@ public:
         join();
     }
 
-    int thread_num() {
+    int threadNum() {
         return thread_num_;
     }
 
-    EventLoopPtr loop() {
-        return master_loop_;
+    void setThreadNum(int num) {
+        thread_num_ = num;
     }
 
-    hloop_t* hloop() {
-        return master_loop_->loop();
+    EventLoopPtr nextLoop() {
+        if (loop_threads_.empty()) return NULL;
+        return loop_threads_[++next_loop_idx_ % loop_threads_.size()]->loop();
     }
 
-    EventLoopPtr nextLoop() {
-        if (isRunning() && !worker_threads_.empty()) {
-            return worker_threads_[++next_loop_idx_ % worker_threads_.size()]->loop();
-        } else {
-            return master_loop_;
+    EventLoopPtr loop(int idx = -1) {
+        if (idx >= 0 && idx < loop_threads_.size()) {
+            return loop_threads_[idx]->loop();
         }
+        return nextLoop();
     }
 
-    // @param wait_threads_started: if ture this method will block until all worker_threads started.
-    void start(bool wait_threads_started = false) {
+    hloop_t* hloop(int idx = -1) {
+        EventLoopPtr ptr = loop(idx);
+        return ptr ? ptr->loop() : NULL;
+    }
+
+    // @param wait_threads_started: if ture this method will block until all loop_threads started.
+    // @param pre: This functor will be executed when loop_thread started.
+    // @param post:This Functor will be executed when loop_thread stopped.
+    void start(bool wait_threads_started = false,
+               std::function<void(const EventLoopPtr&)> pre = NULL,
+               std::function<void(const EventLoopPtr&)> post = NULL) {
         setStatus(kStarting);
 
         if (thread_num_ == 0) {
@@ -60,21 +61,25 @@ public:
         std::shared_ptr<std::atomic<int>> exited_cnt(new std::atomic<int>(0));
 
         for (int i = 0; i < thread_num_; ++i) {
-            auto prefn = [this, started_cnt]() {
-                if (++(*started_cnt) == thread_num_) {
-                    setStatus(kRunning);
-                }
-                return 0;
-            };
-            auto postfn = [this, exited_cnt]() {
-                if (++(*exited_cnt) == thread_num_) {
-                    setStatus(kStopped);
+            EventLoopThreadPtr loop_thread(new EventLoopThread);
+            EventLoopPtr loop = loop_thread->loop();
+            loop_thread->start(false,
+                [this, started_cnt, pre, &loop]() {
+                    if (++(*started_cnt) == thread_num_) {
+                        setStatus(kRunning);
+                    }
+                    if (pre) pre(loop);
+                    return 0;
+                },
+                [this, exited_cnt, post, &loop]() {
+                    if (post) post(loop);
+                    if (++(*exited_cnt) == thread_num_) {
+                        setStatus(kStopped);
+                    }
+                    return 0;
                 }
-                return 0;
-            };
-            EventLoopThreadPtr worker(new EventLoopThread());
-            worker->start(false, prefn, postfn);
-            worker_threads_.push_back(worker);
+            );
+            loop_threads_.push_back(loop_thread);
         }
 
         if (wait_threads_started) {
@@ -84,12 +89,12 @@ public:
         }
     }
 
-    // @param wait_threads_started: if ture this method will block until all worker_threads stopped.
+    // @param wait_threads_started: if ture this method will block until all loop_threads stopped.
     void stop(bool wait_threads_stopped = false) {
         setStatus(kStopping);
 
-        for (auto& worker : worker_threads_) {
-            worker->stop(false);
+        for (auto& loop_thread : loop_threads_) {
+            loop_thread->stop(false);
         }
 
         if (wait_threads_stopped) {
@@ -99,18 +104,17 @@ public:
         }
     }
 
-    // @brief join all worker_threads
-    // @note  destructor will join worker_threads if you forget to call this method.
+    // @brief join all loop_threads
+    // @note  destructor will join loop_threads if you forget to call this method.
     void join() {
-        for (auto& worker : worker_threads_) {
-            worker->join();
+        for (auto& loop_thread : loop_threads_) {
+            loop_thread->join();
         }
     }
 
 private:
-    EventLoopPtr                                master_loop_;
     int                                         thread_num_;
-    std::vector<EventLoopThreadPtr>             worker_threads_;
+    std::vector<EventLoopThreadPtr>             loop_threads_;
     std::atomic<unsigned int>                   next_loop_idx_;
 };
 

+ 3 - 10
evpp/EventLoopThreadPool_test.cpp

@@ -22,12 +22,10 @@ int main(int argc, char* argv[]) {
 
     printf("main tid=%ld\n", hv_gettid());
 
-    EventLoopPtr master_loop(new EventLoop);
-    EventLoopThreadPool loop_threads(master_loop, 4);
-
+    EventLoopThreadPool loop_threads(4);
     loop_threads.start(true);
 
-    int thread_num = loop_threads.thread_num();
+    int thread_num = loop_threads.threadNum();
     for (int i = 0; i < thread_num; ++i) {
         EventLoopPtr loop = loop_threads.nextLoop();
         printf("worker[%d] tid=%ld\n", i, loop->tid());
@@ -47,15 +45,10 @@ int main(int argc, char* argv[]) {
     }
 
     // runAfter 10s
-    master_loop->setTimeout(10000, [&loop_threads](TimerID timerID){
-        EventLoopPtr master_loop = loop_threads.loop();
-        master_loop->stop();
+    loop_threads.loop()->setTimeout(10000, [&loop_threads](TimerID timerID){
         loop_threads.stop(false);
     });
 
-    // master_loop run in main thread
-    master_loop->run();
-
     // wait loop_threads exit
     loop_threads.join();