1 .\"Copyright (c) 2010 Novell Inc., written by Robert Schweikert
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .TH PTHREAD_RWLOCKATTR_SETKIND_NP 3 2021-03-22 "Linux Programmer's Manual"
27 pthread_rwlockattr_setkind_np, pthread_rwlockattr_getkind_np \- set/get
28 the read-write lock kind of the thread read-write lock attribute object
31 .B #include <pthread.h>
33 .BI "int pthread_rwlockattr_setkind_np(pthread_rwlockattr_t *" attr ,
35 .BI "int pthread_rwlockattr_getkind_np("
36 .BI " const pthread_rwlockattr_t *restrict " attr ,
37 .BI " int *restrict " pref );
39 Compile and link with \fI\-pthread\fP.
43 Feature Test Macro Requirements for glibc (see
44 .BR feature_test_macros (7)):
47 .BR pthread_rwlockattr_setkind_np (),
48 .BR pthread_rwlockattr_getkind_np ():
50 _XOPEN_SOURCE >= 500 || _POSIX_C_SOURCE >= 200809L
54 .BR pthread_rwlockattr_setkind_np ()
55 function sets the "lock kind" attribute of the
56 read-write lock attribute object referred to by
58 to the value specified in
62 may be set to one of the following:
64 .B PTHREAD_RWLOCK_PREFER_READER_NP
66 A thread may hold multiple read locks; that is, read locks are recursive.
67 According to The Single Unix Specification, the behavior is unspecified when a
68 reader tries to place a lock, and there is no write lock but writers are
70 Giving preference to the reader, as is set by
71 .BR PTHREAD_RWLOCK_PREFER_READER_NP ,
72 implies that the reader will receive the requested lock, even if
74 As long as there are readers, the writer will be
77 .B PTHREAD_RWLOCK_PREFER_WRITER_NP
78 This is intended as the write lock analog of
79 .BR PTHREAD_RWLOCK_PREFER_READER_NP .
80 This is ignored by glibc because the POSIX requirement to support
81 recursive read locks would cause this option to create trivial
82 deadlocks; instead use
83 .B PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP
84 which ensures the application developer will not take recursive
85 read locks thus avoiding deadlocks.
87 .\" Here is the relevant wording:
89 .\" A thread may hold multiple concurrent read locks on rwlock (that is,
90 .\" successfully call the pthread_rwlock_rdlock() function n times). If
91 .\" so, the thread must perform matching unlocks (that is, it must call
92 .\" the pthread_rwlock_unlock() function n times).
94 .\" By making write-priority work correctly, I broke the above requirement,
95 .\" because I had no clue that recursive read locks are permissible.
97 .\" If a thread which holds a read lock tries to acquire another read lock,
98 .\" and now one or more writers is waiting for a write lock, then the algorithm
99 .\" will lead to an obvious deadlock. The reader will be suspended, waiting for
100 .\" the writers to acquire and release the lock, and the writers will be
101 .\" suspended waiting for every existing read lock to be released.
103 .\" https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_rwlock_rdlock.html
104 .\" https://sourceware.org/legacy-ml/libc-alpha/2000-01/msg00055.html
105 .\" https://sourceware.org/bugzilla/show_bug.cgi?id=7057
107 .B PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP
108 Setting the lock kind to this
109 avoids writer starvation as long as any read locking is not done in a
113 .BR pthread_rwlockattr_getkind_np ()
114 function returns the value of the lock kind attribute of the
115 read-write lock attribute object referred to by
120 On success, these functions return 0.
121 Given valid pointer arguments,
122 .BR pthread_rwlockattr_getkind_np ()
125 .BR pthread_rwlockattr_setkind_np ()
126 returns a nonzero error number.
131 specifies an unsupported value.
134 .BR pthread_rwlockattr_getkind_np ()
136 .BR pthread_rwlockattr_setkind_np ()
137 functions first appeared in glibc 2.1.
139 These functions are non-standard GNU extensions;
140 hence the suffix "_np" (nonportable) in the names.