Yihua Zhang | d36fe07 | 2018-01-23 10:40:45 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2017 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 | |
Alexander Polcyn | db3e898 | 2018-02-21 16:59:24 -0800 | [diff] [blame] | 19 | #include <grpc/support/port_platform.h> |
| 20 | |
Yihua Zhang | d36fe07 | 2018-01-23 10:40:45 -0800 | [diff] [blame] | 21 | #include "src/core/tsi/alts_transport_security.h" |
| 22 | |
| 23 | #include <string.h> |
| 24 | |
| 25 | static alts_shared_resource g_alts_resource; |
| 26 | |
| 27 | alts_shared_resource* alts_get_shared_resource(void) { |
| 28 | return &g_alts_resource; |
| 29 | } |
| 30 | |
Yihua Zhang | 6d55db0 | 2018-02-05 09:30:32 -0800 | [diff] [blame] | 31 | static void grpc_tsi_alts_wait_for_cq_drain() { |
| 32 | gpr_mu_lock(&g_alts_resource.mu); |
| 33 | while (!g_alts_resource.is_cq_drained) { |
| 34 | gpr_cv_wait(&g_alts_resource.cv, &g_alts_resource.mu, |
| 35 | gpr_inf_future(GPR_CLOCK_REALTIME)); |
| 36 | } |
| 37 | gpr_mu_unlock(&g_alts_resource.mu); |
| 38 | } |
| 39 | |
| 40 | void grpc_tsi_alts_signal_for_cq_destroy() { |
| 41 | gpr_mu_lock(&g_alts_resource.mu); |
| 42 | g_alts_resource.is_cq_drained = true; |
| 43 | gpr_cv_signal(&g_alts_resource.cv); |
| 44 | gpr_mu_unlock(&g_alts_resource.mu); |
| 45 | } |
| 46 | |
Yihua Zhang | d36fe07 | 2018-01-23 10:40:45 -0800 | [diff] [blame] | 47 | void grpc_tsi_alts_init() { |
ncteisen | f8a4aae | 2018-08-01 08:36:03 -0700 | [diff] [blame] | 48 | g_alts_resource.channel = nullptr; |
| 49 | g_alts_resource.cq = nullptr; |
| 50 | g_alts_resource.is_cq_drained = false; |
Yihua Zhang | d36fe07 | 2018-01-23 10:40:45 -0800 | [diff] [blame] | 51 | gpr_mu_init(&g_alts_resource.mu); |
Yihua Zhang | 6d55db0 | 2018-02-05 09:30:32 -0800 | [diff] [blame] | 52 | gpr_cv_init(&g_alts_resource.cv); |
Yihua Zhang | d36fe07 | 2018-01-23 10:40:45 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void grpc_tsi_alts_shutdown() { |
Yihua Zhang | 6d55db0 | 2018-02-05 09:30:32 -0800 | [diff] [blame] | 56 | if (g_alts_resource.cq != nullptr) { |
| 57 | grpc_completion_queue_shutdown(g_alts_resource.cq); |
| 58 | grpc_tsi_alts_wait_for_cq_drain(); |
| 59 | grpc_completion_queue_destroy(g_alts_resource.cq); |
| 60 | grpc_channel_destroy(g_alts_resource.channel); |
Vijay Pai | da69355 | 2018-02-16 22:59:03 -0800 | [diff] [blame] | 61 | g_alts_resource.thread.Join(); |
Yihua Zhang | d36fe07 | 2018-01-23 10:40:45 -0800 | [diff] [blame] | 62 | } |
Yihua Zhang | 6d55db0 | 2018-02-05 09:30:32 -0800 | [diff] [blame] | 63 | gpr_cv_destroy(&g_alts_resource.cv); |
| 64 | gpr_mu_destroy(&g_alts_resource.mu); |
Yihua Zhang | d36fe07 | 2018-01-23 10:40:45 -0800 | [diff] [blame] | 65 | } |