2006-01-08 17:02:19 +08:00
|
|
|
Refcounter design for elements of lists/arrays protected by RCU.
|
2005-09-10 04:04:09 +08:00
|
|
|
|
|
|
|
Refcounting on elements of lists which are protected by traditional
|
|
|
|
reader/writer spinlocks or semaphores are straight forward as in:
|
|
|
|
|
2006-01-08 17:02:19 +08:00
|
|
|
1. 2.
|
|
|
|
add() search_and_reference()
|
|
|
|
{ {
|
|
|
|
alloc_object read_lock(&list_lock);
|
|
|
|
... search_for_element
|
|
|
|
atomic_set(&el->rc, 1); atomic_inc(&el->rc);
|
|
|
|
write_lock(&list_lock); ...
|
|
|
|
add_element read_unlock(&list_lock);
|
|
|
|
... ...
|
|
|
|
write_unlock(&list_lock); }
|
2005-09-10 04:04:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
3. 4.
|
|
|
|
release_referenced() delete()
|
|
|
|
{ {
|
2006-01-08 17:02:19 +08:00
|
|
|
... write_lock(&list_lock);
|
|
|
|
atomic_dec(&el->rc, relfunc) ...
|
|
|
|
... delete_element
|
|
|
|
} write_unlock(&list_lock);
|
|
|
|
...
|
|
|
|
if (atomic_dec_and_test(&el->rc))
|
|
|
|
kfree(el);
|
|
|
|
...
|
2005-09-10 04:04:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
If this list/array is made lock free using rcu as in changing the
|
|
|
|
write_lock in add() and delete() to spin_lock and changing read_lock
|
2006-01-08 17:02:19 +08:00
|
|
|
in search_and_reference to rcu_read_lock(), the atomic_get in
|
2005-09-10 04:04:09 +08:00
|
|
|
search_and_reference could potentially hold reference to an element which
|
2006-01-08 17:02:19 +08:00
|
|
|
has already been deleted from the list/array. atomic_inc_not_zero takes
|
2005-09-10 04:04:09 +08:00
|
|
|
care of this scenario. search_and_reference should look as;
|
|
|
|
|
|
|
|
1. 2.
|
|
|
|
add() search_and_reference()
|
|
|
|
{ {
|
2006-01-08 17:02:19 +08:00
|
|
|
alloc_object rcu_read_lock();
|
|
|
|
... search_for_element
|
|
|
|
atomic_set(&el->rc, 1); if (atomic_inc_not_zero(&el->rc)) {
|
|
|
|
write_lock(&list_lock); rcu_read_unlock();
|
|
|
|
return FAIL;
|
|
|
|
add_element }
|
|
|
|
... ...
|
|
|
|
write_unlock(&list_lock); rcu_read_unlock();
|
2005-09-10 04:04:09 +08:00
|
|
|
} }
|
|
|
|
3. 4.
|
|
|
|
release_referenced() delete()
|
|
|
|
{ {
|
2006-01-08 17:02:19 +08:00
|
|
|
... write_lock(&list_lock);
|
|
|
|
atomic_dec(&el->rc, relfunc) ...
|
|
|
|
... delete_element
|
|
|
|
} write_unlock(&list_lock);
|
|
|
|
...
|
|
|
|
if (atomic_dec_and_test(&el->rc))
|
|
|
|
call_rcu(&el->head, el_free);
|
|
|
|
...
|
2005-09-10 04:04:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Sometimes, reference to the element need to be obtained in the
|
2006-01-08 17:02:19 +08:00
|
|
|
update (write) stream. In such cases, atomic_inc_not_zero might be an
|
|
|
|
overkill since the spinlock serialising list updates are held. atomic_inc
|
2005-09-10 04:04:09 +08:00
|
|
|
is to be used in such cases.
|
2006-01-08 17:02:19 +08:00
|
|
|
|