summaryrefslogtreecommitdiff
path: root/include/tools/atomic.h
blob: 4e619eeb413f37aed26cea98b4aa9c5e39ae5493 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
 * atomic.h
 *
 *  Created on: 06.11.2012
 *      Author: savop
 */

#ifndef ATOMIC_H_
#define ATOMIC_H_

#include <vdr/thread.h>

namespace upnp {

namespace tools {

namespace atomic {

class AtomicInteger {
private:
  uint32_t value;
  cMutex mutex;

public:
  AtomicInteger()
  : value(0)
  {}

  inline void increment() {
    mutex.Lock();
    ++value;
    mutex.Unlock();
  }

  inline void decrement() {
    mutex.Lock();
    --value;
    mutex.Unlock();
  }

  operator unsigned int() const {
    return Get();
  }

  unsigned int Get() const {
    return value;
  }

  AtomicInteger& operator++(){
    increment();
    return *this;
  }

  AtomicInteger operator++(int){
    increment();
    return *this;
  }

  AtomicInteger& operator--(){
    decrement();
    return *this;
  }

  AtomicInteger operator--(int){
    decrement();
    return *this;
  }

};

}  // namespace atomic

}  // namespace tools

}  // namespace upnp


#endif /* ATOMIC_H_ */