Debian Codegraph @

upstream/debian-trixie/man3/pthread_rwlockattr_setkind_np.3

raw
1 .\"Copyright (c) 2010 Novell Inc., written by Robert Schweikert
2 .\"
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
4 .\"
5 .TH pthread_rwlockattr_setkind_np 3 2024-05-02 "Linux man-pages 6.9.1"
6 .SH NAME
7 pthread_rwlockattr_setkind_np, pthread_rwlockattr_getkind_np \- set/get
8 the read-write lock kind of the thread read-write lock attribute object
9 .SH LIBRARY
10 POSIX threads library
11 .RI ( libpthread ", " \-lpthread )
12 .SH SYNOPSIS
13 .nf
14 .B #include <pthread.h>
15 .P
16 .BI "int pthread_rwlockattr_setkind_np(pthread_rwlockattr_t *" attr ,
17 .BI " int " pref );
18 .B int pthread_rwlockattr_getkind_np(
19 .BI " const pthread_rwlockattr_t *restrict " attr ,
20 .BI " int *restrict " pref );
21 .P
22 .fi
23 .RS -4
24 Feature Test Macro Requirements for glibc (see
25 .BR feature_test_macros (7)):
26 .RE
27 .P
28 .BR pthread_rwlockattr_setkind_np (),
29 .BR pthread_rwlockattr_getkind_np ():
30 .nf
31 _XOPEN_SOURCE >= 500 || _POSIX_C_SOURCE >= 200809L
32 .fi
33 .SH DESCRIPTION
34 The
35 .BR pthread_rwlockattr_setkind_np ()
36 function sets the "lock kind" attribute of the
37 read-write lock attribute object referred to by
38 .I attr
39 to the value specified in
40 .IR pref .
41 The argument
42 .I pref
43 may be set to one of the following:
44 .TP
45 .B PTHREAD_RWLOCK_PREFER_READER_NP
46 This is the default.
47 A thread may hold multiple read locks; that is, read locks are recursive.
48 According to The Single Unix Specification, the behavior is unspecified when a
49 reader tries to place a lock, and there is no write lock but writers are
50 waiting.
51 Giving preference to the reader, as is set by
52 .BR PTHREAD_RWLOCK_PREFER_READER_NP ,
53 implies that the reader will receive the requested lock, even if
54 a writer is waiting.
55 As long as there are readers, the writer will be
56 starved.
57 .TP
58 .B PTHREAD_RWLOCK_PREFER_WRITER_NP
59 This is intended as the write lock analog of
60 .BR PTHREAD_RWLOCK_PREFER_READER_NP .
61 This is ignored by glibc because the POSIX requirement to support
62 recursive read locks would cause this option to create trivial
63 deadlocks; instead use
64 .B PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP
65 which ensures the application developer will not take recursive
66 read locks thus avoiding deadlocks.
67 .\" ---
68 .\" Here is the relevant wording:
69 .\"
70 .\" A thread may hold multiple concurrent read locks on rwlock (that is,
71 .\" successfully call the pthread_rwlock_rdlock() function n times). If
72 .\" so, the thread must perform matching unlocks (that is, it must call
73 .\" the pthread_rwlock_unlock() function n times).
74 .\"
75 .\" By making write-priority work correctly, I broke the above requirement,
76 .\" because I had no clue that recursive read locks are permissible.
77 .\"
78 .\" If a thread which holds a read lock tries to acquire another read lock,
79 .\" and now one or more writers is waiting for a write lock, then the algorithm
80 .\" will lead to an obvious deadlock. The reader will be suspended, waiting for
81 .\" the writers to acquire and release the lock, and the writers will be
82 .\" suspended waiting for every existing read lock to be released.
83 .\" ---
84 .\" https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_rwlock_rdlock.html
85 .\" https://sourceware.org/legacy-ml/libc-alpha/2000-01/msg00055.html
86 .\" https://sourceware.org/bugzilla/show_bug.cgi?id=7057
87 .TP
88 .B PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP
89 Setting the lock kind to this
90 avoids writer starvation as long as any read locking is not done in a
91 recursive fashion.
92 .P
93 The
94 .BR pthread_rwlockattr_getkind_np ()
95 function returns the value of the lock kind attribute of the
96 read-write lock attribute object referred to by
97 .I attr
98 in the pointer
99 .IR pref .
100 .SH RETURN VALUE
101 On success, these functions return 0.
102 Given valid pointer arguments,
103 .BR pthread_rwlockattr_getkind_np ()
104 always succeeds.
105 On error,
106 .BR pthread_rwlockattr_setkind_np ()
107 returns a nonzero error number.
108 .SH ERRORS
109 .TP
110 .B EINVAL
111 .I pref
112 specifies an unsupported value.
113 .SH STANDARDS
114 GNU;
115 hence the suffix "_np" (nonportable) in the names.
116 .SH HISTORY
117 glibc 2.1.
118 .SH SEE ALSO
119 .BR pthreads (7)