Skip to content

Commit 66f9bc1

Browse files
committed
[libc] Add clock_gettime for Darwin
This patch adds support for clock_gettime for Darwin. Darwin syscall 'gettimeofday' is used to query the time from the system. Many headers in llvm-libc-types, namely clockid_t, struct_timespec, struct_timeval, suseconds_t, time_t_32, time_t_64, are modified to include header guards as Darwin has its own implementation of primitive types.
1 parent dbdaa36 commit 66f9bc1

File tree

15 files changed

+180
-0
lines changed

15 files changed

+180
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
add_header(
2+
time_macros
3+
HDR
4+
time-macros.h
5+
)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//===-- Definition of macros from time.h ---------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_MACROS_LINUX_TIME_MACROS_H
10+
#define LLVM_LIBC_MACROS_LINUX_TIME_MACROS_H
11+
12+
#include <_time.h>
13+
14+
#endif // LLVM_LIBC_MACROS_LINUX_TIME_MACROS_H

libc/include/llvm-libc-macros/time-macros.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include "linux/time-macros.h"
88
#elif defined(__ELF__)
99
#include "baremetal/time-macros.h"
10+
#elif defined(__APPLE__)
11+
#include "darwin/time-macros.h"
1012
#else
1113
#define CLOCKS_PER_SEC 1000000
1214
#endif

libc/include/llvm-libc-types/clockid_t.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
#ifndef LLVM_LIBC_TYPES_CLOCKID_T_H
1010
#define LLVM_LIBC_TYPES_CLOCKID_T_H
1111

12+
#if defined(__APPLE__)
13+
// Darwin provides its own defintion for clockid_t . Use that to prevent
14+
// redeclaration errors and correctness.
15+
#include <_time.h>
16+
#else
1217
typedef int clockid_t;
18+
#endif // __APPLE__
1319

1420
#endif // LLVM_LIBC_TYPES_CLOCKID_T_H

libc/include/llvm-libc-types/struct_timespec.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,18 @@
99
#ifndef LLVM_LIBC_TYPES_STRUCT_TIMESPEC_H
1010
#define LLVM_LIBC_TYPES_STRUCT_TIMESPEC_H
1111

12+
#if defined(__APPLE__)
13+
// Darwin provides its own definition for struct timespec. Include it directly
14+
// to ensure type compatibility and avoid redefinition errors.
15+
#include <sys/_types/_timespec.h>
16+
#else
1217
#include "time_t.h"
1318

1419
struct timespec {
1520
time_t tv_sec; /* Seconds. */
1621
/* TODO: BIG_ENDIAN may require padding. */
1722
long tv_nsec; /* Nanoseconds. */
1823
};
24+
#endif // __APPLE__
1925

2026
#endif // LLVM_LIBC_TYPES_STRUCT_TIMESPEC_H

libc/include/llvm-libc-types/struct_timeval.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@
1212
#include "suseconds_t.h"
1313
#include "time_t.h"
1414

15+
#if defined(__APPLE__)
16+
// Darwin provides its own definition for struct timeval. Include it directly
17+
// to ensure type compatibility and avoid redefinition errors.
18+
#include <sys/_types/_timeval.h>
19+
#else
1520
struct timeval {
1621
time_t tv_sec; // Seconds
1722
suseconds_t tv_usec; // Micro seconds
1823
};
24+
#endif // __APPLE__
1925

2026
#endif // LLVM_LIBC_TYPES_STRUCT_TIMEVAL_H

libc/include/llvm-libc-types/suseconds_t.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
// types...] and suseconds_t are no greater than the width of type long.
1515

1616
// The kernel expects 64 bit suseconds_t at least on x86_64.
17+
#if defined(__APPLE__)
18+
// Darwin provides its own definition for suseconds_t. Include it directly
19+
// to ensure type compatibility and avoid redefinition errors.
20+
#include <sys/_types/_suseconds_t.h>
21+
#else
1722
typedef long suseconds_t;
23+
#endif // __APPLE__
1824

1925
#endif // LLVM_LIBC_TYPES_SUSECONDS_T_H

libc/include/llvm-libc-types/time_t_32.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
#ifndef LLVM_LIBC_TYPES_TIME_T_32_H
1010
#define LLVM_LIBC_TYPES_TIME_T_32_H
1111

12+
#if defined(__APPLE__)
13+
// Darwin provides its own definition for time_t. Include it directly
14+
// to ensure type compatibility and avoid redefinition errors.
15+
#include <sys/_types/_time_t.h>
16+
#else
1217
typedef __INT32_TYPE__ time_t;
18+
#endif // __APPLE__
1319

1420
#endif // LLVM_LIBC_TYPES_TIME_T_32_H

libc/include/llvm-libc-types/time_t_64.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
#ifndef LLVM_LIBC_TYPES_TIME_T_64_H
1010
#define LLVM_LIBC_TYPES_TIME_T_64_H
1111

12+
#if defined(__APPLE__)
13+
// Darwin provides its own definition for time_t. Include it directly
14+
// to ensure type compatibility and avoid redefinition errors.
15+
#include <sys/_types/_time_t.h>
16+
#else
1217
typedef __INT64_TYPE__ time_t;
18+
#endif // __APPLE__
1319

1420
#endif // LLVM_LIBC_TYPES_TIME_T_64_H
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
add_object_library(
2+
clock_gettime
3+
SRCS
4+
clock_gettime.cpp
5+
HDRS
6+
../clock_gettime.h
7+
DEPENDS
8+
libc.src.__support.common
9+
libc.src.__support.error_or
10+
libc.hdr.types.struct_timeval
11+
libc.hdr.types.struct_timespec
12+
)

0 commit comments

Comments
 (0)