Lock-free policies when operating the same resource in multiple ways

Source: Internet
Author: User

During kernel module encoding, the hash table and LRU chain methods are often required for resource management.

You can add a knpostion identifier in the data structure of the resource node,

After both operations are completed, enable this bit to ensure operation consistency.

I wrote some simple code:

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}

 

Lock-free policies when operating the same resource in multiple ways

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.