• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 #include <grpc/support/port_platform.h>
20 
21 #include "src/core/tsi/alts_transport_security.h"
22 
23 #include <string.h>
24 
25 static alts_shared_resource g_alts_resource;
26 
alts_get_shared_resource(void)27 alts_shared_resource* alts_get_shared_resource(void) {
28   return &g_alts_resource;
29 }
30 
grpc_tsi_alts_wait_for_cq_drain()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 
grpc_tsi_alts_signal_for_cq_destroy()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 
grpc_tsi_alts_init()47 void grpc_tsi_alts_init() {
48   g_alts_resource.channel = nullptr;
49   g_alts_resource.cq = nullptr;
50   g_alts_resource.is_cq_drained = false;
51   gpr_mu_init(&g_alts_resource.mu);
52   gpr_cv_init(&g_alts_resource.cv);
53 }
54 
grpc_tsi_alts_shutdown()55 void grpc_tsi_alts_shutdown() {
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);
61     g_alts_resource.thread.Join();
62   }
63   gpr_cv_destroy(&g_alts_resource.cv);
64   gpr_mu_destroy(&g_alts_resource.mu);
65 }
66