lkmpg/examples/example_atomic.c

75 lines
1.9 KiB
C
Raw Normal View History

/*
* example_atomic.c
*/
#include <linux/interrupt.h>
2021-07-22 06:35:24 +08:00
#include <linux/kernel.h>
#include <linux/module.h>
#define BYTE_TO_BINARY_PATTERN "%c%c%c%c%c%c%c%c"
#define BYTE_TO_BINARY(byte) \
(byte & 0x80 ? '1' : '0'), (byte & 0x40 ? '1' : '0'), \
(byte & 0x20 ? '1' : '0'), (byte & 0x10 ? '1' : '0'), \
(byte & 0x08 ? '1' : '0'), (byte & 0x04 ? '1' : '0'), \
(byte & 0x02 ? '1' : '0'), (byte & 0x01 ? '1' : '0')
2021-07-22 06:35:24 +08:00
static void atomic_add_subtract(void)
{
atomic_t debbie;
atomic_t chris = ATOMIC_INIT(50);
atomic_set(&debbie, 45);
/* subtract one */
atomic_dec(&debbie);
atomic_add(7, &debbie);
/* add one */
atomic_inc(&debbie);
pr_info("chris: %d, debbie: %d\n", atomic_read(&chris),
atomic_read(&debbie));
2021-07-22 06:35:24 +08:00
}
static void atomic_bitwise(void)
{
unsigned long word = 0;
pr_info("Bits 0: " BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(word));
2021-07-22 06:35:24 +08:00
set_bit(3, &word);
set_bit(5, &word);
pr_info("Bits 1: " BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(word));
2021-07-22 06:35:24 +08:00
clear_bit(5, &word);
pr_info("Bits 2: " BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(word));
2021-07-22 06:35:24 +08:00
change_bit(3, &word);
pr_info("Bits 3: " BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(word));
2021-07-22 06:35:24 +08:00
if (test_and_set_bit(3, &word))
pr_info("wrong\n");
pr_info("Bits 4: " BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(word));
2021-07-22 06:35:24 +08:00
word = 255;
pr_info("Bits 5: " BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(word));
2021-07-22 06:35:24 +08:00
}
static int example_atomic_init(void)
{
pr_info("example_atomic started\n");
atomic_add_subtract();
atomic_bitwise();
return 0;
}
static void example_atomic_exit(void)
{
pr_info("example_atomic exit\n");
}
module_init(example_atomic_init);
module_exit(example_atomic_exit);
MODULE_DESCRIPTION("Atomic operations example");
MODULE_LICENSE("GPL");