Possible deadlock and solution

Mark M. Hoffman mhoffman at lightlink.com
Fri Sep 27 20:39:26 CEST 2002


* Matthias Andree <matthias.andree at gmx.de> [2002-09-27 19:57:04 +0200]:
> On Fri, 27 Sep 2002, Gyepi SAM wrote:
> 
> > The solution:
> > 
> > Ensure that register_words always locks the databases in the same order by
> > 1. Changing the declaration of register_words from
> > 
> >   void register_words(int fd, wordlist_t *list, wordlist_t *other);
> > 
> > to
> > 
> >   void register_words(int fd, wordlist_t *good, wordlist_t *bad, int transfer_order);
> 
> The real solution when you're about to lock two items that may be
> interdependent is to do this (pseudo code):
> 
> for(;;) {
>   obtain_blocking(a);
>   if (!obtain_nonblocking(b)) {
>     /* release all that we acquired so far, sleep and retry */
>     release(a); sleep(1); continue;
>   }
> 
>   /* work with a, b */
> 
>   release(b);
>   release(a);
>   break;
> }

I'm with Gyepi here.  Locking in order eliminates deadlock.  When there
are 500 different resources to worry about (like the linux kernel) it's
impossible to know or enforce the ordering.  Then the "real solution"
comes into play.  Here, we're talking about two locks - perfect for the
"academic" solution.

Regards,

-- 
Mark M. Hoffman
mhoffman at lightlink.com



More information about the bogofilter-dev mailing list