[MPlayer-dev-eng] [PATCH] Non-blocking mp_msg

Paul Pedaal paul.pedaal at mail.ee
Tue Jun 7 18:01:59 CEST 2005


There is a lot of fflush() in mp_msg making mplayer block when it can't write the status line to stdout for some reason. One can work around it by using the -quiet option, but I thought i'd try to fix it in the source.

So I replaced the fflushes for stdout with my own nbfflush function that does a poll() and flushes only when it would be done without blocking.

Mind that I am not a very experienced programmer and there may be something fundamentally wrong here. Thread safety maybe? At least it didn't dump core on my machine :P

This is from "diff -Nuw mp_msg.old.c mp_msg.c" against the latest CVS snapshot of MPlayer.

---------------------

--- mp_msg.old.c        2005-06-07 18:41:59.000000000 +0300
+++ mp_msg.c    2005-06-07 18:44:48.000000000 +0300
@@ -4,6 +4,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdarg.h>
+#include <poll.h>

 #include "config.h"

@@ -34,15 +35,19 @@
 #ifdef MP_DEBUG
     fprintf(stdout, "Using GNU internationalization\n");
     fprintf(stdout, "Original domain: %s\n", textdomain(NULL));
+#if 0
     fprintf(stdout, "Original dirname: %s\n", bindtextdomain(textdomain(NULL),NULL));
 #endif
     bindtextdomain("mplayer", PREFIX"/share/locale");
     textdomain("mplayer");
 #ifdef MP_DEBUG
     fprintf(stdout, "Current domain: %s\n", textdomain(NULL));
+#if 0
     fprintf(stdout, "Current dirname: %s\n\n", bindtextdomain(textdomain(NULL),NULL));
 #endif
 #endif
+#endif
+#endif
     mp_msg_set_level(MSGL_STATUS);
 }

@@ -58,6 +63,17 @@
     return lev <= mp_msg_levels[mod];
 }

+int nbfflush(FILE* stream){
+    struct pollfd* fds;
+
+    fds[0].fd = fileno(stream);
+    fds[0].events = POLLWRNORM;
+    poll(fds, 1, 0);
+    if (fds[0].revents && POLLWRNORM) {
+         fflush(stream);
+    }
+}
+
 void mp_msg_c( int x, const char *format, ... ){
 #if 1
     va_list va;
@@ -112,7 +128,7 @@
     if ((x & 255) <= MSGL_WARN){
        fprintf(stderr, "%s", tmp);fflush(stderr);
     } else {
-       printf("%s", tmp);fflush(stdout);
+       printf("%s", tmp);nbfflush(stdout);
     }

 #else
@@ -136,12 +152,12 @@
                   break;
        case MSGL_WARN:
               fprintf( stderr, "%s",tmp );
-             fflush(stdout);
+             nbfflush(stdout);
               gtkMessageBox( GTK_MB_WARNING|GTK_MB_SIMPLE,tmp );
                   break;
        default:
               fprintf(stderr, "%s",tmp );
-             fflush(stdout);
+             nbfflush(stdout);
       }
     } else
 #endif
@@ -150,9 +166,18 @@
       vfprintf(stderr,format, va);
       fflush(stderr);
     } else {
-//     printf("%%%%%% ");
+       printf("%%%%%% ");
       vfprintf(stderr,format, va);
-      fflush(stdout);
+      nbfflush(stdout);
+
+      fds[0].fd = fileno(stdout);
+      fds[0].events = POLLWRNORM;
+
+      poll(fds, 1, 0);
+      if (fds[0].revents && POLLWRNORM) {
+        nbfflush(stdout);
+      }
+
     }
     va_end(va);
 #endif
@@ -170,7 +195,7 @@
     va_start(va, format);
     vfprintf(mp_msg_file[x>>8],format, va);
     if(mp_msg_last_file!=mp_msg_file[x>>8]){
-       fflush(mp_msg_file[x>>8]);
+        nbfflush(mp_msg_file[x>>8]);
        mp_msg_last_file=mp_msg_file[x>>8];
     }
     va_end(va);


More information about the MPlayer-dev-eng mailing list