Esempio n. 1
0
static int Load(
    ErlNifEnv *env,
    void** priv_data,
    ERL_NIF_TERM load_info) {
  callsMutex = enif_mutex_create("erlcl_calls");
  return 0;
};
Esempio n. 2
0
queue *queue_create()
{
    queue *ret;
    // int i = 0;
    // qitem *item;

    ret = (queue *) enif_alloc(sizeof(struct queue_t));
    if(ret == NULL) goto error;

    // ret->freeitem = freecb;
    ret->lock = NULL;
    ret->cond = NULL;
    ret->head = NULL;
    ret->tail = NULL;
    ret->length = 0;

    ret->reuseq = NULL;

    ret->lock = enif_mutex_create("queue_lock");
    if(ret->lock == NULL) goto error;

    ret->cond = enif_cond_create("queue_cond");
    if(ret->cond == NULL) goto error;

    return ret;

error:
    if(ret->lock != NULL)
        enif_mutex_destroy(ret->lock);
    if(ret->cond != NULL)
        enif_cond_destroy(ret->cond);
    if(ret != NULL)
        enif_free(ret);
    return NULL;
}
Esempio n. 3
0
queue *
queue_new() {
    queue *ret;

    ret = (queue *) enif_alloc(sizeof(queue));
    if (ret == NULL)
      goto error;

    ret->lock = NULL;
    ret->cond = NULL;
    ret->head = NULL;
    ret->tail = NULL;
    ret->message = NULL;
    ret->length = 0;

    ret->lock = enif_mutex_create("queue_lock");
    if (ret->lock == NULL)
      goto error;

    ret->cond = enif_cond_create("queue_cond");
    if (ret->cond == NULL)
      goto error;

    return ret;

error:
    if (ret->lock != NULL)
      enif_mutex_destroy(ret->lock);
    if (ret->cond != NULL)
      enif_cond_destroy(ret->cond);
    if (ret != NULL)
      enif_free(ret);
    return NULL;
}
Esempio n. 4
0
    static int
load(ErlNifEnv *env, void **priv, ERL_NIF_TERM load_info)
{
    PRIVDATA *data = NULL;
    int nelem = 0;


    if (!enif_get_int(env, load_info, &nelem) || (nelem < 1))
        return (-1);

    data = (PRIVDATA *)enif_alloc(sizeof(PRIVDATA));

    if (data == NULL)
        return (-1);

    data->data = (PRIVVAL *)enif_alloc(sizeof(PRIVVAL) * nelem);
    if (data->data == NULL) {
        enif_free(data);
        return (-1);
    }

    (void)memset(data->data, 0, sizeof(PRIVVAL) * nelem);

    data->lock = enif_mutex_create("wat_lock");
    if (data->lock == NULL)
        return (-1);

    NELEM(data) = nelem;
    *priv = data;

    return (0);
}
Esempio n. 5
0
queue_ptr
queue_create(const char* name)
{
    queue_ptr ret;

    ret = (queue_ptr) enif_alloc(sizeof(struct queue_t));
    if(ret == NULL) goto error;

    ret->lock = NULL;
    ret->cond = NULL;
    ret->head = NULL;
    ret->tail = NULL;
    ret->message = NULL;
    ret->length = 0;

    ret->lock = enif_mutex_create("queue_lock");
    if(ret->lock == NULL) goto error;
    
    ret->cond = enif_cond_create("queue_cond");
    if(ret->cond == NULL) goto error;

    return ret;

error:
    if(ret->lock != NULL) enif_mutex_destroy(ret->lock);
    if(ret->cond != NULL) enif_cond_destroy(ret->cond);
    if(ret != NULL) enif_free(ret);
    return NULL;
}
Esempio n. 6
0
// return a new queue or null if it couldn't be allocated
queue_ptr queue_create( void (*destroy_node)(void*) )
{
	queue_ptr queue = (queue_ptr) node_alloc(sizeof(struct queue));

	if(NULL == queue)
		goto error;

	memset(queue, 0, sizeof(struct queue));
	queue->destroy_node = destroy_node;
	
	queue->lock = enif_mutex_create("queue_lock");

	if(NULL == queue->lock)
		goto error;

	queue->cond = enif_cond_create("queue_condition");
	if(NULL == queue->cond)
		goto error;

	return queue;

error:

	if(NULL != queue)
	{
		queue_destroy(queue);
	}

	return NULL;
}
Esempio n. 7
0
int
on_load(ErlNifEnv* env, void** priv, ERL_NIF_TERM info)
{
    UErrorCode status = U_ZERO_ERROR;
    int i, j;
    couch_ejson_global_ctx_t *globalCtx;

    globalCtx = (couch_ejson_global_ctx_t *) enif_alloc(sizeof(couch_ejson_global_ctx_t));

    if (globalCtx == NULL) {
        return 1;
    }

    if (!enif_get_int(env, info, &globalCtx->numCollators)) {
        return 2;
    }

    if (globalCtx->numCollators < 1) {
        return 3;
    }

    globalCtx->collMutex = enif_mutex_create("coll_mutex");

    if (globalCtx->collMutex == NULL) {
        return 4;
    }

    globalCtx->collators = (UCollator **) enif_alloc(sizeof(UCollator *) * globalCtx->numCollators);

    if (globalCtx->collators == NULL) {
        enif_mutex_destroy(globalCtx->collMutex);
        return 5;
    }

    for (i = 0; i < globalCtx->numCollators; i++) {
        globalCtx->collators[i] = ucol_open("", &status);

        if (U_FAILURE(status)) {
            for (j = 0; j < i; j++) {
                ucol_close(globalCtx->collators[j]);
            }

            enif_free(globalCtx->collators);
            enif_mutex_destroy(globalCtx->collMutex);

            return 5;
        }
    }

    globalCtx->collStackTop = 0;
    *priv = globalCtx;

    ATOM_TRUE = enif_make_atom(env, "true");
    ATOM_FALSE = enif_make_atom(env, "false");
    ATOM_NULL = enif_make_atom(env, "null");
    ATOM_ERROR = enif_make_atom(env, "error");

    return 0;
}
Esempio n. 8
0
queue_t* queue_new(void)
{
    queue_t* queue = (queue_t*)enif_alloc(sizeof(queue_t));
    queue->head = NULL;
    queue->tail = NULL;
    queue->mutex = enif_mutex_create("queue_mutex");
    queue->cond = enif_cond_create("queue_cond");
    return queue;
}
Esempio n. 9
0
File: nif.c Progetto: arekinath/e2qc
static struct cache *
new_cache(ERL_NIF_TERM atom, int max_size, int min_q1_size)
{
	struct cache *c;
	struct atom_node *an;
	int i;

	c = enif_alloc(sizeof(*c));
	memset(c, 0, sizeof(*c));
	c->max_size = max_size;
	c->min_q1_size = min_q1_size;
	c->lookup_lock = enif_rwlock_create("cache->lookup_lock");
	c->cache_lock = enif_rwlock_create("cache->cache_lock");
	c->ctrl_lock = enif_mutex_create("cache->ctrl_lock");
	c->check_cond = enif_cond_create("cache->check_cond");
	TAILQ_INIT(&(c->q1.head));
	TAILQ_INIT(&(c->q2.head));
	for (i = 0; i < N_INCR_BKT; ++i) {
		TAILQ_INIT(&(c->incr_head[i]));
		c->incr_lock[i] = enif_mutex_create("cache->incr_lock");
	}
	RB_INIT(&(c->expiry_head));

	an = enif_alloc(sizeof(*an));
	memset(an, 0, sizeof(*an));
	an->atom = enif_make_copy(gbl->atom_env, atom);
	an->cache = c;

	c->atom_node = an;

	enif_rwlock_rwlock(gbl->atom_lock);
	RB_INSERT(atom_tree, &(gbl->atom_head), an);
	/* start the background thread for the cache. after this, the bg thread now
	   owns the cache and all its data and will free it at exit */
	enif_thread_create("cachethread", &(c->bg_thread), cache_bg_thread, c, NULL);
	enif_rwlock_rwunlock(gbl->atom_lock);

	return c;
}
Esempio n. 10
0
void* nif_create_main_thread(char* name)
{
	nif_thread_state* st = (nif_thread_state*)enif_alloc(sizeof(nif_thread_state));

	st->lock = enif_mutex_create("esdl2_lock");
	st->cond = enif_cond_create("esdl2_cond");
	st->mailbox = (nif_thread_mailbox*)enif_alloc(sizeof(nif_thread_mailbox));
	TAILQ_INIT(st->mailbox);

	enif_thread_create(name, &(st->tid), nif_main_thread, st, NULL);

	return (void*)st;
}
Esempio n. 11
0
static nif_term_t
start(nif_heap_t *hp, int argc, const nif_term_t argv[])
{
	struct salt_pcb 	*sc;
	nif_cond_t 		*cv;
	nif_lock_t 		*lk;
	nif_term_t 		pcb;

	if (argc != 0)
		return (BADARG);

	/* Create thread control block, pass ownership to Erlang. */
	assert(salt_pcb_type != NULL);
	sc = enif_alloc_resource(salt_pcb_type, sizeof(*sc));
	if (sc == NULL)
		goto fail_0;

	cv = enif_cond_create("lots_pcb_cv");
	if (cv == NULL)
		goto fail_1;

	lk = enif_mutex_create("lots_pcb_lock");
	if (lk == NULL)
		goto fail_2;

	sc->sc_vsn = SALT_VSN(1, 0, 0);
	sc->sc_lock = lk;
	sc->sc_cond = cv;
	sc->sc_req_first = NULL;
	sc->sc_req_lastp = &sc->sc_req_first;
	sc->sc_req_npend = 0;
	sc->sc_exit_flag = false;

	if (enif_thread_create("salt_thread", &sc->sc_thread, salt_worker_loop, sc, NULL) != 0)
		goto fail_3;

	pcb = enif_make_resource(hp, sc);
	enif_release_resource(sc);

	return (pcb);

	/* Failure handling. */
 fail_3:
	enif_mutex_destroy(lk);
 fail_2:
	enif_cond_destroy(cv);
 fail_1:
	enif_release_resource(sc);
 fail_0:
	return (BADARG);
}
Esempio n. 12
0
static ErlCall *CreateCall(ERL_NIF_TERM fun, ERL_NIF_TERM args) {
  enif_mutex_lock(callsMutex);

  ErlCall *erlCall = (ErlCall *)malloc(sizeof(ErlCall));
  erlCall->id = id++;
  erlCall->env = enif_alloc_env();
  ERL_NIF_TERM msgFun = enif_make_copy(erlCall->env, fun);
  ERL_NIF_TERM msgArgs = enif_make_copy(erlCall->env, args);
  ERL_NIF_TERM msgId = enif_make_int(erlCall->env, erlCall->id);
  erlCall->msg = enif_make_tuple3(erlCall->env, msgId, msgFun, msgArgs);
  erlCall->cond = enif_cond_create("erlcl_cond");
  erlCall->mutex = enif_mutex_create("erlcl_mutex");
  erlCall->complete = 0;

  HASH_ADD_INT(calls, id, erlCall);

  enif_mutex_unlock(callsMutex);

  return erlCall;
}
Esempio n. 13
0
static int load(ErlNifEnv* env, void** priv, ERL_NIF_TERM load_info)
{
    NifModPrivData* data;
    int retval = 0;

    init(env);
    data = (NifModPrivData*) enif_alloc(sizeof(NifModPrivData));
    CHECK(data != NULL);
    *priv = data;
    data->mtx = enif_mutex_create("nif_mod_priv_data"); 
    data->ref_cnt = 1;
    data->call_history = NULL;

    add_call(env, data, "load");

    do_load_info(env, load_info, &retval);
    if (retval)
	NifModPrivData_release(data);
    return retval;
}
Esempio n. 14
0
static int
load(ErlNifEnv *env, void **priv_data, ERL_NIF_TERM load_info)
{
    PRIV *priv = NULL;

    atom_ok = enif_make_atom(env, "ok");
    atom_error = enif_make_atom(env, "error");
    atom_enomem = enif_make_atom(env, "enomem");

    priv = (PRIV *)enif_alloc(sizeof(PRIV));
    if (priv == NULL)
        return (-1);

    priv->dictpath = enif_make_string(env, GetDefaultCracklibDict(), ERL_NIF_LATIN1);

    priv->lock = enif_mutex_create("cerck_lock");
    if (priv->lock == NULL)
        return (-1);

    *priv_data = priv;

    return (0);
}
Esempio n. 15
0
static ERL_NIF_TERM alloc_msgenv(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    PrivData* priv = (PrivData*) enif_priv_data(env);
    struct make_term_info* mti;
    ERL_NIF_TERM ret;

    mti = (struct make_term_info*) enif_alloc_resource(msgenv_resource_type,
						       sizeof(*mti));
    mti->caller_env = NULL;
    mti->dst_env = enif_alloc_env();
    mti->reuse_push = 0;
    mti->reuse_pull = 0;
    mti->resource_type = priv->rt_arr[0].t;
    mti->other_term = enif_make_list(mti->dst_env, 0);
    mti->blob = enif_make_list(mti->dst_env, 0);
    mti->mtx = enif_mutex_create("nif_SUITE:mtx");
    mti->cond = enif_cond_create("nif_SUITE:cond");
    mti->send_res = 0xcafebabe;
    mti->n = 0;
    ret = enif_make_resource(env, mti);
    enif_release_resource(mti);
    return ret;
}