Skip to contentSkip to conversation

Discourage sched_yield(2)

PATCH New Alejandro Colomar 1 participant 4 messages May 3, 2023 Man-pages: manual pages for Linux -- Sections 2, 3, 4, 5, and 7

lore
Share & tools
lore
#0 lore raw

cover Discourage sched_yield(2)

Hi Andrew,

Here's a patch set for discouraging sched_yield(2). See the formatted page at the bottom. I also updated some POSIX historic detail.

Cheers,
Alex

Alejandro Colomar (3):
sched_yield.2: HISTORY: POSIX.1-2008 makes this non-optional sched_yield.2: NOTES: Remove misleading sentence sched_yield.2: Rename NOTES to CAVEATS, and reorder contents

 man2/sched_yield.2 | 41 +++++++++++++++++++----------------------
 1 file changed, 19 insertions(+), 22 deletions(-)

---

$ MANWIDTH=72 man ./man2/sched_yield.2 | cat
sched_yield(2)            System Calls Manual           sched_yield(2)

NAME
sched_yield - yield the processor

LIBRARY
Standard C library (libc, -lc)

SYNOPSIS

       #include <sched.h>

       int sched_yield(void);

DESCRIPTION
sched_yield() causes the calling thread to relinquish the CPU. The thread is moved to the end of the queue for its static pri‐ ority and a new thread gets to run.

RETURN VALUE
On success, sched_yield() returns 0. On error, -1 is returned, and errno is set to indicate the error.

ERRORS
In the Linux implementation, sched_yield() always succeeds.

STANDARDS

       POSIX.1‐2008.

HISTORY
POSIX.1‐2001 (but optional). POSIX.1‐2008.

Before POSIX.1‐2008, systems on which sched_yield() is avail‐ able defined _POSIX_PRIORITY_SCHEDULING in <unistd.h>.

CAVEATS
sched_yield() is intended for use with real‐time scheduling policies (i.e., SCHED_FIFO or SCHED_RR). Use of sched_yield() with nondeterministic scheduling policies such as SCHED_OTHER is unspecified and very likely means your application design is broken.

If the calling thread is the only thread in the highest prior‐ ity list at that time, it will continue to run after a call to sched_yield().

Avoid calling sched_yield() unnecessarily or inappropriately (e.g., when resources needed by other schedulable threads are still held by the caller), since doing so will result in unnec‐ essary context switches, which will degrade system performance.

SEE ALSO

       sched(7)

Linux man‐pages (unreleased) (date) sched_yield(2)

Signature · 1 line
2.40.1
#1 lore raw

PATCH 1/3 sched_yield.2: HISTORY: POSIX.1-2008 makes this non-optional

---
 man2/sched_yield.2 | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)
man2/sched_yield.2 +10 -8
index f1024762a..bab0f9569 100644
@@ -38,20 +38,22 @@ .SH ERRORS
 .SH STANDARDS
 POSIX.1-2008.
 .SH HISTORY
-POSIX.1-2001.
+POSIX.1-2001 (but optional).
+POSIX.1-2008.
+.PP
+Before POSIX.1-2008,
+systems on which
+.BR sched_yield ()
+is available defined
+.B _POSIX_PRIORITY_SCHEDULING
+in
+.IR <unistd.h> .
 .SH NOTES
 If the calling thread is the only thread in the highest
 priority list at that time,
 it will continue to run after a call to
 .BR sched_yield ().
 .PP
-POSIX systems on which
-.BR sched_yield ()
-is available define
-.B _POSIX_PRIORITY_SCHEDULING
-in
-.IR <unistd.h> .
-.PP
 Strategic calls to
 .BR sched_yield ()
 can improve performance by giving other threads or processes
Signature · 1 line
2.40.1
#2 lore raw

PATCH 2/3 sched_yield.2: NOTES: Remove misleading sentence

In reply to Alejandro Colomar

sched_yield(2) is not the right thing for heavily contended resources. The right thing to do is to call functions that wake the waiting threads.

---
 man2/sched_yield.2 | 5 -----
 1 file changed, 5 deletions(-)
man2/sched_yield.2 +0 -5
index bab0f9569..5e5b45a48 100644
@@ -54,11 +54,6 @@ .SH NOTES
 it will continue to run after a call to
 .BR sched_yield ().
 .PP
-Strategic calls to
-.BR sched_yield ()
-can improve performance by giving other threads or processes
-a chance to run when (heavily) contended resources (e.g., mutexes)
-have been released by the caller.
 Avoid calling
 .BR sched_yield ()
 unnecessarily or inappropriately
Signature · 1 line
2.40.1
#3 lore raw

PATCH 3/3 sched_yield.2: Rename NOTES to CAVEATS, and reorder contents

In reply to Alejandro Colomar

Put the last paragraph at the top of the CAVEATS section, since it's probably the most important for readers. This system call is likely not the right one for most programs; let's discourage its use.

---
 man2/sched_yield.2 | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
man2/sched_yield.2 +12 -12
index 5e5b45a48..4eb858018 100644
@@ -48,7 +48,18 @@ .SH HISTORY
 .B _POSIX_PRIORITY_SCHEDULING
 in
 .IR <unistd.h> .
-.SH NOTES
+.SH CAVEATS
+.BR sched_yield ()
+is intended for use with real-time scheduling policies (i.e.,
+.B SCHED_FIFO
+or
+.BR SCHED_RR ).
+Use of
+.BR sched_yield ()
+with nondeterministic scheduling policies such as
+.B SCHED_OTHER
+is unspecified and very likely means your application design is broken.
+.PP
 If the calling thread is the only thread in the highest
 priority list at that time,
 it will continue to run after a call to
@@ -61,16 +72,5 @@ .SH NOTES
 schedulable threads are still held by the caller),
 since doing so will result in unnecessary context switches,
 which will degrade system performance.
-.PP
-.BR sched_yield ()
-is intended for use with real-time scheduling policies (i.e.,
-.B SCHED_FIFO
-or
-.BR SCHED_RR ).
-Use of
-.BR sched_yield ()
-with nondeterministic scheduling policies such as
-.B SCHED_OTHER
-is unspecified and very likely means your application design is broken.
 .SH SEE ALSO
 .BR sched (7)
Signature · 1 line
2.40.1