Using the atomic_t and refcount_t interfaces
You’ll recall that in our original simple misc character device driver program’s (available in the Linux Kernel Programming – Part 2 companion volume, in the code here: https://github.com/PacktPublishing/Linux-Kernel-Programming-Part-2/blob/main/ch1/miscdrv_rdwr/miscdrv_rdwr.c) open
method (and elsewhere), we defined and manipulated two static global integers, ga
and gb
:
static int ga, gb = 1;
[...]
ga++; gb--;
By now, it should be obvious to you that the place where we operate on these integers is a potential bug if left as it is: it’s shared writable data (aka shared state) being accessed in a possibly concurrent code path and, therefore, qualifies as a critical section, thus requiring protection against concurrent access. (If this isn’t clear to you, please first read the Critical sections, exclusive execution, and atomicity section in the previous chapter carefully.)
In the previous chapter...