thd_windows.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. /* Windows implementation for gpr threads. */
  19. #include <grpc/support/port_platform.h>
  20. #ifdef GPR_WINDOWS
  21. #include <grpc/support/alloc.h>
  22. #include <grpc/support/log.h>
  23. #include <grpc/support/thd.h>
  24. #include <string.h>
  25. #if defined(_MSC_VER)
  26. #define thread_local __declspec(thread)
  27. #elif defined(__GNUC__)
  28. #define thread_local __thread
  29. #else
  30. #error "Unknown compiler - please file a bug report"
  31. #endif
  32. struct thd_info {
  33. void (*body)(void* arg); /* body of a thread */
  34. void* arg; /* argument to a thread */
  35. HANDLE join_event; /* if joinable, the join event */
  36. int joinable; /* true if not detached */
  37. };
  38. static thread_local struct thd_info* g_thd_info;
  39. /* Destroys a thread info */
  40. static void destroy_thread(struct thd_info* t) {
  41. if (t->joinable) CloseHandle(t->join_event);
  42. gpr_free(t);
  43. }
  44. void gpr_thd_init(void) {}
  45. /* Body of every thread started via gpr_thd_new. */
  46. static DWORD WINAPI thread_body(void* v) {
  47. g_thd_info = (struct thd_info*)v;
  48. g_thd_info->body(g_thd_info->arg);
  49. if (g_thd_info->joinable) {
  50. BOOL ret = SetEvent(g_thd_info->join_event);
  51. GPR_ASSERT(ret);
  52. } else {
  53. destroy_thread(g_thd_info);
  54. }
  55. return 0;
  56. }
  57. int gpr_thd_new(gpr_thd_id* t, void (*thd_body)(void* arg), void* arg,
  58. const gpr_thd_options* options) {
  59. HANDLE handle;
  60. struct thd_info* info = (struct thd_info*)gpr_malloc(sizeof(*info));
  61. info->body = thd_body;
  62. info->arg = arg;
  63. *t = 0;
  64. if (gpr_thd_options_is_joinable(options)) {
  65. info->joinable = 1;
  66. info->join_event = CreateEvent(NULL, FALSE, FALSE, NULL);
  67. if (info->join_event == NULL) {
  68. gpr_free(info);
  69. return 0;
  70. }
  71. } else {
  72. info->joinable = 0;
  73. }
  74. handle = CreateThread(NULL, 64 * 1024, thread_body, info, 0, NULL);
  75. if (handle == NULL) {
  76. destroy_thread(info);
  77. } else {
  78. *t = (gpr_thd_id)info;
  79. CloseHandle(handle);
  80. }
  81. return handle != NULL;
  82. }
  83. gpr_thd_id gpr_thd_currentid(void) { return (gpr_thd_id)g_thd_info; }
  84. void gpr_thd_join(gpr_thd_id t) {
  85. struct thd_info* info = (struct thd_info*)t;
  86. DWORD ret = WaitForSingleObject(info->join_event, INFINITE);
  87. GPR_ASSERT(ret == WAIT_OBJECT_0);
  88. destroy_thread(info);
  89. }
  90. #endif /* GPR_WINDOWS */