Commit 8d7eb6e3 by Konstantin Käfer

Merge branch 'cross-platform-threading' into windows

Conflicts:
	src/async.h
	src/statement.h
	wscript
parents 5072f702 50c1d423
#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 uv_async handler.
template <class Item, class Parent> class Async {
......@@ -8,7 +14,7 @@ template <class Item, class Parent> class Async {
protected:
uv_async_t watcher;
pthread_mutex_t mutex;
NODE_SQLITE3_MUTEX_t
std::vector<Item*> data;
Callback callback;
public:
......@@ -18,16 +24,16 @@ public:
Async(Parent* parent_, Callback cb_)
: callback(cb_), parent(parent_) {
watcher.data = this;
pthread_mutex_init(&mutex, NULL);
NODE_SQLITE3_MUTEX_INIT
uv_async_init(uv_default_loop(), &watcher, listener);
}
static void listener(uv_async_t* handle, int status) {
Async* async = static_cast<Async*>(handle->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++) {
uv_unref(uv_default_loop());
async->callback(async->parent, rows[i]);
......@@ -53,9 +59,9 @@ public:
void add(Item* item) {
// Make sure node runs long enough to deliver the messages.
uv_ref(uv_default_loop());
pthread_mutex_lock(&mutex);
NODE_SQLITE3_MUTEX_LOCK(&mutex);
data.push_back(item);
pthread_mutex_unlock(&mutex);
NODE_SQLITE3_MUTEX_UNLOCK(&mutex)
}
void send() {
......@@ -68,7 +74,7 @@ public:
}
~Async() {
pthread_mutex_destroy(&mutex);
NODE_SQLITE3_MUTEX_DESTROY
}
};
......
......@@ -622,11 +622,10 @@ void Statement::Work_Each(uv_work_t* 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)
uv_async_send(&async->watcher);
}
......@@ -659,9 +658,9 @@ void Statement::AsyncEach(uv_async_t* handle, int status) {
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;
......
......@@ -5,6 +5,7 @@
#include <node.h>
#include "database.h"
#include "threading.h"
#include <cstdlib>
#include <cstring>
......@@ -158,7 +159,7 @@ public:
uv_async_t watcher;
Statement* stmt;
Rows data;
pthread_mutex_t mutex;
NODE_SQLITE3_MUTEX_t;
bool completed;
int retrieved;
......@@ -170,7 +171,7 @@ public:
Async(Statement* st, uv_async_cb async_cb) :
stmt(st), completed(false), retrieved(0) {
watcher.data = this;
pthread_mutex_init(&mutex, NULL);
NODE_SQLITE3_MUTEX_INIT
stmt->Ref();
uv_async_init(uv_default_loop(), &watcher, async_cb);
}
......@@ -179,7 +180,7 @@ public:
stmt->Unref();
item_cb.Dispose();
completed_cb.Dispose();
pthread_mutex_destroy(&mutex);
NODE_SQLITE3_MUTEX_DESTROY
}
};
......
#ifndef NODE_SQLITE3_SRC_THREADING_H
#define NODE_SQLITE3_SRC_THREADING_H
#if defined(NODE_SQLITE3_BOOST_THREADING)
#include <boost/thread/mutex.hpp>
#define NODE_SQLITE3_MUTEX_t boost::mutex mutex;
#define NODE_SQLITE3_MUTEX_INIT
#define NODE_SQLITE3_MUTEX_LOCK(m) (*m).lock();
#define NODE_SQLITE3_MUTEX_UNLOCK(m) (*m).unlock();
#define NODE_SQLITE3_MUTEX_DESTROY mutex.unlock();
#else
#define NODE_SQLITE3_MUTEX_t pthread_mutex_t mutex;
#define NODE_SQLITE3_MUTEX_INIT pthread_mutex_init(&mutex,NULL);
#define NODE_SQLITE3_MUTEX_LOCK(m) pthread_mutex_lock(m);
#define NODE_SQLITE3_MUTEX_UNLOCK(m) pthread_mutex_unlock(m);
#define NODE_SQLITE3_MUTEX_DESTROY pthread_mutex_destroy(&mutex);
#endif
#endif // NODE_SQLITE3_SRC_THREADING_H
\ No newline at end of file
......@@ -21,7 +21,7 @@ BUNDLED_SQLITE3_TAR = 'sqlite-autoconf-%s.tar.gz' % BUNDLED_SQLITE3_VERSION
SQLITE3_TARGET = 'deps/%s' % BUNDLED_SQLITE3
sqlite3_test_program = '''
#include "stdio.h"
#include <cstdio>"
#ifdef __cplusplus
extern "C" {
#endif
......@@ -36,6 +36,16 @@ main() {
}
'''
boost_thread_test_program = '''
#include <boost/thread/mutex.hpp>
int
main() {
boost::mutex mutex_;
return 0;
}
'''
def set_options(opt):
opt.tool_options("compiler_cxx")
......@@ -46,6 +56,13 @@ def set_options(opt):
, dest='sqlite3_dir'
)
opt.add_option( '--with-boost'
, action='store'
, default=None
, help='Directory prefix containing boost "lib" and "include" files (default is to use pthreads not boost for threading support)'
, dest='boost_dir'
)
def _conf_exit(conf,msg):
conf.fatal('\n\n' + msg + '\n...check the build/config.log for details')
......@@ -75,10 +92,28 @@ def configure(conf):
msg='Checking for libsqlite3 at %s' % lib,
includes=include):
Utils.pprint('GREEN', 'Sweet, found viable sqlite3 dependency at: %s ' % o.sqlite3_dir)
else:
_conf_exit(conf,'sqlite3 libs/headers not found at %s' % o.sqlite3_dir)
linkflags = []
if o.boost_dir:
lib, include = _build_paths(conf,o.boost_dir)
if conf.check_cxx(lib='boost_thread',
fragment=boost_thread_test_program,
uselib_store='SQLITE3',
libpath=lib,
msg='Checking for libboost_thread at %s' % lib,
includes=include):
Utils.pprint('GREEN', 'Sweet, found viable boost dependency at: %s ' % o.boost_dir)
conf.env.append_value("CXXFLAGS_SQLITE3", ['-DNODE_SQLITE3_BOOST_THREADING'])
else:
_conf_exit(conf,'boost libs/headers not found at %s' % o.boost_dir)
else:
conf.env.append_value("CXXFLAGS_SQLITE3", ["-pthread"])
if os.environ.has_key('LINKFLAGS'):
linkflags.extend(os.environ['LINKFLAGS'].split(' '))
......@@ -128,7 +163,7 @@ def build(bld):
obj = bld.new_task_gen("cxx", "shlib", "node_addon")
build_internal_sqlite3(bld)
obj.cxxflags = ["-g", "-D_FILE_OFFSET_BITS=64", "-D_LARGEFILE_SOURCE",
"-DSQLITE_ENABLE_RTREE=1", "-pthread", "-Wall"]
"-DSQLITE_ENABLE_RTREE=1", "-Wall"]
# uncomment the next line to remove '-undefined dynamic_lookup'
# in order to review linker errors (v8, libev/eio references can be ignored)
#obj.env['LINKFLAGS_MACBUNDLE'] = ['-bundle']
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment