Skip to content

Commit c4693fe

Browse files
authored
Split up emscripten_time.c. NFC (#18906)
Also use the `weak` macros from musl's internal features.h Split out from #18905
1 parent 5cf021d commit c4693fe

File tree

4 files changed

+81
-71
lines changed

4 files changed

+81
-71
lines changed

system/lib/libc/emscripten_time.c

Lines changed: 9 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -15,77 +15,21 @@
1515

1616
// Replaces musl's __tz.c
1717

18-
__attribute__((__weak__)) long timezone = 0;
19-
__attribute__((__weak__)) int daylight = 0;
20-
__attribute__((__weak__)) char *tzname[2] = { 0, 0 };
18+
weak long timezone = 0;
19+
weak int daylight = 0;
20+
weak char *tzname[2] = { 0, 0 };
2121

22-
void _tzset_js(long* timezone, int* daylight, char** tzname);
23-
// Declare these functions `int` rather than time_t to avoid int64 at the wasm
24-
// boundary (avoids 64-bit complexity at the boundary when WASM_BIGINT is
25-
// missing).
26-
// TODO(sbc): Covert back to `time_t` before 2038 ...
27-
int _timegm_js(struct tm *tm);
28-
int _mktime_js(struct tm *tm);
29-
void _localtime_js(const time_t *restrict t, struct tm *restrict tm);
30-
void _gmtime_js(const time_t *restrict t, struct tm *restrict tm);
3122
double emscripten_get_now_res();
3223

33-
__attribute__((__weak__))
34-
void tzset() {
35-
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
36-
static _Atomic bool done_init = false;
37-
if (!done_init) {
38-
pthread_mutex_lock(&lock);
39-
if (!done_init) {
40-
_tzset_js(&timezone, &daylight, tzname);
41-
done_init = true;
42-
}
43-
pthread_mutex_unlock(&lock);
44-
}
45-
}
46-
47-
__attribute__((__weak__))
48-
time_t timegm(struct tm *tm) {
49-
tzset();
50-
return _timegm_js(tm);
51-
}
52-
53-
__attribute__((__weak__))
54-
time_t mktime(struct tm *tm) {
55-
tzset();
56-
return _mktime_js(tm);
57-
}
58-
59-
__attribute__((__weak__))
60-
struct tm *__localtime_r(const time_t *restrict t, struct tm *restrict tm) {
61-
tzset();
62-
_localtime_js(t, tm);
63-
// __localtime_js sets everything but the tmzone pointer
64-
tm->__tm_zone = tm->tm_isdst ? tzname[1] :tzname[0];
65-
return tm;
66-
}
67-
68-
__attribute__((__weak__))
69-
struct tm *__gmtime_r(const time_t *restrict t, struct tm *restrict tm) {
70-
tzset();
71-
_gmtime_js(t, tm);
72-
tm->tm_isdst = 0;
73-
tm->__tm_gmtoff = 0;
74-
tm->__tm_zone = "GMT";
75-
return tm;
76-
}
77-
78-
__attribute__((__weak__))
79-
clock_t __clock() {
24+
weak clock_t __clock() {
8025
static thread_local double start = 0;
8126
if (!start) {
8227
start = emscripten_date_now();
8328
}
8429
return (emscripten_date_now() - start) * (CLOCKS_PER_SEC / 1000);
8530
}
8631

87-
__attribute__((__weak__))
88-
time_t __time(time_t *t) {
32+
weak time_t __time(time_t *t) {
8933
double ret = emscripten_date_now() / 1000;
9034
if (t) {
9135
*t = ret;
@@ -97,8 +41,7 @@ extern bool _emscripten_get_now_is_monotonic();
9741
static thread_local bool checked_monotonic = false;
9842
static thread_local bool is_monotonic = 0;
9943

100-
__attribute__((__weak__))
101-
int __clock_gettime(clockid_t clk, struct timespec *ts) {
44+
weak int __clock_gettime(clockid_t clk, struct timespec *ts) {
10245
if (!checked_monotonic) {
10346
is_monotonic = _emscripten_get_now_is_monotonic();
10447
checked_monotonic = true;
@@ -120,8 +63,7 @@ int __clock_gettime(clockid_t clk, struct timespec *ts) {
12063
return 0;
12164
}
12265

123-
__attribute__((__weak__))
124-
int __clock_getres(clockid_t clk, struct timespec *ts) {
66+
weak int __clock_getres(clockid_t clk, struct timespec *ts) {
12567
if (!checked_monotonic) {
12668
is_monotonic = _emscripten_get_now_is_monotonic();
12769
checked_monotonic = true;
@@ -141,23 +83,19 @@ int __clock_getres(clockid_t clk, struct timespec *ts) {
14183
return 0;
14284
}
14385

144-
__attribute__((__weak__))
145-
int __gettimeofday(struct timeval *restrict tv, void *restrict tz) {
86+
weak int __gettimeofday(struct timeval *restrict tv, void *restrict tz) {
14687
double now_ms = emscripten_date_now();
14788
long long now_s = now_ms / 1000;
14889
tv->tv_sec = now_s; // seconds
14990
tv->tv_usec = (now_ms - (now_s * 1000)) * 1000; // nicroseconds
15091
return 0;
15192
}
15293

153-
__attribute__((__weak__))
154-
int dysize(int year) {
94+
weak int dysize(int year) {
15595
int leap = ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)));
15696
return leap ? 366 : 365;
15797
}
15898

159-
weak_alias(__gmtime_r, gmtime_r);
160-
weak_alias(__localtime_r, localtime_r);
16199
weak_alias(__time, time);
162100
weak_alias(__clock, clock);
163101
weak_alias(__clock_gettime, clock_gettime);

system/lib/libc/mktime.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2023 The Emscripten Authors. All rights reserved.
3+
* Emscripten is available under two separate licenses, the MIT license and the
4+
* University of Illinois/NCSA Open Source License. Both these licenses can be
5+
* found in the LICENSE file.
6+
*/
7+
#include <time.h>
8+
9+
// Declare these functions `int` rather than time_t to avoid int64 at the wasm
10+
// boundary (avoids 64-bit complexity at the boundary when WASM_BIGINT is
11+
// missing).
12+
// TODO(sbc): Covert back to `time_t` before 2038 ...
13+
int _timegm_js(struct tm *tm);
14+
int _mktime_js(struct tm *tm);
15+
void _localtime_js(const time_t *restrict t, struct tm *restrict tm);
16+
void _gmtime_js(const time_t *restrict t, struct tm *restrict tm);
17+
18+
weak time_t timegm(struct tm *tm) {
19+
tzset();
20+
return _timegm_js(tm);
21+
}
22+
23+
weak time_t mktime(struct tm *tm) {
24+
tzset();
25+
return _mktime_js(tm);
26+
}
27+
28+
weak struct tm *__localtime_r(const time_t *restrict t, struct tm *restrict tm) {
29+
tzset();
30+
_localtime_js(t, tm);
31+
// __localtime_js sets everything but the tmzone pointer
32+
tm->__tm_zone = tm->tm_isdst ? tzname[1] :tzname[0];
33+
return tm;
34+
}
35+
36+
weak struct tm *__gmtime_r(const time_t *restrict t, struct tm *restrict tm) {
37+
tzset();
38+
_gmtime_js(t, tm);
39+
tm->tm_isdst = 0;
40+
tm->__tm_gmtoff = 0;
41+
tm->__tm_zone = "GMT";
42+
return tm;
43+
}
44+
45+
weak_alias(__gmtime_r, gmtime_r);
46+
weak_alias(__localtime_r, localtime_r);

system/lib/libc/tzset.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2023 The Emscripten Authors. All rights reserved.
3+
* Emscripten is available under two separate licenses, the MIT license and the
4+
* University of Illinois/NCSA Open Source License. Both these licenses can be
5+
* found in the LICENSE file.
6+
*/
7+
#include <time.h>
8+
#include <stdbool.h>
9+
#include <pthread.h>
10+
11+
void _tzset_js(long* timezone, int* daylight, char** tzname);
12+
13+
weak void tzset() {
14+
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
15+
static _Atomic bool done_init = false;
16+
if (!done_init) {
17+
pthread_mutex_lock(&lock);
18+
if (!done_init) {
19+
_tzset_js(&timezone, &daylight, tzname);
20+
done_init = true;
21+
}
22+
pthread_mutex_unlock(&lock);
23+
}
24+
}

tools/system_libs.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,8 @@ def get_files(self):
11511151
'emscripten_mmap.c',
11521152
'emscripten_scan_stack.c',
11531153
'emscripten_time.c',
1154+
'mktime.c',
1155+
'tzset.c',
11541156
'kill.c',
11551157
'pthread_sigmask.c',
11561158
'raise.c',

0 commit comments

Comments
 (0)