init_MUTEX被廢除(解決error: implicit declaration of function ‘init_MUTEX’)

CSDN GitHub
init_MUTEX被廢除
解決error: implicit declaration of function ‘init_MUTEX’
LDD/problem/port/init_MUTEX


知識共享許可協議
本作品採用知識共享署名-非商業性使用-相同方式共享 4.0 國際許可協議進行許可, 轉載請註明出處

1 問題


近期在移植驅動的時候, 提示瞭如下錯誤

error: implicit declaration of function ‘init_MUTEX’ [-Werror=implicit-function-declaration]

2 原因分析


2.6.37 之後的 Linux 內核中, init_mutex 已經被廢除了, 新版本使用 sema_init 函數

查了一下早期版本的定義, 參見include/linux/semaphore.h, version 2.6.36.4, line 42

static inline void sema_init(struct semaphore *sem, int val)
{
    static struct lock_class_key __key;
    *sem = (struct semaphore) __SEMAPHORE_INITIALIZER(*sem, val);
    lockdep_init_map(&sem->lock.dep_map, "semaphore->lock", &__key, 0);
}

#define init_MUTEX(sem)     sema_init(sem, 1)
#define init_MUTEX_LOCKED(sem)  sema_init(sem, 0)

3 解決方案


修改 init_MUTEXsema_init即可, 也可以在驅動中定義 init_MUTEX

  • 修改 init_MUTEXsema_init
#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 36) && !defined(init_MUTEX)
    sema_init(&sem, 1);
#else
    init_MUTEX(&sem);
#endif
  • 定義 init_MUTEXsema_init
#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 36) && !defined(init_MUTEX)
#define init_MUTEX(sem)     sema_init(sem, 1)
#endif

其實早期的內核中, 定義了 sema_init, 因此其實可以不需要添加 #if #endif 宏, 直接修改 init_MUTEXsema_init 是沒有什麼問題的.


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章