fix locking of USB_MTX
authorNIIBE Yutaka <gniibe@fsij.org>
Sat, 17 Aug 2013 08:15:48 +0000 (17:15 +0900)
committerNIIBE Yutaka <gniibe@fsij.org>
Sat, 17 Aug 2013 08:15:48 +0000 (17:15 +0900)
ChangeLog
src/main.c

index 04f65cf..fd70c4b 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,7 @@
 2013-08-17  Niibe Yutaka  <gniibe@fsij.org>
 
+       * src/main.c (main): Fix locking of USB_MTX.
+
        * src/adc_stm32f103.c (adc_wait_completion): Fix race condition.
        Clear (FLAGS & ~1) only, not all of possible interrupts.
 
index e27a20f..94ee36b 100644 (file)
@@ -875,6 +875,9 @@ main (int argc, char **argv)
          chopstx_mutex_lock (&usb_mtx);
        }
 
+      if (bDeviceState != CONFIGURED)
+       goto not_configured;
+
       /* The connection opened.  */
       count = 0;
 
@@ -902,31 +905,28 @@ main (int argc, char **argv)
            {    /* Only send ZLP when the last packet was fullsize.  */
              neug_wait_full ();
 
-             if (bDeviceState != CONFIGURED)
-               goto not_configured;
-
-             if (!connected || fsij_device_state != FSIJ_DEVICE_RUNNING)
+             chopstx_mutex_lock (&usb_mtx);
+             if (bDeviceState != CONFIGURED || !connected
+                 || fsij_device_state != FSIJ_DEVICE_RUNNING)
                break;
-
-             continue;
            }
-
-         if (i == 64/4)
-           last_was_fullsizepacket = 1;
          else
-           last_was_fullsizepacket = 0;
-
-         chopstx_mutex_lock (&usb_mtx);
-         if (bDeviceState != CONFIGURED)
-           goto not_configured;
+           {
+             if (i == 64/4)
+               last_was_fullsizepacket = 1;
+             else
+               last_was_fullsizepacket = 0;
 
-         if (!connected || fsij_device_state != FSIJ_DEVICE_RUNNING)
-           break;
+             chopstx_mutex_lock (&usb_mtx);
+             if (bDeviceState != CONFIGURED || !connected
+                 || fsij_device_state != FSIJ_DEVICE_RUNNING)
+               break;
 
-         /* Send random data.  */
-         usb_lld_tx_enable (ENDP1, i * 4);
-         chopstx_cond_wait (&cnd_usb, &usb_mtx);
-         count++;
+             /* Prepare sending random data.  */
+             usb_lld_tx_enable (ENDP1, i * 4);
+             chopstx_cond_wait (&cnd_usb, &usb_mtx);
+             count++;
+           }
        }
     }