Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8607ae2

Browse files
committed
Move the Py_{{BEGIN,END}_ALLOW,BLOCK}_THREADS macros in time_sleep()
to inside floatsleep(). This is necessary because floatsleep() does the error handling and it must have grabbed the interpreter lock and thread state before it can do so.
1 parent 3a44e1b commit 8607ae2

1 file changed

Lines changed: 15 additions & 7 deletions

File tree

Modules/timemodule.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,9 @@ time_sleep(self, args)
202202
double secs;
203203
if (!PyArg_Parse(args, "d", &secs))
204204
return NULL;
205-
Py_BEGIN_ALLOW_THREADS
206-
if (floatsleep(secs) != 0) {
207-
Py_BLOCK_THREADS
208-
return NULL;
209-
}
210-
Py_END_ALLOW_THREADS
211-
Py_INCREF(Py_None);
205+
if (floatsleep(secs) != 0)
206+
return NULL;
207+
Py_INCREF(Py_None);
212208
return Py_None;
213209
}
214210

@@ -532,23 +528,29 @@ floatsleep(double secs)
532528
secs = floor(secs);
533529
t.tv_sec = (long)secs;
534530
t.tv_usec = (long)(frac*1000000.0);
531+
Py_BEGIN_ALLOW_THREADS
535532
if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
533+
Py_BLOCK_THREADS
536534
PyErr_SetFromErrno(PyExc_IOError);
537535
return -1;
538536
}
537+
Py_END_ALLOW_THREADS
539538
#else /* !HAVE_SELECT */
540539
#ifdef macintosh
541540
#define MacTicks (* (long *)0x16A)
542541
long deadline;
543542
deadline = MacTicks + (long)(secs * 60.0);
544543
while (MacTicks < deadline) {
544+
/* XXX Should call some yielding function here */
545545
if (PyErr_CheckSignals())
546546
return -1;
547547
}
548548
#else /* !macintosh */
549549
#ifdef __WATCOMC__
550550
/* XXX Can't interrupt this sleep */
551+
Py_BEGIN_ALLOW_THREADS
551552
delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */
553+
Py_END_ALLOW_THREADS
552554
#else /* !__WATCOMC__ */
553555
#ifdef MSDOS
554556
struct timeb t1, t2;
@@ -568,7 +570,9 @@ floatsleep(double secs)
568570
}
569571
for (;;) {
570572
#ifdef QUICKWIN
573+
Py_BEGIN_ALLOW_THREADS
571574
_wyield();
575+
Py_END_ALLOW_THREADS
572576
#endif
573577
if (PyErr_CheckSignals())
574578
return -1;
@@ -580,10 +584,14 @@ floatsleep(double secs)
580584
#else /* !MSDOS */
581585
#ifdef MS_WIN32
582586
/* XXX Can't interrupt this sleep */
587+
Py_BEGIN_ALLOW_THREADS
583588
Sleep((int)(secs*1000));
589+
Py_END_ALLOW_THREADS
584590
#else /* !MS_WIN32 */
585591
/* XXX Can't interrupt this sleep */
592+
Py_BEGIN_ALLOW_THREADS
586593
sleep((int)secs);
594+
Py_END_ALLOW_THREADS
587595
#endif /* !MS_WIN32 */
588596
#endif /* !MSDOS */
589597
#endif /* !__WATCOMC__ */

0 commit comments

Comments
 (0)