blob: 4b95967008cf4af665c3a66c1b33a5b1a5f9e838 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "system_wrappers/include/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
Mirko Bonadei71207422017-09-15 13:58:09 +020017#include "common_types.h" // NOLINT(build/include)
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