From: Ulrich Drepper <drepper@redhat.com>
To: Linus Torvalds <torvalds@transmeta.com>
Cc: Alexander Viro <viro@math.psu.edu>,
Abramo Bagnara <abramo@alsa-project.org>,
Alon Ziv <alonz@nolaviz.org>,
Kernel Mailing List <linux-kernel@vger.kernel.org>,
Mike Kravetz <mkravetz@sequent.com>
Subject: Re: light weight user level semaphores
Date: 19 Apr 2001 15:46:36 -0700 [thread overview]
Message-ID: <m34rvkzewj.fsf@otr.mynet.cygnus.com> (raw)
In-Reply-To: <Pine.LNX.4.31.0104191433001.1334-100000@penguin.transmeta.com>
In-Reply-To: Linus Torvalds's message of "Thu, 19 Apr 2001 14:41:15 -0700 (PDT)"
Linus Torvalds <torvalds@transmeta.com> writes:
> I'm not interested in re-creating the idiocies of Sys IPC.
I'm not talking about sysv semaphores (couldn't care less). And you
haven't read any of the mails with examples I sent.
If the new interface can be useful for anything it must allow to
implement process-shared POSIX mutexes. The user-level representation
of these mutexes are simple variables which in the case of
inter-process mutexes are placed in shared memory. These variables
must be usable with the normal pthread_mutex_lock() functions and
perform whatever is needed.
Whether the pthread_mutex_init() function for shared mutexes is doing
a lot more work and allocates even more memory, I don't care. The
standard certainly permits this and every pthread_mutex_init() must
have a pthread_mutex_destroy() which allows allocating and freeing
resources (no file descriptor, though). So, yes, your FS_create
syscall can allocate something.
But the question is what handle to put in the pthread_mutex_t variable
so the different processes can use the mutex. It cannot be a file
descriptor since it's not shared between processes. It cannot be a
pointer to some other place in the virtual memory since the place
pointed to might not be (and probably isn't if FS_create is allocating
something in the process setting up the mutex). You could put some
magic cookie in the pthread_mutex_t object the kernel can then use.
So, instead of repeating over and over again the same old story, fill
in the gaps here:
int
pthread_mutex_init (pthread_mutex_t *mutex,
const pthread_mutexattr_t *mutex_attr)
{
if (mutex_attr != NULL && mutex_attr->__pshared != 0)
{
... FILL IN HERE ...
}
else
...intra-process mutex, uninteresting here...
}
int
pthread_mutex_lock (pthread_mutex_t *mutex)
{
if (mutex_attr != NULL && mutex_attr->__pshared != 0)
{
... FILL IN HERE ...
}
else
...intra-process mutex, uninteresting here...
}
int
pthread_mutex_destroy (pthread_mutex_t *mutex)
{
if (mutex_attr != NULL && mutex_attr->__pshared != 0)
{
... FILL IN HERE ...
}
else
...intra-process mutex, uninteresting here...
}
These functions must work with something like this:
~~~~~~~~~~~~~~~~~~~~~ cons.c ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
int
main (int argc, char *argv[])
{
char tmpl[] = "/tmp/fooXXXXXX";
int fd = mkstemp (tmpl);
pthread_mutexattr_t attr;
pthread_mutex_t *m1;
pthread_mutex_t *m2;
void *addr;
volatile int *i;
pthread_mutexattr_init (&attr);
pthread_mutexattr_setpshared (&attr, PTHREAD_PROCESS_SHARED);
ftruncate (fd, 2 * sizeof (*m1) + sizeof (int));
addr = mmap (NULL, sizeof (*m1), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
m1 = addr;
m2 = m1 + 1;
i = (int *) (m2 + 1);
*i = 0;
pthread_mutex_init (m1, &attr);
pthread_mutex_lock (m1);
pthread_mutex_init (m2, &attr);
pthread_mutex_lock (m2);
if (fork () == 0)
{
char buf[10];
snprintf (buf, sizeof buf, "%d", fd);
execl ("./prod", "prod", buf, NULL);
}
while (1)
{
pthread_mutex_lock (m1);
printf ("*i = %d\n", *i);
pthread_mutex_unlock (m2);
}
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~prod.c ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
int
main (int argc, char *argv[])
{
int fd = atoi (argv[1]);
void *addr;
pthread_mutex_t *m1;
pthread_mutex_t *m2;
volatile int *i;
addr = mmap (NULL, sizeof (*m1), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
m1 = addr;
m2 = m1 + 1;
i = (int *) (m2 + 1);
while (1)
{
++*i;
pthread_mutex_unlock (m1);
pthread_mutex_lock (m2);
}
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--
---------------. ,-. 1325 Chesapeake Terrace
Ulrich Drepper \ ,-------------------' \ Sunnyvale, CA 94089 USA
Red Hat `--' drepper at redhat.com `------------------------
next prev parent reply other threads:[~2001-04-19 22:47 UTC|newest]
Thread overview: 62+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20010417114433.D1108@w-mikek2.sequent.com>
2001-04-17 19:48 ` Linus Torvalds
2001-04-18 18:13 ` Bernd Eckenfels
2001-04-18 19:35 ` Ulrich Drepper
2001-04-19 8:20 ` Alon Ziv
2001-04-19 8:52 ` Abramo Bagnara
2001-04-19 9:08 ` Alexander Viro
2001-04-19 10:44 ` Abramo Bagnara
2001-04-19 16:11 ` Linus Torvalds
2001-04-19 16:33 ` Alexander Viro
2001-04-19 16:43 ` Linus Torvalds
2001-04-19 17:33 ` Alexander Viro
2001-04-19 17:38 ` Linus Torvalds
2001-04-19 18:24 ` Alexander Viro
2001-04-19 19:26 ` Ulrich Drepper
2001-04-19 19:35 ` Alan Cox
2001-04-19 20:06 ` Ulrich Drepper
2001-04-19 20:11 ` Alan Cox
2001-04-19 20:26 ` Ulrich Drepper
2001-04-19 20:22 ` Ingo Oeser
2001-04-19 20:40 ` Ulrich Drepper
2001-04-19 20:51 ` Linus Torvalds
2001-04-19 21:38 ` Alan Cox
2001-04-19 20:49 ` Linus Torvalds
2001-04-19 21:18 ` Ulrich Drepper
2001-04-19 21:41 ` Linus Torvalds
2001-04-19 22:46 ` Ulrich Drepper [this message]
2001-04-20 1:35 ` Alexander Viro
2001-04-20 2:45 ` Ulrich Drepper
2001-04-19 16:43 ` Abramo Bagnara
2001-04-19 20:47 ` Ingo Oeser
2001-04-19 20:54 ` Linus Torvalds
2001-04-19 9:08 ` Ingo Oeser
2001-04-19 11:51 ` Alan Cox
2001-04-19 16:03 ` Linus Torvalds
2001-04-19 16:38 ` Alan Cox
2001-04-19 16:46 ` Linus Torvalds
2001-04-19 17:12 ` Alan Cox
2001-04-19 22:35 ` Rogier Wolff
2001-04-20 9:29 ` Olaf Titz
2001-04-20 14:19 ` Jesse Pollard
2001-04-20 18:36 ` Olaf Titz
2001-04-20 23:33 ` Linus Torvalds
2001-04-21 4:06 ` fd allocation [was: light weight user level semaphores] Edgar Toernig
2001-04-22 9:48 ` Olaf Titz
2001-04-22 11:41 ` light weight user level semaphores Alon Ziv
2001-04-22 12:44 ` Alan Cox
2001-04-22 15:19 ` Alon Ziv
2001-04-22 14:31 ` Alexander Viro
2001-04-22 16:08 ` Alon Ziv
2001-04-22 11:41 ` Alon Ziv
2001-04-22 14:18 ` David Woodhouse
2001-04-23 13:19 ` David Howells
2001-04-23 14:48 ` Alon Ziv
2001-04-23 15:40 ` David Howells
2001-04-21 10:13 ` Olaf Titz
2001-04-23 15:34 ` Jeff Garzik
2001-04-23 19:18 ` Ingo Oeser
2001-04-24 0:19 ` David Wagner
2001-04-24 0:41 ` Alexander Viro
2001-04-19 19:47 ` Ulrich Drepper
2001-04-19 18:48 ` Olaf Titz
2001-04-19 13:59 George Talbot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=m34rvkzewj.fsf@otr.mynet.cygnus.com \
--to=drepper@redhat.com \
--cc=abramo@alsa-project.org \
--cc=alonz@nolaviz.org \
--cc=drepper@cygnus.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mkravetz@sequent.com \
--cc=torvalds@transmeta.com \
--cc=viro@math.psu.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®