blob: fbc9c2910e45f50b79b869bffd80388468597b1b [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
Henrik Kjellander98f53512015-10-28 18:17:40 +010011#include "webrtc/system_wrappers/include/atomic32.h"
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000012
13#include <assert.h>
14#include <inttypes.h>
15#include <malloc.h>
16
pbos@webrtc.orgacaf3a12013-05-27 15:07:45 +000017#include "webrtc/common_types.h"
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000018
19namespace webrtc {
20
pbos@webrtc.org046deb92013-04-09 09:06:11 +000021Atomic32::Atomic32(int32_t initial_value)
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000022 : 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
pbos@webrtc.org046deb92013-04-09 09:06:11 +000029int32_t Atomic32::operator++() {
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000030 return __sync_fetch_and_add(&value_, 1) + 1;
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000031}
32
pbos@webrtc.org046deb92013-04-09 09:06:11 +000033int32_t Atomic32::operator--() {
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000034 return __sync_fetch_and_sub(&value_, 1) - 1;
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000035}
36
pbos@webrtc.org046deb92013-04-09 09:06:11 +000037int32_t Atomic32::operator+=(int32_t value) {
38 int32_t return_value = __sync_fetch_and_add(&value_, value);
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000039 return_value += value;
40 return return_value;
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000041}
42
pbos@webrtc.org046deb92013-04-09 09:06:11 +000043int32_t Atomic32::operator-=(int32_t value) {
44 int32_t return_value = __sync_fetch_and_sub(&value_, value);
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000045 return_value -= value;
46 return return_value;
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000047}
48
pbos@webrtc.org046deb92013-04-09 09:06:11 +000049bool Atomic32::CompareExchange(int32_t new_value, int32_t compare_value) {
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000050 return __sync_bool_compare_and_swap(&value_, compare_value, new_value);
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000051}
52
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +000053} // namespace webrtc