[FFmpeg-devel] [RFC] avoid non-threadsafe localtime, gmtime

Reimar Döffinger Reimar.Doeffinger
Sat Feb 7 12:02:03 CET 2009


Hello,
attached patch would be the first step to get rid of the non-threadsafe
localtime and gmtime functions.
It should work on all POSIX and current Windows versions.
For anything else it would still be a simple hack to add support to
fallback to the non-threadsafe functions, only libavformat then would
not be thread-safe which keeps the probability of issues quite low.

Greetings,
Reimar D?ffinger
-------------- next part --------------
Index: libavformat/utils.c
===================================================================
--- libavformat/utils.c	(revision 16989)
+++ libavformat/utils.c	(working copy)
@@ -2874,6 +2892,26 @@
     return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
 }
 
+struct tm *ff_localtime(const time_t *timep, struct tm *result)
+{
+#ifdef __MINGW32__
+    localtime_s(result, timep);
+    return result;
+#else
+    return localtime_r(timep, result);
+#endif
+}
+
+struct tm *ff_gmtime(const time_t *timep, struct tm *result)
+{
+#ifdef __MINGW32__
+    _gmtime_s(result, timep);
+    return result;
+#else
+    return gmtime_r(timep, result);
+#endif
+}
+
 int64_t parse_date(const char *datestr, int duration)
 {
     const char *p;
@@ -2920,9 +2958,9 @@
          * current year-month-day time */
         if (!q) {
             if (is_utc) {
-                dt = *gmtime(&now);
+                ff_gmtime(&now, &dt);
             } else {
-                dt = *localtime(&now);
+                ff_localtime(&now, &dt);
             }
             dt.tm_hour = dt.tm_min = dt.tm_sec = 0;
         } else {



More information about the ffmpeg-devel mailing list