Home | History | Annotate | Download | only in iomgr
      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_IOMGR_POLLSET_H
     20 #define GRPC_CORE_LIB_IOMGR_POLLSET_H
     21 
     22 #include <grpc/support/port_platform.h>
     23 
     24 #include <grpc/support/sync.h>
     25 #include <grpc/support/time.h>
     26 
     27 #include "src/core/lib/iomgr/exec_ctx.h"
     28 
     29 extern grpc_core::DebugOnlyTraceFlag grpc_trace_fd_refcount;
     30 
     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 */
     37 
     38 typedef struct grpc_pollset grpc_pollset;
     39 typedef struct grpc_pollset_worker grpc_pollset_worker;
     40 
     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;
     53 
     54 void grpc_set_pollset_vtable(grpc_pollset_vtable* vtable);
     55 
     56 void grpc_pollset_global_init(void);
     57 void grpc_pollset_global_shutdown(void);
     58 
     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);
     66 
     67 /* Do some work on a pollset.
     68    May involve invoking asynchronous callbacks, or actually polling file
     69    descriptors.
     70    Requires pollset's mutex locked.
     71    May unlock its mutex during its execution.
     72 
     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.
     80 
     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).
     84 
     85    Tries not to block past deadline.
     86    May call grpc_closure_list_run on grpc_closure_list, without holding the
     87    pollset
     88    lock */
     89 grpc_error* grpc_pollset_work(grpc_pollset* pollset,
     90                               grpc_pollset_worker** worker,
     91                               grpc_millis deadline) GRPC_MUST_USE_RESULT;
     92 
     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)
     97     GRPC_MUST_USE_RESULT;
     98 
     99 #endif /* GRPC_CORE_LIB_IOMGR_POLLSET_H */
    100