<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Posible race condition on usb_device_dci.c in LPC Microcontrollers</title>
    <link>https://community.nxp.com/t5/LPC-Microcontrollers/Posible-race-condition-on-usb-device-dci-c/m-p/873132#M34840</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have not observed any issues yet, just was looking at code&amp;nbsp;to understand buffer&amp;nbsp;usage/copy ...&lt;/P&gt;&lt;P&gt;and came with this code:&lt;/P&gt;&lt;P&gt;file usb_device_dci.c of LPC SDK 2.5&lt;/P&gt;&lt;P&gt;function&amp;nbsp;USB_DeviceTransfer at line 277 checks if endpoint callback is busy and then "enters critical" to mark it busy&lt;/P&gt;&lt;P&gt;&lt;EM&gt;if (deviceHandle-&amp;gt;epCallback[(uint8_t)((uint32_t)endpoint &amp;lt;&amp;lt; 1U) | direction].isBusy)&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt; {&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;&amp;nbsp; &amp;nbsp; return kStatus_USB_Busy;&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt; }&amp;nbsp; &amp;nbsp; &lt;/EM&gt;"here is race ? if not, then why enter critical to set a boolean ?"&lt;BR /&gt;&lt;EM&gt; USB_OSA_ENTER_CRITICAL();&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt; deviceHandle-&amp;gt;epCallback[(uint8_t)((uint32_t)endpoint &amp;lt;&amp;lt; 1U) | direction].isBusy = 1U;&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt; USB_OSA_EXIT_CRITICAL();&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I think it should be, first enter critical, check and mark it busy and in any case exit critical&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt; USB_OSA_ENTER_CRITICAL();&lt;/EM&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;EM&gt; if (deviceHandle-&amp;gt;epCallback[(uint8_t)((uint32_t)endpoint &amp;lt;&amp;lt; 1U) | direction].isBusy)&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt; {&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;&amp;nbsp; &amp;nbsp; &lt;STRONG&gt;USB_OSA_EXIT_CRITICAL();&lt;/STRONG&gt;&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;&amp;nbsp; &amp;nbsp; return kStatus_USB_Busy;&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt; }&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;deviceHandle-&amp;gt;epCallback[(uint8_t)((uint32_t)endpoint &amp;lt;&amp;lt; 1U) | direction].isBusy = 1U;&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt; USB_OSA_EXIT_CRITICAL();&lt;/EM&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 20 Mar 2019 11:30:46 GMT</pubDate>
    <dc:creator>janbertran</dc:creator>
    <dc:date>2019-03-20T11:30:46Z</dc:date>
    <item>
      <title>Posible race condition on usb_device_dci.c</title>
      <link>https://community.nxp.com/t5/LPC-Microcontrollers/Posible-race-condition-on-usb-device-dci-c/m-p/873132#M34840</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have not observed any issues yet, just was looking at code&amp;nbsp;to understand buffer&amp;nbsp;usage/copy ...&lt;/P&gt;&lt;P&gt;and came with this code:&lt;/P&gt;&lt;P&gt;file usb_device_dci.c of LPC SDK 2.5&lt;/P&gt;&lt;P&gt;function&amp;nbsp;USB_DeviceTransfer at line 277 checks if endpoint callback is busy and then "enters critical" to mark it busy&lt;/P&gt;&lt;P&gt;&lt;EM&gt;if (deviceHandle-&amp;gt;epCallback[(uint8_t)((uint32_t)endpoint &amp;lt;&amp;lt; 1U) | direction].isBusy)&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt; {&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;&amp;nbsp; &amp;nbsp; return kStatus_USB_Busy;&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt; }&amp;nbsp; &amp;nbsp; &lt;/EM&gt;"here is race ? if not, then why enter critical to set a boolean ?"&lt;BR /&gt;&lt;EM&gt; USB_OSA_ENTER_CRITICAL();&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt; deviceHandle-&amp;gt;epCallback[(uint8_t)((uint32_t)endpoint &amp;lt;&amp;lt; 1U) | direction].isBusy = 1U;&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt; USB_OSA_EXIT_CRITICAL();&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I think it should be, first enter critical, check and mark it busy and in any case exit critical&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt; USB_OSA_ENTER_CRITICAL();&lt;/EM&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;EM&gt; if (deviceHandle-&amp;gt;epCallback[(uint8_t)((uint32_t)endpoint &amp;lt;&amp;lt; 1U) | direction].isBusy)&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt; {&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;&amp;nbsp; &amp;nbsp; &lt;STRONG&gt;USB_OSA_EXIT_CRITICAL();&lt;/STRONG&gt;&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;&amp;nbsp; &amp;nbsp; return kStatus_USB_Busy;&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt; }&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;deviceHandle-&amp;gt;epCallback[(uint8_t)((uint32_t)endpoint &amp;lt;&amp;lt; 1U) | direction].isBusy = 1U;&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt; USB_OSA_EXIT_CRITICAL();&lt;/EM&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 20 Mar 2019 11:30:46 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPC-Microcontrollers/Posible-race-condition-on-usb-device-dci-c/m-p/873132#M34840</guid>
      <dc:creator>janbertran</dc:creator>
      <dc:date>2019-03-20T11:30:46Z</dc:date>
    </item>
    <item>
      <title>Re: Posible race condition on usb_device_dci.c</title>
      <link>https://community.nxp.com/t5/LPC-Microcontrollers/Posible-race-condition-on-usb-device-dci-c/m-p/873133#M34841</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Jan,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The logic in this implementation is that you can't use this peripheral if its busy,&amp;nbsp;you'll need to check if other endpoint is using the peripheral, if its available the endpoint enter the critical section and set the flag to indicate&amp;nbsp;other endpoints that the peripheral is busy.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="pastedImage_1.png"&gt;&lt;img src="https://community.nxp.com/t5/image/serverpage/image-id/78816i15F0A8064AC2A3A9/image-size/large?v=v2&amp;amp;px=999" role="button" title="pastedImage_1.png" alt="pastedImage_1.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;You're approach is also right, but in the rare case the endpoint that is keeping the peripheral busy sends an interruption in this interval, this will&amp;nbsp;be lost.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I hope this helps you.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Best Regards,&lt;/P&gt;&lt;P&gt;Alexis Andalon&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 22 Mar 2019 18:39:39 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPC-Microcontrollers/Posible-race-condition-on-usb-device-dci-c/m-p/873133#M34841</guid>
      <dc:creator>Alexis_A</dc:creator>
      <dc:date>2019-03-22T18:39:39Z</dc:date>
    </item>
  </channel>
</rss>

