Example #1
0
static int _do_fork_fs_events_child(int file_or_dir) {
  /* basic fsevents work in the child after a fork */
  pid_t child_pid;
  uv_loop_t loop;

  /* Watch in the parent, prime the loop and/or threads. */
  assert_watch_file_current_dir(uv_default_loop(), file_or_dir);
  child_pid = fork();
  ASSERT(child_pid != -1);

  if (child_pid != 0) {
    /* parent */
    assert_wait_child(child_pid);
  } else {
    /* child */
    /* Ee can watch in a new loop, but dirs only work
       if we're on linux. */
#if defined(__APPLE__)
    file_or_dir = FS_TEST_FILE;
#endif
    printf("Running child\n");
    uv_loop_init(&loop);
    printf("Child first watch\n");
    assert_watch_file_current_dir(&loop, file_or_dir);
    ASSERT(0 == uv_loop_close(&loop));
    printf("Child second watch default loop\n");
    /* Ee can watch in the default loop. */
    ASSERT(0 == uv_loop_fork(uv_default_loop()));
    /* On some platforms (OS X), if we don't update the time now,
     * the timer cb fires before the event loop enters uv__io_poll,
     * instead of after, meaning we don't see the change! This may be
     * a general race.
     */
    uv_update_time(uv_default_loop());
    assert_watch_file_current_dir(uv_default_loop(), file_or_dir);

    /* We can close the parent loop successfully too. This is
       especially important on Apple platforms where if we're not
       careful trying to touch the CFRunLoop, even just to shut it
       down, that we allocated in the FS_TEST_DIR case would crash. */
    ASSERT(0 == uv_loop_close(uv_default_loop()));

    printf("Exiting child \n");
  }

  MAKE_VALGRIND_HAPPY();
  return 0;

}
Example #2
0
static void do_work(void* arg) {
  struct getaddrinfo_req getaddrinfo_reqs[16];
  struct fs_req fs_reqs[16];
  uv_loop_t* loop;
  size_t i;
  int r;
  struct test_thread* thread = arg;

  loop = malloc(sizeof *loop);
  ASSERT(loop != NULL);
  ASSERT(0 == uv_loop_init(loop));

  for (i = 0; i < ARRAY_SIZE(getaddrinfo_reqs); i++) {
    struct getaddrinfo_req* req = getaddrinfo_reqs + i;
    req->counter = 16;
    req->loop = loop;
    getaddrinfo_do(req);
  }

  for (i = 0; i < ARRAY_SIZE(fs_reqs); i++) {
    struct fs_req* req = fs_reqs + i;
    req->counter = 16;
    req->loop = loop;
    fs_do(req);
  }

  r = uv_run(loop, UV_RUN_DEFAULT);
  ASSERT(r == 0);

  ASSERT(0 == uv_loop_close(loop));
  free(loop);
  thread->thread_called = 1;
}
Example #3
0
LibuvTaskScheduler::~LibuvTaskScheduler()
{
	if(m_uv_loop)
	{
		uv_loop_close(m_uv_loop);
	}
}
static void loop_creating_worker(void* context) {
  (void) context;

  do {
    uv_loop_t *loop;
    uv_signal_t signal;
    int r;

    loop = malloc(sizeof(*loop));
    ASSERT(loop != NULL);
    ASSERT(0 == uv_loop_init(loop));

    r = uv_signal_init(loop, &signal);
    ASSERT(r == 0);

    r = uv_signal_start(&signal, signal_unexpected_cb, SIGTERM);
    ASSERT(r == 0);

    uv_close((uv_handle_t*) &signal, NULL);

    r = uv_run(loop, UV_RUN_DEFAULT);
    ASSERT(r == 0);

    uv_loop_close(loop);
    free(loop);

    increment_counter(&loop_creation_counter);
  } while (!stop);
}
Example #5
0
 Loop::~Loop() {
     stop_ = true;
     uv_async_send(&async_);
     thread_->join();
     UV_CHECK(uv_loop_close(loop_));
     free(loop_);
 }
Example #6
0
void ThreadManager::stop() {
    if (adaptor) {
        uv_run(&adaptor->main_loop, UV_RUN_DEFAULT);
        uv_loop_close(&adaptor->main_loop);
        adaptor.reset();
    }
}
Example #7
0
static int
elops_destroy_context2_uv(struct lws_context *context)
{
	struct lws_context_per_thread *pt;
	int n, internal = 0;

	for (n = 0; n < context->count_threads; n++) {
		pt = &context->pt[n];

		/* only for internal loops... */

		if (!pt->event_loop_foreign && pt->uv.io_loop) {
			internal = 1;
			if (!context->finalize_destroy_after_internal_loops_stopped)
				uv_stop(pt->uv.io_loop);
			else {
#if UV_VERSION_MAJOR > 0
				uv_loop_close(pt->uv.io_loop);
#endif
				lws_free_set_NULL(pt->uv.io_loop);
			}
		}
	}

	return internal;
}
Example #8
0
int main() {
    uv_loop_t loop;
    uv_loop_init(&loop);
    uv_run(&loop, UV_RUN_DEFAULT);
    uv_loop_close(&loop);
    return 0;
}
Example #9
0
Thread::~Thread()
{
	if (this->is_started)
		this->stop();

	uv_loop_close(&this->loop);
}
Example #10
0
void
lws_libuv_destroyloop(struct lws_context *context, int tsi)
{
	struct lws_context_per_thread *pt = &context->pt[tsi];
	int m;

	if (!(context->options & LWS_SERVER_OPTION_LIBUV))
		return;

	if (!pt->io_loop_uv)
		return;

	if (context->use_ev_sigint)
		uv_signal_stop(&pt->w_sigint.uv_watcher);
	for (m = 0; m < ARRAY_SIZE(sigs); m++)
		uv_signal_stop(&pt->signals[m]);
	if (!pt->ev_loop_foreign) {
		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));
		m = uv_loop_close(pt->io_loop_uv);
		if (m == UV_EBUSY)
			lwsl_debug("%s: uv_loop_close: UV_EBUSY\n", __func__);
		lws_free(pt->io_loop_uv);
	}
}
Example #11
0
static int unload_module(void) {
	unsigned int i = 0;

	LNOTICE("unloaded module %s", module_name);

	for (i = 0; i < profile_size; i++) {

		close_socket(i);
		free_profile(i);
	}
	
	                 
#if UV_VERSION_MAJOR == 0
	uv_async_send(&async_handle);
	uv_loop_delete(loop);
#else

	if (uv_loop_alive(loop)) {
        	uv_async_send(&async_handle);
	}
   
	uv_stop(loop);
	uv_loop_close(loop);
	free(loop);
#endif
	/* Close socket */
	return 0;
}
Example #12
0
static int
lwsws_get_config_d(void *user, const char *d, const char * const *paths,
		   int count_paths, lejp_callback cb)
{
	uv_dirent_t dent;
	uv_fs_t req;
	char path[256];
	int ret = 0;
	uv_loop_t loop;

	uv_loop_init(&loop);

	if (!uv_fs_scandir(&loop, &req, d, 0, NULL)) {
		lwsl_err("Scandir on %s failed\n", d);
		return 2;
	}

	while (uv_fs_scandir_next(&req, &dent) != UV_EOF) {
		snprintf(path, sizeof(path) - 1, "%s/%s", d, dent.name);
		ret = lwsws_get_config(user, path, paths, count_paths, cb);
		if (ret)
			goto bail;
	}

bail:
	uv_fs_req_cleanup(&req);
	uv_loop_close(&loop);

	return ret;
}
Example #13
0
void event_teardown(void)
{
  if (!deferred_events) {
    // Not initialized(possibly a --version invocation)
    return;
  }

  process_events_from(immediate_events);
  process_events_from(deferred_events);
  // reset the stop_flag to ensure `uv_run` below won't exit early. This hack
  // is required because the `process_events_from` above may call `event_push`
  // which will set the stop_flag to 1(uv_stop)
  uv_default_loop()->stop_flag = 0;
  input_stop_stdin();
  channel_teardown();
  job_teardown();
  server_teardown();
  signal_teardown();
  terminal_teardown();
  // this last `uv_run` will return after all handles are stopped, it will
  // also take care of finishing any uv_close calls made by other *_teardown
  // functions.
  uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  // abort that if we left unclosed handles
  if (uv_loop_close(uv_default_loop())) {
    abort();
  }
}
Example #14
0
int main() {
    uv_loop_t* loop = uv_default_loop();
    uv_idle_t handler = idleHandler(loop);
    uv_idle_start(&handler, callback);
    uv_run(loop, UV_RUN_DEFAULT);
    uv_loop_close(loop);
    return 0;
}
Example #15
0
int main() {
    uv_loop_t *loop = malloc(sizeof(uv_loop_t));
    uv_loop_init(loop);
    printf("Hello!!\n");
    uv_run(loop, UV_RUN_DEFAULT);
    uv_loop_close(loop);
    free(loop);
    return 0;
}
Example #16
0
 ~Loop()
 {
   if (alive()) {
     // this is to call the close callbacks of the watchers
     // so that their handle memory can get freed
     run();
   }
   int rc = uv_loop_close(m_uvloop.get());
   assert(rc==0 || print_error(rc));
 }
Example #17
0
/**
 * Just an example of using uv_loop_configure. Mainly showing how it is called
 * and I've not using it in a real sense yet.
 */
int main() {
    uv_loop_t* loop = uv_default_loop();
    // currently the only signal that is implemented is SIGPROF which is a signal
    // the kernel generates this signal upon the expiration of a profiling timer set by a call to settimer()
    int r = uv_loop_configure(loop, UV_LOOP_BLOCK_SIGNAL, SIGPROF);
    printf("uv_loop_configure returned %d\n", r);
    uv_run(loop, UV_RUN_DEFAULT);
    uv_loop_close(loop);
    return 0;
}
Example #18
0
static int loop_gc(lua_State *L) {
  uv_loop_t* loop = luv_loop(L);
  // Call uv_close on every active handle
  uv_walk(loop, walk_cb, NULL);
  // Run the event loop until all handles are successfully closed
  while (uv_loop_close(loop)) {
    uv_run(loop, UV_RUN_DEFAULT);
  }
  return 0;
}
Example #19
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 #20
0
int
lws_uv_plugins_destroy(struct lws_context *context)
{
	struct lws_plugin *plugin = context->plugin_list, *p;
	lws_plugin_destroy_func func;
	char path[256];
	int pofs = 0;
	void *v;
	int m;

#if  defined(__MINGW32__) || !defined(WIN32)
	pofs = 3;
#endif

	if (!plugin)
		return 0;

	while (plugin) {
		p = plugin;

#if !defined(WIN32) && !defined(__MINGW32__)
		m = lws_snprintf(path, sizeof(path) - 1, "destroy_%s",
				 plugin->name + pofs);
		path[m - 3] = '\0';
#else
		m = lws_snprintf(path, sizeof(path) - 1, "destroy_%s",
				 plugin->name + pofs);
		path[m - 4] = '\0';
#endif

		if (uv_dlsym(&plugin->lib, path, &v)) {
			uv_dlerror(&plugin->lib);
			lwsl_err("Failed to get %s on %s: %s", path,
					plugin->name, plugin->lib.errmsg);
		} else {
			func = (lws_plugin_destroy_func)v;
			m = func(context);
			if (m)
				lwsl_err("Destroying %s failed %d\n",
						plugin->name, m);
		}

		uv_dlclose(&p->lib);
		plugin = p->list;
		p->list = NULL;
		free(p);
	}

	context->plugin_list = NULL;

	while (uv_loop_close(&context->uv.loop))
		;

	return 0;
}
Example #21
0
int main()
{
    loop = uv_default_loop();
    uv_tcp_t* socket = (uv_tcp_t*)malloc(sizeof(uv_tcp_t));
    uv_tcp_init(loop, socket);

    uv_connect_t* connect = (uv_connect_t*)malloc(sizeof(uv_connect_t));
	//
    struct sockaddr_in dest;
    uv_ip4_addr("127.0.0.1", 7000, &dest);
    uv_tcp_connect(connect, socket,(struct sockaddr*)&dest, on_connect);
    uv_run(loop, UV_RUN_DEFAULT);

	uv_loop_close(socket);
	uv_loop_close(connect);
	free(socket);
	free(connect);

	return 0;
}
static void node_reg_test_setup_exit(uv_loop_t* ev) {
    size_t left_tick = 128 * 30; // 30s
    while (left_tick > 0 && UV_EBUSY == uv_loop_close(ev)) {
        uv_run(ev, UV_RUN_NOWAIT);
        CASE_THREAD_SLEEP_MS(8);
        
        -- left_tick;
    }
    
    CASE_EXPECT_NE(left_tick, 0);
}
Example #23
0
File: vm.c Project: 0x73/wren
static void freeVM()
{
  ioShutdown();
  schedulerShutdown();
  
  uv_loop_close(loop);
  free(loop);
  
  wrenFreeVM(vm);

  uv_tty_reset_mode();
}
Example #24
0
void async_destroy(void) {
	assert(async_loop);
	co_delete(trampoline); trampoline = NULL;
	uv_loop_close(async_loop);
	memset(async_loop, 0, sizeof(async_loop));

// TODO: Not safe for various libco backends?
#if defined(CORO_USE_VALGRIND)
	co_delete(master->fiber);
	memset(master, 0, sizeof(master));
#endif
}
Example #25
0
int main()
{
	uv_loop_t *loop = (uv_loop_t*)malloc(sizeof(uv_loop_t));
	uv_loop_init(loop);

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

	uv_loop_close(loop);
	free(loop);
	return 0;
}
Example #26
0
int strus_run_server( unsigned short port, unsigned int nofThreads, strus_globalctx_t* glbctx)
{
	int res;
	struct sockaddr_in addr;
#define NOF_SIGNALS 7
	int signalar[ NOF_SIGNALS] = {SIGINT,SIGILL,SIGABRT,SIGFPE,SIGSEGV,SIGTERM,SIGHUP};
	uv_signal_t signal[ NOF_SIGNALS];
	int ii;

	if (g_glbctx)
	{
		log_error( "cannot run two server instances");
		return -1;
	}
	g_glbctx = glbctx;

	memset( &g_server, 0, sizeof(g_server));
	uv_tcp_init( uv_default_loop(), &g_server);

	for (ii=0; ii<NOF_SIGNALS; ++ii)
	{
		uv_signal_init( g_server.loop, &signal[ ii]);
		uv_signal_start( &signal[ ii], on_signal, signalar[ ii]);
	}
#if (UV_VERSION_NUM>10)
	uv_ip4_addr( "0.0.0.0", port, &addr);
	res = uv_tcp_bind( &g_server, (const struct sockaddr*) &addr, 0);
#else
	addr = uv_ip4_addr( "0.0.0.0", port);
	res = uv_tcp_bind( &g_server, addr);
#endif
	if (res < 0)
	{
		log_error_sys( "error in bind", res);
		return -1;
	}
	res = uv_listen( (uv_stream_t*)&g_server, 256, on_connected);
	if (res < 0)
	{
		log_error_sys( "error in listen", res);
		return -2;
	}
	res = uv_run( g_server.loop, UV_RUN_DEFAULT);
	if (res != 0)
	{
		log_error( "server event loop was aborted");
	}
#if (UV_VERSION_NUM>10)
	uv_loop_close( g_server.loop);
#endif
	g_glbctx = 0;
	return res;
}
Example #27
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;
}
Example #28
0
int main() {
    uv_loop_t* loop = uv_default_loop();
    uv_loop_init(loop);

    uv_timer_t timer;
    uv_timer_init(loop, &timer);
    uv_timer_start(&timer, timer_callback, 0, 1000);

    uv_run(loop, UV_RUN_DEFAULT);

    uv_loop_close(loop);
    return 0;
}
Example #29
0
void ipc_send_recv_helper_threadproc(void* arg) {
  int r;
  uv_loop_t loop;

  r = uv_loop_init(&loop);
  ASSERT(r == 0);

  r = run_ipc_send_recv_helper(&loop, 1);
  ASSERT(r == 0);

  r = uv_loop_close(&loop);
  ASSERT(r == 0);
}
CASE_TEST(atbus_endpoint, get_connection)
{
    atbus::node::conf_t conf;
    atbus::node::default_conf(&conf);
    conf.children_mask = 16;
    uv_loop_t ev_loop;
    uv_loop_init(&ev_loop);

    conf.ev_loop = &ev_loop;
    conf.recv_buffer_size = 64 * 1024;

    char* buffer = new char[conf.recv_buffer_size];
    memset(buffer, -1, sizeof(conf.recv_buffer_size)); // init it and then valgrind will now report uninitialised used

    char addr[32] = { 0 };
    UTIL_STRFUNC_SNPRINTF(addr, sizeof(addr), "mem://0x%p", buffer);
    if (addr[8] == '0' && addr[9] == 'x') {
        memset(addr, 0, sizeof(addr));
        UTIL_STRFUNC_SNPRINTF(addr, sizeof(addr), "mem://%p", buffer);
    }

    // 排除未完成连接
    {
        atbus::node::ptr_t node = atbus::node::create();
        node->init(0x12345678, &conf);

        atbus::connection::ptr_t conn1 = atbus::connection::create(node.get());

        CASE_EXPECT_EQ(0, conn1->connect(addr));

        atbus::connection::ptr_t conn2 = atbus::connection::create(node.get());
        conn2->connect("ipv4://127.0.0.1:80");

        atbus::endpoint::ptr_t ep = atbus::endpoint::create(node.get(), 0x12345679, 8, node->get_pid(), node->get_hostname());
        CASE_EXPECT_TRUE(ep->add_connection(conn1.get(), false));
        CASE_EXPECT_TRUE(ep->add_connection(conn2.get(), false));

        CASE_EXPECT_EQ(0, node->add_endpoint(ep));

        atbus::connection* conn3 = node->get_self_endpoint()->get_data_connection(ep.get());
        CASE_EXPECT_EQ(conn3, conn1.get());

    }

    while (UV_EBUSY == uv_loop_close(&ev_loop)) {
        uv_run(&ev_loop, UV_RUN_ONCE);
    }

    delete []buffer;
}