Repository navigation
Expand file tree
/
Copy pathasync_baton.cc
More file actions
89 lines (75 loc) · 2.58 KB
/
Copy pathasync_baton.cc
File metadata and controls
89 lines (75 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "../include/async_baton.h"
namespace nodegit {
void deleteBaton(AsyncBaton *baton) {
delete baton;
}
AsyncBaton::AsyncBaton()
: asyncResource(ThreadPool::GetCurrentAsyncResource()),
callbackErrorHandle(*ThreadPool::GetCurrentCallbackErrorHandle()),
completedMutex(new std::mutex),
hasCompleted(false)
{}
void AsyncBaton::SignalCompletion() {
std::lock_guard<std::mutex> lock(*completedMutex);
hasCompleted = true;
completedCondition.notify_one();
}
void AsyncBaton::Done() {
onCompletion();
}
Nan::AsyncResource *AsyncBaton::GetAsyncResource() {
return asyncResource;
}
void AsyncBaton::SetCallbackError(v8::Local<v8::Value> error) {
callbackErrorHandle.Reset(error);
}
void AsyncBaton::ExecuteAsyncPerform(AsyncCallback asyncCallback, AsyncCallback asyncCancelCb, CompletionCallback onCompletion) {
auto jsCallback = [asyncCallback, this]() {
asyncCallback(this);
};
auto cancelCallback = [asyncCancelCb, this]() {
asyncCancelCb(this);
};
if (onCompletion) {
this->onCompletion = [this, onCompletion]() {
onCompletion(this);
};
ThreadPool::PostCallbackEvent(
[jsCallback, cancelCallback](
ThreadPool::QueueCallbackFn queueCallback,
ThreadPool::Callback callbackCompleted,
bool isThreaded // Temporary workaround for LFS checkout. Code added to be reverted.
) -> ThreadPool::Callback {
queueCallback(jsCallback, cancelCallback);
callbackCompleted();
return []() {};
}
);
} else {
ThreadPool::PostCallbackEvent(
[this, jsCallback, cancelCallback](
ThreadPool::QueueCallbackFn queueCallback,
ThreadPool::Callback callbackCompleted,
bool isThreaded // Temporary workaround for LFS checkout. Code added to be reverted.
) -> ThreadPool::Callback {
// Temporary workaround for LFS checkout. Code modified to be reverted.
if (!isThreaded) {
this->onCompletion = callbackCompleted;
queueCallback(jsCallback, cancelCallback);
return std::bind(&AsyncBaton::SignalCompletion, this);
}
else {
this->onCompletion = std::bind(&AsyncBaton::SignalCompletion, this);
queueCallback(jsCallback, cancelCallback);
return []() {};
}
}
);
WaitForCompletion();
}
}
void AsyncBaton::WaitForCompletion() {
std::unique_lock<std::mutex> lock(*completedMutex);
while (!hasCompleted) completedCondition.wait(lock);
}
}