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

Comparing ray/src/rt/rayfifo.c (file contents):
Revision 2.2 by greg, Sat Dec 12 19:01:00 2009 UTC vs.
Revision 2.3 by greg, Sat Dec 12 23:08:13 2009 UTC

# Line 41 | Line 41 | static RAY     *r_fifo_buf = NULL;             /* circular FIFO out b
41   static int      r_fifo_len = 0;                 /* allocated FIFO length */
42   static RNUMBER  r_fifo_start = 1;               /* first awaited ray */
43   static RNUMBER  r_fifo_end = 1;                 /* one past FIFO last */
44 + static RNUMBER  r_fifo_next = 1;                /* next ray assignment */
45  
46   #define r_fifo(rn)      (&r_fifo_buf[(rn)&(r_fifo_len-1)])
47  
# Line 79 | Line 80 | ray_fifo_push(         /* send finished ray to output (or que
80  
81          if (ray_fifo_out == NULL)
82                  error(INTERNAL, "ray_fifo_out is NULL");
83 <        if ((r->rno < r_fifo_start) | (r->rno >= r_fifo_end))
84 <                error(INTERNAL, "unexpected ray number in ray_fifo_push");
83 >        if ((r->rno < r_fifo_start) | (r->rno >= r_fifo_next))
84 >                error(INTERNAL, "unexpected ray number in ray_fifo_push()");
85  
85        if (r->rno - r_fifo_start >= r_fifo_len)
86                ray_fifo_growbuf();             /* need more space */
87
86          if (r->rno > r_fifo_start) {            /* insert into output queue */
87 +                if (r->rno - r_fifo_start >= r_fifo_len)
88 +                        ray_fifo_growbuf();     /* need more space */
89                  *r_fifo(r->rno) = *r;
90 +                if (r->rno >= r_fifo_end)
91 +                        r_fifo_end = r->rno + 1;
92                  return(0);
93          }
94                          /* r->rno == r_fifo_start, so transfer ray(s) */
95          do {
96 <                if ((rv = (*ray_fifo_out)(r)) < 0)
96 >                rv = (*ray_fifo_out)(r);
97 >                r->rno = 0;                     /* flag this entry complete */
98 >                if (rv < 0)
99                          return(-1);
100                  nsent += rv;
101                  if (++r_fifo_start < r_fifo_end)
102                          r = r_fifo(r_fifo_start);
103 +                else if (r_fifo_start > r_fifo_end)
104 +                        r_fifo_end = r_fifo_start;
105          } while (r->rno == r_fifo_start);
106  
107          return(nsent);
# Line 107 | Line 113 | ray_fifo_in(           /* add ray to FIFO */
113          RAY     *r
114   )
115   {
116 <        int     rv, rval = 0;
116 >        static int      incall = 0;             /* prevent recursion */
117 >        int             rv, rval = 0;
118  
119 <        if (r_fifo_start >= 1L<<30) {           /* reset our counter */
119 >        if (incall++)
120 >                error(INTERNAL, "recursive call to ray_fifo_in()");
121 >
122 >        if (r_fifo_start >= 1L<<30) {           /* reset our counters */
123                  if ((rv = ray_fifo_flush()) < 0)
124 <                        return(-1);
124 >                        {--incall; return(-1);}
125                  rval += rv;
126          }
127                                                  /* queue ray */
128          rayorigin(r, PRIMARY, NULL, NULL);
129 <        r->rno = r_fifo_end++;
129 >        r->rno = r_fifo_next++;
130          if ((rv = ray_pqueue(r)) < 0)
131 <                return(-1);
131 >                {--incall; return(-1);}
132  
133          if (!rv)                                /* no result this time? */
134 <                return(rval);
134 >                {--incall; return(rval);}
135          
136          do {                                    /* else send/queue result */
137                  if ((rv = ray_fifo_push(r)) < 0)
138 <                        return(-1);
138 >                        {--incall; return(-1);}
139                  rval += rv;
140  
141          } while (ray_presult(r, -1) > 0);       /* empty in-core queue */
142  
143 <        return(rval);
143 >        --incall; return(rval);
144   }
145  
146  
# Line 144 | Line 154 | ray_fifo_flush(void)   /* flush everything and release b
154                          (rv = ray_fifo_push(&myRay)) >= 0)
155                  rval += rv;
156  
157 <        if (rv < 0)                             /* check for error */
157 >        if (rv < 0)                             /* check for exception */
158                  return(-1);
159  
160          if (r_fifo_start != r_fifo_end)
161 <                error(INTERNAL, "could not empty queue in ray_fifo_flush");
161 >                error(INTERNAL, "could not empty queue in ray_fifo_flush()");
162  
163 <        free(r_fifo_buf);
164 <        r_fifo_buf = NULL;
165 <        r_fifo_len = 0;
166 <        r_fifo_end = r_fifo_start = 1;
163 >        if (r_fifo_buf != NULL) {
164 >                free(r_fifo_buf);
165 >                r_fifo_buf = NULL; r_fifo_len = 0;
166 >        }
167 >        r_fifo_next = r_fifo_end = r_fifo_start = 1;
168  
169          return(rval);
170   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines