1
0
hewei.it 4 жил өмнө
parent
commit
3e15658ee2

+ 6 - 6
base/hmutex.h

@@ -139,12 +139,12 @@ static inline int htimed_mutex_lock_for(htimed_mutex_t* mutex, unsigned int ms)
     return pthread_mutex_timedlock(mutex, &ts) != ETIMEDOUT;
 #else
     int ret = 0;
-    unsigned int end = gettick() + ms;
+    unsigned int end = gettick_ms() + ms;
     while ((ret = pthread_mutex_trylock(mutex)) != 0) {
-        if (gettick() >= end) {
+        if (gettick_ms() >= end) {
             break;
         }
-        msleep(1);
+        hv_msleep(1);
     }
     return ret == 0;
 #endif
@@ -183,12 +183,12 @@ static inline int hsem_wait_for(hsem_t* sem, unsigned int ms) {
     return sem_timedwait(sem, &ts) != ETIMEDOUT;
 #else
     int ret = 0;
-    unsigned int end = gettick() + ms;
+    unsigned int end = gettick_ms() + ms;
     while ((ret = sem_trywait(sem)) != 0) {
-        if (gettick() >= end) {
+        if (gettick_ms() >= end) {
             break;
         }
-        msleep(1);
+        hv_msleep(1);
     }
     return ret == 0;
 #endif

+ 8 - 2
base/hplatform.h

@@ -146,7 +146,10 @@
     #include <direct.h>     // for mkdir,rmdir,chdir,getcwd
     #include <io.h>         // for open,close,read,write,lseek,tell
 
-    #define hv_delay(ms)    Sleep(ms)
+    #define hv_sleep(s)     Sleep((s) * 1000)
+    #define hv_msleep(ms)   Sleep(ms)
+    #define hv_usleep(us)   Sleep((us) / 1000)
+    #define hv_delay(ms)    hv_msleep(ms)
     #define hv_mkdir(dir)   mkdir(dir)
 
     #ifndef S_ISREG
@@ -168,7 +171,10 @@
     #include <netinet/udp.h>
     #include <netdb.h>  // for gethostbyname
 
-    #define hv_delay(ms)    usleep((ms)*1000)
+    #define hv_sleep(s)     sleep(s)
+    #define hv_msleep(ms)   usleep((ms) * 1000)
+    #define hv_usleep(us)   usleep(us)
+    #define hv_delay(ms)    hv_msleep(ms)
     #define hv_mkdir(dir)   mkdir(dir, 0777)
 #endif
 

+ 1 - 1
base/htime.c

@@ -9,7 +9,7 @@ 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};
 
-unsigned int gettick() {
+unsigned int gettick_ms() {
 #ifdef OS_WIN
     return GetTickCount();
 #elif HAVE_CLOCK_GETTIME

+ 6 - 24
base/htime.h

@@ -52,35 +52,17 @@ static inline int gettimeofday(struct timeval *tv, struct timezone *tz) {
 }
 #endif
 
-// sleep(s), msleep(ms), usleep(us)
-#ifdef OS_WIN
-static inline void sleep(unsigned int s) {
-    Sleep(s * 1000);
-}
-
-static inline void msleep(unsigned int ms) {
-    Sleep(ms);
-}
-
-static inline void usleep(unsigned int us) {
-    Sleep(us / 1000);
-}
-#else
-static inline void msleep(unsigned int ms) {
-    usleep(ms * 1000);
-}
-#endif
-
-// ms
-HV_EXPORT unsigned int gettick();
-
+HV_EXPORT unsigned int gettick_ms();
 static inline unsigned long long gettimeofday_ms() {
     struct timeval tv;
     gettimeofday(&tv, NULL);
     return tv.tv_sec * (unsigned long long)1000 + tv.tv_usec/1000;
 }
-
-// us
+static inline unsigned long long gettimeofday_us() {
+    struct timeval tv;
+    gettimeofday(&tv, NULL);
+    return tv.tv_sec * (unsigned long long)1000000 + tv.tv_usec;
+}
 HV_EXPORT unsigned long long gethrtime_us();
 
 HV_EXPORT datetime_t datetime_now();

+ 4 - 4
cpputil/hmain.cpp

@@ -480,7 +480,7 @@ static void kill_proc(int pid) {
     kill(pid, SIGNAL_TERMINATE);
 #else
     //SetEvent(s_hEventTerm);
-    //sleep(1);
+    //hv_sleep(1);
     HANDLE hproc = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
     if (hproc) {
         TerminateProcess(hproc, 0);
@@ -507,7 +507,7 @@ void signal_handle(const char* signal) {
         if (g_main_ctx.oldpid > 0) {
             kill_proc(g_main_ctx.oldpid);
             printf("%s stop/waiting\n", g_main_ctx.program_name);
-            msleep(1000);
+            hv_sleep(1);
         }
     } else if (strcmp(signal, "status") == 0) {
         if (g_main_ctx.oldpid > 0) {
@@ -525,7 +525,7 @@ void signal_handle(const char* signal) {
             SetEvent(s_hEventReload);
 #endif
         }
-        sleep(1);
+        hv_sleep(1);
         exit(0);
     } else {
         printf("Invalid signal: '%s'\n", signal);
@@ -613,7 +613,7 @@ int master_workers_run(procedure_t worker_fn, void* worker_userdata,
         g_main_ctx.pid = getpid();
         hlogi("master start/running, pid=%d", g_main_ctx.pid);
         if (wait) {
-            while (1) sleep (1);
+            while (1) hv_sleep (1);
         }
     }
     return 0;

+ 3 - 3
docs/API.md

@@ -10,7 +10,7 @@
 - stdbool.h: bool, true, false
 - stdint.h: int8_t, int16_t, int32_t, int64_t
 - var
-- hv_delay
+- hv_sleep, hv_msleep, hv_usleep, hv_delay
 - hv_mkdir
 - stricmp, strcasecmp
 
@@ -69,10 +69,10 @@
 ### htime.h
 - IS_LEAP_YEAR
 - datetime_t
-- sleep, msleep, usleep
-- gettick
+- gettick_ms
 - gettimeofday
 - gettimeofday_ms
+- gettimeofday_us
 - gethrtime_us
 - datetime_now
 - datetime_mktime

+ 3 - 4
event/hloop.c

@@ -146,9 +146,8 @@ static int hloop_process_events(hloop_t* loop) {
 
     if (loop->nios) {
         nios = hloop_process_ios(loop, blocktime);
-    }
-    else {
-        msleep(blocktime);
+    } else {
+        hv_msleep(blocktime);
     }
     hloop_update_time(loop);
     // wakeup by hloop_stop
@@ -374,7 +373,7 @@ int hloop_run(hloop_t* loop) {
     loop->status = HLOOP_STATUS_RUNNING;
     while (loop->status != HLOOP_STATUS_STOP) {
         if (loop->status == HLOOP_STATUS_PAUSE) {
-            msleep(HLOOP_PAUSE_TIME);
+            hv_msleep(HLOOP_PAUSE_TIME);
             hloop_update_time(loop);
             continue;
         }

+ 1 - 1
evpp/TcpClient_test.cpp

@@ -58,6 +58,6 @@ int main(int argc, char* argv[]) {
     cli.setReconnect(&reconn);
     cli.start();
 
-    while (1) sleep(1);
+    while (1) hv_sleep(1);
     return 0;
 }

+ 1 - 1
evpp/TcpServer_test.cpp

@@ -43,6 +43,6 @@ int main(int argc, char* argv[]) {
     srv.setThreadNum(4);
     srv.start();
 
-    while (1) sleep(1);
+    while (1) hv_sleep(1);
     return 0;
 }

+ 1 - 1
evpp/UdpClient_test.cpp

@@ -41,6 +41,6 @@ int main(int argc, char* argv[]) {
         cli.sendto(str);
     });
 
-    while (1) sleep(1);
+    while (1) hv_sleep(1);
     return 0;
 }

+ 1 - 1
evpp/UdpServer_test.cpp

@@ -34,6 +34,6 @@ int main(int argc, char* argv[]) {
     };
     srv.start();
 
-    while (1) sleep(1);
+    while (1) hv_sleep(1);
     return 0;
 }

+ 1 - 1
examples/http_server_test.cpp

@@ -70,7 +70,7 @@ int main() {
 #else
     // test http_server_stop
     http_server_run(&server, 0);
-    sleep(10);
+    hv_sleep(10);
     http_server_stop(&server);
 #endif
     return 0;

+ 3 - 3
protocol/icmp.c

@@ -63,7 +63,7 @@ int ping(const char* host, int cnt) {
     for (int i = 0; i < sendbytes - sizeof(icmphdr_t); ++i) {
         icmp_req->icmp_data[i] = i;
     }
-    start_tick = gettick();
+    start_tick = gettick_ms();
     while (cnt-- > 0) {
         // NOTE: checksum
         icmp_req->icmp_seq = ++seq;
@@ -108,9 +108,9 @@ int ping(const char* host, int cnt) {
         printd("%d bytes from %s: icmp_seq=%u ttl=%u time=%.1f ms\n", icmp_len, ip, seq, ipheader->ttl, rtt);
         fflush(stdout);
         ++ok_cnt;
-        if (cnt > 0) sleep(1); // sleep a while, then agian
+        if (cnt > 0) hv_sleep(1); // sleep a while, then agian
     }
-    end_tick = gettick();
+    end_tick = gettick_ms();
     printd("--- %s ping statistics ---\n", host);
     printd("%d packets transmitted, %d received, %d%% packet loss, time %d ms\n",
         send_cnt, recv_cnt, (send_cnt-recv_cnt)*100/(send_cnt==0?1:send_cnt), end_tick-start_tick);

+ 6 - 6
unittest/connect_test.c

@@ -10,19 +10,19 @@ int main(int argc, char* argv[]) {
     const char* ip = argv[1];
     int port = atoi(argv[2]);
 
-    unsigned int start_time = gettick();
+    unsigned int start_time = gettick_ms();
     int ret = ConnectNonblock(ip, port);
-    unsigned int end_time = gettick();
+    unsigned int end_time = gettick_ms();
     printf("ConnectNonblock[%s:%d] retval=%d cost=%ums\n", ip, port, ret, end_time-start_time);
 
-    start_time = gettick();
+    start_time = gettick_ms();
     ret = ConnectTimeout(ip, port, 3000);
-    end_time = gettick();
+    end_time = gettick_ms();
     printf("ConnectTimeout[%s:%d] retval=%d cost=%ums\n", ip, port, ret, end_time-start_time);
 
-    start_time = gettick();
+    start_time = gettick_ms();
     ret = Connect(ip, port, 0);
-    end_time = gettick();
+    end_time = gettick_ms();
     printf("ConnectBlock[%s:%d] retval=%d cost=%ums\n", ip, port, ret, end_time-start_time);
 
     return 0;

+ 6 - 6
unittest/hmutex_test.c

@@ -63,9 +63,9 @@ HTHREAD_ROUTINE(test_timed_mutex) {
     htimed_mutex_t mutex;
     htimed_mutex_init(&mutex);
     htimed_mutex_lock(&mutex);
-    time_t start_time = gettick();
+    time_t start_time = gettick_ms();
     htimed_mutex_lock_for(&mutex, 3000);
-    time_t end_time = gettick();
+    time_t end_time = gettick_ms();
     printf("htimed_mutex_lock_for %zdms\n", end_time - start_time);
     htimed_mutex_unlock(&mutex);
     htimed_mutex_destroy(&mutex);
@@ -82,9 +82,9 @@ HTHREAD_ROUTINE(test_condvar) {
     hmutex_lock(&mutex);
     hcondvar_signal(&cv);
     hcondvar_broadcast(&cv);
-    time_t start_time = gettick();
+    time_t start_time = gettick_ms();
     hcondvar_wait_for(&cv, &mutex, 3000);
-    time_t end_time = gettick();
+    time_t end_time = gettick_ms();
     printf("hcondvar_wait_for %zdms\n", end_time - start_time);
     hmutex_unlock(&mutex);
 
@@ -102,9 +102,9 @@ HTHREAD_ROUTINE(test_sem) {
     }
     hsem_post(&sem);
     hsem_wait(&sem);
-    time_t start_time = gettick();
+    time_t start_time = gettick_ms();
     hsem_wait_for(&sem, 3000);
-    time_t end_time = gettick();
+    time_t end_time = gettick_ms();
     printf("hsem_wait_for %zdms\n", end_time - start_time);
     hsem_destroy(&sem);
     printf("hsem test OK!\n");

+ 3 - 3
unittest/hthread_test.cpp

@@ -5,7 +5,7 @@ HTHREAD_ROUTINE(test_thread1) {
     int cnt = 10;
     while (cnt-- > 0) {
         printf("tid=%ld time=%llums\n", hv_gettid(), gettimeofday_ms());
-        hv_delay(100);
+        hv_msleep(100);
     }
     return 0;
 }
@@ -16,7 +16,7 @@ protected:
         int cnt = 10;
         while (cnt-- > 0) {
             printf("tid=%ld time=%llums\n", hv_gettid(), gettimeofday_ms());
-            hv_delay(100);
+            hv_msleep(100);
         }
     }
 };
@@ -52,7 +52,7 @@ int main() {
     TestThread3 thread3;
     thread3.setSleepPolicy(HThread::SLEEP_UNTIL, 100);
     thread3.start();
-    sleep(1);
+    hv_sleep(1);
     thread3.stop();
 
     return 0;

+ 1 - 1
unittest/threadpool_test.cpp

@@ -6,7 +6,7 @@
 
 void print_task(int i) {
     printf("thread[%ld]: task[%d]\n", hv_gettid(), i);
-    sleep(1);
+    hv_sleep(1);
 }
 
 int main(int argc, char** argv) {