| 27 |
|
* pending ray calculations and frees the FIFO buffer. If any of |
| 28 |
|
* the automatic calls to the ray_fifo_out callback return a |
| 29 |
|
* negative value, processing stops and -1 is returned. |
| 30 |
+ |
* |
| 31 |
+ |
* Note: The ray passed to ray_fifo_in() may be overwritten |
| 32 |
+ |
* arbitrarily, since it is passed to ray_pqueue(). |
| 33 |
|
*/ |
| 34 |
|
|
| 35 |
|
#include "ray.h" |
| 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 |
|
|
| 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 |
|
|
| 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(); |
| 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); |
| 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); |
| 135 |
< |
/* else send/queue result */ |
| 136 |
< |
if ((rv = ray_fifo_push(r)) < 0) |
| 137 |
< |
return(-1); |
| 138 |
< |
rval += rv; |
| 139 |
< |
return(rval); |
| 134 |
> |
{--incall; return(rval);} |
| 135 |
> |
|
| 136 |
> |
do { /* else send/queue result */ |
| 137 |
> |
if ((rv = ray_fifo_push(r)) < 0) |
| 138 |
> |
{--incall; return(-1);} |
| 139 |
> |
rval += rv; |
| 140 |
> |
|
| 141 |
> |
} while (ray_presult(r, -1) > 0); /* empty in-core queue */ |
| 142 |
> |
|
| 143 |
> |
--incall; return(rval); |
| 144 |
|
} |
| 145 |
|
|
| 146 |
|
|
| 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 |
|
} |