@matheusbsbs / matheus-bagliano / commits / ccd469450a
Fork 0

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 <congdanhqx@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Doan Tran Cong Danh committed Nov 28, 2019 at 19:25 UTC ccd469450aaf62e6d0ce41a9738823d19d749a78
1 file changed +10 -8
date.c
+10 -8
@@ -64,16 +64,16 @@ static time_t gm_time_t(timestamp_t time, int tz)
64 * thing, which means that tz -0100 is passed in as the integer -100,
65 * even though it means "sixty minutes off"
66 */
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)
68 {
69 time_t t = gm_time_t(time, tz);
70 - return gmtime(&t);
70 + return gmtime_r(&t, tm);
71 }
72
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)
74 {
75 time_t t = time;
76 - return localtime(&t);
76 + return localtime_r(&t, tm);
77 }
78
79 /*
@@ -283,6 +283,7 @@ static void show_date_normal(struct strbuf *buf, timestamp_t time, struct tm *tm
283 const char *show_date(timestamp_t time, int tz, const struct date_mode *mode)
284 {
285 struct tm *tm;
286 + struct tm tmbuf = { 0 };
287 struct tm human_tm = { 0 };
288 int human_tz = -1;
289 static struct strbuf timebuf = STRBUF_INIT;
@@ -318,11 +319,11 @@ const char *show_date(timestamp_t time, int tz, const struct date_mode *mode)
319 }
320
321 if (mode->local)
321 - tm = time_to_tm_local(time);
322 + tm = time_to_tm_local(time, &tmbuf);
323 else
323 - tm = time_to_tm(time, tz);
324 + tm = time_to_tm(time, tz, &tmbuf);
325 if (!tm) {
325 - tm = time_to_tm(0, 0);
326 + tm = time_to_tm(0, 0, &tmbuf);
327 tz = 0;
328 }
329
@@ -959,10 +960,11 @@ void datestamp(struct strbuf *out)
960 {
961 time_t now;
962 int offset;
963 + struct tm tm = { 0 };
964
965 time(&now);
966
965 - offset = tm_to_time_t(localtime(&now)) - now;
967 + offset = tm_to_time_t(localtime_r(&now, &tm)) - now;
968 offset /= 60;
969
970 date_string(now, offset, out);