Skip to contentSkip to conversation

man page for robust mutexes

PATCH In review Sebastian Andrzej Siewior 3 participants 6 messages Dec 1, 2008 – Dec 22, 2008 Man-pages: manual pages for Linux -- Sections 2, 3, 4, 5, and 7

lore
Share & tools
lore
#0 lore raw

Robust mutexes are available since a few days, so here is a man page for them. I used pthread_mutexattr_setprotocol.3p as a template and sneaked a few phrases from there. That's my first man-page however, so some things might be wrong :)

Sebastian

man3p/pthread_mutexattr_setrobust_np.3p new file +240 -0
@@ -0,0 +1,240 @@
+.\" Copyright (c) 2008 Sebastian Andrzej Siewior
+.TH "ROBUST MUTEXES" P 2008 "" "POSIX Programmer's Manual"
+.\" pthread_mutexattr_setrobust_np
+.SH NAME
+pthread_mutexattr_setrobust_np, pthread_mutexattr_getrobust_np \- set or get
+the robustness of a mutex
+.SH SYNOPSIS
+.LP
+\fB#include <pthread.h>
+.br
+.sp
+#define __USE_GNU
+.sp
+int pthread_mutexattr_getrobust_np(pthread_mutexattr_t *\fP\fIattr\fP\fB, int
+\fP\fIrobustness\fP\fB);
+.br
+int pthread_mutexattr_setrobust_np(pthread_mutexattr_t *\fP\fIattr\fP\fB, int
+\fP\fIrobustness\fP\fB);
+.br
+int pthread_mutex_consistent_np(pthread_mutex_t *\fP\fImutex\fP\fB);
+\fP
+\fB
+.br
+\fP
+.SH DESCRIPTION
+.LP
+The \fIpthread_mutexattr_getrobust_np\fP() and
+\fIpthread_mutexattr_setrobust_np\fP() functions, respectively, shall get and
+set the robustness attribute of a mutex attributes object pointed to by
+\fIattr\fP which was previously created by the function
+\fIpthread_mutexattr_init\fP().
+.LP
+The \fIrobustness\fP attribute defines the robustness to be used in utilizing
+mutexes. The value of \fIrobustness\fP may be one of:
+.LP
+.sp
+PTHREAD_MUTEX_STALLED_NP
+.br
+.sp
+PTHREAD_MUTEX_ROBUST_NP
+.br
+.sp
+.LP
+which are defined in the \fI<pthread.h>\fP header if the GNU extensions are
+used.
+.LP
+The default attribute is PTHREAD_MUTEX_STALLED_NP.
+.LP
+When a mutex is created with PTHREAD_MUTEX_STALLED_NP is locked and the owner
+dies, then the next call to pthread_mutex_lock() will block forever. Also,
+the already waiting waiters will wait for ever.
+.LP
+The behavior is different if the mutex is created with
+PTHREAD_MUTEX_ROBUST_NP. If the owner dies while holding the lock, the
+next call to \fIpthread_mutex_lock\fP() will return EOWNERDEAD and the caller
+will acquire lock. The new owner should call
+\fIpthread_mutex_consistent_np\fP() on the mutex once the internal state of
+the protected variables are consistent again. If this is not done, future
+calls to pthread_mutex_lock() will continue to return EOWNERDEAD (although
+locking will function correctly).
+.SH RETURN VALUE
+.LP
+Upon successful completion, \fIpthread_mutexattr_setrobust_np\fP(),
+\fIpthread_mutexattr_getrobust_np\fP() and
+\fIpthread_mutex_consistent_np\fP()shall return zero; otherwise,
+an error number shall be returned to indicate the error.
+.SH ERRORS
+.LP
+The \fIpthread_mutexattr_destroy\fP() function may fail if:
+.TP 7
+.B EINVAL
+The value specified by \fIrobustness\fP is invalid.
+.LP
+The \fIpthread_mutex_consistent_np\fp() function may fail if:
+.TP 7
+.B EINVAL
+The mutex specified by \fImutex\fP is either not PTHREAD_MUTEX_ROBUST_NP or is
+in consistent state.
+\fIThe following sections are informative.\fP
+.SH EXAMPLES
+.LP
+The robost mutexes could be used to share a common lock accross multiple
+process and avoid IPC communication. Here is an example:
+.sp
+.RS
+.nf
+\fB
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <sys/mman.h>
+#include <errno.h>
+
+#define __USE_GNU
+#include <pthread.h>
+
+static const char *lock_name = "/dev/shm/limi_lock";
+static int lock_fd;
+static void *limi_ressource;
+static pthread_mutex_t *limi_mutex;
+
+.sp
+
+static int open_existing_lock(void)
+{
+	int fd;
+	int ret;
+	struct stat buf;
+	int retry = 5;
+
+	fd = open(lock_name, O_RDWR);
+	if (fd < 0)
+		return fd;
+	do {
+
+		ret = fstat(fd, &buf);
+		if (ret < 0)
+			return ret;
+
+		if (buf.st_size == sizeof(*limi_mutex))
+			return fd;
+
+		sleep(1);
+		retry--;
+	} while (retry);
+
+	close(fd);
+	return -1;
+}
+
+.sp
+
+static int create_new_lock(void)
+{
+	int fd;
+	pthread_mutex_t cmutex = PTHREAD_MUTEX_INITIALIZER;
+	pthread_mutexattr_t attr;
+	int ret;
+
+	pthread_mutexattr_init(&attr);
+	pthread_mutexattr_setrobust_np(&attr, PTHREAD_MUTEX_ROBUST_NP);
+	pthread_mutex_init(&cmutex, &attr);
+
+	fd = open(lock_name, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR |
+			S_IRGRP | S_IWGRP);
+	if (fd < 0)
+		return fd;
+
+	ret = write(fd, &cmutex, sizeof(cmutex));
+	if (ret < 0) {
+		fprintf(stderr, "Write to %s failed: %s\n",
+				lock_name, strerror(errno));
+		exit(1);
+	}
+	return fd;
+}
+
+.sp
+
+void limi_lock_init(void)
+{
+	lock_fd = open_existing_lock();
+	if (lock_fd < 0) {
+		lock_fd = create_new_lock();
+		if (lock_fd < 0) {
+			lock_fd = open_existing_lock();
+			if (lock_fd < 0) {
+				fprintf(stderr, "Can't open %s: %s\n",
+						lock_name, strerror(errno));
+				exit(1);
+			}
+		}
+	}
+
+	limi_lock_mmap = mmap(NULL, sizeof(*limi_mutex),
+			PROT_READ | PROT_WRITE, MAP_SHARED, lock_fd, 0);
+
+	if (limi_lock_mmap == MAP_FAILED) {
+		fprintf(stderr, "failed to mmap limi lock: %s\n",
+				strerror(errno));
+		exit(1);
+	}
+	limi_mutex = limi_lock_mmap;
+}
+
+.sp
+
+void limi_lock(void)
+{
+	int ret;
+
+	ret = pthread_mutex_lock(limi_mutex);
+	if (!ret)
+		return;
+
+	if (ret == EOWNERDEAD) {
+		pthread_mutex_consistent_np(limi_mutex);
+		return;
+	}
+
+	fprintf(stderr, "Can not grab lock: %s\n", strerror(ret));
+	exit(1);
+}
+
+.sp
+
+void limi_unlock(void)
+{
+	int ret;
+
+	ret = pthread_mutex_unlock(limi_mutex);
+	if (!ret)
+		return;
+
+	fprintf(stderr, "Can not unlock: %s\n", strerror(ret));
+	exit(1);
+}
+
+\fP
+.fi
+.RE
+.LP
+The code example shows how to share a lock between two applications without
+classic IPC. If one of the applications dies while holding the lock or the
+system reboots unexpectly, the new owner of lock marks the lock state
+consistent. In this example the lock owner does not need to perform any
+validation of the resource protected by the lock. The lock owner knows if
+the previous owner unlocked successfully or died.
+.f
+.SH SEE ALSO
+.LP
+\fIpthread_mutex_create\fP(), \fIpthread_mutex_destroy\fP(),
+\fIpthread_mutexattr_init\fP(), \fIpthread_mutexattr_destroy\fP(),
+\fIpthread_mutex_lock\fP(), \fIpthread_mutex_unlock\fP(), \fI<pthread.h>\fP
+.SH COPYRIGHT
+This man page was contributed by Sebastian Andrzej Siewior.

--
To unsubscribe from this list: send the line "unsubscribe linux-man" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#1 lore raw

Sebastian,

Thanks for writing this page!

I will have more comments later, but a few quick comments now. Could you revise your page in the light of these comments?

Quoted 3 lines · Sebastian Andrzej Siewior

Robust mutexes are available since a few days, so here is a man page for them. I used pthread_mutexattr_setprotocol.3p as a template and sneaked a few phrases from there.

Please be careful here. If there are any sentences taken from a copyrighted source (the 3p pages are copyright by the IEEE), could you please rewrite in your own words.

Quoted 2 lines

That's my first man-page however, so some things
might be wrong :)

Could you take a look at man-pages(7) please -- that will clarify some of my comments below.

man3p/pthread_mutexattr_setrobust_np.3p line 1

Quoted 5 lines

Sebastian

man3p/pthread_mutexattr_setrobust_np.3p new file +1 -0
@@ -0,0 +1,240 @@
+.\" Copyright (c) 2008 Sebastian Andrzej Siewior

You need to put this page under a free license. Have a look here:
http://www.kernel.org/doc/man-pages/licenses.html
The "verbatim" license is the most widely used, and my preference for new pages, but it's your choice.

man3p/pthread_mutexattr_setrobust_np.3p line 2

Quoted 1 line
man3p/pthread_mutexattr_setrobust_np.3p new file +1 -0
+.TH "ROBUST MUTEXES" P 2008 "" "POSIX Programmer's Manual"

s/P/3/

s/POSIX Programmer's Manual/Linux Programmer's Manual/

man3p/pthread_mutexattr_setrobust_np.3p line 8

Quoted 6 lines
man3p/pthread_mutexattr_setrobust_np.3p new file +6 -0
+.\" pthread_mutexattr_setrobust_np
+.SH NAME
+pthread_mutexattr_setrobust_np, pthread_mutexattr_getrobust_np \- set or get
+the robustness of a mutex
+.SH SYNOPSIS
+.LP

I prefer .nf/.fi blocks for the SYNOPIS, and then you can drop all of the .br and .sp clutter. See, for example, fcntl(2).

man3p/pthread_mutexattr_setrobust_np.3p line 9

Quoted 1 line
man3p/pthread_mutexattr_setrobust_np.3p new file +1 -0
+\fB#include <pthread.h>

Please, rather make it .B for each of these lines, and .BI where you need to mix with italic See, for example, fcntl(2).

man3p/pthread_mutexattr_setrobust_np.3p line 12

Quoted 3 lines
man3p/pthread_mutexattr_setrobust_np.3p new file +3 -0
+.br
+.sp
+#define __USE_GNU

s/__USE_GNU/_GNU_SOURCE/

See feature_test_macros(7)

Quoted 5 lines
man3p/pthread_mutexattr_setrobust_np.3p new file +5 -0
+.sp
+int pthread_mutexattr_getrobust_np(pthread_mutexattr_t *\fP\fIattr\fP\fB, int
+\fP\fIrobustness\fP\fB);
+.br
+int pthread_mutexattr_setrobust_np(pthread_mutexattr_t *\fP\fIattr\fP\fB, int

man3p/pthread_mutexattr_setrobust_np.3p line 27

Quoted 10 lines
man3p/pthread_mutexattr_setrobust_np.3p new file +10 -0
+\fP\fIrobustness\fP\fB);
+.br
+int pthread_mutex_consistent_np(pthread_mutex_t *\fP\fImutex\fP\fB);
+\fP
+\fB
+.br
+\fP
+.SH DESCRIPTION
+.LP
+The \fIpthread_mutexattr_getrobust_np\fP() and

The convention used in most man-pages sources is

.BR func ()

Could you please swich all instances to that.

man3p/pthread_mutexattr_setrobust_np.3p line 30

Quoted 3 lines
man3p/pthread_mutexattr_setrobust_np.3p new file +3 -0
+\fIpthread_mutexattr_setrobust_np\fP() functions, respectively, shall get and
+set the robustness attribute of a mutex attributes object pointed to by
+\fIattr\fP which was previously created by the function

Preferred is:

.I attr

Could you please change all instances.

man3p/pthread_mutexattr_setrobust_np.3p line 34

Quoted 4 lines
man3p/pthread_mutexattr_setrobust_np.3p new file +4 -0
+\fIpthread_mutexattr_init\fP().
+.LP
+The \fIrobustness\fP attribute defines the robustness to be used in utilizing
+mutexes. The value of \fIrobustness\fP may be one of:

Please start new sentences on new source lines. (See man-pages(7).)

man3p/pthread_mutexattr_setrobust_np.3p line 44

Quoted 10 lines
man3p/pthread_mutexattr_setrobust_np.3p new file +10 -0
+.LP
+.sp
+PTHREAD_MUTEX_STALLED_NP
+.br
+.sp
+PTHREAD_MUTEX_ROBUST_NP
+.br
+.sp
+.LP
+which are defined in the \fI<pthread.h>\fP header if the GNU extensions are

Use ".I"

man3p/pthread_mutexattr_setrobust_np.3p line 47

Quoted 3 lines
man3p/pthread_mutexattr_setrobust_np.3p new file +3 -0
+used.
+.LP
+The default attribute is PTHREAD_MUTEX_STALLED_NP.

Use .B for constants

man3p/pthread_mutexattr_setrobust_np.3p line 55

Quoted 8 lines
man3p/pthread_mutexattr_setrobust_np.3p new file +8 -0
+.LP
+When a mutex is created with PTHREAD_MUTEX_STALLED_NP is locked and the owner
+dies, then the next call to pthread_mutex_lock() will block forever. Also,
+the already waiting waiters will wait for ever.
+.LP
+The behavior is different if the mutex is created with
+PTHREAD_MUTEX_ROBUST_NP. If the owner dies while holding the lock, the
+next call to \fIpthread_mutex_lock\fP() will return EOWNERDEAD and the caller

Use .B for constants

man3p/pthread_mutexattr_setrobust_np.3p line 62

Quoted 7 lines
man3p/pthread_mutexattr_setrobust_np.3p new file +7 -0
+will acquire lock. The new owner should call
+\fIpthread_mutex_consistent_np\fP() on the mutex once the internal state of
+the protected variables are consistent again. If this is not done, future
+calls to pthread_mutex_lock() will continue to return EOWNERDEAD (although
+locking will function correctly).
+.SH RETURN VALUE
+.LP

.LP is needed after .SH

man3p/pthread_mutexattr_setrobust_np.3p line 69

Quoted 7 lines
man3p/pthread_mutexattr_setrobust_np.3p new file +7 -0
+Upon successful completion, \fIpthread_mutexattr_setrobust_np\fP(),
+\fIpthread_mutexattr_getrobust_np\fP() and
+\fIpthread_mutex_consistent_np\fP()shall return zero; otherwise,
+an error number shall be returned to indicate the error.
+.SH ERRORS
+.LP
+The \fIpthread_mutexattr_destroy\fP() function may fail if:

Wrong function nam above.

man3p/pthread_mutexattr_setrobust_np.3p line 78

Quoted 9 lines
man3p/pthread_mutexattr_setrobust_np.3p new file +9 -0
+.TP 7
+.B EINVAL
+The value specified by \fIrobustness\fP is invalid.
+.LP
+The \fIpthread_mutex_consistent_np\fp() function may fail if:
+.TP 7
+.B EINVAL
+The mutex specified by \fImutex\fP is either not PTHREAD_MUTEX_ROBUST_NP or is
+in consistent state.

"in an incinsistent state"?

Quoted 10 lines
man3p/pthread_mutexattr_setrobust_np.3p new file +10 -0
+\fIThe following sections are informative.\fP
+.SH EXAMPLES
+.LP
+The robost mutexes could be used to share a common lock accross multiple
+process and avoid IPC communication. Here is an example:
+.sp
+.RS
+.nf
+\fB
+#include <sys/types.h>

man3p/pthread_mutexattr_setrobust_np.3p line 98

Quoted 10 lines
man3p/pthread_mutexattr_setrobust_np.3p new file +10 -0
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <sys/mman.h>
+#include <errno.h>
+
+#define __USE_GNU

s/__USE_GNU/_GNU_SOURCE/
and move to top of listing (see feature_test_macros(7).

Quoted 20 lines
man3p/pthread_mutexattr_setrobust_np.3p new file +20 -0
+#include <pthread.h>
+
+static const char *lock_name = "/dev/shm/limi_lock";
+static int lock_fd;
+static void *limi_ressource;
+static pthread_mutex_t *limi_mutex;
+
+.sp
+
+static int open_existing_lock(void)
+{
+       int fd;
+       int ret;
+       struct stat buf;
+       int retry = 5;
+
+       fd = open(lock_name, O_RDWR);
+       if (fd < 0)
+               return fd;
+       do {

man3p/pthread_mutexattr_setrobust_np.3p line 128

Quoted 10 lines
man3p/pthread_mutexattr_setrobust_np.3p new file +10 -0
+
+               ret = fstat(fd, &buf);
+               if (ret < 0)
+                       return ret;
+
+               if (buf.st_size == sizeof(*limi_mutex))
+                       return fd;
+
+               sleep(1);
+               retry--;

s/-/\\-/ for each '-' in source code.

man3p/pthread_mutexattr_setrobust_np.3p line 132

Quoted 4 lines
man3p/pthread_mutexattr_setrobust_np.3p new file +4 -0
+       } while (retry);
+
+       close(fd);
+       return -1;

See previous comment.

Quoted 84 lines
man3p/pthread_mutexattr_setrobust_np.3p new file +84 -0
+}
+
+.sp
+
+static int create_new_lock(void)
+{
+       int fd;
+       pthread_mutex_t cmutex = PTHREAD_MUTEX_INITIALIZER;
+       pthread_mutexattr_t attr;
+       int ret;
+
+       pthread_mutexattr_init(&attr);
+       pthread_mutexattr_setrobust_np(&attr, PTHREAD_MUTEX_ROBUST_NP);
+       pthread_mutex_init(&cmutex, &attr);
+
+       fd = open(lock_name, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR |
+                       S_IRGRP | S_IWGRP);
+       if (fd < 0)
+               return fd;
+
+       ret = write(fd, &cmutex, sizeof(cmutex));
+       if (ret < 0) {
+               fprintf(stderr, "Write to %s failed: %s\n",
+                               lock_name, strerror(errno));
+               exit(1);
+       }
+       return fd;
+}
+
+.sp
+
+void limi_lock_init(void)
+{
+       lock_fd = open_existing_lock();
+       if (lock_fd < 0) {
+               lock_fd = create_new_lock();
+               if (lock_fd < 0) {
+                       lock_fd = open_existing_lock();
+                       if (lock_fd < 0) {
+                               fprintf(stderr, "Can't open %s: %s\n",
+                                               lock_name, strerror(errno));
+                               exit(1);
+                       }
+               }
+       }
+
+       limi_lock_mmap = mmap(NULL, sizeof(*limi_mutex),
+                       PROT_READ | PROT_WRITE, MAP_SHARED, lock_fd, 0);
+
+       if (limi_lock_mmap == MAP_FAILED) {
+               fprintf(stderr, "failed to mmap limi lock: %s\n",
+                               strerror(errno));
+               exit(1);
+       }
+       limi_mutex = limi_lock_mmap;
+}
+
+.sp
+
+void limi_lock(void)
+{
+       int ret;
+
+       ret = pthread_mutex_lock(limi_mutex);
+       if (!ret)
+               return;
+
+       if (ret == EOWNERDEAD) {
+               pthread_mutex_consistent_np(limi_mutex);
+               return;
+       }
+
+       fprintf(stderr, "Can not grab lock: %s\n", strerror(ret));
+       exit(1);
+}
+
+.sp
+
+void limi_unlock(void)
+{
+       int ret;
+
+       ret = pthread_mutex_unlock(limi_mutex);
+       if (!ret)

man3p/pthread_mutexattr_setrobust_np.3p line 226

Quoted 10 lines
man3p/pthread_mutexattr_setrobust_np.3p new file +10 -0
+               return;
+
+       fprintf(stderr, "Can not unlock: %s\n", strerror(ret));
+       exit(1);
+}
+
+\fP
+.fi
+.RE
+.LP

I think it's better to have the explanation of the programs before the listing. Could you relocate it please.

Quoted 4 lines
man3p/pthread_mutexattr_setrobust_np.3p new file +4 -0
+The code example shows how to share a lock between two applications without
+classic IPC. If one of the applications dies while holding the lock or the
+system reboots unexpectly, the new owner of lock marks the lock state
+consistent. In this example the lock owner does not need to perform any

man3p/pthread_mutexattr_setrobust_np.3p line 240

Quoted 10 lines
man3p/pthread_mutexattr_setrobust_np.3p new file +10 -0
+validation of the resource protected by the lock. The lock owner knows if
+the previous owner unlocked successfully or died.
+.f
+.SH SEE ALSO
+.LP
+\fIpthread_mutex_create\fP(), \fIpthread_mutex_destroy\fP(),
+\fIpthread_mutexattr_init\fP(), \fIpthread_mutexattr_destroy\fP(),
+\fIpthread_mutex_lock\fP(), \fIpthread_mutex_unlock\fP(), \fI<pthread.h>\fP
+.SH COPYRIGHT
+This man page was contributed by Sebastian Andrzej Siewior.

Take a look, and you'll see that *no* pages in man-pages carry copyright notices in the formatted output. The copyright notice in the page source suffices.

Cheers,

Michael

Signature · 9 lines
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
git://git.kernel.org/pub/scm/docs/man-pages/man-pages.git
man-pages online: http://www.kernel.org/doc/man-pages/online_pages.html
Found a bug? http://www.kernel.org/doc/man-pages/reporting_bugs.html
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#2 lore raw

PATCH v2 1/1 add man-page for pthread_mutexattr_setrobust_np()

Quoted 1 line · * Michael Kerrisk | 2008-12-07 11:44:54 [-0500]:

Sebastian,

Michael,

Quoted 2 lines

I will have more comments later, but a few quick comments now. Could you revise your page in the light of these comments?

Yup. Here is version two. I somehow mixed up the IEEE man-pages with yours. So first I didn't understand most of you comments :) Once I figured that out I tried to address all of your comments and I've also rephrased everything. Be prepared :)

---
 man3/pthread_mutex_consistent_np.3    |    1 +
 man3/pthread_mutexattr_getrobust_np.3 |    1 +
 man3/pthread_mutexattr_setrobust_np.3 |  255 +++++++++++++++++++++++++++++++++
 3 files changed, 257 insertions(+), 0 deletions(-)
 create mode 100644 man3/pthread_mutex_consistent_np.3
 create mode 100644 man3/pthread_mutexattr_getrobust_np.3
 create mode 100644 man3/pthread_mutexattr_setrobust_np.3
man3/pthread_mutex_consistent_np.3 new file +1 -0
new file mode 100644
index 0000000..a92edb9
@@ -0,0 +1 @@
+.so man3/pthread_mutexattr_setrobust_np.3
man3/pthread_mutexattr_getrobust_np.3 new file +1 -0
new file mode 100644
index 0000000..a92edb9
@@ -0,0 +1 @@
+.so man3/pthread_mutexattr_setrobust_np.3
man3/pthread_mutexattr_setrobust_np.3 new file +255 -0

255 lines not on this page · raw message

Signature · 6 lines
1.6.0.4

--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#3 lore raw

Re: [PATCH v2] add man-page for pthread_mutexattr_setrobust_np()

Quoted 4 lines · Sebastian Andrzej Siewior
Quoted 1 line · * Michael Kerrisk | 2008-12-07 11:44:54 [-0500]:

Sebastian,

Michael,

Sebastian, Michael,

Quoted 31 lines

+.SH EXAMPLE

man3/pthread_mutexattr_setrobust_np.3 new file +30 -0
+The code example shows how to share a lock between two applications without
+System V IPC.
+An advantage over System V semaphores is that the kernel is not invoked in
+case the lock is not hold.
+If one of the applications dies while holding the lock or the system reboots
+unexpectedly, the new owner of lock marks the lock state consistent.
+In this example the lock owner does not need to perform any validation of the
+resource protected by the lock.
+
+.nf
+#define _GNU_SOURCE
+
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+static const char *lock_name = "/dev/shm/limi_lock";
+static pthread_mutex_t *limi_mutex;
+
+static int open_existing_lock(void)
+{
+    int fd;

man3/pthread_mutexattr_setrobust_np.3 line 152

Quoted 10 lines
man3/pthread_mutexattr_setrobust_np.3 new file +10 -0
+    int ret;
+    struct stat buf;
+    int retry = 5;
+
+    fd = open(lock_name, O_RDWR);
+    if (fd < 0)
+        return fd;
+    do {
+        ret = fstat(fd, &buf);
+        if (ret < 0)

Isn't here a close(2) missing?

man3/pthread_mutexattr_setrobust_np.3 line 158

Quoted 6 lines
man3/pthread_mutexattr_setrobust_np.3 new file +6 -0
+            return ret;
+
+        if (buf.st_size == sizeof(*limi_mutex))
+            return fd;
+
+        close(fd);

Isn't this close(2) wrong here?

Quoted 15 lines
man3/pthread_mutexattr_setrobust_np.3 new file +15 -0
+        sleep(1);
+        retry\-\-;
+    } while (retry);
+
+    close(fd);
+    return \-1;
+}
+
+static int create_new_lock(void)
+{
+    int fd;
+    pthread_mutex_t cmutex = PTHREAD_MUTEX_INITIALIZER;
+    pthread_mutexattr_t attr;
+    int ret;
+

man3/pthread_mutexattr_setrobust_np.3 line 183

Quoted 10 lines
man3/pthread_mutexattr_setrobust_np.3 new file +10 -0
+    pthread_mutexattr_init(&attr);
+    pthread_mutexattr_setrobust_np(&attr, PTHREAD_MUTEX_ROBUST_NP);
+    pthread_mutex_init(&cmutex, &attr);
+
+    fd = open(lock_name, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR |
+            S_IRGRP | S_IWGRP);
+    if (fd < 0)
+        return fd;
+
+    ret = write(fd, &cmutex, sizeof(cmutex));

I think its undefined behavior if you copy a struct pthread_mutex. You should use mmap here too.

man3/pthread_mutexattr_setrobust_np.3 line 190

Quoted 7 lines
man3/pthread_mutexattr_setrobust_np.3 new file +7 -0
+    if (ret < 0) {
+        fprintf(stderr, "Write to %s failed: %s\\n",
+                lock_name, strerror(errno));
+        exit(1);
+    }
+    return fd;
+}

--
To unsubscribe from this list: send the line "unsubscribe linux-man" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#4 lore raw

Re: [PATCH v2] add man-page for pthread_mutexattr_setrobust_np()

Quoted 6 lines · * Bert Wesarg | 2008-12-22 08:32:15 [+0100]:
Quoted 6 lines
man3/pthread_mutexattr_setrobust_np.3 new file +6 -0
+static int open_existing_lock(void)
+{
+    int fd;
+    int ret;
+    struct stat buf;
+    int retry = 5;
Quoted 8 lines

man3/pthread_mutexattr_setrobust_np.3 line 152

Quoted 7 lines
man3/pthread_mutexattr_setrobust_np.3 new file +7 -0
+
+    fd = open(lock_name, O_RDWR);
+    if (fd < 0)
+        return fd;
+    do {
+        ret = fstat(fd, &buf);
+        if (ret < 0)

Isn't here a close(2) missing?

yes it is. Thx.

Quoted 7 lines

man3/pthread_mutexattr_setrobust_np.3 line 158

Quoted 6 lines
man3/pthread_mutexattr_setrobust_np.3 new file +6 -0
+            return ret;
+
+        if (buf.st_size == sizeof(*limi_mutex))
+            return fd;
+
+        close(fd);

Isn't this close(2) wrong here?

uhhh. Actually I wanted to have the open() within the do while loop. So I move that and this will be fine then.

Quoted 19 lines
Quoted 19 lines
man3/pthread_mutexattr_setrobust_np.3 new file +19 -0
+        sleep(1);
+        retry\-\-;
+    } while (retry);
+
+    close(fd);
+    return \-1;
+}
+
+static int create_new_lock(void)
+{
+    int fd;
+    pthread_mutex_t cmutex = PTHREAD_MUTEX_INITIALIZER;
+    pthread_mutexattr_t attr;
+    int ret;
+
+    pthread_mutexattr_init(&attr);
+    pthread_mutexattr_setrobust_np(&attr, PTHREAD_MUTEX_ROBUST_NP);
+    pthread_mutex_init(&cmutex, &attr);
+
Quoted 8 lines

man3/pthread_mutexattr_setrobust_np.3 line 183

Quoted 6 lines
man3/pthread_mutexattr_setrobust_np.3 new file +6 -0
+    fd = open(lock_name, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR |
+            S_IRGRP | S_IWGRP);
+    if (fd < 0)
+        return fd;
+
+    ret = write(fd, &cmutex, sizeof(cmutex));

I think its undefined behavior if you copy a struct pthread_mutex. You should use mmap here too.

Why should be this undefined? Is there something special about this struct? And why should this behave different with mmap() ?

Sebastian
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#5 lore raw

Re: [PATCH v2] add man-page for pthread_mutexattr_setrobust_np()

Quoted 19 lines
Quoted 11 lines
Quoted 11 lines
man3/pthread_mutexattr_setrobust_np.3 new file +11 -0
+static int create_new_lock(void)
+{
+    int fd;
+    pthread_mutex_t cmutex = PTHREAD_MUTEX_INITIALIZER;
+    pthread_mutexattr_t attr;
+    int ret;
+
+    pthread_mutexattr_init(&attr);
+    pthread_mutexattr_setrobust_np(&attr, PTHREAD_MUTEX_ROBUST_NP);
+    pthread_mutex_init(&cmutex, &attr);
+
Quoted 8 lines

man3/pthread_mutexattr_setrobust_np.3 line 183

Quoted 6 lines
man3/pthread_mutexattr_setrobust_np.3 new file +6 -0
+    fd = open(lock_name, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR |
+            S_IRGRP | S_IWGRP);
+    if (fd < 0)
+        return fd;
+
+    ret = write(fd, &cmutex, sizeof(cmutex));

I think its undefined behavior if you copy a struct pthread_mutex. You should use mmap here too.

Quoted 2 lines

Why should be this undefined? Is there something special about this struct? And why should this behave different with mmap() ?

If you would use mmap, you would initialize the mutex inside the mmaped area, i.e. directly in the file.

To the copying:

Short answer: http://www.lambdacs.com/cpt/FAQ.html#Q15

Slightly longer:

pthread_mutex_t m1, m2;
pthread_mutex_init(&m1, NULL);
pthread_mutex_lock(&m1);
m2 = m1;
pthread_mutex_unlock(&m2);

How can you be sure, that you have unlocked m1 here?

Yes, you throw away the cmutex after returning from the function and the copy inside the file is the only one left. I still think such code should not be in a documentation.

Bert

Quoted 3 lines

Sebastian

--
To unsubscribe from this list: send the line "unsubscribe linux-man" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html