blob: d816e19b39650705ff98cc2cf3f6f1c869647e41 [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 <libkern/OSAtomic.h>
15#include <stdlib.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 OSAtomicIncrement32Barrier(&value_);
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000031}
32
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000033WebRtc_Word32 Atomic32::operator--() {
34 return OSAtomicDecrement32Barrier(&value_);
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 return OSAtomicAdd32Barrier(value, &value_);
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000039}
40
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000041WebRtc_Word32 Atomic32::operator-=(WebRtc_Word32 value) {
42 return OSAtomicAdd32Barrier(-value, &value_);
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000043}
44
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000045bool Atomic32::CompareExchange(WebRtc_Word32 new_value,
46 WebRtc_Word32 compare_value) {
47 return OSAtomicCompareAndSwap32Barrier(compare_value, new_value, &value_);
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000048}
49
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000050WebRtc_Word32 Atomic32::Value() const {
51 return value_;
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000052}
phoglund@webrtc.org9cb9fc12012-11-09 08:57:25 +000053
tommi@webrtc.orge84373c2012-04-19 14:28:45 +000054} // namespace webrtc