FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
RustPython/crates/stdlib/src/_queue.rs at main · RustPython/RustPython · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
RustPython
/
RustPython
Public
Notifications
You must be signed in to change notification settings
Fork
1.5k
Star
22.3k
Code
Issues
295
Pull requests
99
Discussions
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
RustPython
/
crates
/
stdlib
/
src
/
_queue.rs
Copy path
More file actions
More file actions
Latest commit
History
History
History
354 lines (303 loc) · 11.1 KB
Breadcrumbs
RustPython
/
crates
/
stdlib
/
src
/
_queue.rs
Copy path
File metadata and controls
354 lines (303 loc) · 11.1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
pub
(
crate
)
use
_queue
::
module_def
;
#
[
pymodule
]
mod
_queue
{
use
alloc
::
collections
::
VecDeque
;
use
core
::
time
::
Duration
;
use
std
::
time
::
Instant
;
use
crate
::
vm
::
{
AsObject
,
Py
,
PyObject
,
PyObjectRef
,
PyPayload
,
PyRef
,
PyResult
,
VirtualMachine
,
builtins
::
{
PyBaseExceptionRef
,
PyException
,
PyGenericAlias
,
PyStr
,
PyType
,
PyTypeRef
}
,
function
::
{
PyComparisonValue
,
TimeoutSeconds
}
,
protocol
::
PyNumberMethods
,
types
::
{
AsNumber
,
Comparable
,
Constructor
,
PyComparisonOp
,
Representable
}
,
}
;
type
BufInner
=
VecDeque
<
PyObjectRef
>
;
cfg_select
!
{
feature =
"threading"
=>
{
use
parking_lot
::
{
Condvar
,
Mutex
,
MutexGuard
}
;
type
Buf
=
Mutex
<
BufInner
>
;
}
,
_ =>
{
use
crate
::
common
::
lock
::
PyMutex
;
type
Buf
=
PyMutex
<
BufInner
>
;
}
}
const
INITIAL_RING_BUF_CAPACITY
:
usize
=
8
;
/// `parking_lot`'s `Condvar` doesn't expose a mid-wait signal to us (unlike
/// CPython's raw `sem_timedwait`, which reports `EINTR`), so we poll instead.
// FIXME: interim stopgap. The signal already interrupts the wait with EINTR
// (SA_RESTART cleared via `siginterrupt`), but `parking_lot::Condvar` swallows
// it and re-parks, forcing this poll. Replace with a shared interruptible
// timed-wait that surfaces EINTR (`poll`/wakeup-fd, portable incl. macOS; or
// `sem_timedwait` where available) -- `_thread` lock and `Thread.join` share
// this defect. Then this constant and the chunking loop go away.
#
[
cfg
(
feature =
"threading"
)
]
const
SIGNAL_CHECK_INTERVAL
:
Duration
=
Duration
::
from_millis
(
50
)
;
#
[
pyattr
]
#
[
pyclass
(
module =
"_queue"
,
name =
"Empty"
,
base =
PyException
)
]
#
[
repr
(
transparent
)
]
pub
(
crate
)
struct
PyEmptyError
(
PyException
)
;
#
[
pyclass
(
flags
(
HAS_WEAKREF
)
)
]
impl
PyEmptyError
{
}
/// ## See Also
///
/// [`empty_error`](https://github.com/python/cpython/blob/v3.14.5/Modules/_queuemodule.c#L347-L355).
fn
empty_error
(
vm
:
&
VirtualMachine
)
->
PyBaseExceptionRef
{
vm
.
new_exception_empty
(
PyEmptyError
::
class
(
&
vm
.
ctx
)
.
to_owned
(
)
)
}
#
[
cfg
(
feature =
"threading"
)
]
#
[
derive
(
Debug
)
]
struct
Semaphore
{
mutex
:
Mutex
<
usize
>
,
cond
:
Condvar
,
}
#
[
cfg
(
feature =
"threading"
)
]
impl
Semaphore
{
#
[
must_use
]
fn
new
(
)
->
Self
{
Self
{
mutex
:
Mutex
::
new
(
0
)
,
cond
:
Condvar
::
new
(
)
,
}
}
/// Take `mutex`, detaching first so that blocking on it cannot stall a
/// stop-the-world request.
///
/// A waiter holds this mutex across its `allow_threads` wait, so it can
/// still hold it when it is stopped. An attached thread blocking on it
/// would then never reach a safepoint, the stop would never complete,
/// and the holder would never be resumed to release it.
fn
lock_count
(
&
self
,
vm
:
&
VirtualMachine
)
-> parking_lot
::
MutexGuard
<
'
_
,
usize
>
{
vm
.
allow_threads
(
||
self
.
mutex
.
lock
(
)
)
}
fn
release
(
&
self
,
vm
:
&
VirtualMachine
)
{
{
let
mut
count =
self
.
lock_count
(
vm
)
;
*
count +=
1
;
}
// lock dropped. now we can notify a waiting thread
self
.
cond
.
notify_one
(
)
;
}
/// `Ok(true)` if acquired, `Ok(false)` on timeout, `Err` if a signal
/// handler raised (e.g. `KeyboardInterrupt`) while we were waiting.
fn
acquire
(
&
self
,
block
:
bool
,
deadline
:
Option
<
Instant
>
,
vm
:
&
VirtualMachine
,
)
->
PyResult
<
bool
>
{
loop
{
// Guard must be dropped before check_signals() below, since a
// signal handler may call back into this same queue.
{
let
mut
count =
self
.
lock_count
(
vm
)
;
if
*
count >
0
{
*
count -=
1
;
return
Ok
(
true
)
;
}
if
!block
{
return
Ok
(
false
)
;
}
let
now =
Instant
::
now
(
)
;
let
chunk_deadline = deadline
.
map_or_else
(
|| now +
SIGNAL_CHECK_INTERVAL
,
|dl| dl
.
min
(
now +
SIGNAL_CHECK_INTERVAL
)
,
)
;
vm
.
allow_threads
(
||
self
.
cond
.
wait_until
(
&
mut
count
,
chunk_deadline
)
)
;
if
*
count >
0
{
*
count -=
1
;
return
Ok
(
true
)
;
}
if
let
Some
(
dl
)
= deadline
&&
Instant
::
now
(
)
>= dl
{
return
Ok
(
false
)
;
}
}
vm
.
check_signals
(
)
?
;
}
}
}
#
[
pyattr
]
#
[
pyclass
(
module =
"_queue"
,
name =
"SimpleQueue"
,
unhashable =
true
)
]
#
[
derive
(
Debug
,
PyPayload
)
]
struct
PySimpleQueue
{
buf
:
Buf
,
#
[
cfg
(
feature =
"threading"
)
]
sem
:
Semaphore
,
}
impl
Default
for
PySimpleQueue
{
fn
default
(
)
->
Self
{
Self
{
buf
:
Buf
::
new
(
VecDeque
::
with_capacity
(
INITIAL_RING_BUF_CAPACITY
)
)
,
#
[
cfg
(
feature =
"threading"
)
]
sem
:
Semaphore
::
new
(
)
,
}
}
}
impl
PySimpleQueue
{
#
[
cfg_attr
(
not
(
feature =
"threading"
)
,
expect
(
unused_variables
,
reason =
"only the semaphore needs the vm"
)
)
]
fn
push
(
&
self
,
item
:
PyObjectRef
,
vm
:
&
VirtualMachine
)
{
self
.
buf
.
lock
(
)
.
push_back
(
item
)
;
#
[
cfg
(
feature =
"threading"
)
]
self
.
sem
.
release
(
vm
)
;
}
/// Returns a strong reference from the head of the buffer.
///
/// ## See Also
///
/// [`RingBuf_Get`](https://github.com/python/cpython/blob/v3.14.5/Modules/_queuemodule.c#L133-L154).
fn
get_inner
(
#
[
cfg
(
feature =
"threading"
)
]
buf
:
&
mut
MutexGuard
<
'
_
,
BufInner
>
,
#
[
cfg
(
not
(
feature =
"threading"
)
)
]
buf
:
&
mut
BufInner
,
)
->
Option
<
PyObjectRef
>
{
let
cap = buf
.
capacity
(
)
;
if
buf
.
len
(
)
<
(
cap /
4
)
{
// Items is less than 25% occupied, shrink it by 50%. This allows for
// growth without immediately needing to resize the underlying items array
buf
.
shrink_to
(
cap /
2
)
}
buf
.
pop_front
(
)
}
}
#
[
derive
(
FromArgs
)
]
struct
PutArgs
{
#
[
pyarg
(
positional
)
]
item
:
PyObjectRef
,
#
[
expect
(
dead_code
,
reason =
"Intentional. Provide compatibility with the Queue class"
)
]
#
[
pyarg
(
any
,
optional
,
default
=
true
)
]
block
:
bool
,
#
[
expect
(
dead_code
,
reason =
"Intentional. Provide compatibility with the Queue class"
)
]
#
[
pyarg
(
any
,
optional
)
]
timeout
:
Option
<
PyObjectRef
>
,
}
#
[
derive
(
FromArgs
)
]
struct
GetArgs
{
#
[
pyarg
(
any
,
optional
,
default
=
true
)
]
block
:
bool
,
#
[
pyarg
(
any
,
optional
)
]
timeout
:
Option
<
TimeoutSeconds
>
,
}
#
[
pyclass
(
with
(
Constructor
,
Comparable
,
Representable
)
,
flags
(
BASETYPE
,
HAS_WEAKREF
,
IMMUTABLETYPE
)
)
]
impl
PySimpleQueue
{
#
[
pymethod
]
fn
empty
(
&
self
)
->
bool
{
self
.
buf
.
lock
(
)
.
is_empty
(
)
}
#
[
pymethod
]
fn
qsize
(
&
self
)
->
usize
{
self
.
buf
.
lock
(
)
.
len
(
)
}
#
[
pymethod
]
fn
put
(
&
self
,
args
:
PutArgs
,
vm
:
&
VirtualMachine
)
{
let
PutArgs
{
item
,
..
}
= args
;
self
.
push
(
item
,
vm
)
;
}
#
[
pymethod
]
fn
put_nowait
(
&
self
,
item
:
PyObjectRef
,
vm
:
&
VirtualMachine
)
{
self
.
push
(
item
,
vm
)
;
}
#
[
pymethod
]
fn
get
(
&
self
,
args
:
GetArgs
,
vm
:
&
VirtualMachine
)
->
PyResult
<
PyObjectRef
>
{
let
GetArgs
{
block
,
timeout
}
= args
;
// Non-blocking: just try once
if
!block
{
return
Self
::
get_inner
(
&
mut
self
.
buf
.
lock
(
)
)
.
ok_or_else
(
||
empty_error
(
vm
)
)
;
}
#
[
cfg_attr
(
not
(
feature =
"threading"
)
,
expect
(
unused_variables
,
reason =
"We are still validating the 'timeout' arg even if we don't have threading"
)
)
]
let
deadline =
match
timeout
.
map
(
|v| v
.
to_secs_f64
(
)
)
{
Some
(
v
)
if
v <
0.0
=>
{
return
Err
(
vm
.
new_value_error
(
"'timeout' must be a non-negative number"
)
)
;
}
Some
(
v
)
=>
Some
(
Instant
::
now
(
)
+
Duration
::
from_secs_f64
(
v
)
)
,
None
=>
None
,
}
;
#
[
cfg
(
feature =
"threading"
)
]
{
if
!
self
.
sem
.
acquire
(
block
,
deadline
,
vm
)
?
{
return
Err
(
empty_error
(
vm
)
)
;
}
}
Self
::
get_inner
(
&
mut
self
.
buf
.
lock
(
)
)
.
ok_or_else
(
||
empty_error
(
vm
)
)
}
#
[
pymethod
]
fn
get_nowait
(
&
self
,
vm
:
&
VirtualMachine
)
->
PyResult
<
PyObjectRef
>
{
#
[
cfg
(
feature =
"threading"
)
]
{
if
!
self
.
sem
.
acquire
(
false
,
None
,
vm
)
?
{
return
Err
(
empty_error
(
vm
)
)
;
}
}
Self
::
get_inner
(
&
mut
self
.
buf
.
lock
(
)
)
.
ok_or_else
(
||
empty_error
(
vm
)
)
}
#
[
pyclassmethod
]
fn
__class_getitem__
(
cls
:
PyTypeRef
,
args
:
PyObjectRef
,
vm
:
&
VirtualMachine
,
)
->
PyResult
<
PyGenericAlias
>
{
PyGenericAlias
::
from_args
(
cls
,
args
,
vm
)
}
}
impl
Constructor
for
PySimpleQueue
{
type
Args
=
(
)
;
fn
py_new
(
_cls
:
&
Py
<
PyType
>
,
_args
:
Self
::
Args
,
_vm
:
&
VirtualMachine
)
->
PyResult
<
Self
>
{
Ok
(
Self
::
default
(
)
)
}
}
impl
AsNumber
for
PySimpleQueue
{
fn
as_number
(
)
->
&
'
static
PyNumberMethods
{
static
AS_NUMBER
:
PyNumberMethods
=
PyNumberMethods
{
boolean
:
Some
(
|number
,
_vm|
{
let
zelf = number
.
obj
.
downcast_ref
::
<
PySimpleQueue
>
(
)
.
unwrap
(
)
;
Ok
(
!zelf
.
buf
.
lock
(
)
.
is_empty
(
)
)
}
)
,
..
PyNumberMethods
::
NOT_IMPLEMENTED
}
;
&
AS_NUMBER
}
}
impl
Comparable
for
PySimpleQueue
{
fn
cmp
(
zelf
:
&
Py
<
Self
>
,
other
:
&
PyObject
,
op
:
PyComparisonOp
,
_vm
:
&
VirtualMachine
,
)
->
PyResult
<
PyComparisonValue
>
{
Ok
(
if
let
Some
(
res
)
= op
.
identical_optimization
(
zelf
,
other
)
{
res
.
into
(
)
}
else
{
PyComparisonValue
::
NotImplemented
}
)
}
}
impl
Representable
for
PySimpleQueue
{
fn
repr
(
zelf
:
&
Py
<
Self
>
,
vm
:
&
VirtualMachine
)
->
PyResult
<
PyRef
<
PyStr
>
>
{
Ok
(
vm
.
ctx
.
new_str
(
format
!
(
"<{} at {:#x}>"
,
Self
::
class
(
&
vm
.
ctx
)
.
slot_name
(
)
,
zelf
.
get_id
(
)
)
)
)
}
fn
repr_str
(
_zelf
:
&
Py
<
Self
>
,
_vm
:
&
VirtualMachine
)
->
PyResult
<
String
>
{
unreachable
!
(
"repr() is overridden directly"
)
}
}
}
Back
|
FazBrowse Home
|
New Git URL