ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rpiece.c
(Generate patch)

Comparing ray/src/util/rpiece.c (file contents):
Revision 2.24 by greg, Fri Aug 6 12:58:43 1993 UTC vs.
Revision 2.30 by greg, Mon Apr 11 14:44:10 1994 UTC

# Line 9 | Line 9 | static char SCCSid[] = "$SunId$ LBL";
9   */
10  
11   #include "standard.h"
12 #include <fcntl.h>
12  
13   #ifndef F_SETLKW
14  
# Line 40 | Line 39 | char *argv[];
39   #endif
40   #endif
41                                          /* protection from SYSV signals(!) */
42 < #if defined(sgi) || defined(hpux)
42 > #if defined(sgi)
43   #define guard_io()      sighold(SIGALRM)
44   #define unguard()       sigrelse(SIGALRM)
45   #endif
# Line 62 | Line 61 | int  hres = 1024, vres = 1024, hmult = 4, vmult = 4;
61   char  *outfile = NULL;
62   int  outfd;
63   long  scanorig;
64 < int  syncfd = -1;               /* lock file descriptor */
64 > FILE  *syncfp = NULL;           /* synchronization file pointer */
65 > int  synclst = F_UNLCK;         /* synchronization file lock status */
66   int  nforked = 0;
67  
68 + #define  sflock(t)      if ((t)!=synclst) dolock(fileno(syncfp),synclst=t)
69 +
70   char  *progname;
71   int  verbose = 0;
72 + int  rvrlim = -1;
73  
74   extern long  lseek(), ftell();
75  
# Line 137 | Line 140 | char  *argv[];
140                                          break;
141                                  vmult = atoi(argv[++i]);
142                                  continue;
143 +                        case 'R':               /* recover */
144 +                                if (argv[i][2])
145 +                                        break;
146 +                                rvrlim = 0;
147 +                        /* fall through */
148                          case 'F':               /* syncronization file */
149                                  if (argv[i][2])
150                                          break;
151 <                                if ((syncfd = open(argv[++i],
152 <                                                O_RDWR|O_CREAT, 0666)) < 0) {
151 >                                if ((syncfp =
152 >                fdopen(open(argv[++i],O_RDWR|O_CREAT,0666),"r+")) == NULL) {
153                                          fprintf(stderr, "%s: cannot open\n",
154                                                          argv[i]);
155                                          exit(1);
# Line 174 | Line 182 | char  *argv[];
182   }
183  
184  
185 + dolock(fd, ltyp)                /* lock or unlock a file */
186 + int  fd;
187 + int  ltyp;
188 + {
189 +        static struct flock  fls;       /* static so initialized to zeroes */
190 +        extern char  *sys_errlist[];
191 +
192 +        fls.l_type = ltyp;
193 +        if (fcntl(fd, F_SETLKW, &fls) < 0) {
194 +                fprintf(stderr, "%s: cannot lock/unlock file: %s\n",
195 +                                progname, sys_errlist[errno]);
196 +                exit(1);
197 +        }
198 + }
199 +
200 +
201   init(ac, av)                    /* set up output file and start rpict */
202   int  ac;
203   char  **av;
# Line 188 | Line 212 | char  **av;
212                  fprintf(stderr, "%s: %s\n", progname, err);
213                  exit(1);
214          }
215 <        if (syncfd != -1) {
216 <                char  buf[32];
217 <                buf[read(syncfd, buf, sizeof(buf)-1)] = '\0';
218 <                sscanf(buf, "%d %d", &hmult, &vmult);
215 >        if (syncfp != NULL) {
216 >                sflock(F_RDLCK);
217 >                fscanf(syncfp, "%d %d", &hmult, &vmult);
218 >                sflock(F_UNLCK);
219          }
220                                          /* compute piece size */
221          hres /= hmult;
# Line 206 | Line 230 | char  **av;
230          rpargv[rpargc] = NULL;
231                                          /* open output file */
232          if ((outfd = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0666)) >= 0) {
233 +                dolock(outfd, F_WRLCK);
234                  if ((fp = fdopen(dup(outfd), "w")) == NULL)
235                          goto filerr;
236 <                printargs(ac, av, fp);          /* write header */
236 >                newheader("RADIANCE", fp);      /* create header */
237 >                printargs(ac, av, fp);
238                  fprintf(fp, "SOFTWARE= %s\n", VersionID);
239                  fputs(VIEWSTR, fp);
240                  fprintview(&ourview, fp);
# Line 219 | Line 245 | char  **av;
245                  putc('\n', fp);
246                  fprtresolu(hres*hmult, vres*vmult, fp);
247          } else if ((outfd = open(outfile, O_RDWR)) >= 0) {
248 +                dolock(outfd, F_RDLCK);
249                  if ((fp = fdopen(dup(outfd), "r+")) == NULL)
250                          goto filerr;
251                  getheader(fp, NULL, NULL);      /* skip header */
# Line 236 | Line 263 | char  **av;
263          scanorig = ftell(fp);           /* record position of first scanline */
264          if (fclose(fp) == -1)           /* done with stream i/o */
265                  goto filerr;
266 < #if NFS
240 <        sync();                         /* flush NFS buffers */
241 < #endif
266 >        dolock(outfd, F_UNLCK);
267                                          /* start rpict process */
268          if (open_process(rpd, rpargv) <= 0) {
269                  fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]);
# Line 266 | Line 291 | int
291   nextpiece(xp, yp)               /* get next piece assignment */
292   int  *xp, *yp;
293   {
269        struct flock  fls;
270        char  buf[64];
271
294          if (gotalrm)                    /* someone wants us to quit */
295                  return(0);
296 <        if (syncfd != -1) {             /* use sync file */
297 <                fls.l_type = F_WRLCK;           /* gain exclusive access */
298 <                fls.l_whence = 0;
299 <                fls.l_start = 0L;
300 <                fls.l_len = 0L;
301 <                fcntl(syncfd, F_SETLKW, &fls);
302 <                lseek(syncfd, 0L, 0);
303 <                buf[read(syncfd, buf, sizeof(buf)-1)] = '\0';
304 <                if (sscanf(buf, "%*d %*d %d %d", xp, yp) < 2) {
296 >        if (syncfp != NULL) {           /* use sync file */
297 >                /*
298 >                 * So we don't necessarily have to lock and unlock the file
299 >                 * multiple times (very slow), we establish an exclusive
300 >                 * lock at the beginning on our synchronization file and
301 >                 * maintain it in the subroutine rvrpiece().
302 >                 */
303 >                sflock(F_WRLCK);
304 >                fseek(syncfp, 0L, 0);           /* read position */
305 >                if (fscanf(syncfp, "%*d %*d %d %d", xp, yp) < 2) {
306                          *xp = hmult-1;
307                          *yp = vmult;
308                  }
309 +                if (rvrlim == 0)                /* initialize recovery limit */
310 +                        rvrlim = *xp*vmult + *yp;
311 +                if (rvrpiece(xp, yp)) {         /* do stragglers first */
312 +                        sflock(F_UNLCK);
313 +                        return(1);
314 +                }
315                  if (--(*yp) < 0) {              /* decrement position */
316                          *yp = vmult-1;
317 <                        if (--(*xp) < 0) {      /* all done! */
318 <                                close(syncfd);
317 >                        if (--(*xp) < 0) {      /* all done */
318 >                                sflock(F_UNLCK);
319                                  return(0);
320                          }
321                  }
322 <                sprintf(buf, "%4d %4d\n%4d %4d\n", hmult, vmult, *xp, *yp);
323 <                lseek(syncfd, 0L, 0);           /* write new position */
324 <                write(syncfd, buf, strlen(buf));
325 <                fls.l_type = F_UNLCK;           /* release sync file */
297 <                fcntl(syncfd, F_SETLKW, &fls);
322 >                fseek(syncfp, 0L, 0);           /* write new position */
323 >                fprintf(syncfp, "%4d %4d\n%4d %4d\n\n", hmult, vmult, *xp, *yp);
324 >                fflush(syncfp);
325 >                sflock(F_UNLCK);                /* release sync file */
326                  return(1);
327          }
328 <        if (fgets(buf, sizeof(buf), stdin) == NULL)     /* use stdin */
301 <                return(0);
302 <        if (sscanf(buf, "%d %d", xp, yp) == 2)
303 <                return(1);
304 <        fprintf(stderr, "%s: input format error\n", progname);
305 <        exit(cleanup(1));
328 >        return(scanf("%d %d", xp, yp) == 2);    /* use stdin */
329   }
330  
331  
332   int
333 + rvrpiece(xp, yp)                /* check for recoverable pieces */
334 + register int  *xp, *yp;
335 + {
336 +        static char  *pdone = NULL;     /* which pieces are done */
337 +        static long  readpos = -1;      /* how far we've read */
338 +        register int  i;
339 +        /*
340 +         * This routine is called by nextpiece() with an
341 +         * exclusive lock on syncfp and the file pointer at the
342 +         * appropriate position to read in the finished pieces.
343 +         */
344 +        if (rvrlim < 0)
345 +                return(0);              /* only check if asked */
346 +        if (pdone == NULL)              /* first call */
347 +                pdone = calloc(hmult*vmult, sizeof(char));
348 +        if (pdone == NULL) {
349 +                fprintf(stderr, "%s: out of memory\n", progname);
350 +                exit(1);
351 +        }
352 +        if (readpos != -1)              /* mark what's been done */
353 +                fseek(syncfp, readpos, 0);
354 +        while (fscanf(syncfp, "%d %d", xp, yp) == 2)
355 +                pdone[*xp*vmult+*yp] = 1;
356 +        if (!feof(syncfp)) {
357 +                fprintf(stderr, "%s: format error in sync file\n", progname);
358 +                exit(1);
359 +        }
360 +        readpos = ftell(syncfp);
361 +        i = hmult*vmult;                /* find an unaccounted for piece */
362 +        while (i-- > rvrlim)
363 +                if (!pdone[i]) {
364 +                        *xp = i / vmult;
365 +                        *yp = i % vmult;
366 +                        pdone[i] = 1;   /* consider it done */
367 +                        return(1);
368 +                }
369 +        rvrlim = -1;                    /* nothing left to recover */
370 +        free(pdone);
371 +        pdone = NULL;
372 +        return(0);
373 + }
374 +
375 +
376 + int
377   cleanup(rstat)                  /* close rpict process and clean up */
378   int  rstat;
379   {
# Line 422 | Line 489 | int  xpos, ypos;
489                                  /* lock file section so NFS doesn't mess up */
490          fls.l_whence = 0;
491          fls.l_type = F_WRLCK;
492 <        fcntl(outfd, F_SETLKW, &fls);
492 >        if (fcntl(outfd, F_SETLKW, &fls) < 0)
493 >                filerr("lock");
494   #endif
495                                  /* write new piece to file */
496          if (lseek(outfd, fls.l_start, 0) == -1)
497 <                goto seekerr;
497 >                filerr("seek");
498          if (hmult == 1) {
499                  if (writebuf(outfd, (char *)pbuf,
500                                  vr*hr*sizeof(COLR)) != vr*hr*sizeof(COLR))
501 <                        goto writerr;
501 >                        filerr("write");
502          } else
503                  for (y = 0; y < vr; y++) {
504                          if (writebuf(outfd, (char *)(pbuf+y*hr),
505                                          hr*sizeof(COLR)) != hr*sizeof(COLR))
506 <                                goto writerr;
506 >                                filerr("write");
507                          if (y < vr-1 && lseek(outfd,
508                                          (long)(hmult-1)*hr*sizeof(COLR),
509                                          1) == -1)
510 <                                goto seekerr;
510 >                                filerr("seek");
511                  }
512 + #if NFS
513 +        fls.l_type = F_UNLCK;           /* release lock */
514 +        if (fcntl(outfd, F_SETLKW, &fls) < 0)
515 +                filerr("lock");
516 + #endif
517 +        if (syncfp != NULL) {                   /* record what's been done */
518 +                sflock(F_WRLCK);
519 +                fseek(syncfp, 0L, 2);           /* append index */
520 +                fprintf(syncfp, "%4d %4d\n", xpos, ypos);
521 +                fflush(syncfp);
522 +                                /*** Unlock not necessary, since
523 +                sflock(F_UNLCK);        _exit() or nextpiece() is next ***/
524 +        }
525          if (verbose) {                          /* notify caller */
526                  printf("%d %d done\n", xpos, ypos);
527                  fflush(stdout);
528          }
529 <        if (pid == -1) {        /* didn't fork or fork failed */
449 < #if NFS
450 <                fls.l_type = F_UNLCK;           /* release lock */
451 <                fcntl(outfd, F_SETLKW, &fls);
452 < #endif
529 >        if (pid == -1)          /* didn't fork or fork failed */
530                  return(0);
531 <        }
532 <        _exit(0);               /* else exit child process (releasing lock) */
533 < seekerr:
534 <        fprintf(stderr, "%s: seek error on file \"%s\"\n", progname, outfile);
535 <        _exit(1);
536 < writerr:
537 <        fprintf(stderr, "%s: write error on file \"%s\"\n", progname, outfile);
531 >        _exit(0);               /* else exit child process (releasing locks) */
532 > }
533 >
534 >
535 > filerr(t)                       /* report file error and exit */
536 > char  *t;
537 > {
538 >        extern char  *sys_errlist[];
539 >
540 >        fprintf(stderr, "%s: %s error on file \"%s\": %s\n",
541 >                        progname, t, outfile, sys_errlist[errno]);
542          _exit(1);
543   }
544  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines