You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

863 lines
28 KiB

8 years ago
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <meta name="theme-color" content="#375EAB">
  7. <title>atomic - The Go Programming Language</title>
  8. <link type="text/css" rel="stylesheet" href="../../../lib/godoc/style.css">
  9. <link rel="stylesheet" href="../../../lib/godoc/jquery.treeview.css">
  10. <script type="text/javascript">window.initFuncs = [];</script>
  11. </head>
  12. <body>
  13. <div id='lowframe' style="position: fixed; bottom: 0; left: 0; height: 0; width: 100%; border-top: thin solid grey; background-color: white; overflow: auto;">
  14. ...
  15. </div><!-- #lowframe -->
  16. <div id="topbar" class="wide"><div class="container">
  17. <div class="top-heading" id="heading-wide"><a href="http://localhost:6060/">The Go Programming Language</a></div>
  18. <div class="top-heading" id="heading-narrow"><a href="http://localhost:6060/">Go</a></div>
  19. <a href="index.html#" id="menu-button"><span id="menu-button-arrow">&#9661;</span></a>
  20. <form method="GET" action="http://localhost:6060/search">
  21. <div id="menu">
  22. <a href="http://localhost:6060/doc/">Documents</a>
  23. <a href="http://localhost:6060/pkg/">Packages</a>
  24. <a href="http://localhost:6060/project/">The Project</a>
  25. <a href="http://localhost:6060/help/">Help</a>
  26. <a href="http://localhost:6060/blog/">Blog</a>
  27. <input type="text" id="search" name="q" class="inactive" value="Search" placeholder="Search">
  28. </div>
  29. </form>
  30. </div></div>
  31. <div id="page" class="wide">
  32. <div class="container">
  33. <h1>Package atomic</h1>
  34. <div id="nav"></div>
  35. <!--
  36. Copyright 2009 The Go Authors. All rights reserved.
  37. Use of this source code is governed by a BSD-style
  38. license that can be found in the LICENSE file.
  39. -->
  40. <!--
  41. Note: Static (i.e., not template-generated) href and id
  42. attributes start with "pkg-" to make it impossible for
  43. them to conflict with generated attributes (some of which
  44. correspond to Go identifiers).
  45. -->
  46. <script type='text/javascript'>
  47. document.ANALYSIS_DATA = null;
  48. document.CALLGRAPH = null;
  49. </script>
  50. <div id="short-nav">
  51. <dl>
  52. <dd><code>import "sync/atomic"</code></dd>
  53. </dl>
  54. <dl>
  55. <dd><a href="index.html#pkg-overview" class="overviewLink">Overview</a></dd>
  56. <dd><a href="index.html#pkg-index" class="indexLink">Index</a></dd>
  57. <dd><a href="index.html#pkg-examples" class="examplesLink">Examples</a></dd>
  58. </dl>
  59. </div>
  60. <!-- The package's Name is printed as title by the top-level template -->
  61. <div id="pkg-overview" class="toggleVisible">
  62. <div class="collapsed">
  63. <h2 class="toggleButton" title="Click to show Overview section">Overview ▹</h2>
  64. </div>
  65. <div class="expanded">
  66. <h2 class="toggleButton" title="Click to hide Overview section">Overview ▾</h2>
  67. <p>
  68. Package atomic provides low-level atomic memory primitives
  69. useful for implementing synchronization algorithms.
  70. </p>
  71. <p>
  72. These functions require great care to be used correctly.
  73. Except for special, low-level applications, synchronization is better
  74. done with channels or the facilities of the sync package.
  75. Share memory by communicating;
  76. don&#39;t communicate by sharing memory.
  77. </p>
  78. <p>
  79. The swap operation, implemented by the SwapT functions, is the atomic
  80. equivalent of:
  81. </p>
  82. <pre>old = *addr
  83. *addr = new
  84. return old
  85. </pre>
  86. <p>
  87. The compare-and-swap operation, implemented by the CompareAndSwapT
  88. functions, is the atomic equivalent of:
  89. </p>
  90. <pre>if *addr == old {
  91. *addr = new
  92. return true
  93. }
  94. return false
  95. </pre>
  96. <p>
  97. The add operation, implemented by the AddT functions, is the atomic
  98. equivalent of:
  99. </p>
  100. <pre>*addr += delta
  101. return *addr
  102. </pre>
  103. <p>
  104. The load and store operations, implemented by the LoadT and StoreT
  105. functions, are the atomic equivalents of &#34;return *addr&#34; and
  106. &#34;*addr = val&#34;.
  107. </p>
  108. </div>
  109. </div>
  110. <div id="pkg-index" class="toggleVisible">
  111. <div class="collapsed">
  112. <h2 class="toggleButton" title="Click to show Index section">Index ▹</h2>
  113. </div>
  114. <div class="expanded">
  115. <h2 class="toggleButton" title="Click to hide Index section">Index ▾</h2>
  116. <!-- Table of contents for API; must be named manual-nav to turn off auto nav. -->
  117. <div id="manual-nav">
  118. <dl>
  119. <dd><a href="index.html#AddInt32">func AddInt32(addr *int32, delta int32) (new int32)</a></dd>
  120. <dd><a href="index.html#AddInt64">func AddInt64(addr *int64, delta int64) (new int64)</a></dd>
  121. <dd><a href="index.html#AddUint32">func AddUint32(addr *uint32, delta uint32) (new uint32)</a></dd>
  122. <dd><a href="index.html#AddUint64">func AddUint64(addr *uint64, delta uint64) (new uint64)</a></dd>
  123. <dd><a href="index.html#AddUintptr">func AddUintptr(addr *uintptr, delta uintptr) (new uintptr)</a></dd>
  124. <dd><a href="index.html#CompareAndSwapInt32">func CompareAndSwapInt32(addr *int32, old, new int32) (swapped bool)</a></dd>
  125. <dd><a href="index.html#CompareAndSwapInt64">func CompareAndSwapInt64(addr *int64, old, new int64) (swapped bool)</a></dd>
  126. <dd><a href="index.html#CompareAndSwapPointer">func CompareAndSwapPointer(addr *unsafe.Pointer, old, new unsafe.Pointer) (swapped bool)</a></dd>
  127. <dd><a href="index.html#CompareAndSwapUint32">func CompareAndSwapUint32(addr *uint32, old, new uint32) (swapped bool)</a></dd>
  128. <dd><a href="index.html#CompareAndSwapUint64">func CompareAndSwapUint64(addr *uint64, old, new uint64) (swapped bool)</a></dd>
  129. <dd><a href="index.html#CompareAndSwapUintptr">func CompareAndSwapUintptr(addr *uintptr, old, new uintptr) (swapped bool)</a></dd>
  130. <dd><a href="index.html#LoadInt32">func LoadInt32(addr *int32) (val int32)</a></dd>
  131. <dd><a href="index.html#LoadInt64">func LoadInt64(addr *int64) (val int64)</a></dd>
  132. <dd><a href="index.html#LoadPointer">func LoadPointer(addr *unsafe.Pointer) (val unsafe.Pointer)</a></dd>
  133. <dd><a href="index.html#LoadUint32">func LoadUint32(addr *uint32) (val uint32)</a></dd>
  134. <dd><a href="index.html#LoadUint64">func LoadUint64(addr *uint64) (val uint64)</a></dd>
  135. <dd><a href="index.html#LoadUintptr">func LoadUintptr(addr *uintptr) (val uintptr)</a></dd>
  136. <dd><a href="index.html#StoreInt32">func StoreInt32(addr *int32, val int32)</a></dd>
  137. <dd><a href="index.html#StoreInt64">func StoreInt64(addr *int64, val int64)</a></dd>
  138. <dd><a href="index.html#StorePointer">func StorePointer(addr *unsafe.Pointer, val unsafe.Pointer)</a></dd>
  139. <dd><a href="index.html#StoreUint32">func StoreUint32(addr *uint32, val uint32)</a></dd>
  140. <dd><a href="index.html#StoreUint64">func StoreUint64(addr *uint64, val uint64)</a></dd>
  141. <dd><a href="index.html#StoreUintptr">func StoreUintptr(addr *uintptr, val uintptr)</a></dd>
  142. <dd><a href="index.html#SwapInt32">func SwapInt32(addr *int32, new int32) (old int32)</a></dd>
  143. <dd><a href="index.html#SwapInt64">func SwapInt64(addr *int64, new int64) (old int64)</a></dd>
  144. <dd><a href="index.html#SwapPointer">func SwapPointer(addr *unsafe.Pointer, new unsafe.Pointer) (old unsafe.Pointer)</a></dd>
  145. <dd><a href="index.html#SwapUint32">func SwapUint32(addr *uint32, new uint32) (old uint32)</a></dd>
  146. <dd><a href="index.html#SwapUint64">func SwapUint64(addr *uint64, new uint64) (old uint64)</a></dd>
  147. <dd><a href="index.html#SwapUintptr">func SwapUintptr(addr *uintptr, new uintptr) (old uintptr)</a></dd>
  148. <dd><a href="index.html#Value">type Value</a></dd>
  149. <dd>&nbsp; &nbsp; <a href="index.html#Value.Load">func (v *Value) Load() (x interface{})</a></dd>
  150. <dd>&nbsp; &nbsp; <a href="index.html#Value.Store">func (v *Value) Store(x interface{})</a></dd>
  151. <dd><a href="index.html#pkg-note-BUG">Bugs</a></dd>
  152. </dl>
  153. </div><!-- #manual-nav -->
  154. <div id="pkg-examples">
  155. <h4>Examples</h4>
  156. <dl>
  157. <dd><a class="exampleLink" href="index.html#example_Value_config">Value (Config)</a></dd>
  158. <dd><a class="exampleLink" href="index.html#example_Value_readMostly">Value (ReadMostly)</a></dd>
  159. </dl>
  160. </div>
  161. <h4>Package files</h4>
  162. <p>
  163. <span style="font-size:90%">
  164. <a href="http://localhost:6060/src/sync/atomic/doc.go">doc.go</a>
  165. <a href="http://localhost:6060/src/sync/atomic/value.go">value.go</a>
  166. </span>
  167. </p>
  168. </div><!-- .expanded -->
  169. </div><!-- #pkg-index -->
  170. <div id="pkg-callgraph" class="toggle" style="display: none">
  171. <div class="collapsed">
  172. <h2 class="toggleButton" title="Click to show Internal Call Graph section">Internal call graph ▹</h2>
  173. </div> <!-- .expanded -->
  174. <div class="expanded">
  175. <h2 class="toggleButton" title="Click to hide Internal Call Graph section">Internal call graph ▾</h2>
  176. <p>
  177. In the call graph viewer below, each node
  178. is a function belonging to this package
  179. and its children are the functions it
  180. calls&mdash;perhaps dynamically.
  181. </p>
  182. <p>
  183. The root nodes are the entry points of the
  184. package: functions that may be called from
  185. outside the package.
  186. There may be non-exported or anonymous
  187. functions among them if they are called
  188. dynamically from another package.
  189. </p>
  190. <p>
  191. Click a node to visit that function's source code.
  192. From there you can visit its callers by
  193. clicking its declaring <code>func</code>
  194. token.
  195. </p>
  196. <p>
  197. Functions may be omitted if they were
  198. determined to be unreachable in the
  199. particular programs or tests that were
  200. analyzed.
  201. </p>
  202. <!-- Zero means show all package entry points. -->
  203. <ul style="margin-left: 0.5in" id="callgraph-0" class="treeview"></ul>
  204. </div>
  205. </div> <!-- #pkg-callgraph -->
  206. <h2 id="AddInt32">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=3560:3611#L82">AddInt32</a></h2>
  207. <pre>func AddInt32(addr *<a href="../../builtin/index.html#int32">int32</a>, delta <a href="../../builtin/index.html#int32">int32</a>) (new <a href="../../builtin/index.html#int32">int32</a>)</pre>
  208. <p>
  209. AddInt32 atomically adds delta to *addr and returns the new value.
  210. </p>
  211. <h2 id="AddInt64">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=3965:4016#L90">AddInt64</a></h2>
  212. <pre>func AddInt64(addr *<a href="../../builtin/index.html#int64">int64</a>, delta <a href="../../builtin/index.html#int64">int64</a>) (new <a href="../../builtin/index.html#int64">int64</a>)</pre>
  213. <p>
  214. AddInt64 atomically adds delta to *addr and returns the new value.
  215. </p>
  216. <h2 id="AddUint32">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=3838:3893#L87">AddUint32</a></h2>
  217. <pre>func AddUint32(addr *<a href="../../builtin/index.html#uint32">uint32</a>, delta <a href="../../builtin/index.html#uint32">uint32</a>) (new <a href="../../builtin/index.html#uint32">uint32</a>)</pre>
  218. <p>
  219. AddUint32 atomically adds delta to *addr and returns the new value.
  220. To subtract a signed positive constant value c from x, do AddUint32(&amp;x, ^uint32(c-1)).
  221. In particular, to decrement x, do AddUint32(&amp;x, ^uint32(0)).
  222. </p>
  223. <h2 id="AddUint64">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=4243:4298#L95">AddUint64</a></h2>
  224. <pre>func AddUint64(addr *<a href="../../builtin/index.html#uint64">uint64</a>, delta <a href="../../builtin/index.html#uint64">uint64</a>) (new <a href="../../builtin/index.html#uint64">uint64</a>)</pre>
  225. <p>
  226. AddUint64 atomically adds delta to *addr and returns the new value.
  227. To subtract a signed positive constant value c from x, do AddUint64(&amp;x, ^uint64(c-1)).
  228. In particular, to decrement x, do AddUint64(&amp;x, ^uint64(0)).
  229. </p>
  230. <h2 id="AddUintptr">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=4372:4431#L98">AddUintptr</a></h2>
  231. <pre>func AddUintptr(addr *<a href="../../builtin/index.html#uintptr">uintptr</a>, delta <a href="../../builtin/index.html#uintptr">uintptr</a>) (new <a href="../../builtin/index.html#uintptr">uintptr</a>)</pre>
  232. <p>
  233. AddUintptr atomically adds delta to *addr and returns the new value.
  234. </p>
  235. <h2 id="CompareAndSwapInt32">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=2608:2676#L64">CompareAndSwapInt32</a></h2>
  236. <pre>func CompareAndSwapInt32(addr *<a href="../../builtin/index.html#int32">int32</a>, old, new <a href="../../builtin/index.html#int32">int32</a>) (swapped <a href="../../builtin/index.html#bool">bool</a>)</pre>
  237. <p>
  238. CompareAndSwapInt32 executes the compare-and-swap operation for an int32 value.
  239. </p>
  240. <h2 id="CompareAndSwapInt64">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=2761:2829#L67">CompareAndSwapInt64</a></h2>
  241. <pre>func CompareAndSwapInt64(addr *<a href="../../builtin/index.html#int64">int64</a>, old, new <a href="../../builtin/index.html#int64">int64</a>) (swapped <a href="../../builtin/index.html#bool">bool</a>)</pre>
  242. <p>
  243. CompareAndSwapInt64 executes the compare-and-swap operation for an int64 value.
  244. </p>
  245. <h2 id="CompareAndSwapPointer">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=3400:3488#L79">CompareAndSwapPointer</a></h2>
  246. <pre>func CompareAndSwapPointer(addr *<a href="../../unsafe/index.html">unsafe</a>.<a href="../../unsafe/index.html#Pointer">Pointer</a>, old, new <a href="../../unsafe/index.html">unsafe</a>.<a href="../../unsafe/index.html#Pointer">Pointer</a>) (swapped <a href="../../builtin/index.html#bool">bool</a>)</pre>
  247. <p>
  248. CompareAndSwapPointer executes the compare-and-swap operation for a unsafe.Pointer value.
  249. </p>
  250. <h2 id="CompareAndSwapUint32">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=2915:2986#L70">CompareAndSwapUint32</a></h2>
  251. <pre>func CompareAndSwapUint32(addr *<a href="../../builtin/index.html#uint32">uint32</a>, old, new <a href="../../builtin/index.html#uint32">uint32</a>) (swapped <a href="../../builtin/index.html#bool">bool</a>)</pre>
  252. <p>
  253. CompareAndSwapUint32 executes the compare-and-swap operation for a uint32 value.
  254. </p>
  255. <h2 id="CompareAndSwapUint64">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=3072:3143#L73">CompareAndSwapUint64</a></h2>
  256. <pre>func CompareAndSwapUint64(addr *<a href="../../builtin/index.html#uint64">uint64</a>, old, new <a href="../../builtin/index.html#uint64">uint64</a>) (swapped <a href="../../builtin/index.html#bool">bool</a>)</pre>
  257. <p>
  258. CompareAndSwapUint64 executes the compare-and-swap operation for a uint64 value.
  259. </p>
  260. <h2 id="CompareAndSwapUintptr">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=3231:3305#L76">CompareAndSwapUintptr</a></h2>
  261. <pre>func CompareAndSwapUintptr(addr *<a href="../../builtin/index.html#uintptr">uintptr</a>, old, new <a href="../../builtin/index.html#uintptr">uintptr</a>) (swapped <a href="../../builtin/index.html#bool">bool</a>)</pre>
  262. <p>
  263. CompareAndSwapUintptr executes the compare-and-swap operation for a uintptr value.
  264. </p>
  265. <h2 id="LoadInt32">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=4470:4509#L101">LoadInt32</a></h2>
  266. <pre>func LoadInt32(addr *<a href="../../builtin/index.html#int32">int32</a>) (val <a href="../../builtin/index.html#int32">int32</a>)</pre>
  267. <p>
  268. LoadInt32 atomically loads *addr.
  269. </p>
  270. <h2 id="LoadInt64">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=4548:4587#L104">LoadInt64</a></h2>
  271. <pre>func LoadInt64(addr *<a href="../../builtin/index.html#int64">int64</a>) (val <a href="../../builtin/index.html#int64">int64</a>)</pre>
  272. <p>
  273. LoadInt64 atomically loads *addr.
  274. </p>
  275. <h2 id="LoadPointer">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=4878:4937#L116">LoadPointer</a></h2>
  276. <pre>func LoadPointer(addr *<a href="../../unsafe/index.html">unsafe</a>.<a href="../../unsafe/index.html#Pointer">Pointer</a>) (val <a href="../../unsafe/index.html">unsafe</a>.<a href="../../unsafe/index.html#Pointer">Pointer</a>)</pre>
  277. <p>
  278. LoadPointer atomically loads *addr.
  279. </p>
  280. <h2 id="LoadUint32">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=4627:4669#L107">LoadUint32</a></h2>
  281. <pre>func LoadUint32(addr *<a href="../../builtin/index.html#uint32">uint32</a>) (val <a href="../../builtin/index.html#uint32">uint32</a>)</pre>
  282. <p>
  283. LoadUint32 atomically loads *addr.
  284. </p>
  285. <h2 id="LoadUint64">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=4709:4751#L110">LoadUint64</a></h2>
  286. <pre>func LoadUint64(addr *<a href="../../builtin/index.html#uint64">uint64</a>) (val <a href="../../builtin/index.html#uint64">uint64</a>)</pre>
  287. <p>
  288. LoadUint64 atomically loads *addr.
  289. </p>
  290. <h2 id="LoadUintptr">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=4792:4837#L113">LoadUintptr</a></h2>
  291. <pre>func LoadUintptr(addr *<a href="../../builtin/index.html#uintptr">uintptr</a>) (val <a href="../../builtin/index.html#uintptr">uintptr</a>)</pre>
  292. <p>
  293. LoadUintptr atomically loads *addr.
  294. </p>
  295. <h2 id="StoreInt32">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=4987:5026#L119">StoreInt32</a></h2>
  296. <pre>func StoreInt32(addr *<a href="../../builtin/index.html#int32">int32</a>, val <a href="../../builtin/index.html#int32">int32</a>)</pre>
  297. <p>
  298. StoreInt32 atomically stores val into *addr.
  299. </p>
  300. <h2 id="StoreInt64">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=5076:5115#L122">StoreInt64</a></h2>
  301. <pre>func StoreInt64(addr *<a href="../../builtin/index.html#int64">int64</a>, val <a href="../../builtin/index.html#int64">int64</a>)</pre>
  302. <p>
  303. StoreInt64 atomically stores val into *addr.
  304. </p>
  305. <h2 id="StorePointer">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=5450:5509#L134">StorePointer</a></h2>
  306. <pre>func StorePointer(addr *<a href="../../unsafe/index.html">unsafe</a>.<a href="../../unsafe/index.html#Pointer">Pointer</a>, val <a href="../../unsafe/index.html">unsafe</a>.<a href="../../unsafe/index.html#Pointer">Pointer</a>)</pre>
  307. <p>
  308. StorePointer atomically stores val into *addr.
  309. </p>
  310. <h2 id="StoreUint32">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=5166:5208#L125">StoreUint32</a></h2>
  311. <pre>func StoreUint32(addr *<a href="../../builtin/index.html#uint32">uint32</a>, val <a href="../../builtin/index.html#uint32">uint32</a>)</pre>
  312. <p>
  313. StoreUint32 atomically stores val into *addr.
  314. </p>
  315. <h2 id="StoreUint64">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=5259:5301#L128">StoreUint64</a></h2>
  316. <pre>func StoreUint64(addr *<a href="../../builtin/index.html#uint64">uint64</a>, val <a href="../../builtin/index.html#uint64">uint64</a>)</pre>
  317. <p>
  318. StoreUint64 atomically stores val into *addr.
  319. </p>
  320. <h2 id="StoreUintptr">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=5353:5398#L131">StoreUintptr</a></h2>
  321. <pre>func StoreUintptr(addr *<a href="../../builtin/index.html#uintptr">uintptr</a>, val <a href="../../builtin/index.html#uintptr">uintptr</a>)</pre>
  322. <p>
  323. StoreUintptr atomically stores val into *addr.
  324. </p>
  325. <h2 id="SwapInt32">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=1742:1792#L46">SwapInt32</a></h2>
  326. <pre>func SwapInt32(addr *<a href="../../builtin/index.html#int32">int32</a>, new <a href="../../builtin/index.html#int32">int32</a>) (old <a href="../../builtin/index.html#int32">int32</a>)</pre>
  327. <p>
  328. SwapInt32 atomically stores new into *addr and returns the previous *addr value.
  329. </p>
  330. <h2 id="SwapInt64">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=1878:1928#L49">SwapInt64</a></h2>
  331. <pre>func SwapInt64(addr *<a href="../../builtin/index.html#int64">int64</a>, new <a href="../../builtin/index.html#int64">int64</a>) (old <a href="../../builtin/index.html#int64">int64</a>)</pre>
  332. <p>
  333. SwapInt64 atomically stores new into *addr and returns the previous *addr value.
  334. </p>
  335. <h2 id="SwapPointer">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=2444:2523#L61">SwapPointer</a></h2>
  336. <pre>func SwapPointer(addr *<a href="../../unsafe/index.html">unsafe</a>.<a href="../../unsafe/index.html#Pointer">Pointer</a>, new <a href="../../unsafe/index.html">unsafe</a>.<a href="../../unsafe/index.html#Pointer">Pointer</a>) (old <a href="../../unsafe/index.html">unsafe</a>.<a href="../../unsafe/index.html#Pointer">Pointer</a>)</pre>
  337. <p>
  338. SwapPointer atomically stores new into *addr and returns the previous *addr value.
  339. </p>
  340. <h2 id="SwapUint32">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=2015:2069#L52">SwapUint32</a></h2>
  341. <pre>func SwapUint32(addr *<a href="../../builtin/index.html#uint32">uint32</a>, new <a href="../../builtin/index.html#uint32">uint32</a>) (old <a href="../../builtin/index.html#uint32">uint32</a>)</pre>
  342. <p>
  343. SwapUint32 atomically stores new into *addr and returns the previous *addr value.
  344. </p>
  345. <h2 id="SwapUint64">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=2156:2210#L55">SwapUint64</a></h2>
  346. <pre>func SwapUint64(addr *<a href="../../builtin/index.html#uint64">uint64</a>, new <a href="../../builtin/index.html#uint64">uint64</a>) (old <a href="../../builtin/index.html#uint64">uint64</a>)</pre>
  347. <p>
  348. SwapUint64 atomically stores new into *addr and returns the previous *addr value.
  349. </p>
  350. <h2 id="SwapUintptr">func <a href="http://localhost:6060/src/sync/atomic/doc.go?s=2298:2356#L58">SwapUintptr</a></h2>
  351. <pre>func SwapUintptr(addr *<a href="../../builtin/index.html#uintptr">uintptr</a>, new <a href="../../builtin/index.html#uintptr">uintptr</a>) (old <a href="../../builtin/index.html#uintptr">uintptr</a>)</pre>
  352. <p>
  353. SwapUintptr atomically stores new into *addr and returns the previous *addr value.
  354. </p>
  355. <h2 id="Value">type <a href="http://localhost:6060/src/sync/atomic/value.go?s=445:481#L5">Value</a></h2>
  356. <pre>type Value struct {
  357. <span class="comment">// contains filtered or unexported fields</span>
  358. }</pre>
  359. <p>
  360. A Value provides an atomic load and store of a consistently typed value.
  361. Values can be created as part of other data structures.
  362. The zero value for a Value returns nil from Load.
  363. Once Store has been called, a Value must not be copied.
  364. </p>
  365. <div id="example_Value_config" class="toggle">
  366. <div class="collapsed">
  367. <p class="exampleHeading toggleButton"><span class="text">Example (Config)</span></p>
  368. </div>
  369. <div class="expanded">
  370. <p class="exampleHeading toggleButton"><span class="text">Example (Config)</span></p>
  371. <p>The following example shows how to use Value for periodic program config updates
  372. and propagation of the changes to worker goroutines.
  373. </p>
  374. <p>Code:</p>
  375. <pre class="code">
  376. var config Value <span class="comment">// holds current server configuration</span>
  377. <span class="comment">// Create initial config value and store into config.</span>
  378. config.Store(loadConfig())
  379. go func() {
  380. <span class="comment">// Reload config every 10 seconds</span>
  381. <span class="comment">// and update config value with the new version.</span>
  382. for {
  383. time.Sleep(10 * time.Second)
  384. config.Store(loadConfig())
  385. }
  386. }()
  387. <span class="comment">// Create worker goroutines that handle incoming requests</span>
  388. <span class="comment">// using the latest config value.</span>
  389. for i := 0; i &lt; 10; i++ {
  390. go func() {
  391. for r := range requests() {
  392. c := config.Load()
  393. <span class="comment">// Handle request r using config c.</span>
  394. _, _ = r, c
  395. }
  396. }()
  397. }
  398. </pre>
  399. </div>
  400. </div>
  401. <div id="example_Value_readMostly" class="toggle">
  402. <div class="collapsed">
  403. <p class="exampleHeading toggleButton"><span class="text">Example (ReadMostly)</span></p>
  404. </div>
  405. <div class="expanded">
  406. <p class="exampleHeading toggleButton"><span class="text">Example (ReadMostly)</span></p>
  407. <p>The following example shows how to maintain a scalable frequently read,
  408. but infrequently updated data structure using copy-on-write idiom.
  409. </p>
  410. <p>Code:</p>
  411. <pre class="code">
  412. type Map map[string]string
  413. var m Value
  414. m.Store(make(Map))
  415. var mu sync.Mutex <span class="comment">// used only by writers</span>
  416. <span class="comment">// read function can be used to read the data without further synchronization</span>
  417. read := func(key string) (val string) {
  418. m1 := m.Load().(Map)
  419. return m1[key]
  420. }
  421. <span class="comment">// insert function can be used to update the data without further synchronization</span>
  422. insert := func(key, val string) {
  423. mu.Lock() <span class="comment">// synchronize with other potential writers</span>
  424. defer mu.Unlock()
  425. m1 := m.Load().(Map) <span class="comment">// load current value of the data structure</span>
  426. m2 := make(Map) <span class="comment">// create a new value</span>
  427. for k, v := range m1 {
  428. m2[k] = v <span class="comment">// copy all data from the current object to the new one</span>
  429. }
  430. m2[key] = val <span class="comment">// do the update that we need</span>
  431. m.Store(m2) <span class="comment">// atomically replace the current object with the new one</span>
  432. <span class="comment">// At this point all new readers start working with the new version.</span>
  433. <span class="comment">// The old version will be garbage collected once the existing readers</span>
  434. <span class="comment">// (if any) are done with it.</span>
  435. }
  436. _, _ = read, insert
  437. </pre>
  438. </div>
  439. </div>
  440. <h3 id="Value.Load">func (*Value) <a href="http://localhost:6060/src/sync/atomic/value.go?s=732:770#L17">Load</a></h3>
  441. <pre>func (v *<a href="index.html#Value">Value</a>) Load() (x interface{})</pre>
  442. <p>
  443. Load returns the value set by the most recent Store.
  444. It returns nil if there has been no call to Store for this Value.
  445. </p>
  446. <h3 id="Value.Store">func (*Value) <a href="http://localhost:6060/src/sync/atomic/value.go?s=1242:1278#L34">Store</a></h3>
  447. <pre>func (v *<a href="index.html#Value">Value</a>) Store(x interface{})</pre>
  448. <p>
  449. Store sets the value of the Value to x.
  450. All calls to Store for a given Value must use values of the same concrete type.
  451. Store of an inconsistent type panics, as does Store(nil).
  452. </p>
  453. <h2 id="pkg-note-BUG">Bugs</h2>
  454. <ul style="list-style: none; padding: 0;">
  455. <li><a href="http://localhost:6060/src/sync/atomic/doc.go?s=1207:1656#L36">&#x261e;</a> On x86-32, the 64-bit functions use instructions unavailable before the Pentium MMX.
  456. On non-Linux ARM, the 64-bit functions use instructions unavailable before the ARMv6k core.
  457. On both ARM and x86-32, it is the caller&#39;s responsibility to arrange for 64-bit
  458. alignment of 64-bit words accessed atomically. The first word in a global
  459. variable or in an allocated struct or slice can be relied upon to be
  460. 64-bit aligned.
  461. </li>
  462. </ul>
  463. <div id="footer">
  464. Build version go1.6.<br>
  465. Except as <a href="https://developers.google.com/site-policies#restrictions">noted</a>,
  466. the content of this page is licensed under the
  467. Creative Commons Attribution 3.0 License,
  468. and code is licensed under a <a href="http://localhost:6060/LICENSE">BSD license</a>.<br>
  469. <a href="http://localhost:6060/doc/tos.html">Terms of Service</a> |
  470. <a href="http://www.google.com/intl/en/policies/privacy/">Privacy Policy</a>
  471. </div>
  472. </div><!-- .container -->
  473. </div><!-- #page -->
  474. <!-- TODO(adonovan): load these from <head> using "defer" attribute? -->
  475. <script type="text/javascript" src="../../../lib/godoc/jquery.js"></script>
  476. <script type="text/javascript" src="../../../lib/godoc/jquery.treeview.js"></script>
  477. <script type="text/javascript" src="../../../lib/godoc/jquery.treeview.edit.js"></script>
  478. <script type="text/javascript" src="../../../lib/godoc/godocs.js"></script>
  479. </body>
  480. </html>