我目前正在为类开发一个项目,其中包括一个基于内核的信号量实现。我没有使用define_spinlock(sem_lock);作为/kernel/sys.c中的全局变量,而是创建了一个方案,其中信号量结构保存一个spinlock_t变量(在用户空间中被屏蔽为void*),以便让不同的信号量有机会独立地down()/up()(这应该可以转换为更高效的代码,而不必等待太多时间)。我进行了一个系统调用来初始化一个信号量,并向结构传递一个指针。但是,在初始化信号量的spinlock时,我有一行出错。
我已经联系了我的教授,他说为了完成这项任务,只需要使用一个带有全局spinlock定义的粗略实现。然而,很难让这一切过去。虽然我显然不介意走这条路,但我还是想理解为什么我的实现不起作用。有人能帮我吗?
错误如下:

    CC      kernel/sys.o
kernel/sys.c: In function 'sys_cs1550_sem_init':
kernel/sys.c:2491: error: expected '=', ',', ';', 'asm' or '__attribute__' before '->' token
kernel/sys.c:2491: error: expected expression before '->' token
make[1]: *** [kernel/sys.o] Error 1
make: *** [kernel] Error 2

我试过以下方法:
DEFINE_SPINLOCK(sem->sem_lock);
DEFINE_SPINLOCK(&(sem->sem_lock));
DEFINE_SPINLOCK((&(sem->sem_lock)));
DEFINE_SPINLOCK(*(sem->sem_lock));
spinlock_t *lock = &(sem->sem_lock);
DEFINE_SPINLOCK(lock);

/kernel/sys.c中信号量结构的代码:
/*
* Initialize a semaphore with the creation of it's spinlock.
* The idea is to avoid having spinlocks in user space, by making
* the process as opaque as possible. Our lock is defined in the kernel as a spinlock_t,
* but as a void* in userspace. This allows us to have more than one semaphore as needed.
*/
struct cs1550_sem{
    spinlock_t *sem_lock;                   //The lock for our semaphore.
    int available_resources;                //Total # of available resources.
    struct cs1550_pnode *process_list;      //Pointer to the first node of our linked list.
};

/kernel/sys.c中信号量初始化系统调用的代码:
asmlinkage long sys_cs1550_sem_init(struct cs1550_sem *sem, int resource_cap){
    DEFINE_SPINLOCK(sem->sem_lock);         //Define our lock.
    sem->process_list = NULL;               //Ensure we have a 'stopping point' when dequeueing.
    sem->available_resources = resource_cap;
    return 0;
}
EXPORT_SYMBOL_GPL(sys_cs1550_sem_init);

谢谢你的时间。

最佳答案

你不能那样使用这个宏。

// spinlock_types.h
#define DEFINE_SPINLOCK(x)      spinlock_t x = __SPIN_LOCK_UNLOCKED(x)

您的结构可能如下所示:
struct cs1550_sem{
    spinlock_t sem_lock;                   //The lock for our semaphore.
    int available_resources;                //Total # of available resources.
    struct cs1550_pnode *process_list;      //Pointer to the first node of our linked list.
};

以及初始化:
asmlinkage long sys_cs1550_sem_init(struct cs1550_sem *sem, int resource_cap){
    spin_lock_init(&sem->sem_lock);         //Define our lock.
    sem->process_list = NULL;               //Ensure we have a 'stopping point' when dequeueing.
    sem->available_resources = resource_cap;
    return 0;
}

请看https://www.kernel.org/doc/Documentation/spinlocks.txtspin_lock_init(...)的第3课(第295行)

关于c - DEFINE_SPINLOCK(...)spinlock_t参数错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26178819/

10-16 22:56