summaryrefslogtreecommitdiff
path: root/Modules/_multiprocessing
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2011-11-21 21:26:56 +0100
committerAntoine Pitrou <solipsis@pitrou.net>2011-11-21 21:26:56 +0100
commit6dd381eb62278f75de7ba01626813de84cd248e7 (patch)
tree4b843b5767b3296212c5dbe8c696c1081b2fbfe0 /Modules/_multiprocessing
parentce4a9da70535b4bb9048147b141f01004af2133d (diff)
downloadcpython-git-6dd381eb62278f75de7ba01626813de84cd248e7.tar.gz
Issue #12328: Under Windows, refactor handling of Ctrl-C events and
make _multiprocessing.win32.WaitForMultipleObjects interruptible when the wait_flag parameter is false. Patch by sbt.
Diffstat (limited to 'Modules/_multiprocessing')
-rw-r--r--Modules/_multiprocessing/multiprocessing.c35
-rw-r--r--Modules/_multiprocessing/multiprocessing.h1
-rw-r--r--Modules/_multiprocessing/semaphore.c52
-rw-r--r--Modules/_multiprocessing/win32_functions.c18
4 files changed, 36 insertions, 70 deletions
diff --git a/Modules/_multiprocessing/multiprocessing.c b/Modules/_multiprocessing/multiprocessing.c
index 890b96d7dc..ab2cd72d31 100644
--- a/Modules/_multiprocessing/multiprocessing.c
+++ b/Modules/_multiprocessing/multiprocessing.c
@@ -53,30 +53,6 @@ mp_SetError(PyObject *Type, int num)
}
-/*
- * Windows only
- */
-
-#ifdef MS_WINDOWS
-
-/* On Windows we set an event to signal Ctrl-C; compare with timemodule.c */
-
-HANDLE sigint_event = NULL;
-
-static BOOL WINAPI
-ProcessingCtrlHandler(DWORD dwCtrlType)
-{
- SetEvent(sigint_event);
- return FALSE;
-}
-
-#endif /* MS_WINDOWS */
-
-
-/*
- * All platforms
- */
-
static PyObject*
multiprocessing_address_of_buffer(PyObject *self, PyObject *obj)
{
@@ -165,17 +141,6 @@ PyInit__multiprocessing(void)
if (!temp)
return NULL;
PyModule_AddObject(module, "win32", temp);
-
- /* Initialize the event handle used to signal Ctrl-C */
- sigint_event = CreateEvent(NULL, TRUE, FALSE, NULL);
- if (!sigint_event) {
- PyErr_SetFromWindowsErr(0);
- return NULL;
- }
- if (!SetConsoleCtrlHandler(ProcessingCtrlHandler, TRUE)) {
- PyErr_SetFromWindowsErr(0);
- return NULL;
- }
#endif
/* Add configuration macros */
diff --git a/Modules/_multiprocessing/multiprocessing.h b/Modules/_multiprocessing/multiprocessing.h
index ac0dfd77b2..e3de9baf1b 100644
--- a/Modules/_multiprocessing/multiprocessing.h
+++ b/Modules/_multiprocessing/multiprocessing.h
@@ -100,7 +100,6 @@ PyObject *mp_SetError(PyObject *Type, int num);
extern PyObject *BufferTooShort;
extern PyTypeObject SemLockType;
extern PyTypeObject PipeConnectionType;
-extern HANDLE sigint_event;
/*
* Miscellaneous
diff --git a/Modules/_multiprocessing/semaphore.c b/Modules/_multiprocessing/semaphore.c
index 6749f237ce..e48936e94a 100644
--- a/Modules/_multiprocessing/semaphore.c
+++ b/Modules/_multiprocessing/semaphore.c
@@ -62,7 +62,8 @@ semlock_acquire(SemLockObject *self, PyObject *args, PyObject *kwds)
int blocking = 1;
double timeout;
PyObject *timeout_obj = Py_None;
- DWORD res, full_msecs, msecs, start, ticks;
+ DWORD res, full_msecs, nhandles;
+ HANDLE handles[2], sigint_event;
static char *kwlist[] = {"block", "timeout", NULL};
@@ -96,53 +97,40 @@ semlock_acquire(SemLockObject *self, PyObject *args, PyObject *kwds)
Py_RETURN_TRUE;
}
- /* check whether we can acquire without blocking */
+ /* check whether we can acquire without releasing the GIL and blocking */
if (WaitForSingleObject(self->handle, 0) == WAIT_OBJECT_0) {
self->last_tid = GetCurrentThreadId();
++self->count;
Py_RETURN_TRUE;
}
- msecs = full_msecs;
- start = GetTickCount();
-
- for ( ; ; ) {
- HANDLE handles[2] = {self->handle, sigint_event};
+ /* prepare list of handles */
+ nhandles = 0;
+ handles[nhandles++] = self->handle;
+ if (_PyOS_IsMainThread()) {
+ sigint_event = _PyOS_SigintEvent();
+ assert(sigint_event != NULL);
+ handles[nhandles++] = sigint_event;
+ }
- /* do the wait */
- Py_BEGIN_ALLOW_THREADS
+ /* do the wait */
+ Py_BEGIN_ALLOW_THREADS
+ if (sigint_event != NULL)
ResetEvent(sigint_event);
- res = WaitForMultipleObjects(2, handles, FALSE, msecs);
- Py_END_ALLOW_THREADS
-
- /* handle result */
- if (res != WAIT_OBJECT_0 + 1)
- break;
-
- /* got SIGINT so give signal handler a chance to run */
- Sleep(1);
-
- /* if this is main thread let KeyboardInterrupt be raised */
- if (PyErr_CheckSignals())
- return NULL;
-
- /* recalculate timeout */
- if (msecs != INFINITE) {
- ticks = GetTickCount();
- if ((DWORD)(ticks - start) >= full_msecs)
- Py_RETURN_FALSE;
- msecs = full_msecs - (ticks - start);
- }
- }
+ res = WaitForMultipleObjects(nhandles, handles, FALSE, full_msecs);
+ Py_END_ALLOW_THREADS
/* handle result */
switch (res) {
case WAIT_TIMEOUT:
Py_RETURN_FALSE;
- case WAIT_OBJECT_0:
+ case WAIT_OBJECT_0 + 0:
self->last_tid = GetCurrentThreadId();
++self->count;
Py_RETURN_TRUE;
+ case WAIT_OBJECT_0 + 1:
+ errno = EINTR;
+ return PyErr_SetFromErrno(PyExc_IOError);
case WAIT_FAILED:
return PyErr_SetFromWindowsErr(0);
default:
diff --git a/Modules/_multiprocessing/win32_functions.c b/Modules/_multiprocessing/win32_functions.c
index c017b2a6b7..ddc496d941 100644
--- a/Modules/_multiprocessing/win32_functions.c
+++ b/Modules/_multiprocessing/win32_functions.c
@@ -679,6 +679,7 @@ win32_WaitForMultipleObjects(PyObject* self, PyObject* args)
DWORD result;
PyObject *handle_seq;
HANDLE handles[MAXIMUM_WAIT_OBJECTS];
+ HANDLE sigint_event = NULL;
Py_ssize_t nhandles, i;
int wait_flag;
int milliseconds = INFINITE;
@@ -696,10 +697,10 @@ win32_WaitForMultipleObjects(PyObject* self, PyObject* args)
nhandles = PySequence_Length(handle_seq);
if (nhandles == -1)
return NULL;
- if (nhandles < 0 || nhandles >= MAXIMUM_WAIT_OBJECTS) {
+ if (nhandles < 0 || nhandles >= MAXIMUM_WAIT_OBJECTS - 1) {
PyErr_Format(PyExc_ValueError,
"need at most %zd handles, got a sequence of length %zd",
- MAXIMUM_WAIT_OBJECTS, nhandles);
+ MAXIMUM_WAIT_OBJECTS - 1, nhandles);
return NULL;
}
for (i = 0; i < nhandles; i++) {
@@ -711,14 +712,27 @@ win32_WaitForMultipleObjects(PyObject* self, PyObject* args)
return NULL;
handles[i] = h;
}
+ /* If this is the main thread then make the wait interruptible
+ by Ctrl-C unless we are waiting for *all* handles */
+ if (!wait_flag && _PyOS_IsMainThread()) {
+ sigint_event = _PyOS_SigintEvent();
+ assert(sigint_event != NULL);
+ handles[nhandles++] = sigint_event;
+ }
Py_BEGIN_ALLOW_THREADS
+ if (sigint_event != NULL)
+ ResetEvent(sigint_event);
result = WaitForMultipleObjects((DWORD) nhandles, handles,
(BOOL) wait_flag, (DWORD) milliseconds);
Py_END_ALLOW_THREADS
if (result == WAIT_FAILED)
return PyErr_SetExcFromWindowsErr(PyExc_IOError, 0);
+ else if (sigint_event != NULL && result == WAIT_OBJECT_0 + nhandles - 1) {
+ errno = EINTR;
+ return PyErr_SetFromErrno(PyExc_IOError);
+ }
return PyLong_FromLong((int) result);
}