GitHub PullRequestReviewCommentEvent at 2014-04-07 16:14:29
Repository: neovim
Actor: justinmk
Payload: {"comment":{"url":"https://api.github.com/repos/neovim/neovim/pulls/comments/11351558","id":11351558,"diff_hunk":"@@ -0,0 +1,315 @@\n+#include <stdint.h>\n+#include <stdbool.h>\n+\n+#include <uv.h>\n+\n+#include \"os/job_defs.h\"\n+#include \"os/job.h\"\n+#include \"os/time.h\"\n+#include \"os/shell.h\"\n+#include \"vim.h\"\n+#include \"memory.h\"\n+#include \"term.h\"\n+\n+#define EXIT_TIMEOUT 25\n+#define MAX_RUNNING_JOBS 100\n+#define JOB_BUFFER_SIZE 1024\n+\n+/// Possible lock states of the job buffer\n+typedef enum {\n+  kBufferLockNone = 0, ///< No data was read\n+  kBufferLockStdout,   ///< Data read from stdout\n+  kBufferLockStderr    ///< Data read from stderr\n+} BufferLock;\n+\n+struct _Job {\n+  // Job id the index in the job table plus one. \n+  int id;\n+  // Number of times the job will can be sent SIGTERM before a SIGKILL\n+  int exit_timeout;\n+  // If the job was already stopped\n+  bool stopped;\n+  // Data associated with the job\n+  void *data;\n+  // Buffer for reading from stdout or stderr\n+  char buffer[JOB_BUFFER_SIZE];\n+  // Size of the data from the last read\n+  uint32_t length;\n+  // Buffer lock state\n+  BufferLock lock;\n+  // Callback for consuming data from the buffer\n+  job_read_cb read_cb;\n+  // Structures for process spawning/management used by libuv\n+  uv_process_t proc;\n+  uv_process_options_t proc_opts;\n+  uv_stdio_container_t stdio[3];\n+  uv_pipe_t proc_stdin, proc_stdout, proc_stderr;\n+};\n+\n+static Job *table[MAX_RUNNING_JOBS] = {NULL};\n+static uv_prepare_t job_prepare;\n+\n+// Some helpers shared in this module\n+static bool is_alive(Job *job);\n+static Job * find_job(int id);\n+\n+// Callbacks for libuv\n+static void job_prepare_cb(uv_prepare_t *handle, int status);\n+static void alloc_cb(uv_handle_t *handle, size_t suggested, uv_buf_t *buf);\n+static void read_cb(uv_stream_t *stream, ssize_t cnt, const uv_buf_t *buf);\n+static void write_cb(uv_write_t *req, int status);\n+static void exit_cb(uv_process_t *proc, int64_t status, int term_signal);\n+\n+void job_init()\n+{\n+  uv_disable_stdio_inheritance();\n+  uv_prepare_init(uv_default_loop(), &job_prepare);\n+  uv_prepare_start(&job_prepare, job_prepare_cb);\n+}\n+\n+void job_teardown()\n+{\n+  uint32_t remaining_tries = 6;\n+  int i;\n+  Job *job;\n+\n+  // Politely ask each job to terminate\n+  for (i = 0; i < MAX_RUNNING_JOBS; i++) {\n+    if ((job = table[i]) != NULL) {\n+      uv_process_kill(&job->proc, SIGTERM);\n+    }\n+  }\n+\n+  // Prepare to start shooting\n+  for (i = 0; i < MAX_RUNNING_JOBS; i++) {\n+    if ((job = table[i]) == NULL) continue;\n+    if (is_alive(job)) {\n+      // Still alive\n+      if (remaining_tries--) {\n+        // Since this is the first time we're checking, wait 300ms so\n+        // every job has a chance to exit normally\n+        os_delay(50, 0);\n+      }\n+      uv_process_kill(&job->proc, SIGKILL);\n+    }\n+  }\n+}\n+\n+int job_start(char **argv, void *data, job_read_cb cb)\n+{\n+  int i;\n+  Job *job;\n+ \n+  // Search for a free flot in the table\n+  for (i = 0; i < MAX_RUNNING_JOBS; i++) {\n+    if (table[i] == NULL) {\n+      break;\n+    }\n+  }\n+\n+  if (i == MAX_RUNNING_JOBS) {\n+    // No free slots","path":"src/os/job.c","position":111,"original_position":111,"commit_id":"92fe76d12d1e3d89ff09c323f70639837db6fcb3","original_commit_id":"92fe76d12d1e3d89ff09c323f70639837db6fcb3","user":{"login":"justinmk","id":1359421,"avatar_url":"https://avatars.githubusercontent.com/u/1359421?","gravatar_id":"9447c376788e36a4a7e09dd665f4d870","url":"https://api.github.com/users/justinmk","html_url":"https://github.com/justinmk","followers_url":"https://api.github.com/users/justinmk/followers","following_url":"https://api.github.com/users/justinmk/following{/other_user}","gists_url":"https://api.github.com/users/justinmk/gists{/gist_id}","starred_url":"https://api.github.com/users/justinmk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/justinmk/subscriptions","organizations_url":"https://api.github.com/users/justinmk/orgs","repos_url":"https://api.github.com/users/justinmk/repos","events_url":"https://api.github.com/users/justinmk/events{/privacy}","received_events_url":"https://api.github.com/users/justinmk/received_events","type":"User","site_admin":false},"body":"can't this just be done on line 106?","created_at":"2014-04-07T16:14:29Z","updated_at":"2014-04-07T16:14:29Z","html_url":"https://github.com/neovim/neovim/pull/475#discussion_r11351558","pull_request_url":"https://api.github.com/repos/neovim/neovim/pulls/475","_links":{"self":{"href":"https://api.github.com/repos/neovim/neovim/pulls/comments/11351558"},"html":{"href":"https://github.com/neovim/neovim/pull/475#discussion_r11351558"},"pull_request":{"href":"https://api.github.com/repos/neovim/neovim/pulls/475"}}}}