Created
May 3, 2017 10:21
-
-
Save masami256/cb9b3f6fd5fd84855d8a6a926fecd32b to your computer and use it in GitHub Desktop.
refcount api test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
#include <linux/module.h> | |
#include <linux/kernel.h> | |
#include <linux/refcount.h> | |
#include <linux/smp.h> | |
#include <linux/slab.h> | |
MODULE_DESCRIPTION("refcount api test"); | |
MODULE_AUTHOR("masami256"); | |
MODULE_LICENSE("GPL"); | |
static refcount_t counter1 = REFCOUNT_INIT(1); | |
static refcount_t *counter2; | |
static int non_atomic_counter; | |
static void refcounter_test_run(void *info) | |
{ | |
pr_info("%s: cpu %d\n", __func__, smp_processor_id()); | |
refcount_inc(&counter1); | |
refcount_inc(counter2); | |
non_atomic_counter++; | |
} | |
static int refcounter_test_init(void) | |
{ | |
pr_info("%s start\n", __func__); | |
counter2 = kmalloc(sizeof(refcount_t), GFP_KERNEL); | |
if (!counter2) | |
return -ENOMEM; | |
refcount_set(counter2, 1); | |
on_each_cpu(&refcounter_test_run, NULL, 1); | |
pr_info("counter1: %d\n", refcount_read(&counter1)); | |
pr_info("counter2: %d\n", refcount_read(counter2)); | |
pr_info("non_atomic_counter: %d\n", non_atomic_counter); | |
return 0; | |
} | |
static void refcounter_test_cleanup(void) | |
{ | |
pr_info("%s bye\n", __func__); | |
kfree(counter2); | |
} | |
module_init(refcounter_test_init); | |
module_exit(refcounter_test_cleanup); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment