From: "Marc Poulhiès" <poulhies@adacore.com>
To: gcc-patches@gcc.gnu.org
Cc: Johannes Kliemann <kliemann@adacore.com>
Subject: [COMMITTED] ada: Support setting task affinity on QNX
Date: Tue, 5 Sep 2023 13:08:08 +0200 [thread overview]
Message-ID: <20230905110808.562636-1-poulhies@adacore.com> (raw)
From: Johannes Kliemann <kliemann@adacore.com>
QNX does not support setting the thread affinity via a POSIX API.
This implementation uses QNX's native Thread_Ctl API to set the
thread affinity for Ada tasks.
gcc/ada/
* libgnarl/s-taprop__qnx.adb: Implement Set_Task_Affinity.
Tested on x86_64-pc-linux-gnu, committed on master.
---
gcc/ada/libgnarl/s-taprop__qnx.adb | 45 ++++++++++++++++++++++++++----
1 file changed, 40 insertions(+), 5 deletions(-)
diff --git a/gcc/ada/libgnarl/s-taprop__qnx.adb b/gcc/ada/libgnarl/s-taprop__qnx.adb
index 13335ef4acd..423229854a8 100644
--- a/gcc/ada/libgnarl/s-taprop__qnx.adb
+++ b/gcc/ada/libgnarl/s-taprop__qnx.adb
@@ -49,6 +49,7 @@ with System.Interrupt_Management;
with System.OS_Constants;
with System.OS_Primitives;
with System.Task_Info;
+with System.Multiprocessors;
with System.Soft_Links;
-- We use System.Soft_Links instead of System.Tasking.Initialization
@@ -1317,12 +1318,46 @@ package body System.Task_Primitives.Operations is
-----------------------
procedure Set_Task_Affinity (T : ST.Task_Id) is
- pragma Unreferenced (T);
-
+ use type Multiprocessors.CPU_Range;
+
+ function Thread_Ctl_Ext
+ (Pid : pid_t;
+ Tid : Thread_Id;
+ Command : Interfaces.C.unsigned;
+ Runmask : Interfaces.C.size_t) return Interfaces.C.int
+ with
+ Import, Convention => C, External_Name => "ThreadCtlExt";
+ -- Thread_Ctl_Ext is a generic thread control function in QNX.
+ -- It is defined locally because in the C API its second
+ -- argument is a void pointer that takes different actual
+ -- pointer types or values depending on the command. This
+ -- particular instance of this function only accepts the
+ -- NTO_TCTL_RUNMASK command. The void * pointer in the C
+ -- interface is interpreted as bitmask for this command.
+ -- In the binding size_t is used as an integer type that
+ -- always has the same size as a pointer.
+
+ NTO_TCTL_RUNMASK : constant := 4;
+ -- Command for Thread_Ctl. Using this command in Thread_Ctl
+ -- allows the caller to pass a bitmask that describes on
+ -- which CPU the current thread is allowed to run on.
+
+ Pid : constant pid_t := getpid;
+ Result : Interfaces.C.int;
+ Runmask : Interfaces.C.size_t;
+ -- Each set bit in runmask represents a processor that the thread
+ -- can run on. If all bits are set to one the thread can run on any CPU.
begin
- -- Setting task affinity is not supported by the underlying system
-
- null;
+ if T.Common.Base_CPU = Multiprocessors.Not_A_Specific_CPU then
+ Runmask := Interfaces.C.size_t'Last;
+ else
+ Runmask :=
+ Interfaces.C.size_t
+ (2 ** Natural (T.Common.Base_CPU - Multiprocessors.CPU'First));
+ end if;
+ Result :=
+ Thread_Ctl_Ext (Pid, Get_Thread_Id (T), NTO_TCTL_RUNMASK, Runmask);
+ pragma Assert (Result = 0);
end Set_Task_Affinity;
end System.Task_Primitives.Operations;
--
2.40.0
reply other threads:[~2023-09-05 11:08 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230905110808.562636-1-poulhies@adacore.com \
--to=poulhies@adacore.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=kliemann@adacore.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).