Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.

Importer: Drops uv_mutex in favor of std::mutex #615

Merged
merged 2 commits into from
Jan 10, 2015
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ module.exports.render = function(options) {
if (importer) {
options.importer = function(file, prev, key) {
function done(data) {
console.log(data); // ugly hack
binding.importedCallback({
index: key,
objectLiteral: data
Expand Down
6 changes: 4 additions & 2 deletions src/binding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,14 @@ struct Sass_Import** sass_importer(const char* file, const char* prev, void* coo
* can run uv_async_send without a push.
*/

std::unique_lock<std::mutex> lock(*ctx_w->importer_mutex);

ctx_w->file = file ? strdup(file) : 0;
ctx_w->prev = prev ? strdup(prev) : 0;
ctx_w->async.data = (void*)ctx_w;

uv_async_send(&ctx_w->async);
uv_cond_wait(&ctx_w->importer_condition_variable, &ctx_w->importer_mutex);
ctx_w->importer_condition_variable->wait(lock);
}
else {
NanScope();
Expand Down Expand Up @@ -357,7 +359,7 @@ NAN_METHOD(ImportedCallback) {
sass_context_wrapper* ctx_w = imports_collection[index];

prepare_import_results(returned_value, ctx_w);
uv_cond_signal(&ctx_w->importer_condition_variable);
ctx_w->importer_condition_variable->notify_all();

if (try_catch.HasCaught()) {
node::FatalException(try_catch);
Expand Down
15 changes: 8 additions & 7 deletions src/sass_context_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ extern "C" {

sass_context_wrapper* sass_make_context_wrapper() {
sass_context_wrapper* ctx_w = (sass_context_wrapper*)calloc(1, sizeof(sass_context_wrapper));
uv_mutex_init(&ctx_w->importer_mutex);
uv_cond_init(&ctx_w->importer_condition_variable);

ctx_w->importer_mutex = new std::mutex();
ctx_w->importer_condition_variable = new std::condition_variable();

return ctx_w;
}
Expand All @@ -38,14 +39,14 @@ extern "C" {
sass_delete_file_context(ctx_w->fctx);
}

delete ctx_w->success_callback;
delete ctx_w->error_callback;
delete ctx_w->importer_callback;
delete ctx_w->file;
delete ctx_w->prev;
delete ctx_w->error_callback;
delete ctx_w->success_callback;
delete ctx_w->importer_callback;

uv_mutex_destroy(&ctx_w->importer_mutex);
uv_cond_destroy(&ctx_w->importer_condition_variable);
delete ctx_w->importer_mutex;
delete ctx_w->importer_condition_variable;

NanDisposePersistent(ctx_w->result);

Expand Down
28 changes: 18 additions & 10 deletions src/sass_context_wrapper.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <nan.h>
#include <condition_variable>
#include "libsass/sass_context.h"

#ifdef __cplusplus
Expand All @@ -12,20 +13,27 @@ extern "C" {
void compile_it(uv_work_t* req);

struct sass_context_wrapper {
// binding related
bool is_sync;
void* cookie;
const char* prev;
const char* file;
std::mutex* importer_mutex;
std::condition_variable* importer_condition_variable;

// libsass related
Sass_Import** imports;
Sass_Data_Context* dctx;
Sass_File_Context* fctx;
Persistent<Object> result;
uv_work_t request;
uv_mutex_t importer_mutex;
uv_cond_t importer_condition_variable;

// libuv related
uv_async_t async;
const char* file;
const char* prev;
void* cookie;
bool is_sync;
Sass_Import** imports;
NanCallback* success_callback;
uv_work_t request;

// v8 and nan related
Persistent<Object> result;
NanCallback* error_callback;
NanCallback* success_callback;
NanCallback* importer_callback;
};

Expand Down