Skip to content

Commit ccd4694

Browse files
sgngitster
authored andcommitted
date.c: switch to reentrant {gm,local}time_r
Originally, git was intended to be single-thread executable. `gmtime(3)' and `localtime(3)' can be used in such codebase for cleaner code. Overtime, we're employing multithread in our code base. Let's phase out `gmtime(3)' and `localtime(3)' in favour of `gmtime_r(3)' and `localtime_r(3)'. Signed-off-by: Doan Tran Cong Danh <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d9f6f3b commit ccd4694

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

date.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,16 @@ static time_t gm_time_t(timestamp_t time, int tz)
6464
* thing, which means that tz -0100 is passed in as the integer -100,
6565
* even though it means "sixty minutes off"
6666
*/
67-
static struct tm *time_to_tm(timestamp_t time, int tz)
67+
static struct tm *time_to_tm(timestamp_t time, int tz, struct tm *tm)
6868
{
6969
time_t t = gm_time_t(time, tz);
70-
return gmtime(&t);
70+
return gmtime_r(&t, tm);
7171
}
7272

73-
static struct tm *time_to_tm_local(timestamp_t time)
73+
static struct tm *time_to_tm_local(timestamp_t time, struct tm *tm)
7474
{
7575
time_t t = time;
76-
return localtime(&t);
76+
return localtime_r(&t, tm);
7777
}
7878

7979
/*
@@ -283,6 +283,7 @@ static void show_date_normal(struct strbuf *buf, timestamp_t time, struct tm *tm
283283
const char *show_date(timestamp_t time, int tz, const struct date_mode *mode)
284284
{
285285
struct tm *tm;
286+
struct tm tmbuf = { 0 };
286287
struct tm human_tm = { 0 };
287288
int human_tz = -1;
288289
static struct strbuf timebuf = STRBUF_INIT;
@@ -318,11 +319,11 @@ const char *show_date(timestamp_t time, int tz, const struct date_mode *mode)
318319
}
319320

320321
if (mode->local)
321-
tm = time_to_tm_local(time);
322+
tm = time_to_tm_local(time, &tmbuf);
322323
else
323-
tm = time_to_tm(time, tz);
324+
tm = time_to_tm(time, tz, &tmbuf);
324325
if (!tm) {
325-
tm = time_to_tm(0, 0);
326+
tm = time_to_tm(0, 0, &tmbuf);
326327
tz = 0;
327328
}
328329

@@ -959,10 +960,11 @@ void datestamp(struct strbuf *out)
959960
{
960961
time_t now;
961962
int offset;
963+
struct tm tm = { 0 };
962964

963965
time(&now);
964966

965-
offset = tm_to_time_t(localtime(&now)) - now;
967+
offset = tm_to_time_t(localtime_r(&now, &tm)) - now;
966968
offset /= 60;
967969

968970
date_string(now, offset, out);

0 commit comments

Comments
 (0)