=== modified file 'sites/all/modules/contrib/memcache/memcache.inc'
--- sites/all/modules/contrib/memcache/memcache.inc     2010-08-18 17:49:46 +0000
+++ sites/all/modules/contrib/memcache/memcache.inc     2010-08-19 14:48:01 +0000
@@ -193,33 +193,33 @@ class MemCacheDrupal implements DrupalCa

         // Ensure the prefix being cleared is listed, if not, atomically add it.
         // Adding new prefixes should be rare.
-        if (!in_array($cid, $memcached_prefixes[$this->bin])) {
-          // Acquire a semaphore.
-          $lock_key = dmemcache_key('.prefixes.lock', $this->bin);
-          while (!dmemcache_add($lock_key, 1, 10)) {
-            usleep(1000);
-          }
-
-          // Get a fresh copy of the prefix directory.
-          $memcached_prefixes[$this->bin] = dmemcache_get('.prefixes', $this->bin);
-          if (!is_array($memcached_prefixes[$this->bin])) {
-            $memcached_prefixes[$this->bin] = array();
-          }
-
-          // Only add the prefix if it's not in the updated directory.
+        do {
           if (!in_array($cid, $memcached_prefixes[$this->bin])) {
-            // Add the new prefix.
-            $memcached_prefixes[$this->bin][] = $cid;
-
-            // Store to memcached.
-            dmemcache_set('.prefixes', $memcached_prefixes[$this->bin], 0, $this->bin);
-
-            // Set the clearing counter to zero.
-            dmemcache_set('.prefix.' . $cid, 0, 0, $this->bin);
+            $lock_name = 'memcache.prefixes:' . $this->bin;
+            if (!$lock_acquired = lock_acquire($lock_name)) {
+              lock_wait();
+            }
+
+            // Get a fresh copy of the prefix directory.
+            $memcached_prefixes[$this->bin] = dmemcache_get('.prefixes', $this->bin);
+            if (!is_array($memcached_prefixes[$this->bin])) {
+              $memcached_prefixes[$this->bin] = array();
+            }
+
+            // Only add the prefix if it's not in the updated directory.
+            if (!in_array($cid, $memcached_prefixes[$this->bin])) {
+              // Add the new prefix.
+              $memcached_prefixes[$this->bin][] = $cid;
+
+              // Store to memcached.
+              dmemcache_set('.prefixes', $memcached_prefixes[$this->bin], 0, $this->bin);
+
+              // Set the clearing counter to zero.
+              dmemcache_set('.prefix.' . $cid, 0, 0, $this->bin);
+            }
+            lock_release($lock_name);
           }
-
-          // Release the semaphore.
-          dmemcache_delete('.prefixes.lock', $this->bin);
+          while(in_array($cid, $memcached_prefixes[$this->bin]) && $lock_acquired === FALSE && ++$lock_acquire_attempts < 5);
         }

         // Increment the prefix clearing counter.


