aboutsummaryrefslogtreecommitdiff
path: root/src/system_wrappers/source/atomic32_linux.h
blob: f9f5650f2a78f08db7ed544e0500d9f8ad4dfcf5 (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
/*
 *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

// Atomic system independant 32-bit signed integer.
// Linux implementation.
// Note: Requires gcc 4.1.2 or later.
#ifndef WEBRTC_SYSTEM_WRAPPERS_SOURCE_ATOMIC32_LINUX_H_
#define WEBRTC_SYSTEM_WRAPPERS_SOURCE_ATOMIC32_LINUX_H_

#include <inttypes.h>
#include <malloc.h>

#include "common_types.h"

namespace webrtc {
class Atomic32Impl
{
public:
    inline Atomic32Impl(WebRtc_Word32 initialValue);
    inline ~Atomic32Impl();

    inline WebRtc_Word32 operator++();
    inline WebRtc_Word32 operator--();

    inline Atomic32Impl& operator=(const Atomic32Impl& rhs);
    inline Atomic32Impl& operator=(WebRtc_Word32 rhs);
    inline WebRtc_Word32 operator+=(WebRtc_Word32 rhs);
    inline WebRtc_Word32 operator-=(WebRtc_Word32 rhs);

    inline bool CompareExchange(WebRtc_Word32 newValue,
                                WebRtc_Word32 compareValue);

    inline WebRtc_Word32 Value() const;
private:
    void*        _ptrMemory;
    // Volatile ensures full memory barriers.
    volatile WebRtc_Word32* _value;
};

// TODO (hellner) use aligned_malloc instead of doing it manually.
inline Atomic32Impl::Atomic32Impl(WebRtc_Word32 initialValue)
    : _ptrMemory(NULL),
      _value(NULL)
{   // Align the memory associated with _value on a 32-bit boundary. This is a
    // requirement for the used Linux APIs to be atomic.
    // Keep _ptrMemory to be able to reclaim memory.
    _ptrMemory = malloc(sizeof(WebRtc_Word32)*2);
    _value = (WebRtc_Word32*) (((uintptr_t)_ptrMemory+3)&(~0x3));
    *_value = initialValue;
}

inline Atomic32Impl::~Atomic32Impl()
{
    if(_ptrMemory != NULL)
    {
        free(_ptrMemory);
    }
}

inline WebRtc_Word32 Atomic32Impl::operator++()
{
    WebRtc_Word32 returnValue = __sync_fetch_and_add(_value,1);
    returnValue++;
    return returnValue;
}

inline WebRtc_Word32 Atomic32Impl::operator--()
{
    WebRtc_Word32 returnValue = __sync_fetch_and_sub(_value,1);
    returnValue--;
    return returnValue;
}

inline Atomic32Impl& Atomic32Impl::operator=(const Atomic32Impl& rhs)
{
    *_value = *rhs._value;
    return *this;
}

inline Atomic32Impl& Atomic32Impl::operator=(WebRtc_Word32 rhs)
{
    *_value = rhs;
    return *this;
}

inline WebRtc_Word32 Atomic32Impl::operator+=(WebRtc_Word32 rhs)
{
    WebRtc_Word32 returnValue = __sync_fetch_and_add(_value,rhs);
    returnValue += rhs;
    return returnValue;
}

inline WebRtc_Word32 Atomic32Impl::operator-=(WebRtc_Word32 rhs)
{
    WebRtc_Word32 returnValue = __sync_fetch_and_sub(_value,rhs);
    returnValue -= rhs;
    return returnValue;
}

inline bool Atomic32Impl::CompareExchange(WebRtc_Word32 newValue,
                                          WebRtc_Word32 compareValue)
{
    return __sync_bool_compare_and_swap(_value,compareValue,newValue);
}

inline WebRtc_Word32 Atomic32Impl::Value() const
{
    return *_value;
}
} // namespace webrtc

#endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_ATOMIC32_LINUX_H_