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

Comparing ray/src/rt/ambient.c (file contents):
Revision 2.15 by greg, Wed Jan 20 10:48:23 1993 UTC vs.
Revision 2.19 by greg, Wed Aug 4 14:22:11 1993 UTC

# Line 35 | Line 35 | double minarad;                /* minimum ambient radius */
35  
36   static AMBTREE  atrunk;         /* our ambient trunk node */
37  
38 static char  *ambfname = NULL;  /* ambient file name */
38   static FILE  *ambfp = NULL;     /* ambient file pointer */
39 + static int  nunflshed = 0;      /* number of unflushed ambient values */
40  
41   #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
42  
# Line 45 | Line 45 | static FILE  *ambfp = NULL;    /* ambient file pointer */
45   #define  newambtree()   (AMBTREE *)calloc(8, sizeof(AMBTREE))
46  
47   extern long  ftell(), lseek();
48 < static int  initambfile(), avsave(), avinsert(), ambsync();
48 > static int  initambfile(), avsave(), avinsert();
49 > #ifdef  F_SETLKW
50 > static  aflock();
51 > #endif
52  
53  
54   setambres(ar)                           /* set ambient resolution */
# Line 73 | Line 76 | char  *afile;
76          AMBVAL  amb;
77                                                  /* init ambient limits */
78          setambres(ambres);
79 +        if (afile == NULL)
80 +                return;
81                                                  /* open ambient file */
82 <        if ((ambfname = afile) != NULL)
83 <                if ((ambfp = fopen(afile, "r+")) != NULL) {
84 <                        initambfile(0);
85 <                        headlen = ftell(ambfp);
86 <                        while (readambval(&amb, ambfp))
87 <                                avinsert(&amb, &atrunk, thescene.cuorg,
88 <                                                thescene.cusize);
89 <                                                        /* align */
90 <                        fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1);
91 <                } else if ((ambfp = fopen(afile, "w+")) != NULL)
92 <                        initambfile(1);
93 <                else {
94 <                        sprintf(errmsg, "cannot open ambient file \"%s\"",
95 <                                        afile);
96 <                        error(SYSTEM, errmsg);
97 <                }
82 >        if ((ambfp = fopen(afile, "r+")) != NULL) {
83 >                initambfile(0);
84 >                headlen = ftell(ambfp);
85 >                while (readambval(&amb, ambfp))
86 >                        avinsert(&amb, &atrunk, thescene.cuorg,
87 >                                        thescene.cusize);
88 >                                                /* align */
89 >                fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1);
90 >        } else if ((ambfp = fopen(afile, "w+")) != NULL)
91 >                initambfile(1);
92 >        else {
93 >                sprintf(errmsg, "cannot open ambient file \"%s\"", afile);
94 >                error(SYSTEM, errmsg);
95 >        }
96 >        nunflshed++;    /* lie */
97 >        ambsync();
98   }
99  
100  
# Line 305 | Line 310 | int  creat;
310   {
311          extern char  *progname, *octname, VersionID[];
312  
313 + #ifdef  F_SETLKW
314 +        aflock(creat ? F_WRLCK : F_RDLCK);
315 + #endif
316   #ifdef MSDOS
317          setmode(fileno(ambfp), O_BINARY);
318   #endif
# Line 323 | Line 331 | int  creat;
331                  putambmagic(ambfp);
332          } else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp))
333                  error(USER, "bad ambient file");
326        ambsync();
334   }
335  
336  
# Line 331 | Line 338 | static
338   avsave(av)                              /* insert and save an ambient value */
339   AMBVAL  *av;
340   {
334        static int  nunflshed = 0;
335
341          avinsert(av, &atrunk, thescene.cuorg, thescene.cusize);
342          if (ambfp == NULL)
343                  return;
344          if (writambval(av, ambfp) < 0)
345                  goto writerr;
346 <        if (++nunflshed >= AMBFLUSH) {
346 >        if (++nunflshed >= AMBFLUSH)
347                  if (ambsync() == EOF)
348                          goto writerr;
344                nunflshed = 0;
345        }
349          return;
350   writerr:
351          error(SYSTEM, "error writing ambient file");
# Line 386 | Line 389 | memerr:
389   }
390  
391  
392 < #ifdef  NIX
392 > #ifdef  F_SETLKW
393  
394   static
395 < ambsync()                       /* flush ambient file */
395 > aflock(typ)                     /* lock/unlock ambient file */
396 > int  typ;
397   {
398 <        return(fflush(ambfp));
398 >        static struct flock  fls;       /* static so initialized to zeroes */
399 >
400 >        fls.l_type = typ;
401 >        if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0)
402 >                error(SYSTEM, "cannot (un)lock ambient file");
403   }
404  
397 #else
405  
406 < static
406 > int
407   ambsync()                       /* synchronize ambient file */
408   {
409          static FILE  *ambinp = NULL;
410          static long  lastpos = -1;
404        struct flock  fls;
411          long  flen;
412          AMBVAL  avs;
413          register int  n;
414 <                                /* gain exclusive access */
415 <        fls.l_type = F_WRLCK;
416 <        fls.l_whence = 0;
417 <        fls.l_start = 0L;
412 <        fls.l_len = 0L;
413 <        if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0)
414 <                error(SYSTEM, "cannot lock ambient file");
415 <        if (lastpos < 0)        /* initializing */
414 >
415 >        if (nunflshed == 0)
416 >                return(0);
417 >        if (lastpos < 0)        /* initializing (locked in initambfile) */
418                  goto syncend;
419 +                                /* gain exclusive access */
420 +        aflock(F_WRLCK);
421                                  /* see if file has grown */
422          if ((flen = lseek(fileno(ambfp), 0L, 2)) < 0)
423                  error(SYSTEM, "cannot seek on ambient file");
424          if (n = flen - lastpos) {               /* file has grown */
425 <                if (ambinp == NULL) {
426 <                        ambinp = fopen(ambfname, "r");
425 >                if (ambinp == NULL) {           /* use duplicate filedes */
426 >                        ambinp = fdopen(dup(fileno(ambfp)), "r");
427                          if (ambinp == NULL)
428 <                                error(SYSTEM, "fopen failed in ambsync");
428 >                                error(SYSTEM, "fdopen failed in ambsync");
429                  }
430                  if (fseek(ambinp, lastpos, 0) < 0)
431                          error(SYSTEM, "fseek failed in ambsync");
# Line 436 | Line 440 | ambsync()                      /* synchronize ambient file */
440   syncend:
441          n = fflush(ambfp);                      /* calls write() at last */
442          lastpos = lseek(fileno(ambfp), 0L, 1);
443 <        fls.l_type = F_UNLCK;                   /* release file */
444 <        fcntl(fileno(ambfp), F_SETLKW, &fls);
443 >        aflock(F_UNLCK);                        /* release file */
444 >        nunflshed = 0;
445          return(n);
446 + }
447 +
448 + #else
449 +
450 + int
451 + ambsync()                       /* flush ambient file */
452 + {
453 +        if (nunflshed == 0)
454 +                return(0);
455 +        nunflshed = 0;
456 +        return(fflush(ambfp));
457   }
458  
459   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines