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.107 by greg, Sat Feb 16 00:09:53 2019 UTC vs.
Revision 2.112 by greg, Mon Mar 7 17:52:14 2022 UTC

# Line 37 | Line 37 | static AMBTREE atrunk;         /* our ambient trunk node */
37   static FILE  *ambfp = NULL;     /* ambient file pointer */
38   static int  nunflshed = 0;      /* number of unflushed ambient values */
39  
40 #ifndef SORT_THRESH
41 #ifdef SMLMEM
42 #define SORT_THRESH     ((16L<<20)/sizeof(AMBVAL))
43 #else
44 #define SORT_THRESH     ((64L<<20)/sizeof(AMBVAL))
45 #endif
46 #endif
47 #ifndef SORT_INTVL
48 #define SORT_INTVL      (SORT_THRESH<<1)
49 #endif
50 #ifndef MAX_SORT_INTVL
51 #define MAX_SORT_INTVL  (SORT_INTVL<<6)
52 #endif
53
54
40   static double  avsum = 0.;              /* computed ambient value sum (log) */
41   static unsigned int  navsum = 0;        /* number of values in avsum */
42   static unsigned int  nambvals = 0;      /* total number of indirect values */
43   static unsigned int  nambshare = 0;     /* number of values from file */
59 static unsigned long  ambclock = 0;     /* ambient access clock */
60 static unsigned long  lastsort = 0;     /* time of last value sort */
61 static long  sortintvl = SORT_INTVL;    /* time until next sort */
44   static FILE  *ambinp = NULL;            /* auxiliary file for input */
45   static long  lastpos = -1;              /* last flush position */
46  
65 #define MAXACLOCK       (1L<<30)        /* clock turnover value */
66        /*
67         * Track access times unless we are sharing ambient values
68         * through memory on a multiprocessor, when we want to avoid
69         * claiming our own memory (copy on write).  Go ahead anyway
70         * if more than two thirds of our values are unshared.
71         * Compile with -Dtracktime=0 to turn this code off.
72         */
73 #ifndef tracktime
74 #define tracktime       (shm_boundary == NULL || nambvals > 3*nambshare)
75 #endif
76
47   #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
48  
49   #define  newambval()    (AMBVAL *)malloc(sizeof(AMBVAL))
50  
51 + #define  tfunc(lwr, x, upr)     (((x)-(lwr))/((upr)-(lwr)))
52 +
53   static void initambfile(int creat);
54   static void avsave(AMBVAL *av);
55   static AMBVAL *avstore(AMBVAL  *aval);
# Line 86 | Line 58 | static void freeambtree(AMBTREE  *atp);
58  
59   typedef void unloadtf_t(AMBVAL *);
60   static unloadtf_t avinsert;
89 static unloadtf_t av2list;
61   static unloadtf_t avfree;
62   static void unloadatree(AMBTREE  *at, unloadtf_t *f);
63  
64 < static int aposcmp(const void *avp1, const void *avp2);
94 < static int avlmemi(AMBVAL *avaddr);
95 < static void sortambvals(int always);
64 > static void sortambvals(void);
65  
66 + static int      plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang);
67 + static double   sumambient(COLOR acol, RAY *r, FVECT rn, int al,
68 +                                AMBTREE *at, FVECT c0, double s);
69 + static int      makeambient(COLOR acol, RAY *r, FVECT rn, int al);
70 + static int      extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv,
71 +                                FVECT uvw[3]);
72 +
73   #ifdef  F_SETLKW
74   static void aflock(int  typ);
75   #endif
# Line 133 | Line 109 | setambacc(                             /* set ambient accuracy */
109          if (fabs(newa - olda) >= .05*(newa + olda)) {
110                  ambacc = newa;
111                  if (ambacc > FTINY && nambvals > 0)
112 <                        sortambvals(1);         /* rebuild tree */
112 >                        sortambvals();          /* rebuild tree */
113          }
114   }
115  
# Line 220 | Line 196 | ambdone(void)                  /* close ambient file and free memory
196          navsum = 0;
197          nambvals = 0;
198          nambshare = 0;
223        ambclock = 0;
224        lastsort = 0;
225        sortintvl = SORT_INTVL;
199   }
200  
201  
# Line 255 | Line 228 | ambnotify(                     /* record new modifier */
228                  }
229   }
230  
258 /************ THE FOLLOWING ROUTINES DIFFER BETWEEN NEW & OLD ***************/
231  
260 #ifndef OLDAMB
261
262 #define tfunc(lwr, x, upr)      (((x)-(lwr))/((upr)-(lwr)))
263
264 static int      plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang);
265 static double   sumambient(COLOR acol, RAY *r, FVECT rn, int al,
266                                AMBTREE *at, FVECT c0, double s);
267 static int      makeambient(COLOR acol, RAY *r, FVECT rn, int al);
268 static int      extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv,
269                                FVECT uvw[3]);
270
232   void
233   multambient(            /* compute ambient component & multiply by coef. */
234          COLOR  aval,
# Line 275 | Line 236 | multambient(           /* compute ambient component & multiply
236          FVECT  nrm
237   )
238   {
239 +        static double  logAvgAbsorp = 1;
240          static int  rdepth = 0;                 /* ambient recursion */
241          COLOR   acol, caustic;
242          int     i, ok;
# Line 286 | Line 248 | multambient(           /* compute ambient component & multiply
248          if (ambPmap(aval, r, rdepth))
249                  return;
250  
251 +        if (logAvgAbsorp > 0)                   /* exclude in -aw to avoid growth */
252 +                logAvgAbsorp = log(1.-AVGREFL);
253 +
254          /* PMAP: Factor in specular-diffuse ambient (caustics) from photon
255           * map, if enabled and ray is primary, else caustic is zero.  Continue
256           * with RADIANCE ambient calculation */
# Line 330 | Line 295 | multambient(           /* compute ambient component & multiply
295                  addcolor(aval, caustic);
296                  return;
297          }
333
334        if (tracktime)                          /* sort to minimize thrashing */
335                sortambvals(0);
298                                                  /* interpolate ambient value */
299          setcolor(acol, 0.0, 0.0, 0.0);
300          d = sumambient(acol, r, nrm, rdepth,
# Line 371 | Line 333 | dumbamb:                                       /* return global value */
333          
334          l = bright(ambval);                     /* average in computations */  
335          if (l > FTINY) {
336 <                d = (log(l)*(double)ambvwt + avsum) /
336 >                d = (log(l)*(double)ambvwt + avsum + logAvgAbsorp*navsum) /
337                                  (double)(ambvwt + navsum);
338                  d = exp(d) / l;
339                  scalecolor(aval, d);
340                  multcolor(aval, ambval);        /* apply color of ambval */
341          } else {
342 <                d = exp( avsum / (double)navsum );
342 >                d = exp( avsum/(double)navsum + logAvgAbsorp );
343                  scalecolor(aval, d);            /* neutral color */
344          }
345   }
# Line 475 | Line 437 | sumambient(            /* get interpolated ambient value */
437                  double  u, v, d, delta_r2, delta_t2;
438                  COLOR   ct;
439                  FVECT   uvw[3];
478                                        /* record access */
479                if (tracktime)
480                        av->latick = ambclock;
440                  /*
441                   *  Ambient level test
442                   */
# Line 586 | Line 545 | extambient(            /* extrapolate value at pv, nv */
545   )
546   {
547          const double    min_d = 0.05;
548 +        const double    max_d = 20.;
549          static FVECT    my_uvw[3];
550          FVECT           v1;
551          int             i;
# Line 605 | Line 565 | extambient(            /* extrapolate value at pv, nv */
565          for (i = 3; i--; )
566                  d += v1[i] * (ap->gdir[0]*uvw[0][i] + ap->gdir[1]*uvw[1][i]);
567          
568 <        if (d < min_d)                  /* should not use if we can avoid it */
568 >        if (d < min_d)                  /* clamp min/max scaling */
569                  d = min_d;
570 +        else if (d > max_d)
571 +                d = max_d;
572          copycolor(cr, ap->val);
573          scalecolor(cr, d);
574          return(d > min_d);
# Line 656 | Line 618 | avinsert(                              /* insert ambient value in our tree */
618   }
619  
620  
659 #else /* ! NEWAMB */
660
661 static double   sumambient(COLOR acol, RAY *r, FVECT rn, int al,
662                                AMBTREE *at, FVECT c0, double s);
663 static double   makeambient(COLOR acol, RAY *r, FVECT rn, int al);
664 static void     extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv);
665
666
667 void
668 multambient(            /* compute ambient component & multiply by coef. */
669        COLOR  aval,
670        RAY  *r,
671        FVECT  nrm
672 )
673 {
674        static int  rdepth = 0;                 /* ambient recursion */
675        COLOR   acol, caustic;
676        double  d, l;
677
678        /* PMAP: Factor in ambient from global photon map (if enabled) and return
679         * as all ambient components accounted for */
680        if (ambPmap(aval, r, rdepth))
681                return;
682
683        /* PMAP: Otherwise factor in ambient from caustic photon map
684         * (ambPmapCaustic() returns zero if caustic photons disabled) and
685         * continue with RADIANCE ambient calculation */
686        copycolor(caustic, aval);
687        ambPmapCaustic(caustic, r, rdepth);
688        
689        if (ambdiv <= 0)                        /* no ambient calculation */
690                goto dumbamb;
691                                                /* check number of bounces */
692        if (rdepth >= ambounce)
693                goto dumbamb;
694                                                /* check ambient list */
695        if (ambincl != -1 && r->ro != NULL &&
696                        ambincl != inset(ambset, r->ro->omod))
697                goto dumbamb;
698
699        if (ambacc <= FTINY) {                  /* no ambient storage */
700                copycolor(acol, aval);
701                rdepth++;
702                d = doambient(acol, r, r->rweight, NULL, NULL);
703                rdepth--;
704                if (d <= FTINY)
705                        goto dumbamb;
706                copycolor(aval, acol);          
707        
708           /* PMAP: add in caustic */
709                addcolor(aval, caustic);        
710                return;
711        }
712
713        if (tracktime)                          /* sort to minimize thrashing */
714                sortambvals(0);
715                                                /* interpolate ambient value */
716        setcolor(acol, 0.0, 0.0, 0.0);
717        d = sumambient(acol, r, nrm, rdepth,
718                        &atrunk, thescene.cuorg, thescene.cusize);
719                        
720        if (d > FTINY) {
721                d = 1.0/d;
722                scalecolor(acol, d);
723                multcolor(aval, acol);
724                
725                /* PMAP: add in caustic */
726                addcolor(aval, caustic);        
727                return;
728        }
729        
730        rdepth++;                               /* need to cache new value */
731        d = makeambient(acol, r, nrm, rdepth-1);
732        rdepth--;
733        
734        if (d > FTINY) {
735                multcolor(aval, acol);          /* got new value */
736
737                /* PMAP: add in caustic */
738                addcolor(aval, caustic);                        
739                return;
740        }
741        
742 dumbamb:                                        /* return global value */
743        if ((ambvwt <= 0) | (navsum == 0)) {
744                multcolor(aval, ambval);
745
746                /* PMAP: add in caustic */
747                addcolor(aval, caustic);        
748                return;
749        }
750        
751        l = bright(ambval);                     /* average in computations */
752        if (l > FTINY) {
753                d = (log(l)*(double)ambvwt + avsum) /
754                                (double)(ambvwt + navsum);
755                d = exp(d) / l;
756                scalecolor(aval, d);
757                multcolor(aval, ambval);        /* apply color of ambval */
758        } else {
759                d = exp( avsum / (double)navsum );
760                scalecolor(aval, d);            /* neutral color */
761        }
762 }
763
764
765 static double
766 sumambient(     /* get interpolated ambient value */
767        COLOR  acol,
768        RAY  *r,
769        FVECT  rn,
770        int  al,
771        AMBTREE  *at,
772        FVECT  c0,
773        double  s
774 )
775 {
776        double  d, e1, e2, wt, wsum;
777        COLOR  ct;
778        FVECT  ck0;
779        int  i;
780        int  j;
781        AMBVAL   *av;
782
783        wsum = 0.0;
784                                        /* do this node */
785        for (av = at->alist; av != NULL; av = av->next) {
786                double  rn_dot = -2.0;
787                if (tracktime)
788                        av->latick = ambclock;
789                /*
790                 *  Ambient level test.
791                 */
792                if (av->lvl > al ||     /* list sorted, so this works */
793                                (av->lvl == al) & (av->weight < 0.9*r->rweight))
794                        break;
795                /*
796                 *  Ambient radius test.
797                 */
798                VSUB(ck0, av->pos, r->rop);
799                e1 = DOT(ck0, ck0) / (av->rad * av->rad);
800                if (e1 > ambacc*ambacc*1.21)
801                        continue;
802                /*
803                 *  Direction test using closest normal.
804                 */
805                d = DOT(av->dir, r->ron);
806                if (rn != r->ron) {
807                        rn_dot = DOT(av->dir, rn);
808                        if (rn_dot > 1.0-FTINY)
809                                rn_dot = 1.0-FTINY;
810                        if (rn_dot >= d-FTINY) {
811                                d = rn_dot;
812                                rn_dot = -2.0;
813                        }
814                }
815                e2 = (1.0 - d) * r->rweight;
816                if (e2 < 0.0)
817                        e2 = 0.0;
818                else if (e1 + e2 > ambacc*ambacc*1.21)
819                        continue;
820                /*
821                 *  Ray behind test.
822                 */
823                d = 0.0;
824                for (j = 0; j < 3; j++)
825                        d += (r->rop[j] - av->pos[j]) *
826                                        (av->dir[j] + r->ron[j]);
827                if (d*0.5 < -minarad*ambacc-.001)
828                        continue;
829                /*
830                 *  Jittering final test reduces image artifacts.
831                 */
832                e1 = sqrt(e1);
833                e2 = sqrt(e2);
834                wt = e1 + e2;
835                if (wt > ambacc*(.9+.2*urand(9015+samplendx)))
836                        continue;
837                /*
838                 *  Recompute directional error using perturbed normal
839                 */
840                if (rn_dot > 0.0) {
841                        e2 = sqrt((1.0 - rn_dot)*r->rweight);
842                        wt = e1 + e2;
843                }
844                if (wt <= 1e-3)
845                        wt = 1e3;
846                else
847                        wt = 1.0 / wt;
848                wsum += wt;
849                extambient(ct, av, r->rop, rn);
850                scalecolor(ct, wt);
851                addcolor(acol, ct);
852        }
853        if (at->kid == NULL)
854                return(wsum);
855                                        /* do children */
856        s *= 0.5;
857        for (i = 0; i < 8; i++) {
858                for (j = 0; j < 3; j++) {
859                        ck0[j] = c0[j];
860                        if (1<<j & i)
861                                ck0[j] += s;
862                        if (r->rop[j] < ck0[j] - OCTSCALE*s)
863                                break;
864                        if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s)
865                                break;
866                }
867                if (j == 3)
868                        wsum += sumambient(acol, r, rn, al,
869                                                at->kid+i, ck0, s);
870        }
871        return(wsum);
872 }
873
874
875 static double
876 makeambient(            /* make a new ambient value for storage */
877        COLOR  acol,
878        RAY  *r,
879        FVECT  rn,
880        int  al
881 )
882 {
883        AMBVAL  amb;
884        FVECT   gp, gd;
885        int     i;
886
887        amb.weight = 1.0;                       /* compute weight */
888        for (i = al; i-- > 0; )
889                amb.weight *= AVGREFL;
890        if (r->rweight < 0.1*amb.weight)        /* heuristic override */
891                amb.weight = 1.25*r->rweight;
892        setcolor(acol, AVGREFL, AVGREFL, AVGREFL);
893                                                /* compute ambient */
894        amb.rad = doambient(acol, r, amb.weight, gp, gd);
895        if (amb.rad <= FTINY) {
896                setcolor(acol, 0.0, 0.0, 0.0);
897                return(0.0);
898        }
899        scalecolor(acol, 1./AVGREFL);           /* undo assumed reflectance */
900                                                /* store value */
901        VCOPY(amb.pos, r->rop);
902        VCOPY(amb.dir, r->ron);
903        amb.lvl = al;
904        copycolor(amb.val, acol);
905        VCOPY(amb.gpos, gp);
906        VCOPY(amb.gdir, gd);
907                                                /* insert into tree */
908        avsave(&amb);                           /* and save to file */
909        if (rn != r->ron)
910                extambient(acol, &amb, r->rop, rn);     /* texture */
911        return(amb.rad);
912 }
913
914
621   static void
916 extambient(             /* extrapolate value at pv, nv */
917        COLOR  cr,
918        AMBVAL   *ap,
919        FVECT  pv,
920        FVECT  nv
921 )
922 {
923        FVECT  v1;
924        int  i;
925        double  d;
926
927        d = 1.0;                        /* zeroeth order */
928                                        /* gradient due to translation */
929        for (i = 0; i < 3; i++)
930                d += ap->gpos[i]*(pv[i]-ap->pos[i]);
931                                        /* gradient due to rotation */
932        VCROSS(v1, ap->dir, nv);
933        d += DOT(ap->gdir, v1);
934        if (d <= 0.0) {
935                setcolor(cr, 0.0, 0.0, 0.0);
936                return;
937        }
938        copycolor(cr, ap->val);
939        scalecolor(cr, d);
940 }
941
942
943 static void
944 avinsert(                               /* insert ambient value in our tree */
945        AMBVAL *av
946 )
947 {
948        AMBTREE  *at;
949        AMBVAL  *ap;
950        AMBVAL  avh;
951        FVECT  ck0;
952        double  s;
953        int  branch;
954        int  i;
955
956        if (av->rad <= FTINY)
957                error(CONSISTENCY, "zero ambient radius in avinsert");
958        at = &atrunk;
959        VCOPY(ck0, thescene.cuorg);
960        s = thescene.cusize;
961        while (s*(OCTSCALE/2) > av->rad*ambacc) {
962                if (at->kid == NULL)
963                        if ((at->kid = newambtree()) == NULL)
964                                error(SYSTEM, "out of memory in avinsert");
965                s *= 0.5;
966                branch = 0;
967                for (i = 0; i < 3; i++)
968                        if (av->pos[i] > ck0[i] + s) {
969                                ck0[i] += s;
970                                branch |= 1 << i;
971                        }
972                at = at->kid + branch;
973        }
974        avh.next = at->alist;           /* order by increasing level */
975        for (ap = &avh; ap->next != NULL; ap = ap->next)
976                if ( ap->next->lvl > av->lvl ||
977                                (ap->next->lvl == av->lvl) &
978                                (ap->next->weight <= av->weight) )
979                        break;
980        av->next = ap->next;
981        ap->next = (AMBVAL*)av;
982        at->alist = avh.next;
983 }
984
985 #endif  /* ! NEWAMB */
986
987 /************* FOLLOWING ROUTINES SAME FOR NEW & OLD METHODS ***************/
988
989 static void
622   initambfile(            /* initialize ambient file */
623          int  cre8
624   )
# Line 1052 | Line 684 | avstore(                               /* allocate memory and save aval */
684          if ((av = newambval()) == NULL)
685                  error(SYSTEM, "out of memory in avstore");
686          *av = *aval;
1055        av->latick = ambclock;
687          av->next = NULL;
688          nambvals++;
689          d = bright(av->val);
# Line 1125 | Line 756 | unloadatree(                   /* unload an ambient value tree */
756   }
757  
758  
1128 static struct avl {
1129        AMBVAL  *p;
1130        unsigned long   t;
1131 }       *avlist1;                       /* ambient value list with ticks */
1132 static AMBVAL   **avlist2;              /* memory positions for sorting */
1133 static int      i_avlist;               /* index for lists */
1134
1135 static int alatcmp(const void *av1, const void *av2);
1136
759   static void
760   avfree(AMBVAL *av)
761   {
762          free(av);
763   }
764  
765 +
766   static void
767 < av2list(
1145 <        AMBVAL *av
1146 < )
767 > sortambvals(void)                       /* resort ambient values */
768   {
769 < #ifdef DEBUG
1149 <        if (i_avlist >= nambvals)
1150 <                error(CONSISTENCY, "too many ambient values in av2list1");
1151 < #endif
1152 <        avlist1[i_avlist].p = avlist2[i_avlist] = (AMBVAL*)av;
1153 <        avlist1[i_avlist++].t = av->latick;
1154 < }
769 >        AMBTREE  oldatrunk = atrunk;
770  
771 <
772 < static int
773 < alatcmp(                        /* compare ambient values for MRA */
1159 <        const void *av1,
1160 <        const void *av2
1161 < )
1162 < {
1163 <        long  lc = ((struct avl *)av2)->t - ((struct avl *)av1)->t;
1164 <        return(lc<0 ? -1 : lc>0 ? 1 : 0);
771 >        atrunk.alist = NULL;
772 >        atrunk.kid = NULL;
773 >        unloadatree(&oldatrunk, avinsert);
774   }
775  
776  
1168 /* GW NOTE 2002/10/3:
1169 * I used to compare AMBVAL pointers, but found that this was the
1170 * cause of a serious consistency error with gcc, since the optimizer
1171 * uses some dangerous trick in pointer subtraction that
1172 * assumes pointers differ by exact struct size increments.
1173 */
1174 static int
1175 aposcmp(                        /* compare ambient value positions */
1176        const void      *avp1,
1177        const void      *avp2
1178 )
1179 {
1180        long    diff = *(char * const *)avp1 - *(char * const *)avp2;
1181        if (diff < 0)
1182                return(-1);
1183        return(diff > 0);
1184 }
1185
1186
1187 static int
1188 avlmemi(                                /* find list position from address */
1189        AMBVAL  *avaddr
1190 )
1191 {
1192        AMBVAL  **avlpp;
1193
1194        avlpp = (AMBVAL **)bsearch(&avaddr, avlist2,
1195                        nambvals, sizeof(AMBVAL *), aposcmp);
1196        if (avlpp == NULL)
1197                error(CONSISTENCY, "address not found in avlmemi");
1198        return(avlpp - avlist2);
1199 }
1200
1201
1202 static void
1203 sortambvals(                    /* resort ambient values */
1204        int     always
1205 )
1206 {
1207        AMBTREE  oldatrunk;
1208        AMBVAL  tav, *tap, *pnext;
1209        int     i, j;
1210                                        /* see if it's time yet */
1211        if (!always && (ambclock++ < lastsort+sortintvl ||
1212                        nambvals < SORT_THRESH))
1213                return;
1214        /*
1215         * The idea here is to minimize memory thrashing
1216         * in VM systems by improving reference locality.
1217         * We do this by periodically sorting our stored ambient
1218         * values in memory in order of most recently to least
1219         * recently accessed.  This ordering was chosen so that new
1220         * ambient values (which tend to be less important) go into
1221         * higher memory with the infrequently accessed values.
1222         *      Since we expect our values to need sorting less
1223         * frequently as the process continues, we double our
1224         * waiting interval after each call.
1225         *      This routine is also called by setambacc() with
1226         * the "always" parameter set to 1 so that the ambient
1227         * tree will be rebuilt with the new accuracy parameter.
1228         */
1229        if (tracktime) {                /* allocate pointer arrays to sort */
1230                avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
1231                avlist1 = (struct avl *)malloc(nambvals*sizeof(struct avl));
1232        } else {
1233                avlist2 = NULL;
1234                avlist1 = NULL;
1235        }
1236        if (avlist1 == NULL) {          /* no time tracking -- rebuild tree? */
1237                if (avlist2 != NULL)
1238                        free(avlist2);
1239                if (always) {           /* rebuild without sorting */
1240                        oldatrunk = atrunk;
1241                        atrunk.alist = NULL;
1242                        atrunk.kid = NULL;
1243                        unloadatree(&oldatrunk, avinsert);
1244                }
1245        } else {                        /* sort memory by last access time */
1246                /*
1247                 * Sorting memory is tricky because it isn't contiguous.
1248                 * We have to sort an array of pointers by MRA and also
1249                 * by memory position.  We then copy values in "loops"
1250                 * to minimize memory hits.  Nevertheless, we will visit
1251                 * everyone at least twice, and this is an expensive process
1252                 * when we're thrashing, which is when we need to do it.
1253                 */
1254 #ifdef DEBUG
1255                sprintf(errmsg, "sorting %u ambient values at ambclock=%lu...",
1256                                nambvals, ambclock);
1257                eputs(errmsg);
1258 #endif
1259                i_avlist = 0;
1260                unloadatree(&atrunk, av2list);  /* empty current tree */
1261 #ifdef DEBUG
1262                if (i_avlist < nambvals)
1263                        error(CONSISTENCY, "missing ambient values in sortambvals");
1264 #endif
1265                qsort(avlist1, nambvals, sizeof(struct avl), alatcmp);
1266                qsort(avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
1267                for (i = 0; i < nambvals; i++) {
1268                        if (avlist1[i].p == NULL)
1269                                continue;
1270                        tap = avlist2[i];
1271                        tav = *tap;
1272                        for (j = i; (pnext = avlist1[j].p) != tap;
1273                                        j = avlmemi(pnext)) {
1274                                *(avlist2[j]) = *pnext;
1275                                avinsert(avlist2[j]);
1276                                avlist1[j].p = NULL;
1277                        }
1278                        *(avlist2[j]) = tav;
1279                        avinsert(avlist2[j]);
1280                        avlist1[j].p = NULL;
1281                }
1282                free(avlist1);
1283                free(avlist2);
1284                                                /* compute new sort interval */
1285                sortintvl = ambclock - lastsort;
1286                if (sortintvl >= MAX_SORT_INTVL/2)
1287                        sortintvl = MAX_SORT_INTVL;
1288                else
1289                        sortintvl <<= 1;        /* wait twice as long next */
1290 #ifdef DEBUG
1291                eputs("done\n");
1292 #endif
1293        }
1294        if (ambclock >= MAXACLOCK)
1295                ambclock = MAXACLOCK/2;
1296        lastsort = ambclock;
1297 }
1298
1299
777   #ifdef  F_SETLKW
778  
779   static void
# Line 1308 | Line 785 | aflock(                        /* lock/unlock ambient file */
785  
786          if (typ == fls.l_type)          /* already called? */
787                  return;
788 +
789          fls.l_type = typ;
790 <        if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0)
791 <                error(SYSTEM, "cannot (un)lock ambient file");
790 >        do
791 >                if (fcntl(fileno(ambfp), F_SETLKW, &fls) != -1)
792 >                        return;
793 >        while (errno == EINTR);
794 >        
795 >        error(SYSTEM, "cannot (un)lock ambient file");
796   }
797  
798  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines