@@ -237,9 +237,8 @@ static void sonyflake_dealloc(struct sonyflake_state *self) {
237237 Py_DECREF (tp );
238238}
239239
240- static PyObject * sonyflake_next (struct sonyflake_state * self ) {
240+ static PyObject * sonyflake_next (struct sonyflake_state * self , uint64_t * to_usleep ) {
241241 struct timespec now , future ;
242- uint64_t to_sleep = 0 ;
243242 sonyflake_time current ;
244243 uint64_t sonyflake_id ;
245244
@@ -249,32 +248,32 @@ static PyObject *sonyflake_next(struct sonyflake_state *self) {
249248
250249 current = to_sonyflake_time (& now );
251250
251+ if (to_usleep ) {
252+ * to_usleep = 0 ;
253+ }
254+
252255 if (self -> elapsed_time < current ) {
253256 self -> elapsed_time = current ;
254257 self -> combined_sequence = 0 ;
255258 } else if (incr_combined_sequence (self )) {
256259 self -> elapsed_time ++ ;
257260
258- from_sonyflake_time (self -> elapsed_time , & future );
259- sub_diff (& future , & now );
261+ if (to_usleep ) {
262+ from_sonyflake_time (self -> elapsed_time , & future );
263+ sub_diff (& future , & now );
260264
261- to_sleep = get_time_to_usleep (& future );
265+ * to_usleep = get_time_to_usleep (& future );
266+ }
262267 }
263268
264269 sonyflake_id = compose (self );
265270
266271 PyThread_release_lock (self -> lock );
267272
268- if (to_sleep > 0 ) {
269- Py_BEGIN_ALLOW_THREADS ;
270- usleep (to_sleep );
271- Py_END_ALLOW_THREADS ;
272- }
273-
274273 return PyLong_FromUnsignedLongLong (sonyflake_id );
275274}
276275
277- static PyObject * sonyflake_next_n (struct sonyflake_state * self , Py_ssize_t n ) {
276+ static PyObject * sonyflake_next_n (struct sonyflake_state * self , Py_ssize_t n , uint64_t * to_usleep ) {
278277 assert (n > 0 );
279278
280279 PyObject * out = PyList_New (n );
@@ -284,7 +283,6 @@ static PyObject *sonyflake_next_n(struct sonyflake_state *self, Py_ssize_t n) {
284283 }
285284
286285 struct timespec now , future ;
287- uint64_t to_sleep = 0 ;
288286 sonyflake_time current , diff ;
289287
290288 PyThread_acquire_lock (self -> lock , 1 );
@@ -310,28 +308,27 @@ static PyObject *sonyflake_next_n(struct sonyflake_state *self, Py_ssize_t n) {
310308 PyList_SetItem (out , i , PyLong_FromUnsignedLongLong (compose (self )));
311309 }
312310
311+ if (!to_usleep ) {
312+ goto end ;
313+ }
314+
315+ * to_usleep = 0 ;
313316 diff = self -> elapsed_time - current ;
314317
315318 if (diff <= 0 ) {
316- PyThread_release_lock (self -> lock );
317- return out ;
319+ goto end ;
318320 } else if (diff > 1 ) {
319321 get_relative_current_time (self , & now );
320322 }
321323
322324 from_sonyflake_time (self -> elapsed_time , & future );
323325 sub_diff (& future , & now );
324326
325- to_sleep = get_time_to_usleep (& future );
327+ * to_usleep = get_time_to_usleep (& future );
326328
329+ end :
327330 PyThread_release_lock (self -> lock );
328331
329- if (to_sleep > 0 ) {
330- Py_BEGIN_ALLOW_THREADS ;
331- usleep (to_sleep );
332- Py_END_ALLOW_THREADS ;
333- }
334-
335332 return out ;
336333}
337334
@@ -385,8 +382,21 @@ static PyObject *sonyflake_repr(struct sonyflake_state *self) {
385382 return s ;
386383}
387384
385+ static PyObject * sonyflake_iternext (struct sonyflake_state * self ) {
386+ uint64_t to_usleep = 0 ;
387+ PyObject * sonyflake_id = sonyflake_next (self , & to_usleep );
388+
389+ if (sonyflake_id && to_usleep ) {
390+ Py_BEGIN_ALLOW_THREADS ;
391+ usleep (to_usleep );
392+ Py_END_ALLOW_THREADS ;
393+ }
394+
395+ return sonyflake_id ;
396+ }
397+
388398static PyObject * sonyflake_call (struct sonyflake_state * self , PyObject * args ) {
389- Py_ssize_t n ;
399+ Py_ssize_t n = 0 ;
390400
391401 if (!PyArg_ParseTuple (args , "n" , & n )) {
392402 return NULL ;
@@ -397,7 +407,16 @@ static PyObject *sonyflake_call(struct sonyflake_state *self, PyObject *args) {
397407 return NULL ;
398408 }
399409
400- return sonyflake_next_n (self , n );
410+ uint64_t to_usleep = 0 ;
411+ PyObject * sonyflake_ids = sonyflake_next_n (self , n , & to_usleep );
412+
413+ if (sonyflake_ids && to_usleep ) {
414+ Py_BEGIN_ALLOW_THREADS ;
415+ usleep (to_usleep );
416+ Py_END_ALLOW_THREADS ;
417+ }
418+
419+ return sonyflake_ids ;
401420}
402421
403422PyDoc_STRVAR (sonyflake_doc ,
@@ -414,7 +433,7 @@ static PyType_Slot sonyflake_type_slots[] = {
414433 {Py_tp_alloc , PyType_GenericAlloc },
415434 {Py_tp_dealloc , sonyflake_dealloc },
416435 {Py_tp_iter , PyObject_SelfIter },
417- {Py_tp_iternext , sonyflake_next },
436+ {Py_tp_iternext , sonyflake_iternext },
418437 {Py_tp_new , sonyflake_new },
419438 {Py_tp_init , sonyflake_init },
420439 {Py_tp_call , sonyflake_call },
0 commit comments