Zhichao
Zhichao

Reputation: 619

Is it atomic to access(load/store) 32 bit integer when using ARM Thumb instruction set?

Using ARM cortex with thumb instruction set and Keil realview compiler, is it safe to access to 32 bit integer? Since the thumb register set is 16 bits, does this mean, fetching a 32 bit int needs 2 machine instructions? If so, accessing 32 bit will not be atomic. If my worry is true, does it mean that int assignment should be protected by a critical region?

Upvotes: 2

Views: 1673

Answers (2)

Turbo J
Turbo J

Reputation: 7691

The instruction size is 16-Bit in thumb mode, not the register size.

This means that a constant assignment - as in i=1; - can be seen as atomic. Although more than one instruction is generated, only one will modify the memory location of i even if i is int32_t.

But you need a critical section once you to things like i=i+1. That is of course not atomic.

Upvotes: 0

Igor Skochinsky
Igor Skochinsky

Reputation: 25288

Thumb uses the same 32-bit registers as ARM, so there's no issue there. What's halved is the instruction size (and even that is not strictly true for Thumb-2).

Do not worry, you don't need to change your code if you're compiling to Thumb.

Upvotes: 1

Related Questions