Example #1
0
static void tls_thread(void* arg) {
  ASSERT(NULL == uv_key_get(&tls_key));
  uv_key_set(&tls_key, arg);
  ASSERT(arg == uv_key_get(&tls_key));
  uv_key_set(&tls_key, NULL);
  ASSERT(NULL == uv_key_get(&tls_key));
}
Example #2
0
static void
signal_cb(uv_signal_t *handle, int signum) {
    if (signum == SIGINT || signum == SIGQUIT) {
        char *name = signum == SIGINT ? "SIGINT" : "SIGQUIT";
        logger_log(LOG_INFO, "Received %s, scheduling shutdown...", name);

        close_signal();

        if (concurrency > 1) {
            struct server_context *servers = handle->data;
            for (int i = 0; i < concurrency; i++) {
                struct server_context *server = &servers[i];
                uv_async_send(&server->async_handle);
            }

        } else {
            struct resolver_context *dns = uv_key_get(&thread_resolver_key);
            resolver_shutdown(dns);
            struct server_context *ctx = handle->data;
            uv_close((uv_handle_t *)&ctx->tcp, NULL);
            if (ctx->udprelay) {
                udprelay_close(ctx);
            }
        }
    }
    if (signum == SIGTERM) {
        logger_log(LOG_INFO, "Received SIGTERM, scheduling shutdown...");
        if (daemon_mode) {
            delete_pidfile(pidfile);
        }
        exit(0);
    }
}
Example #3
0
static void luv_work_cb(uv_work_t* req)
{
    uv_thread_t tid = uv_thread_self();
    luv_work_t* work = req->data;
    luv_work_ctx_t* ctx = work->ctx;
    lua_State *L = uv_key_get(&L_key);
    int top;
    if (L == NULL) {
        /* should vm reuse in pool? */
        L = acquire_vm_cb();

        uv_key_set(&L_key, L);
    }
    top = lua_gettop(L);
    if (luaL_loadbuffer(L, ctx->code, ctx->len, "=pool") == 0)
    {
        int i = luv_thread_arg_push(L, &work->arg);
        if (lua_pcall(L, i, LUA_MULTRET, 0)) {
            fprintf(stderr, "Uncaught Error in thread: %s\n", lua_tostring(L, -1));
        }
        luv_thread_arg_set(L, &work->arg, top, lua_gettop(L));
    } else {
        fprintf(stderr, "Uncaught Error: %s\n", lua_tostring(L, -1));
    }

    //release_vm_cb(L);
}
Example #4
0
void http_request_cache_timer(uv_timer_t* handle, int status)
{
    khash_t(string_hashmap)* http_request_cache = uv_key_get(&thread_cache_key);
    if (http_request_cache != NULL)
    {
        free_http_request_cache(http_request_cache);
    }
}
Example #5
0
void RecursiveLockGuard::release() {
    void* lkey = uv_key_get(mutex_key);
    if (lkey) {
        uv_mutex_unlock(mutex);
        uv_key_set(mutex_key, 0);
    }
    locked = false;
}
Example #6
0
RecursiveLockGuard::RecursiveLockGuard(uv_mutex_t* mutex_,
                                       uv_key_t* mutex_key_) 
    : mutex(mutex_), mutex_key(mutex_key_), locked(true) {
    void* lkey = uv_key_get(mutex_key);
    if (!lkey) {
        uv_mutex_lock(mutex);
        uv_key_set(mutex_key, (void*)1);
    }
}
Example #7
0
static mrb_value
mrb_uv_key_get(mrb_state *mrb, mrb_value self)
{
  uv_key_t *key;
  void *p;

  key = (uv_key_t*)mrb_uv_get_ptr(mrb, self, &mrb_uv_key_type);
  p = uv_key_get(key);
  return p? mrb_obj_value(p) : mrb_nil_value();
}
Example #8
0
CAMLprim value
camluv_key_get(value key)
{
  CAMLparam1(key);
  CAMLlocal1(key_value);

  camluv_key_t *camluv_key = camluv_key_struct_val(key);
  void *p = uv_key_get(&(camluv_key->uv_key));
  key_value = (value)p;

  CAMLreturn(key_value);
}
Example #9
0
void
resolve_remote(struct remote_context *remote, char *host, uint16_t port) {
    if (verbose) {
        logger_log(LOG_INFO, "resolve %s", host);
    }
    struct resolver_context *dns = uv_key_get(&thread_resolver_key);
    remote->stage = XSTAGE_RESOLVE;
    remote->host_query = resolver_query(dns, host, port, resolve_cb, remote);
    if (remote->host_query == NULL) {
        remote->stage = XSTAGE_TERMINATE;
        close_client(remote->client);
        close_remote(remote);
    }
}
Example #10
0
static void luv_work_cb(uv_work_t* req)
{
  uv_thread_t tid = uv_thread_self();
  luv_work_t* work = req->data;
  luv_work_ctx_t* ctx = work->ctx;
  lua_State *L = uv_key_get(&L_key);
  int top;
  if (L == NULL) {
    /* vm reuse in threadpool */
    L = acquire_vm_cb();
    uv_key_set(&L_key, L);
  }

  top = lua_gettop(L);
  lua_pushlstring(L, ctx->code, ctx->len);
  lua_rawget(L, LUA_REGISTRYINDEX);
  if (lua_isnil(L, -1))
  {
    lua_pop(L, 1);
    
    lua_pushlstring(L, ctx->code, ctx->len);
    if (luaL_loadbuffer(L, ctx->code, ctx->len, "=pool") != 0)
    {
      fprintf(stderr, "Uncaught Error: %s\n", lua_tostring(L, -1));
      lua_pop(L, 2);

      lua_pushnil(L);
    } else
    {
      lua_pushvalue(L, -1);
      lua_insert(L, lua_gettop(L) - 2);
      lua_rawset(L, LUA_REGISTRYINDEX);
    }
  }

  if (lua_isfunction(L, -1))
  {
    int i = luv_thread_arg_push(L, &work->arg);
    if (lua_pcall(L, i, LUA_MULTRET, 0)) {
      fprintf(stderr, "Uncaught Error in thread: %s\n", lua_tostring(L, -1));
    }
    luv_thread_arg_clear(&work->arg);
    luv_thread_arg_set(L, &work->arg, top + 1, lua_gettop(L));
    lua_settop(L, top);
  } else {
    fprintf(stderr, "Uncaught Error: %s can't be work entry\n", 
      lua_typename(L, lua_type(L,-1)));
  }
}
Example #11
0
File: uvc.c Project: 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;
}
Example #12
0
static mrb_value
mrb_uv_key_set(mrb_state *mrb, mrb_value self)
{
  uv_key_t *key;
  void *p;
  mrb_value new_val;
  mrb_value ary;

  mrb_get_args(mrb, "o", &new_val);

  if (mrb_type(new_val) < MRB_TT_HAS_BASIC) {
    mrb_raisef(mrb, E_TYPE_ERROR, "cannot store value without basic: %S", new_val);
  }

  key = (uv_key_t*)mrb_uv_get_ptr(mrb, self, &mrb_uv_key_type);
  p = uv_key_get(key);

  ary = mrb_iv_get(mrb, self, mrb_intern_lit(mrb, "values"));
  mrb_assert(mrb_array_p(ary));

  if (p) {
    /* remove value */
    int i, dst;
    for (i = 0, dst = 0; i < RARRAY_LEN(ary); ++i) {
      mrb_value v = RARRAY_PTR(ary)[i];
      if (mrb_ptr(v) != p) {
        mrb_ary_ptr(ary)->ptr[dst++] = v;
      }
    }
    RARRAY_LEN(ary) = dst;
  }

  uv_key_set(key, mrb_ptr(new_val));
  mrb_ary_push(mrb, ary, new_val); /* protect from GC */

  return new_val;
}
Example #13
0
wsn_thread_ctx_t* wsn_thread_ctx()
{
  return (wsn_thread_ctx_t*)uv_key_get(&tls_key_);
}
Example #14
0
wsn_thread_ctx_t* wsn_set_thread_ctx(wsn_thread_ctx_t *thread_ctx)
{
  wsn_thread_ctx_t *old_ctx = (wsn_thread_ctx_t*)uv_key_get(&tls_key_);
  uv_key_set(&tls_key_, thread_ctx);
  return old_ctx;
}