<?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 Atomic operations: fetch and add in Digital Signal Controllers</title>
    <link>https://community.nxp.com/t5/Digital-Signal-Controllers/Atomic-operations-fetch-and-add/m-p/426782#M547</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm trying to build a library of lock-less atomic operations for the 56800EX DSC. I can achieve basic reads and stores and also use the atomic bit mask operations for bit manipulation. &lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However I'm trying to work out if it's possible to build a function with a prototype like:&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt; int atomic_fetch_and_add(atomic_t a, int i);&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Which would add 'i' to the atomic value 'a' (which is really just an int) and then return the value of 'a' prior to the add. I imagine this will be an inline assembly function (the others I've do so far are). But as far as I can see there's no way to do this without turning off interrupts because the read and add actions have to be separate. I can't use a parallel move because I can't directly add to a memory location at the same time. I'm wondering if there's some instruction that I've missed that would help with this. Alternatively I could make the atomic_t more complex and have some sort of flag to detect an interrupted access and attempt the action again, but I can't see how to do that either. &lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Perhaps someone else has done this, or something similar. Obviously this function (and related sub/inc/dec functions) would be very useful in controlling a data structure that was shared between multiple interrupt levels. If it's not possible then I'd have to look at some other solution to provide safe data structures. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 10 Aug 2015 09:42:59 GMT</pubDate>
    <dc:creator>spacedog</dc:creator>
    <dc:date>2015-08-10T09:42:59Z</dc:date>
    <item>
      <title>Atomic operations: fetch and add</title>
      <link>https://community.nxp.com/t5/Digital-Signal-Controllers/Atomic-operations-fetch-and-add/m-p/426782#M547</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm trying to build a library of lock-less atomic operations for the 56800EX DSC. I can achieve basic reads and stores and also use the atomic bit mask operations for bit manipulation. &lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However I'm trying to work out if it's possible to build a function with a prototype like:&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt; int atomic_fetch_and_add(atomic_t a, int i);&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Which would add 'i' to the atomic value 'a' (which is really just an int) and then return the value of 'a' prior to the add. I imagine this will be an inline assembly function (the others I've do so far are). But as far as I can see there's no way to do this without turning off interrupts because the read and add actions have to be separate. I can't use a parallel move because I can't directly add to a memory location at the same time. I'm wondering if there's some instruction that I've missed that would help with this. Alternatively I could make the atomic_t more complex and have some sort of flag to detect an interrupted access and attempt the action again, but I can't see how to do that either. &lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Perhaps someone else has done this, or something similar. Obviously this function (and related sub/inc/dec functions) would be very useful in controlling a data structure that was shared between multiple interrupt levels. If it's not possible then I'd have to look at some other solution to provide safe data structures. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 10 Aug 2015 09:42:59 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Digital-Signal-Controllers/Atomic-operations-fetch-and-add/m-p/426782#M547</guid>
      <dc:creator>spacedog</dc:creator>
      <dc:date>2015-08-10T09:42:59Z</dc:date>
    </item>
    <item>
      <title>Re: Atomic operations: fetch and add</title>
      <link>https://community.nxp.com/t5/Digital-Signal-Controllers/Atomic-operations-fetch-and-add/m-p/426783#M548</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi.&amp;nbsp; No I don't think that is possible.&lt;/P&gt;&lt;P&gt;You would have to be blocked waiting for the single bit set and test to indicate that your process has locked "the resource that controls all resources".&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 10 Aug 2015 19:10:13 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Digital-Signal-Controllers/Atomic-operations-fetch-and-add/m-p/426783#M548</guid>
      <dc:creator>johnlwinters</dc:creator>
      <dc:date>2015-08-10T19:10:13Z</dc:date>
    </item>
    <item>
      <title>Re: Atomic operations: fetch and add</title>
      <link>https://community.nxp.com/t5/Digital-Signal-Controllers/Atomic-operations-fetch-and-add/m-p/426784#M549</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks John, I'd pretty much came to the same conclusion. That means I need to disable interrupts to safely do a fetch_and_add. Since I'm in a function I'm wondering if I can used the delayed return, i.e.:&lt;/P&gt;&lt;PRE&gt;&lt;P&gt;asm static int atomic_fetch_and_add(atomic_t *a, int i)&lt;/P&gt;&lt;P&gt;{&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .optimize_iasm off;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; MOVE.W Y0, X0;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Save 'i', 1 cycle&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; RTSD;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Delayed return, 5 cycles&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; MOVE.W X:(R2), Y0;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Store value to return, 1 cycle&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ADD.W&amp;nbsp; X:(R2), X0;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Add atomic value to 'i', 2 cycles&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; MOVE.W X0, X:(R2);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Store back in atomic value, 1 cycle&lt;/P&gt; }&lt;/PRE&gt;&lt;P&gt;Since interrupts are disabled for three delayed instructions after the RTSD nothing can interrupt and they will execute as an atomic block. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm using assembly because if I'm providing an atomic library I want to be sure that the operation is definitely atomic and it's in a function so that I can turn optimization off for that block without impacting the calling code (and I'm assuming it'll effectively work as a compiler barrier to prevent the compiler re-ordering the code on either side of the call).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Are my assumptions correct, is that always going to work atomically and is there any better way to do it?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It certainly appears to work and given the debugger can't step through the delayed instructions I assume that nothing else can interrupt them either. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I think disabling interrupts for 4 cycles is acceptable, if it ever isn't then I'll have to look at implementing a more complex system with separate storage per interrupt level or something like that. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 11 Aug 2015 06:24:52 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Digital-Signal-Controllers/Atomic-operations-fetch-and-add/m-p/426784#M549</guid>
      <dc:creator>spacedog</dc:creator>
      <dc:date>2015-08-11T06:24:52Z</dc:date>
    </item>
  </channel>
</rss>

