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