Let's check out a quick kernel module that demonstrates the usage of the Linux kernel's RMW atomic bit operators ( ch13/1_rmw_atomic_bitops). You should realize that these operators can work on any memory, both a (CPU or device) register or RAM; here, we operate on a simple static global variable (named mem) within the example LKM. It's very simple; let's check it out:
// ch13/1_rmw_atomic_bitops/rmw_atomic_bitops.c
[ ... ]
#include <linux/spinlock.h>
#include <linux/atomic.h>
#include <linux/bitops.h>
#include "../../convenient.h"
[ ... ]
static unsigned long mem;
static u64 t1, t2;
static int MSB = BITS_PER_BYTE - 1;
DEFINE_SPINLOCK(slock);
We include the required headers and declare and initialize a few global variables (notice how our MSB variable uses BIT_PER_BYTE). We employ a simple macro, SHOW(), to display the formatted output with the printk. The init ...