Re: light weight user level semaphores

Ulrich Drepper
2001-04-19T19:48:49Z

Alan Cox <alan@lxorguk.ukuu.org.uk> writes:

> > can libraries use fast semaphores behind the back of the user? They might
> > well want to use the semaphores exactly for things like memory allocator
> > locking etc. But libc certainly cant use fd's behind peoples backs.
>
> libc is entitled to, and most definitely does exactly that. Take a look at
> things like gethostent, getpwent etc etc.

You are mixing two completely different things.

Functions like gethostent() and catopen() are explicitly allowed to be
implemented using file descriptors. If this is allowed the standard
contains appropriate wording.

Other functions like setlocale() do use file descriptors, yes, but
these are not kept. Before the function returns they are closed.
This can cause disruptions in other threads which find descriptors not
allocated sequentially but this has to be taken into account. Rules
for multi-threaded applications are different. A single-threaded
application will not see such a difference.

Now, the standards do not allow POSIX mutexes to be implemented using
file descriptors. The same is true for unnamed POSIX semaphores. So
Linus is right, though for a different reason than he thought.

The situation is a bit different for named POSIX semaphores. These
can be implemented using file descriptors. But they don't have to and
IMO they shouldn't. A memory reference based semaphore implementation
would allow a named semaphore to be implemented using

fd = open (name)
addr = mmap (..fd..)
close (fd)
sem_syscall (addr)

i.e., it can be mapped to a memory reference again.

--
---------------. ,-. 1325 Chesapeake Terrace
Ulrich Drepper \ ,-------------------' \ Sunnyvale, CA 94089 USA
Red Hat `--' drepper at redhat.com `------------------------