--- /dev/null
+! Copyright (C) 2022 Cat Stevens.
+! See http://factorcode.org/license.txt for BSD license.
+USING: alien.c-types alien.syntax kernel ;
+IN: unix.scheduler
+
+CONSTANT: SCHED_FIFO 1
+CONSTANT: SCHED_OTHER 2
+CONSTANT: SCHED_RR 3
+
+CONSTANT: MOST_IDLE_SCHED_POLICY 2
+
+FUNCTION: int sched_get_priority_min ( int policy )
+FUNCTION: int sched_get_priority_max ( int policy )
+
+: policy-priority-range ( policy -- high low )
+ [ sched_get_priority_max ] [ sched_get_priority_min ] bi ;
+
+: priority-allowed? ( policy -- ? )
+ SCHED_OTHER = not ;
--- /dev/null
+! Copyright (C) 2022 Cat Stevens.
+! See http://factorcode.org/license.txt for BSD license.
+USING: alien.c-types alien.syntax kernel sequences ;
+IN: unix.scheduler
+
+! Note: Feature "_POSIX_PRIORITY_SCHEDULING"
+! Scheduling policy values from <sched.h>
+CONSTANT: SCHED_OTHER 0
+CONSTANT: SCHED_FIFO 1
+CONSTANT: SCHED_RR 2
+
+! Note: Feature "__USE_GNU"
+CONSTANT: SCHED_BATCH 3
+CONSTANT: SCHED_ISO 4
+CONSTANT: SCHED_IDLE 5
+CONSTANT: SCHED_DEADLINE 6
+
+CONSTANT: SCHED_RESET_ON_FORK 0x40000000
+! end __USE_GNU
+
+CONSTANT: MOST_IDLE_SCHED_POLICY 5
+
+FUNCTION: int sched_get_priority_min ( int policy )
+FUNCTION: int sched_get_priority_max ( int policy )
+
+: policy-priority-range ( policy -- high low )
+ [ sched_get_priority_max ] [ sched_get_priority_min ] bi ;
+
+: priority-allowed? ( policy -- ? )
+ { SCHED_IDLE SCHED_OTHER SCHED_BATCH } member? not ;
--- /dev/null
+! Copyright (C) 2022 Cat Stevens.
+! See http://factorcode.org/license.txt for BSD license.
+USING: alien.c-types help.markup help.syntax kernel system ;
+IN: unix.scheduler
+
+ARTICLE: "unix.scheduler" "Unix Process Scheduling"
+"The " { $vocab-link "unix.scheduler" } "vocabulary provides an interface to the POSIX process scheduler. " { $link macosx } " does not implement POSIX Process Scheduling, but does have other similar low-level APIs exposed by this vocabulary."
+$nl "Cross platform constants:"
+{ $subsections MOST_IDLE_SCHED_POLICY }
+"Utility words:"
+{ $subsections policy-priority-range priority-allowed? } ;
+
+ABOUT: "unix.scheduler"
+
+HELP: MOST_IDLE_SCHED_POLICY
+{ $description
+ "The scheduling policy value which is, or is most like, the " { $snippet "SCHED_IDLE" } " policy on Linux. The value of this word is platform specific, and differs between " { $link linux } ", " { $link macosx } " and " { $link freebsd } "." }
+ $nl
+ { $snippet "sched(7)" } " describes " { $snippet "SCHED_IDLE" } " to be " { $emphasis "intended for running jobs at extremely low priority (lower even than a +19 nice value with the SCHED_OTHER or SCHED_BATCH policies)."
+} ;
+
+HELP: policy-priority-range
+{ $values { "policy" int } { "high" int } { "low" int } }
+{ $description
+ "Find the upper and lower bound on scheduler priority value, for a given scheduler policy. Each available scheduler policy ("
+ { $snippet "SCHED_OTHER" } ", " { $snippet "SCHED_FIFO" } ", etc) may have its own range of allowable priorities."
+}
+{ $examples
+ { $unchecked-example
+ "USING: formatting unix.scheduler ;"
+ "SCHED_OTHER policy-priority-range \"High: %d Low: %d\\n\" printf"
+ "High: 0 Low: 0"
+ }
+ { $unchecked-example
+ "USING: formatting unix.scheduler ;"
+ "SCHED_FIFO policy-priority-range \"High: %d Low: %d\\n\" printf"
+ "High: 99 Low: 1"
+ }
+} ;
+
+HELP: priority-allowed?
+{ $values { "policy" int } { "?" boolean } }
+{ $description
+ { $link t } " if the input scheduling policy can be used with a non-zero static priority, " { $link POSTPONE: f } " otherwise. This word allows a platform's real-time policies to be distinguished from normal scheduling policies."
+ $nl
+ "Depending on platform, normal scheduling policies (such as " { $snippet "SCHED_OTHER" } ", " { $snippet "SCHED_BATCH" } ", and " { $snippet "SCHED_IDLE" }
+ ") must be used with a static scheduling priority of " { $snippet "0" } ". Similarly, the real-time policies must be used with a non-zero priority, within the"
+ " range found by " { $link policy-priority-range } "."
+}
+{ $examples
+ { $unchecked-example
+ "USE: unix.scheduler"
+ "SCHED_OTHER priority-allowed? ."
+ "f"
+ }
+ { $unchecked-example
+ "USE: unix.scheduler"
+ "SCHED_FIFO priority-allowed? ."
+ "t"
+ }
+} ;