--- a/includes/bootstrap.inc.orig	2011-08-08 17:51:25.000000000 +1000
+++ b/includes/bootstrap.inc	2011-10-17 22:19:40.000000000 +1100
@@ -669,12 +669,22 @@
 function variable_set($name, $value) {
   global $conf, $db_prefix;
 
+  // first variable_* cache race condition.
+  //
+  // unfortunately... locking an already locked lock does not wait until it is unlocked.
+  // so we specifically wait for it.
+  lock_wait('variable_cache_regenerate', 3);
+  lock_acquire('variable_cache_regenerate', 3);
+  // (re-)read the cache.
+  $conf = variable_init( array() );
+
   $serialized_value = serialize($value);
   db_query("UPDATE {variable} SET value = '%s' WHERE name = '%s'", $serialized_value, $name);
   if (!db_affected_rows()) {
     @db_query("INSERT INTO {variable} (name, value) VALUES ('%s', '%s')", $name, $serialized_value);
   }
 
+  // modify the cache.
   $conf[$name] = $value;
 
   // The write-through rebuild optimization isn't compatible with SimpleTest.
@@ -683,8 +693,13 @@
   if (is_string($db_prefix) && strpos($db_prefix, 'simpletest') === 0) {
     cache_clear_all('variables', 'cache');
   }
-  
-  variable_cache_rebuild();
+
+  // update the cache.
+  cache_set('variables', $conf);
+  // unlock.
+  lock_release('variable_cache_regenerate');
+
+  return;
 }
 
 /**
@@ -702,8 +717,15 @@
 function variable_del($name) {
   global $conf, $db_prefix;
 
+  // lock.
+  lock_wait('variable_cache_regenerate', 3);
+  lock_acquire('variable_cache_regenerate', 3);
+  // (re-)read the cache.
+  $conf = variable_init( array() );
+
   db_query("DELETE FROM {variable} WHERE name = '%s'", $name);
 
+  // modify the cache.
   unset($conf[$name]);
 
   // The write-through rebuild optimization isn't compatible with SimpleTest.
@@ -712,19 +734,13 @@
   if (is_string($db_prefix) && strpos($db_prefix, 'simpletest') === 0) {
     cache_clear_all('variables', 'cache');
   }
-  
-  variable_cache_rebuild();
-}
 
-/**
- * Schedules a rebuild of the variable cache on shutdown.
- */
-function variable_cache_rebuild() {
-  static $shutdown_registered = FALSE;
-  if (!$shutdown_registered) {
-    register_shutdown_function('variable_init', array(), TRUE);
-    $shutdown_registered = TRUE;
-  }
+  // update the cache.
+  cache_set('variables', $conf);
+  // unlock.
+  lock_release('variable_cache_regenerate');
+
+  return;
 }
 
 /**
--- a/includes/lock.inc.orig	2011-06-01 17:13:26.000000000 +1000
+++ b/includes/lock.inc	2011-10-17 22:17:17.000000000 +1100
@@ -102,7 +102,8 @@
   $expire = (float)$usec + (float)$sec + $timeout;
   if (isset($locks[$name])) {
     // Try to extend the expiration of a lock we already acquired.
-    if (!db_result(db_query("UPDATE {semaphore} SET expire = %f WHERE name = '%s' AND value = '%s'", $expire, $name, _lock_id()))) {
+    db_query("UPDATE {semaphore} SET expire = %f WHERE name = '%s' AND value = '%s'", $expire, $name, _lock_id());
+    if (!db_affected_rows()) {
       // The lock was broken.
       unset($locks[$name]);
     }
@@ -184,11 +185,12 @@
     // This function should only be called by a request that failed to get a
     // lock, so we sleep first to give the parallel request a chance to finish
     // and release the lock.
-    sleep(1);
     if (lock_may_be_available($name)) {
       // No longer need to wait.
       return FALSE;
     }
+
+    usleep(250000); // 250 mS
   }
   // The caller must still wait longer to get the lock.
   return TRUE;
--- a/includes/cache.inc.orig	2011-06-01 17:13:26.000000000 +1000
+++ b/includes/cache.inc	2011-10-17 22:26:59.000000000 +1100
@@ -23,35 +23,43 @@
   }
 
   $cache = db_fetch_object(db_query("SELECT data, created, headers, expire, serialized FROM {". $table ."} WHERE cid = '%s'", $cid));
-  if (isset($cache->data)) {
-    // If the data is permanent or we're not enforcing a minimum cache lifetime
-    // always return the cached data.
-    if ($cache->expire == CACHE_PERMANENT || !variable_get('cache_lifetime', 0)) {
-      $cache->data = db_decode_blob($cache->data);
-      if ($cache->serialized) {
-        $cache->data = unserialize($cache->data);
-      }
-    }
-    // If enforcing a minimum cache lifetime, validate that the data is
-    // currently valid for this user before we return it by making sure the
-    // cache entry was created before the timestamp in the current session's
-    // cache timer. The cache variable is loaded into the $user object by
-    // sess_read() in session.inc.
-    else {
-      if (isset($user->cache) && $user->cache > $cache->created) {
-        // This cache data is too old and thus not valid for us, ignore it.
-        return 0;
-      }
-      else {
-        $cache->data = db_decode_blob($cache->data);
-        if ($cache->serialized) {
-          $cache->data = unserialize($cache->data);
-        }
-      }
-    }
+  // cache miss.
+  if (!isset($cache->data)) {
+    return 0;
+  }
+
+  // unpack the cached data from db.
+  $cache->data = db_decode_blob($cache->data);
+  if ($cache->serialized) {
+    $cache->data = unserialize($cache->data);
+  }
+
+  //
+  // If the data is permanent or we're not enforcing a minimum cache lifetime
+  // always return the cached data.
+  //
+  if ($cache->expire == CACHE_PERMANENT || !variable_get('cache_lifetime', 0)) {
     return $cache;
   }
-  return 0;
+
+  //
+  // If enforcing a minimum cache lifetime, validate that the data is
+  // currently valid for this user before we return it by making sure the
+  // cache entry was created before the timestamp in the current session's
+  // cache timer. The cache variable is loaded into the $user object by
+  // sess_read() in session.inc.
+  //
+  if ($user->cache && $user->cache > $cache->created) {
+    return 0;
+  }
+
+  // garbage collection deals with inaccessed cached data.
+  // this checks expiry on the returned result data
+  if ($cache->expire > 0 && $cache->expire < time()) {
+    return 0;
+  }
+
+  return $cache;
 }
 
 /**
