_LockThread: use thread-safe EventLoop.idle_add()
authorZac Medico <zmedico@gentoo.org>
Mon, 31 Dec 2012 22:24:04 +0000 (14:24 -0800)
committerZac Medico <zmedico@gentoo.org>
Mon, 31 Dec 2012 22:24:04 +0000 (14:24 -0800)
pym/_emerge/AsynchronousLock.py

index 5bb9663a13c80aa338bda7138527ac3443afa88b..2de1acdcfb3d12b6add461cf245daa6f98efaef5 100644 (file)
@@ -105,19 +105,9 @@ class _LockThread(AbstractPollTask):
        """
 
        __slots__ = ('path',) + \
-               ('_files', '_force_dummy', '_lock_obj',
-               '_thread', '_reg_id',)
+               ('_force_dummy', '_lock_obj', '_thread',)
 
        def _start(self):
-               pr, pw = os.pipe()
-               self._files = {}
-               self._files['pipe_read'] = pr
-               self._files['pipe_write'] = pw
-               for f in self._files.values():
-                       fcntl.fcntl(f, fcntl.F_SETFL,
-                               fcntl.fcntl(f, fcntl.F_GETFL) | os.O_NONBLOCK)
-               self._reg_id = self.scheduler.io_add_watch(self._files['pipe_read'],
-                       self.scheduler.IO_IN, self._output_handler)
                self._registered = True
                threading_mod = threading
                if self._force_dummy:
@@ -128,22 +118,14 @@ class _LockThread(AbstractPollTask):
 
        def _run_lock(self):
                self._lock_obj = lockfile(self.path, wantnewlockfile=True)
-               os.write(self._files['pipe_write'], b'\0')
-
-       def _output_handler(self, f, event):
-               buf = None
-               if event & self.scheduler.IO_IN:
-                       try:
-                               buf = os.read(self._files['pipe_read'], self._bufsize)
-                       except OSError as e:
-                               if e.errno not in (errno.EAGAIN,):
-                                       raise
-               if buf:
-                       self._unregister()
-                       self.returncode = os.EX_OK
-                       self.wait()
+               # Thread-safe callback to EventLoop
+               self.scheduler.idle_add(self._run_lock_cb)
 
-               return True
+       def _run_lock_cb(self):
+               self._unregister()
+               self.returncode = os.EX_OK
+               self.wait()
+               return False
 
        def _cancel(self):
                # There's currently no way to force thread termination.
@@ -164,15 +146,6 @@ class _LockThread(AbstractPollTask):
                        self._thread.join()
                        self._thread = None
 
-               if self._reg_id is not None:
-                       self.scheduler.source_remove(self._reg_id)
-                       self._reg_id = None
-
-               if self._files is not None:
-                       for f in self._files.values():
-                               os.close(f)
-                       self._files = None
-
 class _LockProcess(AbstractPollTask):
        """
        This uses the portage.locks module to acquire a lock asynchronously,