HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
atomic.h
Go to the documentation of this file.
1 /*
2  Copyright 2008 Larry Gritz and the other authors and contributors.
3  All Rights Reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are
7  met:
8  * Redistributions of source code must retain the above copyright
9  notice, this list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright
11  notice, this list of conditions and the following disclaimer in the
12  documentation and/or other materials provided with the distribution.
13  * Neither the name of the software's owners nor the names of its
14  contributors may be used to endorse or promote products derived from
15  this software without specific prior written permission.
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28  (This is the Modified BSD License)
29 */
30 
31 
32 /////////////////////////////////////////////////////////////////////////
33 /// @file atomic.h
34 ///
35 /// @brief Wrappers and utilities for atomics.
36 /////////////////////////////////////////////////////////////////////////
37 
38 #pragma once
39 
41 #include <OpenImageIO/platform.h>
42 
43 #include <atomic>
44 #define OIIO_USE_STDATOMIC 1
45 
46 
47 
49 
50 using std::atomic;
51 typedef atomic<int> atomic_int;
52 typedef atomic<long long> atomic_ll;
53 
54 
55 
56 /// Atomically set avar to the minimum of its current value and bval.
57 template<typename T>
59 atomic_min(atomic<T>& avar, const T& bval)
60 {
61  do {
62  T a = avar.load();
63  if (a <= bval || avar.compare_exchange_weak(a, bval))
64  break;
65  } while (true);
66 }
67 
68 
69 /// Atomically set avar to the maximum of its current value and bval.
70 template<typename T>
72 atomic_max(atomic<T>& avar, const T& bval)
73 {
74  do {
75  T a = avar.load();
76  if (a >= bval || avar.compare_exchange_weak(a, bval))
77  break;
78  } while (true);
79 }
80 
81 
82 
83 // Add atomically to a float and return the original value.
84 OIIO_FORCEINLINE float
85 atomic_fetch_add(atomic<float>& a, float f)
86 {
87  do {
88  float oldval = a.load();
89  float newval = oldval + f;
90  if (a.compare_exchange_weak(oldval, newval))
91  return oldval;
92  } while (true);
93 }
94 
95 
96 // Add atomically to a double and return the original value.
97 OIIO_FORCEINLINE double
98 atomic_fetch_add(atomic<double>& a, double f)
99 {
100  do {
101  double oldval = a.load();
102  double newval = oldval + f;
103  if (a.compare_exchange_weak(oldval, newval))
104  return oldval;
105  } while (true);
106 }
107 
108 
OIIO_FORCEINLINE void atomic_max(atomic< T > &avar, const T &bval)
Atomically set avar to the maximum of its current value and bval.
Definition: atomic.h:72
#define OIIO_FORCEINLINE
Definition: platform.h:249
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:9477
atomic< long long > atomic_ll
Definition: atomic.h:52
Platform-related macros.
atomic< int > atomic_int
Definition: atomic.h:51
GLclampf f
Definition: glew.h:3499
#define OIIO_NAMESPACE_END
Definition: oiioversion.h:66
OIIO_FORCEINLINE float atomic_fetch_add(atomic< float > &a, float f)
Definition: atomic.h:85
#define OIIO_NAMESPACE_BEGIN
Definition: oiioversion.h:65
OIIO_FORCEINLINE void atomic_min(atomic< T > &avar, const T &bval)
Atomically set avar to the minimum of its current value and bval.
Definition: atomic.h:59