Index: includes/bootstrap.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/bootstrap.inc,v
retrieving revision 1.409
diff -u -p -r1.409 bootstrap.inc
--- includes/bootstrap.inc	10 Jul 2010 17:27:50 -0000	1.409
+++ includes/bootstrap.inc	13 Jul 2010 07:13:37 -0000
@@ -870,11 +870,35 @@ function drupal_page_get_cache($check_on
   }
 
   if (drupal_page_is_cacheable()) {
-    $cache = cache_get($base_root . request_uri(), 'cache_page');
+    // If the site is configured to load the page from cache without hitting
+    // the database, drupal_page_get_cache() could get called before the lock
+    // system has been initialized.
+    if (variable_get('page_cache_without_database')) {
+      require_once DRUPAL_ROOT . '/' . variable_get('lock_inc', 'includes/lock.inc');
+      lock_initialize();
+    }
+
+    // Try to get either the cached page or a lock five times. If that fails,
+    // continue with page execution, because the site may still be able to
+    // serve the request.
+    $name = $GLOBALS['base_root'] . request_uri();
+    do {
+      if (!$cache = cache_get($base_root . request_uri(), 'cache_page')) {
+        if (!$lock_acquired = lock_acquire($name)) {
+          lock_wait($name);
+        }
+        else {
+          // Proceed with page build, cache its result, then release the lock.
+          header('X-Drupal-Cache: MISS');
+        }
+      }
+    }
+    while ($cache === FALSE && $lock_acquired === FALSE && ++$lock_acquire_attempts < 5);
+
     if ($cache !== FALSE) {
       $cache_hit = TRUE;
+      return $cache;
     }
-    return $cache;
   }
 }
 
@@ -2103,13 +2127,17 @@ function _drupal_bootstrap_page_cache() 
   foreach (variable_get('cache_backends', array()) as $include) {
     require_once DRUPAL_ROOT . '/' . $include;
   }
-  // Check for a cache mode force from settings.php.
+  // Check for a cache mode force from settings.php. It's not safe to invoke
+  // hooks without the database, so set a flag based on the value of
+  // $conf['page_cache_without_database'].
   if (variable_get('page_cache_without_database')) {
     $cache_enabled = TRUE;
+    $hook_invocation_is_safe = FALSE;
   }
   else {
     drupal_bootstrap(DRUPAL_BOOTSTRAP_VARIABLES, FALSE);
     $cache_enabled = variable_get('cache');
+    $hook_invocation_is_safe = TRUE;
   }
   drupal_block_denied(ip_address());
   // If there is no session cookie and cache is enabled (or forced), try
@@ -2127,23 +2155,18 @@ function _drupal_bootstrap_page_cache() 
       $_GET['q'] = $cache->data['path'];
       drupal_set_title($cache->data['title'], PASS_THROUGH);
       date_default_timezone_set(drupal_get_user_timezone());
-      // If the skipping of the bootstrap hooks is not enforced, call
-      // hook_boot.
-      if (variable_get('page_cache_invoke_hooks', TRUE)) {
+      // If the skipping of bootstrap hooks is not enforced, call hook_boot().
+      if ($hook_invocation_is_safe && variable_get('page_cache_invoke_hooks', TRUE)) {
         bootstrap_invoke_all('boot');
       }
       drupal_serve_page_from_cache($cache);
-      // If the skipping of the bootstrap hooks is not enforced, call
-      // hook_exit.
-      if (variable_get('page_cache_invoke_hooks', TRUE)) {
+      // If the skipping of bootstrap hooks is not enforced, call hook_exit().
+      if ($hook_invocation_is_safe && variable_get('page_cache_invoke_hooks', TRUE)) {
         bootstrap_invoke_all('exit');
       }
       // We are done.
       exit;
     }
-    else {
-      header('X-Drupal-Cache: MISS');
-    }
   }
 }
 
Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.1191
diff -u -p -r1.1191 common.inc
--- includes/common.inc	7 Jul 2010 17:00:42 -0000	1.1191
+++ includes/common.inc	13 Jul 2010 07:13:40 -0000
@@ -4607,6 +4607,7 @@ function drupal_page_set_cache() {
         $cache->data['body'] = gzencode($cache->data['body'], 9, FORCE_GZIP);
       }
       cache_set($cache->cid, $cache->data, 'cache_page', $cache->expire);
+      lock_release($cache->cid);
     }
     return $cache;
   }
@@ -5235,7 +5236,17 @@ function drupal_render_cache_get($elemen
   }
   $bin = isset($elements['#cache']['bin']) ? $elements['#cache']['bin'] : 'cache';
 
-  if (!empty($cid) && $cache = cache_get($cid, $bin)) {
+  $lock_acquire_attempts = 0;
+  do {
+    if (!$cache = cache_get($cid, $bin)) {
+      if (!$lock_acquired = lock_acquire($cid, 1)) {
+        lock_wait($cid, 3);
+      }
+    }
+  }
+  while ($cache === FALSE && $lock_acquired === FALSE && ++$lock_acquire_attempts < 10);
+  
+  if ($cache) {
     // Add additional libraries, JavaScript, CSS and other data attached
     // to this element.
     if (isset($cache->data['#attached'])) {
@@ -5244,6 +5255,10 @@ function drupal_render_cache_get($elemen
     // Return the rendered output.
     return $cache->data['#markup'];
   }
+
+  // Proceed with rendering. Even if we failed to to acquire the lock, we
+  // should try to render the elements. Locking is a pluggable system, so the
+  // site may still function even when repeated attempts to use it fail.
   return FALSE;
 }
 
@@ -5280,6 +5295,7 @@ function drupal_render_cache_set(&$marku
   $bin = isset($elements['#cache']['bin']) ? $elements['#cache']['bin'] : 'cache';
   $expire = isset($elements['#cache']['expire']) ? $elements['#cache']['expire'] : CACHE_PERMANENT;
   cache_set($cid, $data, $bin, $expire);
+  lock_release($cid);
 }
 
 /**
Index: includes/lock.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/lock.inc,v
retrieving revision 1.4
diff -u -p -r1.4 lock.inc
--- includes/lock.inc	6 May 2010 15:20:18 -0000	1.4
+++ includes/lock.inc	13 Jul 2010 07:13:41 -0000
@@ -195,11 +195,16 @@ function lock_may_be_available($name) {
  */
 function lock_wait($name, $delay = 30) {
   $delay = (int) $delay;
-  while ($delay--) {
+  $sleep = 1;
+  while ($delay > 0) {
     // 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);
+    sleep($sleep);
+    // After each sleep, double the value of $sleep, to reduce the potential
+    // for a lock stampede.
+    $delay = $delay - $sleep;
+    $sleep = min($sleep * 2, $delay);
     if (lock_may_be_available($name)) {
       // No longer need to wait.
       return FALSE;
