diff options
author | Vijay Pai <vpai@google.com> | 2018-02-08 13:24:39 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-08 13:24:39 -0800 |
commit | 0024ebc85232333efdbd18899eb6f7bba6b570d8 (patch) | |
tree | 136410619009bf4d8358464dc68ecc6ae04357b0 /src | |
parent | 2a073981f384cc117e7e6b3835888bbf8d30540c (diff) | |
parent | 25b61fd60e9fd24c3b23eac9396e22745f1cf51d (diff) |
Merge pull request #14196 from vjpai/gpr_review_tls
GPR review: Privatize thread-local storage headers
Diffstat (limited to 'src')
-rw-r--r-- | src/core/ext/filters/client_channel/subchannel_index.cc | 2 | ||||
-rw-r--r-- | src/core/lib/gpr/tls.h | 68 | ||||
-rw-r--r-- | src/core/lib/gpr/tls_gcc.h | 50 | ||||
-rw-r--r-- | src/core/lib/gpr/tls_msvc.h | 50 | ||||
-rw-r--r-- | src/core/lib/gpr/tls_pthread.cc | 2 | ||||
-rw-r--r-- | src/core/lib/gpr/tls_pthread.h | 54 | ||||
-rw-r--r-- | src/core/lib/iomgr/ev_epoll1_linux.cc | 2 | ||||
-rw-r--r-- | src/core/lib/iomgr/ev_epollex_linux.cc | 2 | ||||
-rw-r--r-- | src/core/lib/iomgr/ev_epollsig_linux.cc | 2 | ||||
-rw-r--r-- | src/core/lib/iomgr/ev_poll_posix.cc | 2 | ||||
-rw-r--r-- | src/core/lib/iomgr/exec_ctx.h | 2 | ||||
-rw-r--r-- | src/core/lib/iomgr/executor.cc | 2 | ||||
-rw-r--r-- | src/core/lib/iomgr/timer_generic.cc | 3 | ||||
-rw-r--r-- | src/core/lib/surface/completion_queue.cc | 2 |
14 files changed, 233 insertions, 10 deletions
diff --git a/src/core/ext/filters/client_channel/subchannel_index.cc b/src/core/ext/filters/client_channel/subchannel_index.cc index 052b047f43..6ba0d2f745 100644 --- a/src/core/ext/filters/client_channel/subchannel_index.cc +++ b/src/core/ext/filters/client_channel/subchannel_index.cc @@ -24,9 +24,9 @@ #include <grpc/support/alloc.h> #include <grpc/support/avl.h> #include <grpc/support/string_util.h> -#include <grpc/support/tls.h> #include "src/core/lib/channel/channel_args.h" +#include "src/core/lib/gpr/tls.h" // a map of subchannel_key --> subchannel, used for detecting connections // to the same destination in order to share them diff --git a/src/core/lib/gpr/tls.h b/src/core/lib/gpr/tls.h new file mode 100644 index 0000000000..aee8f4d941 --- /dev/null +++ b/src/core/lib/gpr/tls.h @@ -0,0 +1,68 @@ +/* + * + * 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_GPR_TLS_H +#define GRPC_CORE_LIB_GPR_TLS_H + +#include <grpc/support/port_platform.h> + +/** Thread local storage. + + A minimal wrapper that should be implementable across many compilers, + and implementable efficiently across most modern compilers. + + Thread locals have type intptr_t. + + Declaring a thread local variable 'foo': + GPR_TLS_DECL(foo); + Thread locals always have static scope. + + Declaring a thread local class variable 'foo': + GPR_TLS_CLASS_DECL(foo); + + Defining the thread local class variable: + GPR_TLS_CLASS_DEF(foo); + + Initializing a thread local (must be done at library initialization + time): + gpr_tls_init(&foo); + + Destroying a thread local: + gpr_tls_destroy(&foo); + + Setting a thread local (returns new_value): + gpr_tls_set(&foo, new_value); + + Accessing a thread local: + current_value = gpr_tls_get(&foo); + + ALL functions here may be implemented as macros. */ + +#ifdef GPR_GCC_TLS +#include "src/core/lib/gpr/tls_gcc.h" +#endif + +#ifdef GPR_MSVC_TLS +#include "src/core/lib/gpr/tls_msvc.h" +#endif + +#ifdef GPR_PTHREAD_TLS +#include "src/core/lib/gpr/tls_pthread.h" +#endif + +#endif /* GRPC_CORE_LIB_GPR_TLS_H */ diff --git a/src/core/lib/gpr/tls_gcc.h b/src/core/lib/gpr/tls_gcc.h new file mode 100644 index 0000000000..14c59eca55 --- /dev/null +++ b/src/core/lib/gpr/tls_gcc.h @@ -0,0 +1,50 @@ +/* + * + * 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_GPR_TLS_GCC_H +#define GRPC_CORE_LIB_GPR_TLS_GCC_H + +#include <stdbool.h> + +#include <grpc/support/log.h> + +/** Thread local storage based on gcc compiler primitives. + #include tls.h to use this - and see that file for documentation */ + +struct gpr_gcc_thread_local { + intptr_t value; +}; + +#define GPR_TLS_DECL(name) \ + static __thread struct gpr_gcc_thread_local name = {0} + +#define GPR_TLS_CLASS_DECL(name) \ + static __thread struct gpr_gcc_thread_local name + +#define GPR_TLS_CLASS_DEF(name) __thread struct gpr_gcc_thread_local name = {0} + +#define gpr_tls_init(tls) \ + do { \ + } while (0) +#define gpr_tls_destroy(tls) \ + do { \ + } while (0) +#define gpr_tls_set(tls, new_value) (((tls)->value) = (new_value)) +#define gpr_tls_get(tls) ((tls)->value) + +#endif /* GRPC_CORE_LIB_GPR_TLS_GCC_H */ diff --git a/src/core/lib/gpr/tls_msvc.h b/src/core/lib/gpr/tls_msvc.h new file mode 100644 index 0000000000..a6cc4174be --- /dev/null +++ b/src/core/lib/gpr/tls_msvc.h @@ -0,0 +1,50 @@ +/* + * + * 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_GPR_TLS_MSVC_H +#define GRPC_CORE_LIB_GPR_TLS_MSVC_H + +/** Thread local storage based on ms visual c compiler primitives. + #include tls.h to use this - and see that file for documentation */ + +struct gpr_msvc_thread_local { + intptr_t value; +}; + +/** Use GPR_TLS_DECL to declare tls static variables outside a class */ +#define GPR_TLS_DECL(name) \ + static __declspec(thread) struct gpr_msvc_thread_local name = {0} + +/** Use GPR_TLS_CLASS_DECL to declare tls static variable members of a class. + * GPR_TLS_CLASS_DEF needs to be called to define this member. */ +#define GPR_TLS_CLASS_DECL(name) \ + static __declspec(thread) struct gpr_msvc_thread_local name + +#define GPR_TLS_CLASS_DEF(name) \ + __declspec(thread) struct gpr_msvc_thread_local name = {0} + +#define gpr_tls_init(tls) \ + do { \ + } while (0) +#define gpr_tls_destroy(tls) \ + do { \ + } while (0) +#define gpr_tls_set(tls, new_value) (((tls)->value) = (new_value)) +#define gpr_tls_get(tls) ((tls)->value) + +#endif /* GRPC_CORE_LIB_GPR_TLS_MSVC_H */ diff --git a/src/core/lib/gpr/tls_pthread.cc b/src/core/lib/gpr/tls_pthread.cc index ebeef2a8c2..2e5b306909 100644 --- a/src/core/lib/gpr/tls_pthread.cc +++ b/src/core/lib/gpr/tls_pthread.cc @@ -20,7 +20,7 @@ #ifdef GPR_PTHREAD_TLS -#include <grpc/support/tls.h> +#include "src/core/lib/gpr/tls.h" intptr_t gpr_tls_set(struct gpr_pthread_thread_local* tls, intptr_t value) { GPR_ASSERT(0 == pthread_setspecific(tls->key, (void*)value)); diff --git a/src/core/lib/gpr/tls_pthread.h b/src/core/lib/gpr/tls_pthread.h new file mode 100644 index 0000000000..9202653dcb --- /dev/null +++ b/src/core/lib/gpr/tls_pthread.h @@ -0,0 +1,54 @@ +/* + * + * 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_GPR_TLS_PTHREAD_H +#define GRPC_CORE_LIB_GPR_TLS_PTHREAD_H + +#include <grpc/support/log.h> /* for GPR_ASSERT */ +#include <pthread.h> + +/** Thread local storage based on pthread library calls. + #include tls.h to use this - and see that file for documentation */ + +struct gpr_pthread_thread_local { + pthread_key_t key; +}; + +/** Use GPR_TLS_DECL to declare tls static variables outside a class */ +#define GPR_TLS_DECL(name) static struct gpr_pthread_thread_local name = {0} + +/** Use GPR_TLS_CLASS_DECL to declare tls static variable members of a class. + * GPR_TLS_CLASS_DEF needs to be called to define this member. */ +#define GPR_TLS_CLASS_DECL(name) static struct gpr_pthread_thread_local name + +/** Use GPR_TLS_CLASS_DEF to declare tls static variable members of a class. + * GPR_TLS_CLASS_DEF needs to be called to define this member. */ +#define GPR_TLS_CLASS_DEF(name) struct gpr_pthread_thread_local name = {0} + +#define gpr_tls_init(tls) GPR_ASSERT(0 == pthread_key_create(&(tls)->key, NULL)) +#define gpr_tls_destroy(tls) pthread_key_delete((tls)->key) +#define gpr_tls_get(tls) ((intptr_t)pthread_getspecific((tls)->key)) +#ifdef __cplusplus +extern "C" { +#endif +intptr_t gpr_tls_set(struct gpr_pthread_thread_local* tls, intptr_t value); +#ifdef __cplusplus +} +#endif + +#endif /* GRPC_CORE_LIB_GPR_TLS_PTHREAD_H */ diff --git a/src/core/lib/iomgr/ev_epoll1_linux.cc b/src/core/lib/iomgr/ev_epoll1_linux.cc index 1ef70d2f80..b7b5662922 100644 --- a/src/core/lib/iomgr/ev_epoll1_linux.cc +++ b/src/core/lib/iomgr/ev_epoll1_linux.cc @@ -39,11 +39,11 @@ #include <grpc/support/alloc.h> #include <grpc/support/cpu.h> #include <grpc/support/string_util.h> -#include <grpc/support/tls.h> #include <grpc/support/useful.h> #include "src/core/lib/debug/stats.h" #include "src/core/lib/gpr/string.h" +#include "src/core/lib/gpr/tls.h" #include "src/core/lib/gprpp/manual_constructor.h" #include "src/core/lib/iomgr/block_annotate.h" #include "src/core/lib/iomgr/ev_posix.h" diff --git a/src/core/lib/iomgr/ev_epollex_linux.cc b/src/core/lib/iomgr/ev_epollex_linux.cc index 30c7a89824..cd5a410545 100644 --- a/src/core/lib/iomgr/ev_epollex_linux.cc +++ b/src/core/lib/iomgr/ev_epollex_linux.cc @@ -37,11 +37,11 @@ #include <grpc/support/alloc.h> #include <grpc/support/string_util.h> -#include <grpc/support/tls.h> #include <grpc/support/useful.h> #include "src/core/lib/debug/stats.h" #include "src/core/lib/gpr/spinlock.h" +#include "src/core/lib/gpr/tls.h" #include "src/core/lib/gprpp/manual_constructor.h" #include "src/core/lib/iomgr/block_annotate.h" #include "src/core/lib/iomgr/iomgr_internal.h" diff --git a/src/core/lib/iomgr/ev_epollsig_linux.cc b/src/core/lib/iomgr/ev_epollsig_linux.cc index c8954894ec..416447f3da 100644 --- a/src/core/lib/iomgr/ev_epollsig_linux.cc +++ b/src/core/lib/iomgr/ev_epollsig_linux.cc @@ -39,10 +39,10 @@ #include <grpc/support/alloc.h> #include <grpc/support/string_util.h> -#include <grpc/support/tls.h> #include <grpc/support/useful.h> #include "src/core/lib/debug/stats.h" +#include "src/core/lib/gpr/tls.h" #include "src/core/lib/gprpp/manual_constructor.h" #include "src/core/lib/iomgr/block_annotate.h" #include "src/core/lib/iomgr/ev_posix.h" diff --git a/src/core/lib/iomgr/ev_poll_posix.cc b/src/core/lib/iomgr/ev_poll_posix.cc index 8ccc256222..5284fb4104 100644 --- a/src/core/lib/iomgr/ev_poll_posix.cc +++ b/src/core/lib/iomgr/ev_poll_posix.cc @@ -34,11 +34,11 @@ #include <grpc/support/log.h> #include <grpc/support/string_util.h> #include <grpc/support/thd.h> -#include <grpc/support/tls.h> #include <grpc/support/useful.h> #include "src/core/lib/debug/stats.h" #include "src/core/lib/gpr/murmur_hash.h" +#include "src/core/lib/gpr/tls.h" #include "src/core/lib/iomgr/block_annotate.h" #include "src/core/lib/iomgr/iomgr_internal.h" #include "src/core/lib/iomgr/wakeup_fd_cv.h" diff --git a/src/core/lib/iomgr/exec_ctx.h b/src/core/lib/iomgr/exec_ctx.h index 2e71482fb7..3d9a157627 100644 --- a/src/core/lib/iomgr/exec_ctx.h +++ b/src/core/lib/iomgr/exec_ctx.h @@ -22,8 +22,8 @@ #include <grpc/support/atm.h> #include <grpc/support/cpu.h> #include <grpc/support/log.h> -#include <grpc/support/tls.h> +#include "src/core/lib/gpr/tls.h" #include "src/core/lib/iomgr/closure.h" typedef gpr_atm grpc_millis; diff --git a/src/core/lib/iomgr/executor.cc b/src/core/lib/iomgr/executor.cc index 835dc9d0f7..30157e3d1d 100644 --- a/src/core/lib/iomgr/executor.cc +++ b/src/core/lib/iomgr/executor.cc @@ -25,11 +25,11 @@ #include <grpc/support/log.h> #include <grpc/support/sync.h> #include <grpc/support/thd.h> -#include <grpc/support/tls.h> #include <grpc/support/useful.h> #include "src/core/lib/debug/stats.h" #include "src/core/lib/gpr/spinlock.h" +#include "src/core/lib/gpr/tls.h" #include "src/core/lib/iomgr/exec_ctx.h" #define MAX_DEPTH 2 diff --git a/src/core/lib/iomgr/timer_generic.cc b/src/core/lib/iomgr/timer_generic.cc index 177bdec8df..015142fa91 100644 --- a/src/core/lib/iomgr/timer_generic.cc +++ b/src/core/lib/iomgr/timer_generic.cc @@ -29,10 +29,11 @@ #include <grpc/support/log.h> #include <grpc/support/string_util.h> #include <grpc/support/sync.h> -#include <grpc/support/tls.h> #include <grpc/support/useful.h> + #include "src/core/lib/debug/trace.h" #include "src/core/lib/gpr/spinlock.h" +#include "src/core/lib/gpr/tls.h" #include "src/core/lib/iomgr/time_averaged_stats.h" #include "src/core/lib/iomgr/timer_heap.h" diff --git a/src/core/lib/surface/completion_queue.cc b/src/core/lib/surface/completion_queue.cc index 8362522fa1..a2c52082c2 100644 --- a/src/core/lib/surface/completion_queue.cc +++ b/src/core/lib/surface/completion_queue.cc @@ -28,11 +28,11 @@ #include <grpc/support/log.h> #include <grpc/support/string_util.h> #include <grpc/support/time.h> -#include <grpc/support/tls.h> #include "src/core/lib/debug/stats.h" #include "src/core/lib/gpr/spinlock.h" #include "src/core/lib/gpr/string.h" +#include "src/core/lib/gpr/tls.h" #include "src/core/lib/iomgr/pollset.h" #include "src/core/lib/iomgr/timer.h" #include "src/core/lib/profiling/timers.h" |