More comments on the ticket lock code

This commit is contained in:
Nils O. Selåsdal
2014-06-25 17:03:14 +02:00
parent dcc38151b1
commit cfb6022d42
2 changed files with 10 additions and 1 deletions
+3 -1
View File
@@ -3,7 +3,9 @@
#include <pthread.h> #include <pthread.h>
/** An fair alternative to a bare pthread_mutex_t to prevent starvation*/ /** An fair alternative to a bare pthread_mutex_t to prevent starvation.
* The members of UCTicketLock must never be accessed directly.
*/
struct UCTicketLock { struct UCTicketLock {
pthread_cond_t cond; pthread_cond_t cond;
pthread_mutex_t mutex; pthread_mutex_t mutex;
+7
View File
@@ -1,6 +1,13 @@
#include <assert.h> #include <assert.h>
#include "ucore/ticket_lock.h" #include "ucore/ticket_lock.h"
//pthread implementation of http://en.wikipedia.org/wiki/Ticket_lock
//The underlying phtread mutex is not held while the lock is acquired.
//This will allow other threads to enqueue itself.
//
//The condition variable is needed to wake up threads waiting to acquire
//the lock instead of letting the system block on the pthread mutex while waiting.
int uc_ticket_lock_init(struct UCTicketLock *lock) int uc_ticket_lock_init(struct UCTicketLock *lock)
{ {
return uc_ticket_lock_init_ex(lock, NULL, NULL); return uc_ticket_lock_init_ex(lock, NULL, NULL);