Skip to content

Commit 9f8633b

Browse files
committed
Merge branch 'synchronization-mm' into synchronization
* synchronization-mm: (11 commits) Allow concurrent autoloading ...
2 parents 4b75547 + 661a5b6 commit 9f8633b

File tree

2 files changed

+400
-0
lines changed

2 files changed

+400
-0
lines changed

doc/synchronization-notes.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Concurrent Ruby Notes
2+
3+
## Locks
4+
5+
Concurrent Ruby also has an internal extension of `Object` called
6+
`LockableObject`, which provides same synchronization primitives as Java's
7+
Object: `synchronize(&block)`, `wait(timeout = nil)`,
8+
`wait_until(timeout = nil, &condition)`, `signal`, `broadcast`. This class is
9+
intended for internal use in `concurrent-ruby` only and it does not support
10+
subclassing (since it cannot protect its lock from its children, for more
11+
details see [this article](http://wiki.apidesign.org/wiki/Java_Monitor)). It has
12+
minimal interface to be able to use directly locking available on given
13+
platforms.
14+
15+
For non-internal use there is `Lock` and `Condition` implementation in
16+
`Synchronization` namespace, a condition can be obtained with `new_condition`
17+
method on `Lock`. So far their implementation is naive and requires more work.
18+
API is not expected to change.
19+
20+
## Method names conventions
21+
22+
Methods starting with `ns_` are marking methods that are not using
23+
synchronization by themselves, they have to be used inside synchronize block.
24+
They are usually used in pairs to separate the synchronization from behavior and
25+
to allow to call methods in the same object without double locking.
26+
27+
``` ruby
28+
class Node
29+
# ...
30+
def left
31+
synchronize { ns_left }
32+
end
33+
34+
def right
35+
synchronize { ns_right }
36+
end
37+
38+
def to_a
39+
# avoids double locking
40+
synchronize { [ns_left, ns_right] }
41+
end
42+
43+
private
44+
45+
def ns_left
46+
@left
47+
end
48+
49+
def ns_right
50+
@right
51+
end
52+
# ...
53+
end
54+
```
55+
## Piggybacking
56+
57+
Any write executed before volatile write based on program-order is visible to
58+
the volatile read as well, which allows
59+
[piggybacking](http://stackoverflow.com/questions/8769570/volatile-piggyback-is-this-enough-for-visiblity).
60+
Because it creates synchronizes-with (JMM term) order between volatile write
61+
and read, which participates in creating happens-before order.
62+
63+
This trick is used in some of the abstractions, to avoid unnecessary
64+
synchronization or volatile declarations.

0 commit comments

Comments
 (0)