Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void update_var_all_work_done(t_multithread *multithread)
- {
- int i;
- i = 0;
- while (i < multithread->max && multithread->tab[i].work_done)
- i++;
- if (i == multithread->max)
- multithread->all_work_done = 1;
- }
- void signal_other_raycast_threads(int id, t_multithread *multithread)
- {
- int i;
- i = -1;
- while (++i < multithread->max)
- {
- if (i != id)
- {
- printf("cond_signal to thread %i\n", i);
- pthread_mutex_lock(&multithread->tab[i].mutex);
- pthread_cond_signal(&multithread->tab[i].cond);
- pthread_mutex_unlock(&multithread->tab[i].mutex);
- }
- }
- }
- void wait_or_signal(t_thread_env *e, t_multithread *multithread)
- {
- int id;
- id = e->id;
- if (multithread->all_work_done == 0)
- {
- printf("thread number %i is waiting for other threads\n", id);
- pthread_mutex_lock(&e->mutex);
- while (multithread->all_work_done == 0)
- pthread_cond_wait(&e->cond, &e->mutex);
- pthread_mutex_unlock(&e->mutex);
- printf("ENFIN ! thread number %id woke up from condwait\n", id);
- }
- else if (multithread->all_work_done)
- {
- printf("allworkdone sent by thread %i\n", id);
- copy_screenpixels(e->doom, multithread);
- signal_other_raycast_threads(id, multithread);
- }
- }
- void *routine(void *arg)
- {
- t_thread_env *e;
- t_env *doom;
- t_multithread *multithread;
- e = (t_thread_env *)arg;
- doom = (t_env *)e->doom;
- multithread = (t_multithread *)e->multithread;
- while (!multithread->stop)
- {
- printf("new frame> thread_id = %i, e->work_done = %i\n", e->id, e->work_done);
- pthread_mutex_lock(&e->mutex);
- while (multithread->all_work_done || e->work_done)
- pthread_cond_wait(&e->cond, &e->mutex);
- pthread_mutex_unlock(&e->mutex);
- if (!e->work_done)
- do_raycast(doom, e);
- update_var_all_work_done(multithread);
- wait_or_signal(e, multithread);
- }
- return (0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement