Starting point for command processing queue
This commit is contained in:
@@ -1,8 +1,9 @@
|
|||||||
/*
|
/*
|
||||||
* esqlite -- an erlang sqlite nif.
|
* Esqlite -- an erlang sqlite nif.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdio.h> /* for debugging */
|
#include <stdio.h> /* for debugging */
|
||||||
|
#include <assert.h>
|
||||||
#include <erl_nif.h>
|
#include <erl_nif.h>
|
||||||
|
|
||||||
#include "queue.h"
|
#include "queue.h"
|
||||||
@@ -70,6 +71,8 @@ command_create()
|
|||||||
|
|
||||||
cmd->type = cmd_unknown;
|
cmd->type = cmd_unknown;
|
||||||
cmd->ref = 0;
|
cmd->ref = 0;
|
||||||
|
|
||||||
|
return cmd;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -98,18 +101,17 @@ esqlite_db_run(void *arg)
|
|||||||
|
|
||||||
db->alive = 1;
|
db->alive = 1;
|
||||||
|
|
||||||
/* Wait for incoming commands and execute them */
|
|
||||||
while(1) {
|
while(1) {
|
||||||
cmd = get_command(db);
|
cmd = queue_pop(db->commands);
|
||||||
|
|
||||||
/* We are stopping... */
|
/* We are stopping... */
|
||||||
if(cmd_stop == command->type) {
|
if(cmd_stop == cmd->type) {
|
||||||
command_destroy(command);
|
command_destroy(cmd);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Evaluate the command */
|
/* Evaluate the command */
|
||||||
switch(command->type) {
|
switch(cmd->type) {
|
||||||
cmd_open:
|
cmd_open:
|
||||||
/* do open */
|
/* do open */
|
||||||
break;
|
break;
|
||||||
@@ -123,11 +125,12 @@ esqlite_db_run(void *arg)
|
|||||||
assert(0 && "Invalid command");
|
assert(0 && "Invalid command");
|
||||||
}
|
}
|
||||||
|
|
||||||
enif_send(NULL, &(command->pid), command->env, _atom_ok);
|
enif_send(NULL, &(cmd->pid), cmd->env, _atom_ok);
|
||||||
command_destroy(command);
|
command_destroy(cmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
db->alive = 0;
|
db->alive = 0;
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -147,18 +150,17 @@ start_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
|
|||||||
/* initialize the resource */
|
/* initialize the resource */
|
||||||
esqldb = enif_alloc_resource(esqlite_sqlite3_type, sizeof(esqlite_db));
|
esqldb = enif_alloc_resource(esqlite_sqlite3_type, sizeof(esqlite_db));
|
||||||
esqldb->db = NULL;
|
esqldb->db = NULL;
|
||||||
esqldb->alive = 0;
|
|
||||||
|
|
||||||
/* Start the command processing thread */
|
/* Start the command processing thread */
|
||||||
esqldb->opts = enif_thread_opts_create("esqldb_thread_opts");
|
esqldb->opts = enif_thread_opts_create("esqldb_thread_opts");
|
||||||
if(enif_thread_create("", &esqldb->tid, esqlite_db_run, esqldb, esqldb->opts) != 0) {
|
if(enif_thread_create("", &esqldb->tid, esqlite_db_run, esqldb, esqldb->opts) != 0) {
|
||||||
goto error;
|
enif_release_resource(esqldb);
|
||||||
|
return enif_make_tuple2(env, _atom_error, _atom_ok);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* */
|
/* We got the resource... now return it */
|
||||||
esqlite_db = enif_make_resource(env, esqldb);
|
esqlite_db = enif_make_resource(env, esqldb);
|
||||||
enif_release_resource(esqldb);
|
enif_release_resource(esqldb);
|
||||||
|
|
||||||
return enif_make_tuple2(env, _atom_ok, esqlite_db);
|
return enif_make_tuple2(env, _atom_ok, esqlite_db);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user