[MPlayer-dev-eng] [PATCH] use pthreads for cache on cygwin

Reimar Döffinger Reimar.Doeffinger at stud.uni-karlsruhe.de
Fri Nov 14 20:27:55 CET 2008


Hello,
the "native" thread API for cygwin is pthreads, the threading API we
currently use for MinGW is not available for it (my fault for not
checking), and the one we used before to the best of my knowledge would
have memleaks on cygwin, too.
Thus I propose attached patch which uses pthread_create etc.
It depends on the PTHREAD_CACHE define so it will be easy to activate on
other systems, too (I tested it on Linux, someone on -users tested it
for cygwin), but for now will only be used on Cygwin.
Any objections to it?

Greetings,
Reimar Döffinger
-------------- next part --------------
Index: stream/cache2.c
===================================================================
--- stream/cache2.c	(revision 27905)
+++ stream/cache2.c	(working copy)
@@ -16,15 +16,22 @@
 #include <sys/types.h>
 #include <unistd.h>
 
+#ifdef __CYGWIN__
+#define PTHREAD_CACHE 1
+#endif
+
 #include "osdep/shmem.h"
 #include "osdep/timer.h"
-#if defined(__MINGW32__) || defined(__CYGWIN__)
+#if defined(__MINGW32__)
 #include <windows.h>
 static void ThreadProc( void *s );
 #elif defined(__OS2__)
 #define INCL_DOS
 #include <os2.h>
 static void ThreadProc( void *s );
+#elif defined(PTHREAD_CACHE)
+#include <pthread.h>
+static void *ThreadProc(void *s);
 #else
 #include <sys/wait.h>
 #endif
@@ -244,7 +252,7 @@
 
 cache_vars_t* cache_init(int size,int sector){
   int num;
-#if !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(__OS2__)
+#if !defined(__MINGW32__) && !defined(PTHREAD_CACHE) && !defined(__OS2__)
   cache_vars_t* s=shmem_alloc(sizeof(cache_vars_t));
 #else
   cache_vars_t* s=malloc(sizeof(cache_vars_t));
@@ -258,14 +266,14 @@
   }//32kb min_size
   s->buffer_size=num*sector;
   s->sector_size=sector;
-#if !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(__OS2__)
+#if !defined(__MINGW32__) && !defined(PTHREAD_CACHE) && !defined(__OS2__)
   s->buffer=shmem_alloc(s->buffer_size);
 #else
   s->buffer=malloc(s->buffer_size);
 #endif
 
   if(s->buffer == NULL){
-#if !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(__OS2__)
+#if !defined(__MINGW32__) && !defined(PTHREAD_CACHE) && !defined(__OS2__)
     shmem_free(s,sizeof(cache_vars_t));
 #else
     free(s);
@@ -281,14 +289,14 @@
 void cache_uninit(stream_t *s) {
   cache_vars_t* c = s->cache_data;
   if(!s->cache_pid) return;
-#if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
+#if defined(__MINGW32__) || defined(PTHREAD_CACHE) || defined(__OS2__)
   cache_do_control(s, -2, NULL);
 #else
   kill(s->cache_pid,SIGKILL);
   waitpid(s->cache_pid,NULL,0);
 #endif
   if(!c) return;
-#if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
+#if defined(__MINGW32__) || defined(PTHREAD_CACHE) || defined(__OS2__)
   free(c->stream);
   free(c->buffer);
   free(s->cache_data);
@@ -329,17 +337,19 @@
      min = s->buffer_size - s->fill_limit;
   }
   
-#if !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(__OS2__)
+#if !defined(__MINGW32__) && !defined(PTHREAD_CACHE) && !defined(__OS2__)
   if((stream->cache_pid=fork())){
 #else
   {
     stream_t* stream2=malloc(sizeof(stream_t));
     memcpy(stream2,s->stream,sizeof(stream_t));
     s->stream=stream2;
-#if defined(__MINGW32__) || defined(__CYGWIN__)
+#if defined(__MINGW32__)
     stream->cache_pid = _beginthread( ThreadProc, 0, s );
+#elif defined(__OS2__)
+    stream->cache_pid = _beginthread( ThreadProc, NULL, 256 * 1024, s );
 #else
-    stream->cache_pid = _beginthread( ThreadProc, NULL, 256 * 1024, s );
+    pthread_create(&stream->cache_pid, NULL, ThreadProc, s);
 #endif
 #endif
     // wait until cache is filled at least prefill_init %
@@ -358,10 +368,14 @@
     return 1; // parent exits
   }
   
-#if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
+#if defined(__MINGW32__) || defined(PTHREAD_CACHE) || defined(__OS2__)
 }
+#ifdef PTHREAD_CACHE
+static void *ThreadProc( void *s ){
+#else
 static void ThreadProc( void *s ){
 #endif
+#endif
   
 #ifdef CONFIG_GUI
   use_gui = 0; // mp_msg may not use gui stuff in forked code
@@ -374,9 +388,12 @@
     }
 //	 cache_stats(s->cache_data);
   } while (cache_execute_control(s));
-#if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
+#if defined(__MINGW32__) || defined(__OS2__)
   _endthread();
 #endif
+#ifdef PTHREAD_CACHE
+  return NULL;
+#endif
 }
 
 int cache_stream_fill_buffer(stream_t *s){


More information about the MPlayer-dev-eng mailing list