Author: Armin Rigo <[email protected]>
Branch: signal-and-thread
Changeset: r61272:a3bd80f80f35
Date: 2013-02-15 16:56 +0100
http://bitbucket.org/pypy/pypy/changeset/a3bd80f80f35/

Log:    First, refactor stuff

diff --git a/pypy/interpreter/miscutils.py b/pypy/interpreter/miscutils.py
--- a/pypy/interpreter/miscutils.py
+++ b/pypy/interpreter/miscutils.py
@@ -17,7 +17,7 @@
     def setvalue(self, value):
         self._value = value
 
-    def ismainthread(self):
+    def signals_enabled(self):
         return True
 
     def getallvalues(self):
diff --git a/pypy/module/signal/interp_signal.py 
b/pypy/module/signal/interp_signal.py
--- a/pypy/module/signal/interp_signal.py
+++ b/pypy/module/signal/interp_signal.py
@@ -61,16 +61,16 @@
         "NOT_RPYTHON"
         AsyncAction.__init__(self, space)
         self.pending_signal = -1
-        self.fire_in_main_thread = False
+        self.fire_in_another_thread = False
         if self.space.config.objspace.usemodules.thread:
             from pypy.module.thread import gil
             gil.after_thread_switch = self._after_thread_switch
 
     @rgc.no_collect
     def _after_thread_switch(self):
-        if self.fire_in_main_thread:
-            if self.space.threadlocals.ismainthread():
-                self.fire_in_main_thread = False
+        if self.fire_in_another_thread:
+            if self.space.threadlocals.signals_enabled():
+                self.fire_in_another_thread = False
                 SignalActionFlag.rearm_ticker()
                 # this occurs when we just switched to the main thread
                 # and there is a signal pending: we force the ticker to
@@ -82,11 +82,7 @@
         n = self.pending_signal
         if n < 0: n = pypysig_poll()
         while n >= 0:
-            if self.space.config.objspace.usemodules.thread:
-                in_main = self.space.threadlocals.ismainthread()
-            else:
-                in_main = True
-            if in_main:
+            if self.space.threadlocals.signals_enabled():
                 # If we are in the main thread, report the signal now,
                 # and poll more
                 self.pending_signal = -1
@@ -97,7 +93,7 @@
                 # Otherwise, arrange for perform() to be called again
                 # after we switch to the main thread.
                 self.pending_signal = n
-                self.fire_in_main_thread = True
+                self.fire_in_another_thread = True
                 break
 
     def set_interrupt(self):
@@ -107,7 +103,6 @@
             # ^^^ may override another signal, but it's just for testing
         else:
             pypysig_pushback(cpy_signal.SIGINT)
-        self.fire_in_main_thread = True
 
 # ____________________________________________________________
 
@@ -204,9 +199,10 @@
     if WIN32 and signum not in signal_values:
         raise OperationError(space.w_ValueError,
                              space.wrap("invalid signal value"))
-    if not space.threadlocals.ismainthread():
+    if not space.threadlocals.signals_enabled():
         raise OperationError(space.w_ValueError,
-                             space.wrap("signal only works in main thread"))
+                             space.wrap("signal only works in main thread "
+                                 "or a thread in __pypy__.enable_signals()"))
     check_signum_in_range(space, signum)
 
     if space.eq_w(w_handler, space.wrap(SIG_DFL)):
@@ -235,10 +231,11 @@
 
     The fd must be non-blocking.
     """
-    if not space.threadlocals.ismainthread():
+    if not space.threadlocals.signals_enabled():
         raise OperationError(
             space.w_ValueError,
-            space.wrap("set_wakeup_fd only works in main thread"))
+            space.wrap("set_wakeup_fd only works in main thread "
+                       "or a thread in __pypy__.enable_signals()"))
     old_fd = pypysig_set_wakeup_fd(fd)
     return space.wrap(intmask(old_fd))
 
diff --git a/pypy/module/thread/threadlocals.py 
b/pypy/module/thread/threadlocals.py
--- a/pypy/module/thread/threadlocals.py
+++ b/pypy/module/thread/threadlocals.py
@@ -9,11 +9,12 @@
 
     def __init__(self):
         self._valuedict = {}   # {thread_ident: ExecutionContext()}
+        self._signalsenabled = {}   # {thread_ident: None}
         self._cleanup_()
 
     def _cleanup_(self):
         self._valuedict.clear()
-        self._mainthreadident = 0
+        self._signalsenabled.clear()
         self._mostrecentkey = 0        # fast minicaching for the common case
         self._mostrecentvalue = None   # fast minicaching for the common case
 
@@ -33,7 +34,7 @@
         ident = rthread.get_ident()
         if value is not None:
             if len(self._valuedict) == 0:
-                self._mainthreadident = ident
+                self._signalsenabled[ident] = None
             self._valuedict[ident] = value
         else:
             try:
@@ -44,8 +45,14 @@
         self._mostrecentkey = ident
         self._mostrecentvalue = value
 
-    def ismainthread(self):
-        return rthread.get_ident() == self._mainthreadident
+    def signals_enabled(self):
+        return rthread.get_ident() in self._signalsenabled
+
+    def enable_signals(self):
+        self._signalsenabled[rthread.get_ident()] = None
+
+    def disable_signals(self):
+        del self._signalsenabled[rthread.get_ident()]
 
     def getallvalues(self):
         return self._valuedict
@@ -60,4 +67,5 @@
 
     def reinit_threads(self, space):
         "Called in the child process after a fork()"
-        self._mainthreadident = rthread.get_ident()
+        self._signalsenabled.clear()
+        self.enable_signals()
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to