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 #ifndef NODE_SQLITE3_SRC_ASYNC_H
#define 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. // Generic uv_async handler.
template <class Item, class Parent> class Async { template <class Item, class Parent> class Async {
...@@ -8,7 +14,7 @@ template <class Item, class Parent> class Async { ...@@ -8,7 +14,7 @@ template <class Item, class Parent> class Async {
protected: protected:
uv_async_t watcher; uv_async_t watcher;
pthread_mutex_t mutex; NODE_SQLITE3_MUTEX_t
std::vector<Item*> data; std::vector<Item*> data;
Callback callback; Callback callback;
public: public:
...@@ -18,16 +24,16 @@ public: ...@@ -18,16 +24,16 @@ public:
Async(Parent* parent_, Callback cb_) Async(Parent* parent_, Callback cb_)
: callback(cb_), parent(parent_) { : callback(cb_), parent(parent_) {
watcher.data = this; watcher.data = this;
pthread_mutex_init(&mutex, NULL); NODE_SQLITE3_MUTEX_INIT
uv_async_init(uv_default_loop(), &watcher, listener); uv_async_init(uv_default_loop(), &watcher, listener);
} }
static void listener(uv_async_t* handle, int status) { static void listener(uv_async_t* handle, int status) {
Async* async = static_cast<Async*>(handle->data); Async* async = static_cast<Async*>(handle->data);
std::vector<Item*> rows; std::vector<Item*> rows;
pthread_mutex_lock(&async->mutex); NODE_SQLITE3_MUTEX_LOCK(&async->mutex)
rows.swap(async->data); 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++) { for (unsigned int i = 0, size = rows.size(); i < size; i++) {
uv_unref(uv_default_loop()); uv_unref(uv_default_loop());
async->callback(async->parent, rows[i]); async->callback(async->parent, rows[i]);
...@@ -53,9 +59,9 @@ public: ...@@ -53,9 +59,9 @@ public:
void add(Item* item) { void add(Item* item) {
// Make sure node runs long enough to deliver the messages. // Make sure node runs long enough to deliver the messages.
uv_ref(uv_default_loop()); uv_ref(uv_default_loop());
pthread_mutex_lock(&mutex); NODE_SQLITE3_MUTEX_LOCK(&mutex);
data.push_back(item); data.push_back(item);
pthread_mutex_unlock(&mutex); NODE_SQLITE3_MUTEX_UNLOCK(&mutex)
} }
void send() { void send() {
...@@ -68,7 +74,7 @@ public: ...@@ -68,7 +74,7 @@ public:
} }
~Async() { ~Async() {
pthread_mutex_destroy(&mutex); NODE_SQLITE3_MUTEX_DESTROY
} }
}; };
......
...@@ -622,11 +622,10 @@ void Statement::Work_Each(uv_work_t* req) { ...@@ -622,11 +622,10 @@ void Statement::Work_Each(uv_work_t* req) {
sqlite3_mutex_leave(mtx); sqlite3_mutex_leave(mtx);
Row* row = new Row(); Row* row = new Row();
GetRow(row, stmt->handle); GetRow(row, stmt->handle);
NODE_SQLITE3_MUTEX_LOCK(&async->mutex)
pthread_mutex_lock(&async->mutex);
async->data.push_back(row); async->data.push_back(row);
retrieved++; retrieved++;
pthread_mutex_unlock(&async->mutex); NODE_SQLITE3_MUTEX_UNLOCK(&async->mutex)
uv_async_send(&async->watcher); uv_async_send(&async->watcher);
} }
...@@ -659,9 +658,9 @@ void Statement::AsyncEach(uv_async_t* handle, int status) { ...@@ -659,9 +658,9 @@ void Statement::AsyncEach(uv_async_t* handle, int status) {
while (true) { while (true) {
// Get the contents out of the data cache for us to process in the JS callback. // Get the contents out of the data cache for us to process in the JS callback.
Rows rows; Rows rows;
pthread_mutex_lock(&async->mutex); NODE_SQLITE3_MUTEX_LOCK(&async->mutex)
rows.swap(async->data); rows.swap(async->data);
pthread_mutex_unlock(&async->mutex); NODE_SQLITE3_MUTEX_UNLOCK(&async->mutex)
if (rows.empty()) { if (rows.empty()) {
break; break;
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include <node.h> #include <node.h>
#include "database.h" #include "database.h"
#include "threading.h"
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
...@@ -158,7 +159,7 @@ public: ...@@ -158,7 +159,7 @@ public:
uv_async_t watcher; uv_async_t watcher;
Statement* stmt; Statement* stmt;
Rows data; Rows data;
pthread_mutex_t mutex; NODE_SQLITE3_MUTEX_t;
bool completed; bool completed;
int retrieved; int retrieved;
...@@ -170,7 +171,7 @@ public: ...@@ -170,7 +171,7 @@ public:
Async(Statement* st, uv_async_cb async_cb) : Async(Statement* st, uv_async_cb async_cb) :
stmt(st), completed(false), retrieved(0) { stmt(st), completed(false), retrieved(0) {
watcher.data = this; watcher.data = this;
pthread_mutex_init(&mutex, NULL); NODE_SQLITE3_MUTEX_INIT
stmt->Ref(); stmt->Ref();
uv_async_init(uv_default_loop(), &watcher, async_cb); uv_async_init(uv_default_loop(), &watcher, async_cb);
} }
...@@ -179,7 +180,7 @@ public: ...@@ -179,7 +180,7 @@ public:
stmt->Unref(); stmt->Unref();
item_cb.Dispose(); item_cb.Dispose();
completed_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 ...@@ -21,7 +21,7 @@ BUNDLED_SQLITE3_TAR = 'sqlite-autoconf-%s.tar.gz' % BUNDLED_SQLITE3_VERSION
SQLITE3_TARGET = 'deps/%s' % BUNDLED_SQLITE3 SQLITE3_TARGET = 'deps/%s' % BUNDLED_SQLITE3
sqlite3_test_program = ''' sqlite3_test_program = '''
#include "stdio.h" #include <cstdio>"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
...@@ -36,6 +36,16 @@ main() { ...@@ -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): def set_options(opt):
opt.tool_options("compiler_cxx") opt.tool_options("compiler_cxx")
...@@ -46,6 +56,13 @@ def set_options(opt): ...@@ -46,6 +56,13 @@ def set_options(opt):
, dest='sqlite3_dir' , 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): def _conf_exit(conf,msg):
conf.fatal('\n\n' + msg + '\n...check the build/config.log for details') conf.fatal('\n\n' + msg + '\n...check the build/config.log for details')
...@@ -75,10 +92,28 @@ def configure(conf): ...@@ -75,10 +92,28 @@ def configure(conf):
msg='Checking for libsqlite3 at %s' % lib, msg='Checking for libsqlite3 at %s' % lib,
includes=include): includes=include):
Utils.pprint('GREEN', 'Sweet, found viable sqlite3 dependency at: %s ' % o.sqlite3_dir) Utils.pprint('GREEN', 'Sweet, found viable sqlite3 dependency at: %s ' % o.sqlite3_dir)
else: else:
_conf_exit(conf,'sqlite3 libs/headers not found at %s' % o.sqlite3_dir) _conf_exit(conf,'sqlite3 libs/headers not found at %s' % o.sqlite3_dir)
linkflags = [] 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'): if os.environ.has_key('LINKFLAGS'):
linkflags.extend(os.environ['LINKFLAGS'].split(' ')) linkflags.extend(os.environ['LINKFLAGS'].split(' '))
...@@ -128,7 +163,7 @@ def build(bld): ...@@ -128,7 +163,7 @@ def build(bld):
obj = bld.new_task_gen("cxx", "shlib", "node_addon") obj = bld.new_task_gen("cxx", "shlib", "node_addon")
build_internal_sqlite3(bld) build_internal_sqlite3(bld)
obj.cxxflags = ["-g", "-D_FILE_OFFSET_BITS=64", "-D_LARGEFILE_SOURCE", 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' # uncomment the next line to remove '-undefined dynamic_lookup'
# in order to review linker errors (v8, libev/eio references can be ignored) # in order to review linker errors (v8, libev/eio references can be ignored)
#obj.env['LINKFLAGS_MACBUNDLE'] = ['-bundle'] #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