Home | History | Annotate | Download | only in iomgr
      1 /*
      2  *
      3  * Copyright 2016 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 /*
     20  * wakeup_fd_cv uses condition variables to implement wakeup fds.
     21  *
     22  * It is intended for use only in cases when eventfd() and pipe() are not
     23  * available.  It can only be used with the "poll" engine.
     24  *
     25  * Implementation:
     26  * A global table of cv wakeup fds is mantained.  A cv wakeup fd is a negative
     27  * file descriptor.  poll() is then run in a background thread with only the
     28  * real socket fds while we wait on a condition variable trigged by either the
     29  * poll() completion or a wakeup_fd() call.
     30  *
     31  */
     32 
     33 #ifndef GRPC_CORE_LIB_IOMGR_WAKEUP_FD_CV_H
     34 #define GRPC_CORE_LIB_IOMGR_WAKEUP_FD_CV_H
     35 
     36 #include <grpc/support/port_platform.h>
     37 
     38 #include <grpc/support/sync.h>
     39 
     40 #include "src/core/lib/iomgr/ev_posix.h"
     41 
     42 #define GRPC_FD_TO_IDX(fd) (-(fd)-1)
     43 #define GRPC_IDX_TO_FD(idx) (-(idx)-1)
     44 
     45 typedef struct grpc_cv_node {
     46   gpr_cv* cv;
     47   struct grpc_cv_node* next;
     48   struct grpc_cv_node* prev;
     49 } grpc_cv_node;
     50 
     51 typedef struct grpc_fd_node {
     52   int is_set;
     53   grpc_cv_node* cvs;
     54   struct grpc_fd_node* next_free;
     55 } grpc_fd_node;
     56 
     57 typedef struct grpc_cv_fd_table {
     58   gpr_mu mu;
     59   gpr_refcount pollcount;
     60   gpr_cv shutdown_cv;
     61   grpc_fd_node* cvfds;
     62   grpc_fd_node* free_fds;
     63   unsigned int size;
     64   grpc_poll_function_type poll;
     65 } grpc_cv_fd_table;
     66 
     67 extern const grpc_wakeup_fd_vtable grpc_cv_wakeup_fd_vtable;
     68 
     69 #endif /* GRPC_CORE_LIB_IOMGR_WAKEUP_FD_CV_H */
     70