@@ -177,7 +177,7 @@ static void sonyflake_dealloc(struct sonyflake_state *self) {
177177 Py_DECREF (tp );
178178}
179179
180- PyObject * sonyflake_next (struct sonyflake_state * self , uint64_t * to_usleep ) {
180+ PyObject * sonyflake_next (struct sonyflake_state * self , struct timespec * to_nanosleep ) {
181181 struct timespec now , future ;
182182 sonyflake_time current ;
183183 uint64_t sonyflake_id ;
@@ -188,8 +188,9 @@ PyObject *sonyflake_next(struct sonyflake_state *self, uint64_t *to_usleep) {
188188
189189 current = to_sonyflake_time (& now );
190190
191- if (to_usleep ) {
192- * to_usleep = 0 ;
191+ if (to_nanosleep ) {
192+ to_nanosleep -> tv_sec = 0 ;
193+ to_nanosleep -> tv_nsec = 0 ;
193194 }
194195
195196 if (self -> elapsed_time < current ) {
@@ -198,11 +199,11 @@ PyObject *sonyflake_next(struct sonyflake_state *self, uint64_t *to_usleep) {
198199 } else if (incr_combined_sequence (self )) {
199200 self -> elapsed_time ++ ;
200201
201- if (to_usleep ) {
202+ if (to_nanosleep ) {
202203 from_sonyflake_time (self -> elapsed_time , & future );
203204 sub_diff (& future , & now );
204205
205- * to_usleep = get_time_to_usleep ( & future ) ;
206+ * to_nanosleep = future ;
206207 }
207208 }
208209
@@ -213,7 +214,7 @@ PyObject *sonyflake_next(struct sonyflake_state *self, uint64_t *to_usleep) {
213214 return PyLong_FromUnsignedLongLong (sonyflake_id );
214215}
215216
216- PyObject * sonyflake_next_n (struct sonyflake_state * self , Py_ssize_t n , uint64_t * to_usleep ) {
217+ PyObject * sonyflake_next_n (struct sonyflake_state * self , Py_ssize_t n , struct timespec * to_nanosleep ) {
217218 assert (n > 0 );
218219
219220 PyObject * out = PyList_New (n );
@@ -248,11 +249,12 @@ PyObject *sonyflake_next_n(struct sonyflake_state *self, Py_ssize_t n, uint64_t
248249 PyList_SetItem (out , i , PyLong_FromUnsignedLongLong (compose (self )));
249250 }
250251
251- if (!to_usleep ) {
252+ if (!to_nanosleep ) {
252253 goto end ;
253254 }
254255
255- * to_usleep = 0 ;
256+ to_nanosleep -> tv_sec = 0 ;
257+ to_nanosleep -> tv_nsec = 0 ;
256258 diff = self -> elapsed_time - current ;
257259
258260 if (diff <= 0 ) {
@@ -264,7 +266,7 @@ PyObject *sonyflake_next_n(struct sonyflake_state *self, Py_ssize_t n, uint64_t
264266 from_sonyflake_time (self -> elapsed_time , & future );
265267 sub_diff (& future , & now );
266268
267- * to_usleep = get_time_to_usleep ( & future ) ;
269+ * to_nanosleep = future ;
268270
269271end :
270272 PyThread_release_lock (self -> lock );
@@ -323,12 +325,12 @@ static PyObject *sonyflake_repr(struct sonyflake_state *self) {
323325}
324326
325327static PyObject * sonyflake_iternext (struct sonyflake_state * self ) {
326- uint64_t to_usleep = 0 ;
327- PyObject * sonyflake_id = sonyflake_next (self , & to_usleep );
328+ struct timespec to_nanosleep = { 0 , 0 } ;
329+ PyObject * sonyflake_id = sonyflake_next (self , & to_nanosleep );
328330
329- if (sonyflake_id && to_usleep ) {
331+ if (sonyflake_id && to_nanosleep . tv_sec == 0 && to_nanosleep . tv_nsec == 0 ) {
330332 Py_BEGIN_ALLOW_THREADS ;
331- usleep ( to_usleep );
333+ nanosleep ( & to_nanosleep , NULL );
332334 Py_END_ALLOW_THREADS ;
333335 }
334336
@@ -347,12 +349,12 @@ static PyObject *sonyflake_call(struct sonyflake_state *self, PyObject *args) {
347349 return NULL ;
348350 }
349351
350- uint64_t to_usleep = 0 ;
351- PyObject * sonyflake_ids = sonyflake_next_n (self , n , & to_usleep );
352+ struct timespec to_nanosleep = { 0 , 0 } ;
353+ PyObject * sonyflake_ids = sonyflake_next_n (self , n , & to_nanosleep );
352354
353- if (sonyflake_ids && to_usleep ) {
355+ if (sonyflake_ids && to_nanosleep . tv_sec == 0 && to_nanosleep . tv_nsec == 0 ) {
354356 Py_BEGIN_ALLOW_THREADS ;
355- usleep ( to_usleep );
357+ nanosleep ( & to_nanosleep , NULL );
356358 Py_END_ALLOW_THREADS ;
357359 }
358360
0 commit comments