From 24f0f57ea16b77e710f7ff4ae8a048c888ab6b12 Mon Sep 17 00:00:00 2001
From: Sree Kuchibhotla <sreek@google.com>
Date: Fri, 13 May 2016 19:22:44 -0700
Subject: [PATCH] Moving the creation of epoll_fd to pollset_init() instead of
 pollset_add_fd() [Verified stable. All tests pass]

---
 src/core/lib/iomgr/ev_epoll_posix.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)

diff --git a/src/core/lib/iomgr/ev_epoll_posix.c b/src/core/lib/iomgr/ev_epoll_posix.c
index 19465280a1..0ee0ee58a8 100644
--- a/src/core/lib/iomgr/ev_epoll_posix.c
+++ b/src/core/lib/iomgr/ev_epoll_posix.c
@@ -787,6 +787,9 @@ static void pollset_global_shutdown(void) {
 
 static void kick_poller(void) { grpc_wakeup_fd_wakeup(&grpc_global_wakeup_fd); }
 
+/* TODO: sreek. Try to Remove this forward declaration*/
+static void multipoll_with_epoll_pollset_create_efd(grpc_pollset *pollset);
+
 /* main interface */
 
 static void pollset_init(grpc_pollset *pollset, gpr_mu **mu) {
@@ -800,7 +803,9 @@ static void pollset_init(grpc_pollset *pollset, gpr_mu **mu) {
   pollset->idle_jobs.head = pollset->idle_jobs.tail = NULL;
   pollset->local_wakeup_cache = NULL;
   pollset->kicked_without_pollers = 0;
+
   pollset->data.ptr = NULL;
+  multipoll_with_epoll_pollset_create_efd(pollset);
 }
 
 /* TODO(sreek): Maybe merge multipoll_*_destroy() with pollset_destroy()
@@ -1153,13 +1158,15 @@ static void finally_add_fd(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
 }
 
 /* Creates an epoll fd and initializes the pollset */
-static void multipoll_with_epoll_pollset_create_efd(grpc_exec_ctx *exec_ctx,
-                                                    grpc_pollset *pollset) {
+/* TODO: This has to be called ONLY from pollset_init function. and hence it
+ * does not acquire any lock */
+static void multipoll_with_epoll_pollset_create_efd(grpc_pollset *pollset) {
   epoll_hdr *h = gpr_malloc(sizeof(epoll_hdr));
   struct epoll_event ev;
   int err;
 
-  /* Ensuring that the pollset is infact empty (with no epoll fd either) */
+  /* TODO (sreek). remove this assert. Currently added this just to ensure that
+   * we do not overwrite h->epoll_fd without freeing the older one*/
   GPR_ASSERT(pollset->data.ptr == NULL);
 
   pollset->data.ptr = h;
@@ -1188,10 +1195,10 @@ static void multipoll_with_epoll_pollset_create_efd(grpc_exec_ctx *exec_ctx,
 static void multipoll_with_epoll_pollset_add_fd(grpc_exec_ctx *exec_ctx,
                                                 grpc_pollset *pollset,
                                                 grpc_fd *fd) {
-  /* If there is no epoll fd on the pollset, create one */
-  if (pollset->data.ptr == NULL) {
-    multipoll_with_epoll_pollset_create_efd(exec_ctx, pollset);
-  }
+  GPR_ASSERT(pollset->data.ptr != NULL);
+
+  /* TODO(sreek). Remove this unlock code (and also the code that acquires the
+   * lock before calling multipoll_with_epoll_add_fd() function */
 
   gpr_mu_unlock(&pollset->mu);
   finally_add_fd(exec_ctx, pollset, fd);
-- 
GitLab