blob: d3728465d147dbf1a4ca0d6771a0aacf86e3bc7e [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
pbos@webrtc.orgacaf3a12013-05-27 15:07:45 +000011#include "webrtc/system_wrappers/interface/atomic32.h"
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000012
13#include <assert.h>
14#include <libkern/OSAtomic.h>
15#include <stdlib.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 OSAtomicIncrement32Barrier(&value_);
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 OSAtomicDecrement32Barrier(&value_);
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) {
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000038 return OSAtomicAdd32Barrier(value, &value_);
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000039}
40
pbos@webrtc.org046deb92013-04-09 09:06:11 +000041int32_t Atomic32::operator-=(int32_t value) {
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000042 return OSAtomicAdd32Barrier(-value, &value_);
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000043}
44
pbos@webrtc.org046deb92013-04-09 09:06:11 +000045bool Atomic32::CompareExchange(int32_t new_value, int32_t compare_value) {
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000046 return OSAtomicCompareAndSwap32Barrier(compare_value, new_value, &value_);
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000047}
48
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000049} // namespace webrtc