aboutsummaryrefslogtreecommitdiffhomepage
path: root/include
diff options
context:
space:
mode:
authorGravatar Craig Tiller <craig.tiller@gmail.com>2015-04-10 10:39:38 -0700
committerGravatar Craig Tiller <craig.tiller@gmail.com>2015-04-10 10:39:38 -0700
commit8cca9abeda9fb68ddee8a5a34f0ffacf9fe883f5 (patch)
treeb68a7ee283c303da7d686eeaed4da27afd5d4fbc /include
parent3ca65e3228d028e1b5e28c523c2ee5ca92bb9585 (diff)
Thread local storage for grpc
Diffstat (limited to 'include')
-rw-r--r--include/grpc/support/port_platform.h9
-rw-r--r--include/grpc/support/tls.h77
-rw-r--r--include/grpc/support/tls_gcc.h52
-rw-r--r--include/grpc/support/tls_msvc.h52
-rw-r--r--include/grpc/support/tls_pthread.h53
5 files changed, 243 insertions, 0 deletions
diff --git a/include/grpc/support/port_platform.h b/include/grpc/support/port_platform.h
index 41185dbf64..0373f14981 100644
--- a/include/grpc/support/port_platform.h
+++ b/include/grpc/support/port_platform.h
@@ -55,14 +55,17 @@
#define GPR_WINSOCK_SOCKET 1
#ifdef __GNUC__
#define GPR_GCC_ATOMIC 1
+#define GPR_GCC_TLS 1
#else
#define GPR_WIN32_ATOMIC 1
+#define GPR_MSVC_TLS 1
#endif
#elif defined(ANDROID) || defined(__ANDROID__)
#define GPR_ANDROID 1
#define GPR_ARCH_32 1
#define GPR_CPU_LINUX 1
#define GPR_GCC_SYNC 1
+#define GPR_GCC_TLS 1
#define GPR_POSIX_MULTIPOLL_WITH_POLL 1
#define GPR_POSIX_WAKEUP_FD 1
#define GPR_LINUX_EVENTFD 1
@@ -88,6 +91,7 @@
#include <features.h>
#define GPR_CPU_LINUX 1
#define GPR_GCC_ATOMIC 1
+#define GPR_GCC_TLS 1
#define GPR_LINUX 1
#define GPR_LINUX_MULTIPOLL_WITH_EPOLL 1
#define GPR_POSIX_WAKEUP_FD 1
@@ -134,6 +138,7 @@
#define GPR_CPU_POSIX 1
#endif
#define GPR_GCC_ATOMIC 1
+#define GPR_GCC_TLS 1
#define GPR_POSIX_LOG 1
#define GPR_POSIX_MULTIPOLL_WITH_POLL 1
#define GPR_POSIX_WAKEUP_FD 1
@@ -202,6 +207,10 @@
#error Must define exactly one of GPR_POSIX_SOCKET, GPR_WIN32
#endif
+#if defined(GPR_MSVC_TLS) + defined(GPR_GCC_TLS) + defined(GPR_PTHREAD_TLS) != 1
+#error Must define exactly one of GPR_MSVC_TLS, GPR_GCC_TLS, GPR_PTHREAD_TLS
+#endif
+
typedef int16_t gpr_int16;
typedef int32_t gpr_int32;
typedef int64_t gpr_int64;
diff --git a/include/grpc/support/tls.h b/include/grpc/support/tls.h
new file mode 100644
index 0000000000..1077fdec29
--- /dev/null
+++ b/include/grpc/support/tls.h
@@ -0,0 +1,77 @@
+/*
+ *
+ * Copyright 2015, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#ifndef GRPC_SUPPORT_TLS_H
+#define GRPC_SUPPORT_TLS_H
+
+#include "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 gpr_intptr.
+
+ Declaring a thread local variable 'foo':
+ GPR_TLS_DECL(foo, initial_value);
+ Thread locals always have static scope.
+
+ 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:
+ gpr_tls_set(&foo, new_value);
+
+ Accessing a thread local:
+ current_value = gpr_tls_get(&foo, value);
+
+ ALL functions here may be implemented as macros. */
+
+#ifdef GPR_GCC_TLS
+#include "tls_gcc.h"
+#endif
+
+#ifdef GPR_MSVC_TLS
+#include "tls_msvc.h"
+#endif
+
+#ifdef GPR_PTHREAD_TLS
+#include "tls_pthread.h"
+#endif
+
+#endif
diff --git a/include/grpc/support/tls_gcc.h b/include/grpc/support/tls_gcc.h
new file mode 100644
index 0000000000..a078b104ea
--- /dev/null
+++ b/include/grpc/support/tls_gcc.h
@@ -0,0 +1,52 @@
+/*
+ *
+ * Copyright 2015, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#ifndef GRPC_SUPPORT_TLS_GCC_H
+#define GRPC_SUPPORT_TLS_GCC_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 {
+ gpr_intptr value;
+};
+
+#define GPR_TLS_DECL(name) \
+ static __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
diff --git a/include/grpc/support/tls_msvc.h b/include/grpc/support/tls_msvc.h
new file mode 100644
index 0000000000..e574c11884
--- /dev/null
+++ b/include/grpc/support/tls_msvc.h
@@ -0,0 +1,52 @@
+/*
+ *
+ * Copyright 2015, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#ifndef GRPC_SUPPORT_TLS_GCC_H
+#define GRPC_SUPPORT_TLS_GCC_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 {
+ gpr_intptr value;
+};
+
+#define GPR_TLS_DECL(name) \
+ static __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
diff --git a/include/grpc/support/tls_pthread.h b/include/grpc/support/tls_pthread.h
new file mode 100644
index 0000000000..249c4d1cab
--- /dev/null
+++ b/include/grpc/support/tls_pthread.h
@@ -0,0 +1,53 @@
+/*
+ *
+ * Copyright 2015, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#ifndef GRPC_SUPPORT_TLS_PTHREAD_H
+#define GRPC_SUPPORT_TLS_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;
+};
+
+#define GPR_TLS_DECL(name) \
+ static 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_set(tls, new_value) \
+ GPR_ASSERT(pthread_setspecific((tls)->key, (void*)(new_value)) == 0)
+#define gpr_tls_get(tls) ((gpr_intptr)pthread_getspecific((tls)->key))
+
+#endif