Example #1
0
/**
 * This one is asynchronously triggered, so as to ensure we don't have any
 * silly re-entrancy issues.
 */
static void socket_closing_cb(uv_idle_t *idle, int status)
{
    my_sockdata_t *sock = idle->data;

    uv_idle_stop(idle);
    uv_close((uv_handle_t *)idle, generic_close_cb);

    if (sock->pending.read) {
        /**
         * UV doesn't invoke read callbacks once the handle has been closed
         * so we must track this ourselves.
         */
        lcb_assert(sock->pending.read == 1);
        uv_read_stop((uv_stream_t *)&sock->tcp);
        sock->pending.read--;
        decref_sock(sock);
    }

#ifdef DEBUG
    if (sock->pending.read || sock->pending.write) {
        sock_dump_pending(sock);
    }
#endif

    decref_sock(sock);
    sock_do_uv_close(sock);

    (void)status;
}
Example #2
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--;
  }
}
Example #3
0
/* is reexecuted until uv_idle_stop is called */
void wait_for_a_while(uv_idle_t* handle, int status) {
    counter++;

    if (counter >= 10e6)
        /* stops the idle handle */ 
        uv_idle_stop(handle);
}
Example #4
0
int
release_main_loop()
{
    int returnValue = 0;

    SOL_DBG("Entering with state %s", RESOLVE_MAINLOOP_STATE(mainloopState));
    if (mainloopState == MAINLOOP_RELEASED ||
        mainloopState == MAINLOOP_RELEASING_STARTED) {
        return returnValue;
    }

    SOL_DBG("Stopping token handle");
    returnValue = uv_prepare_stop(&uv_token_handle);
    if (returnValue) {
        return returnValue;
    }

    // hijack_main_loop() was called, but the idler has not run yet
    if (mainloopState == MAINLOOP_HIJACKING_STARTED) {
        SOL_DBG("idler has not run yet, so stopping it");
        returnValue = uv_idle_stop(&uv_idle);
        if (!returnValue) {
            mainloopState = MAINLOOP_RELEASED;
        }
    } else {
        SOL_DBG("quitting main loop");
        mainloopState = MAINLOOP_RELEASING_STARTED;
        sol_quit();
    }
    return returnValue;
}
Example #5
0
static void idle_cb(uv_idle_t* handle, int status) {
  ASSERT(handle == &idle_handle);
  ASSERT(status == 0);

  if (++idle_counter == NUM_TICKS)
    uv_idle_stop(handle);
}
Example #6
0
void waitForAWhile(uv_idle_t *handle) {
    counter++;

    if (counter >= 10e6) {
        uv_idle_stop(handle);
    }
}
Example #7
0
static int luv_idle_stop(lua_State* L) {
  uv_idle_t* handle = luv_check_idle(L, 1);
  int ret = uv_idle_stop(handle);
  if (ret < 0) return luv_error(L, ret);
  lua_pushinteger(L, ret);
  return 1;
}
Example #8
0
static void
paint_cb(uv_idle_t *idle)
{
    Data *data = idle->data;
    cg_error_t *error = NULL;
    const cg_gles2_vtable_t *gles2 = data->gles2_vtable;

    /* Draw scene with GLES2 */
    if (!cg_push_gles2_context(
            data->dev, data->gles2_ctx, data->fb, data->fb, &error)) {
        c_error("Failed to push gles2 context: %s\n", error->message);
    }

    /* Clear offscreen framebuffer with a random color */
    gles2->glClearColor(
        c_random_double(), c_random_double(), c_random_double(), 1.0f);
    gles2->glClear(GL_COLOR_BUFFER_BIT);

    cg_pop_gles2_context(data->dev);

    /* Draw scene with Cogl */
    cg_primitive_draw(data->triangle, data->fb, data->pipeline);

    cg_onscreen_swap_buffers(CG_ONSCREEN(data->fb));

    uv_idle_stop(&data->idle);
}
Example #9
0
static void
lws_uv_idle(uv_idle_t *handle
#if UV_VERSION_MAJOR == 0
		, int status
#endif
)
{
	struct lws_context_per_thread *pt = lws_container_of(handle,
					struct lws_context_per_thread, uv_idle);

	lwsl_debug("%s\n", __func__);

	/*
	 * is there anybody with pending stuff that needs service forcing?
	 */
	if (!lws_service_adjust_timeout(pt->context, 1, pt->tid)) {
		/* -1 timeout means just do forced service */
		lws_plat_service_tsi(pt->context, -1, pt->tid);
		/* still somebody left who wants forced service? */
		if (!lws_service_adjust_timeout(pt->context, 1, pt->tid))
			/* yes... come back again later */
			lwsl_debug("%s: done again\n", __func__);
			return;
	}

	/* there is nobody who needs service forcing, shut down idle */
	uv_idle_stop(handle);

	lwsl_debug("%s: done stop\n", __func__);
}
Example #10
0
 ALWAYS_INLINE void releaseHandle(uv_idle_ext_t *handle) {
     if(handle->start){
         uv_idle_stop((uv_idle_t *) handle);
     }
     uv_unref((uv_handle_t *) handle);
     delete handle;
 }
Example #11
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);
}
Example #12
0
void wait_for_a_while(uv_idle_t *handle) {
    counter++;
    if (counter >= 10e6) {
        // 当执行规定次数后停止 handle
        printf("wait_for_a_while was called %lli times", counter);
        uv_idle_stop(handle);
    }
}
Example #13
0
static void uv__next_accept(uv_idle_t* idle, int status) {
  uv_stream_t* stream = idle->data;

  uv_idle_stop(idle);

  if (stream->accepted_fd == -1)
    uv__io_start(stream->loop, &stream->read_watcher);
}
Example #14
0
File: main.c Project: songtzu/study
static void 
idle_routine(uv_idle_t* h, int status)
{
  ++counter;

  if (counter >= 10e6)
    uv_idle_stop(h);
}
Example #15
0
/// Stops watching for events from a `RStream` instance.
///
/// @param rstream The `RStream` instance
void rstream_stop(RStream *rstream)
{
  if (rstream->file_type == UV_FILE) {
    uv_idle_stop(rstream->fread_idle);
  } else {
    uv_read_stop(rstream->stream);
  }
}
Example #16
0
/** @internal Continue with coroutine. */
static void l_ffi_resume_cb(uv_idle_t *check)
{
    lua_State *L = check->data;
    int status = l_resume(L, 0);
    if (status != LUA_YIELD) {
        uv_idle_stop(check); /* Stop coroutine */
        uv_close((uv_handle_t *)check, (uv_close_cb)free);
    }
    lua_pop(L, lua_gettop(L));
}
Example #17
0
/**
 * Callback which will be called upon each event loop tick.
 */
void callback(uv_idle_t* handle) {
    int* counter = (int*) handle->data;
    if (*counter == 10) {
        uv_idle_stop(handle);
    } else {
        printf("tick callback.counter = %d\n", *counter);
        (*counter)++;
        handle->data = counter;
    }
}
Example #18
0
void
lws_libuv_destroyloop(struct lws_context *context, int tsi)
{
	struct lws_context_per_thread *pt = &context->pt[tsi];
//	struct lws_context *ctx;
	int m, budget = 100, ns;

	if (!lws_check_opt(context->options, LWS_SERVER_OPTION_LIBUV))
		return;

	if (!pt->io_loop_uv)
		return;

	lwsl_notice("%s: closing signals + timers context %p\n", __func__, context);

	if (context->use_ev_sigint) {
		uv_signal_stop(&pt->w_sigint.uv_watcher);

		ns = ARRAY_SIZE(sigs);
		if (lws_check_opt(context->options, LWS_SERVER_OPTION_UV_NO_SIGSEGV_SIGFPE_SPIN))
			ns = 2;

		for (m = 0; m < ns; m++) {
			uv_signal_stop(&pt->signals[m]);
			uv_close((uv_handle_t *)&pt->signals[m], lws_uv_close_cb);
		}
	}

	uv_timer_stop(&pt->uv_timeout_watcher);
	uv_close((uv_handle_t *)&pt->uv_timeout_watcher, lws_uv_close_cb);

	uv_idle_stop(&pt->uv_idle);
	uv_close((uv_handle_t *)&pt->uv_idle, lws_uv_close_cb);

	if (pt->ev_loop_foreign)
		return;

	while (budget-- && uv_run(pt->io_loop_uv, UV_RUN_NOWAIT))
		;

	lwsl_notice("%s: closing all loop handles context %p\n", __func__, context);

	uv_stop(pt->io_loop_uv);

	uv_walk(pt->io_loop_uv, lws_uv_walk_cb, NULL);

	while (uv_run(pt->io_loop_uv, UV_RUN_NOWAIT))
		;
#if UV_VERSION_MAJOR > 0
	m = uv_loop_close(pt->io_loop_uv);
	if (m == UV_EBUSY)
		lwsl_err("%s: uv_loop_close: UV_EBUSY\n", __func__);
#endif
	lws_free(pt->io_loop_uv);
}
Example #19
0
int uv_close(uv_handle_t* handle, uv_close_cb close_cb) {
  uv_tcp_t* tcp;
  uv_async_t* async;
  uv_timer_t* timer;

  handle->close_cb = close_cb;

  switch (handle->type) {
    case UV_TCP:
      tcp = (uv_tcp_t*) handle;
      uv_read_stop((uv_stream_t*)tcp);
      ev_io_stop(EV_DEFAULT_ &tcp->write_watcher);
      break;

    case UV_PREPARE:
      uv_prepare_stop((uv_prepare_t*) handle);
      break;

    case UV_CHECK:
      uv_check_stop((uv_check_t*) handle);
      break;

    case UV_IDLE:
      uv_idle_stop((uv_idle_t*) handle);
      break;

    case UV_ASYNC:
      async = (uv_async_t*)handle;
      ev_async_stop(EV_DEFAULT_ &async->async_watcher);
      ev_ref(EV_DEFAULT_UC);
      break;

    case UV_TIMER:
      timer = (uv_timer_t*)handle;
      if (ev_is_active(&timer->timer_watcher)) {
        ev_ref(EV_DEFAULT_UC);
      }
      ev_timer_stop(EV_DEFAULT_ &timer->timer_watcher);
      break;

    default:
      assert(0);
      return -1;
  }

  uv_flag_set(handle, UV_CLOSING);

  /* This is used to call the on_close callback in the next loop. */
  ev_idle_start(EV_DEFAULT_ &handle->next_watcher);
  ev_feed_event(EV_DEFAULT_ &handle->next_watcher, EV_IDLE);
  assert(ev_is_pending(&handle->next_watcher));

  return 0;
}
Example #20
0
 static int64_t HHVM_METHOD(UVIdle, stop) {
     auto* data = Native::data<UVNativeData>(this_);
     uv_idle_ext_t *idle_handle = fetchResource(data);
     int64_t ret = 0;
     if(idle_handle->start){
         ret = uv_idle_stop((uv_idle_t *) idle_handle);
         this_->decRefAndRelease();
         idle_handle->start = false;            
     }
     return ret;
 }    
Example #21
0
void wait_for_a_while(uv_idle_t* handle) {
   static int counter=0;

   log_info(LOG, "Idle %d", counter);

   counter++;
   if (counter > 10) {
      uv_idle_stop(handle);
      LOG->close(LOG);
   }
}
Example #22
0
static void pollEvents(uv_idle_t* handle, int status) {
	AppInfo* info = (AppInfo*)handle->data;
	double lastEventTime = info->lastEventTime;
	double lastUptateTime = info->lastUptateTime;

	GLFWwindow* window = info->window;

	glfwPollEvents();
	HttpClient::pollEvents();

	if(glfwWindowShouldClose(window)) {
		uv_idle_stop(handle);
		info->shouldQuit = 1;
		return;
	}
	
	double t, dt;
	
	t = glfwGetTime();
	info->lastEventTime = t;
	dt = t - lastUptateTime;
	if(dt < 0.016) {
	//	return;
	}
	info->lastUptateTime = t;

	dt = t - lastEventTime;

	int winWidth, winHeight;
	int fbWidth, fbHeight;
	glfwGetWindowSize(window, &winWidth, &winHeight);
	glfwGetFramebufferSize(window, &fbWidth, &fbHeight);

	if(winWidth != info->width || winHeight != info->height) {
		V8Wrapper::resize(winWidth, winHeight);
		info->width = winWidth;
		info->height = winHeight;
	}

	// Update and render
	glViewport(0, 0, fbWidth, fbHeight);glClearColor(0.3f, 0.3f, 0.32f, 1.0f);
	glClearColor(0.3f, 0.3f, 0.32f, 1.0f);
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
////////////////////////////////////////////////////////////////////////////////////
	V8Wrapper::tick(t, dt);
////////////////////////////////////////////////////////////////////////////////////

	glfwSwapBuffers(window);

	return;
}
Example #23
0
static void do_run(uv_idle_t *runner) {
   zmq_channel = circus_zmq_client(MEMORY, LOG, config);
   if (zmq_channel == NULL) {
      log_error(LOG, "Could not allocate zmq_channel");
      LOG->free(LOG);
      config->free(config);
      exit(1);
   }

   mh = circus_message_handler(MEMORY, LOG, config);
   if (mh == NULL) {
      log_error(LOG, "Could not allocate message handler");
      zmq_channel->free(zmq_channel);
      LOG->free(LOG);
      config->free(config);
      exit(1);
   }

   cgi_channel = circus_cgi(MEMORY, LOG, config);
   if (cgi_channel == NULL) {
      log_error(LOG, "Could not allocate cgi_channel");
      mh->free(mh);
      LOG->free(LOG);
      config->free(config);
      exit(1);
   }

   ch = circus_cgi_handler(MEMORY, LOG, config);
   if (ch == NULL) {
      log_error(LOG, "Could not allocate CGI handler");
      cgi_channel->free(cgi_channel);
      mh->free(mh);
      zmq_channel->free(zmq_channel);
      LOG->free(LOG);
      config->free(config);
      exit(1);
   }

   automaton = new_automaton(MEMORY, LOG);
   assert(automaton->state(automaton) == State_started);
   mh->register_to(mh, zmq_channel, automaton);
   ch->register_to(ch, cgi_channel, automaton);

   automaton->on_state(automaton, State_finished, finished, NULL);
   automaton->set_state(automaton, State_read_from_client, NULL);

   log_info(LOG, "Client started.");

   uv_idle_stop(runner);
}
Example #24
0
/******************************************************************************
 ******************************************************************************
 ** Async Errors                                                             **
 ******************************************************************************
 ******************************************************************************/
static void err_idle_cb(uv_idle_t *idle, int status)
{
    my_uvreq_t *uvr = (my_uvreq_t *)idle;
    lcb_io_error_cb callback = uvr->cb.err;

    uv_idle_stop(idle);
    uv_close((uv_handle_t *)idle, generic_close_cb);

    if (callback) {
        callback(&uvr->socket->base);
    }

    decref_sock(uvr->socket);
    (void)status;
}
Example #25
0
// Called by the by the 'idle' handle to emulate a reading event
static void fread_idle_cb(uv_idle_t *handle)
{
  uv_fs_t req;
  RStream *rstream = handle->data;

  rstream->uvbuf.base = rstream->buffer + rstream->wpos;
  rstream->uvbuf.len = rstream->buffer_size - rstream->wpos;

  // the offset argument to uv_fs_read is int64_t, could someone really try
  // to read more than 9 quintillion (9e18) bytes?
  // DISABLED TO FIX BROKEN BUILD ON 32bit
  // TODO(elmart): Review types to allow assertion
  // assert(rstream->fpos <= INT64_MAX);

  // Synchronous read
  uv_fs_read(
      uv_default_loop(),
      &req,
      rstream->fd,
      &rstream->uvbuf,
      1,
      (int64_t) rstream->fpos,
      NULL);

  uv_fs_req_cleanup(&req);

  if (req.result <= 0) {
    uv_idle_stop(rstream->fread_idle);
    emit_read_event(rstream, true);
    return;
  }

  // no errors (req.result (ssize_t) is positive), it's safe to cast.
  size_t nread = (size_t) req.result;

  rstream->wpos += nread;
  rstream->fpos += nread;

  if (rstream->wpos == rstream->buffer_size) {
    // The last read filled the buffer, stop reading for now
    rstream_stop(rstream);
  }

  emit_read_event(rstream, false);
}
Example #26
0
// Called by the by the 'idle' handle to emulate a reading event
static void fread_idle_cb(uv_idle_t *handle)
{
    uv_fs_t req;
    RStream *rstream = handle->data;

    rstream->uvbuf.base = rstream->buffer + rstream->wpos;
    rstream->uvbuf.len = rstream->buffer_size - rstream->wpos;

    // the offset argument to uv_fs_read is int64_t, could someone really try
    // to read more than 9 quintillion (9e18) bytes?
    // upcast is meant to avoid tautological condition warning on 32 bits
    uintmax_t fpos_intmax = rstream->fpos;
    assert(fpos_intmax <= INT64_MAX);

    // Synchronous read
    uv_fs_read(
        uv_default_loop(),
        &req,
        rstream->fd,
        &rstream->uvbuf,
        1,
        (int64_t) rstream->fpos,
        NULL);

    uv_fs_req_cleanup(&req);

    if (req.result <= 0) {
        uv_idle_stop(rstream->fread_idle);
        emit_read_event(rstream, true);
        return;
    }

    // no errors (req.result (ssize_t) is positive), it's safe to cast.
    size_t nread = (size_t) req.result;

    rstream->wpos += nread;
    rstream->fpos += nread;

    if (rstream->wpos == rstream->buffer_size) {
        // The last read filled the buffer, stop reading for now
        rstream_stop(rstream);
    }

    emit_read_event(rstream, false);
}
Example #27
0
int uv_close(uv_handle_t* handle) {
    switch (handle->type) {
    case UV_TCP:
        ev_io_stop(EV_DEFAULT_ &handle->write_watcher);
        ev_io_stop(EV_DEFAULT_ &handle->read_watcher);
        break;

    case UV_PREPARE:
        uv_prepare_stop(handle);
        break;

    case UV_CHECK:
        uv_check_stop(handle);
        break;

    case UV_IDLE:
        uv_idle_stop(handle);
        break;

    case UV_ASYNC:
        ev_async_stop(EV_DEFAULT_ &handle->async_watcher);
        ev_ref(EV_DEFAULT_UC);
        break;

    case UV_TIMER:
        if (ev_is_active(&handle->timer_watcher)) {
            ev_ref(EV_DEFAULT_UC);
        }
        ev_timer_stop(EV_DEFAULT_ &handle->timer_watcher);
        break;

    default:
        assert(0);
        return -1;
    }

    uv_flag_set(handle, UV_CLOSING);

    /* This is used to call the on_close callback in the next loop. */
    ev_idle_start(EV_DEFAULT_ &handle->next_watcher);
    ev_feed_event(EV_DEFAULT_ &handle->next_watcher, EV_IDLE);
    assert(ev_is_pending(&handle->next_watcher));

    return 0;
}
Example #28
0
static void
uv_idle_callback()
{
    SOL_DBG("Entering with state %s", RESOLVE_MAINLOOP_STATE(mainloopState));
    if (mainloopState == MAINLOOP_HIJACKING_STARTED) {
        SOL_DBG("running sol_run()");
        mainloopState = MAINLOOP_HIJACKED;
        sol_run();
        SOL_DBG("sol_run() has returned. state is %s",
            RESOLVE_MAINLOOP_STATE(mainloopState));
        if (mainloopState == MAINLOOP_RELEASING_STARTED) {
            mainloopState = MAINLOOP_RELEASED;
        }
    } else if ( mainloopState == MAINLOOP_HIJACKED) {
        SOL_DBG("main loop already hijacked. Stopping idler");
        uv_idle_stop(&uv_idle);
    }
}
Example #29
0
static void paint_cb(uv_idle_t *idle)
{
    struct demo *demo = idle->data;
    unsigned int i;

    update_catherine_wheel(demo);

    cg_framebuffer_clear4f(demo->fb,
                           CG_BUFFER_BIT_COLOR | CG_BUFFER_BIT_DEPTH,
                           0, 0, 0, 1);

    for (i = 0; i < C_N_ELEMENTS(demo->emitter); i++)
        particle_emitter_paint(demo->emitter[i]);

    cg_onscreen_swap_buffers(demo->fb);

    uv_idle_stop(&demo->idle);
}
Example #30
0
static void
elops_destroy_pt_uv(struct lws_context *context, int tsi)
{
	struct lws_context_per_thread *pt = &context->pt[tsi];
	int m, ns;

	lwsl_info("%s: %d\n", __func__, tsi);

	if (!lws_check_opt(context->options, LWS_SERVER_OPTION_LIBUV))
		return;

	if (!pt->uv.io_loop)
		return;

	if (pt->event_loop_destroy_processing_done)
		return;

	pt->event_loop_destroy_processing_done = 1;

	if (!pt->event_loop_foreign) {
		uv_signal_stop(&pt->w_sigint.uv.watcher);

		ns = LWS_ARRAY_SIZE(sigs);
		if (lws_check_opt(context->options,
				  LWS_SERVER_OPTION_UV_NO_SIGSEGV_SIGFPE_SPIN))
			ns = 2;

		for (m = 0; m < ns; m++) {
			uv_signal_stop(&pt->uv.signals[m]);
			uv_close((uv_handle_t *)&pt->uv.signals[m],
				 lws_uv_close_cb_sa);
		}
	} else
		lwsl_debug("%s: not closing pt signals\n", __func__);

	uv_timer_stop(&pt->uv.timeout_watcher);
	uv_close((uv_handle_t *)&pt->uv.timeout_watcher, lws_uv_close_cb_sa);
	uv_timer_stop(&pt->uv.hrtimer);
	uv_close((uv_handle_t *)&pt->uv.hrtimer, lws_uv_close_cb_sa);

	uv_idle_stop(&pt->uv.idle);
	uv_close((uv_handle_t *)&pt->uv.idle, lws_uv_close_cb_sa);
}