]> git.mdlowis.com Git - projs/tide.git/commitdiff
switched to non blocking I/O and fixed a bug in the poll event configuration
authorMichael D. Lowis <mike@mdlowis.com>
Sun, 6 Jan 2019 19:46:32 +0000 (14:46 -0500)
committerMichael D. Lowis <mike@mdlowis.com>
Sun, 6 Jan 2019 19:46:32 +0000 (14:46 -0500)
src/lib/job.c

index df078c2e9dac9e74443588f9740e2976416d89de..ef8df23800da1aff24f1b95682a4f7726910da59 100644 (file)
@@ -23,7 +23,8 @@ static Job* JobList = NULL;
 
 static void pipe_read(Job* job) {
     struct PipeData* pipedata = job->data;
-    char buffer[4096];
+    char buffer[16385];
+    errno = 0;
     long nread = read(job->fd, buffer, sizeof(buffer)-1);
     if (nread <= 0) {
         job->readfn = NULL;
@@ -42,12 +43,13 @@ static void pipe_read(Job* job) {
 static void pipe_write(Job* job) {
     struct PipeData* pipedata = job->data;
     char* chunk = pipedata->data + pipedata->nwrite;
+    errno = 0;
     long nwrite = write(job->fd, chunk, pipedata->ndata);
     if (nwrite >= 0) {
         pipedata->ndata  -= nwrite;
         pipedata->nwrite += nwrite;
     }
-    if (nwrite < 0 || pipedata->ndata <= 0) {
+    if ((nwrite < 0 && errno != EWOULDBLOCK) || pipedata->ndata <= 0) {
         free(pipedata->data);
         pipedata->data = NULL;
         job->writefn = NULL;
@@ -75,7 +77,7 @@ static void job_process(int fd, int events) {
     Job* job = JobList; // Get job by fd
     while (job && job->fd != fd)
         job = job->next;
-    if (!job) return;
+    if (!job || !events) return;
     if (job->readfn && (events & POLLIN))
         job->readfn(job);
     if (job->writefn && (events & POLLOUT))
@@ -83,6 +85,7 @@ static void job_process(int fd, int events) {
     if (!job->readfn && !job->writefn)
         job_finish(job);
 }
+#include <fcntl.h>
 
 static int job_exec(char** cmd, int* p_pid) {
     int pid, fds[2];
@@ -103,6 +106,7 @@ static int job_exec(char** cmd, int* p_pid) {
         exit(execvp(cmd[0], cmd));
     } else {
         close(fds[1]);
+        fcntl(fds[0], F_SETFL, fcntl(fds[0], F_GETFL, 0) | O_NONBLOCK);
     }
     if (p_pid) *p_pid = pid;
     return fds[0];
@@ -115,8 +119,8 @@ bool job_poll(int ms) {
         JobFds[njobs].fd = job->fd;
         JobFds[njobs].events = 0;
         JobFds[njobs].revents = 0;
-        if (job->readfn) JobFds[njobs].events = POLLIN;
-        if (job->writefn) JobFds[njobs].events = POLLOUT;
+        if (job->readfn) JobFds[njobs].events |= POLLIN;
+        if (job->writefn) JobFds[njobs].events |= POLLOUT;
         if (JobFds[njobs].events) njobs++;
     }
     /* Poll until a job is ready, call the functions based on events */