<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Atomic variable in deadlock</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/107771/atomic-variable-in-deadlock</link><description>Hi 
 I have implemented a fifo that is: -filled by the information I receive in the context of the registered callback when I start the BLE scan (bt_le_scan_start(&amp;amp;scan_param, device_found)). -emptied in the context of the main thread: at each cycle of</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 29 Jan 2024 13:58:06 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/107771/atomic-variable-in-deadlock" /><item><title>RE: Atomic variable in deadlock</title><link>https://devzone.nordicsemi.com/thread/466543?ContentTypeID=1</link><pubDate>Mon, 29 Jan 2024 13:58:06 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b76e7852-54d5-47e5-bf55-82c6599221a9</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Hi Christian,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;It seems I might have been too quick to conclude that it was a priority issue, considering that you observed the same behavior after yielding from the receiver thread. Maybe it was only part of the issue. Zephyr mutexes support both&amp;nbsp;&lt;a href="https://docs.zephyrproject.org/latest/kernel/services/synchronization/mutexes.html#reentrant-locking"&gt;&amp;#39;Reentrant Locking&amp;#39;&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://docs.zephyrproject.org/latest/kernel/services/synchronization/mutexes.html#priority-inheritance"&gt;&amp;#39;Priority Inheritance&amp;#39;&lt;/a&gt;.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Vidar&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Atomic variable in deadlock</title><link>https://devzone.nordicsemi.com/thread/466336?ContentTypeID=1</link><pubDate>Sat, 27 Jan 2024 14:49:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2c33f114-185d-4a48-9898-a5b6c1d2752c</guid><dc:creator>Lorenz898989</dc:creator><description>&lt;p&gt;Hi Vidar, thank you for your reply.&lt;/p&gt;
&lt;p&gt;To prove that everything else in the code works I replaced the atomic variables with a mutex: in this case everything works (I received more than 10 billion BLE messages from multiple transmitters handling them without problems).&lt;/p&gt;
&lt;p&gt;How come with mutex it works and with atomic variables it does not?&lt;/p&gt;
&lt;p&gt;To make it even more curious:&lt;br /&gt;If in the while (!atomic_cas) of waiting I put a k_yeld() or a k_usleep() it keeps freezing, BUT if I put a printk(&amp;quot;&amp;quot;); then it never freezes.&lt;/p&gt;
&lt;p&gt;I had also speculated that it might be a priority issue however I am not convinced: if I stop sending BLE messages after it freezes it keeps getting frozen&lt;/p&gt;
&lt;p&gt;The structure you propose is nice and I will certainly look at it more carefully.&lt;/p&gt;
&lt;p&gt;Best regards&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Cristian&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Atomic variable in deadlock</title><link>https://devzone.nordicsemi.com/thread/466326?ContentTypeID=1</link><pubDate>Sat, 27 Jan 2024 12:31:51 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d77c4e68-7543-4a62-b807-97c46361d5ee</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;Did you consider using the &lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/zephyr/kernel/services/data_passing/fifos.html"&gt;FIFO&lt;/a&gt; implementation from Zephyr or the &amp;#39;&lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/nrf/libraries/others/data_fifo.html#data-fifo"&gt;Data FIFO&lt;/a&gt;&amp;#39; from NRF before making your own? It seems like it would be better to use something that has already been well tested. Anyway, the problem here is that your higher priority BLE receiver thread is blocking your main thread, resulting in the lock never being released.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Vidar&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>