Commit ef58b6f1 by Marcin Cieslak

Use libuv locking primitives instead of C++11

Allow to link the binding against
oldish libstdc++.6.0.9 installed on MacOSX
(from gcc 4.2.1)
parent c6c67acc
...@@ -3,8 +3,8 @@ ...@@ -3,8 +3,8 @@
#include <vector> #include <vector>
#include <nan.h> #include <nan.h>
#include <condition_variable>
#include <algorithm> #include <algorithm>
#include <uv.h>
#define COMMA , #define COMMA ,
...@@ -42,8 +42,8 @@ class CallbackBridge { ...@@ -42,8 +42,8 @@ class CallbackBridge {
Nan::Callback* callback; Nan::Callback* callback;
bool is_sync; bool is_sync;
std::mutex cv_mutex; uv_mutex_t cv_mutex;
std::condition_variable condition_variable; uv_cond_t condition_variable;
uv_async_t *async; uv_async_t *async;
std::vector<L> argv; std::vector<L> argv;
bool has_returned; bool has_returned;
...@@ -60,6 +60,8 @@ CallbackBridge<T, L>::CallbackBridge(Nan::Callback* callback, bool is_sync) : ca ...@@ -60,6 +60,8 @@ CallbackBridge<T, L>::CallbackBridge(Nan::Callback* callback, bool is_sync) : ca
* V8 context is available. * V8 context is available.
*/ */
Nan::HandleScope scope; Nan::HandleScope scope;
uv_mutex_init(&this->cv_mutex);
uv_cond_init(&this->condition_variable);
if (!is_sync) { if (!is_sync) {
this->async = new uv_async_t; this->async = new uv_async_t;
this->async->data = (void*) this; this->async->data = (void*) this;
...@@ -75,6 +77,8 @@ template <typename T, typename L> ...@@ -75,6 +77,8 @@ template <typename T, typename L>
CallbackBridge<T, L>::~CallbackBridge() { CallbackBridge<T, L>::~CallbackBridge() {
delete this->callback; delete this->callback;
this->wrapper.Reset(); this->wrapper.Reset();
uv_cond_destroy(&this->condition_variable);
uv_mutex_destroy(&this->cv_mutex);
if (!is_sync) { if (!is_sync) {
uv_close((uv_handle_t*)this->async, &async_gone); uv_close((uv_handle_t*)this->async, &async_gone);
...@@ -117,11 +121,13 @@ T CallbackBridge<T, L>::operator()(std::vector<void*> argv) { ...@@ -117,11 +121,13 @@ T CallbackBridge<T, L>::operator()(std::vector<void*> argv) {
*/ */
this->argv = argv; this->argv = argv;
std::unique_lock<std::mutex> lock(this->cv_mutex); uv_mutex_lock(&this->cv_mutex);
this->has_returned = false; this->has_returned = false;
uv_async_send(this->async); uv_async_send(this->async);
this->condition_variable.wait(lock, [this] { return this->has_returned; }); while (!this->has_returned) {
uv_cond_wait(&this->condition_variable, &this->cv_mutex);
}
uv_mutex_unlock(&this->cv_mutex);
return this->return_value; return this->return_value;
} }
} }
...@@ -168,11 +174,12 @@ NAN_METHOD(CallbackBridge<T COMMA L>::ReturnCallback) { ...@@ -168,11 +174,12 @@ NAN_METHOD(CallbackBridge<T COMMA L>::ReturnCallback) {
bridge->return_value = bridge->post_process_return_value(info[0]); bridge->return_value = bridge->post_process_return_value(info[0]);
{ {
std::lock_guard<std::mutex> lock(bridge->cv_mutex); uv_mutex_lock(&bridge->cv_mutex);
bridge->has_returned = true; bridge->has_returned = true;
uv_mutex_unlock(&bridge->cv_mutex);
} }
bridge->condition_variable.notify_all(); uv_cond_broadcast(&bridge->condition_variable);
if (try_catch.HasCaught()) { if (try_catch.HasCaught()) {
Nan::FatalException(try_catch); Nan::FatalException(try_catch);
......
#include <nan.h> #include <nan.h>
#include <stdexcept>
#include "custom_function_bridge.h" #include "custom_function_bridge.h"
#include "sass_types/factory.h" #include "sass_types/factory.h"
......
#include <nan.h> #include <nan.h>
#include <stdexcept>
#include "custom_importer_bridge.h" #include "custom_importer_bridge.h"
#include "create_string.h" #include "create_string.h"
......
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
#include <memory> #include <memory>
#include <nan.h> #include <nan.h>
#include <stdlib.h> #include <stdlib.h>
#include <condition_variable>
#include <sass_context.h> #include <sass_context.h>
#include "custom_function_bridge.h" #include "custom_function_bridge.h"
#include "custom_importer_bridge.h" #include "custom_importer_bridge.h"
......
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