src/raytracer.cc
branchpyrit
changeset 6 d8d596d26f25
parent 5 2d97ea5e711a
child 8 e6567b740c5e
equal deleted inserted replaced
5:2d97ea5e711a 6:d8d596d26f25
   203 		d->vx += d->dx;
   203 		d->vx += d->dx;
   204 	}
   204 	}
   205 #ifdef PTHREADS
   205 #ifdef PTHREADS
   206 	pthread_exit((void *)d);
   206 	pthread_exit((void *)d);
   207 #endif
   207 #endif
       
   208 	return (void *)d;
   208 }
   209 }
   209 
   210 
   210 float *Raytracer::render(int w, int h)
   211 float *Raytracer::render(int w, int h)
   211 {
   212 {
   212 	float *data;
   213 	float *data;
   227 
   228 
   228 	// for each pixel...
   229 	// for each pixel...
   229 	vy = starty;
   230 	vy = starty;
   230 
   231 
   231 #ifdef PTHREADS
   232 #ifdef PTHREADS
   232 	int num_threads = 20;
   233 	int num_threads = 2;
   233 	printf("* pthreads enabled, using %d threads\n", num_threads);
   234 	printf("* pthreads enabled, using %d threads\n", num_threads);
   234 	pthread_t threads[num_threads];
   235 	pthread_t threads[num_threads];
   235 	for (int t = 0; t < num_threads; t++)
   236 	for (int t = 0; t < num_threads; t++)
   236 		threads[t] = 0;
   237 		threads[t] = pthread_self();
   237 	int t = 0;
   238 	int t = 0;
   238 #endif
   239 #endif
   239 
   240 
   240 	printf("* rendering row   0 (  0%% done)");
   241 	printf("* rendering row   0 (  0%% done)");
   241 	for (int y = 0; y < h; y++) {
   242 	for (int y = 0; y < h; y++) {
   258 		/* when 't' owerflows, reset it */
   259 		/* when 't' owerflows, reset it */
   259 		if (t >= num_threads)
   260 		if (t >= num_threads)
   260 			t = 0;
   261 			t = 0;
   261 		/* wait for next thread in fifo queue, so the descriptor can be reused;
   262 		/* wait for next thread in fifo queue, so the descriptor can be reused;
   262 		   this also limits number of running threads */
   263 		   this also limits number of running threads */
   263 		if (threads[t] != 0)
   264 		if (!pthread_equal(threads[t], pthread_self()))
   264 			if (pthread_join(threads[t], (void**)&d) == 0)
   265 			if (pthread_join(threads[t], (void**)&d) == 0)
   265 				free(d);
   266 				free(d);
   266 #else
   267 #else
   267 		renderrow((void *)d);
   268 		renderrow((void *)d);
   268 		free(d);
   269 		free(d);