Subject: Re: nsswitch implementation questions...
To: None <lukem@telstra.com.au>
From: Marc Boschma <marcb@bms.itg.telecom.com.au>
List: current-users
Date: 01/18/1996 13:59:58
>Now, the hard part is, how do I support reentrant nsswitch stuff (a
>longer term wishlist item of jtc for multithreaded apps)?
>Do I make the "open the database/parse the file" section non-reentrant,
>but allow the lookups to be reentrant?

Luke, in the "open the database/parse the file" bit just do the
following:

static volatile struct nsswitch *db = NULL;

struct nsswitch *
readdb(...)	/* expacts db set to NULL, otherwise drops out */
{
    static Mutex lock;	/* atomic lock */

    if (db == NULL) {		/* see if we have already been here */
	MutexLock(&lock);	/* make sure we are the only one here */
	    if (db == NULL) {	/* did we interleave with another ? */
		db = realreaddb(...); /* no, so do open */
	    }
	MutexUnlock(&lock);	/* safe now */
    }
    return db;
}

to reread the database, ie. after a stat, set db to NULL and call
readdb(...). Only readdb should ever call realreaddb(...).

This has the advantage of not doing a lock unless nessasary (they are
expensive). Refer to http://www.cs.wustl.edu/~schmidt/TSS-pattern.ps.gz
for more on this...

Marc B.
-------
Marc Boschma                          Email:    marcb@telstra.com.au
Senior Software Specialist            Smail:    Locked Bag No. 4840
Broadband and Multimedia Systems,               Melbourne, VIC  8100
Network Systems, ITG.                 Phone:    +61 3 9634 8798
Telstra                               Fax:      +61 3 9634 5469