Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.1208
diff -u -p -r1.1208 common.inc
--- includes/common.inc	18 Aug 2010 00:44:52 -0000	1.1208
+++ includes/common.inc	18 Aug 2010 09:14:41 -0000
@@ -3210,42 +3210,52 @@ function drupal_build_css_cache($css) {
     $uri = $map[$key];
   }
 
-  if (empty($uri) || !file_exists($uri)) {
-    // Build aggregate CSS file.
-    foreach ($css as $stylesheet) {
-      // Only 'file' stylesheets can be aggregated.
-      if ($stylesheet['type'] == 'file') {
-        $contents = drupal_load_stylesheet($stylesheet['data'], TRUE);
-        // Return the path to where this CSS file originated from.
-        $base = base_path() . dirname($stylesheet['data']) . '/';
-        _drupal_build_css_path(NULL, $base);
-        // Prefix all paths within this CSS file, ignoring external and absolute paths.
-        $data .= preg_replace_callback('/url\(\s*[\'"]?(?![a-z]+:|\/+)([^\'")]+)[\'"]?\s*\)/i', '_drupal_build_css_path', $contents);
-      }
-    }
+  $name = $uri;
 
-    // Per the W3C specification at http://www.w3.org/TR/REC-CSS2/cascade.html#at-import,
-    // @import rules must proceed any other style, so we move those to the top.
-    $regexp = '/@import[^;]+;/i';
-    preg_match_all($regexp, $data, $matches);
-    $data = preg_replace($regexp, '', $data);
-    $data = implode('', $matches[0]) . $data;
+  do {
+    if (empty($uri) || !file_exists($uri)) {
+      if (!$lock_acquired = lock_acquire($name)) {
+        lock_wait($name);
+      }
+      else {
+        // Build aggregate CSS file.
+        foreach ($css as $stylesheet) {
+          // Only 'file' stylesheets can be aggregated.
+          if ($stylesheet['type'] == 'file') {
+            $contents = drupal_load_stylesheet($stylesheet['data'], TRUE);
+            // Return the path to where this CSS file originated from.
+            $base = base_path() . dirname($stylesheet['data']) . '/';
+            _drupal_build_css_path(NULL, $base);
+            // Prefix all paths within this CSS file, ignoring external and absolute paths.
+            $data .= preg_replace_callback('/url\(\s*[\'"]?(?![a-z]+:|\/+)([^\'")]+)[\'"]?\s*\)/i', '_drupal_build_css_path', $contents);
+          }
+        }
 
-    // Prefix filename to prevent blocking by firewalls which reject files
-    // starting with "ad*".
-    $filename = 'css_' . drupal_hash_base64($data) . '.css';
-    // Create the css/ within the files folder.
-    $csspath = 'public://css';
-    $uri = $csspath . '/' . $filename;
-    // Create the CSS file.
-    file_prepare_directory($csspath, FILE_CREATE_DIRECTORY);
-    if (!file_exists($uri) && !file_unmanaged_save_data($data, $uri, FILE_EXISTS_REPLACE)) {
-      return FALSE;
+        // Per the W3C specification at http://www.w3.org/TR/REC-CSS2/cascade.html#at-import,
+        // @import rules must proceed any other style, so we move those to the top.
+        $regexp = '/@import[^;]+;/i';
+        preg_match_all($regexp, $data, $matches);
+        $data = preg_replace($regexp, '', $data);
+        $data = implode('', $matches[0]) . $data;
+
+        // Prefix filename to prevent blocking by firewalls which reject files
+        // starting with "ad*".
+        $filename = 'css_' . drupal_hash_base64($data) . '.css';
+        // Create the css/ within the files folder.
+        $csspath = 'public://css';
+        $uri = $csspath . '/' . $filename;
+        // Create the CSS file.
+        file_prepare_directory($csspath, FILE_CREATE_DIRECTORY);
+        if (!file_exists($uri) && !file_unmanaged_save_data($data, $uri, FILE_EXISTS_REPLACE)) {
+          return FALSE;
+        }
+        // Save the updated map.
+        $map[$key] = $uri;
+        variable_set('drupal_css_cache_files', $map);
+      }
     }
-    // Save the updated map.
-    $map[$key] = $uri;
-    variable_set('drupal_css_cache_files', $map);
   }
+  while (!file_exists($uri) && $lock_acquired === FALSE && ++$lock_acquire_attempts < 5);
   return $uri;
 }
 
@@ -5977,7 +5987,7 @@ function drupal_write_record($table, &$r
     }
 
     if (!property_exists($object, $field)) {
-      // Skip fields that are not provided, default values are already known 
+      // Skip fields that are not provided, default values are already known
       // by the database.
       continue;
     }
