blob: dac23bbf7a4a8ada07fd128adbe0de6935ba7bb0 [file] [log] [blame]
Yihua Zhangd36fe072018-01-23 10:40:45 -08001/*
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 Polcyndb3e8982018-02-21 16:59:24 -080019#include <grpc/support/port_platform.h>
20
Yihua Zhangd36fe072018-01-23 10:40:45 -080021#include "src/core/tsi/alts_transport_security.h"
22
23#include <string.h>
24
25static alts_shared_resource g_alts_resource;
26
27alts_shared_resource* alts_get_shared_resource(void) {
28 return &g_alts_resource;
29}
30
Yihua Zhang6d55db02018-02-05 09:30:32 -080031static 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
40void 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 Zhangd36fe072018-01-23 10:40:45 -080047void grpc_tsi_alts_init() {
ncteisenf8a4aae2018-08-01 08:36:03 -070048 g_alts_resource.channel = nullptr;
49 g_alts_resource.cq = nullptr;
50 g_alts_resource.is_cq_drained = false;
Yihua Zhangd36fe072018-01-23 10:40:45 -080051 gpr_mu_init(&g_alts_resource.mu);
Yihua Zhang6d55db02018-02-05 09:30:32 -080052 gpr_cv_init(&g_alts_resource.cv);
Yihua Zhangd36fe072018-01-23 10:40:45 -080053}
54
55void grpc_tsi_alts_shutdown() {
Yihua Zhang6d55db02018-02-05 09:30:32 -080056 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 Paida693552018-02-16 22:59:03 -080061 g_alts_resource.thread.Join();
Yihua Zhangd36fe072018-01-23 10:40:45 -080062 }
Yihua Zhang6d55db02018-02-05 09:30:32 -080063 gpr_cv_destroy(&g_alts_resource.cv);
64 gpr_mu_destroy(&g_alts_resource.mu);
Yihua Zhangd36fe072018-01-23 10:40:45 -080065}