Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Purpose of the ATOMIC_INIT macro in the Linux kernel

I'm reading the Linux Device Drivers 3rd Edition book online and I'm having trouble understanding the initialization macro for atomic variables:

static atomic_t foobar = ATOMIC_INIT(1);

I've looked through the source code for the Linux kernel v3.2, but I've only come up with two definitions:

#define ATOMIC_INIT(i) { (i) }

and

#define ATOMIC_INIT(i) ((atomic_t) { (i) })

The second version of the definition for the macro seems to be functionally the same as the first -- in fact, it seems redundant to even have an explicit cast when the value would be implicitly cast anyway to atomic_t. Why are there two versions of the definition?

Is the purpose of the ATOMIC_INIT macro just to keep code from breaking if the atomic_t structure changes in a future release of the Linux kernel?

like image 701
Vilhelm Gray Avatar asked Apr 24 '13 18:04

Vilhelm Gray


Video Answer


1 Answers

Many atomic operations must be implemented separately for each architecture. The purpose of the various macros and functions in atomic.h is to hide the differences between architectures.

In practice, all architectures use a single 32-bit variable to implement atomic_t, so there is no practical difference in the various ATOMIC_INIT macros; all the interesting stuff happens in the operations. But the internals might change (and did change once for 32-bit SPARC), so you always should use the offical API.

like image 175
CL. Avatar answered Sep 24 '22 03:09

CL.