2021-07-22 11:25:32 +08:00
|
|
|
/*
|
2021-08-08 01:24:59 +08:00
|
|
|
* example_atomic.c
|
2021-07-22 11:25:32 +08:00
|
|
|
*/
|
2023-02-22 23:43:27 +08:00
|
|
|
#include <linux/atomic.h>
|
|
|
|
#include <linux/bitops.h>
|
2021-07-22 06:35:24 +08:00
|
|
|
#include <linux/module.h>
|
2023-02-23 18:41:23 +08:00
|
|
|
#include <linux/printk.h>
|
2021-07-22 06:35:24 +08:00
|
|
|
|
|
|
|
#define BYTE_TO_BINARY_PATTERN "%c%c%c%c%c%c%c%c"
|
2021-09-02 15:15:07 +08:00
|
|
|
#define BYTE_TO_BINARY(byte) \
|
2021-09-14 00:52:04 +08:00
|
|
|
((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);
|
|
|
|
|
2021-07-22 06:58:13 +08:00
|
|
|
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;
|
|
|
|
|
2021-07-22 06:58:13 +08:00
|
|
|
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);
|
2021-07-22 06:58:13 +08:00
|
|
|
pr_info("Bits 1: " BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(word));
|
2021-07-22 06:35:24 +08:00
|
|
|
clear_bit(5, &word);
|
2021-07-22 06:58:13 +08:00
|
|
|
pr_info("Bits 2: " BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(word));
|
2021-07-22 06:35:24 +08:00
|
|
|
change_bit(3, &word);
|
|
|
|
|
2021-07-22 06:58:13 +08:00
|
|
|
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");
|
2021-07-22 06:58:13 +08:00
|
|
|
pr_info("Bits 4: " BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(word));
|
2021-07-22 06:35:24 +08:00
|
|
|
|
|
|
|
word = 255;
|
2023-09-28 23:26:46 +08:00
|
|
|
pr_info("Bits 5: " BYTE_TO_BINARY_PATTERN "\n", BYTE_TO_BINARY(word));
|
2021-07-22 06:35:24 +08:00
|
|
|
}
|
|
|
|
|
2023-07-05 09:44:21 +08:00
|
|
|
static int __init example_atomic_init(void)
|
2021-07-22 06:35:24 +08:00
|
|
|
{
|
|
|
|
pr_info("example_atomic started\n");
|
|
|
|
|
|
|
|
atomic_add_subtract();
|
|
|
|
atomic_bitwise();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-07-05 09:44:21 +08:00
|
|
|
static void __exit example_atomic_exit(void)
|
2021-07-22 06:35:24 +08:00
|
|
|
{
|
|
|
|
pr_info("example_atomic exit\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
module_init(example_atomic_init);
|
|
|
|
module_exit(example_atomic_exit);
|
|
|
|
|
|
|
|
MODULE_DESCRIPTION("Atomic operations example");
|
|
|
|
MODULE_LICENSE("GPL");
|