File: array.rb

package info (click to toggle)
ruby-concurrent 1.1.6%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 30,284 kB
  • sloc: ruby: 30,875; java: 6,117; javascript: 1,114; ansic: 288; makefile: 10; sh: 6
file content (66 lines) | stat: -rw-r--r-- 2,585 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
require 'concurrent/utility/engine'
require 'concurrent/thread_safe/util'

module Concurrent

  # @!macro concurrent_array
  #
  #   A thread-safe subclass of Array. This version locks against the object
  #   itself for every method call, ensuring only one thread can be reading
  #   or writing at a time. This includes iteration methods like `#each`.
  #
  #   @note `a += b` is **not** a **thread-safe** operation on
  #     `Concurrent::Array`. It reads array `a`, then it creates new `Concurrent::Array`
  #     which is concatenation of `a` and `b`, then it writes the concatenation to `a`.
  #     The read and write are independent operations they do not form a single atomic
  #     operation therefore when two `+=` operations are executed concurrently updates
  #     may be lost. Use `#concat` instead.
  #
  #   @see http://ruby-doc.org/core-2.2.0/Array.html Ruby standard library `Array`

  # @!macro internal_implementation_note
  ArrayImplementation = case
                        when Concurrent.on_cruby?
                          # Array is thread-safe in practice because CRuby runs
                          # threads one at a time and does not do context
                          # switching during the execution of C functions.
                          ::Array

                        when Concurrent.on_jruby?
                          require 'jruby/synchronized'

                          class JRubyArray < ::Array
                            include JRuby::Synchronized
                          end
                          JRubyArray

                        when Concurrent.on_rbx?
                          require 'monitor'
                          require 'concurrent/thread_safe/util/data_structures'

                          class RbxArray < ::Array
                          end

                          ThreadSafe::Util.make_synchronized_on_rbx RbxArray
                          RbxArray

                        when Concurrent.on_truffleruby?
                          require 'concurrent/thread_safe/util/data_structures'

                          class TruffleRubyArray < ::Array
                          end

                          ThreadSafe::Util.make_synchronized_on_truffleruby TruffleRubyArray
                          TruffleRubyArray

                        else
                          warn 'Possibly unsupported Ruby implementation'
                          ::Array
                        end
  private_constant :ArrayImplementation

  # @!macro concurrent_array
  class Array < ArrayImplementation
  end

end