Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int green_mutex_lock(green_mutex_t *mutex) {
- // block timer interrupt
- sigprocmask(SIG_BLOCK, &block, NULL);
- green_t *susp = running;
- while(mutex->taken) {
- // suspend the running thread. Should I have a susp list?
- // add susp to susp queue
- if (mutex->susp == NULL){
- mutex->susp = running;
- }
- else {
- green_t* current = mutex->susp;
- while(current->next != NULL){
- current = current->next;
- }
- current->next = running;
- }
- // select the next thread for execution
- struct green_t* next = myDeQueue();
- // find the next thread
- running = next;
- swapcontext(susp->context, next->context);
- }
- // take the lock. This is enough, the caller function will wait until it gets back, and do whatever with the locks.
- mutex->taken = TRUE;
- mutex->susp = running;
- // unblock
- sigprocmask(SIG_UNBLOCK, &block, NULL);
- return 0;
- }
- int green_mutex_unlock(green_mutex_t *mutex) {
- // block timer interrupt
- sigprocmask(SIG_BLOCK, &block, NULL);
- // move suspended threads to ready queue
- while (mutex->susp != NULL){
- myEnQueue(mutex->susp);
- mutex->susp = mutex->susp->next;
- }
- // release lock
- mutex->taken = FALSE;
- // unblock
- sigprocmask(SIG_UNBLOCK, &block, NULL);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement