Debian Codegraph @

aws_xray_sdk/core/utils/atomic_counter.py

raw
1 import threading
2
3
4 class AtomicCounter(object):
5 """
6 A helper class that implements a thread-safe counter.
7 """
8 def __init__(self, initial=0):
9
10 self.value = initial
11 self._lock = threading.Lock()
12 self._initial = initial
13
14 def increment(self, num=1):
15
16 with self._lock:
17 self.value += num
18 return self.value
19
20 def decrement(self, num=1):
21
22 with self._lock:
23 self.value -= num
24 return self.value
25
26 def get_current(self):
27
28 with self._lock:
29 return self.value
30
31 def reset(self):
32
33 with self._lock:
34 self.value = self._initial
35 return self.value