Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

docs: convert kref semaphore to mutex

Just converting this documentation semaphore reference, since we don't
want to promote semaphore usage.

Signed-off-by: Daniel Walker <dwalker@mvista.com>
Acked-by: Corey Minyard <minyard@acm.org>
Cc: Greg KH <greg@kroah.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Daniel Walker and committed by
Linus Torvalds
1373bed3 13f14b4d

+10 -10
+10 -10
Documentation/kref.txt
··· 141 141 instance, you have a list of items that are each kref-ed, and you wish 142 142 to get the first one. You can't just pull the first item off the list 143 143 and kref_get() it. That violates rule 3 because you are not already 144 - holding a valid pointer. You must add locks or semaphores. For 145 - instance: 144 + holding a valid pointer. You must add a mutex (or some other lock). 145 + For instance: 146 146 147 - static DECLARE_MUTEX(sem); 147 + static DEFINE_MUTEX(mutex); 148 148 static LIST_HEAD(q); 149 149 struct my_data 150 150 { ··· 155 155 static struct my_data *get_entry() 156 156 { 157 157 struct my_data *entry = NULL; 158 - down(&sem); 158 + mutex_lock(&mutex); 159 159 if (!list_empty(&q)) { 160 160 entry = container_of(q.next, struct my_q_entry, link); 161 161 kref_get(&entry->refcount); 162 162 } 163 - up(&sem); 163 + mutex_unlock(&mutex); 164 164 return entry; 165 165 } 166 166 ··· 174 174 175 175 static void put_entry(struct my_data *entry) 176 176 { 177 - down(&sem); 177 + mutex_lock(&mutex); 178 178 kref_put(&entry->refcount, release_entry); 179 - up(&sem); 179 + mutex_unlock(&mutex); 180 180 } 181 181 182 182 The kref_put() return value is useful if you do not want to hold the ··· 191 191 192 192 static void put_entry(struct my_data *entry) 193 193 { 194 - down(&sem); 194 + mutex_lock(&mutex); 195 195 if (kref_put(&entry->refcount, release_entry)) { 196 196 list_del(&entry->link); 197 - up(&sem); 197 + mutex_unlock(&mutex); 198 198 kfree(entry); 199 199 } else 200 - up(&sem); 200 + mutex_unlock(&mutex); 201 201 } 202 202 203 203 This is really more useful if you have to call other routines as part