Is there a way to identify Persistent<Function> uniqueness? - c++

I am storing js callbacks in vector:
std::vector<std::unique_ptr<Persistent<Function>>> callbacks;
Everything is working fine. The problem is that I do not want to store duplicated callbacks because I do not want to notify the same callback twice. I have to compare them somehow. Here is my full function but that does not work:
void ProcessCallback(const FunctionCallbackInfo<Value>& args)
{
std::string returnInfo;
Isolate* isolate = args.GetIsolate();
Local<Function> notifyFunction = Local<Function>::Cast(args[0]);
auto predicate = [&](const std::unique_ptr<Persistent<Function>>& c)
{
return c->Get(isolate)->StrictEquals(notifyFunction);
};
auto it = std::find_if(callbacks.begin(), callbacks.end(), predicate);
if (it == callbacks.end())
{
returnInfo = "Did not find callback. Adding..." + std::to_string(callbacks.size());
auto persistentCallback = std::make_unique<Persistent<Function>>(isolate, notifyFunction);
callbacks.emplace_back(std::move(persistentCallback));
}
else
{
returnInfo = "Callback already exist in a list.\n";
}
args.GetReturnValue().Set(String::NewFromUtf8(isolate, returnInfo.c_str()).ToLocalChecked());
}
In js:
function callback(msg) {
console.log(msg);
}
let addon = require('./build/Release/addon');
console.log(addon.on(callback));
console.log(addon.on(callback));
Is there something that I can rely on to uniquely identify function that is passed from js? Thanks.

Related

Why the below program aborts when a std::vector<std::future<T>> is used.?

I wanted to perform hashing of a stream of input messages in multithreading, so was trying to implement
std::vector<std::future<HashData>> futures;
but the program aborts from abort.h when debugging in Visual Studio 2019.
Code Snippet:
std::vector<std::future<HashData>> futures;
std::vector<std::string> messages;
for (int i = 0; i < messages.size(); i++)
{
std::promise<HashData> promiseHashData;
std::future<HashData> futureHashData = promiseHashData.get_future();
futures.emplace_back(std::move(futureHashData));
std::async(std::launch::async, [&]() {PerformHash(std::move(promiseHashData), messages[i]);});
}
std::vector<HashData> vectorOfHashData;
// wait for all async tasks to complete
for (auto& futureObj : futures)
{
vectorOfHashData.push_back(futureObj.get());
}
void PerformHash(std::promise<HashData>&& promObject, std::string& message)
{
ComputeHashUsingSHA256(message);
HashData data;
// set data for HashData object
data.i = i;
data.blocks = blocks;
data.blocksize = blocksize;
data.blockbufs = blockbufs;
data.secs = secs;
memcpy(data.digest, digest, SHA256_DIGEST_SIZE);
data.has_hashdata = has_hashdata;
memcpy(data.hashdata_buf, hashdata_buf, c_hashsize);
promObject.set_value(data);
}
while debugging the code, observed as only few threads were created using async and post that, the program aborts from abort.h as shown in this
image
The problem is that you capture promiseHashData by reference. At each loop iteration it gets invalidated while the async thread performs computation on it.
You need to capture the instance of the promise by moving it into the lambda, like:
std::async(std::launch::async, [promiseHashData2=std::move(promiseHashData)] ()mutable{PerformHash(std::move(promiseHashData2), messages[i]);});
Or use std::async's feature of returning std::future while changing performHash to return hashData. Using both async and promise is redundant.
To build on the good answer from #ALX23z and answer your comments there:
The reason you get that error is that PerformHash (and your lambda) returns void. The return value from std::async is std::future<X>, where X is the return value of the function you give std::async. Here is a small toy example:
struct HashData {std::size_t h;};
HashData performHash(const std::string &msg) // <- returns HashData
{
HashData hd = {msg.size()};
return hd;
}
int main()
{
std::vector<std::string> messages = {"Bla", "klaf", "this is a message"};
std::vector<std::future<HashData>> futures;
for (const auto &msg : messages)
{
auto fut = std::async(std::launch::async, [&]()
{ return performHash(msg); }); // <- also returns HashData
futures.emplace_back(std::move(fut));
}
std::vector<HashData> results;
for (auto &fut : futures)
results.push_back(fut.get());
for (const auto &hash : results)
std::cout << hash.h << '\n';
}
Also note that you can skip the lambda, and call std::async like this:
auto fut = std::async(std::launch::async, performHash, msg); // performHash is a free function
// If performHash is a method of class HashCalculator - included for completeness sake
HashCalculator calc; // Need an instance somewhere
auto fut = std::async(std::launch::async, &HashCalculator::performHash, calc, msg);

Why XRecordDisableContext() is not working?

void Callback (XPointer, XRecordInterceptData *pRecord) { std::cout << "my logs\n"; }
int main ()
{
if(auto* const pDisplay = XOpenDisplay(nullptr))
{
XRecordClientSpec clients = XRecordAllClients;
auto* pRange = ::XRecordAllocRange();
pRange->device_events = XRecordRange8{KeyPress, ButtonRelease};
auto context = ::XRecordCreateContext(pDisplay, 0, &clients, 1, &pRange, 1);
::XRecordEnableContextAsync(pDisplay, context, Callback, nullptr); // use with/without `...Async()`
::XRecordDisableContext(pDisplay, context);
::XRecordFreeContext(pDisplay, context);
::XFree(pRange);
::XFlush(pDisplay);
::XSync(pDisplay, true);
}
}
I am noticing that even after XRecordDisableContext(), the Callback() continues to be invoked.
How can we disable the recording, so that the callback isn't invoked anymore?
Note:
Have taken example from this site.
Don't know how to use XRecordEnableContext(), so using XRecordEnableContextAsync(). Is that the source of problem?
One way is to move below statement into the Callback() or some equivalent other thread. For testing purpose, I changed the code as below where after few event raised, I disable from the Callback() and it works.
::Display* pDisplay;
XRecordRange* pRange;
XRecordContext context;
#define CHECK(EVENT) if(*pDatum == EVENT) qDebug() << #EVENT
void Handle (XPointer, XRecordInterceptData *pRecord)
{
std::cout << "my logs\n";
static int i = 0;
if(++i < 10)
return;
::XRecordDisableContext(pDisplay, context);
::XRecordFreeContext(pDisplay, context);
::XFree(pRange);
::XFlush(pDisplay);
::XSync(pDisplay, true);
}
// other code same, except 3 variables are global and "Free"-up functions are not required

Moving std::string into capture

Consider the following example that takes a msg and posts it asynchronously.
void sendMessage(std::string msg) {
runAsync<void>([msg = std::move(msg)] {
onPostMessage(cookie, msg.c_str());
});
};
Would the following be faster?
void sendMessage(std::string&& msg) {
What would be the best options at the calling site?
auto msg = ...
postMessage(msg);
or
auto msg = ...
postMessage(std::move(msg));
Note that msg isn't needed after the call anymore.
Will the compiler automatically detect that I am not using msg anymore after the call and "move" it?
I would use the following solution:
void sendMessage(std::string&& msg) {
runAsync<void>([msg = std::move(msg)] {
onPostMessage(cookie, msg.c_str());
});
};
and at the calling site:
auto msg = ...
sendMessage(std::exchange(msg, {}));

How to handle a map argument in a nodejs C++ module

I have a C++ module for nodejs. I need to accept a key/value pair as argument for a method.
var my_map = {'key1': 'value1','key2': 'value2'};
Not sure what to do after this:
void MyClient::AcceptData(const FunctionCallbackInfo<Value>& args)
{
Isolate* isolate = args.GetIsolate();
if (args.Length() != 1)
{
isolate->ThrowException(v8::Exception::TypeError(String::NewFromUtf8(isolate,
"Usage: Key/Value Map")));
return;
}
// It returns true for args[0]->isObject(), but not for args[0]->IsMap()
// Now what? How do I get a C++ map out of args[0] ?
// What do I cast it into?
}
If you are sure it is a Map object, you can retrieve it through this code:
Handle<Map> map = Handle<Map>::cast(args[0]);
And then use the properties and attributes of the map.
Hope this helps.
Ok, I found the answer...
v8::Local<v8::Object> obj = args[0]->ToObject();
v8::Local<v8::Array> props = obj->GetPropertyNames();
std::map<std::string, std::string> theMap;
for (unsigned int i = 0; i < props->Length(); i++)
{
char key[1000], value[1000];
props->Get(i)->ToString()->WriteUtf8(key, 1000);
obj->Get(props->Get(i))->ToString()->WriteUtf8(value, 1000);
theMap.insert(std::make_pair(key, value));
}

winrt c++/cx concurrency access violation exception

What I'm trying to do is check for the existence of a file in the local folder and then copy it there if it isn't found (the file was previously added to the project as an asset).
Here is the code:
Windows::Storage::StorageFile^ MainPage::GetCustomFileAsync(Platform::String^ fileName)
{
using Windows::Storage::StorageFile;
using Windows::Storage::StorageFolder;
auto localFolder = Windows::Storage::ApplicationData::Current->LocalFolder;
auto localTask = concurrency::create_task(localFolder->GetFileAsync(fileName));
StorageFile^ retVal = nullptr;
localTask.then([&](StorageFile^ t){
retVal = t;
}).then([](concurrency::task<void> t)
{
try
{
t.get();
OutputDebugString(L"Found\n");
}
catch (Platform::COMException^ e)
{
OutputDebugString(e->Message->Data());
}
}).wait();
return retVal;
}
StorageFile^ fileVar;
if ((fileVar = this->GetCustomFileAsync("somefile.txt")) == nullptr)
{
String^ path = Windows::ApplicationModel::Package::Current->InstalledLocation->Path + "\\Assets";
concurrency::create_task(Windows::Storage::StorageFolder::GetFolderFromPathAsync(path)).then([](StorageFolder^ folder){
return (folder->GetFileAsync("somefile.txt"));
}).then([](StorageFile^ file){
return (file->CopyAsync(Windows::Storage::ApplicationData::Current->LocalFolder));
}).then([&](StorageFile^ file){
fileVar = file;
OutputDebugString(file->DisplayName->Data());
});
}
What happens is that I get an access violation exception at the point where "file" is being assigned to "fileVar" (because of cross-thread access perhaps?). How to fix this?
Edit: I can't do all the processing there because the file will be accessed many times. In short I need to know when it has been successfully copied and get a handle to it. Here is the code that works
Windows::Storage::StorageFile^ GetFile(Platform::String^ fileName)
{
using Windows::Storage::StorageFile;
using Windows::Storage::StorageFolder;
using Windows::Foundation::AsyncOperationCompletedHandler;
using Windows::Foundation::AsyncStatus;
using Windows::Foundation::IAsyncOperation;
using Platform::String;
auto localFolder = Windows::Storage::ApplicationData::Current->LocalFolder;
bool completed = false;
StorageFile^ retVal = nullptr;
localFolder->GetFileAsync(fileName)->Completed = ref new AsyncOperationCompletedHandler<StorageFile^>([&completed, &retVal, &fileName](IAsyncOperation<StorageFile^>^ fileOperation, AsyncStatus status)
{
if (status == AsyncStatus::Error)
{
String^ path = Windows::ApplicationModel::Package::Current->InstalledLocation->Path + "\\Assets";
Windows::Storage::StorageFolder::GetFolderFromPathAsync(path)->Completed = ref new AsyncOperationCompletedHandler<Windows::Storage::StorageFolder^>(
[&completed, &retVal, &fileName](IAsyncOperation<Windows::Storage::StorageFolder^>^ folderOperation, AsyncStatus status)->void{
auto assetFolder = folderOperation->GetResults();
assetFolder->GetFileAsync(fileName)->Completed = ref new AsyncOperationCompletedHandler<Windows::Storage::StorageFile^>([&completed, &retVal, &fileName](IAsyncOperation<Windows::Storage::StorageFile^>^ fileOperation, AsyncStatus status)->void{
auto file = fileOperation->GetResults();
file->CopyAsync(Windows::Storage::ApplicationData::Current->LocalFolder)->Completed = ref new AsyncOperationCompletedHandler<Windows::Storage::StorageFile^>
([&completed, &retVal, &fileName](IAsyncOperation<Windows::Storage::StorageFile^>^ fileOperation, AsyncStatus status)->void {
retVal = fileOperation->GetResults();
completed = true;
});
});
});
}
else
{
retVal = fileOperation->GetResults();
completed = true;
}
});
while (completed == false);
return retVal;
}
Rather than passing a delegate as an argument and returning void, make your method return task<StorageFile^> and then the caller can do a .then() to continue working once the operation has succeeded.
Or if this is exposed as a public WinRT method (not an internal / private C++ method) then use IAsyncOperation<StorageFile^>^ as the return type, and wrap the whole thing in create_async():
IAsyncOperation<StorageFile^>^ DoStuff(params)
{
return concurrency::create_async([params]
{
// function body goes here
});
}
Here's a solution I put together. Two things that are important to know:
When executing an asynchronous operation using concurrency::create_task the async operation(s) can still be executing when the parent function returns. So the captured variables MUST outlive the context of the parent function. Which obviously won't happen if they are being passed by reference. It took a while to realize this.
WinRT imposes certain restrictions on the concurrency runtime. Calling concurrency::task::get() or concurrency::task::wait() will throw an exception in an STA thread, unless the call is in a task continuation.
More information in this post:
http://social.msdn.microsoft.com/Forums/windowsapps/en-US/ae54980b-41ce-4337-a059-2213b549be4b/concurrencyinvalidoperation-when-calling-tasktget?forum=winappswithnativecode
In that case how to know when the function has finished doing it's job? I opted to pass in a callback (AKA delegate).
delegate void FileOperation(Windows::Storage::StorageFile^ file);
void GetFileConcurrency(Platform::String^ fileName, FileOperation^ fileOp)
{
using Windows::Storage::StorageFile;
using Windows::Storage::StorageFolder;
using Platform::String;
auto localFolder = Windows::Storage::ApplicationData::Current->LocalFolder;
String^ assetFolderPath = Windows::ApplicationModel::Package::Current->InstalledLocation->Path + "\\Assets";
auto localFolderTask = concurrency::create_task(localFolder->GetFileAsync(fileName));
localFolderTask.then([localFolder, assetFolderPath, fileName, fileOp](concurrency::task<StorageFile^> theTask){
try
{
StorageFile^ theFile = theTask.get();
fileOp(theFile);
}
catch (Platform::Exception^ e)
{
OutputDebugString(e->Message->Data());
auto assetFolderTask = concurrency::create_task(StorageFolder::GetFolderFromPathAsync(assetFolderPath));
assetFolderTask.then([localFolder, assetFolderPath, fileName, fileOp](StorageFolder^ assetFolder){
auto assetFileTask = concurrency::create_task(assetFolder->GetFileAsync(fileName));
assetFileTask.then([localFolder, assetFolderPath, fileName, fileOp](StorageFile^ file){
auto copyFileTask = concurrency::create_task(file->CopyAsync(localFolder));
copyFileTask.then([localFolder, assetFolderPath, fileName, fileOp](StorageFile^ file){
OutputDebugString(file->Path->Data());
fileOp(file);
});
});
});
}
});
}