|
1 | | -require 'concurrent/atomic/atomic_fixnum' |
| 1 | +require 'concurrent/atomic' |
2 | 2 |
|
3 | 3 | module Concurrent |
4 | 4 |
|
5 | | - module ThreadLocalSymbolAllocator |
6 | | - |
7 | | - COUNTER = Concurrent::AtomicFixnum.new |
8 | | - |
9 | | - protected |
10 | | - |
11 | | - def allocate_symbol |
12 | | - # Warning: this symbol may never be deallocated |
13 | | - @symbol = :"thread_local_symbol_#{COUNTER.increment}" |
14 | | - end |
15 | | - |
16 | | - end |
17 | | - |
18 | | - module ThreadLocalOldStorage |
19 | | - |
20 | | - include ThreadLocalSymbolAllocator |
21 | | - |
22 | | - protected |
23 | | - |
24 | | - def allocate_storage |
25 | | - allocate_symbol |
26 | | - end |
27 | | - |
28 | | - def get |
29 | | - Thread.current[@symbol] |
30 | | - end |
31 | | - |
32 | | - def set(value) |
33 | | - Thread.current[@symbol] = value |
34 | | - end |
35 | | - |
36 | | - end |
37 | | - |
38 | | - module ThreadLocalNewStorage |
39 | | - |
40 | | - include ThreadLocalSymbolAllocator |
41 | | - |
42 | | - protected |
| 5 | + module ThreadLocalRubyStorage |
43 | 6 |
|
44 | 7 | def allocate_storage |
45 | | - allocate_symbol |
| 8 | + @storage = Atomic.new Hash.new |
46 | 9 | end |
47 | 10 |
|
48 | 11 | def get |
49 | | - Thread.current.thread_variable_get(@symbol) |
| 12 | + @storage.get[Thread.current] |
50 | 13 | end |
51 | 14 |
|
52 | 15 | def set(value) |
53 | | - Thread.current.thread_variable_set(@symbol, value) |
| 16 | + @storage.update { |s| s.merge Thread.current => value } |
54 | 17 | end |
55 | 18 |
|
56 | 19 | end |
@@ -79,10 +42,8 @@ class ThreadLocalVar |
79 | 42 |
|
80 | 43 | if RUBY_PLATFORM == 'java' |
81 | 44 | include ThreadLocalJavaStorage |
82 | | - elsif Thread.current.respond_to?(:thread_variable_set) |
83 | | - include ThreadLocalNewStorage |
84 | 45 | else |
85 | | - include ThreadLocalOldStorage |
| 46 | + include ThreadLocalRubyStorage |
86 | 47 | end |
87 | 48 |
|
88 | 49 | def initialize(default = nil) |
|
0 commit comments