From d4c1abb8530aaa136242742e56d3237c593cf07d Mon Sep 17 00:00:00 2001 From: thinkerou Date: Tue, 21 Jun 2016 10:25:08 +0800 Subject: Merged some codes to the common function for maintainability --- src/core/lib/support/time.c | 117 ++++++++++++++------------------------------ 1 file changed, 38 insertions(+), 79 deletions(-) (limited to 'src/core') diff --git a/src/core/lib/support/time.c b/src/core/lib/support/time.c index 57f8331194..238317ffb4 100644 --- a/src/core/lib/support/time.c +++ b/src/core/lib/support/time.c @@ -80,103 +80,62 @@ gpr_timespec gpr_inf_past(gpr_clock_type type) { return out; } -/* TODO(ctiller): consider merging _nanos, _micros, _millis into a single - function for maintainability. Similarly for _seconds, _minutes, and _hours */ - -gpr_timespec gpr_time_from_nanos(int64_t ns, gpr_clock_type type) { - gpr_timespec result; - result.clock_type = type; - if (ns == INT64_MAX) { - result = gpr_inf_future(type); - } else if (ns == INT64_MIN) { - result = gpr_inf_past(type); - } else if (ns >= 0) { - result.tv_sec = ns / GPR_NS_PER_SEC; - result.tv_nsec = (int32_t)(ns - result.tv_sec * GPR_NS_PER_SEC); +static gpr_timespec get_timespec_by_different_second_unit(int64_t t, int64_t n, + gpr_clock_type type) { + gpr_timespec out; + if (t == INT64_MAX) { + out = gpr_inf_future(type); + } else if (t == INT64_MIN) { + out = gpr_inf_past(type); } else { - /* Calculation carefully formulated to avoid any possible under/overflow. */ - result.tv_sec = (-(999999999 - (ns + GPR_NS_PER_SEC)) / GPR_NS_PER_SEC) - 1; - result.tv_nsec = (int32_t)(ns - result.tv_sec * GPR_NS_PER_SEC); + if (t >= 0) { + out.tv_sec = t / n; + } else { + out.tv_sec = (-((n - 1) - (t + n)) / n) - 1; + } + out.tv_nsec = (int32_t)((t - out.tv_sec * n) * GPR_NS_PER_SEC / n); + out.clock_type = type; } - return result; + return out; } -gpr_timespec gpr_time_from_micros(int64_t us, gpr_clock_type type) { - gpr_timespec result; - result.clock_type = type; - if (us == INT64_MAX) { - result = gpr_inf_future(type); - } else if (us == INT64_MIN) { - result = gpr_inf_past(type); - } else if (us >= 0) { - result.tv_sec = us / 1000000; - result.tv_nsec = (int32_t)((us - result.tv_sec * 1000000) * 1000); +static gpr_timespec get_timespec_by_hour_or_minute_unit(int64_t t, int64_t n, + gpr_clock_type type) { + gpr_timespec out; + if (t >= INT64_MAX / n) { + out = gpr_inf_future(type); + } else if (t <= INT64_MIN / n) { + out = gpr_inf_past(type); } else { - /* Calculation carefully formulated to avoid any possible under/overflow. */ - result.tv_sec = (-(999999 - (us + 1000000)) / 1000000) - 1; - result.tv_nsec = (int32_t)((us - result.tv_sec * 1000000) * 1000); + out.tv_sec = t * n; + out.tv_nsec = 0; + out.clock_type = type; } - return result; + return out; +} + +gpr_timespec gpr_time_from_nanos(int64_t ns, gpr_clock_type type) { + return get_timespec_by_different_second_unit(ns, GPR_NS_PER_SEC, type); +} + +gpr_timespec gpr_time_from_micros(int64_t us, gpr_clock_type type) { + return get_timespec_by_different_second_unit(us, GPR_US_PER_SEC, type); } gpr_timespec gpr_time_from_millis(int64_t ms, gpr_clock_type type) { - gpr_timespec result; - result.clock_type = type; - if (ms == INT64_MAX) { - result = gpr_inf_future(type); - } else if (ms == INT64_MIN) { - result = gpr_inf_past(type); - } else if (ms >= 0) { - result.tv_sec = ms / 1000; - result.tv_nsec = (int32_t)((ms - result.tv_sec * 1000) * 1000000); - } else { - /* Calculation carefully formulated to avoid any possible under/overflow. */ - result.tv_sec = (-(999 - (ms + 1000)) / 1000) - 1; - result.tv_nsec = (int32_t)((ms - result.tv_sec * 1000) * 1000000); - } - return result; + return get_timespec_by_different_second_unit(ms, GPR_MS_PER_SEC, type); } gpr_timespec gpr_time_from_seconds(int64_t s, gpr_clock_type type) { - gpr_timespec result; - result.clock_type = type; - if (s == INT64_MAX) { - result = gpr_inf_future(type); - } else if (s == INT64_MIN) { - result = gpr_inf_past(type); - } else { - result.tv_sec = s; - result.tv_nsec = 0; - } - return result; + return get_timespec_by_different_second_unit(s, 1, type); } gpr_timespec gpr_time_from_minutes(int64_t m, gpr_clock_type type) { - gpr_timespec result; - result.clock_type = type; - if (m >= INT64_MAX / 60) { - result = gpr_inf_future(type); - } else if (m <= INT64_MIN / 60) { - result = gpr_inf_past(type); - } else { - result.tv_sec = m * 60; - result.tv_nsec = 0; - } - return result; + return get_timespec_by_hour_or_minute_unit(m, 60, type); } gpr_timespec gpr_time_from_hours(int64_t h, gpr_clock_type type) { - gpr_timespec result; - result.clock_type = type; - if (h >= INT64_MAX / 3600) { - result = gpr_inf_future(type); - } else if (h <= INT64_MIN / 3600) { - result = gpr_inf_past(type); - } else { - result.tv_sec = h * 3600; - result.tv_nsec = 0; - } - return result; + return get_timespec_by_hour_or_minute_unit(h, 3600, type); } gpr_timespec gpr_time_add(gpr_timespec a, gpr_timespec b) { -- cgit v1.2.3 From 614e6f9e58c28c1c8012622bd3af7449fda3f55e Mon Sep 17 00:00:00 2001 From: thinkerou Date: Sat, 9 Jul 2016 10:11:28 +0800 Subject: Update the clearer name of function --- src/core/lib/support/time.c | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) (limited to 'src/core') diff --git a/src/core/lib/support/time.c b/src/core/lib/support/time.c index 238317ffb4..bc0a9afc15 100644 --- a/src/core/lib/support/time.c +++ b/src/core/lib/support/time.c @@ -80,34 +80,38 @@ gpr_timespec gpr_inf_past(gpr_clock_type type) { return out; } -static gpr_timespec get_timespec_by_different_second_unit(int64_t t, int64_t n, - gpr_clock_type type) { +static gpr_timespec to_seconds_from_sub_second_time(int64_t time_in_units, + int64_t units_per_sec, + gpr_clock_type type) { gpr_timespec out; - if (t == INT64_MAX) { + if (time_in_units == INT64_MAX) { out = gpr_inf_future(type); - } else if (t == INT64_MIN) { + } else if (time_in_units == INT64_MIN) { out = gpr_inf_past(type); } else { - if (t >= 0) { - out.tv_sec = t / n; + if (time_in_units >= 0) { + out.tv_sec = time_in_units / units_per_sec; } else { - out.tv_sec = (-((n - 1) - (t + n)) / n) - 1; + out.tv_sec = (-((units_per_sec - 1) - + (time_in_units + units_per_sec)) / units_per_sec) - 1; } - out.tv_nsec = (int32_t)((t - out.tv_sec * n) * GPR_NS_PER_SEC / n); + out.tv_nsec = (int32_t)((time_in_units - out.tv_sec * units_per_sec) * + GPR_NS_PER_SEC / units_per_sec); out.clock_type = type; } return out; } -static gpr_timespec get_timespec_by_hour_or_minute_unit(int64_t t, int64_t n, - gpr_clock_type type) { +static gpr_timespec to_seconds_from_above_second_time(int64_t time_in_units, + int64_t secs_per_unit, + gpr_clock_type type) { gpr_timespec out; - if (t >= INT64_MAX / n) { + if (time_in_units >= INT64_MAX / secs_per_unit) { out = gpr_inf_future(type); - } else if (t <= INT64_MIN / n) { + } else if (time_in_units <= INT64_MIN / secs_per_unit) { out = gpr_inf_past(type); } else { - out.tv_sec = t * n; + out.tv_sec = time_in_units * secs_per_unit; out.tv_nsec = 0; out.clock_type = type; } @@ -115,27 +119,27 @@ static gpr_timespec get_timespec_by_hour_or_minute_unit(int64_t t, int64_t n, } gpr_timespec gpr_time_from_nanos(int64_t ns, gpr_clock_type type) { - return get_timespec_by_different_second_unit(ns, GPR_NS_PER_SEC, type); + return to_seconds_from_sub_second_time(ns, GPR_NS_PER_SEC, type); } gpr_timespec gpr_time_from_micros(int64_t us, gpr_clock_type type) { - return get_timespec_by_different_second_unit(us, GPR_US_PER_SEC, type); + return to_seconds_from_sub_second_time(us, GPR_US_PER_SEC, type); } gpr_timespec gpr_time_from_millis(int64_t ms, gpr_clock_type type) { - return get_timespec_by_different_second_unit(ms, GPR_MS_PER_SEC, type); + return to_seconds_from_sub_second_time(ms, GPR_MS_PER_SEC, type); } gpr_timespec gpr_time_from_seconds(int64_t s, gpr_clock_type type) { - return get_timespec_by_different_second_unit(s, 1, type); + return to_seconds_from_sub_second_time(s, 1, type); } gpr_timespec gpr_time_from_minutes(int64_t m, gpr_clock_type type) { - return get_timespec_by_hour_or_minute_unit(m, 60, type); + return to_seconds_from_above_second_time(m, 60, type); } gpr_timespec gpr_time_from_hours(int64_t h, gpr_clock_type type) { - return get_timespec_by_hour_or_minute_unit(h, 3600, type); + return to_seconds_from_above_second_time(h, 3600, type); } gpr_timespec gpr_time_add(gpr_timespec a, gpr_timespec b) { -- cgit v1.2.3 From b433830d1ab91e45a29e08d79efe766015157e7c Mon Sep 17 00:00:00 2001 From: thinkerou Date: Sat, 9 Jul 2016 10:40:44 +0800 Subject: Fixed code format --- src/core/lib/support/time.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/core') diff --git a/src/core/lib/support/time.c b/src/core/lib/support/time.c index bc0a9afc15..5a7d043aed 100644 --- a/src/core/lib/support/time.c +++ b/src/core/lib/support/time.c @@ -92,8 +92,9 @@ static gpr_timespec to_seconds_from_sub_second_time(int64_t time_in_units, if (time_in_units >= 0) { out.tv_sec = time_in_units / units_per_sec; } else { - out.tv_sec = (-((units_per_sec - 1) - - (time_in_units + units_per_sec)) / units_per_sec) - 1; + out.tv_sec = (-((units_per_sec - 1) - (time_in_units + units_per_sec)) / + units_per_sec) - + 1; } out.tv_nsec = (int32_t)((time_in_units - out.tv_sec * units_per_sec) * GPR_NS_PER_SEC / units_per_sec); -- cgit v1.2.3 From ef6b991d63f6c170a1886d66a27f8387ef7818e6 Mon Sep 17 00:00:00 2001 From: David Garcia Quintas Date: Mon, 11 Jul 2016 22:18:59 -0700 Subject: Fix tsan race --- src/core/lib/iomgr/network_status_tracker.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) (limited to 'src/core') diff --git a/src/core/lib/iomgr/network_status_tracker.c b/src/core/lib/iomgr/network_status_tracker.c index 38a1c9b7d4..ccbe136db9 100644 --- a/src/core/lib/iomgr/network_status_tracker.c +++ b/src/core/lib/iomgr/network_status_tracker.c @@ -42,27 +42,24 @@ typedef struct endpoint_ll_node { static endpoint_ll_node *head = NULL; static gpr_mu g_endpoint_mutex; -static bool g_init_done = false; +static gpr_once g_once_init = GPR_ONCE_INIT; -void grpc_initialize_network_status_monitor() { - g_init_done = true; - gpr_mu_init(&g_endpoint_mutex); - // TODO(makarandd): Install callback with OS to monitor network status. -} - -void grpc_destroy_network_status_monitor() { - for (endpoint_ll_node *curr = head; curr != NULL;) { - endpoint_ll_node *next = curr->next; - gpr_free(curr); - curr = next; +static void destroy_network_status_monitor(void) { + if (head != NULL) { + gpr_log(GPR_ERROR, + "Memory leaked as all network endpoints were not shut down"); } gpr_mu_destroy(&g_endpoint_mutex); } +static void initialize_network_status_monitor(void) { + gpr_mu_init(&g_endpoint_mutex); + atexit(destroy_network_status_monitor); + // TODO(makarandd): Install callback with OS to monitor network status. +} + void grpc_network_status_register_endpoint(grpc_endpoint *ep) { - if (!g_init_done) { - grpc_initialize_network_status_monitor(); - } + gpr_once_init(&g_once_init, initialize_network_status_monitor); gpr_mu_lock(&g_endpoint_mutex); if (head == NULL) { head = (endpoint_ll_node *)gpr_malloc(sizeof(endpoint_ll_node)); -- cgit v1.2.3