File: atomic_boolean.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 (126 lines) | stat: -rw-r--r-- 3,812 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
require 'concurrent/atomic/mutex_atomic_boolean'
require 'concurrent/synchronization'

module Concurrent

  ###################################################################

  # @!macro atomic_boolean_method_initialize
  #
  #   Creates a new `AtomicBoolean` with the given initial value.
  #
  #   @param [Boolean] initial the initial value

  # @!macro atomic_boolean_method_value_get
  #
  #   Retrieves the current `Boolean` value.
  #
  #   @return [Boolean] the current value

  # @!macro atomic_boolean_method_value_set
  #
  #   Explicitly sets the value.
  #
  #   @param [Boolean] value the new value to be set
  #
  #   @return [Boolean] the current value

  # @!macro atomic_boolean_method_true_question
  #
  #   Is the current value `true`
  #
  #   @return [Boolean] true if the current value is `true`, else false

  # @!macro atomic_boolean_method_false_question
  #
  #   Is the current value `false`
  #
  #   @return [Boolean] true if the current value is `false`, else false

  # @!macro atomic_boolean_method_make_true
  #
  #   Explicitly sets the value to true.
  #
  #   @return [Boolean] true if value has changed, otherwise false

  # @!macro atomic_boolean_method_make_false
  #
  #   Explicitly sets the value to false.
  #
  #   @return [Boolean] true if value has changed, otherwise false

  ###################################################################

  # @!macro atomic_boolean_public_api
  #
  #   @!method initialize(initial = false)
  #     @!macro  atomic_boolean_method_initialize
  #
  #   @!method value
  #     @!macro  atomic_boolean_method_value_get
  #
  #   @!method value=(value)
  #     @!macro  atomic_boolean_method_value_set
  #
  #   @!method true?
  #     @!macro  atomic_boolean_method_true_question
  #
  #   @!method false?
  #     @!macro  atomic_boolean_method_false_question
  #
  #   @!method make_true
  #     @!macro  atomic_boolean_method_make_true
  #
  #   @!method make_false
  #     @!macro  atomic_boolean_method_make_false

  ###################################################################

  # @!visibility private
  # @!macro internal_implementation_note
  AtomicBooleanImplementation = case
                                when defined?(JavaAtomicBoolean)
                                  JavaAtomicBoolean
                                when defined?(CAtomicBoolean)
                                  CAtomicBoolean
                                else
                                  MutexAtomicBoolean
                                end
  private_constant :AtomicBooleanImplementation

  # @!macro atomic_boolean
  #
  #   A boolean value that can be updated atomically. Reads and writes to an atomic
  #   boolean and thread-safe and guaranteed to succeed. Reads and writes may block
  #   briefly but no explicit locking is required.
  #
  #   @!macro thread_safe_variable_comparison
  #
  #   Performance:
  #
  #   ```
  #   Testing with ruby 2.1.2
  #   Testing with Concurrent::MutexAtomicBoolean...
  #     2.790000   0.000000   2.790000 (  2.791454)
  #   Testing with Concurrent::CAtomicBoolean...
  #     0.740000   0.000000   0.740000 (  0.740206)
  #
  #   Testing with jruby 1.9.3
  #   Testing with Concurrent::MutexAtomicBoolean...
  #     5.240000   2.520000   7.760000 (  3.683000)
  #   Testing with Concurrent::JavaAtomicBoolean...
  #     3.340000   0.010000   3.350000 (  0.855000)
  #   ```
  #
  #   @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicBoolean.html java.util.concurrent.atomic.AtomicBoolean
  #
  # @!macro atomic_boolean_public_api
  class AtomicBoolean < AtomicBooleanImplementation
    # @return [String] Short string representation.
    def to_s
      format '%s value:%s>', super[0..-2], value
    end

    alias_method :inspect, :to_s
  end
end