summaryrefslogtreecommitdiff
path: root/erts/doc/src/atomics.xml
blob: 3fca92fb97358b786c572bcafdc5b9a95d6e510e (plain)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE erlref SYSTEM "erlref.dtd">

<erlref>
  <header>
    <copyright>
      <year>2018</year>
      <holder>Ericsson AB. All Rights Reserved.</holder>
    </copyright>
    <legalnotice>
      Licensed under the Apache License, Version 2.0 (the "License");
      you may not use this file except in compliance with the License.
      You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      See the License for the specific language governing permissions and
      limitations under the License.
    </legalnotice>

    <title>atomics</title>
  </header>
  <module>atomics</module>
  <modulesummary>Atomic Functions</modulesummary>
  <description>
    <p>This module provides a set of functions to do atomic operations towards
    mutable atomic variables. The implementation utilizes only
    atomic hardware instructions without any software level locking, which makes
    it very efficient for concurrent access. The atomics are organized into
    arrays with the follwing semantics:</p>
    <list type="bulleted">
      <item>
	<p>Atomics are 64 bit integers.</p>
      </item>
      <item>
	<p>Atomics can be represented as either signed or unsigned.</p>
      </item>
      <item>
	<p>Atomics wrap around at overflow and underflow operations.</p>
      </item>
      <item>
	<p>All operations guarantee atomicity. No intermediate results can be
	seen. The result of one mutation can only be the input to one
	following mutation.</p>
      </item>
      <item>
	<p>All atomic operations are mutually ordered. If atomic B is updated
	<em>after</em> atomic A, then that is how it will appear to any
	concurrent readers. No one can read the new value of B and then read the
	old value of A.</p>
      </item>
      <item>
	<p>Indexes into atomic arrays are one-based. An atomic array of
	arity N contains N atomics with index from 1 to N.</p>
      </item>
    </list>
  </description>

  <datatypes>
    <datatype>
      <name name="atomics_ref"/>
      <desc><p>Identifies an atomic array returned from
        <seealso marker="#new/2"><c>new/2</c></seealso>.</p>
      </desc>
    </datatype>
  </datatypes>

  <funcs>
    <func>
      <name name="new" arity="2"/>
      <fsummary>Create atomic array</fsummary>
      <desc>
        <p>Create a new atomic array of <c><anno>Arity</anno></c> atomics.</p>
	<p>Argument <c><anno>Opts</anno></c> is a list of the following possible
	options:</p>
	<taglist>
	  <tag><c>{signed, boolean()}</c></tag>
	  <item><p>Indicate if the elements of the array will be treated
	  as signed or unsigned integers. Default is <c>true</c> (signed).</p>
	  <p>The integer interval for signed atomics are from <c>-(1 bsl 63)</c>
	  to <c>(1 bsl 63)-1</c> and for unsigned atomics from <c>0</c> to <c>(1
	  bsl 64)-1</c>.</p>
	  </item>
	</taglist>
      </desc>
    </func>

    <func>
      <name name="put" arity="3"/>
      <fsummary>Set atomic value</fsummary>
      <desc>
        <p>Set atomic to <c><anno>Value</anno></c>.</p>
      </desc>
    </func>

    <func>
      <name name="get" arity="2"/>
      <fsummary>Read atomic value</fsummary>
      <desc>
        <p>Read atomic value.</p>
      </desc>
    </func>

    <func>
      <name name="add" arity="3"/>
      <fsummary>Add to atomic</fsummary>
      <desc>
        <p>Add <c><anno>Incr</anno></c> to atomic.</p>
      </desc>
    </func>

    <func>
      <name name="add_get" arity="3"/>
      <fsummary>Atomic add and get</fsummary>
      <desc>
        <p>Atomic addition and return of the result.</p>
      </desc>
    </func>

    <func>
      <name name="sub" arity="3"/>
      <fsummary>Subtract from atomic</fsummary>
      <desc>
        <p>Subtract <c><anno>Decr</anno></c> from atomic.</p>
      </desc>
    </func>

    <func>
      <name name="sub_get" arity="3"/>
      <fsummary>Atomic sub and get</fsummary>
      <desc>
        <p>Atomic subtraction and return of the result.</p>
      </desc>
    </func>

    <func>
      <name name="exchange" arity="3"/>
      <fsummary>Atomic exchange.</fsummary>
      <desc>
        <p>Atomically replaces the value of the atomic with
	<c><anno>Desired</anno></c> and returns the value it held
	previously.</p>
      </desc>
    </func>
    
    <func>
    <name name="compare_exchange" arity="4"/>
       <fsummary>Atomic compare and exchange.</fsummary>
       <desc>
         <p>Atomically compares the atomic with <c><anno>Expected</anno></c>,
 	and if those are equal, set atomic to <c><anno>Desired</anno></c>.
 	Returns <c>ok</c> if <c><anno>Desired</anno></c> was written. Returns
 	the actual atomic value if not equal to <c><anno>Expected</anno></c>.</p>
       </desc>
    </func>
    
    <func>
      <name name="info" arity="1"/>
      <fsummary>Get information about atomic array.</fsummary>
      <desc>
        <p>Return information about an atomic array in a map. The map
	has the following keys:</p>
	<taglist>
	  <tag><c>size</c></tag>
	  <item><p>The number of atomics in the array.</p></item>
	  <tag><c>max</c></tag>
	  <item><p>The highest possible value an atomic in this array can
	  hold.</p></item>
	  <tag><c>min</c></tag>
	  <item><p>The lowest possible value an atomic in this array can
	  hold.</p></item>
	  <tag><c>memory</c></tag>
	  <item><p>Approximate memory consumption for the array in
	  bytes.</p></item>
	</taglist>
      </desc>
    </func>

 </funcs>
</erlref>