Discussion:
[omniORB] omni_mutex::trylock
Luke Deller
2008-09-09 12:22:24 UTC
Permalink
Hello,

Here's a patch to add a non-blocking "trylock" method to the omni_mutex
class in the omnithread library. Would something like this be a
useful addition to the omnithread code base?

See that I added the relevant mapping to a native thread library call
in nt.h, posix.h and VxThread.h. The last of these is untested.

The other implementations (mach.h, pthread_nt.h and solaris.h) appear
to be currently broken due to missing a definition of
OMNI_MUTEX_LOCK_IMPLEMENTATION, so I did not bother with them.

Kind regards,
Luke.

**********************************************************************************************
Important Note
This email (including any attachments) contains information which is confidential and may be subject to legal privilege. If you are not the intended recipient you must not use, distribute or copy this email. If you have received this email in error please notify the
sender immediately and delete this email. Any views expressed in this email are not necessarily the views of IRESS Market Technology Limited.

It is the duty of the recipient to virus scan and otherwise test the information provided before loading onto any computer system.
IRESS Market Technology Limited does not warrant that the information is free of a virus or any other defect or error.
**********************************************************************************************
Luke Deller
2008-09-09 12:24:58 UTC
Permalink
[resending with the attachment this time, oops!]

Here's a patch to add a non-blocking "trylock" method to the omni_mutex
class in the omnithread library. Would something like this be a
useful addition to the omnithread code base?

See that I added the relevant mapping to a native thread library call
in nt.h, posix.h and VxThread.h. The last of these is untested.

The other implementations (mach.h, pthread_nt.h and solaris.h) appear
to be currently broken due to missing a definition of
OMNI_MUTEX_LOCK_IMPLEMENTATION, so I did not bother with them.

Kind regards,
Luke.


**********************************************************************************************
Important Note
This email (including any attachments) contains information which is confidential and may be subject to legal privilege. If you are not the intended recipient you must not use, distribute or copy this email. If you have received this email in error please notify the
sender immediately and delete this email. Any views expressed in this email are not necessarily the views of IRESS Market Technology Limited.

It is the duty of the recipient to virus scan and otherwise test the information provided before loading onto any computer system.
IRESS Market Technology Limited does not warrant that the information is free of a virus or any other defect or error.
**********************************************************************************************
-------------- next part --------------
A non-text attachment was scrubbed...
Name: omni-trylock.patch
Type: application/octet-stream
Size: 3307 bytes
Desc: omni-trylock.patch
Url : http://www.omniorb-support.com/pipermail/omniorb-list/attachments/20080909/751605a1/omni-trylock.obj
Alex Tingle
2008-09-09 19:53:47 UTC
Permalink
Why not add a helper class analogous to omni_mutex_lock? Something like:

class _OMNITHREAD_NTDLL_ omni_mutex_trylock {
omni_mutex& mutex;
public:
const int locked;
omni_mutex_trylock(omni_mutex& m) : mutex(m),
locked(mutex.trylock()) {}
~omni_mutex_trylock(void) { if(locked) mutex.unlock(); }
operator int(void) const { return locked; }
private:
// dummy copy constructor and operator= to prevent copying
omni_mutex_trylock(const omni_mutex_trylock&);
omni_mutex_trylock& operator=(const omni_mutex_trylock&);
};


It would be used like this:

{
omni_mutex_trylock lock(m);
if(lock)
{
// Got mutex.
}
else
{
// Failed to get mutex.
}
}


If you don't like the cast overloading, then you could just use the
public const member...

if(lock.locked) ...


regards,

-Alex

--
Post by Luke Deller
[resending with the attachment this time, oops!]
Here's a patch to add a non-blocking "trylock" method to the
omni_mutex
class in the omnithread library. Would something like this be a
useful addition to the omnithread code base?
See that I added the relevant mapping to a native thread library call
in nt.h, posix.h and VxThread.h. The last of these is untested.
The other implementations (mach.h, pthread_nt.h and solaris.h) appear
to be currently broken due to missing a definition of
OMNI_MUTEX_LOCK_IMPLEMENTATION, so I did not bother with them.
Kind regards,
Luke.
**********************************************************************************************
Important Note
This email (including any attachments) contains information which is
confidential and may be subject to legal privilege. If you are not
the intended recipient you must not use, distribute or copy this
email. If you have received this email in error please notify the
sender immediately and delete this email. Any views expressed in
this email are not necessarily the views of IRESS Market Technology
Limited.
It is the duty of the recipient to virus scan and otherwise test the
information provided before loading onto any computer system.
IRESS Market Technology Limited does not warrant that the
information is free of a virus or any other defect or error.
**********************************************************************************************
<omni-trylock.patch><mime-attachment.txt>
Duncan Grisby
2008-09-19 20:53:51 UTC
Permalink
Post by Luke Deller
Here's a patch to add a non-blocking "trylock" method to the omni_mutex
class in the omnithread library. Would something like this be a
useful addition to the omnithread code base?
Thanks! I've checked it in to CVS. I've added Alex's suggested
omni_mutex_trylock class too. Thanks for that, Alex.

Cheers,

Duncan.
--
-- Duncan Grisby --
-- ***@grisby.org --
-- http://www.grisby.org --
Loading...