ithewei 5 년 전
부모
커밋
6ad35fb309
7개의 변경된 파일140개의 추가작업 그리고 68개의 파일을 삭제
  1. 10 2
      CMakeLists.txt
  2. 2 1
      Makefile
  3. 9 7
      base/hlog.c
  4. 2 0
      event/hloop.h
  5. 53 58
      examples/CMakeLists.txt
  6. 5 0
      unittest/CMakeLists.txt
  7. 59 0
      unittest/hthread_test.cpp

+ 10 - 2
CMakeLists.txt

@@ -5,6 +5,9 @@ project(hv VERSION 1.20.8)
 option(BUILD_SHARED "build shared library" ON)
 option(BUILD_STATIC "build static library" ON)
 
+option(BUILD_EXAMPLES "build examples" ON)
+option(BUILD_UNITTEST "build unittest" ON)
+
 # see config.mk
 option(WITH_PROTOCOL "compile protocol" ON)
 
@@ -179,5 +182,10 @@ endif()
 file(INSTALL ${LIBHV_HEADERS} DESTINATION include/hv)
 install(FILES ${LIBHV_HEADERS} DESTINATION include/hv)
 
-add_subdirectory(unittest)
-add_subdirectory(examples)
+if(BUILD_EXAMPLES)
+    add_subdirectory(examples)
+endif()
+
+if(BUILD_UNITTEST)
+    add_subdirectory(unittest)
+endif()

+ 2 - 1
Makefile

@@ -95,7 +95,8 @@ unittest: prepare
 	$(CC)  -g -Wall -std=c99   -I. -Ibase            -o bin/date              unittest/date_test.c          base/htime.c
 	$(CC)  -g -Wall -std=c99   -I. -Ibase            -o bin/hatomic_test      unittest/hatomic_test.c       -pthread
 	$(CXX) -g -Wall -std=c++11 -I. -Ibase            -o bin/hatomic_cpp_test  unittest/hatomic_test.cpp     -pthread
-	$(CC)  -g -Wall -std=c99   -I. -Ibase            -o bin/hmutex_test       unittest/hmutex_test.c        base/htime.c -pthread
+	$(CXX) -g -Wall -std=c++11 -I. -Ibase            -o bin/hthread_test      unittest/hthread_test.cpp     -pthread
+	$(CC)  -g -Wall -std=c99   -I. -Ibase            -o bin/hmutex_test       unittest/hmutex_test.c        base/htime.c   -pthread
 	$(CC)  -g -Wall -std=c99   -I. -Ibase            -o bin/connect_test      unittest/connect_test.c       base/hsocket.c base/htime.c
 	$(CC)  -g -Wall -std=c99   -I. -Ibase            -o bin/socketpair_test   unittest/socketpair_test.c    base/hsocket.c
 	$(CXX) -g -Wall -std=c++11 -I. -Ibase            -o bin/defer_test        unittest/defer_test.cpp

+ 9 - 7
base/hlog.c

@@ -194,7 +194,7 @@ const char* logger_get_cur_file(logger_t* logger) {
 
 static void logfile_name(const char* filepath, time_t ts, char* buf, int len) {
     struct tm* tm = localtime(&ts);
-    snprintf(buf, len, "%s-%04d-%02d-%02d.log",
+    snprintf(buf, len, "%s.%04d%02d%02d.log",
             filepath,
             tm->tm_year+1900,
             tm->tm_mon+1,
@@ -313,16 +313,18 @@ int logger_print(logger_t* logger, int level, const char* fmt, ...) {
     }
 #undef XXX
 
-    if (!logger->enable_color) {
-        pcolor = "";
-    }
-
     // lock logger->buf
     hmutex_lock(&logger->mutex_);
+
     char* buf = logger->buf;
     int bufsize = logger->bufsize;
-    int len = snprintf(buf, bufsize, "%s[%04d-%02d-%02d %02d:%02d:%02d.%03d][%s] ",
-        pcolor,
+    int len = 0;
+
+    if (logger->enable_color) {
+        len = snprintf(buf, bufsize, "%s", pcolor);
+    }
+
+    len += snprintf(buf + len, bufsize - len, "%04d-%02d-%02d %02d:%02d:%02d.%03d %s ",
         year, month, day, hour, min, sec, ms,
         plevel);
 

+ 2 - 0
event/hloop.h

@@ -8,6 +8,8 @@
 typedef struct hloop_s      hloop_t;
 typedef struct hevent_s     hevent_t;
 
+// NOTE: The following structures are subclasses of hevent_t,
+// inheriting hevent_t data members and function members.
 typedef struct hidle_s      hidle_t;
 typedef struct htimer_s     htimer_t;
 typedef struct htimeout_s   htimeout_t;

+ 53 - 58
examples/CMakeLists.txt

@@ -1,73 +1,68 @@
-add_definitions(-DHV_SOURCE=1)
+list(APPEND EXAMPLES
+    hmain_test
+    hloop_test
+    htimer_test
+    tcp
+    udp
+    nc
+    nmap
+)
 
-aux_source_directory(../base        BASE_SRCS)
-aux_source_directory(../utils       UTILS_SRCS)
-aux_source_directory(../event       EVENT_SRCS)
-aux_source_directory(../http        HTTP_SRCS)
-aux_source_directory(../http/client HTTP_CLIENT_SRCS)
-aux_source_directory(../http/server HTTP_SERVER_SRCS)
-aux_source_directory(../consul      CONSUL_SRCS)
-aux_source_directory(httpd          HTTPD_SRCS)
+include_directories(.. ../base ../event ../utils)
 
-add_executable(hmain_test hmain_test.cpp ${BASE_SRCS} ${UTILS_SRCS})
-target_include_directories(hmain_test PRIVATE .. ../base ../utils)
-target_link_libraries(hmain_test ${LIBS})
+add_executable(hmain_test hmain_test.cpp)
+target_link_libraries(hmain_test hv)
 
-add_executable(htimer_test htimer_test.c ${BASE_SRCS} ${EVENT_SRCS})
-target_include_directories(htimer_test PRIVATE .. ../base ../event)
-target_link_libraries(htimer_test ${LIBS})
+add_executable(hloop_test hloop_test.c)
+target_link_libraries(hloop_test hv)
 
-add_executable(hloop_test hloop_test.c ${BASE_SRCS} ${EVENT_SRCS})
-target_include_directories(hloop_test PRIVATE .. ../base ../event)
-target_link_libraries(hloop_test ${LIBS})
+add_executable(htimer_test htimer_test.c)
+target_link_libraries(htimer_test hv)
 
-add_executable(tcp tcp.c ${BASE_SRCS} ${EVENT_SRCS})
-target_include_directories(tcp PRIVATE .. ../base ../event)
-target_link_libraries(tcp ${LIBS})
+add_executable(tcp tcp.c)
+target_link_libraries(tcp hv)
 
-add_executable(udp udp.c ${BASE_SRCS} ${EVENT_SRCS})
-target_include_directories(udp PRIVATE .. ../base ../event)
-target_link_libraries(udp ${LIBS})
+add_executable(udp udp.c)
+target_link_libraries(udp hv)
 
-add_executable(nc nc.c ${BASE_SRCS} ${EVENT_SRCS})
-target_include_directories(nc PRIVATE .. ../base ../event)
-target_link_libraries(nc ${LIBS})
+add_executable(nc nc.c)
+target_link_libraries(nc hv)
 
-add_executable(nmap nmap.cpp ${BASE_SRCS} ${EVENT_SRCS})
+add_executable(nmap nmap.cpp)
 target_compile_definitions(nmap PRIVATE -DPRINT_DEBUG)
-target_include_directories(nmap PRIVATE .. ../base ../event)
-target_link_libraries(nmap ${LIBS})
+target_link_libraries(nmap hv)
 
-add_executable(httpd ${BASE_SRCS} ${UTILS_SRCS} ${EVENT_SRCS} ${HTTP_SRCS} ${HTTP_SERVER_SRCS} ${HTTPD_SRCS})
-target_include_directories(httpd PRIVATE .. ../base ../utils ../event ../http ../http/server httpd)
-target_link_libraries(httpd ${LIBS})
+if(WITH_HTTP)
+    include_directories(../http)
+if(WITH_HTTP_SERVER)
+    include_directories(../http/server)
+    list(APPEND EXAMPLES httpd)
+    aux_source_directory(httpd HTTPD_SRCS)
+    add_executable(httpd ${HTTPD_SRCS})
+    target_link_libraries(httpd hv)
+endif()
 
-if(WITH_CURL)
-    set(CURL_TARGET_NAME hv_curl)
-else()
+if(WITH_HTTP_CLIENT)
+    include_directories(../http/client)
     set(CURL_TARGET_NAME curl)
+    if(WITH_CURL)
+        set(CURL_TARGET_NAME hv_curl)
+    endif()
+    list(APPEND EXAMPLES ${CURL_TARGET_NAME})
+    add_executable(${CURL_TARGET_NAME} curl.cpp)
+    if(WITH_CURL)
+        set_target_properties(${CURL_TARGET_NAME} PROPERTIES OUTPUT_NAME curl)
+    endif()
+    target_link_libraries(${CURL_TARGET_NAME} hv)
 endif()
-add_executable(${CURL_TARGET_NAME} curl.cpp ${BASE_SRCS} ${UTILS_SRCS} ${HTTP_SRCS} ${HTTP_CLIENT_SRCS})
-if(WITH_CURL)
-    set_target_properties(${CURL_TARGET_NAME} PROPERTIES OUTPUT_NAME curl)
-endif()
-target_include_directories(${CURL_TARGET_NAME} PRIVATE .. ../base ../utils ../http ../http/client)
-target_link_libraries(${CURL_TARGET_NAME} ${LIBS})
 
-add_executable(consul_cli consul_cli.cpp ${BASE_SRCS} ${UTILS_SRCS} ${HTTP_SRCS} ${HTTP_CLIENT_SRCS} ${CONSUL_SRCS})
-target_compile_definitions(consul_cli PRIVATE PRINT_DEBUG)
-target_include_directories(consul_cli PRIVATE .. ../base ../utils ../http ../http/client ../consul)
-target_link_libraries(consul_cli ${LIBS})
+if(WITH_CONSUL)
+    include_directories(../consul)
+    list(APPEND EXAMPLES consul_cli)
+    add_executable(consul_cli consul_cli.cpp)
+    target_compile_definitions(consul_cli PRIVATE PRINT_DEBUG)
+    target_link_libraries(consul_cli hv)
+endif()
+endif()
 
-add_custom_target(examples DEPENDS
-    hmain_test
-    htimer_test
-    hloop_test
-    tcp
-    udp
-    nc
-    nmap
-    httpd
-    ${CURL_TARGET_NAME}
-    consul_cli
-)
+add_custom_target(examples DEPENDS ${EXAMPLES})

+ 5 - 0
unittest/CMakeLists.txt

@@ -13,6 +13,10 @@ add_executable(hatomic_test hatomic_test.c)
 target_include_directories(hatomic_test PRIVATE .. ../base)
 target_link_libraries(hatomic_test -lpthread)
 
+add_executable(hthread_test hthread_test.cpp)
+target_include_directories(hthread_test PRIVATE .. ../base)
+target_link_libraries(hthread_test -lpthread)
+
 add_executable(hmutex_test hmutex_test.c ../base/htime.c)
 target_include_directories(hmutex_test PRIVATE .. ../base)
 target_link_libraries(hmutex_test -lpthread)
@@ -69,6 +73,7 @@ add_custom_target(unittest DEPENDS
     rmdir_p
     date
     hatomic_test
+    hthread_test
     hmutex_test
     connect_test
     socketpair_test

+ 59 - 0
unittest/hthread_test.cpp

@@ -0,0 +1,59 @@
+#include "hthread.h"
+#include "htime.h"
+
+HTHREAD_ROUTINE(test_thread1) {
+    int cnt = 10;
+    while (cnt-- > 0) {
+        printf("tid=%ld time=%llums\n", hv_gettid(), gettimeofday_ms());
+        hv_delay(100);
+    }
+    return 0;
+}
+
+class TestThread2 : public HThread {
+protected:
+    virtual void run() {
+        int cnt = 10;
+        while (cnt-- > 0) {
+            printf("tid=%ld time=%llums\n", hv_gettid(), gettimeofday_ms());
+            hv_delay(100);
+        }
+    }
+};
+
+class TestThread3 : public HThread {
+protected:
+    virtual bool doPrepare() {
+        printf("doPrepare\n");
+        return true;
+    }
+
+    virtual void doTask() {
+        printf("tid=%ld time=%llums\n", hv_gettid(), gettimeofday_ms());
+    }
+
+    virtual bool doFinish() {
+        printf("doFinish\n");
+        return true;
+    }
+};
+
+int main() {
+    printf("c-style hthread_create\n");
+    hthread_t thread1 = hthread_create(test_thread1, NULL);
+    hthread_join(thread1);
+
+    printf("cpp-style override HThread::run\n");
+    TestThread2 thread2;
+    thread2.start();
+    thread2.stop();
+
+    printf("cpp-style override HThread::doTask\n");
+    TestThread3 thread3;
+    thread3.setSleepPolicy(HThread::SLEEP_UNTIL, 100);
+    thread3.start();
+    sleep(1);
+    thread3.stop();
+
+    return 0;
+}