Selaa lähdekoodia

optimize duplicate code

hewei.it 5 vuotta sitten
vanhempi
commit
627d6f1cc0
2 muutettua tiedostoa jossa 33 lisäystä ja 48 poistoa
  1. 13 24
      base/hmutex.h
  2. 20 24
      base/hproc.h

+ 13 - 24
base/hmutex.h

@@ -102,19 +102,22 @@ static inline void honce(honce_t* once, honce_fn fn) {
 #define htimed_mutex_destroy        pthread_mutex_destroy
 #define htimed_mutex_lock           pthread_mutex_lock
 #define htimed_mutex_unlock         pthread_mutex_unlock
+static inline void timespec_after(struct timespec* ts, unsigned int ms) {
+    struct timeval  tv;
+    gettimeofday(&tv, NULL);
+    ts->tv_sec = tv.tv_sec + ms / 1000;
+    ts->tv_nsec = tv.tv_usec * 1000 + ms % 1000 * 1000000;
+    if (ts->tv_nsec >= 1000000000) {
+        ts->tv_nsec -= 1000000000;
+        ts->tv_sec += 1;
+    }
+}
 // true:  OK
 // false: ETIMEDOUT
 static inline int htimed_mutex_lock_for(htimed_mutex_t* mutex, unsigned int ms) {
 #if HAVE_PTHREAD_MUTEX_TIMEDLOCK
     struct timespec ts;
-    struct timeval  tv;
-    gettimeofday(&tv, NULL);
-    ts.tv_sec = tv.tv_sec + ms / 1000;
-    ts.tv_nsec = tv.tv_usec * 1000 + ms % 1000 * 1000000;
-    if (ts.tv_nsec >= 1000000000) {
-        ts.tv_nsec -= 1000000000;
-        ts.tv_sec += 1;
-    }
+    timespec_after(&ts, ms);
     return pthread_mutex_timedlock(mutex, &ts) != ETIMEDOUT;
 #else
     int ret = 0;
@@ -139,14 +142,7 @@ static inline int htimed_mutex_lock_for(htimed_mutex_t* mutex, unsigned int ms)
 // false: ETIMEDOUT
 static inline int hcondvar_wait_for(hcondvar_t* cond, hmutex_t* mutex, unsigned int ms) {
     struct timespec ts;
-    struct timeval  tv;
-    gettimeofday(&tv, NULL);
-    ts.tv_sec = tv.tv_sec + ms / 1000;
-    ts.tv_nsec = tv.tv_usec * 1000 + ms % 1000 * 1000000;
-    if (ts.tv_nsec >= 1000000000) {
-        ts.tv_nsec -= 1000000000;
-        ts.tv_sec += 1;
-    }
+    timespec_after(&ts, ms);
     return pthread_cond_timedwait(cond, mutex, &ts) != ETIMEDOUT;
 }
 
@@ -165,14 +161,7 @@ static inline int hcondvar_wait_for(hcondvar_t* cond, hmutex_t* mutex, unsigned
 static inline int hsem_wait_for(hsem_t* sem, unsigned int ms) {
 #if HAVE_SEM_TIMEDWAIT
     struct timespec ts;
-    struct timeval  tv;
-    gettimeofday(&tv, NULL);
-    ts.tv_sec = tv.tv_sec + ms / 1000;
-    ts.tv_nsec = tv.tv_usec * 1000 + ms % 1000 * 1000000;
-    if (ts.tv_nsec >= 1000000000) {
-        ts.tv_nsec -= 1000000000;
-        ts.tv_sec += 1;
-    }
+    timespec_after(&ts, ms);
     return sem_timedwait(sem, &ts) != ETIMEDOUT;
 #else
     int ret = 0;

+ 20 - 24
base/hproc.h

@@ -4,7 +4,7 @@
 #include "hplatform.h"
 
 typedef struct proc_ctx_s {
-    pid_t           pid; // tid in win32
+    pid_t           pid; // tid in Windows
     procedure_t     init;
     void*           init_userdata;
     procedure_t     proc;
@@ -13,6 +13,18 @@ typedef struct proc_ctx_s {
     void*           exit_userdata;
 } proc_ctx_t;
 
+static inline void hproc_run(proc_ctx_t* ctx) {
+    if (ctx->init) {
+        ctx->init(ctx->init_userdata);
+    }
+    if (ctx->proc) {
+        ctx->proc(ctx->proc_userdata);
+    }
+    if (ctx->exit) {
+        ctx->exit(ctx->exit_userdata);
+    }
+}
+
 #ifdef OS_UNIX
 // unix use multi-processes
 static inline int hproc_spawn(proc_ctx_t* ctx) {
@@ -21,20 +33,12 @@ static inline int hproc_spawn(proc_ctx_t* ctx) {
         perror("fork");
         return -1;
     } else if (pid == 0) {
-        // child proc
+        // child process
         ctx->pid = getpid();
-        if (ctx->init) {
-            ctx->init(ctx->init_userdata);
-        }
-        if (ctx->proc) {
-            ctx->proc(ctx->proc_userdata);
-        }
-        if (ctx->exit) {
-            ctx->exit(ctx->exit_userdata);
-        }
+        hproc_run(ctx);
         exit(0);
     } else if (pid > 0) {
-        // parent proc
+        // parent process
         ctx->pid = pid;
     }
     return pid;
@@ -43,24 +47,16 @@ static inline int hproc_spawn(proc_ctx_t* ctx) {
 // win32 use multi-threads
 static void win_thread(void* userdata) {
     proc_ctx_t* ctx = (proc_ctx_t*)userdata;
-    if (ctx->init) {
-        ctx->init(ctx->init_userdata);
-    }
-    if (ctx->proc) {
-        ctx->proc(ctx->proc_userdata);
-    }
-    if (ctx->exit) {
-        ctx->exit(ctx->exit_userdata);
-    }
+    ctx->pid = GetCurrentThreadId(); // tid in Windows
+    hproc_run(ctx);
 }
 static inline int hproc_spawn(proc_ctx_t* ctx) {
     HANDLE h = (HANDLE)_beginthread(win_thread, 0, ctx);
     if (h == NULL) {
         return -1;
     }
-    int tid = GetThreadId(h);
-    ctx->pid = tid;
-    return tid;
+    ctx->pid = GetThreadId(h); // tid in Windows
+    return ctx->pid;
 }
 #endif