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
11e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org// Atomic, system independent 32-bit integer.  Unless you know what you're
12e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org// doing, use locks instead! :-)
13e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org//
14e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org// Note: assumes 32-bit (or higher) system
15d56d68cd272783ae0cfe11834c2a141ec62519c2kjellander#ifndef WEBRTC_SYSTEM_WRAPPERS_INCLUDE_ATOMIC32_H_
16d56d68cd272783ae0cfe11834c2a141ec62519c2kjellander#define WEBRTC_SYSTEM_WRAPPERS_INCLUDE_ATOMIC32_H_
17e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org
1812dc1a38ca54a000e4fecfbc6d41138b895c9ca5pbos@webrtc.org#include <stddef.h>
19e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org
2088fbb2d86b33a3886bba1af4d098efa2c19eb1e7henrike@webrtc.org#include "webrtc/base/constructormagic.h"
21acaf3a1b131e049000552c53c8cbfe737e2d8f58pbos@webrtc.org#include "webrtc/common_types.h"
22e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org
23e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.orgnamespace webrtc {
24e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org
25e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org// 32 bit atomic variable.  Note that this class relies on the compiler to
26e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org// align the 32 bit value correctly (on a 32 bit boundary), so as long as you're
27e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org// not doing things like reinterpret_cast over some custom allocated memory
28e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org// without being careful with alignment, you should be fine.
299cb9fc17b1e9484f7628eddfa8b23b3a7ff8b0daphoglund@webrtc.orgclass Atomic32 {
309cb9fc17b1e9484f7628eddfa8b23b3a7ff8b0daphoglund@webrtc.org public:
31046deb9b2050ebdf98a41e2d22f852e104dd365apbos@webrtc.org  Atomic32(int32_t initial_value = 0);
329cb9fc17b1e9484f7628eddfa8b23b3a7ff8b0daphoglund@webrtc.org  ~Atomic32();
33e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org
349cb9fc17b1e9484f7628eddfa8b23b3a7ff8b0daphoglund@webrtc.org  // Prefix operator!
35046deb9b2050ebdf98a41e2d22f852e104dd365apbos@webrtc.org  int32_t operator++();
36046deb9b2050ebdf98a41e2d22f852e104dd365apbos@webrtc.org  int32_t operator--();
37e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org
38046deb9b2050ebdf98a41e2d22f852e104dd365apbos@webrtc.org  int32_t operator+=(int32_t value);
39046deb9b2050ebdf98a41e2d22f852e104dd365apbos@webrtc.org  int32_t operator-=(int32_t value);
40e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org
419cb9fc17b1e9484f7628eddfa8b23b3a7ff8b0daphoglund@webrtc.org  // Sets the value atomically to new_value if the value equals compare value.
429cb9fc17b1e9484f7628eddfa8b23b3a7ff8b0daphoglund@webrtc.org  // The function returns true if the exchange happened.
43046deb9b2050ebdf98a41e2d22f852e104dd365apbos@webrtc.org  bool CompareExchange(int32_t new_value, int32_t compare_value);
4482f014aa0bc225076516a3d77ad02deb69cfd809henrike@webrtc.org  int32_t Value() {
4582f014aa0bc225076516a3d77ad02deb69cfd809henrike@webrtc.org    return *this += 0;
4682f014aa0bc225076516a3d77ad02deb69cfd809henrike@webrtc.org  }
47e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org
489cb9fc17b1e9484f7628eddfa8b23b3a7ff8b0daphoglund@webrtc.org private:
499cb9fc17b1e9484f7628eddfa8b23b3a7ff8b0daphoglund@webrtc.org  // Disable the + and - operator since it's unclear what these operations
509cb9fc17b1e9484f7628eddfa8b23b3a7ff8b0daphoglund@webrtc.org  // should do.
519cb9fc17b1e9484f7628eddfa8b23b3a7ff8b0daphoglund@webrtc.org  Atomic32 operator+(const Atomic32& other);
529cb9fc17b1e9484f7628eddfa8b23b3a7ff8b0daphoglund@webrtc.org  Atomic32 operator-(const Atomic32& other);
53e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org
549cb9fc17b1e9484f7628eddfa8b23b3a7ff8b0daphoglund@webrtc.org  // Checks if |_value| is 32bit aligned.
559cb9fc17b1e9484f7628eddfa8b23b3a7ff8b0daphoglund@webrtc.org  inline bool Is32bitAligned() const {
569cb9fc17b1e9484f7628eddfa8b23b3a7ff8b0daphoglund@webrtc.org    return (reinterpret_cast<ptrdiff_t>(&value_) & 3) == 0;
579cb9fc17b1e9484f7628eddfa8b23b3a7ff8b0daphoglund@webrtc.org  }
58e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org
593c089d751ede283e21e186885eaf705c3257ccd2henrikg  RTC_DISALLOW_COPY_AND_ASSIGN(Atomic32);
60e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org
61046deb9b2050ebdf98a41e2d22f852e104dd365apbos@webrtc.org  int32_t value_;
62e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org};
639cb9fc17b1e9484f7628eddfa8b23b3a7ff8b0daphoglund@webrtc.org
64e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org}  // namespace webrtc
65e84373c38f0eb41ce457a2b69f6c5bf05c511f1btommi@webrtc.org
66d56d68cd272783ae0cfe11834c2a141ec62519c2kjellander#endif  // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_ATOMIC32_H_
67