简体   繁体   English

spin_lock_init 采用什么参数?

[英]What argument does spin_lock_init take?

I want to initialize at run time the spin_lock using the spin_lock_init function, but don't know what argument it takes, I saw from the docs that it takes a pointer to spinlock_t type but I don't know how to implement it.我想在运行时初始化spin_lock使用spin_lock_init功能,但不知道是哪一种说法,我看到的文档,它需要一个指向spinlock_t类型,但我不知道如何实现它。 How can we init spinlock at run time using that function?我们如何在运行时使用该函数初始化自旋锁?

int __init(void)
{
    spinlock_t my_lock; // spin lock type
    spin_lock_init(&my_lock); // void
    
    return 64;
}

Your usage is correct.你的用法是正确的。 The spin_lock_init() function is intended to be used when you need to initialize a spinlock at run time . spin_lock_init()函数旨在在您需要在运行时初始化自旋锁时使用 What is odd in your example is the fact that you are defining the spinlock inside a function: what purpose will that spinlock ever have if it's only defined there and gets discarded immediately when the function returns?在您的示例中,奇怪的是您在函数内部定义了自旋锁:如果自旋锁仅在那里定义并在函数返回时立即被丢弃,那么它有什么用途? You usually want to be able to lock/unlock it from different functions (or different calls of the same function).您通常希望能够从不同的函数(或同一函数的不同调用)锁定/解锁它。

Therefore, you usually want to have spinlocks as global variables, something like:因此,您通常希望将自旋锁作为全局变量,例如:

static spinlock_t my_lock;

int __init myinit(void)
{
    spin_lock_init(&my_lock);
    return 0;
}

For usages in which you do not need to initialize the spinlock at runtime (because you already know that you will need it initialized right away), you can use the DEFINE_SPINLOCK() macro, that will both declare and initialize it for you at compile time, saving time and code:对于不需要在运行时初始化自旋锁的用法(因为您已经知道需要立即初始化它),您可以使用DEFINE_SPINLOCK()宏,它会在编译时为您声明和初始化它,节省时间和代码:

static DEFINE_SPINLOCK(my_lock);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM