Keil Logo

C166: Using the _atomic_ Function


Information in this article applies to:

  • C166 All Versions

QUESTION

How can I use the _atomic_ function in my programs to create atomic sections?

ANSWER

The _atomic_ and _endatomic_ library routines specify the start and end of an atomic code section. This section of code must be 4 instructions or less. Use atomic sections to protect program code from hardware interrupts. Instructions in an atomic section are executed as an atom and cannot be interrupted.

For example:

#include

bit testbit;

void function (int flag)
{
_atomic_ (0);
if (flag == 1)
  testbit = 1;
_endatomic_ ();
}

Generates the following code:

0000 D120          ATOMIC    #03H
0002 4881          CMP       R8,#01H
0004 3D01          JMPR      cc_NZ,?C0001
0006 0F00     R    BSET      testbit
0008         ?C0001:
0008 CB00          RET

which uses the ATOMIC instruction to protect the specified code.

MORE INFORMATION

SEE ALSO


Last Reviewed: Thursday, February 25, 2021


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.