Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. void
  2. lock_acquire3(struct lock *lockA, struct lock *lockB, struct lock *lockC)
  3. {
  4. // Write this
  5. int splold;
  6. assert(lockA != NULL);
  7. assert(lockB != NULL);
  8. assert(lockC != NULL);
  9. splold = splhigh();
  10.  
  11. while(1)
  12. {
  13.  
  14. // spin while first lock is previously held
  15. while(*(lockA->status) == 1);
  16.  
  17. // status is now 0 (free to acquire)
  18. if (lock_testAndSet(lockA) == 0)
  19. {
  20.  
  21. // try to get 2nd lock
  22. if (lock_testAndSet(lockB) == 0 && lock_testAndSet(lockC) == 0)
  23. {
  24.  
  25. // try to get 3rd lock
  26. if (lock_testAndSet(lockC) == 0)
  27. {
  28. // all locks acquired, exit
  29. break;
  30. }
  31. else
  32. {
  33. // couldn't get all locks, release A and B
  34. lock_release(lockA);
  35. lock_release(lockB);
  36. }
  37. }
  38. else
  39. {
  40. // couldn't get both locks, release first
  41. lock_release(lockA);
  42. }
  43. }
  44. }
  45.  
  46. splx(splold);
  47. // (void)lock; // suppress warning until code gets written
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement