|
| 1 | +require 'concurrent/utility/engine' |
| 2 | +require 'concurrent/thread_safe/util' |
| 3 | +require 'set' |
| 4 | + |
| 5 | +module Concurrent |
| 6 | + if Concurrent.on_cruby? |
| 7 | + |
| 8 | + # Because MRI never runs code in parallel, the existing |
| 9 | + # non-thread-safe structures should usually work fine. |
| 10 | + |
| 11 | + # @!macro [attach] concurrent_Set |
| 12 | + # |
| 13 | + # A thread-safe subclass of Set. This version locks against the object |
| 14 | + # itself for every method call, ensuring only one thread can be reading |
| 15 | + # or writing at a time. This includes iteration methods like `#each`. |
| 16 | + # |
| 17 | + # @note `a += b` is **not** a **thread-safe** operation on |
| 18 | + # `Concurrent::Set`. It reads Set `a`, then it creates new `Concurrent::Set` |
| 19 | + # which is union of `a` and `b`, then it writes the union to `a`. |
| 20 | + # The read and write are independent operations they do not form a single atomic |
| 21 | + # operation therefore when two `+=` operations are executed concurrently updates |
| 22 | + # may be lost. Use `#merge` instead. |
| 23 | + # |
| 24 | + # @see http://ruby-doc.org/stdlib-2.4.0/libdoc/set/rdoc/Set.html Ruby standard library `Set` |
| 25 | + class Set < ::Set; |
| 26 | + end |
| 27 | + |
| 28 | + elsif Concurrent.on_jruby? |
| 29 | + require 'jruby/synchronized' |
| 30 | + |
| 31 | + # @!macro concurrent_Set |
| 32 | + class Set < ::Set |
| 33 | + include JRuby::Synchronized |
| 34 | + end |
| 35 | + |
| 36 | + elsif Concurrent.on_rbx? || Concurrent.on_truffle? |
| 37 | + require 'monitor' |
| 38 | + require 'concurrent/thread_safe/util/array_hash_rbx' |
| 39 | + |
| 40 | + # @!macro concurrent_Set |
| 41 | + class Set < ::Set |
| 42 | + end |
| 43 | + |
| 44 | + ThreadSafe::Util.make_synchronized_on_rbx Set |
| 45 | + end |
| 46 | +end |
| 47 | + |
0 commit comments