Esempio n. 1
0
void connection_consumer_start(void *arg)
{
    int rc;
    struct server_ctx *ctx;
    uv_loop_t* loop;
    
    ctx = arg;
    loop = uv_loop_new();
    listener_event_loops[ctx->index] = *loop;
    
    http_request_cache_configure_listener(loop, &listener_async_handles[ctx->index]);
    uv_barrier_wait(listeners_created_barrier);
    
    rc = uv_async_init(loop, &ctx->async_handle, connection_consumer_close);
    uv_unref((uv_handle_t*) &ctx->async_handle);
    
    /* Wait until the main thread is ready. */
    uv_sem_wait(&ctx->semaphore);
    get_listen_handle(loop, (uv_stream_t*) &ctx->server_handle);
    uv_sem_post(&ctx->semaphore);
    
    rc = uv_listen((uv_stream_t*)&ctx->server_handle, 128, connection_consumer_new_connection);
    rc = uv_run(loop, UV_RUN_DEFAULT);
    
    uv_loop_delete(loop);
}
Esempio n. 2
0
Node::Node(int recvLength, int prePadding, int postPadding, bool useDefaultLoop) {
    nodeData = new NodeData;
    nodeData->recvBufferMemoryBlock = new char[recvLength];
    nodeData->recvBuffer = nodeData->recvBufferMemoryBlock + prePadding;
    nodeData->recvLength = recvLength - prePadding - postPadding;

    nodeData->tid = pthread_self();

    if (useDefaultLoop) {
        loop = uv_default_loop();
    } else {
        loop = uv_loop_new();
    }

    nodeData->loop = loop;
    nodeData->asyncMutex = &asyncMutex;

    int indices = NodeData::getMemoryBlockIndex(NodeData::preAllocMaxSize) + 1;
    nodeData->preAlloc = new char*[indices];
    for (int i = 0; i < indices; i++) {
        nodeData->preAlloc[i] = nullptr;
    }

    nodeData->clientContext = SSL_CTX_new(SSLv23_client_method());
    SSL_CTX_set_options(nodeData->clientContext, SSL_OP_NO_SSLv3);
}
Esempio n. 3
0
/*
 * Class:     com_iwebpp_libuvpp_handles_LoopHandle
 * Method:    _new
 * Signature: ()J
 */
extern "C" JNIEXPORT  jlong JNICALL Java_com_iwebpp_libuvpp_handles_LoopHandle__1new
  (JNIEnv *env, jclass cls) {

  uv_loop_t* ptr = uv_loop_new();
  assert(ptr);
  return reinterpret_cast<jlong>(ptr);
}
Esempio n. 4
0
LCBUV_API
lcb_error_t lcb_create_libuv_io_opts(int version,
                                     lcb_io_opt_t *io,
                                     lcbuv_options_t *options)
{
    lcb_io_opt_t iop;
    uv_loop_t *loop = NULL;
    my_iops_t *ret;

    if (version != 0) {
        return LCB_PLUGIN_VERSION_MISMATCH;
    }

#ifdef _WIN32
    {
        /** UV unloading on Windows doesn't work well */
        HMODULE module;
        /* We need to provide a symbol */
        static int dummy;
        BOOL result;
        result = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
                                   GET_MODULE_HANDLE_EX_FLAG_PIN,
                                   (LPCSTR)&dummy,
                                   &module);
        if (!result) {
            return LCB_EINTERNAL;
        }
    }
#endif

    ret = calloc(1, sizeof(*ret));

    if (!ret) {
        return LCB_CLIENT_ENOMEM;
    }

    iop = &ret->base;
    iop->version = 2;
    iop->destructor = iops_lcb_dtor;
    iop->v.v2.get_procs = wire_iops2;

    ret->iops_refcount = 1;

    *io = iop;
    if (options) {
        if (options->v.v0.loop) {
            ret->external_loop = 1;
            loop = options->v.v0.loop;
        }
        ret->startstop_noop = options->v.v0.startsop_noop;
    }

    if (!loop) {
        loop = uv_loop_new();
    }

    ret->loop = loop;

    return LCB_SUCCESS;
}
Esempio n. 5
0
static void loop_creating_worker(void* context) {
  (void) context;

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

    loop = uv_loop_new();
    ASSERT(loop != NULL);

    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);
    ASSERT(r == 0);

    uv_loop_delete(loop);

    increment_counter(&loop_creation_counter);
  } while (!stop);
}
Esempio n. 6
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 = uv_loop_new();
  ASSERT(loop != NULL);

  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);

  uv_loop_delete(loop);
  thread->thread_called = 1;
}
Esempio n. 7
0
extern "C" void*
rust_uv_loop_new() {
// XXX libuv doesn't always ignore SIGPIPE even though we don't need it.
#ifndef __WIN32__
    signal(SIGPIPE, SIG_IGN);
#endif
    return (void*)uv_loop_new();
}
Esempio n. 8
0
int main() {
    uv_loop_t *loop = uv_loop_new();

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

    return 0;
}
Esempio n. 9
0
/**
 * create loop, mutex, and start the thread
 */
UVEventLoop::UVEventLoop() {
	runUV = false;
	loop = uv_loop_new();
	if (uv_mutex_init(&mutex) < 0) { // oops
		;
	}
	StartUVRunner();
}
Esempio n. 10
0
int main() {
    uv_loop_t *loop = uv_loop_new();

    printf("Hello World!\n");
    uv_run(loop, UV_RUN_DEFAULT);

    return 0;
}
Esempio n. 11
0
TSockSys::TSockSys() {
	// only one instance alloved
	IAssert(!Active);
	// initialize loop
	Loop = uv_loop_new();
	// done
	Active = true;
}
Esempio n. 12
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. 13
0
static void worker_thread(void* arg) {
    ls_worker_t* w = (ls_worker_t*)arg;

    w->worker_loop = uv_loop_new();
    uv_async_init(w->worker_loop, &(w->worker_async), worker_async_callback);
    w->worker_started = 1;

    uv_run(w->worker_loop, UV_RUN_DEFAULT);
    LOG("  worker_thread[%p] terminate\n", w);
}
Esempio n. 14
0
HTTP::HTTP() 
  :loop(NULL)
{

  loop = uv_loop_new();
  if(!loop) {
    RX_ERROR("Cannot allocate an uv_loop");
    ::exit(EXIT_FAILURE);
  }

}
Esempio n. 15
0
struct EventBase* EventBase_new(struct Allocator* allocator)
{
    struct Allocator* alloc = Allocator_child(allocator);
    struct EventBase_pvt* base = Allocator_calloc(alloc, sizeof(struct EventBase_pvt), 1);
    base->loop = uv_loop_new();
    base->alloc = alloc;
    Identity_set(base);

    Allocator_onFree(alloc, onFree, base);
    calibrateTime(base);
    return &base->pub;
}
Esempio n. 16
0
/* Initializes a new thread context. Note that this doesn't set up a
 * thread itself, it just creates the data structure that exists in
 * MoarVM per thread. */
MVMThreadContext * MVM_tc_create(MVMInstance *instance) {
    MVMThreadContext *tc = calloc(1, sizeof(MVMThreadContext));

    /* Associate with VM instance. */
    tc->instance = instance;

    /* Set up GC nursery. */
    tc->nursery_fromspace   = calloc(1, MVM_NURSERY_SIZE);
    tc->nursery_tospace     = calloc(1, MVM_NURSERY_SIZE);
    tc->nursery_alloc       = tc->nursery_tospace;
    tc->nursery_alloc_limit = (char *)tc->nursery_alloc + MVM_NURSERY_SIZE;

    /* Set up temporary root handling. */
    tc->num_temproots   = 0;
    tc->alloc_temproots = 16;
    tc->temproots       = malloc(sizeof(MVMCollectable **) * tc->alloc_temproots);

    /* Set up intergenerational root handling. */
    tc->num_gen2roots   = 0;
    tc->alloc_gen2roots = 64;
    tc->gen2roots       = malloc(sizeof(MVMCollectable *) * tc->alloc_gen2roots);

    /* Set up the second generation allocator. */
    tc->gen2 = MVM_gc_gen2_create(instance);

    /* Set up table of per-static-frame chains. */
    /* XXX For non-first threads, make them start with the size of the
       main thread's table. or, look into lazily initializing this. */
    tc->frame_pool_table_size = MVMInitialFramePoolTableSize;
    tc->frame_pool_table = calloc(MVMInitialFramePoolTableSize, sizeof(MVMFrame *));

    tc->loop = instance->default_loop ? uv_loop_new() : uv_default_loop();

    /* Create a CallCapture for usecapture instructions in this thread (needs
     * special handling in initial thread as this runs before bootstrap). */
    if (instance->CallCapture)
        tc->cur_usecapture = MVM_repr_alloc_init(tc, instance->CallCapture);

    /* Initialize random number generator state. */
    tc->rand_state[0] = 'T';
    tc->rand_state[1] = 'M';

#if MVM_HLL_PROFILE_CALLS
#define PROFILE_INITIAL_SIZE (1 << 29)
    tc->profile_data_size = PROFILE_INITIAL_SIZE;
    tc->profile_data = malloc(sizeof(MVMProfileRecord) * PROFILE_INITIAL_SIZE);
    tc->profile_index = 0;
#endif

    return tc;
}
Esempio n. 17
0
        ThriftClient::ThriftClient()
        {
            _contextQueue = gcnew ContextQueue();
            _transportPool = gcnew Queue<FrameTransport^>();

            _loop = uv_loop_new();

            _notifier = new uv_async_t();
            _notifier->data = _contextQueue->ToPointer();
            uv_async_init(_loop, _notifier, NotifyCompleted);

            _stop = new uv_async_t();
            uv_async_init(_loop, _stop, StopCompleted);
        }
Esempio n. 18
0
uv_loop_t* uv_default_loop(void) {
  int tid;

  tid = uv_getThreadKeyId();
  if (tid == -1) {
    return uv_default_loop_ex();
  }

  if (loops[tid] == NULL) {
    loops[tid] = uv_loop_new();
  }

  return loops[tid];
}
Esempio n. 19
0
rava_thread_t* ravaL_thread_create(rava_state_t* outer, int narg)
{
  lua_State* L = outer->L;
  int base;

  /* ..., func, arg1, ..., argN */
  base = lua_gettop(L) - narg + 1;
  rava_thread_t* self = (rava_thread_t*)lua_newuserdata(L, sizeof(rava_thread_t));

  luaL_getmetatable(L, RAVA_PROCESS_THREAD);
  lua_setmetatable(L, -2);
  lua_insert(L, base++);

  self->type  = RAVA_STATE_TYPE_THREAD;
  self->flags = RAVA_STATE_READY;
  self->loop  = uv_loop_new();
  self->curr  = (rava_state_t*)self;
  self->L     = luaL_newstate();
  self->outer = outer;
  self->data  = NULL;

  QUEUE_INIT(&self->rouse);

  uv_async_init(self->loop, &self->async, _async_cb);
  uv_unref((uv_handle_t*)&self->async);

  luaL_openlibs(self->L);
  luaopen_rava(self->L);

  lua_settop(self->L, 0);
  ravaL_serialize_encode(L, narg);
  luaL_checktype(L, -1, LUA_TSTRING);
  lua_xmove(L, self->L, 1);

  /* keep a reference for reverse lookup in child */
  lua_pushthread(self->L);
  lua_pushlightuserdata(self->L, (void*)self);
  lua_rawset(self->L, LUA_REGISTRYINDEX);

  uv_thread_create(&self->tid, _thread_enter, self);

  /* inserted udata below function, so now just udata on top */
  TRACE("HERE TOP: %i, base: %i\n", lua_gettop(L), base);

  lua_settop(L, base - 1);

  return self;
}
Esempio n. 20
0
int main() {
    /* initialize a pointer of struct type uv_loop_t */
    uv_loop_t *loop;
    
    /* assign pointer to return pointer of uv_loop_new */
    loop = uv_loop_new();

    /* print out hello world */
    printf("Hello World\n");

    /* starts the event loop in UV_RUN_DEFAULT mode */
    uv_run(loop, UV_RUN_DEFAULT);

    /* return zero if everything went well */
    return 0;
}
Esempio n. 21
0
int main(int argc, char **argv) {
  loop = uv_loop_new(); //uv_default_loop();

  uv_tcp_t server;
  uv_tcp_init(loop, &server);

  struct sockaddr_in bind_addr = uv_ip4_addr("0.0.0.0", 7000);
  uv_tcp_bind(&server, bind_addr);
  int r = uv_listen((uv_stream_t*) &server, 128, on_new_connection);
  if (r) {
    fprintf(stderr, "Listen error %s\n", uv_err_name(uv_last_error(loop)));
    return 1;
  }

  return uv_run(loop);
}
Esempio n. 22
0
// --------------------
int main() {
  char client_key_file[1024];
  sprintf(client_key_file, "%s/%s", dirname(__FILE__), "client-key.pem");
 
  // Initialize SSL
  SSL_library_init();
  SSL_load_error_strings();

  BIO* bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
  SSL_CTX* ssl_ctx = SSL_CTX_new(SSLv23_client_method());
  int rc = SSL_CTX_use_PrivateKey_file(ssl_ctx, client_key_file, SSL_FILETYPE_PEM);
  if(!rc) {
    EXIT("Could not load client key file.\n");
  }

  SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_SSLv2); 
  SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_PEER, dummy_ssl_verify_callback); // our callback always returns true, so no validation
  SSL_CTX_set_info_callback(ssl_ctx, dummy_ssl_info_callback);  // for dibugging
  SSL_CTX_set_msg_callback(ssl_ctx, dummy_ssl_msg_callback);

  uv_loop_t* loop = uv_loop_new();

  // Client context
  Client c;
  c.loop = loop;
  c.connect_req.data = &c;
  c.socket.data = &c;
  c.ssl = NULL;
  c.ssl_ctx = ssl_ctx;

  sprintf(c.host, "%s", "test.localhost");
  sprintf(c.port, "%s", "443");
  sprintf(c.page, "%s", "/chunked.php");

  // Resolve host
  struct addrinfo hints;
  hints.ai_family = PF_INET;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_protocol = IPPROTO_TCP;
  hints.ai_flags = 0;
  uv_getaddrinfo_t resolver;
  resolver.data = &c;
  int r = uv_getaddrinfo(loop, &resolver, on_resolved_callback, c.host, c.port, &hints);
  uv_run(loop, UV_RUN_DEFAULT);
  return 0;
}
Esempio n. 23
0
Error TLSConnectionPrivate::Connect(const std::string &ipaddr, int port, TLSConnectionOptions *opts) {
	int err;

	loop_ = uv_loop_new();

	struct sockaddr_in addr = uv_ip4_addr(ipaddr.c_str(), port);

	err = uv_tcp_init(loop_, &tcpsock_);
	if (err != UV_OK) {
		return UVUtils::ErrorFromLastUVError(loop_);
	}

	if (opts != nullptr) {
		uv_tcp_nodelay(&tcpsock_, opts->tcp_no_delay ? 1 : 0);
	}

	tcpconn_.data = static_cast<void *>(this);
	tcpsock_.data = static_cast<void *>(this);
	err = uv_tcp_connect(&tcpconn_, &tcpsock_, addr, TLSConnectionPrivate::OnConnect);
	if (err != UV_OK) {
		return UVUtils::ErrorFromLastUVError(loop_);
	}

	uv_mutex_init(&wqlock_);
	uv_async_init(loop_, &wqasync_, TLSConnectionPrivate::OnDrainWriteQueue);
	wqasync_.data = this;

	uv_async_init(loop_, &dcasync_, TLSConnectionPrivate::OnDisconnectRequest);
	dcasync_.data = this;

	state_ = TLS_CONNECTION_STATE_PRE_CONNECT;

	err = uv_thread_create(&thread_, TLSConnectionPrivate::TLSConnectionThread, this);
	if (err  == -1) {
		return Error::ErrorFromDescription(
			std::string("TLSConnection"),
			0L,
			std::string("unable to create connection thread")
		);
	}

	return Error::NoError();
}
Esempio n. 24
0
    std::future<response> client::send(const request& req_)
    {
        return std::async(std::launch::async, [=]() -> response {
            response res;

            struct addrinfo hints;

            hints.ai_family     = PF_INET;
            hints.ai_socktype   = SOCK_STREAM;
            hints.ai_protocol   = IPPROTO_TCP;
            hints.ai_flags      = 0;

            uv_getaddrinfo_t resolver;

            data_t data;
            data.req    = &req_;
            data.res    = &res;
            data.loop   = uv_loop_new();

            resolver.data = &data;

            int r = uv_getaddrinfo(
                data.loop,
                &resolver,
                onResolved,
                req_.getUrl().getHost().c_str(),
                std::to_string(req_.getUrl().getPort()).c_str(),
                &hints
            );

            if (r)
            {
                throw std::runtime_error(uv_err_name(r));
            }

            uv_run(data.loop, UV_RUN_DEFAULT);

            uv_loop_delete(data.loop);

            return res;
        });
    }
Esempio n. 25
0
pc_client_t *pc_client_new() {
  pc_client_t *client = (pc_client_t *)pc_jsonp_malloc(sizeof(pc_client_t));

  if(!client) {
    fprintf(stderr, "Fail to malloc for pc_client_t.\n");
    abort();
  }

  memset(client, 0, sizeof(pc_client_t));

  client->uv_loop = uv_loop_new();
  if(client->uv_loop == NULL) {
    fprintf(stderr, "Fail to create uv_loop_t.\n");
    abort();
  }

  pc__client_init(client);

  return client;
}
Esempio n. 26
0
int main(int argc, char **argv)
{
    h2o_http1client_ctx_t ctx = {
        NULL,
        &zero_timeout,
        &io_timeout
    };

    if (argc != 2) {
        fprintf(stderr, "Usage: %s <url>\n", argv[0]);
        return 1;
    }
    url = argv[1];

    h2o_mempool_init(&pool);

    /* setup context */
#if H2O_USE_LIBUV
    ctx.loop = uv_loop_new();
#else
    ctx.loop = h2o_evloop_create();
#endif
    h2o_timeout_init(ctx.loop, &zero_timeout, 0);
    h2o_timeout_init(ctx.loop, &io_timeout, 5000); /* 5 seconds */

    /* setup the first request */
    start_request(&ctx);

    while (cnt_left != 0) {
#if H2O_USE_LIBUV
        uv_run(ctx.loop, UV_RUN_ONCE);
#else
        h2o_evloop_run(ctx.loop);
#endif
    }

    return 0;
}
Esempio n. 27
0
File: uvc.c Progetto: whtc123/libuvc
static uvc_thread_env *uvc_get_env(){
	uvc_ctx *ctx = NULL;
	uv_once(&once,uvc_init);
	uvc_thread_env *env=(uvc_thread_env *)uv_key_get(&uvc_key);
	if(env==NULL){
		env=(uvc_thread_env *)malloc(sizeof(uvc_thread_env));
		memset(env,0,sizeof(uvc_thread_env));
		env->loop = uv_loop_new();
		queue_init(&env->pending_queue);
		queue_init(&env->ready_queue);
		
		ctx = (uvc_ctx *)malloc(sizeof(uvc_ctx));
		memset(ctx, 0, sizeof(uvc_ctx));
		coro_stack_alloc(&ctx->stack, 0);
		coro_create(&ctx->cur, NULL, NULL, ctx->stack.sptr, ctx->stack.ssze);
		sprintf(ctx->name, "ROOT");
		env->schedule_task = ctx;
		env->runing_task = ctx;
		ctx->status = UVC_STATUS_RUNING;
		uv_key_set(&uvc_key,env);
	}
	return env;
}
Esempio n. 28
0
File: mo.cpp Progetto: wehu/mo
void Main::Init() {

    init_mo_object_type();

    // logger and event should be initialized first
    Logger::Init();
    Event::Init();

    uv_loop_t * loop = uv_loop_new();
    assert(loop!=NULL);

    Tick::Init();
    Timer::Init();
    Process::Init();
    Pipe::Init();
    TCP::Init();
    UDP::Init();
    FS::Init();
    TTY::Init();

    Module::Init();

}
Esempio n. 29
0
int
main(int argc, char *argv[])
{
    ProxyContext  proxy_context;
    uv_loop_t    *event_loop = uv_loop_new();

    stack_trace_on_crash();
    proxy_context_init(&proxy_context, event_loop, argc, argv);
    app_context.proxy_context = &proxy_context;
    logger_noformat(&proxy_context, LOG_INFO, "Generating a new key pair");
    dnscrypt_client_init_with_new_key_pair(&proxy_context.dnscrypt_client);
    logger_noformat(&proxy_context, LOG_INFO, "Done");

    if (cert_updater_init(&proxy_context) != 0 ||
            tcp_listener_bind(&proxy_context) != 0 ||
            udp_listener_bind(&proxy_context) != 0) {
        exit(1);
    }

    signal(SIGPIPE, SIG_IGN);
    revoke_privileges(&proxy_context);

    if (cert_updater_start(&proxy_context) != 0) {
        exit(1);
    }
    uv_run(event_loop);

    logger_noformat(&proxy_context, LOG_INFO, "Stopping proxy");
    cert_updater_stop(&proxy_context);
    tcp_listener_stop(&proxy_context);
    udp_listener_stop(&proxy_context);
    uv_loop_delete(event_loop);
    proxy_context_free(&proxy_context);
    alt_arc4random_close();

    return 0;
}
Esempio n. 30
0
 inline loop() : l(uv_loop_new()) {}