Merge branches 'release', 'acpi_pm_device_sleep_state' and 'battery' into release
[linux-2.6-block.git] / Documentation / RCU / rcuref.txt
CommitLineData
d19720a9 1Reference-count design for elements of lists/arrays protected by RCU.
c0dfb290 2
d19720a9
PM
3Reference counting on elements of lists which are protected by traditional
4reader/writer spinlocks or semaphores are straightforward:
c0dfb290 5
095975da
NP
61. 2.
7add() search_and_reference()
8{ {
9 alloc_object read_lock(&list_lock);
10 ... search_for_element
11 atomic_set(&el->rc, 1); atomic_inc(&el->rc);
12 write_lock(&list_lock); ...
13 add_element read_unlock(&list_lock);
14 ... ...
15 write_unlock(&list_lock); }
c0dfb290
DS
16}
17
183. 4.
19release_referenced() delete()
20{ {
095975da
NP
21 ... write_lock(&list_lock);
22 atomic_dec(&el->rc, relfunc) ...
23 ... delete_element
24} write_unlock(&list_lock);
25 ...
26 if (atomic_dec_and_test(&el->rc))
27 kfree(el);
28 ...
c0dfb290
DS
29 }
30
d19720a9
PM
31If this list/array is made lock free using RCU as in changing the
32write_lock() in add() and delete() to spin_lock and changing read_lock
095975da 33in search_and_reference to rcu_read_lock(), the atomic_get in
c0dfb290 34search_and_reference could potentially hold reference to an element which
d19720a9
PM
35has already been deleted from the list/array. Use atomic_inc_not_zero()
36in this scenario as follows:
c0dfb290
DS
37
381. 2.
39add() search_and_reference()
40{ {
095975da
NP
41 alloc_object rcu_read_lock();
42 ... search_for_element
43 atomic_set(&el->rc, 1); if (atomic_inc_not_zero(&el->rc)) {
44 write_lock(&list_lock); rcu_read_unlock();
45 return FAIL;
46 add_element }
47 ... ...
48 write_unlock(&list_lock); rcu_read_unlock();
c0dfb290
DS
49} }
503. 4.
51release_referenced() delete()
52{ {
095975da 53 ... write_lock(&list_lock);
d19720a9
PM
54 if (atomic_dec_and_test(&el->rc)) ...
55 call_rcu(&el->head, el_free); delete_element
56 ... write_unlock(&list_lock);
57} ...
095975da
NP
58 if (atomic_dec_and_test(&el->rc))
59 call_rcu(&el->head, el_free);
60 ...
c0dfb290
DS
61 }
62
d19720a9
PM
63Sometimes, a reference to the element needs to be obtained in the
64update (write) stream. In such cases, atomic_inc_not_zero() might be
65overkill, since we hold the update-side spinlock. One might instead
66use atomic_inc() in such cases.