Esempio n. 1
0
lcb_luv_socket_t
lcb_luv_socket_new(struct lcb_io_opt_st *iops)
{
    /* Find the next 'file descriptor' */

    lcb_socket_t idx;
    lcb_luv_socket_t newsock;
    idx = find_free_idx(IOPS_COOKIE(iops));
    if (idx == -1) {
        iops->v.v0.error = ENFILE;
        return NULL;
    }

    newsock = calloc(1, sizeof(struct lcb_luv_socket_st));
    newsock->idx = idx;
    newsock->parent = IOPS_COOKIE(iops);

    uv_prepare_init(newsock->parent->loop, &newsock->prep);
    newsock->prep_active = 0;

    newsock->prep.data = newsock;
    newsock->u_req.req.data = newsock;
    newsock->refcount = 1;

    uv_tcp_init(IOPS_COOKIE(iops)->loop, &newsock->tcp);
    IOPS_COOKIE(iops)->socktable[idx] = newsock;
    iops->v.v0.error = 0;
    log_socket_debug("%p: Created new socket %p(%d)", iops, newsock, idx);
    return newsock;
}
Esempio n. 2
0
EventLoop::EventLoop() : deletingObjects(false) {
	uv_loop_init(&loop);
	deleteObjectsHandle.data = this;
	uv_prepare_init(&loop, &deleteObjectsHandle);
	uv_prepare_start(&deleteObjectsHandle, &staticDeleteObjects);
	uv_unref((uv_handle_t*) &deleteObjectsHandle);
}
Esempio n. 3
0
static int luv_new_prepare(lua_State* L) {
  uv_prepare_t* handle = lua_newuserdata(L, sizeof(*handle));
  int ret = uv_prepare_init(luv_loop(L), handle);
  if (ret < 0) {
    lua_pop(L, 1);
    return luv_error(L, ret);
  }
  handle->data = luv_setup_handle(L);
  return 1;
}
Esempio n. 4
0
int IOWorker::init() {
  int rc = EventThread<IOWorkerEvent>::init(config_.queue_size_event());
  if (rc != 0) return rc;
  rc = request_queue_.init(loop(), this, &IOWorker::on_execute);
  if (rc != 0) return rc;
  rc = uv_prepare_init(loop(), &prepare_);
  if (rc != 0) return rc;
  rc = uv_prepare_start(&prepare_, on_prepare);
  if (rc != 0) return rc;
  return rc;
}
Esempio n. 5
0
void event_init()
{
  // Initialize input events
  input_init();
  // Timer to wake the event loop if a timeout argument is passed to
  // `event_poll`
  // Signals
  signal_init();
  uv_timer_init(uv_default_loop(), &timer);
  // This prepare handle that actually starts the timer
  uv_prepare_init(uv_default_loop(), &timer_prepare);
}
Esempio n. 6
0
File: event.c Progetto: jollywho/nav
void event_init(void)
{
  log_msg("INIT", "event");
  uv_loop_init(eventloop());
  uv_prepare_init(eventloop(), &mainloop()->event_prepare);
  uv_timer_init(eventloop(), &mainloop()->children_kill_timer);
  uv_signal_init(eventloop(), &mainloop()->children_watcher);
  SLIST_INIT(&mainloop()->children);
  eventloop()->data = eventloop();
  QUEUE_INIT(&eventq()->headtail);
  QUEUE_INIT(&drawq()->headtail);
  mainloop()->running = false;
}
Esempio n. 7
0
File: loop.c Progetto: nubjs/libnub
void nub_loop_init(nub_loop_t* loop) {
  uv_async_t* async_handle;
  int er;

  er = uv_loop_init(&loop->uvloop);
  ASSERT(0 == er);

  er = uv_prepare_init(&loop->uvloop, &loop->queue_processor_);
  ASSERT(0 == er);
  loop->queue_processor_.data = loop;
  uv_unref((uv_handle_t*) &loop->queue_processor_);

  er = uv_mutex_init(&loop->queue_processor_lock_);
  ASSERT(0 == er);

  fuq_init(&loop->blocking_queue_);

  er = uv_sem_init(&loop->loop_lock_sem_, 0);
  ASSERT(0 == er);

  fuq_init(&loop->thread_dispose_queue_);

  er = uv_mutex_init(&loop->thread_dispose_lock_);
  ASSERT(0 == er);

  fuq_init(&loop->work_queue_);

  er = uv_mutex_init(&loop->work_lock_);
  ASSERT(0 == er);

  async_handle = (uv_async_t*) malloc(sizeof(*async_handle));
  CHECK_NE(NULL, async_handle);
  er = uv_async_init(&loop->uvloop, async_handle, nub__thread_dispose);
  ASSERT(0 == er);
  async_handle->data = loop;
  loop->work_ping_ = async_handle;
  uv_unref((uv_handle_t*) loop->work_ping_);

  loop->ref_ = 0;
  loop->disposed_ = 0;

  er = uv_prepare_start(&loop->queue_processor_, nub__async_prepare_cb);
  ASSERT(0 == er);
}
Esempio n. 8
0
static int
Prepare_tp_init(Prepare *self, PyObject *args, PyObject *kwargs)
{
    int r;
    uv_prepare_t *uv_prepare = NULL;
    Loop *loop;
    PyObject *tmp = NULL;

    UNUSED_ARG(kwargs);

    if (UV_HANDLE(self)) {
        PyErr_SetString(PyExc_PrepareError, "Object already initialized");
        return -1;
    }

    if (!PyArg_ParseTuple(args, "O!:__init__", &LoopType, &loop)) {
        return -1;
    }

    tmp = (PyObject *)((Handle *)self)->loop;
    Py_INCREF(loop);
    ((Handle *)self)->loop = loop;
    Py_XDECREF(tmp);

    uv_prepare = PyMem_Malloc(sizeof(uv_prepare_t));
    if (!uv_prepare) {
        PyErr_NoMemory();
        Py_DECREF(loop);
        return -1;
    }

    r = uv_prepare_init(UV_HANDLE_LOOP(self), uv_prepare);
    if (r != 0) {
        RAISE_UV_EXCEPTION(UV_HANDLE_LOOP(self), PyExc_PrepareError);
        Py_DECREF(loop);
        return -1;
    }
    uv_prepare->data = (void *)self;
    UV_HANDLE(self) = (uv_handle_t *)uv_prepare;

    return 0;
}
Esempio n. 9
0
/**
   cs_run : start the cspider
**/
int cs_run(cspider_t *cspider) {
  if (cspider->process == NULL) {
    printf("warn : need to init process function(use cs_setopt_process)\n");
    return 0;
  }
  if (cspider->save == NULL) {
    printf("warn : need to init data persistence function(use cs_setopt_save)\n");
    return 0;
  }
  /*
    set the threadpool's size by setenv()
   */
  char threadpool_size[4] = {0};
  snprintf(threadpool_size, sizeof(threadpool_size), "%d", cspider->threadpool_size);
  setenv("UV_THREADPOOL_SIZE", threadpool_size, 1);
  
  uv_prepare_init(cspider->loop, cspider->idler);
  uv_prepare_start(cspider->idler, watcher);
  
  return uv_run(cspider->loop, UV_RUN_DEFAULT);
}
Esempio n. 10
0
/* u2_unix_io_init(): initialize unix sync.
*/
void
u2_unix_io_init(void)
{
  u2_unix* unx_u = &u2_Host.unx_u;

  uv_timer_init(u2L, &unx_u->tim_u);
  unx_u->alm = u2_no;

  {
    u2_usig* sig_u;

    sig_u = c3_malloc(sizeof(u2_usig));
    uv_signal_init(u2L, &sig_u->sil_u);

    sig_u->num_i = SIGTERM;
    sig_u->nex_u = unx_u->sig_u;
    unx_u->sig_u = sig_u;
  }
  {
    u2_usig* sig_u;

    sig_u = c3_malloc(sizeof(u2_usig));
    uv_signal_init(u2L, &sig_u->sil_u);

    sig_u->num_i = SIGINT;
    sig_u->nex_u = unx_u->sig_u;
    unx_u->sig_u = sig_u;
  }
  {
    u2_usig* sig_u;

    sig_u = c3_malloc(sizeof(u2_usig));
    uv_signal_init(u2L, &sig_u->sil_u);

    sig_u->num_i = SIGWINCH;
    sig_u->nex_u = unx_u->sig_u;
    unx_u->sig_u = sig_u;
  }
  uv_prepare_init(u2_Host.lup_u, &u2_Host.unx_u.pre_u);
}
Esempio n. 11
0
static int
Prepare_tp_init(Prepare *self, PyObject *args, PyObject *kwargs)
{
    int r;
    Loop *loop;

    UNUSED_ARG(kwargs);

    RAISE_IF_HANDLE_INITIALIZED(self, -1);

    if (!PyArg_ParseTuple(args, "O!:__init__", &LoopType, &loop)) {
        return -1;
    }

    r = uv_prepare_init(loop->uv_loop, &self->prepare_h);
    if (r != 0) {
        RAISE_UV_EXCEPTION(loop->uv_loop, PyExc_PrepareError);
        return -1;
    }

    initialize_handle(HANDLE(self), loop);

    return 0;
}
Esempio n. 12
0
int main(int argc, char **argv)
{
  if (!owns_tty()) {
    fprintf(stderr, "process does not own the terminal\n");
    exit(2);
  }

  if (!is_terminal(stdin)) {
    fprintf(stderr, "stdin is not a terminal\n");
    exit(2);
  }

  if (!is_terminal(stdout)) {
    fprintf(stderr, "stdout is not a terminal\n");
    exit(2);
  }

  if (!is_terminal(stderr)) {
    fprintf(stderr, "stderr is not a terminal\n");
    exit(2);
  }

  if (argc > 1) {
    errno = 0;
    int count = (int)strtol(argv[1], NULL, 10);
    if (errno != 0) {
      abort();
    }
    count = (count < 0 || count > 99999) ? 0 : count;
    for (int i = 0; i < count; i++) {
      printf("line%d\n", i);
    }
    fflush(stdout);
    return 0;
  }

  int interrupted = 0;
  uv_prepare_t prepare;
  uv_prepare_init(uv_default_loop(), &prepare);
  uv_prepare_start(&prepare, prepare_cb);
#ifndef WIN32
  uv_tty_init(uv_default_loop(), &tty, fileno(stderr), 1);
#else
  uv_tty_init(uv_default_loop(), &tty, fileno(stdin), 1);
  uv_tty_init(uv_default_loop(), &tty_out, fileno(stdout), 0);
  int width, height;
  uv_tty_get_winsize(&tty_out, &width, &height);
#endif
  uv_tty_set_mode(&tty, UV_TTY_MODE_RAW);
  tty.data = &interrupted;
  uv_read_start(STRUCT_CAST(uv_stream_t, &tty), alloc_cb, read_cb);
#ifndef WIN32
  struct sigaction sa;
  sigemptyset(&sa.sa_mask);
  sa.sa_flags = 0;
  sa.sa_handler = sig_handler;
  sigaction(SIGHUP, &sa, NULL);
  sigaction(SIGWINCH, &sa, NULL);
#else
  uv_signal_t sigwinch_watcher;
  uv_signal_init(uv_default_loop(), &sigwinch_watcher);
  uv_signal_start(&sigwinch_watcher, sigwinch_cb, SIGWINCH);
#endif
  uv_run(uv_default_loop(), UV_RUN_DEFAULT);

#ifndef WIN32
  // XXX: Without this the SIGHUP handler is skipped on some systems.
  sleep(100);
#endif

  return 0;
}
Esempio n. 13
0
// Wait for some event
bool event_poll(int32_t ms)
{
  uv_run_mode run_mode = UV_RUN_ONCE;

  if (input_ready()) {
    // If there's a pending input event to be consumed, do it now
    return true;
  }

  static int recursive = 0;

  if (!(recursive++)) {
    // Only needs to start the libuv handle the first time we enter here
    input_start();
  }

  uv_timer_t timer;
  uv_prepare_t timer_prepare;
  TimerData timer_data = {.ms = ms, .timed_out = false, .timer = &timer};

  if (ms > 0) {
    uv_timer_init(uv_default_loop(), &timer);
    // This prepare handle that actually starts the timer
    uv_prepare_init(uv_default_loop(), &timer_prepare);
    // Timeout passed as argument to the timer
    timer.data = &timer_data;
    // We only start the timer after the loop is running, for that we
    // use a prepare handle(pass the interval as data to it)
    timer_prepare.data = &timer_data;
    uv_prepare_start(&timer_prepare, timer_prepare_cb);
  } else if (ms == 0) {
    // For ms == 0, we need to do a non-blocking event poll by
    // setting the run mode to UV_RUN_NOWAIT.
    run_mode = UV_RUN_NOWAIT;
  }

  do {
    // Run one event loop iteration, blocking for events if run_mode is
    // UV_RUN_ONCE
    uv_run(uv_default_loop(), run_mode);
    // Process immediate events outside uv_run since libuv event loop not
    // support recursion(processing events may cause a recursive event_poll
    // call)
    event_process(false);
  } while (
      // Continue running if ...
      !input_ready() &&   // we have no input
      !event_has_deferred() &&   // no events are waiting to be processed
      run_mode != UV_RUN_NOWAIT &&   // ms != 0
      !timer_data.timed_out);  // we didn't get a timeout

  if (!(--recursive)) {
    // Again, only stop when we leave the top-level invocation
    input_stop();
  }

  if (ms > 0) {
    // Ensure the timer-related handles are closed and run the event loop
    // once more to let libuv perform it's cleanup
    uv_close((uv_handle_t *)&timer, NULL);
    uv_close((uv_handle_t *)&timer_prepare, NULL);
    uv_run(uv_default_loop(), UV_RUN_NOWAIT);
    event_process(false);
  }

  return input_ready() || event_has_deferred();
}

bool event_has_deferred()
{
  return !kl_empty(get_queue(true));
}

// Push an event to the queue
void event_push(Event event, bool deferred)
{
  *kl_pushp(Event, get_queue(deferred)) = event;
}
Esempio n. 14
0
int
hijack_main_loop()
{
    int returnValue;
    uv_loop_t *uv_loop = NULL;

    SOL_DBG("Entering with state %s", RESOLVE_MAINLOOP_STATE(mainloopState));
    if (mainloopState == MAINLOOP_HIJACKED ||
        mainloopState == MAINLOOP_HIJACKING_STARTED) {
        return 0;
    }

    uv_loop = uv_default_loop();

    // The actual hijacking starts here, inspired by node-gtk. The plan:
    // 1. uv has two ways of letting us know that it needs to run its loop. One
    //    is that its backend timeout is >= 0, and the other is a file
    //    descriptor which can become readable/writable/errored. So, attach a
    //    source to the soletta main loop which will run the uv main loop in
    //    a non-blocking fashion. Also attach a file descriptor watch via which
    //    uv can signal that it needs to run an iteration.
    // 2. Attach an idler to the uv main loop and call sol_run() from it when
    //    it first runs. This interrupts the uv main loop, because sol_run()
    //    doesn't return but, since we've already added the above sources to
    //    the soletta main loop in the first step, the source or the file
    //    descriptor watch will end up running one non-blocking iteration of
    //    the uv main loop which, in turn, will recursively call the idler we
    //    added. At that point, the idler can remove itself from the uv main
    //    loop. After that, only the soletta main loop runs, but it runs an
    //    iteration of the uv main loop in a non-blocking fashion whenever the
    //    uv main loop signals to the soletta main loop via the attached
    //    source or the attached file descriptor watch.
    // 3. Attach a token handle to the uv main loop which represents all
    //    soletta open handles. This is necessary because the uv main loop
    //    would otherwise quit when it runs out of its own handles. We remove
    //    this token handle when we release the uv main loop so that if, at
    //    that point, it has no more handles, it is free to cause the node.js
    //    process to quit.

    // We allocate the various needed structures only once. After that, we
    // reuse them. We never free them, even if we release the uv main loop.
    if (!uv_loop_source) {
        uv_loop_source = sol_mainloop_add_source(&uv_loop_source_funcs,
            uv_loop);
        if (!uv_loop_source) {
            return -ENOMEM;
        }
    }

    if (!uv_loop_fd) {
        uv_loop_fd = sol_fd_add(uv_backend_fd(uv_loop),
            SOL_FD_FLAGS_IN | SOL_FD_FLAGS_OUT | SOL_FD_FLAGS_ERR,
            uv_loop_fd_changed, uv_loop);
        if (!uv_loop_fd) {
            return -ENOMEM;
        }
    }

    returnValue = uv_prepare_init(uv_loop, &uv_token_handle);
    if (returnValue) {
        return returnValue;
    }

    returnValue = uv_idle_init(uv_loop, &uv_idle);
    if (returnValue) {
        return returnValue;
    }

    SOL_DBG("Starting token handle");
    returnValue = uv_prepare_start(&uv_token_handle, uv_token_callback);
    if (returnValue) {
        return returnValue;
    }

    SOL_DBG("Starting idler");
    returnValue = uv_idle_start(&uv_idle, uv_idle_callback);
    if (returnValue) {
        return returnValue;
    }

    mainloopState = MAINLOOP_HIJACKING_STARTED;
    return 0;
}
Esempio n. 15
0
File: job.c Progetto: Saneyan/neovim
void job_init()
{
  uv_disable_stdio_inheritance();
  uv_prepare_init(uv_default_loop(), &job_prepare);
}