title: How to protect a global variable shared by isr and regular function?
body: <p>Let's say I have <code>function 1</code> and an <code>isr routine</code>, both share and update the same flag without any lock between them. <strong>the system is single threaded.</strong></p>
  
  <p>the while will be a 3 arm assembly instructions, which means it is not atomic operation, is it ok to share a global variable between non isr and isr functions without any lock or protection?</p>
  
  <p><strong>function 1:</strong></p>
  
  <pre><code>while (flag == false);
  flag = false;
  </code></pre>
  
  <p><strong>isr routine:</strong></p>
  
  <pre><code>do something
  flag=true
  </code></pre>
  
  <hr>
  
  <p>I don't remember there is a linux kernel mechanism for locking between sleepable and non sleepable context e.g. <code>irq</code> and <code>kernel thread</code>.</p>
  
  <hr>
  
  <p><strong>Thanks @artless for his answer here are some issues I am not sure about:</strong></p>
  
  <ol>
  <li><p>Is there a way I won't miss interrupt at all ?</p></li>
  <li><p>How the memory barriers solve the issue, does it have effect when the code runs on single cpu ?</p></li>
  <li><p>What is the expected behavior when using barriers between different contexts ?</p></li>
  <li><p>Can a sleep in the while <code>loop</code> can solve problems of syncs ?</p></li>
  </ol>