内核中的许多数据结构都是通过链表来的维护的,Linux内核提供了链表的通用处理操作,供内核中其他数据结构使用。只需将链表结构嵌入到目标数据结构,就可以利用通用的链表操作目标数据结构了。
数据结构定义:
#include <linux/list.h>
/*内核中的通用链表数据结构定义*/
struct list_head
{
struct list_head *next, *prev;
};
/*内嵌了通用链表数据结构的自定义的数据结构*/
struct mydatastructure
{
struct list_head mylist; /* Embed */
/* … */ /* Actual Fields */
};
内核中链表的常用操作:
INIT_LIST_HEAD() 初始化链表头
list_add() 将元素增加到链表头后
list_add_tail() 将元素添加到链表尾
list_del() 从链表中删除一个元素
list_replace() 将链表中的元素替换为另一个
list_entry() 遍历链表中的每一个元素
list_for_each_entry() 简化链表迭代接口
list_for_each_entry_safe() //如果迭代过程中需要删除结点,则用这个
list_empty() 检查链表是否为空
list_splice() 将两个链表合并
一个例子:
/*用于同步,以及串联逻辑数据结构的辅助结构*/
static struct _mydrv_wq {
struct list_head mydrv_worklist; /* Work List 链头*/
spinlock_t lock; /* Protect the list */
wait_queue_head_t todo; /* Synchronize submitter
and worker */
} mydrv_wq;
/*逻辑相关的数据结构*/
struct _mydrv_work {
struct list_head mydrv_workitem; /* The work chain */
void (*worker_func)(void *); /* Work to perform */
void *worker_data; /* Argument to worker_func */
/* ... */ /* Other fields */
} mydrv_work;static int __init
mydrv_init(void)
{
/* Initialize the lock to protect against
concurrent list access */
spin_lock_init(&mydrv_wq.lock);
/* Initialize the wait queue for communication
between the submitter and the worker */
init_waitqueue_head(&mydrv_wq.todo);
/* Initialize the list head */
INIT_LIST_HEAD(&mydrv_wq.mydrv_worklist);
/* Start the worker thread. See Listing 3.4 */
kernel_thread(mydrv_worker, NULL,
CLONE_FS | CLONE_FILES | CLONE_SIGHAND | SIGCHLD);
return 0;
}哈希链表
struct hlist_head
{
struct hlist_node *first;
};
struct hlist_node
{
struct hlist_node *next, **pprev;
};
977

被折叠的 条评论
为什么被折叠?



