File tree Expand file tree Collapse file tree 1 file changed +19
-10
lines changed Expand file tree Collapse file tree 1 file changed +19
-10
lines changed Original file line number Diff line number Diff line change @@ -9,7 +9,8 @@ module GC
9
9
10
10
using .. C: C
11
11
12
- const QUEUE = Channel {C.PyPtr} (Inf )
12
+ const QUEUE = C. PyPtr[]
13
+ const QUEUE_LOCK = Threads. SpinLock ()
13
14
const HOOK = WeakRef ()
14
15
15
16
"""
@@ -55,22 +56,28 @@ function gc()
55
56
end
56
57
57
58
function unsafe_free_queue ()
58
- while isready (QUEUE )
59
- ptr = take! ( QUEUE)
59
+ lock (QUEUE_LOCK )
60
+ for ptr in QUEUE
60
61
if ptr != C. PyNULL
61
62
C. Py_DecRef (ptr)
62
63
end
63
64
end
65
+ empty! (QUEUE)
66
+ unlock (QUEUE_LOCK)
64
67
nothing
65
68
end
66
69
67
70
function enqueue (ptr:: C.PyPtr )
68
71
if ptr != C. PyNULL && C. CTX. is_initialized
69
72
if C. PyGILState_Check () == 1
70
73
C. Py_DecRef (ptr)
71
- unsafe_free_queue ()
74
+ if ! isempty (QUEUE)
75
+ unsafe_free_queue ()
76
+ end
72
77
else
73
- put! (QUEUE, ptr)
78
+ lock (QUEUE_LOCK)
79
+ push! (QUEUE, ptr)
80
+ unlock (QUEUE_LOCK)
74
81
end
75
82
end
76
83
nothing
@@ -84,11 +91,13 @@ function enqueue_all(ptrs)
84
91
C. Py_DecRef (ptr)
85
92
end
86
93
end
87
- unsafe_free_queue ()
88
- else
89
- for ptr in ptrs
90
- put! (QUEUE, ptr)
94
+ if ! isempty (QUEUE)
95
+ unsafe_free_queue ()
91
96
end
97
+ else
98
+ lock (QUEUE_LOCK)
99
+ append! (QUEUE, ptrs)
100
+ unlock (QUEUE_LOCK)
92
101
end
93
102
end
94
103
nothing
113
122
function _gchook_finalizer (x)
114
123
if C. CTX. is_initialized
115
124
finalizer (_gchook_finalizer, x)
116
- if isready (QUEUE) && C. PyGILState_Check () == 1
125
+ if ! isempty (QUEUE) && C. PyGILState_Check () == 1
117
126
unsafe_free_queue ()
118
127
end
119
128
end
You can’t perform that action at this time.
0 commit comments