Hello,
I have a C library and I am writing a non-blocking wrapper using Node JS. I
am using uv_work_queue to call C library function. Workflow will be like,
1- First function gets called on main thread. This one sets up a WorkerData
(a struct used for passing data to thread) instance with all necessary
information required to do the job. This object is wrapped inside uv_work_t
instance's "data" field.
2 - Calls uv_queue_work (uv_default_loop(), ... )
3 - Each worker thread will acquire a mutex lock, get a work handle (C
library requires this handle for every call) from the queue (std::queue)
and removes the handle from queue. It releases the mutex after all these.
If no handles are available in the queue, a new one will be created.
4 - C library will be called with the above handle. Once the work is done,
acquire mutex lock and the handle will be added back to the queue.
I wrote some JS code to test the above functionality. It will be something
like,
for(i = 0; i < 100; i ++)
{
mylib.process ("test", function (err, result) {
});
}
actual code is available here -
https://github.com/navaneeth/libvarnam-nodejs/blob/master/test.js
Interestingly, this fails with random segmentation faults. I have seen some
segfaults at accessing the queue even though all the operations that modify
the queue are in critical section. I have also got assertion failures
inside libuv. node: ../deps/uv/src/unix/stream.c:489: uv__write: Assertion
`n == 0' failed.
Some observations:
* When I reduce the loop iterations to 20, all works good.
* When I add a delay in between calls, it works.
I am wondering why this happens? It looks like when in a tight loop,
mutexes are not working correctly and allowing multiple threads to get into
the critical section. When these segfaults happening, I have seen queue's
size() returns some random values which is a clear indication of mutexes
failing.
Code is avilable here -
https://github.com/navaneeth/libvarnam-nodejs/blob/master/src/varnamjs.cc.
Varnam::Transliterate() is the entry point.
Any help would be great!
--
Thanks
Navaneeth
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en