Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Suspend the current thread
- on the condition.
- */
- int green_cond_wait(green_cond_t* cond, green_mutex_t* mutex) {
- // block timer interrupt
- sigprocmask(SIG_BLOCK, &block, NULL);
- // suspend the running thread on condition
- green_t *susp = running;
- if (cond->end == NULL) {
- cond->front = cond->end = susp;
- return 0;
- }
- cond->end->next = susp;
- cond->end = susp;
- if(mutex != NULL) {
- // release the lock if we have a mutex
- mutex->taken = FALSE;
- // schedule suspended threads
- green_t* suspended = mutex->susp;
- while (suspended != NULL) {
- myEnQueue(suspended);
- suspended++;
- //suspended = mutex->susp;
- }
- }
- // schedule the next thread
- struct green_t* next = myDeQueue();
- running = next;
- swapcontext(susp->context, next->context);
- if(mutex != NULL) {
- // try to take the lock
- while(mutex->taken) {
- // bad luck, suspend
- }
- // take the lock
- mutex->taken = TRUE;
- }
- // unblock
- sigprocmask(SIG_UNBLOCK, &block, NULL);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement