2 * This file has been modified for the cdrkit suite.
4 * The behaviour and appearence of the program code below can differ to a major
5 * extent from the version distributed by the original author(s).
7 * For details, see Changelog file distributed with the cdrkit package. If you
8 * received this file from another source then ask the distributing person for
9 * a log of modifications.
13 #ident "$Id: workers.c,v 1.3 2006/07/04 04:57:42 hpa Exp $"
14 /* ----------------------------------------------------------------------- *
16 * Copyright 2001-2006 H. Peter Anvin - All Rights Reserved
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
21 * USA; either version 2 of the License, or (at your option) any later
22 * version; incorporated herein by reference.
24 * ----------------------------------------------------------------------- */
29 * Parallel job maintenance
32 #include "mkzftree.h" /* Must be included first! */
42 extern int parallel; /* Number of parallel jobs */
44 /* Functions related to parallel execution */
45 static volatile int work_threads = 0;
46 static int is_worker = 0;
48 /* This waits for one worker to finish */
49 static void wait_for_one_worker(void)
53 if ( wait(&status) > 0 ) {
56 if ( WIFSIGNALED(status) ) {
57 kill(getpid(), WTERMSIG(status));
59 } else if ( WEXITSTATUS(status) ) {
60 exit(WEXITSTATUS(status));
65 /* This waits for *all* workers to finish */
66 void wait_for_all_workers(void)
68 while ( work_threads )
69 wait_for_one_worker();
72 /* This returns 1 if the "job" at hand should be performed */
73 int spawn_worker(void)
77 if ( opt.parallel == 0 )
82 /* Wait for a work slot */
83 while ( work_threads >= opt.parallel )
84 wait_for_one_worker();
86 /* Spawn worker process */
87 work_threads++; /* Avoids race conditions */
91 return 1; /* Do it ourselves */
104 /* Routine to perform at the end of the job */
105 void end_worker(int err)