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

Commit 97e1563

Browse files
committed
Merge pull request #615 from am11/std-mutex
Importer: Drops uv_mutex in favor of std::mutex
2 parents f5f48bd + ff53926 commit 97e1563

File tree

4 files changed

+31
-19
lines changed

4 files changed

+31
-19
lines changed

lib/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ module.exports.render = function(options) {
206206
if (importer) {
207207
options.importer = function(file, prev, key) {
208208
function done(data) {
209+
console.log(data); // ugly hack
209210
binding.importedCallback({
210211
index: key,
211212
objectLiteral: data

src/binding.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,14 @@ struct Sass_Import** sass_importer(const char* file, const char* prev, void* coo
8181
* can run uv_async_send without a push.
8282
*/
8383

84+
std::unique_lock<std::mutex> lock(*ctx_w->importer_mutex);
85+
8486
ctx_w->file = file ? strdup(file) : 0;
8587
ctx_w->prev = prev ? strdup(prev) : 0;
8688
ctx_w->async.data = (void*)ctx_w;
8789

8890
uv_async_send(&ctx_w->async);
89-
uv_cond_wait(&ctx_w->importer_condition_variable, &ctx_w->importer_mutex);
91+
ctx_w->importer_condition_variable->wait(lock);
9092
}
9193
else {
9294
NanScope();
@@ -357,7 +359,7 @@ NAN_METHOD(ImportedCallback) {
357359
sass_context_wrapper* ctx_w = imports_collection[index];
358360

359361
prepare_import_results(returned_value, ctx_w);
360-
uv_cond_signal(&ctx_w->importer_condition_variable);
362+
ctx_w->importer_condition_variable->notify_all();
361363

362364
if (try_catch.HasCaught()) {
363365
node::FatalException(try_catch);

src/sass_context_wrapper.cpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ extern "C" {
2424

2525
sass_context_wrapper* sass_make_context_wrapper() {
2626
sass_context_wrapper* ctx_w = (sass_context_wrapper*)calloc(1, sizeof(sass_context_wrapper));
27-
uv_mutex_init(&ctx_w->importer_mutex);
28-
uv_cond_init(&ctx_w->importer_condition_variable);
27+
28+
ctx_w->importer_mutex = new std::mutex();
29+
ctx_w->importer_condition_variable = new std::condition_variable();
2930

3031
return ctx_w;
3132
}
@@ -38,14 +39,14 @@ extern "C" {
3839
sass_delete_file_context(ctx_w->fctx);
3940
}
4041

41-
delete ctx_w->success_callback;
42-
delete ctx_w->error_callback;
43-
delete ctx_w->importer_callback;
4442
delete ctx_w->file;
4543
delete ctx_w->prev;
44+
delete ctx_w->error_callback;
45+
delete ctx_w->success_callback;
46+
delete ctx_w->importer_callback;
4647

47-
uv_mutex_destroy(&ctx_w->importer_mutex);
48-
uv_cond_destroy(&ctx_w->importer_condition_variable);
48+
delete ctx_w->importer_mutex;
49+
delete ctx_w->importer_condition_variable;
4950

5051
NanDisposePersistent(ctx_w->result);
5152

src/sass_context_wrapper.h

+18-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <nan.h>
2+
#include <condition_variable>
23
#include "libsass/sass_context.h"
34

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

1415
struct sass_context_wrapper {
16+
// binding related
17+
bool is_sync;
18+
void* cookie;
19+
const char* prev;
20+
const char* file;
21+
std::mutex* importer_mutex;
22+
std::condition_variable* importer_condition_variable;
23+
24+
// libsass related
25+
Sass_Import** imports;
1526
Sass_Data_Context* dctx;
1627
Sass_File_Context* fctx;
17-
Persistent<Object> result;
18-
uv_work_t request;
19-
uv_mutex_t importer_mutex;
20-
uv_cond_t importer_condition_variable;
28+
29+
// libuv related
2130
uv_async_t async;
22-
const char* file;
23-
const char* prev;
24-
void* cookie;
25-
bool is_sync;
26-
Sass_Import** imports;
27-
NanCallback* success_callback;
31+
uv_work_t request;
32+
33+
// v8 and nan related
34+
Persistent<Object> result;
2835
NanCallback* error_callback;
36+
NanCallback* success_callback;
2937
NanCallback* importer_callback;
3038
};
3139

0 commit comments

Comments
 (0)