86674

Critical sections in ARM

Question:

I am experienced in implementing critical sections on the AVR family of processors, where all you do is disable interrupts (with a memory barrier of course), do the critical operation, and then reenable interrupts:

void my_critical_function() { cli(); //Disable interrupts // Mission critical code here sei(); //Enable interrupts }

Now my question is this:

Does this simple method apply to the ARM architecture of processor as well? I have heard things about the processor doing lookahead on the instructions, and other black magic, and was wondering primarily if these types of things could be problematic to this implementation of critical sections.

Answer1:

Assuming you're on a Cortex-M processor, take a look at the LDREX and STREX instructions, which are available in C via the __LDREXW() and __STREXW() macros provided by CMSIS (the Cortex Microcontroller Software Interface Standard). They can be used to build extremely lightweight mutual exclusion mechanisms.

Basically,

data = __LDREXW(address)

works like data = *address except that it sets an 'exclusive access flag' in the CPU. When you've finished manipulating your data, write it back using

success = __STREXW(address, data)

which is like *address = data but will only succeed in writing if the exclusive access flag is still set. If it does succeed in writing then it also clears the flag. It returns 0 on success and 1 on failure. If the STREX fails, you have to go back to the LDREX and try again.

For simple exclusive access to a shared variable, nothing else is required. For example:

do { data = LDREX(address); data++; } while (STREXW(address, data));

The interesting thing about this mechanism is that it's effectively 'last come, first served'; if this code is interrupted and the interrupt uses LDREX and STREX, the STREX interrupt will succeed and the (lower-priority) user code will have to retry.

If you're using an operating system, the same primitives can be used to build 'proper' semaphores and mutexes (see <a href="http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dai0321a/BIHEJCHB.html" rel="nofollow">this application note</a>, for example); but then again if you're using an OS you probably already have access to mutexes through its API!

Answer2:

ARM architecture is very wide and as I understand you probably mean ARM Cortex M micro controllers.

You can use this technique, but many ARM uCs offer much more. As I do know what is the actual hardware I can only give you some examples:

<ol><li>bitband area. In this memory regions you can set and reset bits atomic way.</li> <li>Hardware semaphores (STM32H7)</li> <li>Hardware MUTEX-es (some NXP uCs)</li> </ol>

etc etc.

Recommend

  • Azure Functions - how to set up IoTHubTrigger for my IoTHub messages?
  • Django-tables2 column total
  • How to get output of a Lisp program into Python?
  • GWT Tomcat problem to RPC call
  • Getting all field names form a firestore document to an arraylist
  • user registeration form without captcha?
  • IE8 does not submit hidden input in form
  • Integrating search on a website where the backend is MYSQL
  • Passing dynamic array of struct to function for allocation
  • How to solve [Vue warn]: props must be strings when using array syntax?
  • How to change legend label in “theme” argument in ggplot2?
  • Prevent word wrap in label with AutoSize=false
  • Webclient.DownloadFile() .aspx file won't open
  • What is the difference between ViewData, ViewBag and TempData? [duplicate]
  • what is deep_ping [closed]
  • Keycode event for BACKSPACE in JavaFx
  • Using SQLite with iOS… very beginner program
  • XPATH: select subset of xml file
  • PHP Gettext: how to change the default MO path after setting the path of the domain?
  • List new files added (by anyone) between two dates
  • Visual Studio SQL Server design and inline editing features missing
  • AngularJS window scroll event does not fire
  • Fast and efficient way to expand a dataset in R
  • Image gets distorted sometime while uploading
  • Maven compilation complication due to complex folder structure
  • how do you uninstall an xampp installation that refuses to uninstall?
  • How to parse this JSON in PHP?
  • .NET COM assembly interacting with Excel via BackgroundWorker
  • Query URL for Log In Status, javascript
  • How to add a progress ring to the splash screen in Windows 8?
  • pass sessionid through jquery ajax call to php
  • Android NDK refer to external libraries in JNI
  • Remove stopwords and tolower function slow on a Corpus in R
  • Conflicting declaration using constexpr and auto in C++11
  • VSTS work items list through REST API
  • How to handle a codeigniter PDF generator