Skip to content

Commit 836e3b2

Browse files
authored
test: use C __atomic builtins for debug counters (#719)
1 parent fd65027 commit 836e3b2

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

uvloop/cbhandles.pyx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ cdef class Handle:
99
cdef inline _set_loop(self, Loop loop):
1010
self.loop = loop
1111
if UVLOOP_DEBUG:
12-
loop._debug_cb_handles_total += 1
13-
loop._debug_cb_handles_count += 1
12+
system.__atomic_fetch_add(
13+
&loop._debug_cb_handles_total, 1, system.__ATOMIC_RELAXED)
14+
system.__atomic_fetch_add(
15+
&loop._debug_cb_handles_count, 1, system.__ATOMIC_RELAXED)
1416
if loop._debug:
1517
self._source_traceback = extract_stack()
1618

@@ -21,7 +23,8 @@ cdef class Handle:
2123

2224
def __dealloc__(self):
2325
if UVLOOP_DEBUG and self.loop is not None:
24-
self.loop._debug_cb_handles_count -= 1
26+
system.__atomic_fetch_sub(
27+
&self.loop._debug_cb_handles_count, 1, system.__ATOMIC_RELAXED)
2528
if self.loop is None:
2629
raise RuntimeError('Handle.loop is None in Handle.__dealloc__')
2730

@@ -174,8 +177,10 @@ cdef class TimerHandle:
174177
self._cancelled = 0
175178

176179
if UVLOOP_DEBUG:
177-
self.loop._debug_cb_timer_handles_total += 1
178-
self.loop._debug_cb_timer_handles_count += 1
180+
system.__atomic_fetch_add(
181+
&self.loop._debug_cb_timer_handles_total, 1, system.__ATOMIC_RELAXED)
182+
system.__atomic_fetch_add(
183+
&self.loop._debug_cb_timer_handles_count, 1, system.__ATOMIC_RELAXED)
179184

180185
if context is None:
181186
context = Context_CopyCurrent()
@@ -205,7 +210,8 @@ cdef class TimerHandle:
205210

206211
def __dealloc__(self):
207212
if UVLOOP_DEBUG:
208-
self.loop._debug_cb_timer_handles_count -= 1
213+
system.__atomic_fetch_sub(
214+
&self.loop._debug_cb_timer_handles_count, 1, system.__ATOMIC_RELAXED)
209215
if self.timer is not None:
210216
raise RuntimeError('active TimerHandle is deallacating')
211217

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)