From 0a6ce86779bd86b81420b89f552548aedd1c1e4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20O=2E=20Sel=C3=A5sdal?= Date: Wed, 25 Jun 2014 17:03:14 +0200 Subject: [PATCH] More comments on the ticket lock code --- include/ucore/ticket_lock.h | 4 +++- src/ticket_lock.c | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/include/ucore/ticket_lock.h b/include/ucore/ticket_lock.h index abaf3f1..f68c0a2 100644 --- a/include/ucore/ticket_lock.h +++ b/include/ucore/ticket_lock.h @@ -3,7 +3,9 @@ #include -/** 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 { pthread_cond_t cond; pthread_mutex_t mutex; diff --git a/src/ticket_lock.c b/src/ticket_lock.c index b6b03f9..a5d5bfa 100644 --- a/src/ticket_lock.c +++ b/src/ticket_lock.c @@ -1,6 +1,13 @@ #include #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) { return uc_ticket_lock_init_ex(lock, NULL, NULL);