Skip to content
Browse files

enable usage of boost threads instead of direct posix/pthreads usage …

…- all tests are passing on OS X 10.7 with boost 1.47 - todo: test on linux - refs #48
  • Loading branch information...
1 parent 9ee3c90 commit 357f57bbe3ab25e2aec81dc657579d107b387758 @springmeyer springmeyer committed Nov 6, 2011
Showing with 21 additions and 16 deletions.
  1. +14 −8 src/async.h
  2. +4 −5 src/statement.cc
  3. +3 −3 src/statement.h
View
22 src/async.h
@@ -1,34 +1,40 @@
#ifndef NODE_SQLITE3_SRC_ASYNC_H
#define NODE_SQLITE3_SRC_ASYNC_H
+#include "threading.h"
+
+#if defined(NODE_SQLITE3_BOOST_THREADING)
+#include <boost/thread/mutex.hpp>
+#endif
+
// Generic ev_async handler.
template <class Item, class Parent> class Async {
typedef void (*Callback)(Parent* parent, Item* item);
protected:
ev_async watcher;
- pthread_mutex_t mutex;
+ NODE_SQLITE3_MUTEX_t
std::vector<Item*> data;
Callback callback;
public:
Parent* parent;
public:
inline Async(Parent* parent_, Callback cb_)
- : callback(cb_), parent(parent_) {
+ : mutex(),callback(cb_), parent(parent_) {
watcher.data = this;
ev_async_init(&watcher, listener);
ev_async_start(EV_DEFAULT_UC_ &watcher);
- pthread_mutex_init(&mutex, NULL);
+ NODE_SQLITE3_MUTEX_INIT
}
static void listener(EV_P_ ev_async *w, int revents) {
Async* async = static_cast<Async*>(w->data);
std::vector<Item*> rows;
- pthread_mutex_lock(&async->mutex);
+ NODE_SQLITE3_MUTEX_LOCK(&async->mutex)
rows.swap(async->data);
- pthread_mutex_unlock(&async->mutex);
+ NODE_SQLITE3_MUTEX_UNLOCK(&async->mutex)
for (unsigned int i = 0, size = rows.size(); i < size; i++) {
ev_unref(EV_DEFAULT_UC);
async->callback(async->parent, rows[i]);
@@ -38,9 +44,9 @@ template <class Item, class Parent> class Async {
inline void add(Item* item) {
// Make sure node runs long enough to deliver the messages.
ev_ref(EV_DEFAULT_UC);
- pthread_mutex_lock(&mutex);
+ NODE_SQLITE3_MUTEX_LOCK(&mutex)
data.push_back(item);
- pthread_mutex_unlock(&mutex);
+ NODE_SQLITE3_MUTEX_UNLOCK(&mutex)
}
inline void send() {
@@ -54,7 +60,7 @@ template <class Item, class Parent> class Async {
inline ~Async() {
ev_invoke(EV_DEFAULT_UC_ &watcher, ev_async_pending(&watcher));
- pthread_mutex_destroy(&mutex);
+ NODE_SQLITE3_MUTEX_DESTROY
ev_async_stop(EV_DEFAULT_UC_ &watcher);
}
};
View
9 src/statement.cc
@@ -636,11 +636,10 @@ int Statement::EIO_Each(eio_req *req) {
sqlite3_mutex_leave(mtx);
Row* row = new Row();
GetRow(row, stmt->handle);
-
- pthread_mutex_lock(&async->mutex);
+ NODE_SQLITE3_MUTEX_LOCK(&async->mutex)
async->data.push_back(row);
retrieved++;
- pthread_mutex_unlock(&async->mutex);
+ NODE_SQLITE3_MUTEX_UNLOCK(&async->mutex)
ev_async_send(EV_DEFAULT_ &async->watcher);
}
@@ -667,9 +666,9 @@ void Statement::AsyncEach(EV_P_ ev_async *w, int revents) {
while (true) {
// Get the contents out of the data cache for us to process in the JS callback.
Rows rows;
- pthread_mutex_lock(&async->mutex);
+ NODE_SQLITE3_MUTEX_LOCK(&async->mutex)
rows.swap(async->data);
- pthread_mutex_unlock(&async->mutex);
+ NODE_SQLITE3_MUTEX_UNLOCK(&async->mutex)
if (rows.empty()) {
break;
View
6 src/statement.h
@@ -156,7 +156,7 @@ class Statement : public EventEmitter {
ev_async watcher;
Statement* stmt;
Rows data;
- pthread_mutex_t mutex;
+ NODE_SQLITE3_MUTEX_t;
Persistent<Function> callback;
bool completed;
int retrieved;
@@ -171,14 +171,14 @@ class Statement : public EventEmitter {
callback = Persistent<Function>::New(cb);
completed_callback = Persistent<Function>::New(completed_cb);
stmt->Ref();
- pthread_mutex_init(&mutex, NULL);
+ NODE_SQLITE3_MUTEX_INIT
}
~Async() {
callback.Dispose();
completed_callback.Dispose();
stmt->Unref();
- pthread_mutex_destroy(&mutex);
+ NODE_SQLITE3_MUTEX_DESTROY
ev_async_stop(EV_DEFAULT_UC_ &watcher);
}
};

0 comments on commit 357f57b

Please sign in to comment.
Something went wrong with that request. Please try again.