Skip to content

Instantly share code, notes, and snippets.

@masami256
Created May 3, 2017 10:21
Show Gist options
  • Save masami256/cb9b3f6fd5fd84855d8a6a926fecd32b to your computer and use it in GitHub Desktop.
Save masami256/cb9b3f6fd5fd84855d8a6a926fecd32b to your computer and use it in GitHub Desktop.
refcount api test
#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