diff --git a/uvloop/cbhandles.pyx b/uvloop/cbhandles.pyx index 2914b42e..00070816 100644 --- a/uvloop/cbhandles.pyx +++ b/uvloop/cbhandles.pyx @@ -9,8 +9,10 @@ cdef class Handle: cdef inline _set_loop(self, Loop loop): self.loop = loop if UVLOOP_DEBUG: - loop._debug_cb_handles_total += 1 - loop._debug_cb_handles_count += 1 + system.__atomic_fetch_add( + &loop._debug_cb_handles_total, 1, system.__ATOMIC_RELAXED) + system.__atomic_fetch_add( + &loop._debug_cb_handles_count, 1, system.__ATOMIC_RELAXED) if loop._debug: self._source_traceback = extract_stack() @@ -21,7 +23,8 @@ cdef class Handle: def __dealloc__(self): if UVLOOP_DEBUG and self.loop is not None: - self.loop._debug_cb_handles_count -= 1 + system.__atomic_fetch_sub( + &self.loop._debug_cb_handles_count, 1, system.__ATOMIC_RELAXED) if self.loop is None: raise RuntimeError('Handle.loop is None in Handle.__dealloc__') @@ -174,8 +177,10 @@ cdef class TimerHandle: self._cancelled = 0 if UVLOOP_DEBUG: - self.loop._debug_cb_timer_handles_total += 1 - self.loop._debug_cb_timer_handles_count += 1 + system.__atomic_fetch_add( + &self.loop._debug_cb_timer_handles_total, 1, system.__ATOMIC_RELAXED) + system.__atomic_fetch_add( + &self.loop._debug_cb_timer_handles_count, 1, system.__ATOMIC_RELAXED) if context is None: context = Context_CopyCurrent() @@ -205,7 +210,8 @@ cdef class TimerHandle: def __dealloc__(self): if UVLOOP_DEBUG: - self.loop._debug_cb_timer_handles_count -= 1 + system.__atomic_fetch_sub( + &self.loop._debug_cb_timer_handles_count, 1, system.__ATOMIC_RELAXED) if self.timer is not None: raise RuntimeError('active TimerHandle is deallacating') diff --git a/uvloop/includes/system.pxd b/uvloop/includes/system.pxd index 367fedd1..89d0e327 100644 --- a/uvloop/includes/system.pxd +++ b/uvloop/includes/system.pxd @@ -94,3 +94,11 @@ cdef extern from "includes/fork_handler.h": void setForkHandler(OnForkHandler handler) void resetForkHandler() void setMainThreadID(uint64_t id) + + +cdef extern from * nogil: + uint64_t __atomic_fetch_add(uint64_t *ptr, uint64_t val, int memorder) + uint64_t __atomic_fetch_sub(uint64_t *ptr, uint64_t val, int memorder) + + cdef enum: + __ATOMIC_RELAXED