ithewei 5 anni fa
parent
commit
2dd049340b
6 ha cambiato i file con 46 aggiunte e 59 eliminazioni
  1. 2 2
      Makefile
  2. 1 0
      base/hmutex.h
  3. 0 45
      base/hplatform.h
  4. 22 0
      base/htime.c
  5. 11 0
      base/htime.h
  6. 10 12
      unittest/hmutex_test.c

+ 2 - 2
Makefile

@@ -91,8 +91,8 @@ consul_cli: prepare
 	$(MAKEF) TARGET=$@ SRCDIRS=". base utils http http/client consul" SRCS="examples/consul_cli.cpp" DEFINES="PRINT_DEBUG"
 
 unittest: prepare
-	$(CC)  -g -Wall -std=c99   -I. -Ibase            -o bin/hmutex_test       unittest/hmutex_test.c        -pthread
-	$(CC)  -g -Wall -std=c99   -I. -Ibase            -o bin/connect_test      unittest/connect_test.c       base/hsocket.c
+	$(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
 	$(CXX) -g -Wall -std=c++11 -I. -Ibase            -o bin/hstring_test      unittest/hstring_test.cpp     base/hstring.cpp base/hbase.c

+ 1 - 0
base/hmutex.h

@@ -2,6 +2,7 @@
 #define HV_MUTEX_H_
 
 #include "hplatform.h"
+#include "htime.h"
 
 #ifdef _MSC_VER
 #define hmutex_t                CRITICAL_SECTION

+ 0 - 45
base/hplatform.h

@@ -193,7 +193,6 @@ typedef unsigned __int64    uint64_t;
 #endif
 
 #ifndef _MSC_VER
-
 #if HAVE_SYS_TIME_H
 #include <sys/time.h>   // for gettimeofday
 #endif
@@ -201,50 +200,6 @@ typedef unsigned __int64    uint64_t;
 #if HAVE_PTHREAD_H
 #include <pthread.h>
 #endif
-
-#endif
-
-#ifdef OS_WIN
-static inline void sleep(unsigned int s) {
-    Sleep(s*1000);
-}
-
-static inline void usleep(unsigned int us) {
-    Sleep(us/1000);
-}
-#endif
-
-static inline void msleep(unsigned int ms) {
-#ifdef OS_WIN
-    Sleep(ms);
-#else
-    usleep(ms*1000);
-#endif
-}
-
-#if HAVE_CLOCK_GETTIME
-#ifndef CLOCK_REALTIME
-#define CLOCK_REALTIME 0
-#endif
-
-#ifndef CLOCK_MONOTONIC
-#define CLOCK_MONOTONIC 1
-#endif
-#endif
-
-// ms
-static inline unsigned int gettick() {
-#ifdef OS_WIN
-    return GetTickCount();
-#elif HAVE_CLOCK_GETTIME
-    struct timespec ts;
-    clock_gettime(CLOCK_MONOTONIC, &ts);
-    return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
-#else
-    struct timeval tv;
-    gettimeofday(&tv, NULL);
-    return tv.tv_sec * 1000 + tv.tv_usec / 1000;
 #endif
-}
 
 #endif // HV_PLATFORM_H_

+ 22 - 0
base/htime.c

@@ -19,6 +19,28 @@ static const uint8_t s_days[] = \
 //   1       3       5       7   8       10      12
     {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
 
+void msleep(unsigned int ms) {
+#ifdef OS_WIN
+    Sleep(ms);
+#else
+    usleep(ms*1000);
+#endif
+}
+
+unsigned int gettick() {
+#ifdef OS_WIN
+    return GetTickCount();
+#elif HAVE_CLOCK_GETTIME
+    struct timespec ts;
+    clock_gettime(CLOCK_MONOTONIC, &ts);
+    return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
+#else
+    struct timeval tv;
+    gettimeofday(&tv, NULL);
+    return tv.tv_sec * 1000 + tv.tv_usec / 1000;
+#endif
+}
+
 unsigned long long gethrtime() {
 #ifdef OS_WIN
     static LONGLONG s_freq = 0;

+ 11 - 0
base/htime.h

@@ -26,6 +26,14 @@ typedef struct datetime_s {
 } datetime_t;
 
 #ifdef OS_WIN
+static inline void sleep(unsigned int s) {
+    Sleep(s*1000);
+}
+
+static inline void usleep(unsigned int us) {
+    Sleep(us/1000);
+}
+
 #ifdef _MSC_VER
 /* @see winsock2.h
 // Structure used in select() call, taken from the BSD file sys/time.h
@@ -63,6 +71,9 @@ static inline unsigned long long timestamp_ms() {
     return tv.tv_sec * (unsigned long long)1000 + tv.tv_usec/1000;
 }
 
+void msleep(unsigned int ms);
+// ms
+unsigned int gettick();
 // us
 unsigned long long gethrtime();
 

+ 10 - 12
unittest/hmutex_test.c

@@ -1,8 +1,6 @@
 #include "hthread.h"
 #include "hmutex.h"
-
-#include <stdio.h>
-#include <time.h>
+#include "htime.h"
 
 void once_print() {
     printf("exec once\n");
@@ -53,12 +51,12 @@ HTHREAD_ROUTINE(test_timed_mutex) {
     htimed_mutex_t mutex;
     htimed_mutex_init(&mutex);
     htimed_mutex_lock(&mutex);
-    time_t start_time = time(NULL);
+    time_t start_time = gettick();
     htimed_mutex_lock_for(&mutex, 3000);
-    time_t end_time = time(NULL);
+    time_t end_time = gettick();
+    printf("htimed_mutex_lock_for %zdms\n", end_time - start_time);
     htimed_mutex_unlock(&mutex);
     htimed_mutex_destroy(&mutex);
-    printf("htimed_mutex_lock_for %zds\n", end_time - start_time);
     printf("htimed_mutex test OK!\n");
     return 0;
 }
@@ -72,10 +70,10 @@ HTHREAD_ROUTINE(test_condvar) {
     hmutex_lock(&mutex);
     hcondvar_signal(&cv);
     hcondvar_broadcast(&cv);
-    time_t start_time = time(NULL);
+    time_t start_time = gettick();
     hcondvar_wait_for(&cv, &mutex, 3000);
-    time_t end_time = time(NULL);
-    printf("hcondvar_wait_for %zds\n", end_time - start_time);
+    time_t end_time = gettick();
+    printf("hcondvar_wait_for %zdms\n", end_time - start_time);
     hmutex_unlock(&mutex);
 
     hmutex_destroy(&mutex);
@@ -92,10 +90,10 @@ HTHREAD_ROUTINE(test_sem) {
     }
     hsem_post(&sem);
     hsem_wait(&sem);
-    time_t start_time = time(NULL);
+    time_t start_time = gettick();
     hsem_wait_for(&sem, 3000);
-    time_t end_time = time(NULL);
-    printf("hsem_wait_for %zds\n", end_time - start_time);
+    time_t end_time = gettick();
+    printf("hsem_wait_for %zdms\n", end_time - start_time);
     hsem_destroy(&sem);
     printf("hsem test OK!\n");
     return 0;