多种方式操作同种资源时的免锁策略

内核模块编码时,对资源的管理经常需要hash表和lru链两种方式。

可以在资源结点的数据结构中增加一个knpostion标识位,

两者都操作完毕后,使能该位,从而保证操作的一致性。

写了些简单的代码:

82 void resource_remove(resource_node_t *node)
83 {
84 ne_u32 hash_pos;
85 re_do_list *lru_list;
86
87 lru_list = per_cpu(Lru_list_pcpu, node->cpu);
88 nos_spin_lock_bh(&lru_list[node->lru_idx].lock);
89 if(likely(NODE_ALIVE == nos_atomic_read(&node->knpostion))){
90 nos_atomic_set(&node->knpostion, NODE_DEAD);
91 ne_list_del(&node->lru_list);
92 nos_spin_unlock_bh(&lru_list[node->lru_idx].lock);
93 /*
94 * del from hash
95 */
96 hash_pos = resource_get_hash(node).hash_pos;
97 nos_spin_lock_bh(&(resource_hashtable + hash_pos)->lock);
98 ne_list_del(&node->list);
99 resource_put(node);
100 nos_spin_unlock_bh(&(resource_hashtable + hash_pos)->lock);
101 }else{
102 nos_spin_unlock_bh(&lru_list[node->lru_idx].lock);
103 }
104 }

63 void resource_insert(resource_node_t *node)
64 {
65 ne_u32 hash_pos;
66 re_do_list *lru_list;
67 hash_pos = resource_get_hash(node).hash_pos;
68 nos_spin_lock_bh(&(resource_hashtable + hash_pos)->lock);
69 ne_list_add_tail(&node->list, &(resource_hashtable + hash_pos)->list);
70 nos_spin_unlock_bh(&(resource_hashtable + hash_pos)->lock);
71
72 node->create_jiffies = jiffies;
73
74 lru_list = per_cpu(Lru_list_pcpu, node->cpu);
75 nos_spin_lock_bh(&lru_list[node->lru_idx].lock);
76 ne_list_add_tail(&node->lru_list, &lru_list[node->lru_idx].list);
77 nos_atomic_set(&node->knpostion, NODE_ALIVE);
78 resource_hold(node);
79 nos_spin_unlock_bh(&lru_list[node->lru_idx].lock);
80 }

 

posted on 2014-10-22 17:07  Kernel_wu  阅读(175)  评论(0编辑  收藏  举报

导航