1
-
2
- /* Thread module */
3
- /* Interface to Sjoerd's portable C thread library */
4
-
5
1
#include "Python.h"
6
2
#include "structmember.h" /* offsetof */
7
3
@@ -23,8 +19,6 @@ extern PyTypeObject PySimpleQueueType; /* forward decl */
23
19
24
20
static PyObject * EmptyError ;
25
21
26
- static _PyTime_t infinite_timeout_val ;
27
-
28
22
29
23
typedef struct {
30
24
PyObject_HEAD
@@ -103,7 +97,7 @@ _queue_SimpleQueue___init___impl(simplequeueobject *self)
103
97
_queue.SimpleQueue.put
104
98
item: object
105
99
106
- Put the item on the queue, without blocking .
100
+ Put the item on the queue. This method never blocks .
107
101
108
102
[clinic start generated code]*/
109
103
@@ -147,8 +141,9 @@ simplequeue_pop_item(simplequeueobject *self)
147
141
self -> lst_pos += 1 ;
148
142
count = n - self -> lst_pos ;
149
143
if (self -> lst_pos > count ) {
150
- /* reclaim space at beginning of list */
144
+ /* The list is more than 50% empty, reclaim space at the beginning */
151
145
if (PyList_SetSlice (self -> lst , 0 , self -> lst_pos , NULL )) {
146
+ /* Undo pop */
152
147
self -> lst_pos -= 1 ;
153
148
PyList_SET_ITEM (self -> lst , self -> lst_pos , item );
154
149
return NULL ;
@@ -214,25 +209,6 @@ _queue_SimpleQueue_get_impl(simplequeueobject *self, int block,
214
209
microseconds = -1 ;
215
210
}
216
211
217
- // fprintf(stderr,
218
- // "GET [%lu] (lst_pos = %zd, count = %zd, locked = %d)\n",
219
- // PyThread_get_thread_ident(), self->lst_pos, PyList_GET_SIZE(self->lst) - self->lst_pos, self->locked);
220
- if (self -> lst_pos < PyList_GET_SIZE (self -> lst )) {
221
- /* Fast path */
222
- assert (!self -> locked );
223
- /* BEGIN GIL-protected critical section */
224
- item = simplequeue_pop_item (self );
225
- if (item == NULL ) {
226
- return NULL ;
227
- }
228
- /* END GIL-protected critical section */
229
- assert (!self -> locked );
230
- // fprintf(stderr,
231
- // "/GET [%lu] fast (lst_pos = %zd, count = %zd, locked = %d)\n",
232
- // PyThread_get_thread_ident(), self->lst_pos, PyList_GET_SIZE(self->lst) - self->lst_pos, self->locked);
233
- return item ;
234
- }
235
-
236
212
/* put() signals the queue to be non-empty by releasing the lock.
237
213
* So we simply try to acquire the lock in a loop, until the condition
238
214
* (queue non-empty) becomes true.
@@ -383,6 +359,5 @@ PyInit__queue(void)
383
359
if (PyModule_AddObject (m , "SimpleQueue" , (PyObject * )& PySimpleQueueType ) < 0 )
384
360
return NULL ;
385
361
386
- infinite_timeout_val = _PyTime_FromSeconds (-1 );
387
362
return m ;
388
363
}
0 commit comments