Skip to content

Commit

Permalink
---
Browse files Browse the repository at this point in the history
yaml
---
r: 83476
b: refs/heads/master
c: 1373bed
h: refs/heads/master
v: v3
  • Loading branch information
Daniel Walker authored and Linus Torvalds committed Feb 6, 2008
1 parent 8896440 commit 968b6fe
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion [refs]
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
---
refs/heads/master: 13f14b4d8be225cbb11ff2be7c048590a9ccf87b
refs/heads/master: 1373bed34e30b8632aaf43aa85d72329c83f7077
20 changes: 10 additions & 10 deletions trunk/Documentation/kref.txt
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ The last rule (rule 3) is the nastiest one to handle. Say, for
instance, you have a list of items that are each kref-ed, and you wish
to get the first one. You can't just pull the first item off the list
and kref_get() it. That violates rule 3 because you are not already
holding a valid pointer. You must add locks or semaphores. For
instance:
holding a valid pointer. You must add a mutex (or some other lock).
For instance:

static DECLARE_MUTEX(sem);
static DEFINE_MUTEX(mutex);
static LIST_HEAD(q);
struct my_data
{
Expand All @@ -155,12 +155,12 @@ struct my_data
static struct my_data *get_entry()
{
struct my_data *entry = NULL;
down(&sem);
mutex_lock(&mutex);
if (!list_empty(&q)) {
entry = container_of(q.next, struct my_q_entry, link);
kref_get(&entry->refcount);
}
up(&sem);
mutex_unlock(&mutex);
return entry;
}

Expand All @@ -174,9 +174,9 @@ static void release_entry(struct kref *ref)

static void put_entry(struct my_data *entry)
{
down(&sem);
mutex_lock(&mutex);
kref_put(&entry->refcount, release_entry);
up(&sem);
mutex_unlock(&mutex);
}

The kref_put() return value is useful if you do not want to hold the
Expand All @@ -191,13 +191,13 @@ static void release_entry(struct kref *ref)

static void put_entry(struct my_data *entry)
{
down(&sem);
mutex_lock(&mutex);
if (kref_put(&entry->refcount, release_entry)) {
list_del(&entry->link);
up(&sem);
mutex_unlock(&mutex);
kfree(entry);
} else
up(&sem);
mutex_unlock(&mutex);
}

This is really more useful if you have to call other routines as part
Expand Down

0 comments on commit 968b6fe

Please sign in to comment.