Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct mylock {
- pthread_mutex_t entry;
- pthread_mutex_t manager;
- pthread_mutex_t core;
- int counter;
- int state;
- }
- void mylock_acquire(struct mylock *lock, int state)
- {
- assert(state != 0);
- pthread_mutex_lock(&lock->entry);
- pthread_mutex_lock(&lock->manager);
- if (lock->state != state) {
- pthread_mutex_unlock(&lock->manager);
- pthread_mutex_lock(&lock->core);
- pthread_mutex_lock(&lock->manager);
- lock->state = state;
- }
- ++lock->counter;
- pthread_mutex_unlock(&lock->manager);
- pthread_mutex_unlock(&lock->entry);
- }
- void mylock_release(struct mylock *lock)
- {
- pthread_mutex_lock(&lock->manager);
- if (--lock->counter == 0) {
- lock->state = 0;
- pthread_mutex_unlock(&lock->core);
- }
- pthread_mutex_unlock(&lock->manager);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement