/* * Start the processing thread */ static ERL_NIF_TERM esqlite_start(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { esqlite_connection *conn; ERL_NIF_TERM db_conn; /* Initialize the resource */ conn = enif_alloc_resource(esqlite_connection_type, sizeof(esqlite_connection)); if(!conn) return make_error_tuple(env, "no_memory"); conn->db = NULL; /* Create command queue */ conn->commands = queue_create(); if(!conn->commands) { enif_release_resource(conn); return make_error_tuple(env, "command_queue_create_failed"); } /* Start command processing thread */ conn->opts = enif_thread_opts_create("esqldb_thread_opts"); if(enif_thread_create("esqlite_connection", &conn->tid, esqlite_connection_run, conn, conn->opts) != 0) { enif_release_resource(conn); return make_error_tuple(env, "thread_create_failed"); } db_conn = enif_make_resource(env, conn); enif_release_resource(conn); return make_ok_tuple(env, db_conn); }
ctx_t* init_ctx() { int status; ctx_t *ctx = static_cast<ctx_t*>(enif_alloc_resource(res_type, sizeof(*ctx))); if (ctx == NULL) { goto done; } ctx->queue = async_queue_create(); ctx->topts = enif_thread_opts_create(const_cast<char*>("snappy_thread_opts")); status = enif_thread_create(const_cast<char*>("worker"), &ctx->tid, worker, ctx, ctx->topts); if (status != 0) { enif_release_resource(ctx); ctx = NULL; goto done; } done: return ctx; }
static int load(ErlNifEnv* env, void** priv, ERL_NIF_TERM load_info) { state_t* state = (state_t*) enif_alloc(sizeof(state_t)); if(state == NULL) return -1; state->queue = queue_create(); if(state->queue == NULL) goto error; state->opts = enif_thread_opts_create("thread_opts"); if(enif_thread_create( "", &(state->qthread), thr_main, state, state->opts ) != 0) { goto error; } state->atom_ok = enif_make_atom(env, "ok"); *priv = (void*) state; return 0; error: if(state->queue != NULL) queue_destroy(state->queue); //enif_free(state->queue); //double free bug ( already called free at queue_destroy() ) return -1; }
vm_ptr vm_init(ErlNifResourceType* res_type, JSRuntime* runtime, size_t stack_size) { vm_ptr vm = (vm_ptr) enif_alloc_resource(res_type, sizeof(struct vm_t)); if(vm == NULL) return NULL; vm->runtime = runtime; vm->curr_job = NULL; vm->stack_size = stack_size; vm->jobs = queue_create(); if(vm->jobs == NULL) goto error; vm->opts = enif_thread_opts_create("vm_thread_opts"); if(enif_thread_create("", &vm->tid, vm_run, vm, vm->opts) != 0) goto error; return vm; error: enif_release_resource(vm); return NULL; }
int worker_init(worker_t *worker,int id) { struct queue_t *q; ErlNifThreadOpts* opts; q = queue_create(); if(q == NULL ) { goto queue_error; } worker->q = q; worker->id =id; opts = enif_thread_opts_create("lua_thread_opts"); if(opts == NULL) { goto opts_error; } worker->opts = opts; if(enif_thread_create("lua_thread", &worker->tid, worker_run, worker, worker->opts) != 0) { goto create_error; } return 0; create_error: enif_thread_opts_destroy(opts); opts_error: queue_destroy(q); queue_error: return -1; }