Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
| 19 | /* Win32 code for gpr synchronization support. */ |
| 20 | |
jtattermusch | 98bffb7 | 2014-12-09 12:47:19 -0800 | [diff] [blame] | 21 | #include <grpc/support/port_platform.h> |
| 22 | |
Yuchen Zeng | 12dfdc3 | 2016-04-26 22:05:41 -0700 | [diff] [blame] | 23 | #ifdef GPR_WINDOWS |
jtattermusch | 98bffb7 | 2014-12-09 12:47:19 -0800 | [diff] [blame] | 24 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 25 | #include <grpc/support/log.h> |
| 26 | #include <grpc/support/sync.h> |
| 27 | #include <grpc/support/time.h> |
| 28 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 29 | void gpr_mu_init(gpr_mu *mu) { |
| 30 | InitializeCriticalSection(&mu->cs); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 31 | mu->locked = 0; |
| 32 | } |
| 33 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 34 | void gpr_mu_destroy(gpr_mu *mu) { DeleteCriticalSection(&mu->cs); } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 35 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 36 | void gpr_mu_lock(gpr_mu *mu) { |
| 37 | EnterCriticalSection(&mu->cs); |
| 38 | GPR_ASSERT(!mu->locked); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 39 | mu->locked = 1; |
| 40 | } |
| 41 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 42 | void gpr_mu_unlock(gpr_mu *mu) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 43 | mu->locked = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 44 | LeaveCriticalSection(&mu->cs); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 47 | int gpr_mu_trylock(gpr_mu *mu) { |
| 48 | int result = TryEnterCriticalSection(&mu->cs); |
| 49 | if (result) { |
| 50 | if (mu->locked) { /* This thread already holds the lock. */ |
| 51 | LeaveCriticalSection(&mu->cs); /* Decrement lock count. */ |
| 52 | result = 0; /* Indicate failure */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 53 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 54 | mu->locked = 1; |
| 55 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 56 | return result; |
| 57 | } |
| 58 | |
| 59 | /*----------------------------------------*/ |
| 60 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 61 | void gpr_cv_init(gpr_cv *cv) { InitializeConditionVariable(cv); } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 62 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 63 | void gpr_cv_destroy(gpr_cv *cv) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 64 | /* Condition variables don't need destruction in Win32. */ |
| 65 | } |
| 66 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 67 | int gpr_cv_wait(gpr_cv *cv, gpr_mu *mu, gpr_timespec abs_deadline) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 68 | int timeout = 0; |
jtattermusch | 275b3ac | 2014-12-09 15:54:14 -0800 | [diff] [blame] | 69 | DWORD timeout_max_ms; |
nnoble | 4eec017 | 2014-12-11 11:07:51 -0800 | [diff] [blame] | 70 | mu->locked = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 71 | if (gpr_time_cmp(abs_deadline, gpr_inf_future(abs_deadline.clock_type)) == |
| 72 | 0) { |
| 73 | SleepConditionVariableCS(cv, &mu->cs, INFINITE); |
| 74 | } else { |
Alistair Veitch | 0a7468a | 2016-02-05 13:13:13 -0800 | [diff] [blame] | 75 | abs_deadline = gpr_convert_clock_type(abs_deadline, GPR_CLOCK_REALTIME); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 76 | gpr_timespec now = gpr_now(abs_deadline.clock_type); |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 77 | int64_t now_ms = (int64_t)now.tv_sec * 1000 + now.tv_nsec / 1000000; |
| 78 | int64_t deadline_ms = |
| 79 | (int64_t)abs_deadline.tv_sec * 1000 + abs_deadline.tv_nsec / 1000000; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 80 | if (now_ms >= deadline_ms) { |
| 81 | timeout = 1; |
| 82 | } else { |
Nicolas "Pixel" Noble | 742eac1 | 2016-01-26 22:41:19 +0100 | [diff] [blame] | 83 | if ((deadline_ms - now_ms) >= INFINITE) { |
| 84 | timeout_max_ms = INFINITE - 1; |
| 85 | } else { |
| 86 | timeout_max_ms = (DWORD)(deadline_ms - now_ms); |
| 87 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 88 | timeout = (SleepConditionVariableCS(cv, &mu->cs, timeout_max_ms) == 0 && |
| 89 | GetLastError() == ERROR_TIMEOUT); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 90 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 91 | } |
nnoble | 4eec017 | 2014-12-11 11:07:51 -0800 | [diff] [blame] | 92 | mu->locked = 1; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 93 | return timeout; |
| 94 | } |
| 95 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 96 | void gpr_cv_signal(gpr_cv *cv) { WakeConditionVariable(cv); } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 97 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 98 | void gpr_cv_broadcast(gpr_cv *cv) { WakeAllConditionVariable(cv); } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 99 | |
| 100 | /*----------------------------------------*/ |
| 101 | |
| 102 | static void *dummy; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 103 | struct run_once_func_arg { |
| 104 | void (*init_function)(void); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 105 | }; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 106 | static BOOL CALLBACK run_once_func(gpr_once *once, void *v, void **pv) { |
Yash Tibrewal | 922bc8a | 2017-09-22 16:32:41 -0700 | [diff] [blame] | 107 | struct run_once_func_arg *arg = (struct run_once_func_arg *)v; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 108 | (*arg->init_function)(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 109 | return 1; |
| 110 | } |
| 111 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 112 | void gpr_once_init(gpr_once *once, void (*init_function)(void)) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 113 | struct run_once_func_arg arg; |
| 114 | arg.init_function = init_function; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 115 | InitOnceExecuteOnce(once, run_once_func, &arg, &dummy); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 116 | } |
jtattermusch | 98bffb7 | 2014-12-09 12:47:19 -0800 | [diff] [blame] | 117 | |
Yuchen Zeng | 12dfdc3 | 2016-04-26 22:05:41 -0700 | [diff] [blame] | 118 | #endif /* GPR_WINDOWS */ |