title: How to compare and swap atomically in ARM7?
body: <p>I would like to modify a global variable which is shared by different tasks and IRQ contexts in a RTOS. Therefore I need to modify this variable atomically.
  In my current implementation, I have been using enable_irq/disable_irq functions to modify the statement atomically.</p>
  
  <pre><code>extern int g_var;
  
  void set_bit_atomic(int mask)
  {
      disable_irq();
      g_var |= mask;
      enable_irq();
  }
  </code></pre>
  
  <p>I've found the <code>__sync_bool_compare_and_swap</code> function in GCC <a href="http://gcc.gnu.org/onlinedocs/gcc-4.3.5/gcc/Atomic-Builtins.html" rel="nofollow">documentation</a> as a helper for atomic operations.</p>
  
  <p>My current toolchain is KEIL MDK, and I would like to switch to the approach shown below,</p>
  
  <pre><code>void set_bit_atomic(int mask)
  {
      volatile int tmp;
      do {
          tmp = g_var;
      } while (!__sync_bool_compare_and_swap(&amp;g_var, tmp, tmp | mask));
  }
  </code></pre>
  
  <p>How can I write <code>__sync_bool_compare_and_swap</code> function in ARMv4 command set(as inline assembly)?</p>