diff options
author | Vijay Pai <vpai@google.com> | 2017-12-02 22:20:50 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-02 22:20:50 -0800 |
commit | 7d7babb68987b3d572e21dfb8a4db73995872fa8 (patch) | |
tree | c8dfe24a4179fd3cfe46d383ad35fd7f05041ef3 /src/core/lib/support | |
parent | da341bcb8969264800f63eac718e5b483aba9896 (diff) | |
parent | d1945788c5ef92c3ed772da945e4f74a1681381a (diff) |
Merge branch 'master' into threadnames
Diffstat (limited to 'src/core/lib/support')
24 files changed, 456 insertions, 300 deletions
diff --git a/src/core/lib/support/arena.h b/src/core/lib/support/arena.h index 4d43c56bb9..cfe973a036 100644 --- a/src/core/lib/support/arena.h +++ b/src/core/lib/support/arena.h @@ -27,10 +27,6 @@ #include <stddef.h> -#ifdef __cplusplus -extern "C" { -#endif - typedef struct gpr_arena gpr_arena; // Create an arena, with \a initial_size bytes in the first allocated buffer @@ -40,8 +36,4 @@ void* gpr_arena_alloc(gpr_arena* arena, size_t size); // Destroy an arena, returning the total number of bytes allocated size_t gpr_arena_destroy(gpr_arena* arena); -#ifdef __cplusplus -} -#endif - #endif /* GRPC_CORE_LIB_SUPPORT_ARENA_H */ diff --git a/src/core/lib/support/debug_location.h b/src/core/lib/support/debug_location.h new file mode 100644 index 0000000000..0939da595d --- /dev/null +++ b/src/core/lib/support/debug_location.h @@ -0,0 +1,52 @@ +/* + * + * Copyright 2017 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef GRPC_CORE_LIB_SUPPORT_DEBUG_LOCATION_H +#define GRPC_CORE_LIB_SUPPORT_DEBUG_LOCATION_H + +namespace grpc_core { + +// Used for tracking file and line where a call is made for debug builds. +// No-op for non-debug builds. +// Callers can use the DEBUG_LOCATION macro in either case. +#ifndef NDEBUG +class DebugLocation { + public: + DebugLocation(const char* file, int line) : file_(file), line_(line) {} + bool Log() const { return true; } + const char* file() const { return file_; } + int line() const { return line_; } + + private: + const char* file_; + const int line_; +}; +#define DEBUG_LOCATION DebugLocation(__FILE__, __LINE__) +#else +class DebugLocation { + public: + bool Log() const { return false; } + const char* file() const { return nullptr; } + int line() const { return -1; } +}; +#define DEBUG_LOCATION DebugLocation() +#endif + +} // namespace grpc_core + +#endif /* GRPC_CORE_LIB_SUPPORT_DEBUG_LOCATION_H */ diff --git a/src/core/lib/support/env.h b/src/core/lib/support/env.h index f50d7bcb7a..2452fd330d 100644 --- a/src/core/lib/support/env.h +++ b/src/core/lib/support/env.h @@ -21,10 +21,6 @@ #include <stdio.h> -#ifdef __cplusplus -extern "C" { -#endif - /* Env utility functions */ /* Gets the environment variable value with the specified name. @@ -42,8 +38,4 @@ void gpr_setenv(const char* name, const char* value); level of logging. So DO NOT USE THIS. */ const char* gpr_getenv_silent(const char* name, char** dst); -#ifdef __cplusplus -} -#endif - #endif /* GRPC_CORE_LIB_SUPPORT_ENV_H */ diff --git a/src/core/lib/support/fork.cc b/src/core/lib/support/fork.cc new file mode 100644 index 0000000000..d59ca5584c --- /dev/null +++ b/src/core/lib/support/fork.cc @@ -0,0 +1,62 @@ +/* + * + * Copyright 2017 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include "src/core/lib/support/fork.h" + +#include <string.h> + +#include <grpc/support/alloc.h> +#include <grpc/support/useful.h> + +#include "src/core/lib/support/env.h" + +/* + * NOTE: FORKING IS NOT GENERALLY SUPPORTED, THIS IS ONLY INTENDED TO WORK + * AROUND VERY SPECIFIC USE CASES. + */ + +static int override_fork_support_enabled = -1; +static int fork_support_enabled; + +void grpc_fork_support_init() { +#ifdef GRPC_ENABLE_FORK_SUPPORT + fork_support_enabled = 1; +#else + fork_support_enabled = 0; + char* env = gpr_getenv("GRPC_ENABLE_FORK_SUPPORT"); + if (env != NULL) { + static const char* truthy[] = {"yes", "Yes", "YES", "true", + "True", "TRUE", "1"}; + for (size_t i = 0; i < GPR_ARRAY_SIZE(truthy); i++) { + if (0 == strcmp(env, truthy[i])) { + fork_support_enabled = 1; + } + } + gpr_free(env); + } +#endif + if (override_fork_support_enabled != -1) { + fork_support_enabled = override_fork_support_enabled; + } +} + +int grpc_fork_support_enabled() { return fork_support_enabled; } + +void grpc_enable_fork_support(int enable) { + override_fork_support_enabled = enable; +} diff --git a/src/core/lib/support/fork.h b/src/core/lib/support/fork.h new file mode 100644 index 0000000000..215d4214a6 --- /dev/null +++ b/src/core/lib/support/fork.h @@ -0,0 +1,35 @@ +/* + * + * Copyright 2017 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef GRPC_CORE_LIB_SUPPORT_FORK_H +#define GRPC_CORE_LIB_SUPPORT_FORK_H + +/* + * NOTE: FORKING IS NOT GENERALLY SUPPORTED, THIS IS ONLY INTENDED TO WORK + * AROUND VERY SPECIFIC USE CASES. + */ + +void grpc_fork_support_init(void); + +int grpc_fork_support_enabled(void); + +// Test only: Must be called before grpc_init(), and overrides +// environment variables/compile flags +void grpc_enable_fork_support(int enable); + +#endif /* GRPC_CORE_LIB_SUPPORT_FORK_H */ diff --git a/src/core/lib/support/histogram.cc b/src/core/lib/support/histogram.cc deleted file mode 100644 index 73c821a28b..0000000000 --- a/src/core/lib/support/histogram.cc +++ /dev/null @@ -1,227 +0,0 @@ -/* - * - * Copyright 2015 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include <grpc/support/histogram.h> - -#include <math.h> -#include <stddef.h> -#include <string.h> - -#include <grpc/support/alloc.h> -#include <grpc/support/log.h> -#include <grpc/support/port_platform.h> -#include <grpc/support/useful.h> - -/* Histograms are stored with exponentially increasing bucket sizes. - The first bucket is [0, m) where m = 1 + resolution - Bucket n (n>=1) contains [m**n, m**(n+1)) - There are sufficient buckets to reach max_bucket_start */ - -struct gpr_histogram { - /* Sum of all values seen so far */ - double sum; - /* Sum of squares of all values seen so far */ - double sum_of_squares; - /* number of values seen so far */ - double count; - /* m in the description */ - double multiplier; - double one_on_log_multiplier; - /* minimum value seen */ - double min_seen; - /* maximum value seen */ - double max_seen; - /* maximum representable value */ - double max_possible; - /* number of buckets */ - size_t num_buckets; - /* the buckets themselves */ - uint32_t* buckets; -}; - -/* determine a bucket index given a value - does no bounds checking */ -static size_t bucket_for_unchecked(gpr_histogram* h, double x) { - return (size_t)(log(x) * h->one_on_log_multiplier); -} - -/* bounds checked version of the above */ -static size_t bucket_for(gpr_histogram* h, double x) { - size_t bucket = bucket_for_unchecked(h, GPR_CLAMP(x, 1.0, h->max_possible)); - GPR_ASSERT(bucket < h->num_buckets); - return bucket; -} - -/* at what value does a bucket start? */ -static double bucket_start(gpr_histogram* h, double x) { - return pow(h->multiplier, x); -} - -gpr_histogram* gpr_histogram_create(double resolution, - double max_bucket_start) { - gpr_histogram* h = (gpr_histogram*)gpr_malloc(sizeof(gpr_histogram)); - GPR_ASSERT(resolution > 0.0); - GPR_ASSERT(max_bucket_start > resolution); - h->sum = 0.0; - h->sum_of_squares = 0.0; - h->multiplier = 1.0 + resolution; - h->one_on_log_multiplier = 1.0 / log(1.0 + resolution); - h->max_possible = max_bucket_start; - h->count = 0.0; - h->min_seen = max_bucket_start; - h->max_seen = 0.0; - h->num_buckets = bucket_for_unchecked(h, max_bucket_start) + 1; - GPR_ASSERT(h->num_buckets > 1); - GPR_ASSERT(h->num_buckets < 100000000); - h->buckets = (uint32_t*)gpr_zalloc(sizeof(uint32_t) * h->num_buckets); - return h; -} - -void gpr_histogram_destroy(gpr_histogram* h) { - gpr_free(h->buckets); - gpr_free(h); -} - -void gpr_histogram_add(gpr_histogram* h, double x) { - h->sum += x; - h->sum_of_squares += x * x; - h->count++; - if (x < h->min_seen) { - h->min_seen = x; - } - if (x > h->max_seen) { - h->max_seen = x; - } - h->buckets[bucket_for(h, x)]++; -} - -int gpr_histogram_merge(gpr_histogram* dst, const gpr_histogram* src) { - if ((dst->num_buckets != src->num_buckets) || - (dst->multiplier != src->multiplier)) { - /* Fail because these histograms don't match */ - return 0; - } - gpr_histogram_merge_contents(dst, src->buckets, src->num_buckets, - src->min_seen, src->max_seen, src->sum, - src->sum_of_squares, src->count); - return 1; -} - -void gpr_histogram_merge_contents(gpr_histogram* dst, const uint32_t* data, - size_t data_count, double min_seen, - double max_seen, double sum, - double sum_of_squares, double count) { - size_t i; - GPR_ASSERT(dst->num_buckets == data_count); - dst->sum += sum; - dst->sum_of_squares += sum_of_squares; - dst->count += count; - if (min_seen < dst->min_seen) { - dst->min_seen = min_seen; - } - if (max_seen > dst->max_seen) { - dst->max_seen = max_seen; - } - for (i = 0; i < dst->num_buckets; i++) { - dst->buckets[i] += data[i]; - } -} - -static double threshold_for_count_below(gpr_histogram* h, double count_below) { - double count_so_far; - double lower_bound; - double upper_bound; - size_t lower_idx; - size_t upper_idx; - - if (h->count == 0) { - return 0.0; - } - - if (count_below <= 0) { - return h->min_seen; - } - if (count_below >= h->count) { - return h->max_seen; - } - - /* find the lowest bucket that gets us above count_below */ - count_so_far = 0.0; - for (lower_idx = 0; lower_idx < h->num_buckets; lower_idx++) { - count_so_far += h->buckets[lower_idx]; - if (count_so_far >= count_below) { - break; - } - } - if (count_so_far == count_below) { - /* this bucket hits the threshold exactly... we should be midway through - any run of zero values following the bucket */ - for (upper_idx = lower_idx + 1; upper_idx < h->num_buckets; upper_idx++) { - if (h->buckets[upper_idx]) { - break; - } - } - return (bucket_start(h, (double)lower_idx) + - bucket_start(h, (double)upper_idx)) / - 2.0; - } else { - /* treat values as uniform throughout the bucket, and find where this value - should lie */ - lower_bound = bucket_start(h, (double)lower_idx); - upper_bound = bucket_start(h, (double)(lower_idx + 1)); - return GPR_CLAMP(upper_bound - (upper_bound - lower_bound) * - (count_so_far - count_below) / - h->buckets[lower_idx], - h->min_seen, h->max_seen); - } -} - -double gpr_histogram_percentile(gpr_histogram* h, double percentile) { - return threshold_for_count_below(h, h->count * percentile / 100.0); -} - -double gpr_histogram_mean(gpr_histogram* h) { - GPR_ASSERT(h->count != 0); - return h->sum / h->count; -} - -double gpr_histogram_stddev(gpr_histogram* h) { - return sqrt(gpr_histogram_variance(h)); -} - -double gpr_histogram_variance(gpr_histogram* h) { - if (h->count == 0) return 0.0; - return (h->sum_of_squares * h->count - h->sum * h->sum) / - (h->count * h->count); -} - -double gpr_histogram_maximum(gpr_histogram* h) { return h->max_seen; } - -double gpr_histogram_minimum(gpr_histogram* h) { return h->min_seen; } - -double gpr_histogram_count(gpr_histogram* h) { return h->count; } - -double gpr_histogram_sum(gpr_histogram* h) { return h->sum; } - -double gpr_histogram_sum_of_squares(gpr_histogram* h) { - return h->sum_of_squares; -} - -const uint32_t* gpr_histogram_get_contents(gpr_histogram* h, size_t* size) { - *size = h->num_buckets; - return h->buckets; -} diff --git a/src/core/lib/support/log.cc b/src/core/lib/support/log.cc index e9adc6c349..2a40745e97 100644 --- a/src/core/lib/support/log.cc +++ b/src/core/lib/support/log.cc @@ -27,7 +27,7 @@ #include <stdio.h> #include <string.h> -extern "C" void gpr_default_log(gpr_log_func_args* args); +void gpr_default_log(gpr_log_func_args* args); static gpr_atm g_log_func = (gpr_atm)gpr_default_log; static gpr_atm g_min_severity_to_print = GPR_LOG_VERBOSITY_UNSET; diff --git a/src/core/lib/support/log_android.cc b/src/core/lib/support/log_android.cc index 73d24cd84d..0d3ac0fe52 100644 --- a/src/core/lib/support/log_android.cc +++ b/src/core/lib/support/log_android.cc @@ -39,8 +39,8 @@ static android_LogPriority severity_to_log_priority(gpr_log_severity severity) { return ANDROID_LOG_DEFAULT; } -extern "C" void gpr_log(const char* file, int line, gpr_log_severity severity, - const char* format, ...) { +void gpr_log(const char* file, int line, gpr_log_severity severity, + const char* format, ...) { char* message = NULL; va_list args; va_start(args, format); @@ -50,7 +50,7 @@ extern "C" void gpr_log(const char* file, int line, gpr_log_severity severity, free(message); } -extern "C" void gpr_default_log(gpr_log_func_args* args) { +void gpr_default_log(gpr_log_func_args* args) { const char* final_slash; const char* display_file; char* output = NULL; diff --git a/src/core/lib/support/log_linux.cc b/src/core/lib/support/log_linux.cc index e0e277fe87..6b1f1c71e4 100644 --- a/src/core/lib/support/log_linux.cc +++ b/src/core/lib/support/log_linux.cc @@ -56,7 +56,7 @@ void gpr_log(const char* file, int line, gpr_log_severity severity, free(message); } -extern "C" void gpr_default_log(gpr_log_func_args* args) { +void gpr_default_log(gpr_log_func_args* args) { const char* final_slash; char* prefix; const char* display_file; diff --git a/src/core/lib/support/log_posix.cc b/src/core/lib/support/log_posix.cc index e765f91390..9fab480a8d 100644 --- a/src/core/lib/support/log_posix.cc +++ b/src/core/lib/support/log_posix.cc @@ -56,7 +56,7 @@ void gpr_log(const char* file, int line, gpr_log_severity severity, gpr_free(allocated); } -extern "C" void gpr_default_log(gpr_log_func_args* args) { +void gpr_default_log(gpr_log_func_args* args) { const char* final_slash; const char* display_file; char time_buffer[64]; diff --git a/src/core/lib/support/log_windows.cc b/src/core/lib/support/log_windows.cc index d4481791e2..0013bf448f 100644 --- a/src/core/lib/support/log_windows.cc +++ b/src/core/lib/support/log_windows.cc @@ -65,7 +65,7 @@ void gpr_log(const char* file, int line, gpr_log_severity severity, } /* Simple starter implementation */ -extern "C" void gpr_default_log(gpr_log_func_args* args) { +void gpr_default_log(gpr_log_func_args* args) { const char* final_slash; const char* display_file; char time_buffer[64]; diff --git a/src/core/lib/support/mpscq.h b/src/core/lib/support/mpscq.h index fb22742050..648ead1f5b 100644 --- a/src/core/lib/support/mpscq.h +++ b/src/core/lib/support/mpscq.h @@ -24,10 +24,6 @@ #include <stdbool.h> #include <stddef.h> -#ifdef __cplusplus -extern "C" { -#endif - // Multiple-producer single-consumer lock free queue, based upon the // implementation from Dmitry Vyukov here: // http://www.1024cores.net/home/lock-free-algorithms/queues/intrusive-mpsc-node-based-queue @@ -84,8 +80,5 @@ gpr_mpscq_node* gpr_locked_mpscq_try_pop(gpr_locked_mpscq* q); // Pop a node. Returns NULL only if the queue was empty at some point after // calling this function gpr_mpscq_node* gpr_locked_mpscq_pop(gpr_locked_mpscq* q); -#ifdef __cplusplus -} -#endif #endif /* GRPC_CORE_LIB_SUPPORT_MPSCQ_H */ diff --git a/src/core/lib/support/murmur_hash.h b/src/core/lib/support/murmur_hash.h index d02bba6962..422770f103 100644 --- a/src/core/lib/support/murmur_hash.h +++ b/src/core/lib/support/murmur_hash.h @@ -23,15 +23,7 @@ #include <stddef.h> -#ifdef __cplusplus -extern "C" { -#endif - /* compute the hash of key (length len) */ uint32_t gpr_murmur_hash3(const void* key, size_t len, uint32_t seed); -#ifdef __cplusplus -} -#endif - #endif /* GRPC_CORE_LIB_SUPPORT_MURMUR_HASH_H */ diff --git a/src/core/lib/support/ref_counted.h b/src/core/lib/support/ref_counted.h new file mode 100644 index 0000000000..4c662f9119 --- /dev/null +++ b/src/core/lib/support/ref_counted.h @@ -0,0 +1,122 @@ +/* + * + * Copyright 2017 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef GRPC_CORE_LIB_SUPPORT_REF_COUNTED_H +#define GRPC_CORE_LIB_SUPPORT_REF_COUNTED_H + +#include <grpc/support/log.h> +#include <grpc/support/sync.h> + +#include "src/core/lib/debug/trace.h" +#include "src/core/lib/support/debug_location.h" +#include "src/core/lib/support/memory.h" + +namespace grpc_core { + +// A base class for reference-counted objects. +// New objects should be created via New() and start with a refcount of 1. +// When the refcount reaches 0, the object will be deleted via Delete(). +class RefCounted { + public: + void Ref() { gpr_ref(&refs_); } + + void Unref() { + if (gpr_unref(&refs_)) { + Delete(this); + } + } + + // Not copyable nor movable. + RefCounted(const RefCounted&) = delete; + RefCounted& operator=(const RefCounted&) = delete; + + protected: + // Allow Delete() to access destructor. + template <typename T> + friend void Delete(T*); + + RefCounted() { gpr_ref_init(&refs_, 1); } + + virtual ~RefCounted() {} + + private: + gpr_refcount refs_; +}; + +// An alternative version of the RefCounted base class that +// supports tracing. This is intended to be used in cases where the +// object will be handled both by idiomatic C++ code using smart +// pointers and legacy code that is manually calling Ref() and Unref(). +// Once all of our code is converted to idiomatic C++, we may be able to +// eliminate this class. +class RefCountedWithTracing { + public: + void Ref() { gpr_ref(&refs_); } + + void Ref(const DebugLocation& location, const char* reason) { + if (location.Log() && trace_flag_ != nullptr && trace_flag_->enabled()) { + gpr_atm old_refs = gpr_atm_no_barrier_load(&refs_.count); + gpr_log(GPR_DEBUG, "%s:%p %s:%d ref %" PRIdPTR " -> %" PRIdPTR " %s", + trace_flag_->name(), this, location.file(), location.line(), + old_refs, old_refs + 1, reason); + } + Ref(); + } + + void Unref() { + if (gpr_unref(&refs_)) { + Delete(this); + } + } + + void Unref(const DebugLocation& location, const char* reason) { + if (location.Log() && trace_flag_ != nullptr && trace_flag_->enabled()) { + gpr_atm old_refs = gpr_atm_no_barrier_load(&refs_.count); + gpr_log(GPR_DEBUG, "%s:%p %s:%d unref %" PRIdPTR " -> %" PRIdPTR " %s", + trace_flag_->name(), this, location.file(), location.line(), + old_refs, old_refs - 1, reason); + } + Unref(); + } + + // Not copyable nor movable. + RefCountedWithTracing(const RefCountedWithTracing&) = delete; + RefCountedWithTracing& operator=(const RefCountedWithTracing&) = delete; + + protected: + // Allow Delete() to access destructor. + template <typename T> + friend void Delete(T*); + + RefCountedWithTracing() : RefCountedWithTracing(nullptr) {} + + explicit RefCountedWithTracing(TraceFlag* trace_flag) + : trace_flag_(trace_flag) { + gpr_ref_init(&refs_, 1); + } + + virtual ~RefCountedWithTracing() {} + + private: + TraceFlag* trace_flag_ = nullptr; + gpr_refcount refs_; +}; + +} // namespace grpc_core + +#endif /* GRPC_CORE_LIB_SUPPORT_REF_COUNTED_H */ diff --git a/src/core/lib/support/ref_counted_ptr.h b/src/core/lib/support/ref_counted_ptr.h new file mode 100644 index 0000000000..dc2385e369 --- /dev/null +++ b/src/core/lib/support/ref_counted_ptr.h @@ -0,0 +1,90 @@ +/* + * + * Copyright 2017 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef GRPC_CORE_LIB_SUPPORT_REF_COUNTED_PTR_H +#define GRPC_CORE_LIB_SUPPORT_REF_COUNTED_PTR_H + +#include <utility> + +#include "src/core/lib/support/memory.h" + +namespace grpc_core { + +// A smart pointer class for objects that provide Ref() and Unref() methods, +// such as those provided by the RefCounted base class. +template <typename T> +class RefCountedPtr { + public: + RefCountedPtr() {} + + // If value is non-null, we take ownership of a ref to it. + explicit RefCountedPtr(T* value) { value_ = value; } + + // Move support. + RefCountedPtr(RefCountedPtr&& other) { + value_ = other.value_; + other.value_ = nullptr; + } + RefCountedPtr& operator=(RefCountedPtr&& other) { + if (value_ != nullptr) value_->Unref(); + value_ = other.value_; + other.value_ = nullptr; + return *this; + } + + // Copy support. + RefCountedPtr(const RefCountedPtr& other) { + if (other.value_ != nullptr) other.value_->Ref(); + value_ = other.value_; + } + RefCountedPtr& operator=(const RefCountedPtr& other) { + // Note: Order of reffing and unreffing is important here in case value_ + // and other.value_ are the same object. + if (other.value_ != nullptr) other.value_->Ref(); + if (value_ != nullptr) value_->Unref(); + value_ = other.value_; + return *this; + } + + ~RefCountedPtr() { + if (value_ != nullptr) value_->Unref(); + } + + // If value is non-null, we take ownership of a ref to it. + void reset(T* value = nullptr) { + if (value_ != nullptr) value_->Unref(); + value_ = value; + } + + T* get() const { return value_; } + + T& operator*() const { return *value_; } + T* operator->() const { return value_; } + + private: + T* value_ = nullptr; +}; + +template <typename T, typename... Args> +inline RefCountedPtr<T> MakeRefCounted(Args&&... args) { + return RefCountedPtr<T>(New<T>(std::forward<Args>(args)...)); +} + +} // namespace grpc_core + +#endif /* GRPC_CORE_LIB_SUPPORT_REF_COUNTED_PTR_H */ diff --git a/src/core/lib/support/string.h b/src/core/lib/support/string.h index 0b18ffcec1..dd37f0b0e1 100644 --- a/src/core/lib/support/string.h +++ b/src/core/lib/support/string.h @@ -24,10 +24,6 @@ #include <grpc/support/port_platform.h> -#ifdef __cplusplus -extern "C" { -#endif - /* String utility functions */ /* Flags for gpr_dump function. */ @@ -109,8 +105,5 @@ void* gpr_memrchr(const void* s, int c, size_t n); /** Return true if lower(s) equals "true", "yes" or "1", otherwise false. */ bool gpr_is_true(const char* s); -#ifdef __cplusplus -} -#endif #endif /* GRPC_CORE_LIB_SUPPORT_STRING_H */ diff --git a/src/core/lib/support/string_windows.h b/src/core/lib/support/string_windows.h index 6771647581..7c7f31e7aa 100644 --- a/src/core/lib/support/string_windows.h +++ b/src/core/lib/support/string_windows.h @@ -21,10 +21,6 @@ #include <grpc/support/port_platform.h> -#ifdef __cplusplus -extern "C" { -#endif - #ifdef GPR_WINDOWS /* These allocate new strings using gpr_malloc to convert from and to utf-8. */ @@ -33,8 +29,4 @@ LPSTR gpr_tchar_to_char(LPCTSTR input); #endif /* GPR_WINDOWS */ -#ifdef __cplusplus -} -#endif - #endif /* GRPC_CORE_LIB_SUPPORT_STRING_WINDOWS_H */ diff --git a/src/core/lib/support/thd_internal.h b/src/core/lib/support/thd_internal.h new file mode 100644 index 0000000000..38bffc847d --- /dev/null +++ b/src/core/lib/support/thd_internal.h @@ -0,0 +1,30 @@ +/* + * + * Copyright 2015 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef GRPC_CORE_LIB_SUPPORT_THD_INTERNAL_H +#define GRPC_CORE_LIB_SUPPORT_THD_INTERNAL_H + +#include <grpc/support/time.h> + +/* Internal interfaces between modules within the gpr support library. */ +void gpr_thd_init(); + +/* Wait for all outstanding threads to finish, up to deadline */ +int gpr_await_threads(gpr_timespec deadline); + +#endif /* GRPC_CORE_LIB_SUPPORT_THD_INTERNAL_H */ diff --git a/src/core/lib/support/thd_posix.cc b/src/core/lib/support/thd_posix.cc index fb30880174..339c40fe4c 100644 --- a/src/core/lib/support/thd_posix.cc +++ b/src/core/lib/support/thd_posix.cc @@ -24,18 +24,29 @@ #include <grpc/support/alloc.h> #include <grpc/support/log.h> +#include <grpc/support/sync.h> #include <grpc/support/thd.h> #include <grpc/support/useful.h> #include <pthread.h> #include <stdlib.h> #include <string.h> +#include "src/core/lib/support/fork.h" + +static gpr_mu g_mu; +static gpr_cv g_cv; +static int g_thread_count; +static int g_awaiting_threads; + struct thd_arg { void (*body)(void* arg); /* body of a thread */ void* arg; /* argument to a thread */ const char* name; /* name of thread. Can be nullptr. */ }; +static void inc_thd_count(); +static void dec_thd_count(); + /* Body of every thread started via gpr_thd_new. */ static void* thread_body(void* v) { struct thd_arg a = *(struct thd_arg*)v; @@ -54,6 +65,7 @@ static void* thread_body(void* v) { #endif // GPR_APPLE_PTHREAD_NAME } (*a.body)(a.arg); + dec_thd_count(); return nullptr; } @@ -70,6 +82,7 @@ int gpr_thd_new(gpr_thd_id* t, const char* thd_name, a->body = thd_body; a->arg = arg; a->name = thd_name; + inc_thd_count(); GPR_ASSERT(pthread_attr_init(&attr) == 0); if (gpr_thd_options_is_detached(options)) { @@ -84,6 +97,7 @@ int gpr_thd_new(gpr_thd_id* t, const char* thd_name, if (!thread_started) { /* don't use gpr_free, as this was allocated using malloc (see above) */ free(a); + dec_thd_count(); } *t = (gpr_thd_id)p; return thread_started; @@ -93,4 +107,46 @@ gpr_thd_id gpr_thd_currentid(void) { return (gpr_thd_id)pthread_self(); } void gpr_thd_join(gpr_thd_id t) { pthread_join((pthread_t)t, nullptr); } +/***************************************** + * Only used when fork support is enabled + */ + +static void inc_thd_count() { + if (grpc_fork_support_enabled()) { + gpr_mu_lock(&g_mu); + g_thread_count++; + gpr_mu_unlock(&g_mu); + } +} + +static void dec_thd_count() { + if (grpc_fork_support_enabled()) { + gpr_mu_lock(&g_mu); + g_thread_count--; + if (g_awaiting_threads && g_thread_count == 0) { + gpr_cv_signal(&g_cv); + } + gpr_mu_unlock(&g_mu); + } +} + +void gpr_thd_init() { + gpr_mu_init(&g_mu); + gpr_cv_init(&g_cv); + g_thread_count = 0; + g_awaiting_threads = 0; +} + +int gpr_await_threads(gpr_timespec deadline) { + gpr_mu_lock(&g_mu); + g_awaiting_threads = 1; + int res = 0; + if (g_thread_count > 0) { + res = gpr_cv_wait(&g_cv, &g_mu, deadline); + } + g_awaiting_threads = 0; + gpr_mu_unlock(&g_mu); + return res == 0; +} + #endif /* GPR_POSIX_SYNC */ diff --git a/src/core/lib/support/thd_windows.cc b/src/core/lib/support/thd_windows.cc index b54d0b6c62..f920770f32 100644 --- a/src/core/lib/support/thd_windows.cc +++ b/src/core/lib/support/thd_windows.cc @@ -50,6 +50,8 @@ static void destroy_thread(struct thd_info* t) { gpr_free(t); } +void gpr_thd_init(void) {} + /* Body of every thread started via gpr_thd_new. */ static DWORD WINAPI thread_body(void* v) { g_thd_info = (struct thd_info*)v; diff --git a/src/core/lib/support/time_posix.cc b/src/core/lib/support/time_posix.cc index 7f65205800..47a849480f 100644 --- a/src/core/lib/support/time_posix.cc +++ b/src/core/lib/support/time_posix.cc @@ -127,9 +127,7 @@ static gpr_timespec now_impl(gpr_clock_type clock) { } #endif -extern "C" { gpr_timespec (*gpr_now_impl)(gpr_clock_type clock_type) = now_impl; -} #ifdef GPR_LOW_LEVEL_COUNTERS gpr_atm gpr_now_call_count; diff --git a/src/core/lib/support/time_precise.h b/src/core/lib/support/time_precise.h index 3befda3d86..35cd154dbd 100644 --- a/src/core/lib/support/time_precise.h +++ b/src/core/lib/support/time_precise.h @@ -21,15 +21,7 @@ #include <grpc/support/time.h> -#ifdef __cplusplus -extern "C" { -#endif - void gpr_precise_clock_init(void); void gpr_precise_clock_now(gpr_timespec* clk); -#ifdef __cplusplus -} -#endif - #endif /* GRPC_CORE_LIB_SUPPORT_TIME_PRECISE_H */ diff --git a/src/core/lib/support/time_windows.cc b/src/core/lib/support/time_windows.cc index 08c1b22964..fb17e5c079 100644 --- a/src/core/lib/support/time_windows.cc +++ b/src/core/lib/support/time_windows.cc @@ -68,9 +68,7 @@ static gpr_timespec now_impl(gpr_clock_type clock) { return now_tv; } -extern "C" { gpr_timespec (*gpr_now_impl)(gpr_clock_type clock_type) = now_impl; -} gpr_timespec gpr_now(gpr_clock_type clock_type) { return gpr_now_impl(clock_type); diff --git a/src/core/lib/support/tmpfile.h b/src/core/lib/support/tmpfile.h index 437d871786..c5ceda8675 100644 --- a/src/core/lib/support/tmpfile.h +++ b/src/core/lib/support/tmpfile.h @@ -21,18 +21,10 @@ #include <stdio.h> -#ifdef __cplusplus -extern "C" { -#endif - /* Creates a temporary file from a prefix. If tmp_filename is not NULL, *tmp_filename is assigned the name of the created file and it is the responsibility of the caller to gpr_free it unless an error occurs in which case it will be set to NULL. */ FILE* gpr_tmpfile(const char* prefix, char** tmp_filename); -#ifdef __cplusplus -} -#endif - #endif /* GRPC_CORE_LIB_SUPPORT_TMPFILE_H */ |