Skip to content
This repository has been archived by the owner on Jun 15, 2018. It is now read-only.

Fix build for Node.js 0.6 and up #17

Merged
merged 2 commits into from Aug 24, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -5,5 +5,5 @@
"keywords": [], "keywords": [],
"author": "Jeroen Janssen <jeroen.janssen@gmail.com>", "author": "Jeroen Janssen <jeroen.janssen@gmail.com>",
"main": "./sdlmixer", "main": "./sdlmixer",
"engines": { "node": "0.4.x" } "engine": "node >= 0.4.x"
} }
31 changes: 20 additions & 11 deletions src/async_uv.h
@@ -1,75 +1,84 @@
#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 <node_version.h>


// Generic uv_async handler. // Generic uv_async handler.
template <class Item, class Parent> class Async { template <class Item, class Parent> class Async {
typedef void (*Callback)(Parent* parent, Item* item); typedef void (*Callback)(Parent* parent, Item* item);

protected: protected:
uv_async_t watcher; uv_async_t watcher;
pthread_mutex_t mutex; pthread_mutex_t mutex;
std::vector<Item*> data; std::vector<Item*> data;
Callback callback; Callback callback;
public: public:
Parent* parent; Parent* parent;

public: 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); pthread_mutex_init(&mutex, NULL);
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); pthread_mutex_lock(&async->mutex);
rows.swap(async->data); rows.swap(async->data);
pthread_mutex_unlock(&async->mutex); pthread_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++) {
#if NODE_VERSION_AT_LEAST(0, 7, 9)
uv_unref((uv_handle_t *)&async->watcher);
#else
uv_unref(uv_default_loop()); uv_unref(uv_default_loop());
#endif
async->callback(async->parent, rows[i]); async->callback(async->parent, rows[i]);
} }
} }

static void close(uv_handle_t* handle) { static void close(uv_handle_t* handle) {
assert(handle != NULL); assert(handle != NULL);
assert(handle->data != NULL); assert(handle->data != NULL);
Async* async = static_cast<Async*>(handle->data); Async* async = static_cast<Async*>(handle->data);
delete async; delete async;
handle->data = NULL; handle->data = NULL;
} }

void finish() { void finish() {
// Need to call the listener again to ensure all items have been // Need to call the listener again to ensure all items have been
// processed. Is this a bug in uv_async? Feels like uv_close // processed. Is this a bug in uv_async? Feels like uv_close
// should handle that. // should handle that.
listener(&watcher, 0); listener(&watcher, 0);
uv_close((uv_handle_t*)&watcher, close); uv_close((uv_handle_t*)&watcher, close);
} }

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.
#if NODE_VERSION_AT_LEAST(0, 7, 9)
uv_ref((uv_handle_t *)&watcher);
#else
uv_ref(uv_default_loop()); uv_ref(uv_default_loop());
#endif
pthread_mutex_lock(&mutex); pthread_mutex_lock(&mutex);
data.push_back(item); data.push_back(item);
pthread_mutex_unlock(&mutex); pthread_mutex_unlock(&mutex);
} }

void send() { void send() {
uv_async_send(&watcher); uv_async_send(&watcher);
} }

void send(Item* item) { void send(Item* item) {
add(item); add(item);
send(); send();
} }

~Async() { ~Async() {
pthread_mutex_destroy(&mutex); pthread_mutex_destroy(&mutex);
} }
}; };


#endif #endif