Issue #8407, issue #11859: Add signal.pthread_sigmask() function to fetch
and/or change the signal mask of the calling thread.

Fix also tests of test_io using threads and an alarm: use pthread_sigmask() to
ensure that the SIGALRM signal is received by the main thread.

Original patch written by Jean-Paul Calderone.
diff --git a/Doc/library/signal.rst b/Doc/library/signal.rst
index 698b1e7..ffe7f09 100644
--- a/Doc/library/signal.rst
+++ b/Doc/library/signal.rst
@@ -13,9 +13,6 @@
   underlying implementation), with the exception of the handler for
   :const:`SIGCHLD`, which follows the underlying implementation.
 
-* There is no way to "block" signals temporarily from critical sections (since
-  this is not supported by all Unix flavors).
-
 * Although Python signal handlers are called asynchronously as far as the Python
   user is concerned, they can only occur between the "atomic" instructions of the
   Python interpreter.  This means that signals arriving during long calculations
@@ -119,6 +116,28 @@
    in user and kernel space. SIGPROF is delivered upon expiration.
 
 
+.. data:: SIG_BLOCK
+
+   A possible value for the *how* parameter to :func:`pthread_sigmask`
+   indicating that signals are to be blocked.
+
+   .. versionadded:: 3.3
+
+.. data:: SIG_UNBLOCK
+
+   A possible value for the *how* parameter to :func:`pthread_sigmask`
+   indicating that signals are to be unblocked.
+
+   .. versionadded:: 3.3
+
+.. data:: SIG_SETMASK
+
+   A possible value for the *how* parameter to :func:`pthread_sigmask`
+   indicating that the signal mask is to be replaced.
+
+   .. versionadded:: 3.3
+
+
 The :mod:`signal` module defines one exception:
 
 .. exception:: ItimerError
@@ -161,6 +180,34 @@
    :manpage:`signal(2)`.)
 
 
+.. function:: pthread_sigmask(how, mask)
+
+   Fetch and/or change the signal mask of the calling thread.  The signal mask
+   is the set of signals whose delivery is currently blocked for the caller.
+   The old signal mask is returned.
+
+   The behavior of the call is dependent on the value of *how*, as follows.
+
+    * :data:`SIG_BLOCK`: The set of blocked signals is the union of the current
+      set and the *mask* argument.
+    * :data:`SIG_UNBLOCK`: The signals in *mask* are removed from the current
+      set of blocked signals.  It is permissible to attempt to unblock a
+      signal which is not blocked.
+    * :data:`SIG_SETMASK`: The set of blocked signals is set to the *mask*
+      argument.
+
+   *mask* is a list of signal numbers (e.g. [:const:`signal.SIGINT`,
+   :const:`signal.SIGTERM`]).
+
+   For example, ``signal.pthread_sigmask(signal.SIG_BLOCK, [])`` reads the
+   signal mask of the calling thread.
+
+   Availability: Unix. See the man page :manpage:`sigprocmask(3)` and
+   :manpage:`pthread_sigmask(3)` for further information.
+
+   .. versionadded:: 3.3
+
+
 .. function:: setitimer(which, seconds[, interval])
 
    Sets given interval timer (one of :const:`signal.ITIMER_REAL`,