Date: Mon, 21 Nov 2016 17:10:40 +0200 From: Konstantin Belousov <kostikbel@gmail.com> To: Volker Lendecke <Volker.Lendecke@SerNet.DE> Cc: freebsd-hackers@freebsd.org Subject: Re: process shared mutexes? Message-ID: <20161121151040.GA54029@kib.kiev.ua> In-Reply-To: <20161121141616.GB30947@sernet.de> References: <20161121133528.GA30947@sernet.de> <20161121135036.GY54029@kib.kiev.ua> <20161121141616.GB30947@sernet.de>
next in thread | previous in thread | raw e-mail | index | archive | help
On Mon, Nov 21, 2016 at 03:16:16PM +0100, Volker Lendecke wrote:
> On Mon, Nov 21, 2016 at 03:50:36PM +0200, Konstantin Belousov wrote:
> > On Mon, Nov 21, 2016 at 02:35:28PM +0100, Volker Lendecke wrote:
> > > Hello!
> > >
> > > For Samba's tdb I'm trying to get process shared robust mutexes to
> > > work. However, tdb has a usage pattern that seems to confuse FreeBSD
> > > 11 (32-bit x86 if that matters).
> > >
> > > The attached program fails in the final pthread_mutex_lock call. If I
> > > comment out the call to
> > >
> > > ptr = mmap(NULL, 0xb0, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0x1000);
> > >
> > > it works.
> > >
> > > FWIW, tdb uses robust shared mutexes on Linux successfully for a while
> > > now. I haven't tried Solaris yet, the only other platform I know about
> > > that has them.
> > >
> > > What am I doing wrong?
Please see the libthr(3) man page, in particular, read the RUN-TIME
SETTINGS section, the description of the kern.ipc.umtx_vnode_persistent
sysctl.
Does setting the sysctl to 1 allow your program to run ?
> > >
> > > Thanks,
> > >
> >
> > There is no attached program, please mail it either inline or put it
> > somewhere on web.
>
> Hmm. Inline now.
>
> Volker
>
>
> #include <stdio.h>
> #include <pthread.h>
> #include <errno.h>
> #include <unistd.h>
> #include <sys/types.h>
> #include <sys/stat.h>
> #include <fcntl.h>
> #include <sys/mman.h>
> #include <string.h>
>
> int main(int argc, const char *argv[])
> {
> int fd, ret;
> void *ptr;
> pthread_mutex_t *m;
> pthread_mutexattr_t attr;
>
> if (argc != 2) {
> fprintf(stderr, "usage: %s <filename>\n", argv[0]);
> return 1;
> }
>
> fd = open(argv[1], O_RDWR|O_CREAT, 0644);
> if (fd == -1) {
> perror("open failed");
> return 1;
> }
>
> ret = ftruncate(fd, 0x1000+0xb0);
> if (ret == -1) {
> perror("ftruncate failed");
> return 1;
> }
>
> m = mmap(NULL, 0x1000, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
> if (m == MAP_FAILED) {
> perror("mmap failed");
> return 1;
> }
>
> ret = pthread_mutexattr_init(&attr);
> if (ret != 0) {
> fprintf(stderr, "pthread_mutexattr_init failed: %s\n",
> strerror(ret));
> return 1;
> }
>
> ret = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
> if (ret != 0) {
> fprintf(stderr, "pthread_mutexattr_setpshared failed: %s\n",
> strerror(ret));
> return 1;
> }
>
> ret = pthread_mutex_init(m, &attr);
> if (ret != 0) {
> fprintf(stderr, "pthread_mutex_init failed: %s\n",
> strerror(ret));
> return 1;
> }
>
> ret = munmap(m, 0x1000);
> if (ret == -1) {
> perror("munmap failed");
> return 1;
> }
>
> #if 1
> ptr = mmap(NULL, 0xb0, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0x1000);
> if (ptr == MAP_FAILED) {
> perror("mmap failed");
> return 1;
> }
> #endif
>
> m = mmap(NULL, 0x1000, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
> if (m == MAP_FAILED) {
> perror("mmap failed");
> return 1;
> }
>
> ret = pthread_mutex_lock(m);
> if (ret != 0) {
> fprintf(stderr, "pthread_mutex_lock failed: %s\n",
> strerror(ret));
> return 1;
> }
>
> ret = pthread_mutex_unlock(m);
> if (ret != 0) {
> fprintf(stderr, "pthread_mutex_lock failed: %s\n",
> strerror(ret));
> return 1;
> }
>
> return 0;
> }
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20161121151040.GA54029>
