An atomic, thread-safe counter
Methods:
-
__init__ – Initialize a new atomic counter to given initial value
-
dec – Atomically decrement the counter by num and return the new value
-
inc – Atomically increment the counter by num and return the new value
Source code in vllm/utils/counter.py
| class AtomicCounter:
"""An atomic, thread-safe counter"""
def __init__(self, initial: int = 0) -> None:
"""Initialize a new atomic counter to given initial value"""
super().__init__()
self._value = initial
self._lock = threading.Lock()
@property
def value(self) -> int:
return self._value
def inc(self, num: int = 1) -> int:
"""Atomically increment the counter by num and return the new value"""
with self._lock:
self._value += num
return self._value
def dec(self, num: int = 1) -> int:
"""Atomically decrement the counter by num and return the new value"""
with self._lock:
self._value -= num
return self._value
|
__init__(initial=0)
Initialize a new atomic counter to given initial value
Source code in vllm/utils/counter.py
| def __init__(self, initial: int = 0) -> None:
"""Initialize a new atomic counter to given initial value"""
super().__init__()
self._value = initial
self._lock = threading.Lock()
|
dec(num=1)
Atomically decrement the counter by num and return the new value
Source code in vllm/utils/counter.py
| def dec(self, num: int = 1) -> int:
"""Atomically decrement the counter by num and return the new value"""
with self._lock:
self._value -= num
return self._value
|
inc(num=1)
Atomically increment the counter by num and return the new value
Source code in vllm/utils/counter.py
| def inc(self, num: int = 1) -> int:
"""Atomically increment the counter by num and return the new value"""
with self._lock:
self._value += num
return self._value
|