Esempio n. 1
0
static void(loop_stop)()
{
   uv_stop(loop);
   uv_idle_t* idle = s_malloc0(sizeof(*idle));
   uv_idle_init(loop, idle);
   uv_idle_start(idle, loop_stop_cb);
}
Esempio n. 2
0
void rstream_set_file(RStream *rstream, uv_file file)
{
    rstream->file_type = uv_guess_handle(file);

    if (rstream->free_handle) {
        // If this is the second time we're calling this function, free the
        // previously allocated memory
        if (rstream->fread_idle != NULL) {
            uv_close((uv_handle_t *)rstream->fread_idle, close_cb);
        } else {
            uv_close((uv_handle_t *)rstream->stream, close_cb);
        }
    }

    if (rstream->file_type == UV_FILE) {
        // Non-blocking file reads are simulated with a idle handle that reads
        // in chunks of rstream->buffer_size, giving time for other events to
        // be processed between reads.
        rstream->fread_idle = xmalloc(sizeof(uv_idle_t));
        uv_idle_init(uv_default_loop(), rstream->fread_idle);
        rstream->fread_idle->data = rstream;
    } else {
        // Only pipes are supported for now
        assert(rstream->file_type == UV_NAMED_PIPE
               || rstream->file_type == UV_TTY);
        rstream->stream = xmalloc(sizeof(uv_pipe_t));
        uv_pipe_init(uv_default_loop(), (uv_pipe_t *)rstream->stream, 0);
        uv_pipe_open((uv_pipe_t *)rstream->stream, file);
        rstream->stream->data = rstream;
    }

    rstream->fd = file;
    rstream->free_handle = true;
}
Esempio n. 3
0
static void idle_1_cb(uv_handle_t* handle, int status) {
  int r;

  LOG("IDLE_1_CB\n");

  ASSERT(handle != NULL);
  ASSERT(status == 0);

  ASSERT(idles_1_active > 0);

  /* Init idle_2 and make it active */
  if (!idle_2_is_active) {
    r = uv_idle_init(&idle_2_handle, idle_2_close_cb, NULL);
    ASSERT(r == 0);
    r = uv_idle_start(&idle_2_handle, idle_2_cb);
    ASSERT(r == 0);
    idle_2_is_active = 1;
    idle_2_cb_started++;
  }

  idle_1_cb_called++;

  if (idle_1_cb_called % 5 == 0) {
    r = uv_idle_stop((uv_idle_t*)handle);
    ASSERT(r == 0);
    idles_1_active--;
  }
}
Esempio n. 4
0
uv_idle_t idleHandler(uv_loop_t* loop) {
    uv_idle_t handler;
    uv_idle_init(loop, &handler);
    int* p = 0;
    handler.data = &p;
    return handler;
}
Esempio n. 5
0
static void next_tick(uv_idle_t* handle, int status) {
  uv_loop_t* loop = handle->loop;
  uv_idle_stop(handle);
  uv_idle_init(loop, &idle_handle);
  uv_idle_start(&idle_handle, idle_cb);
  uv_timer_init(loop, &timer_handle);
  uv_timer_start(&timer_handle, timer_cb, 0, 0);
}
Esempio n. 6
0
    rust_uvtmp_thread() {
	task = rust_scheduler::get_task();
	stop_flag = false;
	loop = uv_loop_new();
	uv_idle_init(loop, &idle);
	idle.data = this;
	uv_idle_start(&idle, idle_cb);
    }
Esempio n. 7
0
void run_UVloop(void *arg)
{
    uv_idle_t idler;
    uv_idle_init(UV_loop,&idler);
    //uv_async_init(UV_loop,&Tasks_async,async_handler);
    uv_idle_start(&idler,SuperNET_idler);
    uv_run(UV_loop,UV_RUN_DEFAULT);
    printf("end of uv_run\n");
}
Esempio n. 8
0
static void enter_loop(MVMThreadContext *tc, MVMCallsite *callsite, MVMRegister *args) {
    uv_idle_t idle;
    if (uv_idle_init(tc->loop, &idle) != 0)
        MVM_panic(1, "Unable to initialize idle worker for event loop");
    idle.data = tc;
    if (uv_idle_start(&idle, idle_handler) != 0)
        MVM_panic(1, "Unable to start idle worker for event loop");
    uv_run(tc->loop, UV_RUN_DEFAULT);
    MVM_panic(1, "Supposedly unending event loop thread ended");
}
Esempio n. 9
0
static int luv_new_idle(lua_State* L) {
  uv_idle_t* handle = (uv_idle_t*)luv_newuserdata(L, sizeof(*handle));
  int ret = uv_idle_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. 10
0
/** @internal Schedule deferred continuation. */
static int l_ffi_defer(lua_State *L)
{
    uv_idle_t *check = malloc(sizeof(*check));
    if (!check) {
        return kr_error(ENOMEM);
    }
    uv_idle_init(uv_default_loop(), check);
    check->data = L;
    return uv_idle_start(check, l_ffi_resume_cb);
}
Esempio n. 11
0
    static void HHVM_METHOD(UVIdle, __construct, const Variant &v_loop) {
        auto* data = Native::data<UVNativeData>(this_);
        data->resource_handle = (void *) new uv_idle_ext_t();
        uv_idle_ext_t *idle_handle = fetchResource(data);
        idle_handle->start = false;
        idle_handle->idle_object_data = NULL;
        
        if(v_loop.isNull()){
            uv_idle_init(uv_default_loop(), idle_handle);
            return;
        }
        
        Object loop = v_loop.toObject();
        checkUVLoopInstance(loop, 1, s_uvidle, StaticString("__construct"));
        auto* loop_data = Native::data<UVLoopData>(loop.get());        
        SET_LOOP(this_, loop, s_uvidle);
        uv_idle_init(loop_data->loop, idle_handle);

    }
Esempio n. 12
0
std::shared_ptr<Idle> Idle::Create(Loop& loop) {
  auto h = std::make_shared<Idle>(private_init{});
  int err = uv_idle_init(loop.GetRaw(), h->GetRaw());
  if (err < 0) {
    loop.ReportError(err);
    return nullptr;
  }
  h->Keep();
  return h;
}
Esempio n. 13
0
int main() {
    uv_idle_t idler;

    uv_idle_init(uv_default_loop(), &idler);
    uv_idle_start(&idler, wait_for_a_while);

    printf("Idling...\n");
    uv_run(uv_default_loop(), UV_RUN_DEFAULT);

    return 0;
}
Esempio n. 14
0
// idle-watcher callbacks are low priority, as they are only invoked for each iteration when the event loop has no other pending events
int main() {
  int r;

  loop = uv_default_loop();

  r = uv_idle_init(loop, &idler);
  if (r) ERROR("initing idler", r);

  wait_for_next_input();

  return uv_run(loop, UV_RUN_DEFAULT);
}
Esempio n. 15
0
DLLEXPORT uv_idle_t *jl_idle_init(uv_loop_t *loop, jl_value_t *julia_struct)
{
    if (!loop)
        return 0;
    uv_idle_t *idle = malloc(sizeof(uv_idle_t));
    if (uv_idle_init(loop,idle)) {
        free(idle);
        return 0;
    }
    idle->data = julia_struct;
    return idle;
}
Esempio n. 16
0
int main(int argc, char **argv) {
    uv_idle_t idler;
    uv_idle_init(uv_default_loop(), &idler);
    uv_idle_start(&idler, waitForAWhile);

    printf(".. idling...");

    uv_run(uv_default_loop(), UV_RUN_DEFAULT);
    uv_loop_close(uv_default_loop());


    return 0;
}
Esempio n. 17
0
int main()
{
	uv_idle_t idler;

    uv_idle_init(get_loop(), &idler);
    uv_idle_start(&idler, wait_for_a_while);


    printf("Now quitting.\n");
    uv_run(get_loop(), UV_RUN_DEFAULT);

    return 0;
}
Esempio n. 18
0
int main(int argc, char **argv)
{
    cg_onscreen_t *onscreen;
    cg_error_t *error = NULL;
    struct demo demo;
    float fovy, aspect, z_near, z_2d, z_far;
    uv_loop_t *loop = uv_default_loop();

    demo.dev = cg_device_new ();
    if (!demo.dev || error != NULL)
        c_error("Failed to create Cogl context\n");

    onscreen = cg_onscreen_new(demo.dev, WIDTH, HEIGHT);

    demo.fb = onscreen;
    demo.width = cg_framebuffer_get_width(demo.fb);
    demo.height = cg_framebuffer_get_height(demo.fb);

    cg_onscreen_show(onscreen);
    cg_framebuffer_set_viewport(demo.fb, 0, 0, demo.width, demo.height);

    fovy = 45;
    aspect = (float)demo.width / (float)demo.height;
    z_near = 0.1;
    z_2d = 1000;
    z_far = 2000;

    cg_framebuffer_perspective(demo.fb, fovy, aspect, z_near, z_far);
    c_matrix_init_identity(&demo.view);
    c_matrix_view_2d_in_perspective(&demo.view, fovy, aspect, z_near, z_2d,
                                     demo.width, demo.height);
    cg_framebuffer_set_modelview_matrix(demo.fb, &demo.view);
    demo.swap_ready = true;

    cg_onscreen_add_frame_callback(demo.fb, frame_event_cb, &demo, NULL);

    init_particle_emitters(&demo);

    demo.timer = c_timer_new();
    demo.spin_rate = 0;
    demo.angle_between_emitters = 2 * M_PI / C_N_ELEMENTS(demo.emitter);

    uv_idle_init(loop, &demo.idle);
    demo.idle.data = &demo;
    uv_idle_start(&demo.idle, paint_cb);

    cg_uv_set_mainloop(demo.dev, loop);
    uv_run(loop, UV_RUN_DEFAULT);

    return 0;
}
Esempio n. 19
0
unsigned int lcbuv_close_socket(lcb_io_opt_t iobase, lcb_sockdata_t *sockbase)
{
    dCOMMON_VARS(iobase, sockbase)
    uv_idle_t *idle = calloc(1, sizeof(*idle));

    assert(sock->lcb_close_called == 0);

    sock->lcb_close_called = 1;
    idle->data = sock;
    uv_idle_init(io->loop, idle);
    uv_idle_start(idle, socket_closing_cb);

    (void)io;
    return 0;
}
Esempio n. 20
0
static void run(void) {
   uv_idle_t runner;
   uv_idle_init(uv_default_loop(), &runner);
   uv_idle_start(&runner, do_run);

   uv_run(uv_default_loop(), UV_RUN_DEFAULT);
   uv_loop_close(uv_default_loop());
   log_info(LOG, "Client stopped.");

   automaton->free(automaton);
   ch->free(ch);
   cgi_channel->free(cgi_channel);
   mh->free(mh);
   zmq_channel->free(zmq_channel);
}
Esempio n. 21
0
File: main.c Progetto: songtzu/study
int 
main(int argc, char* argv[])
{
  uv_idle_t idle;
  uv_loop_t* loop = uv_default_loop();

  uv_idle_init(loop, &idle);
  uv_idle_start(&idle, idle_routine);

  fprintf(stdout, "[%lu]Idling begin ...\n", clock());
  uv_run(loop, UV_RUN_DEFAULT);
  fprintf(stdout, "[%lu]Idling finish ...\n", clock());

  return 0;
}
Esempio n. 22
0
static unsigned int close_socket(lcb_io_opt_t iobase, lcb_sockdata_t *sockbase)
{
    my_sockdata_t *sock = (my_sockdata_t *)sockbase;
    my_iops_t *io = (my_iops_t *)iobase;

    uv_idle_t *idle = calloc(1, sizeof(*idle));

    lcb_assert(sock->lcb_close_called == 0);

    sock->lcb_close_called = 1;
    idle->data = sock;
    uv_idle_init(io->loop, idle);
    uv_idle_start(idle, socket_closing_cb);

    return 0;
}
Esempio n. 23
0
int main() {
   uv_idle_t idler;

   LOG = circus_new_log_file_descriptor(stdlib_memory, LOG_INFO, 1);

   uv_idle_init(uv_default_loop(), &idler);
   uv_idle_start(&idler, wait_for_a_while);

   LOG->set_format(LOG, "[%T] %O:%G\n");

   uv_run(uv_default_loop(), UV_RUN_DEFAULT);
   uv_loop_close(uv_default_loop());

   LOG->free(LOG);
   return 0;
}
Esempio n. 24
0
int main() {
    /* our reference to the default loop */
    uv_loop_t *loop;

    /* a idler of uv_idle_t struct type */
    uv_idle_t idler;

    /* let uv return the default loop */
    loop = uv_default_loop();

    /* initialize a idle handle with the default loop and the reference of our idle type */
    uv_idle_init(loop, &idler);
    /* start executing the idle handler and call the wait_for_a_while callback */
    uv_idle_start(&idler, wait_for_a_while);

    printf("Idling ...\n");

    /* start runing the loop in default mode */
    uv_run(loop, UV_RUN_DEFAULT);
}
Esempio n. 25
0
static void send_error(lcb_io_opt_t iobase, lcb_sockdata_t *sockbase,
                       lcb_io_error_cb callback)
{
    my_sockdata_t *sock = (my_sockdata_t *)sockbase;
    my_iops_t *io = (my_iops_t *)iobase;
    my_uvreq_t *uvr;

    if (!sock) {
        return;
    }

    uvr = alloc_uvreq(sock, (generic_callback_t)callback);

    if (!uvr) {
        return;
    }

    uv_idle_init(io->loop, &uvr->uvreq.idle);
    uv_idle_start(&uvr->uvreq.idle, err_idle_cb);
    incref_sock(sock);
}
Esempio n. 26
0
int sg_etp_session_start(sg_etp_session_t * session, int interval_ms, uv_udp_t * udp)
{
    int ret = ERROR;

    SG_ASSERT_RET(NULL != session, "session pointer is NULL", ret);

    session->interval = interval_ms;

    if (NULL == udp) /* self-contained udp, for client */
    {
        /* init udp */
        session->udp = &(session->udp_hdl);
        ret = uv_udp_init(session->loop, session->udp);
        SG_ASSERT_RET(ret >= 0, "init udp failed", ERROR);
        session->udp->data = session;
        ret = uv_udp_recv_start(session->udp, on_uv_alloc_buffer, on_client_recv_udp);
        SG_ASSERT_RET(ret >= 0, "start udp recv failed", ERROR);
    }
    else
    {
        session->udp = udp;
    }

    ret = ikcp_nodelay(session->kcp, 1, interval_ms, 2, 0);
    SG_ASSERT_RET(ret >= 0, "ikcp nodelay failed", ERROR);

    /* start a timer for kcp update and receiving */
    ret = uv_timer_init(session->loop, &(session->timer));
    SG_ASSERT_RET(ret >= 0, "init timer failed", ERROR);
    session->timer.data = session; /* link client pointer to timer */
    ret = uv_timer_start(&(session->timer), on_uv_timer_cb, session->interval, session->interval);
    SG_ASSERT_RET(ret >= 0, "start timer failed", ERROR);

    uv_idle_init(session->loop, &(session->idle));
    session->idle.data = session;

    SG_CALLBACK(session->on_open, session);

    return OK;
}
Esempio n. 27
0
void stream_init(Loop *loop, Stream *stream, int fd, uv_stream_t *uvstream,
    void *data)
{
  stream->uvstream = uvstream;

  if (fd >= 0) {
    uv_handle_type type = uv_guess_handle(fd);
    stream->fd = fd;

    if (type == UV_FILE) {
      // Non-blocking file reads are simulated with an idle handle that reads in
      // chunks of the ring buffer size, giving time for other events to be
      // processed between reads.
      uv_idle_init(&loop->uv, &stream->uv.idle);
      stream->uv.idle.data = stream;
    } else {
      assert(type == UV_NAMED_PIPE || type == UV_TTY);
      uv_pipe_init(&loop->uv, &stream->uv.pipe, 0);
      uv_pipe_open(&stream->uv.pipe, fd);
      stream->uvstream = (uv_stream_t *)&stream->uv.pipe;
    }
  }

  if (stream->uvstream) {
    stream->uvstream->data = stream;
  }

  stream->data = data;
  stream->internal_data = NULL;
  stream->fpos = 0;
  stream->curmem = 0;
  stream->maxmem = 0;
  stream->pending_reqs = 0;
  stream->read_cb = NULL;
  stream->write_cb = NULL;
  stream->close_cb = NULL;
  stream->internal_close_cb = NULL;
  stream->closed = false;
  stream->buffer = NULL;
}
Esempio n. 28
0
int main() {
    uv_idle_t idler;
    uv_loop_t *loop = uv_default_loop();

    // 初始化 idle handle
    uv_idle_init(loop, &idler);
    // 启用 idle handle 并传入回调
    uv_idle_start(&idler, wait_for_a_while);

    printf("Idling... \n");

    // ==========================================
    // http://docs.libuv.org/en/v1.x/loop.html
    // ==========================================

    // int uv_run(uv_loop_t* loop, uv_run_mode mode)
    // This function runs the event loop. It will act differently depending on the specified mode:

    // UV_RUN_DEFAULT: Runs the event loop until there are no more active and referenced handles or requests.
    // Returns non-zero if uv_stop() was called and there are still active handles or requests. Returns zero in all other cases.
    //
    // UV_RUN_ONCE: Poll for i/o once. Note that this function blocks if there are no pending callbacks.
    // Returns zero when done (no active handles or requests left), or non-zero if more callbacks
    // are expected (meaning you should run the event loop again sometime in the future).
    //
    // UV_RUN_NOWAIT: Poll for i/o once but don’t block if there are no pending callbacks.
    // Returns zero if done (no active handles or requests left), or non-zero if more callbacks
    // are expected (meaning you should run the event loop again sometime in the future).

    // 开始轮询,直到该loop中没有活动的引用
    // 在此例中,当uv_idle_stop(handle)调用后,所有的活动执行完成
    // 此时将会停止loop
    uv_run(loop, UV_RUN_DEFAULT);

    // 关闭轮询
    // Releases all internal loop resources.
    uv_loop_close(loop);

    return 0;
}
Esempio n. 29
0
File: tcp.c Progetto: kazupon/libuv
int uv_tcp_listen(uv_tcp_t* tcp, int backlog, uv_connection_cb cb) {
  static int single_accept = -1;

  if (tcp->delayed_error)
    return uv__set_sys_error(tcp->loop, tcp->delayed_error);

  if (single_accept == -1) {
    const char* val = getenv("UV_TCP_SINGLE_ACCEPT");
    single_accept = (val == NULL) || (atoi(val) != 0); /* on by default */
  }

  if (!single_accept)
    goto no_single_accept;

  tcp->idle_handle = malloc(sizeof(*tcp->idle_handle));
  if (tcp->idle_handle == NULL)
    return uv__set_sys_error(tcp->loop, ENOMEM);

  if (uv_idle_init(tcp->loop, tcp->idle_handle))
    abort();

  tcp->flags |= UV_TCP_SINGLE_ACCEPT;

no_single_accept:
  if (maybe_new_socket(tcp, AF_INET, UV_STREAM_READABLE))
    return -1;

  if (listen(tcp->fd, backlog))
    return uv__set_sys_error(tcp->loop, errno);

  tcp->connection_cb = cb;

  /* Start listening for connections. */
  uv__io_set(&tcp->read_watcher, uv__server_io, tcp->fd, UV__IO_READ);
  uv__io_start(tcp->loop, &tcp->read_watcher);

  return 0;
}
Esempio n. 30
0
void uv_eio_init(uv_loop_t* loop) {
  if (loop->flags & UV_LOOP_EIO_INITIALIZED) return;
  loop->flags |= UV_LOOP_EIO_INITIALIZED;

  uv_idle_init(loop, &loop->uv_eio_poller);
  uv_idle_start(&loop->uv_eio_poller, uv_eio_do_poll);
  loop->uv_eio_poller.flags |= UV__HANDLE_INTERNAL;

  loop->uv_eio_want_poll_notifier.data = loop;
  uv_async_init(loop,
                &loop->uv_eio_want_poll_notifier,
                uv_eio_want_poll_notifier_cb);
  loop->uv_eio_want_poll_notifier.flags |= UV__HANDLE_INTERNAL;
  uv__handle_unref(&loop->uv_eio_want_poll_notifier);

  uv_async_init(loop,
                &loop->uv_eio_done_poll_notifier,
                uv_eio_done_poll_notifier_cb);
  loop->uv_eio_done_poll_notifier.flags |= UV__HANDLE_INTERNAL;
  uv__handle_unref(&loop->uv_eio_done_poll_notifier);

  uv_once(&uv__eio_init_once_guard, uv__eio_init);
}