diff mbox

[uclibc-ng-devel] pthread_getcpuclockid.c: fix clockid calculation

Message ID CAKo4hFJwBMq78cpWPwH+obzVkquVd3Ezr+ey410_bm85tca-+w@mail.gmail.com
State Accepted
Headers show

Commit Message

Sergey Korolev April 27, 2017, 6:21 a.m. UTC
Hi, Waldemar.

Should uClibc-ng support Linux kernels before 2.6.24?

On Thu, Apr 27, 2017 at 8:33 AM, Waldemar Brodkorb <wbx@uclibc-ng.org>
wrote:

> Hi Sergey,
> Sergey Korolev wrote,
>
> > Current uClibc-ng version incorrectly calculates clockid
> > in pthread_getcpuclockid (at least for modern kernels). The simplest test
> > program
> >
> > fails with
> >
> > clock_gettime: Invalid argument
> >
> > Tested on Linux 3.4 / MIPS built with GCC 5.4.0.
>
> Thanks for the test case.
>
> > Other implementations, for example musl, use a simple calculation
> > https://git.musl-libc.org/cgit/musl/tree/src/thread/
> pthread_getcpuclockid.c
> >
> > Looks strange, but the official glibc repository
> > https://sourceware.org/git/?p=glibc.git;a=blob_plain;f=nptl/
> > pthread_getcpuclockid.c;hb=HEAD
> > has the same implementation as in uClibc-ng.
>
> uClibc imported NPTL from GNU libc in 2005. There are still many
> similaraties.
>
> Will you propose a new patch? ( I have read your second mail)
>
> best regards
>  Waldemar
>
>

Comments

Waldemar Brodkorb April 27, 2017, 6:23 a.m. UTC | #1
Hi,
Sergey Korolev wrote,

> Hi, Waldemar.
> 
> Should uClibc-ng support Linux kernels before 2.6.24?

That would be good, some users still use something like 2.6.18 IIRC.

best regards
 Waldemar
diff mbox

Patch

From 50266570943a755e42b96e0158ef85e579d2fcf0 Mon Sep 17 00:00:00 2001
From: Sergey Korolev <s.korolev@ndmsystems.com>
Date: Tue, 25 Apr 2017 02:14:59 +0300
Subject: [PATCH] pthread_getcpuclockid.c: fix clockid calculation

According to newer linux kernel sources (since 2.6.24)
MAKE_THREAD_CPUCLOCK macro-like computations used to get clockid.
---
 .../unix/sysv/linux/pthread_getcpuclockid.c        | 22 ++++------------------
 1 file changed, 4 insertions(+), 18 deletions(-)

diff --git a/libpthread/nptl/sysdeps/unix/sysv/linux/pthread_getcpuclockid.c b/libpthread/nptl/sysdeps/unix/sysv/linux/pthread_getcpuclockid.c
index ca3570f..2fe9115 100644
--- a/libpthread/nptl/sysdeps/unix/sysv/linux/pthread_getcpuclockid.c
+++ b/libpthread/nptl/sysdeps/unix/sysv/linux/pthread_getcpuclockid.c
@@ -20,6 +20,8 @@ 
 #include <sys/time.h>
 #include <tls.h>
 
+#define CPUCLOCK_PERTHREAD_MASK	4
+#define CPUCLOCK_SCHED		2
 
 int
 pthread_getcpuclockid (
@@ -33,24 +35,8 @@  pthread_getcpuclockid (
     /* Not a valid thread handle.  */
     return ESRCH;
 
-#ifdef CLOCK_THREAD_CPUTIME_ID
-  /* We need to store the thread ID in the CLOCKID variable together
-     with a number identifying the clock.  We reserve the low 3 bits
-     for the clock ID and the rest for the thread ID.  This is
-     problematic if the thread ID is too large.  But 29 bits should be
-     fine.
-
-     If some day more clock IDs are needed the ID part can be
-     enlarged.  The IDs are entirely internal.  */
-  if (pd->tid >= 1 << (8 * sizeof (*clockid) - CLOCK_IDFIELD_SIZE))
-    return ERANGE;
-
-  /* Store the number.  */
-  *clockid = CLOCK_THREAD_CPUTIME_ID | (pd->tid << CLOCK_IDFIELD_SIZE);
+  *clockid = ((~(clockid_t) (pd->tid)) << CLOCK_IDFIELD_SIZE)
+    | CPUCLOCK_SCHED | CPUCLOCK_PERTHREAD_MASK;
 
   return 0;
-#else
-  /* We don't have a timer for that.  */
-  return ENOENT;
-#endif
 }
-- 
2.7.4