• 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_SRC_CORE_LIB_IOMGR_POLLSET_H
20 #define GRPC_SRC_CORE_LIB_IOMGR_POLLSET_H
21 
22 #include <grpc/support/port_platform.h>
23 #include <grpc/support/sync.h>
24 #include <grpc/support/time.h>
25 
26 #include "src/core/lib/iomgr/exec_ctx.h"
27 #include "src/core/lib/iomgr/iomgr_fwd.h"
28 
29 // A grpc_pollset is a set of file descriptors that a higher level item is
30 // interested in. For example:
31 //  - a server will typically keep a pollset containing all connected channels,
32 //    so that it can find new calls to service
33 //  - a completion queue might keep a pollset with an entry for each transport
34 //    that is servicing a call that it's tracking
35 
36 typedef struct grpc_pollset_worker grpc_pollset_worker;
37 
38 typedef struct grpc_pollset_vtable {
39   void (*global_init)(void);
40   void (*global_shutdown)(void);
41   void (*init)(grpc_pollset* pollset, gpr_mu** mu);
42   void (*shutdown)(grpc_pollset* pollset, grpc_closure* closure);
43   void (*destroy)(grpc_pollset* pollset);
44   grpc_error_handle (*work)(grpc_pollset* pollset, grpc_pollset_worker** worker,
45                             grpc_core::Timestamp deadline);
46   grpc_error_handle (*kick)(grpc_pollset* pollset,
47                             grpc_pollset_worker* specific_worker);
48   size_t (*pollset_size)(void);
49 } grpc_pollset_vtable;
50 
51 void grpc_set_pollset_vtable(grpc_pollset_vtable* vtable);
52 
53 void grpc_pollset_global_init(void);
54 void grpc_pollset_global_shutdown(void);
55 
56 size_t grpc_pollset_size(void);
57 // Initialize a pollset: assumes *pollset contains all zeros
58 void grpc_pollset_init(grpc_pollset* pollset, gpr_mu** mu);
59 // Begin shutting down the pollset, and call closure when done.
60 // pollset's mutex must be held
61 void grpc_pollset_shutdown(grpc_pollset* pollset, grpc_closure* closure);
62 void grpc_pollset_destroy(grpc_pollset* pollset);
63 
64 // Do some work on a pollset.
65 // May involve invoking asynchronous callbacks, or actually polling file
66 // descriptors.
67 // Requires pollset's mutex locked.
68 // May unlock its mutex during its execution.
69 
70 // worker is a (platform-specific) handle that can be used to wake up
71 // from grpc_pollset_work before any events are received and before the timeout
72 // has expired. It is both initialized and destroyed by grpc_pollset_work.
73 // Initialization of worker is guaranteed to occur BEFORE the
74 // pollset's mutex is released for the first time by grpc_pollset_work
75 // and it is guaranteed that it will not be released by grpc_pollset_work
76 // AFTER worker has been destroyed.
77 
78 // It's legal for worker to be NULL: in that case, this specific thread can not
79 // be directly woken with a kick, but maybe be indirectly (with a kick against
80 // the pollset as a whole).
81 
82 // Tries not to block past deadline.
83 // May call grpc_closure_list_run on grpc_closure_list, without holding the
84 // pollset
85 // lock
86 grpc_error_handle grpc_pollset_work(grpc_pollset* pollset,
87                                     grpc_pollset_worker** worker,
88                                     grpc_core::Timestamp deadline);
89 
90 // Break one polling thread out of polling work for this pollset.
91 // If specific_worker is non-NULL, then kick that worker.
92 grpc_error_handle grpc_pollset_kick(grpc_pollset* pollset,
93                                     grpc_pollset_worker* specific_worker);
94 
95 #endif  // GRPC_SRC_CORE_LIB_IOMGR_POLLSET_H
96