RE: Are PR_Locks thread re-entrant?
"Francisco Puentes" <[email protected]>
| Newsgroups | gmane.comp.mozilla.devel.nspr |
|---|---|
| Message-ID | <001101c91bdc$5df4a900$19ddfb00$@es> |
I use Monitors to allow reentrant lock. It works for my very well.
I have implemented two classes: Monitor and Lockable.
Follows the code: (Monitor.h, Monitor.cpp, Lockable.h, Lockable.cpp)
--------------- cut here
------------------------------------------------------------------
#include <nspr.h>
#include <prmon.h>
#ifndef _MONITOR_H
#define _MONITOR_H
class Monitor
{
private:
PRMonitor*monitor;
public:
Monitor();
virtual ~Monitor();
void enter();
void exit();
bool wait(PRIntervalTime ticks);
void notify(bool all=false);
};
#endif
--------------- cut here
------------------------------------------------------------------
#include "Monitor.h"
Monitor::Monitor()
{
monitor=PR_NewMonitor();
}
Monitor::~Monitor()
{
PR_DestroyMonitor(monitor);
}
void Monitor::enter()
{
PR_EnterMonitor(monitor);
}
void Monitor::exit()
{
PR_ExitMonitor(monitor);
}
bool Monitor::wait(PRIntervalTime ticks)
{
return (PR_Wait(monitor,ticks)==PR_SUCCESS);
}
void Monitor::notify(bool all)
{
if(all)
{
PR_NotifyAll(monitor);
}
else
{
PR_Notify(monitor);
}
}
--------------- cut here
------------------------------------------------------------------
#include "Monitor.h"
#ifndef _LOCKABLE_H
#define _LOCKABLE_H
class Lockable: protected Monitor
{
private:
public:
Lockable();
virtual ~Lockable();
void lock();
void unlock();
};
#endif
--------------- cut here
------------------------------------------------------------------
#include "Lockable.h"
Lockable::Lockable(): Monitor()
{
}
Lockable::~Lockable()
{
}
void Lockable::lock()
{
enter();
}
void Lockable::unlock()
{
exit();
}
--------------- cut here
------------------------------------------------------------------
As you can see I use PR_EnterMonitor and PR_ExitMonitor to implement Lock
and Unlock.
Monitor operations are reentrant.
-----Mensaje original-----
De: [email protected]
[mailto:[email protected]] En nombre
de Rich Megginson
Enviado el: sábado, 20 de septiembre de 2008 1:25
Para: [email protected]
Asunto: Re: Are PR_Locks thread re-entrant?
Wan-Teh Chang wrote:
> On Fri, Sep 19, 2008 at 4:08 PM, Rich Megginson
> <[email protected]> wrote:
>> That is, can you lock a mutex more than once in the same thread? We
>> have a case where a function acquires a PR_Lock mutex and is called
>> recursively.
>
> No, you can't lock a PRLock more than once in the same thread.
> That's a programming error.
Any suggestions? I need to allow re-entrant code in the same thread but
lock out other threads.
>
> Wan-Teh
_______________________________________________
dev-tech-nspr mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-nspr