From: Arnaud Charlet <charlet@adacore.com>
To: gcc-patches@gcc.gnu.org
Cc: "Douglas B. Rupp" <rupp@adacore.com>
Subject: [Ada] Implement RT_Resolution properly
Date: Wed, 07 Jan 2015 10:23:00 -0000 [thread overview]
Message-ID: <20150107102311.GA4891@adacore.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 1098 bytes --]
Previously RT_Resolution was returning a hard coded dummy value.
With this change we now use the relevant system call to compute the
clock resolution.
Tested on x86_64-pc-linux-gnu, committed on trunk
2015-01-07 Doug Rupp <rupp@adacore.com>
* s-osinte-mingw.ads (LARGE_INTEGR): New subtype.
(QueryPerformanceFrequency): New imported procedure.
* s-taprop-mingw.adb (RT_Resolution): Call above and return
resolution vice a hardcoded value.
* s-taprop-solaris.adb (RT_Resolution): Call clock_getres and return
resolution vice a hardcoded value.
* s-linux-android.ads (clockid_t): New subtype.
* s-osinte-aix.ads (clock_getres): New imported subprogram.
* s-osinte-android.ads (clock_getres): Likewise.
* s-osinte-freebsd.ads (clock_getres): Likewise.
* s-osinte-solaris-posix.ads (clock_getres): Likewise.
* s-osinte-darwin.ads (clock_getres): New subprogram.
* s-osinte-darwin.adb (clock_getres): New subprogram.
* thread.c (__gnat_clock_get_res) [__APPLE__]: New function.
* s-taprop-posix.adb (RT_Resolution): Call clock_getres to
calculate resolution vice hard coded value.
[-- Attachment #2: difs --]
[-- Type: text/plain, Size: 7429 bytes --]
Index: s-linux-android.ads
===================================================================
--- s-linux-android.ads (revision 219191)
+++ s-linux-android.ads (working copy)
@@ -47,6 +47,7 @@
subtype long is Interfaces.C.long;
subtype suseconds_t is Interfaces.C.long;
subtype time_t is Interfaces.C.long;
+ subtype clockid_t is Interfaces.C.int;
type timespec is record
tv_sec : time_t;
Index: s-osinte-aix.ads
===================================================================
--- s-osinte-aix.ads (revision 219191)
+++ s-osinte-aix.ads (working copy)
@@ -206,6 +206,11 @@
tp : access timespec) return int;
pragma Import (C, clock_gettime, "clock_gettime");
+ function clock_getres
+ (clock_id : clockid_t;
+ res : access timespec) return int;
+ pragma Import (C, clock_getres, "clock_getres");
+
function To_Duration (TS : timespec) return Duration;
pragma Inline (To_Duration);
Index: s-osinte-android.ads
===================================================================
--- s-osinte-android.ads (revision 219191)
+++ s-osinte-android.ads (working copy)
@@ -211,6 +211,11 @@
(clock_id : clockid_t;
tp : access timespec) return int;
+ function clock_getres
+ (clock_id : clockid_t;
+ res : access timespec) return int;
+ pragma Import (C, clock_getres, "clock_getres");
+
function To_Duration (TS : timespec) return Duration;
pragma Inline (To_Duration);
Index: s-osinte-darwin.adb
===================================================================
--- s-osinte-darwin.adb (revision 219191)
+++ s-osinte-darwin.adb (working copy)
@@ -129,6 +129,36 @@
return Result;
end clock_gettime;
+ ------------------
+ -- clock_getres --
+ ------------------
+
+ function clock_getres
+ (clock_id : clockid_t;
+ res : access timespec) return int
+ is
+ pragma Unreferenced (clock_id);
+
+ -- Darwin Threads don't have clock_getres.
+
+ Nano : constant := 10**9;
+ nsec : int := 0;
+ Result : int := -1;
+
+ function clock_get_res return int;
+ pragma Import (C, clock_get_res, "__gnat_clock_get_res");
+
+ begin
+ nsec := clock_get_res;
+ res.all := To_Timespec (Duration (0.0) + Duration (nsec) / Nano);
+
+ if nsec > 0 then
+ Result := 0;
+ end if;
+
+ return Result;
+ end clock_getres;
+
-----------------
-- sched_yield --
-----------------
Index: s-osinte-darwin.ads
===================================================================
--- s-osinte-darwin.ads (revision 219191)
+++ s-osinte-darwin.ads (working copy)
@@ -189,6 +189,10 @@
(clock_id : clockid_t;
tp : access timespec) return int;
+ function clock_getres
+ (clock_id : clockid_t;
+ res : access timespec) return int;
+
function To_Duration (TS : timespec) return Duration;
pragma Inline (To_Duration);
Index: s-osinte-freebsd.ads
===================================================================
--- s-osinte-freebsd.ads (revision 219191)
+++ s-osinte-freebsd.ads (working copy)
@@ -202,6 +202,11 @@
type clockid_t is new int;
+ function clock_getres
+ (clock_id : clockid_t;
+ res : access timespec) return int;
+ pragma Import (C, clock_getres, "clock_getres");
+
function clock_gettime
(clock_id : clockid_t;
tp : access timespec)
Index: s-osinte-mingw.ads
===================================================================
--- s-osinte-mingw.ads (revision 219191)
+++ s-osinte-mingw.ads (working copy)
@@ -53,6 +53,8 @@
subtype int is Interfaces.C.int;
subtype long is Interfaces.C.long;
+ subtype LARGE_INTEGER is System.Win32.LARGE_INTEGER;
+
-------------------
-- General Types --
-------------------
@@ -104,6 +106,18 @@
procedure kill (sig : Signal);
pragma Import (C, kill, "raise");
+ ------------
+ -- Clock --
+ ------------
+
+ procedure QueryPerformanceFrequency
+ (lpPerformanceFreq : access LARGE_INTEGER);
+ pragma Import
+ (Stdcall, QueryPerformanceFrequency, "QueryPerformanceFrequency");
+
+ -- According to the spec, on XP and later than function cannot fail,
+ -- so we ignore the return value and import it as a procedure.
+
-------------
-- Threads --
-------------
Index: s-osinte-solaris-posix.ads
===================================================================
--- s-osinte-solaris-posix.ads (revision 219191)
+++ s-osinte-solaris-posix.ads (working copy)
@@ -189,6 +189,11 @@
type clockid_t is new int;
+ function clock_getres
+ (clock_id : clockid_t;
+ res : access timespec) return int;
+ pragma Import (C, clock_getres, "clock_getres");
+
function clock_gettime
(clock_id : clockid_t;
tp : access timespec) return int;
Index: s-taprop-mingw.adb
===================================================================
--- s-taprop-mingw.adb (revision 219191)
+++ s-taprop-mingw.adb (working copy)
@@ -1076,8 +1076,10 @@
-------------------
function RT_Resolution return Duration is
+ Ticks_Per_Second : aliased LARGE_INTEGER;
begin
- return 0.000_001; -- 1 micro-second
+ QueryPerformanceFrequency (Ticks_Per_Second'Access);
+ return Duration (1.0 / Ticks_Per_Second);
end RT_Resolution;
----------------
Index: s-taprop-posix.adb
===================================================================
--- s-taprop-posix.adb (revision 219191)
+++ s-taprop-posix.adb (working copy)
@@ -743,8 +743,13 @@
-------------------
function RT_Resolution return Duration is
+ TS : aliased timespec;
+ Result : Interfaces.C.int;
begin
- return 10#1.0#E-6;
+ Result := clock_getres (OSC.CLOCK_REALTIME, TS'Unchecked_Access);
+ pragma Assert (Result = 0);
+
+ return To_Duration (TS);
end RT_Resolution;
------------
Index: s-taprop-solaris.adb
===================================================================
--- s-taprop-solaris.adb (revision 219191)
+++ s-taprop-solaris.adb (working copy)
@@ -785,8 +785,13 @@
-------------------
function RT_Resolution return Duration is
+ TS : aliased timespec;
+ Result : Interfaces.C.int;
begin
- return 10#1.0#E-6;
+ Result := clock_getres (OSC.CLOCK_REALTIME, TS'Unchecked_Access);
+ pragma Assert (Result = 0);
+
+ return To_Duration (TS);
end RT_Resolution;
-----------
Index: thread.c
===================================================================
--- thread.c (revision 219191)
+++ thread.c (working copy)
@@ -54,3 +54,35 @@
}
#endif
+
+#if defined (__APPLE__)
+#include <mach/mach.h>
+#include <mach/clock.h>
+#endif
+
+/* Return the clock ticks per nanosecond for Posix systems lacking the
+ Posix extension function clock_getres, or else 0 nsecs on error. */
+
+int
+__gnat_clock_get_res (void)
+{
+#if defined (__APPLE__)
+ clock_serv_t clock_port;
+ mach_msg_type_number_t count;
+ int nsecs;
+ int result;
+
+ count = 1;
+ result = host_get_clock_service
+ (mach_host_self (), SYSTEM_CLOCK, &clock_port);
+
+ if (result == KERN_SUCCESS)
+ result = clock_get_attributes (clock_port, CLOCK_GET_TIME_RES,
+ (clock_attr_t) &nsecs, &count);
+
+ if (result == KERN_SUCCESS)
+ return nsecs;
+#endif
+
+ return 0;
+}
reply other threads:[~2015-01-07 10:23 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=20150107102311.GA4891@adacore.com \
--to=charlet@adacore.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=rupp@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).