diff options
author | Vijay Pai <vpai@google.com> | 2018-02-13 14:40:39 -0800 |
---|---|---|
committer | Vijay Pai <vpai@google.com> | 2018-02-15 21:30:13 -0800 |
commit | 58a62755fc6546a117b7b8f3a0a344f85b2ea5f9 (patch) | |
tree | 294e8432672a2a8b3b2bd1bab7d24e75e1a6d4b6 /src/core/lib/gpr | |
parent | b0d71823a0f031ad1c04be30f22653177139da0b (diff) |
Remove support for detached threads. All threads must be joined.
Diffstat (limited to 'src/core/lib/gpr')
-rw-r--r-- | src/core/lib/gpr/thd.cc | 49 | ||||
-rw-r--r-- | src/core/lib/gpr/thd.h | 29 | ||||
-rw-r--r-- | src/core/lib/gpr/thd_posix.cc | 14 | ||||
-rw-r--r-- | src/core/lib/gpr/thd_windows.cc | 29 |
4 files changed, 17 insertions, 104 deletions
diff --git a/src/core/lib/gpr/thd.cc b/src/core/lib/gpr/thd.cc deleted file mode 100644 index 11391418b1..0000000000 --- a/src/core/lib/gpr/thd.cc +++ /dev/null @@ -1,49 +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. - * - */ - -/* Platform-independent features for gpr threads. */ - -#include "src/core/lib/gpr/thd.h" - -#include <string.h> - -enum { GPR_THD_JOINABLE = 1 }; - -gpr_thd_options gpr_thd_options_default(void) { - gpr_thd_options options; - memset(&options, 0, sizeof(options)); - return options; -} - -void gpr_thd_options_set_detached(gpr_thd_options* options) { - options->flags &= ~GPR_THD_JOINABLE; -} - -void gpr_thd_options_set_joinable(gpr_thd_options* options) { - options->flags |= GPR_THD_JOINABLE; -} - -int gpr_thd_options_is_detached(const gpr_thd_options* options) { - if (!options) return 1; - return (options->flags & GPR_THD_JOINABLE) == 0; -} - -int gpr_thd_options_is_joinable(const gpr_thd_options* options) { - if (!options) return 0; - return (options->flags & GPR_THD_JOINABLE) == GPR_THD_JOINABLE; -} diff --git a/src/core/lib/gpr/thd.h b/src/core/lib/gpr/thd.h index 58ce0d0088..9d8140114c 100644 --- a/src/core/lib/gpr/thd.h +++ b/src/core/lib/gpr/thd.h @@ -28,38 +28,15 @@ #include <grpc/support/thd_id.h> #include <grpc/support/time.h> -/** Thread creation options. */ -typedef struct { - int flags; /** Opaque field. Get and set with accessors below. */ -} gpr_thd_options; - /** Create a new thread running (*thd_body)(arg) and place its thread identifier in *t, and return true. If there are insufficient resources, return false. thd_name is the name of the thread for identification purposes on platforms that support thread naming. - If options==NULL, default options are used. - The thread is immediately runnable, and exits when (*thd_body)() returns. */ + The thread must be joined. */ int gpr_thd_new(gpr_thd_id* t, const char* thd_name, - void (*thd_body)(void* arg), void* arg, - const gpr_thd_options* options); - -/** Return a gpr_thd_options struct with all fields set to defaults. */ -gpr_thd_options gpr_thd_options_default(void); - -/** Set the thread to become detached on startup - this is the default. */ -void gpr_thd_options_set_detached(gpr_thd_options* options); - -/** Set the thread to become joinable - mutually exclusive with detached. */ -void gpr_thd_options_set_joinable(gpr_thd_options* options); - -/** Returns non-zero if the option detached is set. */ -int gpr_thd_options_is_detached(const gpr_thd_options* options); - -/** Returns non-zero if the option joinable is set. */ -int gpr_thd_options_is_joinable(const gpr_thd_options* options); + void (*thd_body)(void* arg), void* arg); -/** Blocks until the specified thread properly terminates. - Calling this on a detached thread has unpredictable results. */ +/** Blocks until the specified thread properly terminates. */ void gpr_thd_join(gpr_thd_id t); /* Internal interfaces between modules within the gpr support library. */ diff --git a/src/core/lib/gpr/thd_posix.cc b/src/core/lib/gpr/thd_posix.cc index fcd174bfba..a5640d8cc4 100644 --- a/src/core/lib/gpr/thd_posix.cc +++ b/src/core/lib/gpr/thd_posix.cc @@ -72,8 +72,7 @@ static void* thread_body(void* v) { } int gpr_thd_new(gpr_thd_id* t, const char* thd_name, - void (*thd_body)(void* arg), void* arg, - const gpr_thd_options* options) { + void (*thd_body)(void* arg), void* arg) { int thread_started; pthread_attr_t attr; pthread_t p; @@ -87,15 +86,12 @@ int gpr_thd_new(gpr_thd_id* t, const char* thd_name, inc_thd_count(); GPR_ASSERT(pthread_attr_init(&attr) == 0); - if (gpr_thd_options_is_detached(options)) { - GPR_ASSERT(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) == - 0); - } else { - GPR_ASSERT(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE) == - 0); - } + GPR_ASSERT(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE) == 0); + thread_started = (pthread_create(&p, &attr, &thread_body, a) == 0); + GPR_ASSERT(pthread_attr_destroy(&attr) == 0); + if (!thread_started) { /* don't use gpr_free, as this was allocated using malloc (see above) */ free(a); diff --git a/src/core/lib/gpr/thd_windows.cc b/src/core/lib/gpr/thd_windows.cc index b467bd2662..53093e80f3 100644 --- a/src/core/lib/gpr/thd_windows.cc +++ b/src/core/lib/gpr/thd_windows.cc @@ -40,15 +40,14 @@ struct thd_info { void (*body)(void* arg); /* body of a thread */ void* arg; /* argument to a thread */ - HANDLE join_event; /* if joinable, the join event */ - int joinable; /* true if not detached */ + HANDLE join_event; /* the join event */ }; static thread_local struct thd_info* g_thd_info; /* Destroys a thread info */ static void destroy_thread(struct thd_info* t) { - if (t->joinable) CloseHandle(t->join_event); + CloseHandle(t->join_event); gpr_free(t); } @@ -58,32 +57,22 @@ void gpr_thd_init(void) {} static DWORD WINAPI thread_body(void* v) { g_thd_info = (struct thd_info*)v; g_thd_info->body(g_thd_info->arg); - if (g_thd_info->joinable) { - BOOL ret = SetEvent(g_thd_info->join_event); - GPR_ASSERT(ret); - } else { - destroy_thread(g_thd_info); - } + BOOL ret = SetEvent(g_thd_info->join_event); + GPR_ASSERT(ret); return 0; } int gpr_thd_new(gpr_thd_id* t, const char* thd_name, - void (*thd_body)(void* arg), void* arg, - const gpr_thd_options* options) { + void (*thd_body)(void* arg), void* arg) { HANDLE handle; struct thd_info* info = (struct thd_info*)gpr_malloc(sizeof(*info)); info->body = thd_body; info->arg = arg; *t = 0; - if (gpr_thd_options_is_joinable(options)) { - info->joinable = 1; - info->join_event = CreateEvent(NULL, FALSE, FALSE, NULL); - if (info->join_event == NULL) { - gpr_free(info); - return 0; - } - } else { - info->joinable = 0; + info->join_event = CreateEvent(NULL, FALSE, FALSE, NULL); + if (info->join_event == NULL) { + gpr_free(info); + return 0; } handle = CreateThread(NULL, 64 * 1024, thread_body, info, 0, NULL); if (handle == NULL) { |