Advertisement
Guest User

Untitled

a guest
Jul 20th, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.82 KB | None | 0 0
  1.  
  2. void    update_var_all_work_done(t_multithread *multithread)
  3. {
  4.     int i;
  5.  
  6.     i = 0;
  7.     while (i < multithread->max && multithread->tab[i].work_done)
  8.         i++;
  9.     if (i == multithread->max)
  10.         multithread->all_work_done = 1;
  11. }
  12.  
  13. void    signal_other_raycast_threads(int id, t_multithread *multithread)
  14. {
  15.     int i;
  16.  
  17.     i = -1;
  18.     while (++i < multithread->max)
  19.     {
  20.         if (i != id)
  21.         {
  22.             printf("cond_signal to thread %i\n", i);
  23.             pthread_mutex_lock(&multithread->tab[i].mutex);
  24.             pthread_cond_signal(&multithread->tab[i].cond);
  25.             pthread_mutex_unlock(&multithread->tab[i].mutex);
  26.         }
  27.     }
  28. }
  29.  
  30. void    wait_or_signal(t_thread_env *e, t_multithread *multithread)
  31. {
  32.     int     id;
  33.    
  34.     id = e->id;
  35.     if (multithread->all_work_done == 0)
  36.     {
  37.         printf("thread number %i is waiting for other threads\n", id);
  38.  
  39.         pthread_mutex_lock(&e->mutex);
  40.         while (multithread->all_work_done == 0)
  41.             pthread_cond_wait(&e->cond, &e->mutex);
  42.         pthread_mutex_unlock(&e->mutex);
  43.  
  44.         printf("ENFIN ! thread number %id woke up from condwait\n", id);
  45.     }
  46.     else if (multithread->all_work_done)
  47.     {
  48.         printf("allworkdone sent by thread %i\n", id);
  49.         copy_screenpixels(e->doom, multithread);
  50.         signal_other_raycast_threads(id, multithread);
  51.     }
  52. }
  53.  
  54. void    *routine(void *arg)
  55. {
  56.     t_thread_env    *e;
  57.     t_env           *doom;
  58.     t_multithread   *multithread;
  59.  
  60.     e = (t_thread_env *)arg;
  61.     doom = (t_env *)e->doom;
  62.     multithread = (t_multithread *)e->multithread;
  63.  
  64.     while (!multithread->stop)
  65.     {
  66.         printf("new frame> thread_id = %i, e->work_done = %i\n", e->id, e->work_done);
  67.  
  68.         pthread_mutex_lock(&e->mutex);
  69.         while (multithread->all_work_done || e->work_done)
  70.             pthread_cond_wait(&e->cond, &e->mutex);
  71.         pthread_mutex_unlock(&e->mutex);
  72.  
  73.         if (!e->work_done)
  74.             do_raycast(doom, e);
  75.  
  76.         update_var_all_work_done(multithread);
  77.         wait_or_signal(e, multithread);
  78.     }
  79.     return (0);
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement