Concurrency Bug in memcached
Brian Aker
brian at tangent.org
Tue Feb 5 01:42:54 UTC 2008
Hi!
Here is a patch to fix the problem with concurrency in the
set_current_time() call.
diff -r 1d3a59c1dbd3 memcached.c
--- a/memcached.c Mon Feb 04 15:07:29 2008 -0800
+++ b/memcached.c Mon Feb 04 17:33:21 2008 -0800
@@ -2560,7 +2560,9 @@ static struct event clockevent;
/* time-sensitive callers can call it by hand with this, outside the
normal ever-1-second timer */
static void set_current_time(void) {
- current_time = (rel_time_t) (time(0) - stats.started);
+ volatile rel_time_t new_time;
+ new_time = (rel_time_t) (time(0) - stats.started);
+ while(!( __sync_bool_compare_and_swap(¤t_time, current_time,
new_time)));
}
The problem will be experienced when you are on a dual processor
machine (aka not a dual core (for most vendors) just a dual
processor). The volatile declaration only helps with caching, it does
not solve the problem where one processor will see one thing, while
another will see a different value during the setting of the variable.
One note about the above, I did not use a mutex since this variable
would then have to be locked in all sections of the code (which would
be slow...). This is a gcc specific fix BTW. If others are interested
we can put together an include file for different platforms.
Cheers,
-Brian
--
_______________________________________________________
Brian "Krow" Aker, brian at tangent.org
Seattle, Washington
http://krow.net/ <-- Me
http://tangent.org/ <-- Software
http://exploitseattle.com/ <-- Fun
_______________________________________________________
You can't grep a dead tree.
More information about the memcached
mailing list