Diligent Engine API Reference
BasicAtomics.h
1 /* Copyright 2015-2018 Egor Yusov
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
10  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
11  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS.
12  *
13  * In no event and under no legal theory, whether in tort (including negligence),
14  * contract, or otherwise, unless required by applicable law (such as deliberate
15  * and grossly negligent acts) or agreed to in writing, shall any Contributor be
16  * liable for any damages, including any direct, indirect, special, incidental,
17  * or consequential damages of any character arising as a result of this License or
18  * out of the use or inability to use the software (including but not limited to damages
19  * for loss of goodwill, work stoppage, computer failure or malfunction, or any and
20  * all other commercial damages or losses), even if such Contributor has been advised
21  * of the possibility of such damages.
22  */
23 
24 #pragma once
25 
26 #include <atomic>
27 
28 struct BasicAtomics
29 {
30  typedef long Long;
31  typedef std::atomic<Long> AtomicLong;
32  typedef std::atomic<int64_t> AtomicInt64;
33 
34  // The function returns the resulting INCREMENTED value.
35  template<typename Type>
36  static inline Type AtomicIncrement(std::atomic<Type> &Val)
37  {
38  return ++Val;
39  }
40 
41  // The function returns the resulting DECREMENTED value.
42  template<typename Type>
43  static inline Type AtomicDecrement(std::atomic<Type> &Val)
44  {
45  return --Val;
46  }
47 
48  // The function compares the Destination value with the Comparand value. If the Destination value is equal
49  // to the Comparand value, the Exchange value is stored in the address specified by Destination.
50  // Otherwise, no operation is performed.
51  // The function returns the initial value of the Destination parameter
52  template<typename Type>
53  static inline Type AtomicCompareExchange( std::atomic<Type> &Destination, Type Exchange, Type Comparand)
54  {
55  Destination.compare_exchange_strong(Comparand, Exchange);
56  return Comparand;
57  }
58 
59  template<typename Type>
60  static inline Type AtomicAdd( std::atomic<Type> &Destination, Type Val)
61  {
62  return std::atomic_fetch_add(&Destination, Val);
63  }
64 
65 };