OSDN Git Service

Switch to upstream sleep(3) and usleep(3).
authorElliott Hughes <enh@google.com>
Thu, 21 Nov 2013 00:09:06 +0000 (16:09 -0800)
committerElliott Hughes <enh@google.com>
Thu, 21 Nov 2013 00:24:16 +0000 (16:24 -0800)
Also fix the signature of usleep, and the definition of useconds_t which
should be unsigned, as the 'u' in its name implies.

This patch also cleans up the existing FreeBSD hacks by moving the libm
stuff from <sys/cdefs.h> to a libm-private header, and adding comments
about the hacks we use to build FreeBSD source.

Change-Id: Ibe5067a380502df94a0a3a7901969b35411085b6

libc/Android.mk
libc/bionic/sleep.c [deleted file]
libc/bionic/usleep.c [deleted file]
libc/include/sys/cdefs.h
libc/include/sys/types.h
libc/include/unistd.h
libc/upstream-freebsd/freebsd-compat.h
libc/upstream-freebsd/lib/libc/gen/sleep.c [new file with mode: 0644]
libc/upstream-freebsd/lib/libc/gen/usleep.c [new file with mode: 0644]
libm/Android.mk
libm/freebsd-compat.h [new file with mode: 0644]

index 551c633..af96503 100644 (file)
@@ -120,7 +120,6 @@ libc_common_src_files := \
        bionic/siglist.c \
        bionic/signame.c \
        bionic/sigsetmask.c \
-       bionic/sleep.c \
        bionic/strndup.c \
        bionic/strntoimax.c \
        bionic/strntoumax.c \
@@ -132,7 +131,6 @@ libc_common_src_files := \
        bionic/time64.c \
        bionic/umount.c \
        bionic/unlockpt.c \
-       bionic/usleep.c \
        bionic/utmp.c \
        bionic/wcscoll.c \
 
@@ -310,6 +308,8 @@ libc_tzcode_src_files := \
     tzcode/strptime.c \
 
 libc_upstream_freebsd_src_files := \
+    upstream-freebsd/lib/libc/gen/sleep.c \
+    upstream-freebsd/lib/libc/gen/usleep.c \
     upstream-freebsd/lib/libc/stdio/clrerr.c \
     upstream-freebsd/lib/libc/stdio/fclose.c \
     upstream-freebsd/lib/libc/stdio/fdopen.c \
diff --git a/libc/bionic/sleep.c b/libc/bionic/sleep.c
deleted file mode 100644 (file)
index aafbd60..0000000
+++ /dev/null
@@ -1,52 +0,0 @@
-/*
- * Copyright (C) 2008 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 <time.h>
-#include <errno.h>
-
-unsigned int sleep(unsigned int seconds)
-{
-    struct timespec  t;
-
-   /* seconds is unsigned, while t.tv_sec is signed
-    * some people want to do sleep(UINT_MAX), so fake
-    * support for it by only sleeping 2 billion seconds
-    */
-    if ((int)seconds < 0)
-        seconds = 0x7fffffff;
-        
-    t.tv_sec  = seconds;
-    t.tv_nsec = 0;
-
-    if ( !nanosleep( &t, &t ) )
-    return 0;
-
-    if ( errno == EINTR )
-        return t.tv_sec;
-
-    return -1;
-}
diff --git a/libc/bionic/usleep.c b/libc/bionic/usleep.c
deleted file mode 100644 (file)
index 19e8ee8..0000000
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- * Copyright (C) 2008 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 <time.h>
-#include <errno.h>
-
-int usleep(unsigned long usec)
-{
-  struct timespec ts;
-
-  ts.tv_sec  = usec/1000000UL;
-
-#ifdef __arm__
-    /* avoid divisions and modulos on the ARM */
-  ts.tv_nsec = (usec - ts.tv_sec*1000000UL)*1000;
-#else
-  ts.tv_nsec = (usec % 1000000UL) * 1000UL;
-#endif
-
-  for (;;)
-  {
-    if ( nanosleep( &ts, &ts ) == 0 )
-        return 0;
-
-    // We try again if the nanosleep failure is EINTR.
-    // The other possible failures are EINVAL (which we should pass through),
-    // and ENOSYS, which doesn't happen.
-    if ( errno != EINTR )
-        return -1;
-  }
-}
index 175c28b..c7f2ac7 100644 (file)
 #endif
 #define __BIONIC_FORTIFY_UNKNOWN_SIZE ((size_t) -1)
 
-/* Android-added: for FreeBSD's libm. */
-#define __weak_reference(sym,alias) \
-    __asm__(".weak " #alias); \
-    __asm__(".equ "  #alias ", " #sym)
-#define __strong_reference(sym,aliassym) \
-    extern __typeof (sym) aliassym __attribute__ ((__alias__ (#sym)))
-
 #endif /* !_SYS_CDEFS_H_ */
index bb4a9d4..d793f81 100644 (file)
@@ -55,6 +55,7 @@ typedef __kernel_dev_t       dev_t;
 typedef __kernel_fsblkcnt_t  fsblkcnt_t;
 typedef __kernel_fsfilcnt_t  fsfilcnt_t;
 typedef __kernel_gid32_t     gid_t;
+typedef __kernel_uid32_t     uid_t;
 typedef __kernel_id_t        id_t;
 typedef __kernel_ino_t       ino_t;
 typedef __kernel_key_t       key_t;
@@ -94,10 +95,10 @@ typedef  .... pthread_t;
 typedef __kernel_ssize_t ssize_t;
 #endif
 
-typedef __kernel_suseconds_t  suseconds_t;
-typedef __kernel_time_t       time_t;
-typedef __kernel_uid32_t        uid_t;
-typedef signed long           useconds_t;
+typedef __kernel_time_t time_t;
+
+typedef __kernel_suseconds_t suseconds_t;
+typedef unsigned long useconds_t;
 
 typedef __kernel_daddr_t       daddr_t;
 typedef __kernel_timer_t       timer_t;
index 91b0d8d..29758f5 100644 (file)
@@ -159,7 +159,7 @@ extern int ftruncate64(int, off64_t);
 extern int pause(void);
 extern unsigned int alarm(unsigned int);
 extern unsigned int sleep(unsigned int);
-extern int usleep(unsigned long);
+extern int usleep(useconds_t);
 
 extern int gethostname(char *, size_t);
 
index 8030eea..74dc679 100644 (file)
 #define __USE_BSD
 #define REPLACE_GETOPT
 
+/*
+ * FreeBSD's libc has three symbols for every symbol:
+ *
+ *     __f will be the actual implementation.
+ *     _f will be a weak reference to __f (used for calls to f from within the library).
+ *     f will be a weak reference to __f (used for calls to f from outside the library).
+ *
+ * We collapse this into just the one symbol, f.
+ */
+
+/* Prevent weak reference generation. */
+#define __weak_reference(sym,alias)
+
+/* Ensure that the implementation itself gets the underscore-free name. */
+#define __sleep sleep
+#define __usleep usleep
+
+/* Redirect internal C library calls to the public function. */
 #define _close close
 #define _fcntl fcntl
 #define _fstat fstat
+#define _nanosleep nanosleep
 #define _open open
 
-#define _sseek __sseek /* Needed as long as we have a mix of OpenBSD and FreeBSD stdio. */
+/* This one is only needed as long as we have a mix of OpenBSD and FreeBSD stdio. */
+#define _sseek __sseek
 
 #endif
diff --git a/libc/upstream-freebsd/lib/libc/gen/sleep.c b/libc/upstream-freebsd/lib/libc/gen/sleep.c
new file mode 100644 (file)
index 0000000..b807c2d
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * 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
new file mode 100644 (file)
index 0000000..7d6559b
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * 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);
index 1d87dc2..39e5563 100644 (file)
@@ -217,7 +217,11 @@ libm_common_src_files += fake_long_double.c
 
 # TODO: re-enable i387/e_sqrtf.S for x86, and maybe others.
 
-libm_common_cflags := -DFLT_EVAL_METHOD=0 -std=c99
+libm_common_cflags := \
+    -DFLT_EVAL_METHOD=0 \
+    -std=c99 \
+    -include $(LOCAL_PATH)/freebsd-compat.h \
+
 libm_common_includes := $(LOCAL_PATH)/upstream-freebsd/lib/msun/src/
 
 libm_arm_includes := $(LOCAL_PATH)/arm
diff --git a/libm/freebsd-compat.h b/libm/freebsd-compat.h
new file mode 100644 (file)
index 0000000..ca5b42c
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _BIONIC_LIBM_FREEBSD_COMPAT_H_included
+#define _BIONIC_LIBM_FREEBSD_COMPAT_H_included
+
+#define __weak_reference(sym,alias) \
+    __asm__(".weak " #alias); \
+    __asm__(".equ "  #alias ", " #sym)
+
+#define __strong_reference(sym,aliassym) \
+    extern __typeof (sym) aliassym __attribute__ ((__alias__ (#sym)))
+
+#endif