• Post Reply Bookmark Topic Watch Topic
  • New Topic

In JNI, how know that object already locked?  RSS feed

 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an object on which I want to call env->MonitorEnter, but only if we don't already have the lock. If it is locked, I want to find out if we have the lock and if so i want to call env->MonitorExit. How would I do this?
[ November 17, 2006: Message edited by: Dan Bizman ]
 
Jayesh Lalwani
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you want to do that?. If you did have a function that checked the status of a monitor, and if you call that function before entering the monitor, your code will not be re-entrant. So, you will need a monitor for your monitor checking code.

Let's say you have a function called checkMonitorOwnership() that returns true if the current thread owns the monitor. Then the following piece of code is not thread safe



It's better to the monitor check it's own ownership in a single atomic operation. MonitorEnter already does that. You don;t need to do anything else.
 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jayesh Lalwani:
Why do you want to do that?. If you did have a function that checked the status of a monitor, and if you call that function before entering the monitor, your code will not be re-entrant. So, you will need a monitor for your monitor checking code.

Let's say you have a function called checkMonitorOwnership() that returns true if the current thread owns the monitor. Then the following piece of code is not thread safe



It's better to the monitor check it's own ownership in a single atomic operation. MonitorEnter already does that. You don;t need to do anything else.


How does MonitorEnter let me know I already have the lock?

I'm fine with calling MonitorEnter twice if I have to, to find out the second call that i already have the lock. Then I'll just call MonitorExit twice.

I'm not checking to see if someone else has the monitor before calling MonitorEnter, rather, I want to call monitorenter and then if I already have it, call monitorexit (twice). Is there any way to do this?
 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, i think I wasn't clear. What i want is this:

* The monitor in Java is actually a counter. When it reaches 0, you can acquire the monitor. I want the value of monitor.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!