Advertisement
Guest User

newWait.c

a guest
Dec 12th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.08 KB | None | 0 0
  1. /*
  2. Suspend the current thread
  3. on the condition.
  4. */
  5. int green_cond_wait(green_cond_t* cond, green_mutex_t* mutex) {
  6.     // block timer interrupt
  7.     sigprocmask(SIG_BLOCK, &block, NULL);
  8.     // suspend the running thread on condition
  9.     green_t *susp = running;
  10.     if (cond->end == NULL) {
  11.        cond->front = cond->end = susp;
  12.        return 0;
  13.     }
  14.     cond->end->next = susp;
  15.     cond->end = susp;
  16.  
  17.     if(mutex != NULL) {
  18.         // release the lock if we have a mutex
  19.         mutex->taken = FALSE;
  20.         // schedule suspended threads
  21.         green_t* suspended = mutex->susp;
  22.         while (suspended != NULL) {
  23.             myEnQueue(suspended);
  24.             suspended++;
  25.             //suspended = mutex->susp;
  26.         }
  27.     }
  28.     // schedule the next thread
  29.     struct green_t* next = myDeQueue();
  30.     running = next;
  31.     swapcontext(susp->context, next->context);
  32.  
  33.     if(mutex != NULL) {
  34.         // try to take the lock
  35.         while(mutex->taken) {
  36.             // bad luck, suspend
  37.            
  38.         }
  39.         // take the lock
  40.         mutex->taken = TRUE;
  41.     }
  42.     // unblock
  43.     sigprocmask(SIG_UNBLOCK, &block, NULL);
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement