On Sat, May 12, 2012 at 6:38 PM, Ben Noordhuis <[email protected]> wrote:
> On Sat, May 12, 2012 at 12:21 PM, Nikhil Marathe <[email protected]> wrote:
>> I am working on a binding to a C++ library. A certain piece of code
>> can be called either from the main node/v8 thread,
>> or in a worker thread (from uv_queue_work), depending on which JS
>> method is invoked by the user of the binding.
>> How do I figure out in the C++ code whether the code is running in the
>> main node/v8 thread or in the worker thread,
>> because this will decide whether I need to use a mutex or not
>
> What is the mutex protecting? If it's a global or shared data
> structure, you need to lock the mutex in the main thread as well.
>

The situation goes something like this (the binding is for TagLib,
which extracts tags from music files):

1. The user provides a function first to the binding which is used to
resolve the filetype.
This function is supposed to return a string idenitifying the mime
type. This function is registered
with TagLib, and from then on I have no control over 'when' it is invoked.

Now it is possible for this function (we'll call it the resolver) to
be called in two different ways.
1. From an async worker thread if the binding function that reads a
tag asynchronously is used.
Now since any JS code needs to run in the main thread, here is what I do:
- In the worker thread, I lock a mutex, then use uv_async_send to
notify the main event loop. Then the worker
thread tries to lock the mutex again and is blocked. The
async watcher triggers the JS function from (1) in the main thread,
then unlocks the mutex so that the worker
thread can continue. This works perfectly.

2. The sync version of reading a tag will already be running in the
main thread. But if I do the mutex lock/unlock here
the process will stall because the unlock will never be triggered
because the thread is blocked. But it doesn't seem
possible to identify whether this mutex code is being run in a worker
thread or the main thread, so that I can choose
alternative code paths.

So the problem I'm trying to solve is: identify what thread the code
is running in, in a platform independent manner.
I hope that was clear enough.

Thanks,
Nikhil

-- 
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

Reply via email to