blob: 8a52617efeae88f420df8508b4c39b8f72b6aaf8 [file] [log] [blame]
tommi@webrtc.orge84373c2012-04-19 14:28:45 +00001/*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "atomic32.h"
12
13#include <assert.h>
14#include <inttypes.h>
15#include <malloc.h>
16
17#include "common_types.h"
18
19namespace webrtc {
20
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000021Atomic32::Atomic32(WebRtc_Word32 initial_value)
22 : value_(initial_value) {
23 assert(Is32bitAligned());
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000024}
25
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000026Atomic32::~Atomic32() {
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000027}
28
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000029WebRtc_Word32 Atomic32::operator++() {
30 return __sync_fetch_and_add(&value_, 1) + 1;
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000031}
32
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000033WebRtc_Word32 Atomic32::operator--() {
34 return __sync_fetch_and_sub(&value_, 1) - 1;
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000035}
36
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000037WebRtc_Word32 Atomic32::operator+=(WebRtc_Word32 value) {
38 WebRtc_Word32 return_value = __sync_fetch_and_add(&value_, value);
39 return_value += value;
40 return return_value;
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000041}
42
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000043WebRtc_Word32 Atomic32::operator-=(WebRtc_Word32 value) {
44 WebRtc_Word32 return_value = __sync_fetch_and_sub(&value_, value);
45 return_value -= value;
46 return return_value;
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000047}
48
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000049bool Atomic32::CompareExchange(WebRtc_Word32 new_value,
50 WebRtc_Word32 compare_value) {
51 return __sync_bool_compare_and_swap(&value_, compare_value, new_value);
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000052}
53
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000054WebRtc_Word32 Atomic32::Value() const {
55 return value_;
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000056}
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000057
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000058} // namespace webrtc