From owner-freebsd-hackers@freebsd.org Mon Nov 21 14:16:22 2016 Return-Path: Delivered-To: freebsd-hackers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id C1FE9C4C115 for ; Mon, 21 Nov 2016 14:16:22 +0000 (UTC) (envelope-from Volker.Lendecke@SerNet.DE) Received: from mail.SerNet.de (mail1.SerNet.de [193.175.80.2]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 8C5B71DB7 for ; Mon, 21 Nov 2016 14:16:21 +0000 (UTC) (envelope-from Volker.Lendecke@SerNet.DE) Received: from intern.SerNet.DE by mail.SerNet.DE with esmtps (Exim 4.84_2 #1) id 1c8pOM-0001KM-Mq; Mon, 21 Nov 2016 15:16:18 +0100 Received: by intern.sernet.de id 1c8pOM-0004gu-K9; Mon, 21 Nov 2016 15:16:18 +0100 Date: Mon, 21 Nov 2016 15:16:16 +0100 From: Volker Lendecke To: Konstantin Belousov Cc: freebsd-hackers@freebsd.org Subject: Re: process shared mutexes? Message-ID: <20161121141616.GB30947@sernet.de> Reply-To: Volker.Lendecke@SerNet.DE References: <20161121133528.GA30947@sernet.de> <20161121135036.GY54029@kib.kiev.ua> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20161121135036.GY54029@kib.kiev.ua> User-Agent: Mutt/1.5.23 (2014-03-12) X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 21 Nov 2016 14:16:22 -0000 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? > > > > Thanks, > > > > There is no attached program, please mail it either inline or put it > somewhere on web. Hmm. Inline now. Volker #include #include #include #include #include #include #include #include #include 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 \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; }