This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new e9ccab2db3 fs/poll: using callback mechanism to implement poll 
notification
e9ccab2db3 is described below

commit e9ccab2db36c43415987fc49ae230bb844e08b98
Author: wangbowen6 <wangbow...@xiaomi.com>
AuthorDate: Wed Sep 28 11:30:28 2022 +0800

    fs/poll: using callback mechanism to implement poll notification
    
    Signed-off-by: wangbowen6 <wangbow...@xiaomi.com>
---
 fs/vfs/fs_epoll.c  |  5 +----
 fs/vfs/fs_poll.c   | 47 +++++++++++++++++++++++++++++++++++++----------
 include/sys/poll.h |  8 +++++++-
 3 files changed, 45 insertions(+), 15 deletions(-)

diff --git a/fs/vfs/fs_epoll.c b/fs/vfs/fs_epoll.c
index f835acde0e..4d3ab3db26 100644
--- a/fs/vfs/fs_epoll.c
+++ b/fs/vfs/fs_epoll.c
@@ -365,10 +365,7 @@ int epoll_ctl(int epfd, int op, int fd, struct epoll_event 
*ev)
         return -1;
     }
 
-  if (eph->poll[0].sem)
-    {
-      poll_notify(&eph->poll, 1, eph->poll[0].events);
-    }
+  poll_notify(&eph->poll, 1, eph->poll[0].events);
 
   return 0;
 }
diff --git a/fs/vfs/fs_poll.c b/fs/vfs/fs_poll.c
index 0962dfcb20..86ca32ff96 100644
--- a/fs/vfs/fs_poll.c
+++ b/fs/vfs/fs_poll.c
@@ -90,6 +90,37 @@ static int poll_fdsetup(int fd, FAR struct pollfd *fds, bool 
setup)
   return file_poll(filep, fds, setup);
 }
 
+/****************************************************************************
+ * Name: poll_default_cb
+ *
+ * Description:
+ *   The default poll callback function, this function do the final step of
+ *   poll notification.
+ *
+ * Input Parameters:
+ *   fds - The fds
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+static void poll_default_cb(FAR struct pollfd *fds)
+{
+  int semcount = 0;
+  FAR sem_t *pollsem;
+
+  if (fds->arg != NULL)
+    {
+      pollsem = (FAR sem_t *)fds->arg;
+      nxsem_get_value(pollsem, &semcount);
+      if (semcount < 1)
+        {
+          nxsem_post(pollsem);
+        }
+    }
+}
+
 /****************************************************************************
  * Name: poll_setup
  *
@@ -118,7 +149,8 @@ static inline int poll_setup(FAR struct pollfd *fds, nfds_t 
nfds,
        * on each thread.
        */
 
-      fds[i].sem     = sem;
+      fds[i].arg     = sem;
+      fds[i].cb      = poll_default_cb;
       fds[i].revents = 0;
       fds[i].priv    = NULL;
       fds[i].events |= POLLERR | POLLHUP;
@@ -267,7 +299,8 @@ static inline int poll_teardown(FAR struct pollfd *fds, 
nfds_t nfds,
 
       /* Un-initialize the poll structure */
 
-      fds[i].sem = NULL;
+      fds[i].arg = NULL;
+      fds[i].cb  = NULL;
     }
 
   return ret;
@@ -297,7 +330,6 @@ static inline int poll_teardown(FAR struct pollfd *fds, 
nfds_t nfds,
 void poll_notify(FAR struct pollfd **afds, int nfds, pollevent_t eventset)
 {
   int i;
-  int semcount;
   FAR struct pollfd *fds;
 
   DEBUGASSERT(afds != NULL && nfds >= 1);
@@ -317,15 +349,10 @@ void poll_notify(FAR struct pollfd **afds, int nfds, 
pollevent_t eventset)
               fds->revents &= ~POLLOUT;
             }
 
-          if (fds->revents != 0)
+          if (fds->revents != 0 && fds->cb != NULL)
             {
               finfo("Report events: %08" PRIx32 "\n", fds->revents);
-
-              nxsem_get_value(fds->sem, &semcount);
-              if (semcount < 1)
-                {
-                  nxsem_post(fds->sem);
-                }
+              fds->cb(fds);
             }
         }
     }
diff --git a/include/sys/poll.h b/include/sys/poll.h
index d7888c1cd4..16ca698a35 100644
--- a/include/sys/poll.h
+++ b/include/sys/poll.h
@@ -97,6 +97,11 @@ typedef unsigned int nfds_t;
 
 typedef uint32_t pollevent_t;
 
+/* The poll callback type */
+
+struct pollfd;
+typedef CODE void (*pollcb_t)(FAR struct pollfd *fds);
+
 /* This is the NuttX variant of the standard pollfd structure.  The poll()
  * interfaces receive a variable length array of such structures.
  *
@@ -117,7 +122,8 @@ struct pollfd
   /* Non-standard fields used internally by NuttX. */
 
   FAR void    *ptr;     /* The psock or file being polled */
-  FAR sem_t   *sem;     /* Pointer to semaphore used to post output event */
+  FAR void    *arg;     /* The poll callback function argument */
+  pollcb_t     cb;      /* The poll callback function */
   FAR void    *priv;    /* For use by drivers */
 };
 

Reply via email to