From ade5d6bb6a7763772a1c46ddfdc618edccd145e8 Mon Sep 17 00:00:00 2001 From: ChenLing Date: Fri, 2 Aug 2019 17:10:33 +0800 Subject: [PATCH] =?UTF-8?q?Mod=20=20aaa-12=20=E4=BF=AE=E6=94=B9=E5=86=85?= =?UTF-8?q?=E6=A0=B8=E6=80=81hsah=E8=A1=A8review=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98=20RCA=EF=BC=9A=20SOL=EF=BC=9A=20=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E4=BA=BA=EF=BC=9Achenling=20=E6=A3=80=E8=A7=86=E4=BA=BA?= =?UTF-8?q?=EF=BC=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Product/modules/userhash/k-userhash.c | 34 +++++++++++++++------------ 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/Product/modules/userhash/k-userhash.c b/Product/modules/userhash/k-userhash.c index a9564c505..054df99c4 100644 --- a/Product/modules/userhash/k-userhash.c +++ b/Product/modules/userhash/k-userhash.c @@ -1,5 +1,10 @@ #include "k-userhash.h" #include +#include +#include + +/*声明读写锁 */ +DEFINE_RWLOCK(g_obj_lock); /*定义大小为HASH_SIZE的hashtable */ static struct list_head hash_array[HASH_SIZE]; @@ -15,7 +20,7 @@ void init_hashMap(void) /*获取hashtable位置索引 */ int get_hash_index(uint32_t ip) { - unsigned int val = ip % HASH_SIZE; + int val = ip % HASH_SIZE; return val; } @@ -29,15 +34,8 @@ USERINFO *search_user(uint32_t ip) index = get_hash_index(ip); - /*判断表中该单元是否为NLL */ - /* list_empty - tests whether a list is empty*/ - /* @head: the list to test.*/ - if( list_empty(&hash_array[index])) - { - return NULL; - } - /*查找用户IP */ + read_lock(&g_obj_lock); list_for_each(pos, &hash_array[index]) { @@ -47,9 +45,10 @@ USERINFO *search_user(uint32_t ip) * @member: the name of the list_head within the struct. */ pNode = list_entry(pos, USERINFO, hnode); if(pNode != NULL) - printk("user ip:%d user id:%d\n", pNode->user_ip, pNode->user_id); + printk("user ip:%ld user id:%d\n", pNode->user_ip, pNode->user_id); return pNode; } + read_unlock(&g_obj_lock); return NULL; } @@ -71,11 +70,10 @@ int add_user(uint32_t ip, int id) pNode = (USERINFO *)kmalloc(sizeof(USERINFO), GFP_KERNEL); if (pNode == NULL) { - return -1; + return ENOMEM; } - pNode->user_ip = ip; - + list_add_tail(&pNode->hnode,&hash_array[index]); } printk("IP ALEADY EXISTED\n"); @@ -88,7 +86,7 @@ int add_user(uint32_t ip, int id) void del_user(uint32_t ip) { int index; - struct list_head *pos; + struct list_head *pos, *n; USERINFO *pNode; index = get_hash_index(ip); @@ -100,13 +98,15 @@ void del_user(uint32_t ip) } /*查找用户ip */ - list_for_each(pos, &hash_array[index]) + write_lock(&g_obj_lock); + list_for_each_safe(pos, n, &hash_array[index]) { pNode = list_entry(pos, USERINFO, hnode); if(pNode->user_ip == ip) list_del(&pNode->hnode); kfree(pNode); } + write_unlock(&g_obj_lock); return; } @@ -123,12 +123,14 @@ void free_all_user(void) { if(list_empty(&hash_array[i])) continue; + write_lock(&g_obj_lock); list_for_each_safe(pos, n, &hash_array[i]) { pNode = list_entry(pos, USERINFO, hnode); list_del(&pNode->hnode); kfree(pNode); } + write_unlock(&g_obj_lock); } } @@ -142,11 +144,13 @@ void printk_all_user(void) for(i = 0; i < HASH_SIZE; i++) continue; + read_lock(&g_obj_lock); list_for_each_safe(pos, n, &hash_array[i]) { pNode = list_entry(pos, USERINFO, hnode); printk("user ip:%d user id:%d\n", pNode->user_ip, pNode->user_id); } + read_unlock(&g_obj_lock); }