Skip to content

Instantly share code, notes, and snippets.

@orlandov
Created March 1, 2010 19:41
Show Gist options
  • Select an option

  • Save orlandov/318729 to your computer and use it in GitHub Desktop.

Select an option

Save orlandov/318729 to your computer and use it in GitHub Desktop.
/*
I Have a C++ class Sqlite3Db that inherits from EventEmitter. It has a
private variable `db` of type (sqlite3*) which is the sqlite handle.
Is it okay to pass this handle to the function executing
in eio's threadpool?
*/
struct connect_request {
sqlite3 *db;
char filename[1];
};
static int eio_Connect(eio_req *req) {
// Note: this function is executed in the thread pool! CAREFUL
printf("eio_Connect\n");
struct connection_request *conn_req = (connection_request *) req->data;
// This is a pointer to the EventEmitter (Sqlite3Db)'s sqlite3 handle
sqlite3 *db = conn_req->db;
req->result = 0;
return 0;
}
// entry point to `db.connect(filename)`
static Handle<Value> Connect(const Arguments& args) {
HandleScope scope;
printf("Connect %s\n", *filename);
Sqlite3Db* dbo = ObjectWrap::Unwrap<Sqlite3Db>(args.This());
/*
Sqlite3Db has an operator overload for (sqlite3*) which returns the private `db` member
*/
sqlite3 *db = *dbo;
REQ_STR_ARG(0, filename);
struct connect_request *conn_req = (struct connect_request *)
calloc(1, sizeof(struct connect_request) + filename.length());
strcpy(conn_req->filename, *filename);
// PACK db handle
conn_req->db = db;
eio_custom(eio_Connect, EIO_PRI_DEFAULT, eio_AfterConnect, db);
ev_ref(EV_DEFAULT_UC);
printf("end\n");
return Undefined();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment