Linux内核链表list_head扩展---klist(二)

2014-11-24 08:54:32 ? 作者: ? 浏览: 4
extern void klist_iter_exit(struct klist_iter *i);
66extern struct klist_node *klist_next(struct klist_iter *i);
67
68#endif
69----------------------


klist实现代码 1/*
2 * klist.c - Routines for manipulating klists.
3 *
4 * Copyright (C) 2005 Patrick Mochel
5 *
6 * This file is released under the GPL v2.
7 *
8 * This klist interface provides a couple of structures that wrap around
9 * struct list_head to provide explicit list "head" (struct klist) and list
10 * "node" (struct klist_node) objects. For struct klist, a spinlock is
11 * included that protects access to the actual list itself. struct
12 * klist_node provides a pointer to the klist that owns it and a kref
13 * reference count that indicates the number of current users of that node
14 * in the list.
15 *
16 * The entire point is to provide an interface for iterating over a list
17 * that is safe and allows for modification of the list during the
18 * iteration (e.g. insertion and removal), including modification of the
19 * current node on the list.
20 *
21 * It works using a 3rd object type - struct klist_iter - that is declared
22 * and initialized before an iteration. klist_next() is used to acquire the
23 * next element in the list. It returns NULL if there are no more items.
24 * Internally, that routine takes the klist's lock, decrements the
25 * reference count of the previous klist_node and increments the count of
26 * the next klist_node. It then drops the lock and returns.
27 *
28 * There are primitives for adding and removing nodes to/from a klist.
29 * When deleting, klist_del() will simply decrement the reference count.
30 * Only when the count goes to 0 is the node removed from the list.
31 * klist_remove() will try to delete the node from the list and block until
32 * it is actually removed. This is useful for objects (like devices) that
33 * have been removed from the system and must be freed (but must wait until
34 * all accessors have finished).
35 */
36
37#include
38#include
39#include
40
/* 下面定义一些节点操作方法,先看下去,再来理解这些操作真正作用 */
41/*
42 * Use the lowest bit of n_klist to mark deleted nodes and exclude
43 * dead ones from iteration.
44 */
45#define KNODE_DEAD 1LU
46#define KNODE_KLIST_MASK ~KNODE_DEAD
47
/* 由节点获取链表头 */
48static struct klist *knode_klist(struct klist_node *knode)
49{
50 return (struct klist *)
51 ((unsigned long)knode->n_klist & KNODE_KLIST_MASK);
52}
53
/* 判断节点“死了” */
54static bool knode_dead(struct klist_node *knode)
55{
56 return (unsigned long)knode->n_klist & KNODE_DEAD;
57}
58
/* 设置节点的链表 */
59static void knode_set_klist(struct klist_node *knode, struct klist *klist)
60{
61 knode->n_klist = klist;
62 /* no knode deserves to start its life dead */
/* 没有节点刚开始就是“死的” */
63 WARN_ON(knode_dead(knode));
64}
65
/* “杀死”节点 */
66static void knode_kill(struct klist_node *knode)
67{
68 /* and no knode should die twice ever either, see we're very humane */
/* 没有节点能“死”两次,瞧我们多人性化 */
69 WARN_ON(knode_dead(knode));
70 *(unsigned long *)&knode->n_klist |= KNODE_DEAD;
71}
72
73/**
74 * klist_init - Initialize a klist structure.
75 * @k: The klist we're initializing.
76 * @get: The get function for the embedding object (NULL if none)
77 * @put: The put function for the embedding object (NULL if none)
78 *
79 * Initialises the klist structure. If the klist_node structures are
80 * going to be embedded in refcounted ob

-->

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: