Add clock_settime and clock_nanosleep.

Add the missing prototypes, fix the existing prototypes to use clockid_t
rather than int, fix clock_nanosleep's failure behavior, and add simple
tests.

Bug: 17644443
Bug: https://code.google.com/p/android/issues/detail?id=77372
Change-Id: I03fba369939403918abcabae9551a7123953d780
Signed-off-by: Haruki Hasegawa <h6a.h4i.0@gmail.com>
This commit is contained in:
Haruki Hasegawa 2014-10-13 00:50:47 +09:00 committed by Elliott Hughes
parent c229705051
commit 1816025684
11 changed files with 79 additions and 22 deletions

View File

@ -98,6 +98,7 @@ libc_bionic_src_files := \
bionic/chown.cpp \ bionic/chown.cpp \
bionic/clearenv.cpp \ bionic/clearenv.cpp \
bionic/clock.cpp \ bionic/clock.cpp \
bionic/clock_nanosleep.cpp \
bionic/clone.cpp \ bionic/clone.cpp \
bionic/__cmsg_nxthdr.cpp \ bionic/__cmsg_nxthdr.cpp \
bionic/connect.cpp \ bionic/connect.cpp \

View File

@ -202,9 +202,9 @@ int swapoff(const char*) all
int settimeofday(const struct timeval*, const struct timezone*) all int settimeofday(const struct timeval*, const struct timezone*) all
clock_t times(struct tms*) all clock_t times(struct tms*) all
int nanosleep(const struct timespec*, struct timespec*) all int nanosleep(const struct timespec*, struct timespec*) all
int clock_settime(clockid_t clk_id, const struct timespec* tp) all int clock_settime(clockid_t, const struct timespec*) all
int clock_getres(clockid_t clk_id, struct timespec* res) all int clock_getres(clockid_t, struct timespec*) all
int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec* req, struct timespec* rem) all int __clock_nanosleep:clock_nanosleep(clockid_t, int, const struct timespec*, struct timespec*) all
int getitimer(int, const struct itimerval*) all int getitimer(int, const struct itimerval*) all
int setitimer(int, const struct itimerval*, struct itimerval*) all int setitimer(int, const struct itimerval*, struct itimerval*) all
int __timer_create:timer_create(clockid_t clockid, struct sigevent* evp, __kernel_timer_t* timerid) all int __timer_create:timer_create(clockid_t clockid, struct sigevent* evp, __kernel_timer_t* timerid) all

View File

@ -2,7 +2,7 @@
#include <private/bionic_asm.h> #include <private/bionic_asm.h>
ENTRY(clock_nanosleep) ENTRY(__clock_nanosleep)
mov ip, r7 mov ip, r7
ldr r7, =__NR_clock_nanosleep ldr r7, =__NR_clock_nanosleep
swi #0 swi #0
@ -11,4 +11,4 @@ ENTRY(clock_nanosleep)
bxls lr bxls lr
neg r0, r0 neg r0, r0
b __set_errno_internal b __set_errno_internal
END(clock_nanosleep) END(__clock_nanosleep)

View File

@ -2,7 +2,7 @@
#include <private/bionic_asm.h> #include <private/bionic_asm.h>
ENTRY(clock_nanosleep) ENTRY(__clock_nanosleep)
mov x8, __NR_clock_nanosleep mov x8, __NR_clock_nanosleep
svc #0 svc #0
@ -11,4 +11,5 @@ ENTRY(clock_nanosleep)
b.hi __set_errno_internal b.hi __set_errno_internal
ret ret
END(clock_nanosleep) END(__clock_nanosleep)
.hidden __clock_nanosleep

View File

@ -2,7 +2,7 @@
#include <private/bionic_asm.h> #include <private/bionic_asm.h>
ENTRY(clock_nanosleep) ENTRY(__clock_nanosleep)
.set noreorder .set noreorder
.cpload t9 .cpload t9
li v0, __NR_clock_nanosleep li v0, __NR_clock_nanosleep
@ -16,4 +16,4 @@ ENTRY(clock_nanosleep)
j t9 j t9
nop nop
.set reorder .set reorder
END(clock_nanosleep) END(__clock_nanosleep)

View File

@ -2,7 +2,7 @@
#include <private/bionic_asm.h> #include <private/bionic_asm.h>
ENTRY(clock_nanosleep) ENTRY(__clock_nanosleep)
.set push .set push
.set noreorder .set noreorder
li v0, __NR_clock_nanosleep li v0, __NR_clock_nanosleep
@ -22,4 +22,5 @@ ENTRY(clock_nanosleep)
j t9 j t9
move ra, t0 move ra, t0
.set pop .set pop
END(clock_nanosleep) END(__clock_nanosleep)
.hidden __clock_nanosleep

View File

@ -2,7 +2,7 @@
#include <private/bionic_asm.h> #include <private/bionic_asm.h>
ENTRY(clock_nanosleep) ENTRY(__clock_nanosleep)
pushl %ebx pushl %ebx
.cfi_def_cfa_offset 8 .cfi_def_cfa_offset 8
.cfi_rel_offset ebx, 0 .cfi_rel_offset ebx, 0
@ -33,4 +33,4 @@ ENTRY(clock_nanosleep)
popl %ecx popl %ecx
popl %ebx popl %ebx
ret ret
END(clock_nanosleep) END(__clock_nanosleep)

View File

@ -2,7 +2,7 @@
#include <private/bionic_asm.h> #include <private/bionic_asm.h>
ENTRY(clock_nanosleep) ENTRY(__clock_nanosleep)
movq %rcx, %r10 movq %rcx, %r10
movl $__NR_clock_nanosleep, %eax movl $__NR_clock_nanosleep, %eax
syscall syscall
@ -13,4 +13,5 @@ ENTRY(clock_nanosleep)
call __set_errno_internal call __set_errno_internal
1: 1:
ret ret
END(clock_nanosleep) END(__clock_nanosleep)
.hidden __clock_nanosleep

View File

@ -0,0 +1,38 @@
/*
* Copyright (C) 2014 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 "private/ErrnoRestorer.h"
extern "C" int __clock_nanosleep(clockid_t, int, const timespec*, timespec*);
int clock_nanosleep(clockid_t clock_id, int flags, const timespec* in, timespec* out) {
ErrnoRestorer errno_restorer;
return (__clock_nanosleep(clock_id, flags, in, out) == 0) ? 0 : errno;
}

View File

@ -85,8 +85,10 @@ extern void tzset(void) __LIBC_ABI_PUBLIC__;
extern clock_t clock(void) __LIBC_ABI_PUBLIC__; extern clock_t clock(void) __LIBC_ABI_PUBLIC__;
extern int clock_getres(int, struct timespec*) __LIBC_ABI_PUBLIC__; extern int clock_getres(clockid_t, struct timespec*) __LIBC_ABI_PUBLIC__;
extern int clock_gettime(int, struct timespec*) __LIBC_ABI_PUBLIC__; extern int clock_gettime(clockid_t, struct timespec*) __LIBC_ABI_PUBLIC__;
extern int clock_nanosleep(clockid_t, int, const struct timespec*, struct timespec*) __LIBC_ABI_PUBLIC__;
extern int clock_settime(clockid_t, const struct timespec*) __LIBC_ABI_PUBLIC__;
extern int timer_create(int, struct sigevent*, timer_t*) __LIBC_ABI_PUBLIC__; extern int timer_create(int, struct sigevent*, timer_t*) __LIBC_ABI_PUBLIC__;
extern int timer_delete(timer_t) __LIBC_ABI_PUBLIC__; extern int timer_delete(timer_t) __LIBC_ABI_PUBLIC__;

View File

@ -412,3 +412,16 @@ TEST(time, clock) {
clock_t t1 = clock(); clock_t t1 = clock();
ASSERT_LT(t1 - t0, CLOCKS_PER_SEC / 1000); ASSERT_LT(t1 - t0, CLOCKS_PER_SEC / 1000);
} }
TEST(time, clock_settime) {
errno = 0;
timespec ts;
ASSERT_EQ(-1, clock_settime(-1, &ts));
ASSERT_EQ(EINVAL, errno);
}
TEST(time, clock_nanosleep) {
timespec in;
timespec out;
ASSERT_EQ(EINVAL, clock_nanosleep(-1, 0, &in, &out));
}