[PATCH 2/5] os: add OsBlockSIGIO and OsReleaseSIGIO

Peter Hutterer peter.hutterer at who-t.net
Mon Jul 2 23:01:15 PDT 2012


Let the dix be in charge of changing the sigprocmask so we only have one
entity that changes it.

Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
 include/os.h     |    6 +++
 os/utils.c       |   54 +++++++++++++++++++++--
 test/Makefile.am |    3 +-
 test/os.c        |  130 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 189 insertions(+), 4 deletions(-)
 create mode 100644 test/os.c

diff --git a/include/os.h b/include/os.h
index 276eb52..8540b82 100644
--- a/include/os.h
+++ b/include/os.h
@@ -333,6 +333,12 @@ OsBlockSignals(void);
 extern _X_EXPORT void
 OsReleaseSignals(void);
 
+extern _X_EXPORT int
+OsBlockSIGIO(void);
+
+extern _X_EXPORT void
+OsReleaseSIGIO(void);
+
 extern _X_EXPORT void
 OsAbort(void)
     _X_NORETURN;
diff --git a/os/utils.c b/os/utils.c
index 3a1ef93..55f58b9 100644
--- a/os/utils.c
+++ b/os/utils.c
@@ -1165,15 +1165,15 @@ OsBlockSignals(void)
     if (BlockedSignalCount++ == 0) {
         sigset_t set;
 
+#ifdef SIGIO
+        OsBlockSIGIO();
+#endif
         sigemptyset(&set);
         sigaddset(&set, SIGALRM);
         sigaddset(&set, SIGVTALRM);
 #ifdef SIGWINCH
         sigaddset(&set, SIGWINCH);
 #endif
-#ifdef SIGIO
-        sigaddset(&set, SIGIO);
-#endif
         sigaddset(&set, SIGTSTP);
         sigaddset(&set, SIGTTIN);
         sigaddset(&set, SIGTTOU);
@@ -1183,12 +1183,60 @@ OsBlockSignals(void)
 #endif
 }
 
+#ifdef SIG_BLOCK
+static sig_atomic_t sigio_blocked;
+#endif
+
+/**
+ * returns zero if this call caused SIGIO to be blocked now, non-zero if it
+ * was already blocked by a previous call to this function.
+ */
+int
+OsBlockSIGIO(void)
+{
+#ifdef SIGIO
+#ifdef SIG_BLOCK
+    if (sigio_blocked++ == 0) {
+        sigset_t set, old;
+        int ret;
+
+        sigemptyset(&set);
+        sigaddset(&set, SIGIO);
+        sigprocmask(SIG_BLOCK, &set, &old);
+        ret = sigismember(&old, SIGIO);
+        return ret;
+    } else
+        return 1;
+#endif
+#endif
+}
+
+void
+OsReleaseSIGIO(void)
+{
+#ifdef SIGIO
+#ifdef SIG_BLOCK
+    if (--sigio_blocked == 0) {
+        sigset_t set;
+
+        sigemptyset(&set);
+        sigaddset(&set, SIGIO);
+        sigprocmask(SIG_UNBLOCK, &set, NULL);
+    } else if (sigio_blocked < 0) {
+        BUG_WARN(sigio_blocked < 0);
+        sigio_blocked = 0;
+    }
+#endif
+#endif
+}
+
 void
 OsReleaseSignals(void)
 {
 #ifdef SIG_BLOCK
     if (--BlockedSignalCount == 0) {
         sigprocmask(SIG_SETMASK, &PreviousSignalMask, 0);
+        OsReleaseSIGIO();
     }
 #endif
 }
diff --git a/test/Makefile.am b/test/Makefile.am
index b2a53aa..436351c 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -5,7 +5,7 @@ if XORG
 # Tests that require at least some DDX functions in order to fully link
 # For now, requires xf86 ddx, could be adjusted to use another
 SUBDIRS += xi2
-noinst_PROGRAMS += xkb input xtest misc fixes xfree86  hashtabletest
+noinst_PROGRAMS += xkb input xtest misc fixes xfree86  hashtabletest os
 endif
 check_LTLIBRARIES = libxservertest.la
 
@@ -37,6 +37,7 @@ fixes_LDADD=$(TEST_LDADD)
 xfree86_LDADD=$(TEST_LDADD)
 touch_LDADD=$(TEST_LDADD)
 hashtabletest_LDADD=$(TEST_LDADD) $(top_srcdir)/Xext/hashtable.c
+os_LDADD=$(TEST_LDADD)
 
 libxservertest_la_LIBADD = $(XSERVER_LIBS)
 if XORG
diff --git a/test/os.c b/test/os.c
new file mode 100644
index 0000000..1460a34
--- /dev/null
+++ b/test/os.c
@@ -0,0 +1,130 @@
+/**
+ * Copyright © 2012 Red Hat, Inc.
+ *
+ *  Permission is hereby granted, free of charge, to any person obtaining a
+ *  copy of this software and associated documentation files (the "Software"),
+ *  to deal in the Software without restriction, including without limitation
+ *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ *  and/or sell copies of the Software, and to permit persons to whom the
+ *  Software is furnished to do so, subject to the following conditions:
+ *
+ *  The above copyright notice and this permission notice (including the next
+ *  paragraph) shall be included in all copies or substantial portions of the
+ *  Software.
+ *
+ *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ *  DEALINGS IN THE SOFTWARE.
+ */
+
+#ifdef HAVE_DIX_CONFIG_H
+#include <dix-config.h>
+#endif
+
+#include <signal.h>
+#include "os.h"
+
+static int
+sig_is_blocked(int sig)
+{
+    sigset_t current;
+
+    sigemptyset(&current);
+    assert(sigprocmask(SIG_BLOCK, NULL, &current) == 0);
+    return sigismember(&current, sig);
+}
+
+static void block_sigio_test(void)
+{
+#ifdef SIG_BLOCK
+    sigset_t current;
+
+    sigemptyset(&current);
+    assert(!sig_is_blocked(SIGIO));
+
+    /* block once */
+    OsBlockSIGIO();
+    assert(sig_is_blocked(SIGIO));
+    OsReleaseSIGIO();
+    assert(!sig_is_blocked(SIGIO));
+
+    /* block twice, nested */
+    OsBlockSIGIO();
+    assert(sig_is_blocked(SIGIO));
+    OsBlockSIGIO();
+    assert(sig_is_blocked(SIGIO));
+    OsReleaseSIGIO();
+    assert(sig_is_blocked(SIGIO));
+    OsReleaseSIGIO();
+    assert(!sig_is_blocked(SIGIO));
+
+    /* block all */
+    OsBlockSignals();
+    assert(sig_is_blocked(SIGIO));
+    OsReleaseSignals();
+    assert(!sig_is_blocked(SIGIO));
+
+    /* block all nested */
+    OsBlockSignals();
+    assert(sig_is_blocked(SIGIO));
+    OsBlockSignals();
+    assert(sig_is_blocked(SIGIO));
+    OsReleaseSignals();
+    assert(sig_is_blocked(SIGIO));
+    OsReleaseSignals();
+    assert(!sig_is_blocked(SIGIO));
+
+    /* mix the two */
+    /* ABBA */
+    OsBlockSignals();
+    assert(sig_is_blocked(SIGIO));
+    OsBlockSIGIO();
+    assert(sig_is_blocked(SIGIO));
+    OsReleaseSIGIO();
+    assert(sig_is_blocked(SIGIO));
+    OsReleaseSignals();
+    assert(!sig_is_blocked(SIGIO));
+
+    /* ABAB */
+    OsBlockSignals();
+    assert(sig_is_blocked(SIGIO));
+    OsBlockSIGIO();
+    assert(sig_is_blocked(SIGIO));
+    OsReleaseSignals();
+    assert(sig_is_blocked(SIGIO));
+    OsReleaseSIGIO();
+    assert(!sig_is_blocked(SIGIO));
+
+    /* BAAB */
+    OsBlockSIGIO();
+    assert(sig_is_blocked(SIGIO));
+    OsBlockSignals();
+    assert(sig_is_blocked(SIGIO));
+    OsReleaseSignals();
+    assert(sig_is_blocked(SIGIO));
+    OsReleaseSIGIO();
+    assert(!sig_is_blocked(SIGIO));
+
+    /* BABA */
+    OsBlockSIGIO();
+    assert(sig_is_blocked(SIGIO));
+    OsBlockSignals();
+    assert(sig_is_blocked(SIGIO));
+    OsReleaseSIGIO();
+    assert(sig_is_blocked(SIGIO));
+    OsReleaseSignals();
+    assert(!sig_is_blocked(SIGIO));
+
+#endif
+}
+
+int
+main(int argc, char **argv)
+{
+    block_sigio_test();
+    return 0;
+}
-- 
1.7.10.4



More information about the xorg-devel mailing list