1e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org/*
2e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org *
4e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org *  Use of this source code is governed by a BSD-style license
5e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org *  that can be found in the LICENSE file in the root of the source
6e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org *  tree. An additional intellectual property rights grant can be found
7e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org *  in the file PATENTS.  All contributing project authors may
8e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
9e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org */
10e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org
1198f53510b222f71fdd8b799b2f33737ceeb28c61Henrik Kjellander#include "webrtc/system_wrappers/include/atomic32.h"
12e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org
13e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org#include <assert.h>
14e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org#include <inttypes.h>
15e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org#include <malloc.h>
16e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org
17acaf3a1b131e049000552c53c8cbfe737e2d8f58pbos@webrtc.org#include "webrtc/common_types.h"
18e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org
19e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.orgnamespace webrtc {
20e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org
21046deb9b2050ebdf98a41e2d22f852e104dd365apbos@webrtc.orgAtomic32::Atomic32(int32_t initial_value)
229cb9fc17b1e9484f7628eddfa8b23b3a7ff8b0daphoglund@webrtc.org    : value_(initial_value) {
239cb9fc17b1e9484f7628eddfa8b23b3a7ff8b0daphoglund@webrtc.org  assert(Is32bitAligned());
24e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org}
25e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org
269cb9fc17b1e9484f7628eddfa8b23b3a7ff8b0daphoglund@webrtc.orgAtomic32::~Atomic32() {
27e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org}
28e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org
29046deb9b2050ebdf98a41e2d22f852e104dd365apbos@webrtc.orgint32_t Atomic32::operator++() {
309cb9fc17b1e9484f7628eddfa8b23b3a7ff8b0daphoglund@webrtc.org  return __sync_fetch_and_add(&value_, 1) + 1;
31e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org}
32e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org
33046deb9b2050ebdf98a41e2d22f852e104dd365apbos@webrtc.orgint32_t Atomic32::operator--() {
349cb9fc17b1e9484f7628eddfa8b23b3a7ff8b0daphoglund@webrtc.org  return __sync_fetch_and_sub(&value_, 1) - 1;
35e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org}
36e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org
37046deb9b2050ebdf98a41e2d22f852e104dd365apbos@webrtc.orgint32_t Atomic32::operator+=(int32_t value) {
38046deb9b2050ebdf98a41e2d22f852e104dd365apbos@webrtc.org  int32_t return_value = __sync_fetch_and_add(&value_, value);
399cb9fc17b1e9484f7628eddfa8b23b3a7ff8b0daphoglund@webrtc.org  return_value += value;
409cb9fc17b1e9484f7628eddfa8b23b3a7ff8b0daphoglund@webrtc.org  return return_value;
41e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org}
42e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org
43046deb9b2050ebdf98a41e2d22f852e104dd365apbos@webrtc.orgint32_t Atomic32::operator-=(int32_t value) {
44046deb9b2050ebdf98a41e2d22f852e104dd365apbos@webrtc.org  int32_t return_value = __sync_fetch_and_sub(&value_, value);
459cb9fc17b1e9484f7628eddfa8b23b3a7ff8b0daphoglund@webrtc.org  return_value -= value;
469cb9fc17b1e9484f7628eddfa8b23b3a7ff8b0daphoglund@webrtc.org  return return_value;
47e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org}
48e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org
49046deb9b2050ebdf98a41e2d22f852e104dd365apbos@webrtc.orgbool Atomic32::CompareExchange(int32_t new_value, int32_t compare_value) {
509cb9fc17b1e9484f7628eddfa8b23b3a7ff8b0daphoglund@webrtc.org  return __sync_bool_compare_and_swap(&value_, compare_value, new_value);
51e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org}
52e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org
53d900e8bea84c474696bf0219aed1353ce65ffd8epbos@webrtc.org}  // namespace webrtc
54