clock_gettime.c 876 字节
Newer Older
R
Rich Felker 已提交
1
#include <time.h>
2 3
#include <errno.h>
#include <stdint.h>
R
Rich Felker 已提交
4
#include "syscall.h"
5
#include "libc.h"
6
#include "atomic.h"
R
Rich Felker 已提交
7

8
static int sc_clock_gettime(clockid_t clk, struct timespec *ts)
R
Rich Felker 已提交
9
{
10
	int r = __syscall(SYS_clock_gettime, clk, ts);
11 12 13
	if (!r) return r;
	if (r == -ENOSYS) {
		if (clk == CLOCK_REALTIME) {
14
			__syscall(SYS_gettimeofday, ts, 0);
15 16 17 18 19 20 21
			ts->tv_nsec = (int)ts->tv_nsec * 1000;
			return 0;
		}
		r = -EINVAL;
	}
	errno = -r;
	return -1;
R
Rich Felker 已提交
22
}
23

24
void *__vdsosym(const char *, const char *);
25 26 27

int __clock_gettime(clockid_t clk, struct timespec *ts)
{
28
#ifdef VDSO_CGT_SYM
29
	static int (*volatile cgt)(clockid_t, struct timespec *);
30 31 32 33 34 35 36 37 38
	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
39 40
}

41
weak_alias(__clock_gettime, clock_gettime);