--- a/includes/bootstrap.inc.orig	2011-08-08 17:51:25.000000000 +1000
+++ b/includes/bootstrap.inc	2011-10-15 22:30:33.000000000 +1100
@@ -669,12 +669,25 @@
 function variable_set($name, $value) {
   global $conf, $db_prefix;
 
+  if (function_exists('db_insert')) {
+    // 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 +696,16 @@
   if (is_string($db_prefix) && strpos($db_prefix, 'simpletest') === 0) {
     cache_clear_all('variables', 'cache');
   }
-  
-  variable_cache_rebuild();
+
+  if (function_exists('db_insert')) {
+    // update the cache.
+    cache_set('variables', $conf);
+
+    // unlock.
+    lock_release('variable_cache_regenerate');
+  }
+
+  return;
 }
 
 /**
@@ -702,8 +723,18 @@
 function variable_del($name) {
   global $conf, $db_prefix;
 
+  if (function_exists('db_insert')) {
+    // 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 +743,16 @@
   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;
+  if (function_exists('db_insert')) {
+    // 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-14 18:51:37.000000000 +1100
@@ -184,11 +184,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;
