Index: job.c
===================================================================
RCS file: /sources/make/make/job.c,v
retrieving revision 1.211
diff -u -r1.211 job.c
--- job.c	6 Nov 2010 21:56:24 -0000	1.211
+++ job.c	15 Apr 2011 14:02:12 -0000
@@ -243,6 +243,10 @@
 /* Number of jobserver tokens this instance is currently using.  */
 
 unsigned int jobserver_tokens = 0;
+
+/* File descriptor to use as a sync semaphore in -j mode. */
+
+int syncfd = -2;
 
 #ifdef WINDOWS32
 /*
@@ -514,6 +518,56 @@
   */
 }
 
+/* Synchronize the output of jobs in -j mode to keep the results of
+   each job together. This is done by holding the results in temp files,
+   one each for stdout and stderr, and only releasing them to "real"
+   stdout and stderr when an exclusive lock can be obtained. */
+
+static void
+sync_output(struct child *c)
+{
+  ssize_t nread;
+  char buffer[8192];
+  struct flock fl;
+
+  if (c->tempout && lseek(fileno(c->tempout), 0, SEEK_SET) == -1)
+    perror("lseek");
+  if (c->temperr && lseek(fileno(c->temperr), 0, SEEK_SET) == -1)
+    perror("lseek");
+
+  memset(&fl, 0, sizeof(fl));
+  fl.l_type = F_WRLCK;
+  fl.l_whence = SEEK_SET;
+  fl.l_pid = getpid();
+  fl.l_start = fl.l_len = 0;
+  if (fcntl(syncfd, F_SETLKW, &fl) == -1)
+    perror("fcntl");
+  else
+    {
+      /*
+       * We've entered the "critical section" during which a lock is held.
+       * We want to keep it as short as possible.
+       */
+      if (c->tempout)
+	while ((nread = fread(buffer, 1, sizeof(buffer), c->tempout)) > 0)
+	  write(fileno(stdout), buffer, nread);
+
+      if (c->temperr)
+	while ((nread = fread(buffer, 1, sizeof(buffer), c->temperr)) > 0)
+	  write(fileno(stderr), buffer, nread);
+
+      /* Exit the critical section */
+      fl.l_type = F_UNLCK;
+      if (fcntl(syncfd, F_SETLKW, &fl) == -1)
+	perror("fcntl");
+    }
+
+  if (c->tempout)
+      fclose(c->tempout);
+  if (c->temperr)
+      fclose(c->temperr);
+}
+
 extern int shell_function_pid, shell_function_completed;
 
 /* Reap all dead children, storing the returned status and the new command
@@ -790,6 +844,10 @@
         c->sh_batch_file = NULL;
       }
 
+      /* Synchronize parallel output if requested */
+      if (syncfd >= 0 && c->tempout)
+	sync_output(c);
+
       /* If this child had the good stdin, say it is now free.  */
       if (c->good_stdin)
         good_stdin_used = 0;
@@ -1053,6 +1111,7 @@
 #else
   char **argv;
 #endif
+  static int combined_output;
 
   /* If we have a completely empty commandset, stop now.  */
   if (!child->command_ptr)
@@ -1356,6 +1415,42 @@
 
 #else  /* !__EMX__ */
 
+      /* If .PARALLELSYNC is turned on, determine a file descriptor
+          to synchronize on. */
+
+      if (parallel_sync && syncfd == -2)
+	{
+	  struct stat stbuf_o, stbuf_e;
+
+	  if (fstat(fileno(stdout), &stbuf_o) == 0 &&
+	      fstat(fileno(stderr), &stbuf_e) == 0)
+	    {
+	      syncfd = fileno(stdout);
+	      combined_output =
+		stbuf_o.st_dev == stbuf_e.st_dev &&
+		stbuf_o.st_ino == stbuf_e.st_ino;
+	    }
+	  else if (fstat(fileno(stdout), &stbuf_o) == 0)
+	    syncfd = fileno(stdout);
+	  else if (fstat(fileno(stderr), &stbuf_e) == 0)
+	    syncfd = fileno(stderr);
+	  else
+	    syncfd = -1;
+	}
+
+      if (syncfd >= 0)
+	child->tempout = child->temperr = NULL;
+	if (combined_output && (!(child->tempout = tmpfile())))
+	  {
+	    perror("tmpfile()");
+	    goto error;
+	  }
+	else if (!(child->tempout = tmpfile()) || !(child->temperr = tmpfile()))
+	  {
+	    perror("tmpfile()");
+	    goto error;
+	  }
+
       child->pid = vfork ();
       environ = parent_environ;	/* Restore value child may have clobbered.  */
       if (child->pid == 0)
@@ -1379,6 +1474,22 @@
             setrlimit (RLIMIT_STACK, &stack_limit);
 #endif
 
+	  /* Arrange to collect output into tempfiles */
+	  if (syncfd >= 0)
+	    {
+	      int outfd = fileno(stdout);
+	      int errfd = fileno(stderr);
+
+	      if ((child->tempout &&
+		  (close(outfd) == -1 || dup2(fileno(child->tempout), outfd) == -1))
+		|| (child->temperr &&
+		  (close(errfd) == -1 || dup2(fileno(child->temperr), errfd) == -1)))
+		{
+		  perror("dup2()");
+		  goto error;
+		}
+	    }
+
 	  child_execute_job (child->good_stdin ? 0 : bad_stdin, 1,
                              argv, child->environment);
 	}
Index: job.h
===================================================================
RCS file: /sources/make/make/job.h,v
retrieving revision 1.27
diff -u -r1.27 job.h
--- job.h	13 Jul 2010 01:20:41 -0000	1.27
+++ job.h	15 Apr 2011 14:02:12 -0000
@@ -64,6 +64,8 @@
     unsigned int good_stdin:1;	/* Nonzero if this child has a good stdin.  */
     unsigned int deleted:1;	/* Nonzero if targets have been deleted.  */
     unsigned int dontcare:1;    /* Saved dontcare flag.  */
+    FILE *tempout;		/* Optional place to save stdout */
+    FILE *temperr;		/* Optional place to save stderr */
   };
 
 extern struct child *children;
Index: main.c
===================================================================
RCS file: /sources/make/make/main.c,v
retrieving revision 1.246
diff -u -r1.246 main.c
--- main.c	29 Aug 2010 23:05:27 -0000	1.246
+++ main.c	15 Apr 2011 14:02:12 -0000
@@ -503,6 +503,13 @@
 
 int one_shell;
 
+/* Nonzero if we have seen the '.PARALLELSYNC' target.
+   This attempts to synchronize the output of parallel
+   jobs such that the results of each job stay together.
+   It works best in combination with .ONESHELL.   */
+
+int parallel_sync;
+
 /* Nonzero if we have seen the `.NOTPARALLEL' target.
    This turns off parallel builds for this invocation of make.  */
 
Index: make.h
===================================================================
RCS file: /sources/make/make/make.h,v
retrieving revision 1.147
diff -u -r1.147 make.h
--- make.h	21 Feb 2011 07:30:11 -0000	1.147
+++ make.h	15 Apr 2011 14:02:12 -0000
@@ -522,7 +522,7 @@
 extern int print_version_flag, print_directory_flag, check_symlink_flag;
 extern int warn_undefined_variables_flag, trace_flag, posix_pedantic;
 extern int not_parallel, second_expansion, clock_skew_detected;
-extern int rebuilding_makefiles, one_shell;
+extern int rebuilding_makefiles, one_shell, parallel_sync;
 
 /* can we run commands via 'sh -c xxx' or must we use batch files? */
 extern int batch_mode_shell;
Index: read.c
===================================================================
RCS file: /sources/make/make/read.c,v
retrieving revision 1.196
diff -u -r1.196 read.c
--- read.c	30 Nov 2010 14:48:53 -0000	1.196
+++ read.c	15 Apr 2011 14:02:12 -0000
@@ -1961,6 +1961,8 @@
       else if (streq (name, ".ONESHELL"))
         one_shell = 1;
 #endif
+      else if (streq (name, ".PARALLELSYNC"))
+        parallel_sync = 1;
 
       /* If this is a static pattern rule:
          `targets: target%pattern: prereq%pattern; recipe',
