• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 #ifndef GRPC_CORE_LIB_TRANSPORT_CONNECTIVITY_STATE_H
20 #define GRPC_CORE_LIB_TRANSPORT_CONNECTIVITY_STATE_H
21 
22 #include <grpc/support/port_platform.h>
23 
24 #include <grpc/grpc.h>
25 #include "src/core/lib/debug/trace.h"
26 #include "src/core/lib/iomgr/closure.h"
27 
28 typedef struct grpc_connectivity_state_watcher {
29   /** we keep watchers in a linked list */
30   struct grpc_connectivity_state_watcher* next;
31   /** closure to notify on change */
32   grpc_closure* notify;
33   /** the current state as believed by the watcher */
34   grpc_connectivity_state* current;
35 } grpc_connectivity_state_watcher;
36 
37 typedef struct {
38   /** current grpc_connectivity_state */
39   gpr_atm current_state_atm;
40   /** error associated with state */
41   grpc_error* current_error;
42   /** all our watchers */
43   grpc_connectivity_state_watcher* watchers;
44   /** a name to help debugging */
45   char* name;
46 } grpc_connectivity_state_tracker;
47 
48 extern grpc_core::TraceFlag grpc_connectivity_state_trace;
49 
50 /** enum --> string conversion */
51 const char* grpc_connectivity_state_name(grpc_connectivity_state state);
52 
53 void grpc_connectivity_state_init(grpc_connectivity_state_tracker* tracker,
54                                   grpc_connectivity_state init_state,
55                                   const char* name);
56 void grpc_connectivity_state_destroy(grpc_connectivity_state_tracker* tracker);
57 
58 /** Set connectivity state; not thread safe; access must be serialized with an
59  *  external lock */
60 void grpc_connectivity_state_set(grpc_connectivity_state_tracker* tracker,
61                                  grpc_connectivity_state state,
62                                  grpc_error* associated_error,
63                                  const char* reason);
64 
65 /** Return true if this connectivity state has watchers.
66     Access must be serialized with an external lock. */
67 bool grpc_connectivity_state_has_watchers(
68     grpc_connectivity_state_tracker* tracker);
69 
70 /** Return the last seen connectivity state. No need to synchronize access. */
71 grpc_connectivity_state grpc_connectivity_state_check(
72     grpc_connectivity_state_tracker* tracker);
73 
74 /** Return the last seen connectivity state, and the associated error.
75     Access must be serialized with an external lock. */
76 grpc_connectivity_state grpc_connectivity_state_get(
77     grpc_connectivity_state_tracker* tracker, grpc_error** error);
78 
79 /** Return 1 if the channel should start connecting, 0 otherwise.
80     If current==NULL cancel notify if it is already queued (success==0 in that
81     case).
82     Access must be serialized with an external lock. */
83 bool grpc_connectivity_state_notify_on_state_change(
84     grpc_connectivity_state_tracker* tracker, grpc_connectivity_state* current,
85     grpc_closure* notify);
86 
87 #endif /* GRPC_CORE_LIB_TRANSPORT_CONNECTIVITY_STATE_H */
88