Skip to content

Commit 39111e9

Browse files
committed
debug: use C __atomic builtins for counters
1 parent fd65027 commit 39111e9

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

uvloop/cbhandles.pyx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ cdef class Handle:
1010
self.loop = loop
1111
if UVLOOP_DEBUG:
1212
loop._debug_cb_handles_total += 1
13-
loop._debug_cb_handles_count += 1
13+
system.__atomic_fetch_add(
14+
&loop._debug_cb_handles_count, 1, system.__ATOMIC_RELAXED)
1415
if loop._debug:
1516
self._source_traceback = extract_stack()
1617

@@ -21,7 +22,8 @@ cdef class Handle:
2122

2223
def __dealloc__(self):
2324
if UVLOOP_DEBUG and self.loop is not None:
24-
self.loop._debug_cb_handles_count -= 1
25+
system.__atomic_fetch_sub(
26+
&self.loop._debug_cb_handles_count, 1, system.__ATOMIC_RELAXED)
2527
if self.loop is None:
2628
raise RuntimeError('Handle.loop is None in Handle.__dealloc__')
2729

uvloop/includes/system.pxd

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,11 @@ cdef extern from "includes/fork_handler.h":
9494
void setForkHandler(OnForkHandler handler)
9595
void resetForkHandler()
9696
void setMainThreadID(uint64_t id)
97+
98+
99+
cdef extern from * nogil:
100+
uint64_t __atomic_fetch_add(uint64_t *ptr, uint64_t val, int memorder)
101+
uint64_t __atomic_fetch_sub(uint64_t *ptr, uint64_t val, int memorder)
102+
103+
cdef enum:
104+
__ATOMIC_RELAXED

0 commit comments

Comments
 (0)