57 lines
1.4 KiB
C
57 lines
1.4 KiB
C
struct RecMutex {
|
|
pthread_mutex_t mutex;
|
|
pthread_cond_t locked;
|
|
int lock;
|
|
int owner;
|
|
};
|
|
|
|
int rec_mutex_lock(RecMutex *rmutex) {
|
|
|
|
//CurrentThread is owner, increment lock
|
|
if (rmutex->owner == CurrentThread()) {
|
|
rmutex->lock++;
|
|
return 0;
|
|
} else {
|
|
//Lock the mutex
|
|
pthread_mutex_lock(&(rmutex->mutex));
|
|
|
|
//Wait until the lock is free
|
|
while (rmutex->lock != 0) {
|
|
pthread_cond_wait( &(rmutex->locked), &(rmutex->mutex) );
|
|
}
|
|
|
|
//Set structure paramaters
|
|
rmutex->owner = CurrentThread();
|
|
rmutex->lock = 1;
|
|
pthread_mutex_unlock(&(rmutex->mutex));
|
|
|
|
return 0; //Everything is A-OK
|
|
}
|
|
}
|
|
|
|
int rec_mutex_unlock(RecMutex *rmutex) {
|
|
if (rmutex->owner == CurrentThread()) {
|
|
rmutex->lock--;
|
|
} else {
|
|
return -2; //called without reason = error
|
|
}
|
|
|
|
if (rmutex->lock == 0) {
|
|
rmutex->owner = -1;
|
|
pthread_cond_signal(&(rmutex->locked));
|
|
} else if (rmutex->lock < 0) {
|
|
return -1; //lock is broken = error
|
|
}
|
|
|
|
return 0; //everything is A-OK
|
|
}
|
|
|
|
int rec_mutex_init(RecMutex *rmutex) {
|
|
pthread_mutex_init(&(rmutex->mutex), NULL);
|
|
pthread_cond_init(&(rmutex->locked),NULL);
|
|
rmutex->lock = 0;
|
|
rmutex->owner = -1;
|
|
|
|
return 0;
|
|
}
|