[vhffs-dev] [933] well, if 2 threads do a cache miss, they both send the query to the db server (everything is right here), then the both try to add the query in the cache, the first success, the second failed but it is not aware about that.

[ Thread Index | Date Index | More vhffs.org/vhffs-dev Archives ]


Revision: 933
Author:   gradator
Date:     2007-09-20 23:21:24 +0000 (Thu, 20 Sep 2007)

Log Message:
-----------
well, if 2 threads do a cache miss, they both send the query to the db server (everything is right here), then the both try to add the query in the cache, the first success, the second failed but it is not aware about that. The problem is, the 2 functions which sent the query call the clear function which unref the cache entry if cache is enabled, so the cache entry is unreferenced 2 times while cache entry is referenced only 1 time. The solution is to make the add function a bit more clever, it returns the pointer from the cache if the entry already exist, free the data received which are not needed anymore and reference the cache entry by one.

Modified Paths:
--------------
    trunk/vhffs-fs/vhffsfs.c


Modified: trunk/vhffs-fs/vhffsfs.c
===================================================================
--- trunk/vhffs-fs/vhffsfs.c	2007-09-20 21:24:50 UTC (rev 932)
+++ trunk/vhffs-fs/vhffsfs.c	2007-09-20 23:21:24 UTC (rev 933)
@@ -134,13 +134,15 @@
 	pthread_mutex_unlock(&vhffsfs.cachelock);
 }
 
-void vhffsfs_cache_add(char *query, PGresult *result)  {
+PGresult *vhffsfs_cache_add(char *query, PGresult *result)  {
 	vhffsfs_cache_query *vcq;
 	vhffsfs_cache_lock();
 	vcq = g_hash_table_lookup(vhffsfs.cachequeries, query);
 	if(vcq) {
+		vcq->ref++;
 		vhffsfs_cache_unlock();
-		return;
+		PQclear(result);
+		return vcq->result;
 	}
 #ifdef WITH_CACHE_DEBUG
 	printf("CACHE: ADDING: '%s'\n", query);
@@ -153,6 +155,7 @@
 	g_hash_table_insert(vhffsfs.cachequeries, vcq->query, vcq);
 	g_ptr_array_add(vhffsfs.cachekeys, vcq->query);
 	vhffsfs_cache_unlock();
+	return vcq->result;
 }
 
 void vhffsfs_cache_del(char *query)  {
@@ -338,7 +341,7 @@
 		//fprintf(stdout, "SLOT[%d]: QUERY: '%s'\n", i, query);
 #ifdef WITH_CACHE
 		// fill cache
-		vhffsfs_cache_add(query, res);
+		res = vhffsfs_cache_add(query, res);
 #endif
 	}
 
@@ -347,9 +350,6 @@
 }
 
 static inline void vhffsfs_PGClear(char *query, PGresult *result)  {
-
-	if(!result || !query) return;
-
 #ifdef WITH_CACHE
 	vhffsfs_cache_unref(query);
 #endif


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/