SHARE
TWEET

Untitled

a guest Jun 16th, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. struct mylock {
  2.     pthread_mutex_t entry;
  3.     pthread_mutex_t manager;
  4.     pthread_mutex_t core;
  5.     int counter;
  6.     int state;
  7. }
  8.  
  9. void mylock_acquire(struct mylock *lock, int state)
  10. {
  11.     assert(state != 0);
  12.     pthread_mutex_lock(&lock->entry);
  13.     pthread_mutex_lock(&lock->manager);
  14.     if (lock->state != state) {
  15.         pthread_mutex_unlock(&lock->manager);
  16.         pthread_mutex_lock(&lock->core);
  17.         pthread_mutex_lock(&lock->manager);
  18.         lock->state = state;
  19.     }
  20.     ++lock->counter;
  21.     pthread_mutex_unlock(&lock->manager);
  22.     pthread_mutex_unlock(&lock->entry);
  23. }
  24.  
  25. void mylock_release(struct mylock *lock)
  26. {
  27.     pthread_mutex_lock(&lock->manager);
  28.     if (--lock->counter == 0) {
  29.         lock->state = 0;
  30.         pthread_mutex_unlock(&lock->core);
  31.     }
  32.     pthread_mutex_unlock(&lock->manager);
  33. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top