Index: includes/bootstrap.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/bootstrap.inc,v
retrieving revision 1.399
diff -u -p -r1.399 bootstrap.inc
--- includes/bootstrap.inc	14 Jun 2010 13:24:32 -0000	1.399
+++ includes/bootstrap.inc	23 Jun 2010 06:43:09 -0000
@@ -743,13 +743,27 @@ function drupal_get_filename($type, $nam
  * file.
  */
 function variable_initialize($conf = array()) {
-  // NOTE: caching the variables improves performance by 20% when serving cached pages.
+  // NOTE: caching the variables improves performance by 20% when serving
+  // cached pages.
   if ($cached = cache_get('variables', 'cache_bootstrap')) {
     $variables = $cached->data;
   }
   else {
-    $variables = array_map('unserialize', db_query('SELECT name, value FROM {variable}')->fetchAllKeyed());
-    cache_set('variables', $variables, 'cache_bootstrap');
+    // Cache miss. Avoid a stampede.
+    require_once DRUPAL_ROOT . '/' . variable_get('lock_inc', 'includes/lock.inc');
+    lock_initialize();
+    $name = 'variable_init';
+    if (!lock_acquire($name, 1)) {
+      // Another request is building the variable cache.
+      // Wait, then re-run this function.
+      lock_wait($name);
+      return variable_initialize();
+    }
+    else {
+    // Proceed with variable rebuild.
+      $variables = array_map('unserialize', db_query('SELECT name, value FROM {variable}')->fetchAllKeyed());
+      cache_set('variables', $variables, 'cache_bootstrap');
+    }
   }
 
   foreach ($conf as $name => $value) {
