Use pthread to avoid circular calls to timer library

pull/14720/head
yang-g 7 years ago
parent e05618f379
commit 22bcb486bd
  1. 18
      src/core/lib/profiling/basic_timers.cc

@ -31,7 +31,6 @@
#include <string.h> #include <string.h>
#include "src/core/lib/gpr/env.h" #include "src/core/lib/gpr/env.h"
#include "src/core/lib/gprpp/thd.h"
typedef enum { BEGIN = '{', END = '}', MARK = '.' } marker_type; typedef enum { BEGIN = '{', END = '}', MARK = '.' } marker_type;
@ -69,7 +68,7 @@ static pthread_cond_t g_cv;
static gpr_timer_log_list g_in_progress_logs; static gpr_timer_log_list g_in_progress_logs;
static gpr_timer_log_list g_done_logs; static gpr_timer_log_list g_done_logs;
static int g_shutdown; static int g_shutdown;
static grpc_core::Thread* g_writing_thread; static pthread_t g_writing_thread;
static __thread int g_thread_id; static __thread int g_thread_id;
static int g_next_thread_id; static int g_next_thread_id;
static int g_writing_enabled = 1; static int g_writing_enabled = 1;
@ -150,7 +149,7 @@ static void write_log(gpr_timer_log* log) {
} }
} }
static void writing_thread(void* unused) { static void* writing_thread(void* unused) {
gpr_timer_log* log; gpr_timer_log* log;
pthread_mutex_lock(&g_mu); pthread_mutex_lock(&g_mu);
for (;;) { for (;;) {
@ -165,7 +164,7 @@ static void writing_thread(void* unused) {
} }
if (g_shutdown) { if (g_shutdown) {
pthread_mutex_unlock(&g_mu); pthread_mutex_unlock(&g_mu);
return; return NULL;
} }
} }
} }
@ -183,8 +182,7 @@ static void finish_writing(void) {
g_shutdown = 1; g_shutdown = 1;
pthread_cond_signal(&g_cv); pthread_cond_signal(&g_cv);
pthread_mutex_unlock(&g_mu); pthread_mutex_unlock(&g_mu);
g_writing_thread->Join(); pthread_join(g_writing_thread, NULL);
grpc_core::Delete(g_writing_thread);
gpr_log(GPR_INFO, "flushing logs"); gpr_log(GPR_INFO, "flushing logs");
@ -203,8 +201,12 @@ void gpr_timers_set_log_filename(const char* filename) {
} }
static void init_output() { static void init_output() {
g_writing_thread = grpc_core::New<grpc_core::Thread>("timer_output_thread", pthread_attr_t attr;
writing_thread, nullptr); pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
pthread_create(&g_writing_thread, &attr, &writing_thread, NULL);
pthread_attr_destroy(&attr);
atexit(finish_writing); atexit(finish_writing);
} }

Loading…
Cancel
Save