From 1308bbac2d1b01d26c466dc0ace36b36bab9df7c Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 6 Sep 2017 17:43:11 +0530 Subject: [PATCH] Move creation of wakeup fds to c module --- kitty/boss.py | 5 ++--- kitty/child-monitor.c | 26 ++++++++++++++++++++++---- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/kitty/boss.py b/kitty/boss.py index 9992a22da..01ff6bd41 100644 --- a/kitty/boss.py +++ b/kitty/boss.py @@ -31,7 +31,7 @@ from .keys import ( from .session import create_session from .shaders import Sprites from .tabs import SpecialWindow, TabManager -from .utils import handle_unix_signals, pipe2, safe_print +from .utils import handle_unix_signals, safe_print if isosx: from .fast_data_types import cocoa_update_title @@ -101,10 +101,9 @@ class Boss(Thread): self.resize_gl_viewport = False self.shutting_down = False self.signal_fd = handle_unix_signals() - read_wakeup_fd, write_wakeup_fd = pipe2() self.ui_timers = Timers() self.child_monitor = ChildMonitor( - read_wakeup_fd, write_wakeup_fd, self.signal_fd, opts.repaint_delay / 1000.0, + self.signal_fd, opts.repaint_delay / 1000.0, self.on_child_death, self.update_screen, self.ui_timers, DumpCommands(args) if args.dump_commands or args.dump_bytes else None) set_boss(self) diff --git a/kitty/child-monitor.c b/kitty/child-monitor.c index 58c9712d3..f688e01c6 100644 --- a/kitty/child-monitor.c +++ b/kitty/child-monitor.c @@ -7,6 +7,7 @@ #include "data-types.h" #include +#include #include #include @@ -51,20 +52,35 @@ static uint8_t drain_buf[1024]; #define INCREF_CHILD(x) XREF_CHILD(x, Py_INCREF) #define DECREF_CHILD(x) XREF_CHILD(x, Py_DECREF) +static inline bool +self_pipe(int fds[2]) { + int flags; + flags = pipe(fds); + if (flags != 0) return false; + flags = fcntl(fds[0], F_GETFD); + if (flags == -1) { return false; } + if (fcntl(fds[0], F_SETFD, flags | FD_CLOEXEC) == -1) { return false; } + flags = fcntl(fds[0], F_GETFL); + if (flags == -1) { return false; } + if (fcntl(fds[0], F_SETFL, flags | O_NONBLOCK) == -1) { return false; } + return true; +} + static PyObject * new(PyTypeObject *type, PyObject *args, PyObject UNUSED *kwds) { ChildMonitor *self; PyObject *dump_callback, *death_notify, *update_screen, *timers; - int wakeup_fd, write_wakeup_fd, signal_fd, ret; + int signal_fd, ret, wakeup_fds[2]; double repaint_delay; if (created) { PyErr_SetString(PyExc_RuntimeError, "Can have only a single ChildMonitor instance"); return NULL; } - if (!PyArg_ParseTuple(args, "iiidOOOO", &wakeup_fd, &write_wakeup_fd, &signal_fd, &repaint_delay, &death_notify, &update_screen, &timers, &dump_callback)) return NULL; + if (!PyArg_ParseTuple(args, "idOOOO", &signal_fd, &repaint_delay, &death_notify, &update_screen, &timers, &dump_callback)) return NULL; created = true; if ((ret = pthread_mutex_init(&children_lock, NULL)) != 0) { PyErr_Format(PyExc_RuntimeError, "Failed to create children_lock mutex: %s", strerror(ret)); return NULL; } + if (!self_pipe(wakeup_fds)) return PyErr_SetFromErrno(PyExc_OSError); self = (ChildMonitor *)type->tp_alloc(type, 0); if (self == NULL) return PyErr_NoMemory(); self->death_notify = death_notify; Py_INCREF(death_notify); @@ -75,9 +91,9 @@ new(PyTypeObject *type, PyObject *args, PyObject UNUSED *kwds) { parse_func = parse_worker_dump; } else parse_func = parse_worker; self->count = 0; - fds[0].fd = wakeup_fd; fds[1].fd = signal_fd; + fds[0].fd = wakeup_fds[0]; fds[1].fd = signal_fd; fds[0].events = POLLIN; fds[1].events = POLLIN; - self->write_wakeup_fd = write_wakeup_fd; + self->write_wakeup_fd = wakeup_fds[1]; self->repaint_delay = repaint_delay; return (PyObject*) self; @@ -99,6 +115,8 @@ dealloc(ChildMonitor* self) { add_queue_count--; FREE_CHILD(add_queue[add_queue_count]); } + close(self->write_wakeup_fd); + close(fds[0].fd); }