]> git.rm.cloudns.org Git - xonotic/darkplaces.git/commitdiff
sys: make TimeString thread-safe
authorbones_was_here <bones_was_here@xonotic.au>
Sat, 24 Aug 2024 18:22:08 +0000 (04:22 +1000)
committerbones_was_here <bones_was_here@xonotic.au>
Wed, 28 Aug 2024 13:10:44 +0000 (23:10 +1000)
Also avoids copying to an intermediate buffer in the warmer paths.

Signed-off-by: bones_was_here <bones_was_here@xonotic.au>
cl_demo.c
cl_main.c
cl_parse.c
cl_screen.c
console.c
sbar.c
sv_main.c
sys.h
sys_shared.c

index a2ee5f9257c5e7ea839aa10c16e8be91773c46b9..0f42a7d0d4a1fc3c6e8332f820a16407e4a8a332 100644 (file)
--- a/cl_demo.c
+++ b/cl_demo.c
@@ -520,7 +520,8 @@ static void CL_FinishTimeDemo (void)
        fpsmax = cls.td_onesecondmaxfps;
        // LadyHavoc: timedemo now prints out 7 digits of fraction, and min/avg/max
        Con_Printf("%i frames %5.7f seconds %5.7f fps, one-second fps min/avg/max: %.0f %.0f %.0f (%i seconds)\n", frames, time, totalfpsavg, fpsmin, fpsavg, fpsmax, cls.td_onesecondavgcount);
-       Log_Printf("benchmark.log", "date %s | enginedate %s | demo %s | commandline %s | run %d | result %i frames %5.7f seconds %5.7f fps, one-second fps min/avg/max: %.0f %.0f %.0f (%i seconds)\n", Sys_TimeString("%Y-%m-%d %H:%M:%S"), engineversion, cls.demoname, cmdline.string, benchmark_runs + 1, frames, time, totalfpsavg, fpsmin, fpsavg, fpsmax, cls.td_onesecondavgcount);
+       Sys_TimeString(vabuf, sizeof(vabuf), "%Y-%m-%d %H:%M:%S");
+       Log_Printf("benchmark.log", "date %s | enginedate %s | demo %s | commandline %s | run %d | result %i frames %5.7f seconds %5.7f fps, one-second fps min/avg/max: %.0f %.0f %.0f (%i seconds)\n", vabuf, engineversion, cls.demoname, cmdline.string, benchmark_runs + 1, frames, time, totalfpsavg, fpsmin, fpsavg, fpsmax, cls.td_onesecondavgcount);
        if (Sys_CheckParm("-benchmark"))
        {
                ++benchmark_runs;
index 71443f14c0d69ba19c30252bb74aa2ab76980fb0..1792141051135167fa2b7923d2b43030c0eebee7 100644 (file)
--- a/cl_main.c
+++ b/cl_main.c
@@ -2310,6 +2310,8 @@ static void CL_Locs_Save_f(cmd_state_t *cmd)
        cl_locnode_t *loc;
        qfile_t *outfile;
        char locfilename[MAX_QPATH];
+       char timestring[128];
+
        if (!cl.locnodes)
        {
                Con_Printf("No loc points/boxes exist!\n");
@@ -2332,7 +2334,8 @@ static void CL_Locs_Save_f(cmd_state_t *cmd)
                        break;
        if (loc)
        {
-               FS_Printf(outfile, "// %s %s saved by %s\n// x,y,z,x,y,z,\"name\"\n\n", locfilename, Sys_TimeString("%Y-%m-%d"), engineversion);
+               Sys_TimeString(timestring, sizeof(timestring), "%Y-%m-%d");
+               FS_Printf(outfile, "// %s %s saved by %s\n// x,y,z,x,y,z,\"name\"\n\n", locfilename, timestring, engineversion);
                for (loc = cl.locnodes;loc;loc = loc->next)
                        if (VectorCompare(loc->mins, loc->maxs))
                                break;
index b5d0cd4fbec8f2379b5ce91525c0e4af309cca39..30c455f521bdf61788a9de56a4406484fe82689a 100644 (file)
@@ -1930,7 +1930,8 @@ static void CL_ParseServerInfo (void)
                        }
 
                        // start a new demo file
-                       dpsnprintf (demofile, sizeof(demofile), "%s_%s.dem", Sys_TimeString (cl_autodemo_nameformat.string), cl.worldbasename);
+                       Sys_TimeString(vabuf, sizeof(vabuf), cl_autodemo_nameformat.string);
+                       dpsnprintf (demofile, sizeof(demofile), "%s_%s.dem", vabuf, cl.worldbasename);
 
                        Con_Printf ("Auto-recording to %s.\n", demofile);
 
index 7604fda6429beef3d7645e6b3266d601cb50670f..bf4c3d194566d8a87ea81d49ce4543b5811e1ab2 100644 (file)
@@ -959,10 +959,11 @@ void SCR_ScreenShot_f(cmd_state_t *cmd)
                int shotnumber100;
 
                // TODO maybe make capturevideo and screenshot use similar name patterns?
+               Sys_TimeString(vabuf, sizeof(vabuf), "%Y%m%d%H%M%S");
                if (scr_screenshot_name_in_mapdir.integer && cl.worldbasename[0])
-                       dpsnprintf(prefix_name, sizeof(prefix_name), "%s/%s%s", cl.worldbasename, scr_screenshot_name.string, Sys_TimeString("%Y%m%d%H%M%S"));
+                       dpsnprintf(prefix_name, sizeof(prefix_name), "%s/%s%s", cl.worldbasename, scr_screenshot_name.string, vabuf);
                else
-                       dpsnprintf(prefix_name, sizeof(prefix_name), "%s%s", scr_screenshot_name.string, Sys_TimeString("%Y%m%d%H%M%S"));
+                       dpsnprintf(prefix_name, sizeof(prefix_name), "%s%s", scr_screenshot_name.string, vabuf);
 
                // find a file name to save it to
                for (shotnumber100 = 0;shotnumber100 < 100;shotnumber100++)
@@ -981,10 +982,11 @@ void SCR_ScreenShot_f(cmd_state_t *cmd)
        else
        {
                // TODO maybe make capturevideo and screenshot use similar name patterns?
+               Sys_TimeString(vabuf, sizeof(vabuf), scr_screenshot_name.string);
                if (scr_screenshot_name_in_mapdir.integer && cl.worldbasename[0])
-                       dpsnprintf(prefix_name, sizeof(prefix_name), "%s/%s", cl.worldbasename, Sys_TimeString(scr_screenshot_name.string));
+                       dpsnprintf(prefix_name, sizeof(prefix_name), "%s/%s", cl.worldbasename, vabuf);
                else
-                       dpsnprintf(prefix_name, sizeof(prefix_name), "%s", Sys_TimeString(scr_screenshot_name.string));
+                       dpsnprintf(prefix_name, sizeof(prefix_name), "%s", vabuf);
 
                // if prefix changed, gamedir or map changed, reset the shotnumber so
                // we scan again
@@ -1040,6 +1042,8 @@ static void SCR_CaptureVideo_BeginVideo(void)
        double r, g, b;
        unsigned int i;
        int width = cl_capturevideo_width.integer, height = cl_capturevideo_height.integer;
+       char timestring[128];
+
        if (cls.capturevideo.active)
                return;
        memset(&cls.capturevideo, 0, sizeof(cls.capturevideo));
@@ -1075,7 +1079,8 @@ static void SCR_CaptureVideo_BeginVideo(void)
        cls.capturevideo.realtime = cl_capturevideo_realtime.integer != 0;
        cls.capturevideo.screenbuffer = (unsigned char *)Mem_Alloc(tempmempool, vid.mode.width * vid.mode.height * 4);
        cls.capturevideo.outbuffer = (unsigned char *)Mem_Alloc(tempmempool, width * height * (4+4) + 18);
-       dpsnprintf(cls.capturevideo.basename, sizeof(cls.capturevideo.basename), "video/%s%03i", Sys_TimeString(cl_capturevideo_nameformat.string), cl_capturevideo_number.integer);
+       Sys_TimeString(timestring, sizeof(timestring), cl_capturevideo_nameformat.string);
+       dpsnprintf(cls.capturevideo.basename, sizeof(cls.capturevideo.basename), "video/%s%03i", timestring, cl_capturevideo_number.integer);
        Cvar_SetValueQuick(&cl_capturevideo_number, cl_capturevideo_number.integer + 1);
 
        /*
index c1c3f2f1a686e2bc598634ee4163c6fa4315575c..f46eddb5fb8bab5ce1863613f378dfab85eb24a6 100644 (file)
--- a/console.c
+++ b/console.c
@@ -1166,7 +1166,6 @@ void Con_MaskPrint(unsigned additionalmask, const char *msg)
                // if this is the beginning of a new line, print timestamp
                if (index == 0)
                {
-                       const char *timestamp = timestamps.integer ? Sys_TimeString(timeformat.string) : "";
                        // reset the color
                        // FIXME: 1. perhaps we should use a terminal system 2. use a constant instead of 7!
                        line[index++] = STRING_COLOR_TAG;
@@ -1198,9 +1197,8 @@ void Con_MaskPrint(unsigned additionalmask, const char *msg)
                                Con_Rcon_AddChar(*msg);
                        }
                        // store timestamp
-                       for (;*timestamp;index++, timestamp++)
-                               if (index < (int)sizeof(line) - 2)
-                                       line[index] = *timestamp;
+                       if (timestamps.integer)
+                               index += Sys_TimeString(&line[index], sizeof(line) - index, timeformat.string);
                        // add the mask
                        mask |= additionalmask;
                }
diff --git a/sbar.c b/sbar.c
index 43b64b9e3396f827d9c64aeffa55c27614b21025..8c0f0cf5e67e500d333a7184dc90237c1fc1f81a 100644 (file)
--- a/sbar.c
+++ b/sbar.c
@@ -1142,12 +1142,12 @@ void Sbar_ShowFPS(void)
        }
        if (cl_showtime.integer)
        {
-               dp_strlcpy(timestring, Sys_TimeString(cl_showtime_format.string), sizeof(timestring));
+               Sys_TimeString(timestring, sizeof(timestring), cl_showtime_format.string);
                fps_strings++;
        }
        if (cl_showdate.integer)
        {
-               dp_strlcpy(datestring, Sys_TimeString(cl_showdate_format.string), sizeof(datestring));
+               Sys_TimeString(datestring, sizeof(datestring), cl_showdate_format.string);
                fps_strings++;
        }
        if (cl_showblur.integer)
index 7d5c45ad95dc40ee6f7fac7e22ddc0078d51961e..7778f117e4e03af9ebe34b7e64684889031fd8c7 100644 (file)
--- a/sv_main.c
+++ b/sv_main.c
@@ -812,6 +812,7 @@ void SV_SendServerinfo (client_t *client)
        {
                char demofile[MAX_OSPATH];
                char ipaddress[MAX_QPATH];
+               char timestring[128];
                size_t j;
 
                // start a new demo file
@@ -819,7 +820,9 @@ void SV_SendServerinfo (client_t *client)
                for(j = 0; ipaddress[j]; ++j)
                        if(!isalnum(ipaddress[j]))
                                ipaddress[j] = '-';
-               dpsnprintf (demofile, sizeof(demofile), "%s_%s_%d_%s.dem", Sys_TimeString (sv_autodemo_perclient_nameformat.string), sv.worldbasename, PRVM_NUM_FOR_EDICT(client->edict), ipaddress);
+               Sys_TimeString(timestring, sizeof(timestring), sv_autodemo_perclient_nameformat.string);
+               dpsnprintf(demofile, sizeof(demofile), "%s_%s_%d_%s.dem", timestring,
+                               sv.worldbasename, PRVM_NUM_FOR_EDICT(client->edict), ipaddress);
 
                SV_StartDemoRecording(client, demofile, -1);
        }
diff --git a/sys.h b/sys.h
index c2f238e21973677622703296beaf71d0e239533f..4c46754f5aeade66dce2bc1d265f653c72c43636 100644 (file)
--- a/sys.h
+++ b/sys.h
@@ -202,7 +202,7 @@ void Sys_Init_Commands (void);
 
 
 /// \returns current timestamp
-char *Sys_TimeString(const char *timeformat);
+size_t Sys_TimeString(char buf[], size_t bufsize, const char *timeformat);
 
 //
 // system IO interface (these are the sys functions that need to be implemented in a new driver atm)
index 0d6cf410d51eb04cfbccfa9b61d74d7cfe792980..ddf905df046b8c58720a02f794d606f2388441da 100644 (file)
 
 sys_t sys;
 
-static char sys_timestring[128];
-char *Sys_TimeString(const char *timeformat)
+
+size_t Sys_TimeString(char buf[], size_t bufsize, const char *timeformat)
 {
        time_t mytime = time(NULL);
+       size_t strlen;
 #if _MSC_VER >= 1400
        struct tm mytm;
        localtime_s(&mytm, &mytime);
-       strftime(sys_timestring, sizeof(sys_timestring), timeformat, &mytm);
+       strlen = strftime(buf, bufsize, timeformat, &mytm);
 #else
-       strftime(sys_timestring, sizeof(sys_timestring), timeformat, localtime(&mytime));
+       strlen = strftime(buf, bufsize, timeformat, localtime(&mytime));
 #endif
-       return sys_timestring;
+       if (!strlen) // means the array contents are undefined (but it's not always an error)
+               buf[0] = '\0'; // better fix it
+       return strlen;
 }