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/nuttx.git

commit b64dd8bed0afdaa797da8a5095fdc5081dc562ee
Author: dongjiuzhu1 <[email protected]>
AuthorDate: Fri Nov 3 16:43:23 2023 +0800

    sim/raw_gadget: let's raw_epxhandle exit when unregister usbdev
    
    Signed-off-by: dongjiuzhu1 <[email protected]>
---
 arch/sim/src/sim/posix/sim_rawgadget.c | 40 ++++++++++++++++++++++++++++++++--
 1 file changed, 38 insertions(+), 2 deletions(-)

diff --git a/arch/sim/src/sim/posix/sim_rawgadget.c 
b/arch/sim/src/sim/posix/sim_rawgadget.c
index 4234ad2704..8d49879727 100644
--- a/arch/sim/src/sim/posix/sim_rawgadget.c
+++ b/arch/sim/src/sim/posix/sim_rawgadget.c
@@ -26,6 +26,7 @@
 #include <sys/types.h>
 #include <sys/ioctl.h>
 
+#include <signal.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -199,6 +200,7 @@ struct usb_raw_gadget_dev_t
   struct usb_raw_control_io_s ep0_ctrl;
   struct usb_raw_ep_entry_s   eps_entry[USB_RAW_EPS_NUM_MAX];
   struct usb_raw_eps_info_s   eps_info;
+  bool                        loop_stop;
 };
 
 /****************************************************************************
@@ -218,6 +220,16 @@ static struct usb_raw_gadget_dev_t g_raw_gadget_dev =
  * Private Functions
  ****************************************************************************/
 
+static void host_raw_handle_signal(int signum)
+{
+  struct usb_raw_gadget_dev_t *dev = &g_raw_gadget_dev;
+
+  if (signum == SIGUSR2)
+    {
+      dev->loop_stop = true;
+    }
+}
+
 static void host_raw_fifocreate(struct usb_raw_fifo_s *fifo,
                                    uint16_t elem_size, uint16_t elem_num)
 {
@@ -586,8 +598,13 @@ static void *host_raw_ep0handle(void *arg)
   struct usb_raw_gadget_dev_t *dev = &g_raw_gadget_dev;
   struct usb_raw_ep_entry_s *entry = &dev->eps_entry[0];
   struct usb_raw_control_event_s event;
+  struct sigaction action;
+
+  memset(&action, 0, sizeof(action));
+  action.sa_handler = host_raw_handle_signal;
+  sigaction(SIGUSR2, &action, NULL);
 
-  while (dev->fd >= 0)
+  while (!dev->loop_stop)
     {
       event.inner.type = 0;
       event.inner.length = sizeof(event.ctrl);
@@ -621,8 +638,13 @@ static void *host_raw_ephandle(void *arg)
   struct usb_raw_gadget_dev_t *dev = &g_raw_gadget_dev;
   struct usb_raw_ep_entry_s *entry = arg;
   struct usb_raw_data_io_s *io;
+  struct sigaction action;
 
-  while (dev->fd >= 0)
+  memset(&action, 0, sizeof(action));
+  action.sa_handler = host_raw_handle_signal;
+  sigaction(SIGUSR2, &action, NULL);
+
+  while (!dev->loop_stop)
     {
       io = (struct usb_raw_data_io_s *)
             host_raw_fifoalloc(&entry->fifo);
@@ -687,6 +709,7 @@ int host_usbdev_init(uint32_t speed)
   host_raw_vbusdraw(fd, 0x32);
   host_raw_configure(fd);
   dev->fd = fd;
+  dev->loop_stop = false;
 
   host_raw_fifocreate(&dev->eps_entry[0].fifo,
                       (sizeof(struct host_usb_ctrlreq_s)
@@ -700,7 +723,20 @@ int host_usbdev_init(uint32_t speed)
 int host_usbdev_deinit(void)
 {
   struct usb_raw_gadget_dev_t *dev = &g_raw_gadget_dev;
+  int i;
+
+  for (i = 0; i < USB_RAW_EPS_NUM_MAX &&
+              dev->eps_entry[i].ep_thread > 0; i++)
+    {
+      pthread_kill(dev->eps_entry[i].ep_thread, SIGUSR2);
+      pthread_join(dev->eps_entry[i].ep_thread, NULL);
+    }
+
+  pthread_kill(dev->ep0_thread, SIGUSR2);
+  pthread_join(dev->ep0_thread, NULL);
   host_raw_close(dev->fd);
+  dev->fd = -1;
+
   return 0;
 }
 

Reply via email to