mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Jens Gustedt <Jens.Gustedt@inria.fr>
To: musl@lists.openwall.com
Subject: [PATCH 2/9] additions to src/time and some implied minor changes here and there
Date: Mon, 01 Sep 2014 00:46:05 +0200	[thread overview]
Message-ID: <e055ebf55ff719cd25cb1446a642c8c032475223.1409524413.git.Jens.Gustedt@inria.fr> (raw)
In-Reply-To: <cover.1409524413.git.Jens.Gustedt@inria.fr>

This refactors clock_gettime and adds two functions, thrd_sleep and
timespec_get.

The refactoring of clock_gettime introduces a function "  clock_gettime"
that has the same signature as clock_gettime, but that returns an
eventual error instead of returning -1 and setting errno. All places that
use clock_gettime functionality internally in musl do this
unconditionally, so there is no reason to mess around with errno.

As an additional fallout for kernels that don't implement the
clock_gettime syscall (yet!), the syscall that results in ENOSYS is
effected only once (instead, as previously, for every call) and a
fallback to gettimeofday is used.

The two newly introduced C11 functions have easy functional equivalences
in POSIX, but these have subtle differences in the handling of errors.

thrd_sleep forces concrete numerical values as error return

timespec_get has a call interface that is incompatible with POSIX because
it has a bogus coding for its clock constants, and also this clock
constants must be returned in case of success. For the moment we only
implement one single clock, TIME_UTC, and map this to
CLOCK_REALTIME. This is the clock that we later need to measure time for
the timedlock and timedwait.

Also, the error handling is different than the one for clock_gettime: the
possible reason for failure are returned and not set in errno.
---
 src/aio/aio_suspend.c      |    4 +++-
 src/network/res_mkquery.c  |    4 +++-
 src/network/res_msend.c    |    4 +++-
 src/time/__clock_gettime.c |   44 ++++++++++++++++++++++++++++++++++++++++++++
 src/time/clock_gettime.c   |   41 +++++++----------------------------------
 src/time/ftime.c           |    4 +++-
 src/time/gettimeofday.c    |    4 +++-
 src/time/thrd_sleep.c      |   26 ++++++++++++++++++++++++++
 src/time/timespec_get.c    |   12 ++++++++++++
 9 files changed, 104 insertions(+), 39 deletions(-)
 create mode 100644 src/time/__clock_gettime.c
 create mode 100644 src/time/thrd_sleep.c
 create mode 100644 src/time/timespec_get.c

diff --git a/src/aio/aio_suspend.c b/src/aio/aio_suspend.c
index 39a1d3a..1e3b7d2 100644
--- a/src/aio/aio_suspend.c
+++ b/src/aio/aio_suspend.c
@@ -2,6 +2,8 @@
 #include <errno.h>
 #include "pthread_impl.h"
 
+int __clock_gettime(clockid_t clk, struct timespec *ts);
+
 /* Due to the requirement that aio_suspend be async-signal-safe, we cannot
  * use any locks, wait queues, etc. that would make it more efficient. The
  * only obviously-correct algorithm is to generate a wakeup every time any
@@ -35,7 +37,7 @@ int aio_suspend(const struct aiocb *const cbs[], int cnt, const struct timespec
 		}
 
 		if (first && ts) {
-			clock_gettime(CLOCK_MONOTONIC, &at);
+			__clock_gettime(CLOCK_MONOTONIC, &at);
 			at.tv_sec += ts->tv_sec;
 			if ((at.tv_nsec += ts->tv_nsec) >= 1000000000) {
 				at.tv_nsec -= 1000000000;
diff --git a/src/network/res_mkquery.c b/src/network/res_mkquery.c
index ec4568a..8bbaf23 100644
--- a/src/network/res_mkquery.c
+++ b/src/network/res_mkquery.c
@@ -3,6 +3,8 @@
 #include <time.h>
 #include "libc.h"
 
+int __clock_gettime(clockid_t clk, struct timespec *ts);
+
 int __res_mkquery(int op, const char *dname, int class, int type,
 	const unsigned char *data, int datalen,
 	const unsigned char *newrr, unsigned char *buf, int buflen)
@@ -32,7 +34,7 @@ int __res_mkquery(int op, const char *dname, int class, int type,
 	q[i+3] = class;
 
 	/* Make a reasonably unpredictable id */
-	clock_gettime(CLOCK_REALTIME, &ts);
+	__clock_gettime(CLOCK_REALTIME, &ts);
 	id = ts.tv_nsec + ts.tv_nsec/65536UL & 0xffff;
 	q[0] = id/256;
 	q[1] = id;
diff --git a/src/network/res_msend.c b/src/network/res_msend.c
index 35f106d..eff968a 100644
--- a/src/network/res_msend.c
+++ b/src/network/res_msend.c
@@ -14,6 +14,8 @@
 #include "syscall.h"
 #include "lookup.h"
 
+int __clock_gettime(clockid_t clk, struct timespec *ts);
+
 static void cleanup(void *p)
 {
 	__syscall(SYS_close, (intptr_t)p);
@@ -22,7 +24,7 @@ static void cleanup(void *p)
 static unsigned long mtime()
 {
 	struct timespec ts;
-	clock_gettime(CLOCK_REALTIME, &ts);
+	__clock_gettime(CLOCK_REALTIME, &ts);
 	return (unsigned long)ts.tv_sec * 1000
 		+ ts.tv_nsec / 1000000;
 }
diff --git a/src/time/__clock_gettime.c b/src/time/__clock_gettime.c
new file mode 100644
index 0000000..adbd4e6
--- /dev/null
+++ b/src/time/__clock_gettime.c
@@ -0,0 +1,44 @@
+#include <time.h>
+#include <errno.h>
+#include <stdint.h>
+#include "syscall.h"
+#include "libc.h"
+#include "atomic.h"
+
+static int sc_clock_gettime(clockid_t clk, struct timespec *ts)
+{
+	return __syscall(SYS_clock_gettime, clk, ts);
+}
+
+static int sc_gettimeofday(clockid_t clk, struct timespec *ts)
+{
+	if (clk != CLOCK_REALTIME)
+		return EINVAL;
+	__syscall(SYS_gettimeofday, ts, 0);
+	ts->tv_nsec = (int)ts->tv_nsec * 1000;
+	return 0;
+}
+
+void *__vdsosym(const char *, const char *);
+
+int __clock_gettime(clockid_t clk, struct timespec *ts)
+{
+	static int (*cgt)(clockid_t, struct timespec *);
+	if (!cgt) {
+		int (*f)(clockid_t, struct timespec *) = 0;
+#ifdef VDSO_CGT_SYM
+		f = __vdsosym(VDSO_CGT_VER, VDSO_CGT_SYM);
+#endif
+		if (!f) {
+			int r = sc_clock_gettime(clk, ts);
+			if (r == -ENOSYS) {
+				f = sc_gettimeofday;
+			} else {
+				a_cas_p(&cgt, 0, sc_clock_gettime);
+				return r;
+			}
+		}
+		a_cas_p(&cgt, 0, f);
+	}
+	return cgt(clk, ts);
+}
diff --git a/src/time/clock_gettime.c b/src/time/clock_gettime.c
index 799251d..13ddb89 100644
--- a/src/time/clock_gettime.c
+++ b/src/time/clock_gettime.c
@@ -1,41 +1,14 @@
 #include <time.h>
 #include <errno.h>
-#include <stdint.h>
-#include "syscall.h"
-#include "libc.h"
-#include "atomic.h"
 
-static int sc_clock_gettime(clockid_t clk, struct timespec *ts)
+int __clock_gettime(clockid_t clk, struct timespec *ts);
+
+int clock_gettime(clockid_t clk, struct timespec *ts)
 {
-	int r = __syscall(SYS_clock_gettime, clk, ts);
-	if (!r) return r;
-	if (r == -ENOSYS) {
-		if (clk == CLOCK_REALTIME) {
-			__syscall(SYS_gettimeofday, ts, 0);
-			ts->tv_nsec = (int)ts->tv_nsec * 1000;
-			return 0;
-		}
-		r = -EINVAL;
-	}
+	int r = __clock_gettime(clk, ts);
+	/* In case of -1 lower levels already have already taken care
+	* of setting errno. */
+	if (r >= -1) return r;
 	errno = -r;
 	return -1;
 }
-
-void *__vdsosym(const char *, const char *);
-
-int __clock_gettime(clockid_t clk, struct timespec *ts)
-{
-#ifdef VDSO_CGT_SYM
-	static int (*cgt)(clockid_t, struct timespec *);
-	if (!cgt) {
-		void *f = __vdsosym(VDSO_CGT_VER, VDSO_CGT_SYM);
-		if (!f) f = (void *)sc_clock_gettime;
-		a_cas_p(&cgt, 0, f);
-	}
-	return cgt(clk, ts);
-#else
-	return sc_clock_gettime(clk, ts);
-#endif
-}
-
-weak_alias(__clock_gettime, clock_gettime);
diff --git a/src/time/ftime.c b/src/time/ftime.c
index a1734d0..00af1ca 100644
--- a/src/time/ftime.c
+++ b/src/time/ftime.c
@@ -1,10 +1,12 @@
 #include <sys/timeb.h>
 #include <time.h>
 
+int __clock_gettime(clockid_t clk, struct timespec *ts);
+
 int ftime(struct timeb *tp)
 {
 	struct timespec ts;
-	clock_gettime(CLOCK_REALTIME, &ts);
+	__clock_gettime(CLOCK_REALTIME, &ts);
 	tp->time = ts.tv_sec;
 	tp->millitm = ts.tv_nsec / 1000000;
 	tp->timezone = tp->dstflag = 0;
diff --git a/src/time/gettimeofday.c b/src/time/gettimeofday.c
index 691f8e9..375ec0e 100644
--- a/src/time/gettimeofday.c
+++ b/src/time/gettimeofday.c
@@ -2,11 +2,13 @@
 #include <sys/time.h>
 #include "syscall.h"
 
+int __clock_gettime(clockid_t clk, struct timespec *ts);
+
 int gettimeofday(struct timeval *restrict tv, void *restrict tz)
 {
 	struct timespec ts;
 	if (!tv) return 0;
-	clock_gettime(CLOCK_REALTIME, &ts);
+	__clock_gettime(CLOCK_REALTIME, &ts);
 	tv->tv_sec = ts.tv_sec;
 	tv->tv_usec = (int)ts.tv_nsec / 1000;
 	return 0;
diff --git a/src/time/thrd_sleep.c b/src/time/thrd_sleep.c
new file mode 100644
index 0000000..3dbfe47
--- /dev/null
+++ b/src/time/thrd_sleep.c
@@ -0,0 +1,26 @@
+#include <time.h>
+#include <errno.h>
+#include "syscall.h"
+#include "libc.h"
+#include "threads.h"
+
+/* Roughly __syscall already returns the right thing: 0 if all went
+   well or a negative error indication, otherwise. */
+int thrd_sleep(const struct timespec *req, struct timespec *rem)
+{
+	int ret = __syscall(SYS_nanosleep, req, rem);
+	switch (ret) {
+	case 0:
+		return 0;
+		/* error described by POSIX:                                    */
+		/* EINTR  The nanosleep() function was interrupted by a signal. */
+		/* The C11 wording is:                                          */
+		/* -1 if it has been interrupted by a signal                    */
+	case -EINTR:
+		return -1;
+		/* EINVAL: described by POSIX */
+		/* EFAULT: described for linux */
+	default:
+		return -2;
+	}
+}
diff --git a/src/time/timespec_get.c b/src/time/timespec_get.c
new file mode 100644
index 0000000..601c3cc
--- /dev/null
+++ b/src/time/timespec_get.c
@@ -0,0 +1,12 @@
+#include <time.h>
+
+int __clock_gettime(clockid_t clk, struct timespec *ts);
+
+/* There is no other implemented value than TIME_UTC, all other values
+   are considered erroneous. */
+int timespec_get(struct timespec * ts, int base)
+{
+	if (base != TIME_UTC) return 0;
+	int ret = __clock_gettime(CLOCK_REALTIME, ts);
+	return ret < 0 ? 0 : base;
+}
-- 
1.7.10.4



  parent reply	other threads:[~2014-08-31 22:46 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-08-31 22:45 [PATCH 0/9] C thread patch series, v. 8.6 and 9.7 Jens Gustedt
2014-08-31 22:45 ` [PATCH 1/9] interface additions for the C thread implementation Jens Gustedt
2014-09-07  0:21   ` Rich Felker
2014-09-07  9:13     ` Jens Gustedt
2014-09-07 10:05       ` Alexander Monakov
2014-09-07 11:16         ` Jens Gustedt
2014-09-07 11:31           ` Alexander Monakov
2014-09-07 11:32           ` Rich Felker
2014-09-07 14:45             ` Jens Gustedt
2014-09-07 15:16               ` Rich Felker
2014-09-07 16:51                 ` Jens Gustedt
2014-09-07 16:55                   ` Rich Felker
2014-09-07  1:19   ` Rich Felker
2014-08-31 22:46 ` Jens Gustedt [this message]
2014-09-06 17:44   ` [PATCH 2/9] additions to src/time and some implied minor changes here and there Rich Felker
2014-08-31 22:46 ` [PATCH 3/9] use weak symbols for the POSIX functions that will be used by C threads Jens Gustedt
2014-09-06 18:52   ` Rich Felker
2014-08-31 22:46 ` [PATCH 4/9] add the functions for tss_t and once_flag Jens Gustedt
2014-08-31 22:46 ` [PATCH 5/9] add the functions for mtx_t Jens Gustedt
2014-09-07  1:51   ` Rich Felker
2014-09-07  1:54   ` Rich Felker
2014-08-31 22:47 ` [PATCH 6/9] add the functions for cnd_t Jens Gustedt
2014-08-31 22:47 ` [PATCH 7/9] add the thrd_xxxxxx functions Jens Gustedt
2014-09-07 14:24   ` Rich Felker
2014-09-07 14:52     ` Jens Gustedt
2014-09-07 15:17       ` Rich Felker
2014-08-31 22:47 ` [PATCH 8/9] separate pthread_create and pthread_exit in two different TU Jens Gustedt
2014-08-31 22:48 ` [PATCH 9/9] Separate pthread_create and thrd_create Jens Gustedt

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=e055ebf55ff719cd25cb1446a642c8c032475223.1409524413.git.Jens.Gustedt@inria.fr \
    --to=jens.gustedt@inria.fr \
    --cc=musl@lists.openwall.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.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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 NNTP newsgroup(s).