sleep/usleep: switch to trivial implementations.
Upstream keeps rearranging the deckchairs for these, so let's just
switch to the [roughly] one-liners rather than track that...
Test: treehugger
Change-Id: If655cf7a7f316657de44d41fadd43a8c55ee6f23
diff --git a/libc/Android.bp b/libc/Android.bp
index 3e2ee1d..8eefe10 100644
--- a/libc/Android.bp
+++ b/libc/Android.bp
@@ -271,8 +271,6 @@
defaults: ["libc_defaults"],
srcs: [
"upstream-freebsd/lib/libc/gen/ldexp.c",
- "upstream-freebsd/lib/libc/gen/sleep.c",
- "upstream-freebsd/lib/libc/gen/usleep.c",
"upstream-freebsd/lib/libc/stdlib/getopt_long.c",
"upstream-freebsd/lib/libc/stdlib/hcreate.c",
"upstream-freebsd/lib/libc/stdlib/hcreate_r.c",
@@ -1178,6 +1176,7 @@
"bionic/sigaction.cpp",
"bionic/signal.cpp",
"bionic/sigprocmask.cpp",
+ "bionic/sleep.cpp",
"bionic/spawn.cpp",
"bionic/stat.cpp",
"bionic/stdlib_l.cpp",
@@ -1212,6 +1211,7 @@
"bionic/tmpfile.cpp",
"bionic/umount.cpp",
"bionic/unlink.cpp",
+ "bionic/usleep.cpp",
"bionic/wait.cpp",
"bionic/wchar.cpp",
"bionic/wchar_l.cpp",
diff --git a/libc/bionic/sleep.cpp b/libc/bionic/sleep.cpp
new file mode 100644
index 0000000..fcfd9b2
--- /dev/null
+++ b/libc/bionic/sleep.cpp
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <unistd.h>
+
+#include <time.h>
+
+unsigned sleep(unsigned s) {
+#if !defined(__LP64__)
+ // `s` is `unsigned`, but tv_sec is `int` on LP32.
+ if (s > INT_MAX) return s - INT_MAX + sleep(INT_MAX);
+#endif
+
+ timespec ts = {.tv_sec = static_cast<time_t>(s)};
+ return (nanosleep(&ts, &ts) == -1) ? ts.tv_sec : 0;
+}
diff --git a/libc/bionic/usleep.cpp b/libc/bionic/usleep.cpp
new file mode 100644
index 0000000..2204684
--- /dev/null
+++ b/libc/bionic/usleep.cpp
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <unistd.h>
+
+#include <time.h>
+
+int usleep(useconds_t us) {
+ timespec ts;
+ ts.tv_sec = us / 1000000;
+ ts.tv_nsec = (us % 1000000) * 1000;
+ return nanosleep(&ts, nullptr);
+}
diff --git a/libc/upstream-freebsd/lib/libc/gen/sleep.c b/libc/upstream-freebsd/lib/libc/gen/sleep.c
deleted file mode 100644
index b807c2d..0000000
--- a/libc/upstream-freebsd/lib/libc/gen/sleep.c
+++ /dev/null
@@ -1,67 +0,0 @@
-/*
- * Copyright (c) 1989, 1993
- * The Regents of the University of California. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 4. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#if defined(LIBC_SCCS) && !defined(lint)
-static char sccsid[] = "@(#)sleep.c 8.1 (Berkeley) 6/4/93";
-#endif /* LIBC_SCCS and not lint */
-#include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
-
-#include "namespace.h"
-#include <errno.h>
-#include <limits.h>
-#include <time.h>
-#include <unistd.h>
-#include "un-namespace.h"
-
-unsigned int
-__sleep(unsigned int seconds)
-{
- struct timespec time_to_sleep;
- struct timespec time_remaining;
-
- /*
- * Avoid overflow when `seconds' is huge. This assumes that
- * the maximum value for a time_t is >= INT_MAX.
- */
- if (seconds > INT_MAX)
- return (seconds - INT_MAX + __sleep(INT_MAX));
-
- time_to_sleep.tv_sec = seconds;
- time_to_sleep.tv_nsec = 0;
- if (_nanosleep(&time_to_sleep, &time_remaining) != -1)
- return (0);
- if (errno != EINTR)
- return (seconds); /* best guess */
- return (time_remaining.tv_sec +
- (time_remaining.tv_nsec != 0)); /* round up */
-}
-
-__weak_reference(__sleep, sleep);
-__weak_reference(__sleep, _sleep);
diff --git a/libc/upstream-freebsd/lib/libc/gen/usleep.c b/libc/upstream-freebsd/lib/libc/gen/usleep.c
deleted file mode 100644
index 7d6559b..0000000
--- a/libc/upstream-freebsd/lib/libc/gen/usleep.c
+++ /dev/null
@@ -1,52 +0,0 @@
-/*
- * Copyright (c) 1989, 1993
- * The Regents of the University of California. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 4. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#if defined(LIBC_SCCS) && !defined(lint)
-static char sccsid[] = "@(#)usleep.c 8.1 (Berkeley) 6/4/93";
-#endif /* LIBC_SCCS and not lint */
-#include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
-
-#include "namespace.h"
-#include <time.h>
-#include <unistd.h>
-#include "un-namespace.h"
-
-int
-__usleep(useconds_t useconds)
-{
- struct timespec time_to_sleep;
-
- time_to_sleep.tv_nsec = (useconds % 1000000) * 1000;
- time_to_sleep.tv_sec = useconds / 1000000;
- return (_nanosleep(&time_to_sleep, NULL));
-}
-
-__weak_reference(__usleep, usleep);
-__weak_reference(__usleep, _usleep);
diff --git a/tests/signal_test.cpp b/tests/signal_test.cpp
index 1ae174a..6db7751 100644
--- a/tests/signal_test.cpp
+++ b/tests/signal_test.cpp
@@ -20,6 +20,7 @@
#include <sys/types.h>
#include <unistd.h>
+#include <chrono>
#include <thread>
#include <android-base/macros.h>
@@ -27,6 +28,8 @@
#include "SignalUtils.h"
+using namespace std::chrono_literals;
+
static int SIGNAL_MIN() {
return 1; // Signals start at 1 (SIGHUP), not 0.
}
@@ -753,12 +756,6 @@
ASSERT_EQ(0, errno);
}
-static int64_t NanoTime() {
- timespec t;
- clock_gettime(CLOCK_MONOTONIC, &t);
- return static_cast<int64_t>(t.tv_sec) * 1000000000LL + t.tv_nsec;
-}
-
TEST(signal, sigtimedwait_timeout) {
// Block SIGALRM.
sigset_t just_SIGALRM;
@@ -768,13 +765,14 @@
ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, &original_set));
// Wait timeout.
- int64_t start_time = NanoTime();
+ auto t0 = std::chrono::steady_clock::now();
siginfo_t info;
timespec timeout = { .tv_sec = 0, .tv_nsec = 1000000 };
errno = 0;
ASSERT_EQ(-1, sigtimedwait(&just_SIGALRM, &info, &timeout));
ASSERT_EQ(EAGAIN, errno);
- ASSERT_GE(NanoTime() - start_time, 1000000);
+ auto t1 = std::chrono::steady_clock::now();
+ ASSERT_GE(t1-t0, 1000000ns);
ASSERT_EQ(0, sigprocmask(SIG_SETMASK, &original_set, nullptr));
}
diff --git a/tests/system_properties_test2.cpp b/tests/system_properties_test2.cpp
index c061faa..b9936dd 100644
--- a/tests/system_properties_test2.cpp
+++ b/tests/system_properties_test2.cpp
@@ -19,16 +19,16 @@
#include <errno.h>
#include <sys/wait.h>
#include <unistd.h>
+
+#include <chrono>
#include <sstream>
#include <string>
#if defined(__BIONIC__)
#include <sys/system_properties.h>
-
-static uint64_t NanoTime() {
- timespec now;
- clock_gettime(CLOCK_MONOTONIC, &now);
- return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000000) + now.tv_nsec;
+int64_t NanoTime() {
+ auto t = std::chrono::time_point_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now());
+ return t.time_since_epoch().count();
}
#endif
diff --git a/tests/time_test.cpp b/tests/time_test.cpp
index 4e17242..378b4ac 100644
--- a/tests/time_test.cpp
+++ b/tests/time_test.cpp
@@ -24,13 +24,17 @@
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
+
#include <atomic>
+#include <chrono>
#include "SignalUtils.h"
#include "utils.h"
#include "private/bionic_constants.h"
+using namespace std::chrono_literals;
+
TEST(time, time) {
// Acquire time
time_t p1, t1 = time(&p1);
@@ -797,7 +801,7 @@
ASSERT_EQ(EINVAL, errno);
}
-TEST(time, clock_nanosleep) {
+TEST(time, clock_nanosleep_EINVAL) {
timespec in;
timespec out;
ASSERT_EQ(EINVAL, clock_nanosleep(-1, 0, &in, &out));
@@ -810,6 +814,29 @@
ASSERT_EQ(EINVAL, clock_nanosleep(CLOCK_THREAD_CPUTIME_ID, 0, &in, nullptr));
}
+TEST(time, clock_nanosleep) {
+ auto t0 = std::chrono::steady_clock::now();
+ const timespec ts = {.tv_nsec = 5000000};
+ ASSERT_EQ(0, clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, nullptr));
+ auto t1 = std::chrono::steady_clock::now();
+ ASSERT_GE(t1-t0, 5000000ns);
+}
+
+TEST(time, nanosleep) {
+ auto t0 = std::chrono::steady_clock::now();
+ const timespec ts = {.tv_nsec = 5000000};
+ ASSERT_EQ(0, nanosleep(&ts, nullptr));
+ auto t1 = std::chrono::steady_clock::now();
+ ASSERT_GE(t1-t0, 5000000ns);
+}
+
+TEST(time, nanosleep_EINVAL) {
+ timespec ts = {.tv_sec = -1};
+ errno = 0;
+ ASSERT_EQ(-1, nanosleep(&ts, nullptr));
+ ASSERT_EQ(EINVAL, errno);
+}
+
TEST(time, bug_31938693) {
// User-visible symptoms in N:
// http://b/31938693
diff --git a/tests/unistd_test.cpp b/tests/unistd_test.cpp
index 10c1710..99d92e6 100644
--- a/tests/unistd_test.cpp
+++ b/tests/unistd_test.cpp
@@ -33,6 +33,8 @@
#include <sys/wait.h>
#include <unistd.h>
+#include <chrono>
+
#include <android-base/file.h>
#include <android-base/strings.h>
@@ -46,6 +48,8 @@
#define UNISTD_DEATHTEST unistd_DeathTest
#endif
+using namespace std::chrono_literals;
+
static void* get_brk() {
return sbrk(0);
}
@@ -1516,3 +1520,17 @@
swab("hello", buf, -1);
ASSERT_EQ('x', buf[0]);
}
+
+TEST(UNISTD_TEST, usleep) {
+ auto t0 = std::chrono::steady_clock::now();
+ ASSERT_EQ(0, usleep(5000));
+ auto t1 = std::chrono::steady_clock::now();
+ ASSERT_GE(t1-t0, 5000us);
+}
+
+TEST(UNISTD_TEST, sleep) {
+ auto t0 = std::chrono::steady_clock::now();
+ ASSERT_EQ(0U, sleep(1));
+ auto t1 = std::chrono::steady_clock::now();
+ ASSERT_GE(t1-t0, 1s);
+}
diff --git a/tests/utils.h b/tests/utils.h
index fe41019..1f4cece 100644
--- a/tests/utils.h
+++ b/tests/utils.h
@@ -14,8 +14,7 @@
* limitations under the License.
*/
-#ifndef __TEST_UTILS_H
-#define __TEST_UTILS_H
+#pragma once
#include <dlfcn.h>
#include <fcntl.h>
@@ -254,5 +253,3 @@
std::string output_;
};
#endif
-
-#endif